
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
Kl„fw  ã               @   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 )z‡Create 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 )	zþ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).
        Ú.é   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   	'