3
\                 @   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 r`d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   sl  t jj| std}t|d }i }t j| }|j  d}tjd}x |D ]}| d | }t jj	|slqL|j
|rtd|  qL|jds|jd	s|jd
s|jds|jds|jdrtd|  qLd}	yt j|tj }	W n, tk
r   td| }
t|
 wLY nX |	|kr4td| }
t|
 qL||	 |krXtd| }
t|
 qL||	7 }tjd dk rztj }ntj }y|j| W n, tk
r   td| }
t|
 wLY nX x|j D ]}t|dkrtd| }
t|
 qt|std| }
t|
 qy(tjj| td| }
t|
 wW n tk
rV   Y nX d}xn|j|D ]`\}}t|dkrtd| }
t|
 d}P t|dkrhtd||d }
t|
 d}P qhW |r֐q||krtd| }
t|
 i }x |j|D ]\}}|||< qW yt|| |||< W n, tk
r\ } zt| W Y dd}~X nX qW qLW |S )!zwGet profiles found in profiles database.  Returns dictionary with
       profile name as key and tuples for fields
    z!Profiles directory does not exist
   i   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 longTz4Skipping '%(value)s': value too long for '%(field)s')valuefieldz(Duplicate profile '%s', using last foundNi (  i   )ospathisdir_r   listdirsortrecompileisfilesearchr   endswithstatST_SIZE	Exceptionr   sysversion_infoConfigParserZRawConfigParserconfigparserreadZsectionslenvalid_profile_nameufwutilZget_services_protoitemsverify_profile)Zprofiles_direrr_msgZmax_sizeZprofilesfilesZ
total_sizeZpatfZabs_pathsizeZwarn_msgZcdictpskipkeyr
   Zpdicte r-   2/usr/lib/python3/dist-packages/ufw/applications.pyget_profiles    s    







$r/   c             C   sB   | dkrdS yt |  dS  tk
r,   Y nX 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)namer-   r-   r.   r       s    r    c             C   s  dddg}xR|D ]J}||kr8t d| |d }t|q|| st d| |d }t|qW |d jd}yZxT|D ]L}tjj|\}}|dkrd	|ksd
|krt|tjjd||}	t|	 qtW W n@ t	k
r }
 z"t|
 t d|  }t|W Y dd}
~
X nX dS )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'|any:,ZACCEPTzInvalid ports in profile '%s'NT)
r   r   splitr!   r"   Zparse_port_protocommonZUFWRuler   r   )r3   profileZ
app_fieldsr'   r%   r6   r)   ZportprotoZruler,   r-   r-   r.   r$      s.    



r$   c             C   s$   d}d}|| kr | | r | | }|S )z#Retrieve the title from the profile r4   r-   )r>   sr   r-   r-   r.   	get_title   s
    rB   c             C   s$   d}d}|| kr | | r | | }|S )z)Retrieve the description from the profiler@   r5   r-   )r>   rA   r   r-   r-   r.   get_description   s
    rC   c             C   s*   g }d}|| kr&| | r&| | j d}|S )z'Retrieve a list of ports from a profiler6   r8   )r<   )r>   r6   r   r-   r-   r.   	get_ports   s
    rD   )__doc__r   r   r   Zufw.utilr!   r   r   Z
ufw.commonr   r   r   r   r   r/   r    r$   rB   rC   rD   r-   r-   r-   r.   <module>   s    
x 		