
    #fd                         d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	 ddl
Z
e
j        d         dk     rddlZnddlZd Zd Zd Zd	 Zd
 Zd ZdS )z'applications.py: common classes for ufw    N)debugwarn)UFWError   c                 	   t           j                            |           st          d          }t	          |          d}i }t          j        |           }|                                 d}t          j        d          }|D ]}| dz   |z   }t           j        	                    |          s+|
                    |          rt          d|z             S|                    d          si|                    d          sT|                    d	          s?|                    d
          s*|                    d          s|                    d          rt          d|z             d}		 t          j        |          t          j                 }	n2# t          $ r% t          d          |z  }
t!          |
           Y :w xY w|	|k    r#t          d          |z  }
t!          |
           g||	z   |k    r#t          d          |z  }
t!          |
           ||	z  }t"          j        d         dk     rt'          j                    }nt+          j                    }	 |                    |           n2# t          $ r% t          d          |z  }
t!          |
           Y w xY w|                                D ]}t1          |          dk    r"t          d          |z  }
t!          |
           8t3          |          s"t          d          |z  }
t!          |
           i	 t4          j                            |           t          d          |z  }
t!          |
           # t          $ r Y nw xY wd}|                    |          D ]x\  }}t1          |          dk    r%t          d          |z  }
t!          |
           d} n<t1          |          dk    r(t          d          ||dz  }
t!          |
           d} ny|rO||v r!t          d          |z  }
t!          |
           i }|                    |          D ]
\  }}|||<   	 t=          ||           |||<   # t          $ r}t!          |           Y d}~d}~ww xY w	|S )zwGet profiles found in profiles database.  Returns dictionary with
       profile name as key and tuples for fields
    z!Profiles directory does not existi   r   z^\./zSkipping '%s': hidden filez	.dpkg-newz	.dpkg-oldz
.dpkg-distz.rpmnewz.rpmsave~zSkipping '%s'zSkipping '%s': couldn't statzSkipping '%s': too bigz*Skipping '%s': too many files read alreadyr   zSkipping '%s': couldn't process@   zSkipping '%s': name too longzSkipping '%s': invalid namez$Skipping '%s': also in /etc/servicesFzSkipping '%s': field too longTi   z4Skipping '%(value)s': value too long for '%(field)s')valuefieldz(Duplicate profile '%s', using last foundN)ospathisdir_r   listdirsortrecompileisfilesearchr   endswithstatST_SIZE	Exceptionr   sysversion_infoConfigParserRawConfigParserconfigparserreadsectionslenvalid_profile_nameufwutilget_services_protoitemsverify_profile)profiles_direrr_msgmax_sizeprofilesfiles
total_sizepatfabs_pathsizewarn_msgcdictpskipkeyr   pdictes                     2/usr/lib/python3/dist-packages/ufw/applications.pyget_profilesr;       s    7==&&  788wHHJ|$$E	JJLLLJ
*V

C c c#%)w~~h'' 	::a== 	.!4555::k"" 	ajj&=&= 	::l##	'(zz)'<'<	::j!!	%&ZZ__	 /Q'((( 	78$$T\2DD 	 	 	788A>HNNNH	
 (??122a8HNNNx''EFF!LHNNNd
A"" 022EE 022E	JJx     	 	 	:;;qAHNNNH	 !! 1	 1	A1vv{{;<<BX%a(( :;;qAX++A...CDDJX    D#kk!nn  
Us88b== !@AAQGHNNNDEu::$$  "/  0  0,-"<"< >H NNNDE %  H}}GHHANXE#kk!nn # #
U"c

q%(((#   Qa1	f OsO   )$F+F=<F=I..+JJA M
M,+M,Q""
R,RRc                     | dk    rdS 	 t          |            dS # t          $ r Y nw xY wt          j        d|           rdS dS )z0Only accept a limited set of characters for nameallFz ^[a-zA-Z0-9][a-zA-Z0-9 _\-\.+]*$T)intr   r   match)names    r:   r#   r#      sh     u}}uD			u   
 
x3T:: t5s    
((c                 f   g d}|D ]V}||vr$t          d          | |dz  }t          |          ||         s$t          d          | |dz  }t          |          W|d                             d          }	 |D ]q}t          j                            |          \  }}|dk    rd|v sd	|v rt          |          t          j                            d
||          }	t          |	           rnB# t          $ r5}
t          |
           t          d          | z  }t          |          d}
~
ww xY wdS )z'Make sure profile has everything needed)titledescriptionportsz/Profile '%(fn)s' missing required field '%(f)s')fnr0   z1Profile '%(fn)s' has empty required field '%(f)s'rD   |any:,ACCEPTzInvalid ports in profile '%s'NT)
r   r   splitr$   r%   parse_port_protocommonUFWRuler   r   )r@   profile
app_fieldsr0   r*   rD   r5   portprotoruler9   s              r:   r(   r(      s   222J 	$ 	$GIJJ $1--/G 7### 	$KLL"&Q//1G7###	$
 G""3''E  	 	AH55a88MT5~~3$;;#++w''':%%ht<<D$KKKK	       a344=w 
 4s   :A4C/ /
D.90D))D.c                 6    d}d}|| v r| |         r| |         }|S )z#Retrieve the title from the profile rB    rO   sr   s      r:   	get_titlerY      s.    
AEGENENH    c                 6    d}d}|| v r| |         r| |         }|S )z)Retrieve the description from the profilerU   rC   rV   rW   s      r:   get_descriptionr\      s.    
AEGENENHrZ   c                 \    g }d}|| v r#| |         r| |                              d          }|S )z'Retrieve a list of ports from a profilerD   rF   )rK   )rO   rD   r   s      r:   	get_portsr^      s<    EEGEN$$S))LrZ   )__doc__r   r   r   ufw.utilr$   r   r   
ufw.commonr   r   r   r   r   r;   r#   r(   rY   r\   r^   rV   rZ   r:   <module>rb      s    - -" 
			 				                         



Au u up  (  @        rZ   