ó
â^c           @   sţ   d  Z  d d l Z d d l Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m	 Z	 d	 d d
     YZ
 d   Z d   Z d   Z d   Z d   Z d d d     YZ d d d     YZ d d d     YZ d d d     YZ d S(   s7   
Classes for representing and manipulating interfaces.
i˙˙˙˙Ni   (   t   access(   t	   refpolicy(   t   objectmodel(   t   matching(   t   _t   Paramc           B   sS   e  Z d  Z d   Z d   Z d   Z e e e  Z e d d    Z d   Z	 RS(   s;   
    Object representing a paramater for an interface.
    c         C   s1   d |  _  t j |  _ t j   |  _ t |  _ d  S(   Nt    (   t   _Param__nameR   t   SRC_TYPEt   typet   IdSett   obj_classest   Truet   required(   t   self(    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   __init__&   s    	c         C   s/   t  j |  s" t d |   n  | |  _ d  S(   Ns   Name [%s] is not a param(   R    t
   is_idparamt
   ValueErrorR   (   R   t   name(    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   set_name,   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   get_name1   s    t   fgetc         C   s   t  |  j d  S(   Ni   (   t   intR   (   R   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   <lambda>6   s    c         C   s*   d |  j  t j |  j d j |  j  f S(   Ns0   <sepolgen.policygen.Param instance [%s, %s, %s]>t    (   R   R   t   field_to_strR	   t   joinR   (   R   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   __repr__8   s    (
   t   __name__t
   __module__t   __doc__R   R   R   t   propertyR   t   numR   (    (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR   "   s   			c         C   s+  d } |  | k ră | |  } | | j  k r/ d  S| t j k sM | t j k rÚ | j  t j k sq | j  t j k rÚ d } | r | j g } n g  } x6 t j | j |  D] } | t j	 k r¨ d } Pq¨ q¨ Wt j | _  qd } n( t
   } |  | _ | | _  | | | j <| r'| j j | j  n  | S(   Ni    i   (   R	   R   R   t   TGT_TYPEt	   obj_classt	   itertoolst   chainR   R   t   implicitly_typed_objectsR   R   t   add(   R   R	   t   avt   paramst   rett   pt   avobjst   obj(    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   __param_insert>   s0    
$				c         C   s  d } t  } t j |  j  rK t |  j t j |  |  d k rK d } qK n  t j |  j  r t |  j t j |  |  d k r d } q n  t j |  j	  rÉ t |  j	 t j
 |  |  d k rÉ d } qÉ n  xA |  j D]6 } t j |  rÓ t | t  d k r	d } q	qÓ qÓ W| S(   sj  Extract the paramaters from an access vector.

    Extract the paramaters (in the form $N) from an access
    vector, storing them as Param objects in a dictionary.
    Some attempt is made at resolving conflicts with other
    entries in the dict, but if an unresolvable conflict is
    found it is reported to the caller.

    The goal here is to figure out how interface paramaters are
    actually used in the interface - e.g., that $1 is a domain used as
    a SRC_TYPE. In general an interface will look like this:

    interface(`foo', `
       allow $1 foo : file read;
    ')

    This is simple to figure out - $1 is a SRC_TYPE. A few interfaces
    are more complex, for example:

    interface(`foo_trans',`
       domain_auto_trans($1,fingerd_exec_t,fingerd_t)

       allow $1 fingerd_t:fd use;
       allow fingerd_t $1:fd use;
       allow fingerd_t $1:fifo_file rw_file_perms;
       allow fingerd_t $1:process sigchld;
    ')

    Here the usage seems ambigious, but it is not. $1 is still domain
    and therefore should be returned as a SRC_TYPE.

    Returns:
      0  - success
      1  - conflict found
    i    i   (   t   FalseR    R   t   src_typeR-   R   R   t   tgt_typeR!   R"   t	   OBJ_CLASSt   permst   PERM(   R'   R(   R)   t	   found_srct   perm(    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   av_extract_paramsj   s     $!!!c         C   s/   t  j |  j  r+ t |  j t j d  |  Sd  S(   N(   R    R   t   roleR-   R   t   ROLEt   None(   R7   R(   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   role_extract_paramsŁ   s    c            sŹ     f d   } d } | |  j  t j  r3 d } n  | |  j t j  rQ d } n  | |  j t j  ro d } n  t j |  j	  r¨ t
 |  j	 t j d     r¨ d } q¨ n  | S(   Nc            sK   d } x> |  D]6 } t  j |  r t | | d     rC d } qC q q W| S(   Ni    i   (   R    R   R-   R9   (   t   setR	   R)   t   x(   R(   (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   extract_from_set¨   s    i    i   (   t	   src_typesR   R   t	   tgt_typesR!   R   R1   R    R   t	   dest_typeR-   t	   DEST_TYPER9   (   t   ruleR(   R=   R)   (    (   R(   s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   type_rule_extract_params§   s    			c         C   sQ   d } xD |  j  D]9 } t j |  r t | t j d  |  rI d } qI q q W| S(   Ni    i   (   t   argsR    R   R-   R   R   R9   (   t   ifcallR(   R)   t   arg(    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   ifcall_extract_paramsŔ   s    t   AttributeVectorc           B   s   e  Z d    Z d   Z RS(   c         C   s   d |  _  t j   |  _ d  S(   NR   (   R   R    t   AccessVectorSet(   R   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR   Ě   s    	c         C   s   |  j  j |  d  S(   N(   R    t   add_av(   R   R'   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyRJ   Đ   s    (   R   R   R   RJ   (    (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyRH   Ë   s   	t   AttributeSetc           B   s#   e  Z d    Z d   Z d   Z RS(   c         C   s   i  |  _  d  S(   N(   t
   attributes(   R   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR   Ô   s    c         C   s   | |  j  | j <d  S(   N(   RL   R   (   R   t   attr(    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   add_attr×   s    c         C   s­   d   } d  } x | D]y } | d  } | d d k r[ | rL |  j |  n  | |  } q | r | j d  } t j |  } | j |  q q W| rŠ |  j |  n  d  S(   Nc         S   sb   |  d d !j    } t |  d k s5 | d d k rH t d |    n  t   } | d | _ | S(   Ni   i˙˙˙˙i   i    t	   Attributes#   Syntax error Attribute statement %s(   t   splitt   lent   SyntaxErrorRH   R   (   t   linet   fieldst   a(    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt
   parse_attrŰ   s    "	i˙˙˙˙i    t   [t   ,(   R9   RN   RP   R    t   AccessVectorRJ   (   R   t   fdRV   RU   RS   t   lR'   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt	   from_fileÚ   s    		
(   R   R   R   RN   R\   (    (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyRK   Ó   s   		t   InterfaceVectorc           B   sG   e  Z d i  d   Z i  d  Z d   Z d   Z d   Z d   Z RS(   c         C   sP   t  |  _ d |  _ t j   |  _ i  |  _ | rC |  j | |  n  t |  _ d  S(   NR   (	   R   t   enabledR   R    RI   R(   t   from_interfaceR.   t   expanded(   R   t	   interfaceRL   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR   ó   s    			c         C   sĐ  | j  |  _  xq | j   D]c } | j t j j k r: q n  d | j  k rO q n  t j |  } x | D] } |  j |  qe Wq W| rHxż | j	   D]Ž } xĽ | j
 D] } | | j
 k rž qŁ n  | j
 | } xo | j D]d }	 t j |	  } | j | j  k r| j | _ n  | j | j  k r,| j | _ n  |  j |  qŐ WqŁ Wq Wn  x) | j   D] }
 t |
 |  j  rUqUqUWx) | j   D] } t | |  j  rqqWx) | j   D] } t | |  j  r­q­q­Wd  S(   Nt	   dontaudit(   R   t   avrulest	   rule_typeR   t   AVRulet   ALLOWR    t   avrule_to_access_vectorsRJ   t   typeattributesRL   t   copyR/   R	   R0   t   rolesR:   R(   t	   typerulesRC   t   interface_callsRG   (   R   Ra   RL   t   avrulet   avsR'   t   typeattributeRM   t   attr_vecRU   R7   RB   RE   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR_     s>    c         C   s/   t  | |  j  d k r n  |  j j |  d  S(   Ni   (   R6   R(   R    RJ   (   R   R'   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyRJ   8  s    c         C   sN   g  } | j  d |  j  x$ |  j D] } | j  t |   q$ Wd j |  S(   Ns   [InterfaceVector %s]s   
(   t   appendR   R    t   strR   (   R   t   sR'   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt	   to_string>  s
    c         C   s
   |  j    S(   N(   R   (   R   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   __str__E  s    c         C   s   d |  j  |  j f S(   Ns   <InterfaceVector %s:%s>(   R   R^   (   R   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR   H  s    N(	   R   R   R9   R   R_   RJ   Rt   Ru   R   (    (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR]   ň   s   4			t   InterfaceSetc           B   s   e  Z d d   Z d   Z d   Z d   Z d   Z d   Z i  d  Z	 d i  d  Z
 d   Z d	   Z d
   Z d   Z RS(   c         C   s(   i  |  _  i  |  _ g  |  _ | |  _ d  S(   N(   t
   interfacest   tgt_type_mapt   tgt_type_allt   output(   R   Rz   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR   M  s    			c         C   s$   |  j  r  |  j  j | d  n  d  S(   Ns   
(   Rz   t   write(   R   Rr   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   oS  s    	c         C   sä   xÝ t  |  j j   d d   D]˝ } | j d | j  xJ t  | j j   d d   D]* } | j d | j t j | j f  qX W| j d  t  | j	 j
    } x1 | D]) } | j d j |   | j d  qŻ Wq Wd  S(	   Nt   keyc         S   s   |  j  S(   N(   R   (   R<   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR   X  s    s   [InterfaceVector %s c         S   s   |  j  S(   N(   R   (   R<   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR   Z  s    s   %s:%s s   ]
RX   s   
(   t   sortedRw   t   valuesR{   R   R(   R   R   R	   R    t   to_listR   (   R   RZ   t   ivt   paramt   avlR'   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   to_fileW  s    %%(c         C   sˇ   d   } d  } x | D]y } | d  } | d d k r[ | rL |  j |  n  | |  } q | r | j d  } t j |  } | j |  q q W| rŠ |  j |  n  |  j   d  S(   Nc         S   sű   |  d d !j    } t |  d k  s5 | d d k rH t d |    n  t   } | d | _ t |  d k rt d  Sx | d D]t } | j  d  } t |  d k rš t d |    n  t   } | d | _ t j | d | _ | | j	 | j <q W| S(	   Ni   i˙˙˙˙i   i    R]   s)   Syntax error InterfaceVector statement %st   :s-   Invalid param in InterfaceVector statement %s(
   RP   RQ   RR   R]   R   R   R   t   str_to_fieldR	   R(   (   RS   RT   t   ifvt   fieldR*   R   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt	   parse_ifvc  s     "		i˙˙˙˙i    RW   RX   (   R9   t   add_ifvRP   R    RY   RJ   t   index(   R   RZ   R   R   RS   R[   R'   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR\   b  s    	
c         C   s   | |  j  | j <d  S(   N(   Rw   R   (   R   R   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR     s    c         C   s­   xŚ |  j  j   D] } t   } xP | j D]E } t j | j  r^ |  j j |  t   } Pn  | j | j  q) Wx0 | D]( } |  j	 j
 | g   } | j |  qy Wq Wd  S(   N(   Rw   R   R;   R    R   R0   Ry   Rq   R&   Rx   t
   setdefault(   R   R   R?   R'   R	   R[   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR     s    		c         C   s    t  | |  } |  j |  d  S(   N(   R]   R   (   R   Ra   RL   R   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR&     s    c         C   sT   x6 t  j | j   | j    D] } |  j | |  q W|  j |  |  j   d  S(   N(   R#   R$   Rw   t	   templatesR&   t   expand_ifcallsR   (   R   t   headersRz   RL   t   i(    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   add_headers  s    %c         C   sq   t  j |  rf t | d  } | t | j  k r8 d  S| j | d } t | t  r\ | S| g Sn | g Sd  S(   Ni   (   R    R   R   RQ   RD   R9   t
   isinstancet   list(   R   t   idRE   R    RF   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt	   map_paramĄ  s    
c         C   s&  |  j  | j |  } | d  k r% d  S|  j  | j |  } | d  k rJ d  S|  j  | j |  } | d  k ro d  St j   } xB | j D]7 } |  j  | |  }	 |	 d  k rŻ q q | j |	  q Wt	 |  d k rÖ d  SxI | D]A }
 x8 | D]0 } x' | D] } | j
 j |
 | | |  q÷ Wqę WqÝ Wd  S(   Ni    (   R   R/   R9   R0   R"   R   R
   R2   t   updateRQ   R    R&   (   R   R   R'   RE   R>   R?   R   t	   new_permsR5   R*   R/   R0   R"   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt
   map_add_avą  s*    c         C   sD  | d  f g } |  j | j } t | _ xt |  d k r?| j d  \ } } |  j | j } | | k r§ x$ | j D] } |  j | | |  qx W| j r§ q+ q§ n  x | j	   D] }	 |	 j
 | j k ră |  j t d   d  Sy | |	 j
 }
 Wn. t k
 r$|  j t d |	 j
   q´ n X| j |
 |	 f  q´ Wq+ Wd  S(   Ni    i˙˙˙˙s   Found circular interface classs#   Missing interface definition for %s(   R9   Rw   R   R   R`   RQ   t   popR    R   Rl   t   ifnameR|   R   t   KeyErrorRq   (   R   Ra   t
   if_by_namet   stackR   t   curt
   cur_ifcallt   cur_ifvR'   RE   t   newif(    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   do_expand_ifcallsÍ  s*    			c         C   sy   i  } x3 t  j | j   | j    D] } | | | j <q% Wx6 t  j | j   | j    D] } |  j | |  q[ Wd  S(   N(   R#   R$   Rw   R   R   R˘   (   R   R   R   R   Ra   (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyR   ń  s
    %%N(   R   R   R9   R   R|   R   R\   R   R   R&   R   R   R   R˘   R   (    (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyRv   L  s   			#					$(    (    (    (    (    (   R   Ri   R#   R   R    R   R   R   t   sepolgeni18nR   R   R-   R6   R:   RC   RG   RH   RK   R]   Rv   (    (    (    s9   /usr/lib64/python2.7/site-packages/sepolgen/interfaces.pyt   <module>   s"   	,	9			Z