ó
£ãˆMc           @   s˜   d  d l  Td  d l Z d  d l Z d  d l Td  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(   iÿÿÿÿ(   t   *N(   t   debugt   warn(   t   UFWErrorc         C   s  t  j j |  ƒ s1 t d ƒ |  } t d ƒ ‚ n  d } i  } t  j |  ƒ } | j ƒ  d } t j d ƒ } x| D]ˆ} |  d | } t  j j	 | ƒ sž qr n  | j
 | ƒ rÁ t d | ƒ qr n  | j d	 ƒ s| j d
 ƒ s| j d ƒ s| j d ƒ s| j d ƒ s| j d ƒ r/t d | ƒ qr n  d }	 y t  j | ƒ t }	 Wn. t k
 r|t d ƒ | }
 t |
 ƒ qr n X|	 | k r©t d ƒ | }
 t |
 ƒ qr n  | |	 | k rÙt d ƒ | }
 t |
 ƒ qr n  | |	 7} t ƒ  } y | j | ƒ Wn. t k
 r-t d ƒ | }
 t |
 ƒ qr n XxÉ| j ƒ  D]»} t | ƒ d k rs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;Wqr 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 exists(   Error: 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   RawConfigParsert   readt   sectionst   lent   valid_profile_namet   ufwt   utilt   get_services_protot   Falset   itemst   Truet   has_key(   t   dirt   err_msgt   max_sizet   profilest   filest
   total_sizet   patt   ft   abst   sizet   warn_msgt   cdictt   pt   skipt   keyR   t   pdict(    (    s4   /usr/lib/python2.7/dist-packages/ufw/applications.pyt   get_profiles   s¦    




	




	
c         C   s*   |  d k r t  St j d |  ƒ r& t St  S(   s0   Only accept a limited set of characters for namet   alls   ^[a-zA-Z][a-zA-Z0-9 _\-\.+]*$(   R   R   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    (   R5   t   profilet
   app_fieldsR)   R#   R8   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    R6   (   R!   (   R@   t   strR   (    (    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 profileRG   R7   (   R!   (   R@   RH   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 profileR8   R:   (   R!   R<   (   R@   R8   R   (    (    s4   /usr/lib/python2.7/dist-packages/ufw/applications.pyt	   get_portsÉ   s
    (   t   ConfigParserR   R   R   t   ufw.utilR   R   R   t
   ufw.commonR   R2   R   RF   RI   RJ   RK   (    (    (    s4   /usr/lib/python2.7/dist-packages/ufw/applications.pyt   <module>   s   

	o		#		