
    eX                     v    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e      Z G d d      Zy)z!common.py: common classes for ufw    N)debugufwz/lib/ufwz/usr/share/ufwz/etcz/usrz	/usr/sbinTc                       e Zd ZdZd Zd Zy)UFWErrorz$This class represents ufw exceptionsc                     || _         y N)value)selfr	   s     ,/usr/lib/python3/dist-packages/ufw/common.py__init__zUFWError.__init__#   s	    
    c                 ,    t        | j                        S r   )reprr	   r
   s    r   __str__zUFWError.__str__&   s    DJJr   N)__name__
__module____qualname____doc__r   r    r   r   r   r   !   s    . r   r   c                       e Zd ZdZ	 	 	 ddZd Zd Zd Zd Zd Z	ddZ
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)UFWRulez$This class represents firewall rulesc
                 >   d| _         d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        d| _        d| _        d| _        d| _        || _        d| _        	 | j'                  |       | j)                  |       | j+                  |       | j+                  |d       | j-                  |       | j/                  |       | j1                  |       | j3                  |	       y # t4        $ r  w xY w)NF 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-   s
             r   r   zUFWRule.__init__,   s
    


		
	OOF#h'MM% MM%'LLLLy)W% 		s   B	D Dc                 "    | j                         S r   )format_ruler   s    r   r   zUFWRule.__str__O   s    !!r   c                     d| z  }t        | j                        }|j                          |D ]  }|d|d| j                  |   z  } |S )zPrint rule to stdoutz'%s'z, =)list__dict__sort)r
   reskeysks       r   _get_attribzUFWRule._get_attribR   sN    oDMM"		 	5A4==#344C	5
r   c                    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
   rules     r   dup_rulezUFWRule.dup_rule[   s    t{{DMM2kk||''8888ZZ
ZZ
ZZ
II	II	|| --!//||||r   c                 :   d}| j                   dk7  r|d| j                   z  z  }| j                  dk7  r|d| j                  z  z  }| j                  dk(  r|dz  }n|d| j                  z   z  }| j                  r|dz  }| j                  dk7  r9| j
                  dk7  r*|d| j                  z   z  }|dz  }|d	| j
                  z   z  }nC| j                  dk7  r|d| j                  z   z  }n!| j
                  dk7  r|d	| j
                  z   z  }| j                  d
k7  r!| j                  dk7  r|d| j                  z   z  }| j                  s!| j                  dk7  r|d| j                  z   z  }| j                  d
k7  r!| j                  dk7  r|d| j                  z   z  }| j                  s!| j
                  dk7  r|d| j
                  z   z  }d}| j                  dk7  rd| j                  z   }| j                  dk(  r	|d|z  z  }nL| j                  dk(  r|d|z  z  }| j                  dk(  r&|dz  }n | j                  dk(  r	|d|z  z  }n|d|z  z  }| j                  dk7  s| j                  dk7  rd}t        j                  d      }| j                  dk7  r"|d|j                  d| j                        z   z  }| j                  dk7  r| j                  dk7  r|dz  }| j                  dk7  r"|d|j                  d| j                        z   z  }|d z  }|d|z   z  }|j                         S )!zFormat rule for later parsingr   z -i %sz -o %sanyz -p allz -p z -m multiportz
 --dports z
 --sports 	0.0.0.0/0::/0z -d z	 --dport z -s z	 --sport _allowz -j ACCEPT%srejectz -j REJECT%stcpz --reject-with tcp-resetlimitz -j LIMIT%sz
 -j DROP%sz-m comment --comment ' dapp_z%20,sapp_')r)   r*   r"   r#   r    r!   r   r   r(   r&   r$   r%   recompilesubstrip)r
   rule_strlstrr-   	pat_spaces        r   r6   zUFWRule.format_ruler   s   "D$5$566H#D$6$677H ==E!	!H..HzzO+::&4::+>tzz 99H/Htzz 99HZZ5(tzz 99HZZ5(tzz 99H88{"txx6'9))HzzdjjE1djj00H88{"txx6'9))HzzdjjE1djj00H<<2%D;;'!$//H[[H$$//H}}%66[[G#..H--H99?dii2o.G

3IyyB7Y]]5$))%DDDyyB499?3yyB7Y]]5$))%DDDsNGg%H~~r   c                     |j                         j                  d      }|d   dk(  s|d   dk(  s|d   dk(  r|d   | _        nd| _        d}t        |      dkD  r|d   }| j	                  |       y	)
zSets action of the rulerG   r   rH   rI   rK   denyr      N)lowersplitr&   lenset_logtype)r
   r&   tmpr(   s       r   r.   zUFWRule.set_action   ss    lln""3'q6WA( 2c!f6Ga&DK DKs8a<!fG!r   c                 ^   t        d      |z  }|dk(  rn|dk(  r| j                  rn|dk(  r| j                  rnt        j                  d|      st        j                  d|      rt        |      |j                  d      |j                  d      z   d	kD  rt        |      |j                  d      }t        |      d
kD  rd| _	        d}|D ]  }t        j                  d|      rnd| _	        |j                  d      }|D ])  }t        |      d
k  st        |      dkD  s t        |       t        |d         t        |d
         k\  rt        |      t        j                  d|      r't        |      d
k  st        |      dkD  rCt        |      t        j                  d|      r	 t        j                  |      }nt        |      |r|dt        |      z   z  }t        |      } |}|dk(  rt        |      | _        yt        |      | _        y# t        $ r t        |      w xY w)z:Sets port and location (destination or source) of the rulezBad port '%s'rD   r   r   z^[,:]z[,:]$rN   :   rZ   Tr   z	^\d+:\d+$i  r   z^\d+$z
^\w[\w\-]+N)rG   r$   r%   rQ   matchr   countr\   r]   r#   intsocketgetservbyname	Exceptionstrr!   r    )	r
   portlocerr_msgportsr_   pranqs	            r   r0   zUFWRule.set_port   s   O$-5=E\diiE\diiXXh%(D)A7##jjo

3/257##JJsOE5zA~!
C !88L!,!%DJ''#,C  4q6A:Q%"*7"334 3q6{c#a&k1&w//XXh*1vzSVe^&w//XXmQ/0"003 #7++3Q<'Ca&C1!4 D%<TDJTDJ % 0&w//0s   *HH,c                     |t         j                  j                  dgz   v r|| _        yt	        d      |z  }t        |      )zSets protocol of the rulerD   zUnsupported protocol '%s'N)r   utilsupported_protocolsr"   rG   r   )r
   r"   rl   s      r   r/   zUFWRule.set_protocol   s=    sxx33ug==$DM34AG7##r   c                    | j                   re| j                  r%| j                  dk(  s| j                  dk(  rd| _        | j                  r'| j                  dk(  s| j                  dk(  rd| _        yyy| j                  r%| j                  dk(  s| j                  dk(  rd| _        | j                  r'| j                  dk(  s| j                  dk(  rd| _        yyy)zAdjusts src and dst based on v6rD   rE   rF   N)r   r   r   r   s    r   _fix_anywherezUFWRule._fix_anywhere   s    77xxTXX.$((k2I!xxTXX.$((k2I! 3Jx xxTXX.$((f2D&xxTXX.$((f2D& 3Exr   c                 2    || _         | j                          y)zXSets whether this is ipv6 rule, and adjusts src and dst
           accordingly.
        N)r   ru   )r
   r   s     r   set_v6zUFWRule.set_v6  s     r   c                     |j                         }|dk7  r6t        j                  j                  |d      st	        d      }t        |      || _        | j                          y)zSets source address of rulerD   zBad source addressN)r[   r   rr   valid_addressrG   r   r   ru   r
   addrr_   rl   s       r   r1   zUFWRule.set_src  sP    jjl%< 6 6sE B,-G7##r   c                     |j                         }|dk7  r6t        j                  j                  |d      st	        d      }t        |      || _        | j                          y)z Sets destination address of rulerD   zBad destination addressN)r[   r   rr   ry   rG   r   r   ru   rz   s       r   r2   zUFWRule.set_dst  sP    jjl%< 6 6sE B12G7##r   c                 z   |dk7  r|dk7  rt        d      }t        |      dt        |      v rt        d      }t        |      dt        |      v rt        d      }t        |      t        |      dk(  st        |      d	k(  rt        d
      }t        |      t        t        |            dk(  rt        d      }t        |      t        t        |            dkD  rt        d      }t        |      t	        j
                  dt        |            st        d      }t        |      |dk(  r|| _        y|| _        y)zSets an interface for ruleinoutzBad interface type!z+Bad interface name: reserved character: '!'ra   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)rG   r   ri   r]   rQ   rc   r)   r*   )r
   if_typenamerl   s       r   set_interfacezUFWRule.set_interface'  s)    d?w%/,-G7## #d)EFG7###d)IJG7##t9s4yD0CDG7##D	NaEFG7##D	NREFG7## xx4c$i@,-G7##d? $D!%Dr   c                     t        |      dk7  r8t        j                  dt        |            st        d      |z  }t	        |      t        |      | _        y)zSets the position of the rulez-1z^[0-9]+z,Insert position '%s' is not a valid positionN)ri   rQ   rc   rG   r   re   r'   )r
   numrl   s      r   set_positionzUFWRule.set_positionW  sG    
 s8tBHHZS$BFG3OG7##Cr   c                     |j                         dk(  s|j                         dk(  s|dk(  r|j                         | _        yt        d      |z  }t        |      )zSets logtype of the rulelogzlog-allr   zInvalid log type '%s'N)r[   r(   rG   r   )r
   r(   rl   s      r   r^   zUFWRule.set_logtypea  sL    ==?e#w}})'Cb="==?DL/0G<G7##r   c                 X    |dk(  s|dk(  r|| _         yt        d      |z  }t        |      )zSets direction of the ruler~   r   zUnsupported direction '%s'N)r+   rG   r   )r
   r+   rl   s      r   r3   zUFWRule.set_directionj  s3    	U 2&DN45CG7##r   c                 T    t         j                  j                  | j                        S )zGet decoded comment of the rule)r   rr   
hex_decoder-   r   s    r   get_commentzUFWRule.get_commentr  s    xx""4<<00r   c                     || _         y)zSets comment of the ruleN)r-   )r
   r-   s     r   r4   zUFWRule.set_commentv  s	    r   c                 N   d}| j                   rF	 t        j                  j                  | j                   | j                        \  | _         }|r|| _        | j                  rF	 t        j                  j                  | j                  | j                        \  | _	        }|r|| _        | j                  rP| j                  j                  d      }t        j                  j                  |       dj                  |      | _
        | j                  rQ| j                  j                  d      }t        j                  j                  |       dj                  |      | _        yy# t
        $ r t        d      }t        |      w xY w# t
        $ r t        d      }t        |      w xY w)z&Normalize src and dst to standard formFz"Could not normalize source addressz'Could not normalize destination addressrN   N)r   r   rr   normalize_addressr   rh   rG   r   r   r   r    r\   
human_sortjoinr!   )r
   changedrl   rm   s       r   	normalizezUFWRule.normalizez  sS   88(&)hh&@&@AE'J#7 &88(&)hh&@&@CG77'L#7 &::JJ$$S)EHH&%DJ::JJ$$S)EHH&%DJ /  (@Aw''(  (EFw''(s   <E! "<F ! F F$c                    | r|s
t               d| d|d}| j                  |j                  k7  rt        |       y| j                  |j                  k7  rt        |       y| j                  |j                  k7  rt        |       y| j
                  |j
                  k7  rt        |       y| j                  |j                  k7  rt        |       y| j                  |j                  k7  rt        |       y| j                  |j                  k7  rt        |       y| j                  |j                  k7  rt        |       y| j                  |j                  k7  rt        |       y| j                  |j                  k7  rt        |       y| j                  |j                  k7  rt        |       y| j                  |j                  k7  rt        |       y| j                  |j                  k(  rI| j                  |j                  k(  r0| j                   |j                   k(  rt#        d      }t        |       y| j                  |j                  k(  rI| j                  |j                  k(  r0| j                   |j                   k7  rt#        d      }t        |       yt#        d	      | j                  |j                  | j                  |j                  | j                   |j                   d
z  }t        |       y)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 'z' 'rP   rZ   zFound exact matchr   z$Found exact match, excepting commentzZFound non-action/non-logtype/comment match (%(xa)s/%(ya)s/'%(xc)s' %(xl)s/%(yl)s/'%(yc)s'))xayaxlylxcyc)
ValueErrorr    r   r!   r"   r   r   r   r$   r%   r)   r*   r+   r,   r&   r(   r-   rG   )xydbg_msgs      r   rc   zUFWRule.match  s-    ,  +,Q/77agg'N77agg'N::#'N55AEE>'N55AEE>'N44144<'N66QVV'N66QVV'N>>Q^^+'N??aoo-'N;;!++%'N99		!'N88qxxAII$:		QYY&+,G'N88qxxAII$:		QYY&>?G'N F GHHAHHIIQYYIIQYY89
 	gr   c                 >   d }| r|s
t               | j                  |      dk(  ryd| d| j                  d|d|j                  d	}|j                  dk7  rt	        d|z   d	z          y
|j
                  | j
                  k7  rt	        |dz          y
| j                  |j                  k7  r|j                  dk7  rt	        d|z          y
|j                  dk7  r, || j                  |j                        st	        d|z          y
|j                  dk(  r| j                  dk(  r| j                  | j                        rn>| j                  |j                  k7  rd|j                  vrt	        d|z          y
| j                  |j                  k7  rd|j                  v r| j                  |j                  k(  rt        j                  j                  | j                  |j                  | j                        st	        d|z   d| j                  d|j                  dz          y
| j                  dk7  rF| j                  |j                  k7  r-t	        d|z   d| j                  d|j                  dz          y
	 t        j                  j                  |j                  | j                        }|j                  |k7  r1d|j                  vr#t	        d|z   d|j                  d|dz          y
|j                  |k7  rd|j                  v rq| j                  |j                  k(  rXt        j                  j                  ||j                  | j                        s#t	        d|z   d|d|j                  dz          y
| j                  |j                  k7  r-t	        d|z   d| j                  d|j                  dz          y
t	        d| d| j                  d|d|j                  d	       y# t        $ r! t	        d|z   d|j                  z  z          Y y
w xY w)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                     d| v sd| v r| |k(  ryy|j                  d      D ]S  }| |k(  r yd|v s|j                  d      \  }}t        |       t        |      k\  s;t        |       t        |      k  sS y y)z:Returns True if p is an exact match or within a multi rulerN   ra   TF)r\   re   )test_pto_matchrj   lowhighs        r   _match_portsz-UFWRule.fuzzy_dst_match.<locals>._match_ports  s~    f}vX% s+ $T>$;"&**S/KS$6{c#h.3v;#d)3K#$ r   r   zNo fuzzy match 'z (v6=z)' 'z)'r~   z(direction) z (not incoming)rZ   z (forward does not match)rD   z(protocol) z(dport) r   /z(dst) z ('z' not in network 'z')z(interface) z (z != )z %s does not existz(v6) z(fuzzy match) 'r   )r   rc   r   r+   r   r,   r"   r    r)   _is_anywherer   r   rr   
in_networkget_ip_from_ifIOError)r   r   r   r   if_ips        r   fuzzy_dst_matchzUFWRule.fuzzy_dst_match  sH   	" , 771:?  qttQ& ;;$.7*->>? 99		!'778 ::#

e(;-')* 77eL!''$B*w&'>>R ~~#quu(= !%%Cquu$4h()!%%C155LQTTQTT\88&&quuaeeQTT:h(uuaee,% % & ~~#!..(Hnw.~~q~~27 7 8//E uu~#QUU"2nw.uue2% % &%C155LQTTQTT\88&&uaeeQTT:nw.7<aeeDE F44144<'G#quuaee&DDE 	1addAqttLM+  nw.1E~~2' ' (s   =4O2 2'PPc                     |dk(  s|dk(  ryy)zCheck if address is anywhererF   rE   TFr   )r
   r{   s     r   r   zUFWRule._is_anywhereN  s    6>T[0r   c                    d}| j                   dk7  s| j                  dk7  r1| j                   d| j                  d| j                  d| j                  }| j                   dk(  r5| j                  d| j                  d| j                  d| j                  }| j                  dk(  r5| j                   d| j                  d| j
                  d| j                  }| j                  dk(  r#| j                  dk(  r|d| j                  z  z  }|S | j                  dk7  r|d| j                  z  z  }| j                  dk7  r|d| j                  z  z  }|S )a  Returns a tuple to identify an app rule. Tuple is:
             dapp dst sapp src direction_iface|direction
           or
             dport dst sapp src direction_iface|direction
           or
             dapp dst sport src direction_iface|direction

           where direction_iface is of form 'in_eth0', 'out_eth0' or
           'in_eth0 out_eth0' (ie, both interfaces used). If no interfaces are
           specified, then tuple ends with the direction instead.
        r   rL   z %sz in_%sz out_%s)	r$   r%   r   r   r    r!   r)   r*   r+   )r
   tupls     r   get_app_tuplezUFWRule.get_app_tupleT  s    99?dii2o$(IItxxDHHMDyyB(,

DHHdii)-3yyB(,		488TZZ)-3   B&4+=+=+C00  $$*H(9(9::D%%+I););<<Dr   c                    | j                   dk7  rA| j                  dk7  s| j                  dk7  r#t        d      | j                   z  }t	        |      | j                   t
        j                  j                  v r(|dk(  r#t        d      | j                   z  }t	        |      | j                   t
        j                  j                  v rB| j                  dk7  s| j                  dk7  r#t        d      | j                   z  }t	        |      yy)zVerify rulerD   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$   rG   r   r   rr   ipv4_only_protocolsportless_protocolsr    r!   )r
   rule_iptyperl   s      r   verifyzUFWRule.verifyv  s     ==E!IIOtyyBMN==*G7##==CHH888$AB(G7##==CHH777zzU"djjE&9=>!]],w'' ': 8r   N)rD   rE   rD   rE   r~   Fr   )r   )r   r   r   r   r   r   r?   rB   r6   r.   r0   r/   ru   rw   r1   r2   r   r   r^   r3   r   r4   r   rc   r   r   r   r   r   r   r   r   r   *   s    .:EGL!F".A F"3#j$'.&`!$$1!)FAFl\ D(r   r   )r   rQ   rf   ufw.utilr   r   programName	state_dir	share_dir	trans_dir
config_dir
prefix_diriptables_dir	do_checksrh   r   r   r   r   r   <module>r      sV    '" 
   			

	 y  `	( `	(r   