+
    4iX                     x    R t ^ RIt^ RIt^ RIt^ RIHt RtRtRt]t	Rt
RtRtR	t ! R
 R]4      t ! R R4      tR# )z!common.py: common classes for ufwN)debugufwz/lib/ufwz/usr/share/ufwz/etcz/usrz	/usr/sbinTc                   0   a  ] tR t^!t o RtR tR tRtV tR# )UFWErrorz$This class represents ufw exceptionsc                    Wn         R # Nvalue)selfr	   s   &&,/usr/lib/python3/dist-packages/ufw/common.py__init__UFWError.__init__#   s    
    c                ,    \        V P                  4      # r   )reprr	   r
   s   &r   __str__UFWError.__str__&   s    DJJr   r   N)	__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes____classdictcell____classdict__s   @r   r   r   !   s     .   r   r   c                      a  ] tR t^*t o RtRR ltR tR tR tR t	R t
RR ltR	 tR
 tR tR tR tR tR tR tR tR tR tR tR tR tR tR tR tRtV tR# )UFWRulez$This class represents firewall rulesc
                B   R V n         R V n        R V n        RV n        RV n        RV n        RV n        RV n        R V n        RV n	        RV n
        RV n        ^ V n        RV n        RV n        RV n        RV n        Wn        RV n         V P'                  V4       V P)                  V4       V P+                  V4       V P+                  VR4       V P-                  V4       V P/                  V4       V P1                  V4       V P3                  V	4       R#   \4         d    h i ; i)F srcN)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!   r1   r2   r3   s
   &&&&&&&&&&r   r   UFWRule.__init__,   s    


		
	OOF#h'MM% MM%'LLLLy)W% 		s   B	D Dc                "    V P                  4       # r   )format_ruler   s   &r   r   UFWRule.__str__O   s    !!r   c                    RV ,          p\        V P                  4      pVP                  4        V F%  pVRV: RV P                  V,          : 2,          pK'  	  V# )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                   \        V P                  V P                  4      pV P                  Vn        V P                  Vn        V P
                  Vn        V P                  Vn        V P                  Vn        V P                  Vn        V P                  Vn	        V P                  Vn
        V P                  Vn        V P                  Vn        V P                  Vn        V P                  Vn        V P                  Vn        V P                   Vn        V P"                  Vn        V P$                  Vn        V P&                  Vn        V# )zReturn a duplicate of a rule)r   r,   r(   r"   r#   r$   r%   r!   r&   r'   r)   r*   r+   r-   r.   r/   r0   r1   r2   r3   )r
   rules   & r   dup_ruleUFWRule.dup_rule[   s    t{{DMM2kk||''8888ZZ
ZZ
ZZ
II	II	|| --!//||||r   c                   RpV P                   R8w  d   VRV P                   ,          ,          pV P                  R8w  d   VRV P                  ,          ,          pV P                  R8X  d   VR,          pMVRV P                  ,           ,          pV P                  '       d   VR,          pV P                  R8w  dP   V P
                  R8w  d?   VRV P                  ,           ,          pVR,          pVR	V P
                  ,           ,          pMWV P                  R8w  d   VRV P                  ,           ,          pM+V P
                  R8w  d   VR	V P
                  ,           ,          pV P                  R
8w  d,   V P                  R8w  d   VRV P                  ,           ,          pV P                  '       g,   V P                  R8w  d   VRV P                  ,           ,          pV P                  R
8w  d,   V P                  R8w  d   VRV P                  ,           ,          pV P                  '       g,   V P
                  R8w  d   VRV P
                  ,           ,          pRpV P                  R8w  d   RV P                  ,           pV P                  R8X  d   VRV,          ,          pMnV P                  R8X  d,   VRV,          ,          pV P                  R8X  d
   VR,          pM2V P                  R8X  d   VRV,          ,          pMVRV,          ,          pV P                  R8w  g   V P                  R8w  d   Rp\        P                  ! R4      pV P                  R8w  d+   VRVP                  RV P                  4      ,           ,          pV P                  R8w  d   V P                  R8w  d
   VR,          pV P                  R8w  d+   VRVP                  RV P                  4      ,           ,          pVR ,          pVRV,           ,          pVP                  4       # )!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/   r0   r(   r)   r&   r'   r%   r!   r.   r,   r*   r+   recompilesubstrip)r
   rule_strlstrr3   	pat_spaces   &    r   r=   UFWRule.format_ruler   s   "D$5$566H#D$6$677H ==E!	!H..HzzzO+::&4::+>tzz 99H/Htzz 99HZZ5(tzz 99HZZ5(tzz 99H88{"txx6'9))HzzzdjjE1djj00H88{"txx6'9))HzzzdjjE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                   VP                  4       P                  R4      pV^ ,          R8X  g   V^ ,          R8X  g   V^ ,          R8X  d   V^ ,          V n        MRV n        Rp\        V4      ^8  d
   V^,          pV P	                  V4       R# )zSets action of the rulerQ   rR   rS   rU   denyr    N)lowersplitr,   lenset_logtype)r
   r,   tmpr.   s   &&  r   r4   UFWRule.set_action   ss    lln""3'q6WA( 2c!f6Ga&DK DKs8a<!fG!r   c                <   \        R4      V,          pVR8X  d   EM=VR8X  d   V P                  '       d   EM"VR8X  d   V P                  '       d   EM\        P                  ! RV4      '       g   \        P                  ! RV4      '       d   \        V4      hVP                  R4      VP                  R4      ,           ^8  d   \        V4      hVP                  R4      p\        V4      ^8  d   R	V n	        R
pV EFX  p\        P                  ! RV4      '       d   R	V n	        VP                  R4      pV F.  p\        V4      ^8  g   \        V4      R8  g   K%  \        V4      h	  \        V^ ,          4      \        V^,          4      8  d   \        V4      hM\        P                  ! RV4      '       d-   \        V4      ^8  g   \        V4      R8  d   \        V4      hM@\        P                  ! RV4      '       d    \        P                  ! V4      pM\        V4      hV'       d   VR\        V4      ,           ,          pEKM  \        V4      pEK[  	  TpVR8X  d   \        V4      V n        R# \        V4      V n        R#   \         d    \        T4      hi ; i)z:Sets port and location (destination or source) of the rulezBad port '%s'rN   r%   r!   z^[,:]z[,:]$rX   :Tr    z	^\d+:\d+$i  z^\d+$z
^\w[\w\-]+N)rQ   r*   r+   r[   matchr   countrf   rg   r)   intsocketgetservbyname	Exceptionstrr'   r&   )	r
   portlocerr_msgportsri   pranqs	   &&&      r   r6   UFWRule.set_port   s   O$-5=E\diiiE\diii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   JJc                    V\         P                  P                  R.,           9   d	   Wn        R# \	        R4      V,          p\        V4      h)zSets protocol of the rulerN   zUnsupported protocol '%s'N)r   utilsupported_protocolsr(   rQ   r   )r
   r(   rv   s   && r   r5   UFWRule.set_protocol   s;    sxx33ug==$M34AG7##r   c                   V P                   '       d}   V P                  '       d*   V P                  R8X  g   V P                  R8X  d   RV n        V P                  '       d.   V P                  R8X  g   V P                  R8X  d
   RV n        R# R# R# V P                  '       d*   V P                  R8X  g   V P                  R8X  d   RV n        V P                  '       d.   V P                  R8X  g   V P                  R8X  d
   RV n        R# R# R# )zAdjusts src and dst based on v6rN   rO   rP   N)r$   r%   r!   r   s   &r   _fix_anywhereUFWRule._fix_anywhere   s    777xxxTXX.$((k2I!xxxTXX.$((k2I! 3Jx xxxTXX.$((f2D&xxxTXX.$((f2D& 3Exr   c                2    Wn         V P                  4        R# )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                    VP                  4       pVR8w  d=   \        P                  P                  VR4      '       g   \	        R4      p\        V4      hW n        V P                  4        R# )zSets source address of rulerN   zBad source addressN)re   r   r}   valid_addressrQ   r   r!   r   r
   addrri   rv   s   &&  r   r7   UFWRule.set_src  sQ    jjl%< 6 6sE B B,-G7##r   c                    VP                  4       pVR8w  d=   \        P                  P                  VR4      '       g   \	        R4      p\        V4      hW n        V P                  4        R# )z Sets destination address of rulerN   zBad destination addressN)re   r   r}   r   rQ   r   r%   r   r   s   &&  r   r8   UFWRule.set_dst  sQ    jjl%< 6 6sE B B12G7##r   c                   VR8w  d   VR8w  d   \        R4      p\        V4      hR\        V4      9   d   \        R4      p\        V4      hR\        V4      9   d   \        R4      p\        V4      h\        V4      R8X  g   \        V4      R	8X  d   \        R
4      p\        V4      h\        \        V4      4      ^ 8X  d   \        R4      p\        V4      h\        \        V4      4      ^8  d   \        R4      p\        V4      h\        P
                  ! R\        V4      4      '       g   \        R4      p\        V4      hVR8X  d	   W n        R# W n        R# )zSets an interface for ruleinoutzBad interface type!z+Bad interface name: reserved character: '!'rl   z/Bad interface name: can't use interface aliases.z..z)Bad interface name: can't use '.' or '..'z+Bad interface name: interface name is emptyz+Bad interface name: interface name too longz^[a-zA-Z0-9_\-\.\+,=%@]+$zBad interface nameN)rQ   r   rs   rg   r[   rm   r/   r0   )r
   if_typenamerv   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                    \        V4      R8w  dD   \        P                  ! R\        V4      4      '       g   \        R4      V,          p\	        V4      h\        V4      V n        R# )zSets the position of the rulez-1z^[0-9]+z,Insert position '%s' is not a valid positionN)rs   r[   rm   rQ   r   ro   r-   )r
   numrv   s   && r   set_positionUFWRule.set_positionW  sJ    
 s8tBHHZS$B$BFG3OG7##Cr   c                    VP                  4       R8X  g   VP                  4       R8X  g   VR8X  d   VP                  4       V n        R# \        R4      V,          p\        V4      h)zSets logtype of the rulelogzlog-allr    zInvalid log type '%s'N)re   r.   rQ   r   )r
   r.   rv   s   && r   rh   UFWRule.set_logtypea  sL    ==?e#w}})'Cb="==?DL/0G<G7##r   c                h    VR8X  g   VR8X  d	   Wn         R# \        R4      V,          p\        V4      h)zSets direction of the ruler   r   zUnsupported direction '%s'N)r1   rQ   r   )r
   r1   rv   s   && r   r9   UFWRule.set_directionj  s1    	U 2&N45CG7##r   c                T    \         P                  P                  V P                  4      # )zGet decoded comment of the rule)r   r}   
hex_decoder3   r   s   &r   get_commentUFWRule.get_commentr  s    xx""4<<00r   c                    Wn         R# )zSets comment of the ruleN)r3   )r
   r3   s   &&r   r:   UFWRule.set_commentv  s    r   c                   RpV P                   '       dL    \        P                  P                  V P                   V P                  4      w  V n         pT'       d   Yn        V P                  '       dL    \        P                  P                  V P                  V P                  4      w  V n	        pT'       d   Yn        V P                  '       dQ   V P                  P                  R4      p\        P                  P                  V4       RP                  V4      V n
        V P                  '       dS   V P                  P                  R4      p\        P                  P                  V4       RP                  V4      V n        R# R#   \
         d    \        R4      p\        T4      hi ; i  \
         d    \        R4      p\        T4      hi ; i)z&Normalize src and dst to standard formFz"Could not normalize source addressz'Could not normalize destination addressrX   N)r!   r   r}   normalize_addressr$   rr   rQ   r   r#   r%   r&   rf   
human_sortjoinr'   )r
   changedrv   rw   s   &   r   	normalizeUFWRule.normalizez  sW   888(&)hh&@&@AE'J#7 &888(&)hh&@&@CG77'L#7 &:::JJ$$S)EHH&%DJ:::JJ$$S)EHH&%DJ /  (@Aw''(  (EFw''(s   <F 3<F* "F'*"Gc                ~   V '       d	   V'       g   \        4       hRV : RV: R2pV P                  VP                  8w  d   \        V4       ^# V P                  VP                  8w  d   \        V4       ^# V P                  VP                  8w  d   \        V4       ^# V P
                  VP
                  8w  d   \        V4       ^# V P                  VP                  8w  d   \        V4       ^# V P                  VP                  8w  d   \        V4       ^# V P                  VP                  8w  d   \        V4       ^# V P                  VP                  8w  d   \        V4       ^# V P                  VP                  8w  d   \        V4       ^# V P                  VP                  8w  d   \        V4       ^# V P                  VP                  8w  d   \        V4       ^# V P                  VP                  8w  d   \        V4       ^# V P                  VP                  8X  dO   V P                  VP                  8X  d4   V P                   VP                   8X  d   \#        R4      p\        V4       ^ # V P                  VP                  8X  dO   V P                  VP                  8X  d4   V P                   VP                   8w  d   \#        R4      p\        V4       R# \#        R4      RV P                  RVP                  R	V P                  R
VP                  RV P                   RVP                   /,          p\        V4       R# )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' 'rZ   zFound exact matchz$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/   r0   r1   r2   r,   r.   r3   rQ   )xydbg_msgs   && r   rm   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I F GAHHdAHHAIItQYYAIItQYY89
 	g	r   c                	   R pV '       d	   V'       g   \        4       hV P                  V4      ^ 8X  d   ^ # RV : RV P                  : RV: RVP                  : R2	pVP                  R8w  d   \	        RV,           R,           4       ^# VP
                  V P
                  8w  d   \	        VR	,           4       ^# V P                  VP                  8w  d&   VP                  R
8w  d   \	        RV,           4       ^# VP                  R
8w  d8   V! V P                  VP                  4      '       g   \	        RV,           4       ^# VP                  R8X  Ed;   V P                  R8X  d$   V P                  V P                  4      '       d   EMV P                  VP                  8w  d&   RVP                  9  d   \	        RV,           4       ^# V P                  VP                  8w  d   RVP                  9   d   V P                  VP                  8X  d~   \        P                  P                  V P                  VP                  V P                  4      '       g9   \	        RV,           RV P                  : RVP                  : R2,           4       ^# EMV P                  R8w  dT   V P                  VP                  8w  d9   \	        RV,           RV P                  : RVP                  : R2,           4       ^#  \        P                  P                  VP                  V P                  4      pTP                  T8w  d@   RTP                  9  d/   \	        RT,           RTP                  : RT: R2,           4       ^# TP                  T8w  d   RTP                  9   d   T P                  TP                  8X  di   \        P                  P                  YAP                  T P                  4      '       g/   \	        RT,           RT: RTP                  : R2,           4       ^# V P                  VP                  8w  d9   \	        RV,           RV P                  : RVP                  : R2,           4       ^# \	        RV : RV P                  : RV: RVP                  : R2	4       R#   \         d/    \	        RT,           RTP                  ,          ,           4        ^# i ; i)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                   RV 9   g   RV 9   d   W8X  d   R# R# VP                  R4       F_  pW8X  d    R# RV9   g   K  VP                  R4      w  r4\        V 4      \        V4      8  g   KC  \        V 4      \        V4      8:  g   K^   R# 	  R# )z:Returns True if p is an exact match or within a multi rulerX   rl   TF)rf   ro   )test_pto_matchrt   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   zNo fuzzy match 'z (v6=z)' 'z)'r   z(direction) z (not incoming)z (forward does not match)rN   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   rm   r$   r1   r   r2   r(   r&   r/   _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  sU   	" , 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   14R 5SSc                &    VR8X  g   VR8X  d   R# R# )zCheck if address is anywhererP   rO   TF )r
   r   s   &&r   r   UFWRule._is_anywhereN  s    6>T[0r   c                   RpV P                   R8w  g   V P                  R8w  Edb   V P                   : RV P                  : RV P                  : RV P                  : 2pV P                   R8X  d:   V P                  : RV P                  : RV P                  : RV P                  : 2pV P                  R8X  d:   V P                   : RV P                  : RV P
                  : RV P                  : 2pV P                  R8X  d.   V P                  R8X  d   VRV P                  ,          ,          pV# V P                  R8w  d   VRV P                  ,          ,          pV P                  R8w  d   VRV P                  ,          ,          pV# )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    rV   z %sz in_%sz out_%s)	r*   r+   r%   r!   r&   r'   r/   r0   r1   )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                P   V P                   R8w  dJ   V P                  R8w  g   V P                  R8w  d(   \        R4      V P                   ,          p\	        V4      hV P                   \
        P                  P                  9   d/   VR8X  d(   \        R4      V P                   ,          p\	        V4      hV P                   \
        P                  P                  9   dL   V P                  R8w  g   V P                  R8w  d(   \        R4      V P                   ,          p\	        V4      hR# R# )zVerify rulerN   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*   rQ   r   r   r}   ipv4_only_protocolsportless_protocolsr&   r'   )r
   rule_iptyperv   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,   r3   r*   r1   r&   r%   r2   r/   r0   r.   r)   r-   r(   r"   r+   r'   r!   r#   r$   N)rN   rO   rN   rO   r   Fr    )r%   )r   r   r   r   r   r   r   rG   rK   r=   r4   r6   r5   r   r   r7   r8   r   r   rh   r9   r   r:   r   rm   r   r   r   r   r   r   r   s   @r   r   r   *   s     .!F".A F"3#j$'.&`!$$1!)FAFl\ D( (r   r   )r   r[   rp   ufw.utilr   r   programName	state_dir	share_dir	trans_dir
config_dir
prefix_diriptables_dir	do_checksrr   r   r   r   r   r   <module>r      sV    '" 
   			

	 y  `	( `	(r   