o
    c                     @   s  d Z ddlZddlZddlZddlZddl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dlZddlmZ dd	lmZ dd
lmZ eeZdede	e fddZdede
e fddZdedefddZdedeeef fddZdededeeef fddZdefddZdedee defddZde	e deeef fd d!Zd"e	e de	e fd#d$Zd%e	e de	e fd&d'Z d(e	e d)ede	e fd*d+Z!d(e	e defd,d-Z"d.edefd/d0Z#dS )1z- Utility functions for certbot-apache plugin     N)Dict)Iterable)List)Optional)Tuple)errors)util)osmod_namereturnc                 C   s   dddgi}| | g S )a  Get known module dependencies.

    .. note:: This does not need to be accurate in order for the client to
        run.  This simply keeps things clean if the user decides to revert
        changes.
    .. warning:: If all deps are not included, it may cause incorrect parsing
        behavior, due to enable_mod's shortcut for updating the parser's
        currently defined modules (`.ApacheParser.add_mod`)
        This would only present a major problem in extremely atypical
        configs that use ifmod for the missing deps.

    sslsetenvifmime)get)r
   deps r   Q/opt/certbot/lib/python3.10/site-packages/certbot_apache/_internal/apache_util.pyget_mod_deps   s   r   
vhost_pathc                 C   s   | r|  ds	dS t| d S )zGet file path from augeas_vhost_path.

    Takes in Augeas path and returns the file name

    :param str vhost_path: Augeas virtual host path

    :returns: filename of vhost
    :rtype: str

    z/files/Nr   )
startswith_split_aug_pathr   r   r   r   get_file_path)   s   r   c                 C   s   t | d S )zGet the Augeas path for a vhost with the file path removed.

    :param str vhost_path: Augeas virtual host path

    :returns: Augeas path to vhost relative to the containing file
    :rtype: str

       )r   r   r   r   r   get_internal_aug_path:   s   	r   c                 C   sT   | dd }g }t j|s!|d\}}}|| t j|r|dt|fS )a:  Splits an Augeas path into a file path and an internal path.

    After removing "/files", this function splits vhost_path into the
    file path and the remaining Augeas path.

    :param str vhost_path: Augeas virtual host path

    :returns: file path and internal Augeas path
    :rtype: `tuple` of `str`

       N/)r	   pathexists
rpartitionappendjoinreversed)r   	file_pathinternal_path_internal_path_partr   r   r   r   F   s   
r   filepathvarnamec                 C   s   i }t ||  }t|D ]>\}}|dkr0t||d kr0||d  d}|d ||d < qt|dkrL|drL|dd d}|d ||d < q|S )z Parses Defines from a variable in configuration file

    :param str filepath: Path of file to parse
    :param str varname: Name of the variable

    :returns: Dict of Define:Value pairs
    :rtype: `dict`

    z-D   r   =r   N)r   get_var_from_filesplit	enumeratelen	partitionr   )r'   r(   return_varsa_optsiv	var_partsr   r   r   parse_define_file^   s   
r5   c                   C   s   t tddS )z< Returns an unique id to be used as a VirtualHost identifier   zutf-8)binasciihexlifyr	   urandomdecoder   r   r   r   	unique_idw   s   r;   pathsc                    s   t  fdd|D S )a*  
    Returns true if the filepath is included in the list of paths
    that may contain full paths or wildcard paths that need to be
    expanded.

    :param str filepath: Filepath to check
    :param list paths: List of paths to check against

    :returns: True if included
    :rtype: bool
    c                 3   s    | ]	}t   |V  qd S )N)fnmatch).0r   r'   r   r   	<genexpr>   s    z$included_in_paths.<locals>.<genexpr>)any)r'   r<   r   r?   r   included_in_paths|   s   rB   
define_cmdc                 C   sr   i }t | d}z|d W n ty   i  Y S w |D ]}|dd}t|dkr.|d nd}|||d < q|S )z
    Gets Defines from httpd process and returns a dictionary of
    the defined variables.

    :param list define_cmd: httpd command to dump defines

    :returns: dictionary of defined variables
    :rtype: dict
    zDefine: ([^ \n]*)DUMP_RUN_CFGr*   r   r)    r   )parse_from_subprocessremove
ValueErrorr,   r.   )rC   	variablesmatchesmatchpartsvaluer   r   r   parse_defines   s   
rN   inc_cmdc                 C   
   t | dS )z
    Gets Include directives from httpd process and returns a list of
    their values.

    :param list inc_cmd: httpd command to dump includes

    :returns: list of found Include directive values
    :rtype: list of str
    z\(.*\) (.*)rF   )rO   r   r   r   parse_includes      
rR   mod_cmdc                 C   rP   )z
    Get loaded modules from httpd process, and return the list
    of loaded module names.

    :param list mod_cmd: httpd command to dump loaded modules

    :returns: list of found LoadModule module names
    :rtype: list of str
    z(.*)_modulerQ   )rT   r   r   r   parse_modules   rS   rU   commandregexpc                 C   s   t | }t||S )zGet values from stdout of subprocess command

    :param list command: Command to run
    :param str regexp: Regexp for parsing

    :returns: list parsed from command output
    :rtype: list

    )_get_runtime_cfgrecompilefindall)rV   rW   stdoutr   r   r   rF      s   
rF   c              	   C   s   zt j| t jt jddt d}|j|j}}W n ttfy1   t	
d| tj td| w |jdkrBt	d| td|S )	zq
    Get runtime configuration info.

    :param command: Command to run

    :returns: stdout from command

    TF)r\   stderruniversal_newlinescheckenvz2Error running command %s for runtime parameters!%sz-Error accessing loaded Apache parameters: {0}r   z$Error in checking parameter list: %sz^Apache is unable to check whether or not the module is loaded because Apache is misconfigured.)
subprocessrunPIPEr   env_no_snap_for_external_callsr\   r]   OSErrorrH   loggererrorr	   linesepr   MisconfigurationErrorformat
returncodewarning)rV   procr\   r]   r   r   r   rX      s6   	
rX   prefixc              
   C   s   t dtjddd| S )z
    Find a TLS Apache config file in the dedicated storage.
    :param str prefix: prefix of the TLS Apache config file to find
    :return: the path the TLS Apache config file
    :rtype: str
    certbot_apache	_internaltls_configsz{0}-options-ssl-apache.conf)pkg_resourcesresource_filenamer	   r   r!   rj   )rn   r   r   r   find_ssl_apache_conf   s   rt   )$__doc__r7   r=   loggingrY   ra   typingr   r   r   r   r   rr   certbotr   r   certbot.compatr	   	getLogger__name__rf   strr   r   r   r   r5   r;   boolrB   rN   rR   rU   rF   rX   rt   r   r   r   r   <module>   s:    
$