U
    ˜­“]¦!  ã                   @   s¼   d 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mZ dd	lmZ dd
lmZ e e¡Zej ej¡G dd„ dejƒƒZG dd„ dejƒZdS )zG Distribution specific override class for CentOS family (RHEL, Fedora) é    N)Úerrors)Ú
interfaces)Úutil)ÚMisconfigurationError)ÚList)Úapache_util©Úconfigurator)Úparserc                       s”   e Zd ZdZedddddddgddgdd	gdd
gdddddde dd¡dZ‡ fdd„Z‡ fdd„Z	‡ fdd„Z
dd„ Z‡ fdd„Zdd„ Z‡  ZS )ÚCentOSConfiguratorz1CentOS specific ApacheConfigurator override classz
/etc/httpdz/etc/httpd/conf.dz*.confz/var/log/httpdZ	apachectlz-vZgracefulÚrestartZ
configtestNz-le-ssl.confFÚcertbot_apachezcentos-options-ssl-apache.conf)Úserver_rootÚ
vhost_rootZvhost_filesZ	logs_rootÚctlZversion_cmdZrestart_cmdÚrestart_cmd_altZconftest_cmdZenmodZdismodZle_vhost_extZhandle_modulesZhandle_sitesZchallenge_locationZMOD_SSL_CONF_SRCc                    sV   t  ¡ }|d  ¡ dk}ztt| ƒ ¡  W n& tjk
rP   |rJ|  ¡  n‚ Y nX dS )a(  
        Override config_test to mitigate configtest error in vanilla installation
        of mod_ssl in Fedora. The error is caused by non-existent self-signed
        certificates referenced by the configuration, that would be autogenerated
        during the first (re)start of httpd.
        r   ÚfedoraN)	r   Zget_os_infoÚlowerÚsuperr   Úconfig_testr   r   Ú_try_restart_fedora)ÚselfZos_infor   ©Ú	__class__© ú@/usr/lib/python3/dist-packages/certbot_apache/override_centos.pyr   .   s    
zCentOSConfigurator.config_testc              
      sZ   zt  dddg¡ W n2 tjk
rF } zt t|ƒ¡‚W 5 d}~X Y nX tt| ƒ ¡  dS )z]
        Tries to restart httpd using systemctl to generate the self signed keypair.
        Z	systemctlr   ZhttpdN)	r   Z
run_scriptr   ZSubprocessErrorr   Ústrr   r   r   )r   Úerrr   r   r   r   A   s
     z&CentOSConfigurator._try_restart_fedorac                    s&   t t| ƒ ¡  |  d¡| jd d< dS )z„
        Override the options dictionary initialization in order to support
        alternative restart cmd used in CentOS.
        r   r   r   N)r   r   Ú_prepare_optionsÚoptionZoptions©r   r   r   r   r   N   s    z#CentOSConfigurator._prepare_optionsc                 C   s   t |  d¡|  d¡| j| dS )zInitializes the ApacheParserr   r   r   )ÚCentOSParserr   Úversionr    r   r   r   Ú
get_parserV   s      þzCentOSConfigurator.get_parserc                    s(   t t| ƒj||Ž | jdk r$|  ¡  dS )zÐ
        Override _deploy_cert in order to ensure that the Apache configuration
        has "LoadModule ssl_module..." before parsing the VirtualHost configuration
        that was created by Certbot
        )é   é   r   N)r   r   Ú_deploy_certr"   Ú _deploy_loadmodule_ssl_if_needed©r   ÚargsÚkwargsr   r   r   r&   \   s    
zCentOSConfigurator._deploy_certc                 C   s–  | j jdddd}g }g }g }|D ]š}| d¡d }| j  |¡}|rZ||kr^d}t|ƒ‚n|}| j  |¡r²| j jd |kr˜d	| ¡ ks’d
| ¡ kr˜ dS | d¡d }	| |	¡ q"| |¡ q"|sÆdS | j j	t  
| j jd ¡ddd}
| j  |
dd… d|¡ | |
dd… ¡ |  jd7  _|D ]n}| d¡d }	| j j |¡ | j j|	ddddd… }||kr"| j  |d|¡ | |¡ |  jd7  _q"dS )z
        Add "LoadModule ssl_module <pre-existing path>" to main httpd.conf if
        it doesn't exist there already.
        Z
LoadModuleZ
ssl_moduleF)Zexcludeú/r   z¾Certbot encountered multiple LoadModule directives for LoadModule ssl_module with differing library paths. Please remove or comment out the one(s) that are not in use, and run Certbot again.Údefaultz	ifmodule/zifmodule[1]Nz
/directiveú
!mod_ssl.cT)Z	beginningéÿÿÿÿz3Added LoadModule ssl_module to main configuration.
zPWrapped pre-existing LoadModule ssl_module inside of <IfModule !mod_ssl> block.
)r
   Zfind_dirÚ
rpartitionÚget_all_argsr   Únot_modssl_ifmoduleZlocr   ÚappendZcreate_ifmodZget_aug_pathZadd_dirZ
save_notesÚsplitZaugÚremoveZ	get_ifmod)r   ZloadmodsZcorrect_ifmodsZloadmod_argsZloadmod_pathsÚmZ
noarg_pathZ	path_argsÚmsgZ
nodir_pathZrootconf_ifmodZloadmod_pathZ	ssl_ifmodr   r   r   r'   f   sV    

ÿ þ
ÿÿ

z3CentOSConfigurator._deploy_loadmodule_ssl_if_needed)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚdictÚpkg_resourcesZresource_filenameZOS_DEFAULTSr   r   r   r#   r&   r'   Ú__classcell__r   r   r   r   r      s8    ÿð
r   c                       s<   e Zd ZdZ‡ fdd„Z‡ fdd„Zdd„ Zdd	„ Z‡  ZS )
r!   z+CentOS specific ApacheParser override classc                    s   d| _ tt| ƒj||Ž d S )Nz/etc/sysconfig/httpd)Úsysconfig_filepr   r!   Ú__init__r(   r   r   r   r?   ¬   s    zCentOSParser.__init__c                    s   t t| ƒ ¡  |  ¡  dS )z: Override for update_runtime_variables for custom parsing N)r   r!   Úupdate_runtime_variablesÚparse_sysconfig_varr    r   r   r   r@   ±   s    z%CentOSParser.update_runtime_variablesc                 C   s*   t  | jd¡}|D ]}|| | j|< qdS )z: Parses Apache CLI options from CentOS configuration file ZOPTIONSN)r   Zparse_define_filer>   Z	variables)r   ZdefinesÚkr   r   r   rA   ·   s    z CentOSParser.parse_sysconfig_varc                 C   s”   d|  ¡ krdS |  ¡ }|r| d¡}|d s0q|d |d  }|d  d¡rd||d  d¡d 7 }|dt|ƒ… }d|  |¡kr†d	S |d }qdS )
z8Checks if the provided Augeas path has argument !mod_sslZifmoduleFr   é   r$   ú[r+   r-   T)r   r/   Ú
startswithÚ	partitionÚlenr0   )r   ÚpathZworkpathÚpartsZ
ifmod_pathZifmod_real_pathr   r   r   r1   ½   s    

z CentOSParser.not_modssl_ifmodule)	r7   r8   r9   r:   r?   r@   rA   r1   r=   r   r   r   r   r!   ª   s
   r!   )r:   Zloggingr<   Zzope.interfaceÚzopeZcertbotr   r   r   Zcertbot.errorsr   Zacme.magic_typingr   r   r   r	   r
   Z	getLoggerr7   ZloggerZ	interfaceZproviderZIPluginFactoryZApacheConfiguratorr   ZApacheParserr!   r   r   r   r   Ú<module>   s    
 