U
    \                     @   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   sf  t j| std}t|d}i }t | }|  d}td}|D ]}| d | }t j	|shqH|
|rtd|  qH|ds|ds|d	s|d
s|ds|drtd|  qHd}	zt |tj }	W n. tk
r   td| }
t|
 Y qHY nX |	|kr2td| }
t|
 qH||	 |krVtd| }
t|
 qH||	7 }tjd dk rxt }nt }z|| W n. tk
r   td| }
t|
 Y qHY nX | D ]}t|dkrtd| }
t|
 qt|std| }
t|
 qz*tj| td| }
t|
 W qW n tk
rT   Y nX d}||D ]h\}}t|dkrtd| }
t|
 d} qt|dkrdtd||d }
t|
 d} qΐqd|rؐq||krtd| }
t|
 i }||D ]\}}|||< qzt|| |||< W n, tk
rZ } zt| W 5 d}~X Y nX qqH|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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   sD   | dkrdS zt |  W dS  tk
r.   Y nX 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)namer,   r,   r-   r      s    r   c              
   C   s  dddg}|D ]J}||kr6t d| |d }t|q|| st d| |d }t|q|d d}zV|D ]L}tj|\}}|dkrd	|ksd
|krt|tjd||}	t|	 qnW n> t	k
r }
 z t|
 t d|  }t|W 5 d}
~
X Y 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   )r2   profileZ
app_fieldsr&   r$   r5   r(   ZportprotoZruler+   r,   r,   r-   r#      s2    


r#   c                 C   s$   d}d}|| kr | | r | | }|S )z#Retrieve the title from the profile r3   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?   r4   r,   r@   r,   r,   r-   get_description   s
    rC   c                 C   s*   g }d}|| kr&| | r&| |  d}|S )z'Retrieve a list of ports from a profiler5   r7   )r;   )r=   r5   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 		