B
    u&`                 @   s
  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 e  d dlT d dl	Z	d dl
Z
d dlZd dlZd dlmZ d	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 Zd$ddZd%ddZed kreeejd! ejd" ejd#d  dS )&    )print_function)absolute_import)division)unicode_literals)standard_library)*N)
native_strz/etc/cagefs/filtersP   c             G   s   | rt ||  d S )N)print)debugmsgargs r   !/usr/share/cagefs/check_params.pydmesg   s    r   c             C   s   y:t j| }tt jtd| d}t|}|  W n t	k
rN   dS X t
|dkrxd|kstd|kstd|krx|S || |ddS )	z*
    Load JSON config by command name
    z%s.jsonrN   allowdenyrestrict_pathdefault)ospathbasenameopenjoinCONFIGS_DIRjsonloadclose	Exceptionlenget)command_pathnamefZfull_configr   r   r   load_config   s    
r&   c             C   s
   |  dS )z
    Return True if arg is option name, not parameter of an option
    :param arg: option or parameter
    :type arg: string
    -)
startswith)argr   r   r   is_option_name5   s    r*   c             C   s.   x(| D ] }x|D ]}| |rdS qW qW dS )z*
    Check denied params in args list
    TF)r(   )r   	deny_listr)   optr   r   r   has_denied_params>   s
    


r-   c             C   s&   x | D ]}t |r||krdS qW dS )z-
    Check is all args allow for program
    TF)r*   )r   
allow_listr)   r   r   r   has_extra_paramsI   s    
r/   c             G   s0   t td t  tt jB | |  t   dS )z4
    Wrapper for syslog or other logging system
    zcagefs.check_paramsN)syslogZopenlogr   LOG_AUTHPRIVZLOG_PIDZcloselog)messager   r   r   r   to_logS   s    r3   c             C   s&   | dkrdS | d dkr"d| f S | S )N /z%s/r   )r   r   r   r   addslash\   s
    
r7   c             C   st   t tj|}d| }| dks*| dr>tj| d|S | |ksT| |d rhtj| ||S tj| S )N~z~/r5   )r7   r   r   realpathr(   replace)r   userhome_dirZuserpathr   r   r   
expanduserd   s    r=   Fc          	   C   s$  t tj|}xt|D  ]\}}||kry||d  }	W n tk
rT   wY nX t|	| |}	t |	}	|	|st|d||| ||d   t	d||| ||d   dS qxj|D ]b}
||
r|t
|
d }	t|	| |}	t |	}	|	|st|d|||  t	d|||  dS qW qW dS )aT  
    Return True when args contain paths that refer outside of user's home directory
    :param args: parameters (options) from command line
    :type args: list of strings
    :param restrict_path_list: names of parameters (options) that should use paths inside user's home directory only
    :type restrict_path_list: list of strings
    r   z0Attempt to call program %s with %s %s parametersTNz,Attempt to call program %s with %s parameterF)r7   r   r   r9   	enumerate
IndexErrorr=   r(   r   r3   r!   )r;   homedirr#   r   restrict_path_listr   r<   ir)   r   r,   r   r   r   
check_pathn   s.    



rC   c       
      C   sN  t |dkrt|d dS t |dk r:t|d|d  dS |d }|dd }t|}t|dt| |szt|d| dS |d	d}|d
d}|dd}	|s|s|	st|d dS |r|rt|d dS |rt||rt|d| td| dS |r"t||r"t|d| td| dS |	r@t| ||||	|r@dS t|d dS )z
    Program main function
    :params - list of strings that specify command and its parameters, such as ['/path/command', '-a', 'arg', '-C', '/path/to/config']
    r   zNo parameters specifiedr      z8Command has no parameters. Allow execution of command %sNz
config: %sz/Config not found. Allow execution of command %sr   r   r   z,empty config - allow user to run the commandzWinvalid config - both allow and deny lists are specified. allow user to run the commandz1Attempt to call program %s with denied parametersz0Attempt to call program %s with extra parameterszExecution allowed)	r!   r   r&   strr"   r-   r3   r/   rC   )
r;   r@   Zparamsr   r#   r   Zconfigr.   r+   rA   r   r   r   main   sD    





rF   __main__r   rD      )F)F)Z
__future__r   r   r   r   Zfuturer   Zinstall_aliasesbuiltinsr   r   sysr0   Zfuture.utilsr   r   r1   r   r&   r*   r-   r/   r3   r7   r=   rC   rF   __name__exitargvr   r   r   r   <module>   s2   	
	

%
5
