3
D\}V                 @   sj   d Z ddlZddlZddlZddlmZ dZdZdZeZ	dZ
dZd	Zd
ZG dd deZG dd dZdS )z!common.py: common classes for ufw    N)debugufwz/lib/ufwz/usr/share/ufwz/etcz/usrz/sbinTc               @   s    e Zd ZdZdd Zdd ZdS )UFWErrorz$This class represents ufw exceptionsc             C   s
   || _ d S )N)value)selfr    r   ,/usr/lib/python3/dist-packages/ufw/common.py__init__#   s    zUFWError.__init__c             C   s
   t | jS )N)reprr   )r   r   r   r   __str__&   s    zUFWError.__str__N)__name__
__module____qualname____doc__r	   r   r   r   r   r   r   !   s   r   c               @   s   e Zd ZdZd9ddZd	d
 Zdd Zdd Zdd Zdd Z	d:ddZ
dd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8S );UFWRulez$This class represents firewall rulesany	0.0.0.0/0inF c
       
      C   s   d| _ d| _d| _d| _d| _d| _d| _d| _d| _d| _	d| _
d| _d| _d| _d| _d| _d| _|| _d| _yV| j| | j| | j| | j|d | j| | j| | j| | j|	 W n tk
r    Y nX d S )NFr   r   src)removeupdatedv6dstr   dportsportprotocolmultidappsappactionpositionlogtypeinterface_ininterface_out	directionforwardcomment
set_actionset_protocolset_portset_srcset_dstset_directionset_commentr   )
r   r    r   r   r   r   r   r%   r&   r'   r   r   r   r	   ,   s<    





zUFWRule.__init__c             C   s   | j  S )N)format_rule)r   r   r   r   r   O   s    zUFWRule.__str__c             C   sB   d|  }t | j}|j  x"|D ]}|d|| j| f 7 }q W |S )zPrint rule to stdoutz'%s'z, %s=%s)list__dict__sort)r   reskeyskr   r   r   _get_attribR   s    

zUFWRule._get_attribc             C   s   t | j| j}| j|_| j|_| j|_| j|_| j|_| j|_| j	|_	| j
|_
| j|_| j|_| j|_| j|_| j|_| j|_| j|_| j|_| j|_|S )zReturn a duplicate of a rule)r   r    r   r   r   r   r   r   r   r   r   r   r   r!   r"   r#   r$   r%   r&   r'   )r   Zruler   r   r   dup_rule[   s&    zUFWRule.dup_rulec             C   s  d}| j dkr|d| j  7 }| jdkr4|d| j 7 }| jdkrH|d7 }n|d| j 7 }| jr|d7 }| jdkr| jdkr|d| j 7 }|d7 }|d	| j 7 }n2| jdkr|d| j 7 }n| jdkr|d	| j 7 }| jd
kr| jdkr|d| j 7 }| j r| jdkr|d| j 7 }| jd
kr<| jdkr<|d| j 7 }| j r`| jdkr`|d| j 7 }d}| jdkrzd| j }| j	dkr|d| 7 }nT| j	dkr|d| 7 }| jdkr|d7 }n&| j	dkr|d| 7 }n|d| 7 }| j
dks | jdkrd}tjd}| j
dkr0|d|jd| j
 7 }| j
dkrP| jdkrP|d7 }| jdkrr|d|jd| j 7 }|d 7 }|d| 7 }|j S )!zFormat rule for later parsingr   z -i %sz -o %sr   z -p allz -p z -m multiportz
 --dports z
 --sports z	0.0.0.0/0z::/0z -d z	 --dport z -s z	 --sport _allowz -j ACCEPT%srejectz -j REJECT%sZtcpz --reject-with tcp-resetlimitz -j LIMIT%sz
 -j DROP%sz-m comment --comment ' Zdapp_z%20,Zsapp_')r#   r$   r   r   r   r   r   r   r"   r    r   r   recompilesubstrip)r   Zrule_strZlstrr'   Z	pat_spacer   r   r   r/   r   sd    








zUFWRule.format_rulec             C   sj   |j  jd}|d dks2|d dks2|d dkr>|d | _nd| _d}t|dkr\|d }| j| d	S )
zSets action of the ruler8   r   r9   r:   r;   Zdenyr      N)lowersplitr    lenset_logtype)r   r    tmpr"   r   r   r   r(      s    $zUFWRule.set_actionr   c       	      C   s  t d| }|dkrn|dkr*| jr*n|dkr<| jr<ntjd|sTtjd|r`t|nd|jd|jd d	krt|n@|jd}t|d
krd| _	d}x|D ]}tjd|r"d| _	|jd}x,|D ]$}t
|d
k st
|dkrt|qW t
|d t
|d
 krt|nztjd|rVt
|d
k sLt
|dkrt|nFtjd|rytj|}W n tk
r   t|Y nX nt||r|dt| 7 }qt|}qW |}|dkrt|| _n
t|| _dS )z:Sets port and location (destination or source) of the rulezBad port '%s'r   r   r   z^[,:]z[,:]$r=   :   rC   Tr   z	^\d+:\d+$i  r   z^\d+$z
^\w[\w\-]+N)r8   r   r   r?   matchr   countrE   rF   r   intsocketZgetservbyname	Exceptionstrr   r   )	r   portlocerr_msgportsrH   pZranqr   r   r   r*      sP    





zUFWRule.set_portc             C   s2   |t jjdg kr|| _ntd| }t|dS )zSets protocol of the ruler   zUnsupported protocol '%s'N)r   utilZsupported_protocolsr   r8   r   )r   r   rS   r   r   r   r)      s    zUFWRule.set_protocolc             C   s   | j rH| jr&| jdks | jdkr&d| _| jr| jdks@| jdkrd| _n@| jrh| jdksb| jdkrhd| _| jr| jdks| jdkrd| _dS )zAdjusts src and dst based on v6r   z	0.0.0.0/0z::/0N)r   r   r   )r   r   r   r   _fix_anywhere   s    zUFWRule._fix_anywherec             C   s   || _ | j  dS )zXSets whether this is ipv6 rule, and adjusts src and dst
           accordingly.
        N)r   rX   )r   r   r   r   r   set_v6  s    zUFWRule.set_v6c             C   sB   |j  }|dkr0tjj|d r0td}t||| _| j  dS )zSets source address of ruler   zBad source addressN)rD   r   rW   valid_addressr8   r   r   rX   )r   addrrH   rS   r   r   r   r+     s    zUFWRule.set_srcc             C   sB   |j  }|dkr0tjj|d r0td}t||| _| j  dS )z Sets destination address of ruler   zBad destination addressN)rD   r   rW   rZ   r8   r   r   rX   )r   r[   rH   rS   r   r   r   r,     s    zUFWRule.set_dstc             C   s   |dkr |dkr t d}t|dt|kr<t d}t|dt|krXt d}t|t|dkspt|d	krt d
}t|tt|dkrt d}t|tt|dkrt d}t|tjdt|st d}t||dkr|| _n|| _dS )zSets an interface for ruler   outzBad interface type!z+Bad interface name: reserved character: '!'rI   z/Bad interface name: can't use interface aliases.z..z)Bad interface name: can't use '.' or '..'r   z+Bad interface name: interface name is empty   z+Bad interface name: interface name too longz^[a-zA-Z0-9_\-\.\+,=%@]+$zBad interface nameN)r8   r   rP   rF   r?   rK   r#   r$   )r   Zif_typenamerS   r   r   r   set_interface'  s0    zUFWRule.set_interfacec             C   s@   t |dkr2tjdt | r2td| }t|t|| _dS )zSets the position of the rulez-1z^[0-9]+z,Insert position '%s' is not a valid positionN)rP   r?   rK   r8   r   rM   r!   )r   ZnumrS   r   r   r   set_positionW  s    zUFWRule.set_positionc             C   sD   |j  dks |j  dks |dkr,|j  | _ntd| }t|dS )zSets logtype of the rulelogzlog-allr   zInvalid log type '%s'N)rD   r"   r8   r   )r   r"   rS   r   r   r   rG   a  s
    zUFWRule.set_logtypec             C   s0   |dks|dkr|| _ ntd| }t|dS )zSets direction of the ruler   r\   zUnsupported direction '%s'N)r%   r8   r   )r   r%   rS   r   r   r   r-   j  s    zUFWRule.set_directionc             C   s   t jj| jS )zGet decoded comment of the rule)r   rW   Z
hex_decoder'   )r   r   r   r   get_commentr  s    zUFWRule.get_commentc             C   s
   || _ dS )zSets comment of the ruleN)r'   )r   r'   r   r   r   r.   v  s    zUFWRule.set_commentc             C   s   d}| j rVytjj| j | j\| _ }W n$ tk
rJ   td}t|Y nX |rV|| _| j	rytjj| j	| j\| _	}W n$ tk
r   td}t|Y nX |r|| _| j
r| j
jd}tjj| dj|| _
| jr| jjd}tjj| dj|| _dS )z&Normalize src and dst to standard formFz"Could not normalize source addressz'Could not normalize destination addressr=   N)r   r   rW   Znormalize_addressr   rO   r8   r   r   r   r   rE   Z
human_sortjoinr   )r   ZchangedrS   rT   r   r   r   	normalizez  s6    

zUFWRule.normalizec             C   s  |  s| rt  d| |f }| j|jkr6t| dS | j|jkrNt| dS | j|jkrft| dS | j|jkr~t| dS | j|jkrt| dS | j|jkrt| dS | j|jkrt| dS | j	|j	krt| dS | j
|j
krt| dS | j|jkrt| dS | j|jkr*t| dS | j|jkrDt| dS | j|jkr| j|jkr| j|jkrtd}t| dS | j|jkr| j|jkr| j|jkrtd}t| d	S td| j|j| j|j| j|jd }t| d
S )zCheck if rules match
        Return codes:
          0  match
          1  no match
         -1  match all but action, log-type and/or comment
         -2  match all but comment
        zNo match '%s' '%s'rC   zFound exact matchr   z$Found exact match, excepting comment   zZFound non-action/non-logtype/comment match (%(xa)s/%(ya)s/'%(xc)s' %(xl)s/%(yl)s/'%(yc)s'))ZxaZyaZxlZylZxcZyc)
ValueErrorr   r   r   r   r   r   r   r   r   r#   r$   r%   r&   r    r"   r'   r8   )xydbg_msgr   r   r   rK     sn    zUFWRule.matchc             C   s  dd }|  s| rt  | j|dkr,dS d| | j||jf }|jdkr^td| d  dS |j| jkrzt|d	  dS | j|jkr|jd
krtd|  dS |jd
kr|| j|j rtd|  dS |jdkr~| jdkr| j	| j
rn| j
|j
krd|j
krtd|  dS | j
|j
krd|j
kr| j|jkrtjj| j
|j
| j rtd| d| j
|j
f   dS n| jdkr| j|jkrtd| d| j|jf   dS ytjj|j| j}W n, tk
r   td| d|j   dS X |j
|kr2d|j
kr2td| d|j
|f   dS |j
|krd|j
kr| j|jkrtjj||j
| j rtd| d||j
f   dS | j|jkrtd| d| j
|j
f   dS td| | j||jf  dS )a  This will match if x is more specific than y. Eg, for protocol if x
           is tcp and y is all or for address if y is a network and x is a
           subset of y (where x is either an address or network). Returns:

            0  match
            1  no match
           -1  fuzzy match

           This is a fuzzy destination match, so source ports or addresses
           are not considered, and (currently) only incoming.
        c             S   s~   d| ksd| kr | |krdS dS xX|j dD ]J}| |kr<dS d|kr,|j d\}}t| t|kr,t| t|kr,dS q,W dS )z:Returns True if p is an exact match or within a multi ruler=   rI   TF)rE   rM   )Ztest_pZto_matchrQ   ZlowZhighr   r   r   _match_ports  s     z-UFWRule.fuzzy_dst_match.<locals>._match_portsr   z(No fuzzy match '%s (v6=%s)' '%s (v6=%s)'r   z(direction) z (not incoming)rC   z (forward does not match)r   z(protocol) z(dport) r   /z(dst) z ('%s' not in network '%s')z(interface) z (%s != %s)z %s does not existz(v6) z'(fuzzy match) '%s (v6=%s)' '%s (v6=%s)'ri   )rj   rK   r   r%   r   r&   r   r   r#   _is_anywherer   r   rW   Z
in_networkZget_ip_from_ifIOError)rk   rl   rn   rm   Zif_ipr   r   r   fuzzy_dst_match  sl    
(



&zUFWRule.fuzzy_dst_matchc             C   s   |dks|dkrdS dS )zCheck if address is anywherez::/0z	0.0.0.0/0TFr   )r   r[   r   r   r   rp   N  s    zUFWRule._is_anywherec             C   s   d}| j dks| jdkrd| j | j| j| jf }| j dkrRd| j| j| j| jf }| jdkrtd| j | j| j| jf }| jdkr|d| j 7 }| jdkr|d| j 7 }|S )a$  Returns a tuple to identify an app rule. Tuple is:
             dapp dst sapp src
           or
             dport dst sapp src
           or
             dapp dst sport src

           All of these might have in_eth0 out_eth0 (or similar) if an
           interface is also defined.
        r   z%s %s %s %sz in_%sz out_%s)r   r   r   r   r   r   r#   r$   )r   Ztuplr   r   r   get_app_tupleT  s    





zUFWRule.get_app_tuplec             C   s   | j dkr4| jdks| jdkr4td| j  }t|| j tjjkr`|dkr`td| j  }t|| j tjjkr| j	dks| j
dkrtd| j  }t|dS )zVerify ruler   r   z3Improper rule syntax ('%s' specified with app rule)r   z'Invalid IPv6 address with protocol '%s'zInvalid port with protocol '%s'N)r   r   r   r8   r   r   rW   Zipv4_only_protocolsZportless_protocolsr   r   )r   Zrule_iptyperS   r   r   r   verifyq  s    
zUFWRule.verifyN)r   r   r   r   r   Fr   )r   )r   r   r   r   r	   r   r6   r7   r/   r(   r*   r)   rX   rY   r+   r,   ra   rb   rG   r-   rd   r.   rf   rK   rr   rp   rs   rt   r   r   r   r   r   *   s6     
!	C
5

0
	#Cnr   )r   r?   rN   Zufw.utilr   r   ZprogramNameZ	state_dirZ	share_dirZ	trans_dirZ
config_dirZ
prefix_dirZiptables_dirZ	do_checksrO   r   r   r   r   r   r   <module>   s   	