
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
Klfw                 @   s   d Z ddlZddlZyddlmZ W n  ek
rD   ddlmZ Y nX ddlZddlZddl	Z	ddl
Z
ddlmZ dd ZG dd	 d	Zd
d ZG dd dZdS )zU
Utilities for dealing with the compilation of modules and creation
of module tress.
    N)getstatusoutput   )defaultsc             C   s0   t jd| }t|dkr(| d j r(dS dS dS )z'Check that a module name is valid.
    z[^a-zA-Z0-9_\-\.]r   TFN)refindalllenisalpha)modnamem r   /usr/lib/python3.6/module.pyis_valid_name(   s    r   c               @   sN   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dddZ
dS )
ModuleTreec             C   s   || _ d | _d S )N)r	   dirname)selfr	   r   r   r   __init__2   s    zModuleTree.__init__c             C   s   | j S )N)r   )r   r   r   r   dir_name6   s    zModuleTree.dir_namec             C   s   | j d | j d S )N/z.te)r   r	   )r   r   r   r   te_name9   s    zModuleTree.te_namec             C   s   | j d | j d S )Nr   z.fc)r   r	   )r   r   r   r   fc_name<   s    zModuleTree.fc_namec             C   s   | j d | j d S )Nr   z.if)r   r	   )r   r   r   r   if_name?   s    zModuleTree.if_namec             C   s   | j d | j d S )Nr   z.pp)r   r	   )r   r   r   r   package_nameB   s    zModuleTree.package_namec             C   s
   | j d S )Nz	/Makefile)r   )r   r   r   r   makefile_nameE   s    zModuleTree.makefile_nameNc             C   s   |d | j  | _tj| j t| j d}|r>|jd|  n|jdtj   |j	  t| j
 dj	  t| j dj	  t| j dj	  d S )Nr   wzinclude )r	   r   osmkdiropenr   writer   refpolicy_makefilecloser   r   r   )r   Zparent_dirnameZmakefile_includefdr   r   r   createH   s    zModuleTree.create)N)__name__
__module____qualname__r   r   r   r   r   r   r   r!   r   r   r   r   r   1   s   r   c             C   s   t jjt jj| d d S )Nr   r   )r   pathsplitextsplit)
sourcenamer   r   r   modname_from_sourcenameX   s    r)   c               @   sT   e Zd ZdZdddZdd Zdd Zd	d
 ZdddZdd Z	dd Z
dd ZdS )ModuleCompilerat  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.
    Nc             C   s<   t j | _d| _d| _d| _|| _d| _tj	 | _
d| _dS )zCreate a ModuleCompiler instance, optionally with an
        output file object for verbose output of the compilation process.
        Tz/usr/bin/checkmodulez/usr/bin/semodule_package z/usr/bin/makeN)selinuxZis_selinux_mls_enabledmlsmodulecheckmodulesemodule_packageoutputlast_outputr   r   refpol_makefilemake)r   r1   r   r   r   r   t   s    

zModuleCompiler.__init__c             C   s    | j r| j j|d  || _d S )N
)r1   r   r2   )r   strr   r   r   o   s    zModuleCompiler.oc             C   s$   | j | t|\}}| j | |S )N)r7   r   )r   commandrcr1   r   r   r   run   s    

zModuleCompiler.runc             C   sJ   |j d}t|dk r td|dj|dd }|d }|d }||fS )	zGenerate 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).
        .   z,invalid sourcefile name %s (must end in .te)r   r   z.modz.pp)r'   r   RuntimeErrorjoin)r   r(   Z	splitnamebasenamer	   packagenamer   r   r   gen_filenames   s    

zModuleCompiler.gen_filenamesTc             C   sD   |r| j | n0| j|\}}| j|| | j|| tj| dS )a  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)refpol_buildrB   compilepackager   unlink)r   r(   Z	refpolicyr	   rA   r   r   r   create_module_package   s    z$ModuleCompiler.create_module_packagec             C   s4   | j d | j }| j|}|dkr0td| j d S )Nz -f r   zcompilation failed:
%s)r4   r3   r:   r>   r2   )r   r(   r8   r9   r   r   r   rC      s    
zModuleCompiler.refpol_buildc             C   sp   | j g}| jr|jd | jr(|jd |jd |j| |j| | jdj|}|dkrltd| j d S )Nz-Mz-mz-o r   zcompilation failed:
%s)r/   r-   appendr.   r:   r?   r>   r2   )r   r(   r	   sr9   r   r   r   rD      s    




zModuleCompiler.compilec             C   sZ   | j g}|jd |j| |jd |j| | jdj|}|dkrVtd| j d S )Nz-oz-mrH   r   zpackaging failed [%s])r0   rI   r:   r?   r>   r2   )r   r	   rA   rJ   r9   r   r   r   rE      s    



zModuleCompiler.package)N)T)r"   r#   r$   __doc__r   r7   r:   rB   rG   rC   rD   rE   r   r   r   r   r*   [   s   

	r*   )rK   r   Ztempfile
subprocessr   ImportErrorZcommandsr   Zos.pathZshutilr,   r+   r   r   r   r)   r*   r   r   r   r   <module>   s   	'