ó
¢¢^Oc           @   s¢   d  Z  d d l 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 „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d S(   s'   applications.py: common classes for ufwiÿÿÿÿN(   t   debugt   warn(   t   UFWErrorc         C   s  t  j j |  ƒ s- t d ƒ } t | ƒ ‚ n  d } i  } t  j |  ƒ } | j ƒ  d } t j d ƒ } x–| D]Ž} |  d | } t  j j	 | ƒ sš qn n  | j
 | ƒ r½ t d | ƒ qn n  | j d ƒ s| j d	 ƒ s| j d
 ƒ s| j d ƒ s| j d ƒ s| j d ƒ r+t d | ƒ qn n  d }	 y t  j | ƒ t j }	 Wn. t k
 r{t d ƒ | }
 t |
 ƒ qn n X|	 | k r¨t d ƒ | }
 t |
 ƒ qn n  | |	 | k rØt d ƒ | }
 t |
 ƒ qn n  | |	 7} t j ƒ  } y | j | ƒ Wn. t k
 r/t d ƒ | }
 t |
 ƒ qn n XxÉ| j ƒ  D]»} t | ƒ d k rut d ƒ | }
 t |
 ƒ q=n  t | ƒ s¡t d ƒ | }
 t |
 ƒ q=n  y1 t j j | ƒ t d ƒ | }
 t |
 ƒ w=Wn t k
 rån Xt } x— | j | ƒ D]† \ } } t | ƒ d k r>t d ƒ | }
 t |
 ƒ t } Pn  t | ƒ d k rüt d ƒ i | d 6| d 6}
 t |
 ƒ t } PqüqüW| r’q=n  | j | ƒ r¾t d ƒ | }
 t |
 ƒ n  i  } x' | j | ƒ D] \ } } | | | <qÔW| | | <q=Wqn W| S(   sw   Get profiles found in profiles database.  Returns dictionary with
       profile name as key and tuples for fields
    s!   Profiles directory does not existi
   i   i    s   ^\.t   /s   Skipping '%s': hidden files	   .dpkg-news	   .dpkg-olds
   .dpkg-dists   .rpmnews   .rpmsavet   ~s   Skipping '%s's   Skipping '%s': couldn't stats   Skipping '%s': too bigs*   Skipping '%s': too many files read alreadys   Skipping '%s': couldn't processi@   s   Skipping '%s': name too longs   Skipping '%s': invalid names$   Skipping '%s': also in /etc/servicess   Skipping '%s': field too longs4   Skipping '%(value)s': value too long for '%(field)s't   valuet   fields(   Duplicate profile '%s', using last foundi (  i    (   t   ost   patht   isdirt   _R   t   listdirt   sortt   ret   compilet   isfilet   searchR    t   endswitht   statt   ST_SIZEt	   ExceptionR   t   ConfigParsert   RawConfigParsert   readt   sectionst   lent   valid_profile_namet   ufwt   utilt   get_services_protot   Falset   itemst   Truet   has_key(   t   profiles_dirt   err_msgt   max_sizet   profilest   filest
   total_sizet   patt   ft   abs_patht   sizet   warn_msgt   cdictt   pt   skipt   keyR   t   pdict(    (    s4   /usr/lib/python2.7/dist-packages/ufw/applications.pyt   get_profiles   s¦    









	
c         C   sF   |  d k r t  Sy t |  ƒ t  SWn n Xt j d |  ƒ rB t St  S(   s0   Only accept a limited set of characters for namet   alls    ^[a-zA-Z0-9][a-zA-Z0-9 _\-\.+]*$(   R   t   intR   t   matchR    (   t   name(    (    s4   /usr/lib/python2.7/dist-packages/ufw/applications.pyR   ‰   s    
c         C   s[  d d d g } x | D]y } | j  | ƒ sX t d ƒ i |  d 6| d 6} t | ƒ ‚ q | | s t d ƒ i |  d 6| d 6} t | ƒ ‚ q q W| d j d ƒ } t | ƒ d	 k  rÌ t d
 ƒ |  } t SyO xH | D]@ } t j j | ƒ \ } } t j	 j
 d | | ƒ }	 t |	 ƒ qÖ WWn9 t k
 rV}
 t |
 ƒ t d ƒ |  } t | ƒ ‚ n Xt S(   s'   Make sure profile has everything neededt   titlet   descriptiont   portss/   Profile '%(fn)s' missing required field '%(f)s't   fnR)   s1   Profile '%(fn)s' has empty required field '%(f)s't   |i   s   No ports found in profile '%s't   ACCEPTs   Invalid ports in profile '%s'(   R!   R
   R   t   splitR   R   R   R   t   parse_port_protot   commont   UFWRuleR    R   R    (   R6   t   profilet
   app_fieldsR)   R#   R9   R.   t   portt   protot   rulet   e(    (    s4   /usr/lib/python2.7/dist-packages/ufw/applications.pyt   verify_profileœ   s0    	
	
c         C   s6   d } d } |  j  | ƒ r2 |  | r2 |  | } n  | S(   s#   Retrieve the title from the profilet    R7   (   R!   (   RA   t   sR   (    (    s4   /usr/lib/python2.7/dist-packages/ufw/applications.pyt	   get_title¿   s
    c         C   s6   d } d } |  j  | ƒ r2 |  | r2 |  | } n  | S(   s)   Retrieve the description from the profileRH   R8   (   R!   (   RA   RI   R   (    (    s4   /usr/lib/python2.7/dist-packages/ufw/applications.pyt   get_descriptionÇ   s
    c         C   s?   g  } d } |  j  | ƒ r; |  | r; |  | j d ƒ } n  | S(   s'   Retrieve a list of ports from a profileR9   R;   (   R!   R=   (   RA   R9   R   (    (    s4   /usr/lib/python2.7/dist-packages/ufw/applications.pyt	   get_portsÏ   s
    (   t   __doc__R   R   R   R   t   ufw.utilR   R    R   t
   ufw.commonR   R2   R   RG   RJ   RK   RL   (    (    (    s4   /usr/lib/python2.7/dist-packages/ufw/applications.pyt   <module>   s   	o		#		