ó
â„^c           @   sÒ   d  Z  d d l Z d d l Z y d d l m Z Wn! e k
 rU d d l m Z n Xd d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l m Z d „  Z d d d „  ƒ  YZ d	 „  Z d
 d d „  ƒ  YZ d S(   sU   
Utilities for dealing with the compilation of modules and creation
of module tress.
iÿÿÿÿN(   t   getstatusoutputi   (   t   defaultsc         C   s@   t  j d |  ƒ } t | ƒ d k r8 |  d j ƒ  r8 t St Sd S(   s'   Check that a module name is valid.
    s   [^a-zA-Z0-9_\-\.]i    N(   t   ret   findallt   lent   isalphat   Truet   False(   t   modnamet   m(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   is_valid_name(   s    "t
   ModuleTreec           B   sS   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z
 RS(	   c         C   s   | |  _  d  |  _ d  S(   N(   R   t   Nonet   dirname(   t   selfR   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   __init__2   s    	c         C   s   |  j  S(   N(   R   (   R   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   dir_name6   s    c         C   s   |  j  d |  j d S(   Nt   /s   .te(   R   R   (   R   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   te_name9   s    c         C   s   |  j  d |  j d S(   NR   s   .fc(   R   R   (   R   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   fc_name<   s    c         C   s   |  j  d |  j d S(   NR   s   .if(   R   R   (   R   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   if_name?   s    c         C   s   |  j  d |  j d S(   NR   s   .pp(   R   R   (   R   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   package_nameB   s    c         C   s   |  j  d S(   Ns	   /Makefile(   R   (   R   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   makefile_nameE   s    c         C   sÃ   | d |  j  |  _ t j |  j ƒ t |  j ƒ  d ƒ } | rS | j d | ƒ n | j d t j ƒ  ƒ | j	 ƒ  t |  j
 ƒ  d ƒ j	 ƒ  t |  j ƒ  d ƒ j	 ƒ  t |  j ƒ  d ƒ j	 ƒ  d  S(   NR   t   ws   include (   R   R   t   ost   mkdirt   openR   t   writeR   t   refpolicy_makefilet   closeR   R   R   (   R   t   parent_dirnamet   makefile_includet   fd(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   createH   s    
N(   t   __name__t
   __module__R   R   R   R   R   R   R   R   R!   (    (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyR   1   s   							c         C   s$   t  j j t  j j |  ƒ d ƒ d S(   Ni   i    (   R   t   patht   splitextt   split(   t
   sourcename(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   modname_from_sourcenameX   s    t   ModuleCompilerc           B   s\   e  Z d  Z d	 d „ Z d „  Z d „  Z d „  Z e d „ Z	 d „  Z
 d „  Z d „  Z RS(
   st  ModuleCompiler eases running of the module compiler.

    The ModuleCompiler class encapsulates running the commandline
    module compiler (checkmodule) and module packager (semodule_package).
    You are likely interested in the create_module_package method.
    
    Several options are controlled via paramaters (only effects the 
    non-refpol builds):
    
     .mls          [boolean] Generate an MLS module (by passed -M to
                   checkmodule). True to generate an MLS module, false
                   otherwise.
                   
     .module       [boolean] Generate a module instead of a base module.
                   True to generate a module, false to generate a base.
                   
     .checkmodule  [string] Fully qualified path to the module compiler.
                   Default is /usr/bin/checkmodule.
                   
     .semodule_package [string] Fully qualified path to the module
                   packager. Defaults to /usr/bin/semodule_package.
     .output       [file object] File object used to write verbose
                   output of the compililation and packaging process.
    c         C   sX   t  j ƒ  |  _ t |  _ d |  _ d |  _ | |  _ d |  _ t	 j
 ƒ  |  _ d |  _ d S(   s‡   Create a ModuleCompiler instance, optionally with an
        output file object for verbose output of the compilation process.
        s   /usr/bin/checkmodules   /usr/bin/semodule_packaget    s   /usr/bin/makeN(   t   selinuxt   is_selinux_mls_enabledt   mlsR   t   modulet   checkmodulet   semodule_packaget   outputt   last_outputR   R   t   refpol_makefilet   make(   R   R1   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyR   t   s    					c         C   s-   |  j  r  |  j  j | d ƒ n  | |  _ d  S(   Ns   
(   R1   R   R2   (   R   t   str(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   o   s    	c         C   s0   |  j  | ƒ t | ƒ \ } } |  j  | ƒ | S(   N(   R6   R    (   R   t   commandt   rcR1   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   run†   s    c         C   sg   | j  d ƒ } t | ƒ d k  r3 t d | ƒ ‚ n  d j | d d !ƒ } | d } | d } | | f S(   sþ   Generate the module and policy package filenames from
        a source file name. The source file must be in the form
        of "foo.te". This will generate "foo.mod" and "foo.pp".
        
        Returns a tuple with (modname, policypackage).
        t   .i   s,   invalid sourcefile name %s (must end in .te)i    iÿÿÿÿs   .mods   .pp(   R&   R   t   RuntimeErrort   join(   R   R'   t	   splitnamet   basenameR   t   packagename(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   gen_filenames   s    

c         C   s\   | r |  j  | ƒ nB |  j | ƒ \ } } |  j | | ƒ |  j | | ƒ t j | ƒ d S(   sŒ  Create a module package saved in a packagename from a
        sourcename.

        The create_module_package creates a module package saved in a
        file named sourcename (.pp is the standard extension) from a
        source file (.te is the standard extension). The source file
        should contain SELinux policy statements appropriate for a
        base or non-base module (depending on the setting of .module).

        Only file names are accepted, not open file objects or
        descriptors because the command line SELinux tools are used.

        On error a RuntimeError will be raised with a descriptive
        error message.
        N(   t   refpol_buildR@   t   compilet   packageR   t   unlink(   R   R'   t	   refpolicyR   R?   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   create_module_packagež   s    c         C   sI   |  j  d |  j } |  j | ƒ } | d k rE t d |  j ƒ ‚ n  d  S(   Ns    -f i    s   compilation failed:
%s(   R4   R3   R9   R;   R2   (   R   R'   R7   R8   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyRA   ¶   s    c         C   s£   |  j  g } |  j r% | j d ƒ n  |  j r> | j d ƒ n  | j d ƒ | j | ƒ | j | ƒ |  j d j | ƒ ƒ } | d k rŸ t d |  j ƒ ‚ n  d  S(   Ns   -Ms   -ms   -ot    i    s   compilation failed:
%s(   R/   R-   t   appendR.   R9   R<   R;   R2   (   R   R'   R   t   sR8   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyRB   ¿   s    		c         C   s~   |  j  g } | j d ƒ | j | ƒ | j d ƒ | j | ƒ |  j d j | ƒ ƒ } | d k rz t d |  j ƒ ‚ n  d  S(   Ns   -os   -mRG   i    s   packaging failed [%s](   R0   RH   R9   R<   R;   R2   (   R   R   R?   RI   R8   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyRC   Í   s    N(   R"   R#   t   __doc__R   R   R6   R9   R@   R   RF   RA   RB   RC   (    (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyR)   [   s   						(    (    (   RJ   R   t   tempfilet
   subprocessR    t   ImportErrort   commandsR   t   os.patht   shutilR+   R*   R   R
   R   R(   R)   (    (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/module.pyt   <module>   s   		'	