ó
â„^c           @   s  d  Z  d d l Z d d l Z d d l j Z y d d l TWn n Xd 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
 l m Z d Z d Z d Z d d d „  ƒ  YZ d e d „ Z d „  Z d d d „  ƒ  YZ d „  Z d S(   s>   
classes and algorithms for the generation of SELinux policy.
iÿÿÿÿN(   t   *i   (   t	   refpolicy(   t   objectmodel(   t   access(   t
   interfaces(   t   matching(   t   utili    i   t   PolicyGeneratorc           B   sŒ   e  Z d  Z d d „ Z d d d „ Z e d „ Z e d „ Z	 d „  Z
 d „  Z d d „ Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z RS(   s¤  Generate a reference policy module from access vectors.

    PolicyGenerator generates a new reference policy module
    or updates an existing module based on requested access
    in the form of access vectors.

    It generates allow rules and optionally module require
    statements and reference policy interfaces. By default
    only allow rules are generated. The methods .set_gen_refpol
    and .set_gen_requires turns on interface generation and
    requires generation respectively.

    PolicyGenerator can also optionally add comments explaining
    why a particular access was allowed based on the audit
    messages that generated the access. The access vectors
    passed in must have the .audit_msgs field set correctly
    and .explain set to SHORT|LONG_EXPLANATION to enable this
    feature.

    The module created by PolicyGenerator can be passed to
    output.ModuleWriter to output a text representation.
    c         C   s[   d |  _ t |  _ t |  _ | r- | |  _ n t j ƒ  |  _	 t |  _
 d |  _ d |  _ d S(   sø   Initialize a PolicyGenerator with an optional
        existing module.

        If the module paramater is not None then access
        will be added to the passed in module. Otherwise
        a new reference policy module will be created.
        N(   t   Nonet   ifgent   NO_EXPLANATIONt   explaint   Falset   gen_requirest   moduelR   t   Modulet   modulet	   dontauditt	   mislabledt   domains(   t   selfR   (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   __init__D   s    					c         C   s;   | r$ t  | | ƒ |  _ t |  _ n	 d |  _ |  j ƒ  d S(   s?  Set whether reference policy interfaces are generated.

        To turn on interface generation pass in an interface set
        to use for interface generation. To turn off interface
        generation pass in None.

        If interface generation is enabled requires generation
        will also be enabled.
        N(   t   InterfaceGeneratorR	   t   TrueR   R   t"   _PolicyGenerator__set_module_style(   R   t   if_sett	   perm_maps(    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   set_gen_refpolX   s
    
	c         C   s   | |  _  d S(   s&  Set whether module requires are generated.

        Passing in true will turn on requires generation and
        False will disable generation. If requires generation is
        disabled interface generation will also be disabled and
        can only be re-enabled via .set_gen_refpol.
        N(   R   (   R   t   status(    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   set_gen_requiresj   s    c         C   s   | |  _  d S(   s)   Set whether access is explained.
        N(   R   (   R   R   (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   set_gen_explaint   s    c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   set_gen_dontaudity   s    c         C   s?   |  j  r t } n t } x  |  j j ƒ  D] } | | _ q( Wd  S(   N(   R	   R   R   R   t   module_declarationsR   (   R   R   t   mod(    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   __set_module_style|   s
    		s   1.0c         C   s…   d } x |  j j ƒ  D] } | } q W| sQ t j ƒ  } |  j j j d | ƒ n  | | _ | | _ |  j	 rx t
 | _ n	 t | _ d S(   s?   Set the name of the module and optionally the version.
        i    N(   R   R   R    R   t   ModuleDeclarationt   childrent   insertt   namet   versionR	   R   R   (   R   R&   R'   t   mR!   (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   set_module_name„   s    
			c         C   s    |  j  r t  |  j ƒ n  |  j S(   N(   R   R   (   R   (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt
   get_module•   s    	c         C   sy   d d  l  } yL | j | j d ƒ } | d j d ƒ d } | | j k rW | |  _ d  SWn t k
 rk n Xd  |  _ d  S(   Niÿÿÿÿi    i   t   :i   (   t   selinuxt   matchpathcont   obj_patht   splitt   tgt_typeR   t   OSErrorR   (   R   t   avR,   t   contextR/   (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   __restore_label   s    	c         C   s&  x| D]} t  j | ƒ } |  j r4 | j | _ n  d | _ |  j rp t t  j t	 | d |  j ƒƒ ƒ | _ n  | j
 ƒ  } | d d k rÁ | j t j k rÁ | j d d j | d ƒ 7_ n  x9 | j D]. } | d k sé | d k ró t } PqË t } qË W| d d  k	 rø| d d k rø| rø| j t j k rø| j t k rMd } n d } | j d	 d j | d
 ƒ d j | d ƒ d j | d ƒ f 7_ | j d k rø| j d d j | d ƒ d j | j ƒ d j | ƒ d j | j ƒ f 7_ qøn  |  j | ƒ |  j d  k	 rZ| j t j k rZ| j d d j | j ƒ d j | j ƒ f 7_ n  | j t j k r~| j d 7_ n  | j t j k r¢| j d 7_ n  | j t j k r#t | j ƒ d k r| j d d j g  | j D] } | d ^ qãƒ 7_ q#| j d | j d d 7_ n  | j t j k r›| j d 7_ | j d 7_ | j d | j d 7_ x+ | j d D] }	 | j d |	 7_ q{Wn  yf| j t j k r d | j k r d | j k sÝd | j k r |  j st t d d ƒd d |  _ n  g  }
 xn g  t  t g i | j! t" 6| j t# 6| j t$ 6ƒ D] } | t% ^ q@D]% } | |  j k rT|
 j& | ƒ qTqTWt |
 ƒ d k r½| j d | j! | j d j |
 ƒ f 7_ q t |
 ƒ d k r | j d | j! | j d j |
 ƒ f 7_ q n  Wn n X|  j' j( j& | ƒ q Wd  S(   Nt    t	   verbosityi    s$   
#!!!! WARNING: '%s' is a base type.i   t   writet   creates   (/.*?)sU   
#!!!! WARNING '%s' is not allowed to write or create to %s.  Change the label to %s.i   sG   
#!!!! $ semanage fcontext -a -t %s %s%s   
#!!!! $ restorecon -R -v %ssY   
#!!!! The file '%s' is mislabeled on your system.  
#!!!! Fix with $ restorecon -R -v %ss0   
#!!!! This avc is allowed in the current policys:   
#!!!! This avc has a dontaudit rule in the current policysH   
#!!!! This avc can be allowed using one of the these booleans:
#     %ss   , s5   
#!!!! This avc can be allowed using the boolean '%s'sŽ   
#!!!! This avc is a constraint violation.  You would need to modify the attributes of either the source or target types to allow this access.s   
#Constraint rule: s   
#	s?   
#	Possible cause is the source %s and target %s are different.t   dirt   openR&   t   domaint   typessL   
#!!!! The source type '%s' can write to a '%s' of the following type:
# %s
sM   
#!!!! The source type '%s' can write to a '%s' of the following types:
# %s
()   R   t   AVRuleR   t	   DONTAUDITt	   rule_typet   commentR   t   strt   Commentt   explain_accesst   base_file_typet   typet	   audit2whyt   ALLOWt   joint   permsR   R   R   t	   obj_classR9   R.   t   _PolicyGenerator__restore_labelR   t   BOOLEANt   lent   datat
   CONSTRAINTt   TERULER   t   seinfot	   ATTRIBUTEt   sesearcht   src_typet   SCONTEXTt   CLASSt   PERMSt   TCONTEXTt   appendR   R$   (   R   t   avsR2   t   rulet	   base_typet   permt
   permissiont   compt   xt   reasonR<   t   i(    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   __add_allow_rules©   sr    			*"#
 	CS!49!	 N.5c         C   sT   |  j  r= |  j  j | |  j ƒ \ } } |  j j j | ƒ n | } |  j | ƒ d S(   sJ   Add the access from the access vector set to this
        module.
        N(   R	   t   genR   R   R$   t   extendt!   _PolicyGenerator__add_allow_rules(   R   t   av_sett	   raw_allowt   ifcalls(    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt
   add_accessó   s
    		c         C   s(   x! | D] } |  j  j j | ƒ q Wd  S(   N(   R   R$   RY   (   R   t   role_type_sett	   role_type(    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   add_role_types  s    N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   t   SHORT_EXPLANATIONR   R   R   R)   R*   RK   Rf   Rj   Rm   (    (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyR   -   s   
					J	c      	      sƒ  g  ‰ ‡  ‡ f d †  } | t  k rø xÇ |  j D]¼ } ˆ j d | j ƒ ˆ j d t | j ƒ t | j ƒ f ƒ ˆ j d | j t j	 | j
 ƒ f ƒ ˆ j d | j | j | j f ƒ ˆ j t j d | j d d d	 d
 d d ƒƒ q. W| ƒ  n‡ | rˆ j d |  j |  j |  j |  j j ƒ  f ƒ t |  j ƒ d k ru|  j d } ˆ j d | j | j | j f ƒ n  | ƒ  n  ˆ S(   sª  Explain why a policy statement was generated.

    Return a string containing a text explanation of
    why a policy statement was generated. The string is
    commented and wrapped and can be directly inserted
    into a policy.

    Params:
      av - access vector representing the access. Should
       have .audit_msgs set appropriately.
      verbosity - the amount of explanation provided. Should
       be set to NO_EXPLANATION, SHORT_EXPLANATION, or
       LONG_EXPLANATION.
    Returns:
      list of strings - strings explaining the access or an empty
       string if verbosity=NO_EXPLANATION or there is not sufficient
       information to provide an explanation.
    c             sg   ˆ  s
 d  Sˆ j  d ƒ xI ˆ  j ƒ  D]; }  t |  j ˆ  j ƒ } ˆ j  d | j ƒ  |  j f ƒ q$ Wd  S(   Ns    Interface options:s      %s # [%d](   RY   t   allt   call_interfacet	   interfaceR2   t	   to_stringt   dist(   t   matcht   ifcall(   t   mlt   s(    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   explain_interfaces  s    s    %ss     scontext="%s" tcontext="%s"s     class="%s" perms="%s"s     comm="%s" exe="%s" path="%s"s	   message="t   "iP   t   initial_indents     t   subsequent_indents      s)    src="%s" tgt="%s" class="%s", perms="%s"i    s    comm="%s" exe="%s" path="%s"(   t   LONG_EXPLANATIONt
   audit_msgsRY   t   headerRA   t   scontextt   tcontextt   tclassR   t   list_to_space_strt   accessest   commt   exet   pathRe   t   textwrapt   wrapt   messageRT   R0   RJ   RI   t   to_space_strRM   (   R2   Ry   R6   R{   t   msg(    (   Ry   Rz   s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyRC   	  s*    	 	#&
	&&
c         C   s+  g  } g  } | j  |  j j ƒ  ƒ | j d d „  d t ƒ t j ƒ  } |  j | _ x¶ t	 t
 | ƒ ƒ D]¢ } | | j t j k r˜ | j j | j ƒ qf | | j t j k rÄ | j j | j ƒ qf | | j t j k rð | j j | j ƒ qf | | j GHd sf t ‚ qf Wt
 | j ƒ d k s't ‚ | S(   Nt   keyc         S   s   |  j  S(   N(   t   num(   t   param(    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   <lambda>E  s    t   reversei    (   Re   t   paramst   valuest   sortR   R   t   InterfaceCallR&   t   ifnamet   rangeRM   RE   t   SRC_TYPEt   argsRY   RT   t   TGT_TYPER0   t	   OBJ_CLASSRJ   t   AssertionError(   Rt   R2   R”   R›   Rx   Rb   (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyRs   @  s"    R   c           B   s/   e  Z d d  „ Z d „  Z d „  Z d „  Z RS(   c         C   s5   | |  _  |  j | ƒ t j | ƒ |  _ g  |  _ d  S(   N(   t   ifst   hack_check_ifsR   t   AccessMatchert   matchert   calls(   R   RŸ   R   (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyR   Z  s    	c         C   sÆ   x¿ | j  j ƒ  D]® } g  } | j | j j ƒ  ƒ | j d d „  d t ƒ xp t t | ƒ ƒ D]\ } | d | | j k rˆ t	 | _
 Pn  | | j t j t j t j g k r^ t	 | _
 Pq^ q^ Wq Wd  S(   NR   c         S   s   |  j  S(   N(   R   (   R‘   (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyR’   h  s    R“   i   (   R   R•   Re   R”   R–   R   R™   RM   R   R   t   enabledRE   R   Rš   Rœ   R   (   R   RŸ   R`   R”   Rb   (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyR    `  s    		c         C   s  |  j  | ƒ } g  } xi |  j D]^ } t | j ƒ  j | j ƒ } | rj t j t | j | | ƒ ƒ | _	 n  | j
 | | f ƒ q Wg  } x | D]y \ } } t }	 xN | D]F }
 |
 j | ƒ r§ |
 j	 rä | j	 rä |
 j	 j | j	 ƒ n  t }	 q§ q§ W|	 sŽ | j
 | ƒ qŽ qŽ W| | f S(   N(   Rw   R£   Rs   t   bestRt   R2   R   RB   RC   R@   RY   R   t   matchest   mergeR   (   R   RZ   R6   t   raw_avRi   Ry   Rx   t   dRŸ   t   foundt   o_ifcall(    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyRd   w  s$    $c         C   sl   g  } x_ | D]W } t  j ƒ  } |  j j |  j | | ƒ t | ƒ rW |  j j | ƒ q | j | ƒ q W| S(   N(   R   t	   MatchListR¢   t
   search_ifsRŸ   RM   R£   RY   (   R   RZ   R¨   R2   t   ans(    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyRw   Ž  s    N(   Rn   Ro   R   R   R    Rd   Rw   (    (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyR   Y  s   		c         C   s.   d „  } x |  j  ƒ  D] } | | ƒ q Wd S(   s*   Add require statements to the module.
    c         S   s  t  j ƒ  } xa |  j ƒ  D]S } | j j | j ƒ | j j | j ƒ x$ | j D] } | j | | j	 ƒ qO Wq Wx8 |  j
 ƒ  D]* } x! | j D] } | j j | ƒ q Wq} Wx: |  j ƒ  D], } | j j | j ƒ | j j | j ƒ q¸ W| j j d ƒ |  j j d | ƒ d  S(   NR   i    (   R   t   Requiret   avrulesR<   t   updatet	   src_typest	   tgt_typest   obj_classest   add_obj_classRI   t   interface_callsR›   t   addt
   role_typest   rolest   rolet   discardR$   R%   (   t   nodet   rt   avrulet   objRx   t   argRl   (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   collect_requiresž  s    N(   t   nodes(   R   RÁ   R¼   (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyR   ›  s    	(    (    (   Rp   t	   itertoolsRŠ   t   selinux.audit2whyRF   t   setoolsR5   R   R   R   R   R   R   R
   Rq   R   R   R   RC   Rs   R   R   (    (    (    s8   /usr/lib64/python2.7/site-packages/sepolgen/policygen.pyt   <module>   s*   Ü7	B