
    6mgX                     v    S r SSKrSSKrSSKrSSKJr  SrSrSr\r	Sr
SrS	rS
r " S S\5      r " S S5      rg)z!common.py: common classes for ufw    N)debugufwz/lib/ufwz/usr/share/ufwz/etcz/usrz	/usr/sbinTc                   $    \ rS rSrSrS rS rSrg)UFWError!   z$This class represents ufw exceptionsc                     Xl         g Nvalue)selfr   s     ,/usr/lib/python3/dist-packages/ufw/common.py__init__UFWError.__init__#   s    
    c                 ,    [        U R                  5      $ r	   )reprr   r   s    r   __str__UFWError.__str__&   s    DJJr   r
   N)__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes__ r   r   r   r   !   s    . r   r   c                       \ rS rSrSr   SS jrS rS rS rS r	S r
SS	 jrS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rSrg)UFWRule*   z$This class represents firewall rulesc
                 >   SU l         SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l	        SU l
        SU l        SU l        SU l        SU l        SU l        SU l        Xl        SU l         U R'                  U5        U R)                  U5        U R+                  U5        U R+                  US5        U R-                  U5        U R/                  U5        U R1                  U5        U R3                  U	5        g ! [4         a    e f = f)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"   r2   r3   r4   s
             r   r   UFWRule.__init__,   s    


		
	OOF#h'MM% MM%'LLLLy)W% 		s   B	D Dc                 "    U R                  5       $ r	   )format_ruler   s    r   r   UFWRule.__str__O   s    !!r   c                     SU -  n[        U R                  5      nUR                  5         U H  nUSU< SU R                  U   < 3-  nM     U$ )zPrint rule to stdoutz'%s'z, =)list__dict__sort)r   reskeysks       r   _get_attribUFWRule._get_attribR   sI    oDMM"		A4==#344C 
r   c                    [        U R                  U R                  5      nU R                  Ul        U R                  Ul        U R
                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        U R                  Ul	        U R                  Ul
        U R                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        U R                   Ul        U R"                  Ul        U R$                  Ul        U R&                  Ul        U$ )zReturn a duplicate of a rule)r   r-   r)   r#   r$   r%   r&   r"   r'   r(   r*   r+   r,   r.   r/   r0   r1   r2   r3   r4   )r   rules     r   dup_ruleUFWRule.dup_rule[   s    t{{DMM2kk||''8888ZZ
ZZ
ZZ
II	II	|| --!//||||r   c                    SnU R                   S:w  a  USU R                   -  -  nU R                  S:w  a  USU R                  -  -  nU R                  S:X  a  US-  nOUSU R                  -   -  nU R                  (       a  US-  nU R                  S:w  a:  U R
                  S:w  a*  USU R                  -   -  nUS-  nUS	U R
                  -   -  nOEU R                  S:w  a  USU R                  -   -  nO"U R
                  S:w  a  US	U R
                  -   -  nU R                  S
:w  a"  U R                  S:w  a  USU R                  -   -  nU R                  (       d"  U R                  S:w  a  USU R                  -   -  nU R                  S
:w  a"  U R                  S:w  a  USU R                  -   -  nU R                  (       d"  U R
                  S:w  a  USU R
                  -   -  nSnU R                  S:w  a  SU R                  -   nU R                  S:X  a	  USU-  -  nOOU R                  S:X  a  USU-  -  nU R                  S:X  a  US-  nO!U R                  S:X  a	  USU-  -  nOUSU-  -  nU R                  S:w  d  U R                  S:w  a  Sn[        R                  " S5      nU R                  S:w  a"  USUR                  SU R                  5      -   -  nU R                  S:w  a  U R                  S:w  a  US-  nU R                  S:w  a"  USUR                  SU R                  5      -   -  nUS -  nUSU-   -  nUR                  5       $ )!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_')r0   r1   r)   r*   r'   r(   r&   r"   r/   r-   r+   r,   recompilesubstrip)r   rule_strlstrr4   	pat_spaces        r   r>   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                     UR                  5       R                  S5      nUS   S:X  d  US   S:X  d	  US   S:X  a  US   U l        OSU l        Sn[        U5      S:  a  US   nU R	                  U5        g	)
zSets action of the rulerR   r   rS   rT   rV   denyr!      N)lowersplitr-   lenset_logtype)r   r-   tmpr/   s       r   r5   UFWRule.set_action   ss    lln""3'q6WA( 2c!f6Ga&DK DKs8a<!fG!r   c                    [        S5      U-  nUS:X  a  GOUS:X  a  U R                  (       a  GOUS:X  a  U R                  (       a  GO[        R                  " SU5      (       d  [        R                  " SU5      (       a  [        U5      eUR                  S5      UR                  S5      -   S	:  a  [        U5      eUR                  S5      n[        U5      S
:  a  SU l	        SnU GH?  n[        R                  " SU5      (       at  SU l	        UR                  S5      nU H,  n[        U5      S
:  d  [        U5      S:  d  M#  [        U5      e   [        US   5      [        US
   5      :  a  [        U5      eO[        R                  " SU5      (       a*  [        U5      S
:  d  [        U5      S:  a  [        U5      eO?[        R                  " SU5      (       a   [        R                  " U5      nO[        U5      eU(       a  US[        U5      -   -  nGM4  [        U5      nGMB     UnUS:X  a  [        U5      U l        g[        U5      U l        g! [         a    [        U5      ef = f)z:Sets port and location (destination or source) of the rulezBad port '%s'rO   r&   r"   z^[,:]z[,:]$rY   :   rf   Tr!   z	^\d+:\d+$i  r   z^\d+$z
^\w[\w\-]+N)rR   r+   r,   r\   matchr   countrh   ri   r*   intsocketgetservbyname	Exceptionstrr(   r'   )	r   portlocerr_msgportsrk   pranqs	            r   r7   UFWRule.set_port   s   O$-5=E\diiE\diiXXh%%(D)A)A7##jjo

3/257##JJsOE5zA~!
C88L!,,!%DJ''#,C q6A:Q%"*7"33 ! 3q6{c#a&k1&w// 2XXh**1vzSVe^&w// &4XXmQ//0"003 #7++3Q<'Ca&C1 4 D%<TDJTDJ % 0&w//0s    II-c                     U[         R                  R                  S/-   ;   a  Xl        g[	        S5      U-  n[        U5      e)zSets protocol of the rulerO   zUnsupported protocol '%s'N)r   utilsupported_protocolsr)   rR   r   )r   r)   ry   s      r   r6   UFWRule.set_protocol   s;    sxx33ug==$M34AG7##r   c                    U R                   (       as  U R                  (       a'  U R                  S:X  d  U R                  S:X  a  SU l        U R                  (       a)  U R                  S:X  d  U R                  S:X  a  SU l        gggU R                  (       a'  U R                  S:X  d  U R                  S:X  a  SU l        U R                  (       a)  U R                  S:X  d  U R                  S:X  a  SU l        ggg)zAdjusts src and dst based on v6rO   rP   rQ   N)r%   r&   r"   r   s    r   _fix_anywhereUFWRule._fix_anywhere   s    77xxTXX.$((k2I!xxTXX.$((k2I! 3Jx xxTXX.$((f2D&xxTXX.$((f2D& 3Exr   c                 0    Xl         U R                  5         g)zESets whether this is ipv6 rule, and adjusts src and dst
accordingly.
N)r%   r   )r   r%   s     r   set_v6UFWRule.set_v6  s     r   c                     UR                  5       nUS:w  a;  [        R                  R                  US5      (       d  [	        S5      n[        U5      eX l        U R                  5         g)zSets source address of rulerO   zBad source addressN)rg   r   r   valid_addressrR   r   r"   r   r   addrrk   ry   s       r   r8   UFWRule.set_src  sQ    jjl%< 6 6sE B B,-G7##r   c                     UR                  5       nUS:w  a;  [        R                  R                  US5      (       d  [	        S5      n[        U5      eX l        U R                  5         g)z Sets destination address of rulerO   zBad destination addressN)rg   r   r   r   rR   r   r&   r   r   s       r   r9   UFWRule.set_dst  sQ    jjl%< 6 6sE B B12G7##r   c                    US:w  a  US:w  a  [        S5      n[        U5      eS[        U5      ;   a  [        S5      n[        U5      eS[        U5      ;   a  [        S5      n[        U5      e[        U5      S:X  d  [        U5      S	:X  a  [        S
5      n[        U5      e[        [        U5      5      S:X  a  [        S5      n[        U5      e[        [        U5      5      S:  a  [        S5      n[        U5      e[        R
                  " S[        U5      5      (       d  [        S5      n[        U5      eUS:X  a  X l        gX l        g)zSets an interface for ruleinoutzBad interface type!z+Bad interface name: reserved character: '!'rn   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)rR   r   rv   ri   r\   rp   r0   r1   )r   if_typenamery   s       r   set_interface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? $!%r   c                     [        U5      S:w  a>  [        R                  " S[        U5      5      (       d  [        S5      U-  n[	        U5      e[        U5      U l        g)zSets the position of the rulez-1z^[0-9]+z,Insert position '%s' is not a valid positionN)rv   r\   rp   rR   r   rr   r.   )r   numry   s      r   set_positionUFWRule.set_positionW  sJ    
 s8tBHHZS$B$BFG3OG7##Cr   c                     UR                  5       S:X  d  UR                  5       S:X  d  US:X  a  UR                  5       U l        g[        S5      U-  n[        U5      e)zSets logtype of the rulelogzlog-allr!   zInvalid log type '%s'N)rg   r/   rR   r   )r   r/   ry   s      r   rj   UFWRule.set_logtypea  sL    ==?e#w}})'Cb="==?DL/0G<G7##r   c                 Z    US:X  d  US:X  a  Xl         g[        S5      U-  n[        U5      e)zSets direction of the ruler   r   zUnsupported direction '%s'N)r2   rR   r   )r   r2   ry   s      r   r:   UFWRule.set_directionj  s1    	U 2&N45CG7##r   c                 T    [         R                  R                  U R                  5      $ )zGet decoded comment of the rule)r   r   
hex_decoder4   r   s    r   get_commentUFWRule.get_commentr  s    xx""4<<00r   c                     Xl         g)zSets comment of the ruleN)r4   )r   r4   s     r   r;   UFWRule.set_commentv  s    r   c                    SnU R                   (       aJ   [        R                  R                  U R                   U R                  5      u  U l         nU(       a  Xl        U R                  (       aJ   [        R                  R                  U R                  U R                  5      u  U l	        nU(       a  Xl        U R                  (       aP  U R                  R                  S5      n[        R                  R                  U5        SR                  U5      U l
        U R                  (       aQ  U R                  R                  S5      n[        R                  R                  U5        SR                  U5      U l        gg! [
         a    [        S5      n[        U5      ef = f! [
         a    [        S5      n[        U5      ef = f)z&Normalize src and dst to standard formFz"Could not normalize source addressz'Could not normalize destination addressrY   N)r"   r   r   normalize_addressr%   ru   rR   r   r$   r&   r'   rh   
human_sortjoinr(   )r   changedry   rz   s       r   	normalizeUFWRule.normalizez  sO   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= 0<F! =!F!!Gc                 &   U (       a  U(       d
  [        5       eSU < SU< S3nU R                  UR                  :w  a  [        U5        gU R                  UR                  :w  a  [        U5        gU R                  UR                  :w  a  [        U5        gU R
                  UR
                  :w  a  [        U5        gU R                  UR                  :w  a  [        U5        gU R                  UR                  :w  a  [        U5        gU R                  UR                  :w  a  [        U5        gU R                  UR                  :w  a  [        U5        gU R                  UR                  :w  a  [        U5        gU R                  UR                  :w  a  [        U5        gU R                  UR                  :w  a  [        U5        gU R                  UR                  :w  a  [        U5        gU R                  UR                  :X  aK  U R                  UR                  :X  a1  U R                   UR                   :X  a  [#        S5      n[        U5        gU R                  UR                  :X  aK  U R                  UR                  :X  a1  U R                   UR                   :w  a  [#        S5      n[        U5        g[#        S	5      U R                  UR                  U R                  UR                  U R                   UR                   S
.-  n[        U5        g)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' 'r[   rf   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,   r0   r1   r2   r3   r-   r/   r4   rR   )xydbg_msgs      r   rp   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                    S nU (       a  U(       d
  [        5       eU R                  U5      S:X  a  gSU < SU R                  < SU< SUR                  < S3	nUR                  S:w  a  [	        SU-   S	-   5        g
UR
                  U R
                  :w  a  [	        US-   5        g
U R                  UR                  :w  a  UR                  S:w  a  [	        SU-   5        g
UR                  S:w  a1  U" U R                  UR                  5      (       d  [	        SU-   5        g
UR                  S:X  Ga$  U R                  S:X  a"  U R                  U R                  5      (       a  GO]U R                  UR                  :w  a  SUR                  ;  a  [	        SU-   5        g
U R                  UR                  :w  a  SUR                  ;   a  U R                  UR                  :X  as  [        R                  R                  U R                  UR                  U R                  5      (       d/  [	        SU-   SU R                  < SUR                  < S3-   5        g
GOkU R                  S:w  aI  U R                  UR                  :w  a/  [	        SU-   SU R                  < SUR                  < S3-   5        g
 [        R                  R                  UR                  U R                  5      nUR                  U:w  a5  SUR                  ;  a%  [	        SU-   SUR                  < SU< S3-   5        g
UR                  U:w  a  SUR                  ;   ax  U R                  UR                  :X  a^  [        R                  R                  XAR                  U R                  5      (       d%  [	        SU-   SU< SUR                  < S3-   5        g
U R                  UR                  :w  a/  [	        SU-   SU R                  < SUR                  < S3-   5        g
[	        SU < SU R                  < SU< SUR                  < S3	5        g! [         a!    [	        SU-   SUR                  -  -   5         g
f = f)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U ;   d  SU ;   a  X:X  a  ggUR                  S5       HY  nX:X  a    gSU;   d  M  UR                  S5      u  p4[        U 5      [        U5      :  d  M?  [        U 5      [        U5      ::  d  MY    g   g)z:Returns True if p is an exact match or within a multi rulerY   rn   TF)rh   rr   )test_pto_matchrw   lowhighs        r   _match_ports-UFWRule.fuzzy_dst_match.<locals>._match_ports  ss    f}v% s+>$;"&**S/KS6{c#h.3v;#d)3K# , r   r   zNo fuzzy match 'z (v6=z)' 'z)'r   z(direction) z (not incoming)rf   z (forward does not match)rO   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   rp   r%   r2   r   r3   r)   r'   r0   _is_anywherer&   r   r   
in_networkget_ip_from_ifIOError)r   r   r   r   if_ips        r   fuzzy_dst_matchUFWRule.fuzzy_dst_match  sS   	" , 771:?  qttQ& ;;$.7*->>? 99		!'778 ::#

e(;-')* 77eL!''$B$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&&ueeQTT::nw.7<aeeDE F44144<'G#quuaee&DDE 	1addAqttLM+  nw.1E~~2' ' (s   -4P8 8(Q#"Q#c                     US:X  d  US:X  a  gg)zCheck if address is anywhererQ   rP   TFr   )r   r   s     r   r   UFWRule._is_anywhereN  s    6>T[0r   c                    SnU R                   S:w  d  U R                  S:w  GaC  U R                   < SU R                  < SU R                  < SU R                  < 3nU R                   S:X  a9  U R                  < SU R                  < SU R                  < SU R                  < 3nU R                  S:X  a9  U R                   < SU R                  < SU R
                  < SU R                  < 3nU R                  S:X  a$  U R                  S:X  a  USU R                  -  -  nU$ U R                  S:w  a  USU R                  -  -  nU R                  S:w  a  USU R                  -  -  nU$ )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!   rW   z %sz in_%sz out_%s)	r+   r,   r&   r"   r'   r(   r0   r1   r2   )r   tupls     r   get_app_tuple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                 $   U R                   S:w  aC  U R                  S:w  d  U R                  S:w  a#  [        S5      U R                   -  n[	        U5      eU R                   [
        R                  R                  ;   a)  US:X  a#  [        S5      U R                   -  n[	        U5      eU R                   [
        R                  R                  ;   aD  U R                  S:w  d  U R                  S:w  a#  [        S5      U R                   -  n[	        U5      egg)zVerify rulerO   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+   rR   r   r   r   ipv4_only_protocolsportless_protocolsr'   r(   )r   rule_iptypery   s      r   verify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   )r-   r4   r+   r2   r'   r&   r3   r0   r1   r/   r*   r.   r)   r#   r,   r(   r"   r$   r%   N)rO   rP   rO   rP   r   Fr!   )r&   )r   r   r   r   r   r   r   rH   rL   r>   r5   r7   r6   r   r   r8   r9   r   r   rj   r:   r   r;   r   rp   r   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   r\   rs   ufw.utilr   r   programName	state_dir	share_dir	trans_dir
config_dir
prefix_diriptables_dir	do_checksru   r   r   r   r   r   <module>r      sV    '" 
   			

	 y  `	( `	(r   