o
    Fa                     @   s   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 r0ddlZnddlZdd Zdd	 Zd
d Zdd Zdd Zdd ZdS )z'applications.py: common classes for ufw    N)debugwarn)UFWError   c                 C   s6  t j| std}t|d}i }t | }|  d}td}|D ]s}| d | }t j	|s4q$|
|r@td|  q$|ds^|ds^|d	s^|d
s^|ds^|dretd|  q$d}	z
t |tj }	W n ty   td| }
t|
 Y q$w |	|krtd| }
t|
 q$||	 |krtd| }
t|
 q$||	7 }tjd dk rt }nt }z|| W n ty   td| }
t|
 Y q$w | D ]}t|dkrtd| }
t|
 qt|std| }
t|
 qztj| td| }
t|
 W q ty   Y nw d}||D ]2\}}t|dkr7td| }
t|
 d} nt|dkrOtd||d }
t|
 d} nq|rUq||v rdtd| }
t|
 i }||D ]	\}}|||< qkzt|| |||< W q ty } z
t| W Y d}~qd}~ww q$|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e r8   2/usr/lib/python3/dist-packages/ufw/applications.pyget_profiles    s   






3r:   c                 C   sB   | dkrdS zt |  W dS  ty   Y nw t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)namer8   r8   r9   r!      s   r!   c              
   C   s   g d}|D ]$}||vrt d| |d }t||| s*t d| |d }t|q|d d}z,|D ]&}tj|\}}|dkrOd|v sKd	|v rOt|tjd
||}	t|	 q5W dS  t	yx }
 zt|
 t d|  }t|d}
~
ww )z'Make sure profile has everything needed)titledescriptionportsz/Profile '%(fn)s' missing required field '%(f)s')fnr.   z1Profile '%(fn)s' has empty required field '%(f)s'rA   |any:,ACCEPTzInvalid ports in profile '%s'NT)
r   r   splitr"   r#   parse_port_protocommonUFWRuler   r   )r>   profile
app_fieldsr.   r(   rA   r3   portprotoruler7   r8   r8   r9   r&      s:   
r&   c                 C   $   d}d}|| v r| | r| | }|S )z#Retrieve the title from the profile r?   r8   rL   sr
   r8   r8   r9   	get_title   
   rU   c                 C   rQ   )z)Retrieve the description from the profilerR   r@   r8   rS   r8   r8   r9   get_description   rV   rW   c                 C   s*   g }d}|| v r| | r| |  d}|S )z'Retrieve a list of ports from a profilerA   rC   )rH   )rL   rA   r
   r8   r8   r9   	get_ports   s
   rX   )__doc__r   r   r   ufw.utilr"   r   r   
ufw.commonr   r   r   r   r   r:   r!   r&   rU   rW   rX   r8   r8   r8   r9   <module>   s"    
x 		