????

Your IP : 216.73.216.67


Current Path : /proc/1724857/root/lib/python3.9/site-packages/ipaplatform/redhat/__pycache__/
Upload File :
Current File : //proc/1724857/root/lib/python3.9/site-packages/ipaplatform/redhat/__pycache__/tasks.cpython-39.pyc

a

�jiwl�@sFdZddlmZmZddlZddlZddlZddlmZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlmZddlmZddl
mZddlmZdd	lmZdd
lmZddlZddlmZddlmZdd
lm Z ddl!m"Z"m#Z#e�$e%�Z&dej'dgfgZ(e�)d�Z*eGdd�de#��Z+Gdd�de"�Z,e,�Z-dS)zZ
This module contains default Red Hat OS family-specific implementations of
system tasks.
�)�print_function�absolute_importN)�Path)�find_library)�total_ordering)�CalledProcessError)�PyAsn1Error)�directivesetter)�ipautil)�	constants)�paths)�
get_auth_tool)�BaseTaskNamespace�IPAAbstractVersionZsofthsm2z
p11-kit-proxyz�
    # auto-generated by IPA installer
    [main]
    dns={dnsprocessing}

    [global-dns]
    searches={searches}

    [global-dns-domain-*]
    servers={servers}
c@seZdZdZdd�ZdS)�
IPAVersionNcCsH|j}|dur>t�td��}|j}tjtjg|_tj|_||_|||�S)z.Lazy load and call librpm's rpmvercmp
        NZrpm)	�_rpmvercmp_func�ctypesZCDLLrZ	rpmvercmpZc_char_p�argtypesZc_intZrestype)�self�a�bZrpmvercmp_funcZlibrpm�r�</usr/lib/python3.9/site-packages/ipaplatform/redhat/tasks.py�_vercmpSszIPAVersion._vercmp)�__name__�
__module__�__qualname__rrrrrrrOsrcsjeZdZdQdd�Zdd�Zejfdd�Zdd	�Zd
d�Z	dd
�Z
dd�ZdRdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�ZdSd*d+�Zed,d-��Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Z d:d;�Z!d<d=�Z"d>d?�Z#dd)d@��fdAdB�
Z$dT�fdCdD�	Z%dEdF�Z&dGdH�Z'dIdJ�Z(dKdL�Z)�fdMdN�Z*�fdOdP�Z+�Z,S)U�RedHatTaskNamespaceFcCsNtj}|��rtj�|�sdS|g}|r2|�d�|�|�tj|dd�dS)aRestore SELinux security context on the given filepath.

        SELinux equivalent is /path/to/restorecon <filepath>
        restorecon's return values are not reliable so we have to
        ignore them (BZ #739604).

        ipautil.run() will do the logging.
        Nz-FF)Z
raiseonerr)	rZSBIN_RESTORECON�is_selinux_enabled�os�path�exists�appendr
�run)r�filepath�force�
restorecon�argsrrr�restore_contextcs	

z#RedHatTaskNamespace.restore_contextcCsDzt�tjg�Wn(tjy(YdSty:YdS0dSdS)z2Check if SELinux is available and enabled
        FTN)r
r#rZSELINUXENABLEDr�OSError�rrrrrysz&RedHatTaskNamespace.is_selinux_enabledcCs(|��sdStj�|�s$td|��dS)a�
        We don't have a specific package requirement for policycoreutils
        which provides restorecon. This is because we don't require
        SELinux on client installs. However if SELinux is enabled then
        this package is required.

        This function returns nothing but may raise a Runtime exception
        if SELinux is enabled but restorecon is not available.
        FzoSELinux is enabled but %s does not exist.
Install the policycoreutils package and start the installation again.T)rrr r!�RuntimeError)rr&rrr�check_selinux_status�s
�z(RedHatTaskNamespace.check_selinux_statuscCs\tj�tj�std��z&tjddd�}|��dur:t	d��Wnt	yVtd��Yn0dS)aChecks whether IPv6 kernel module is loaded.

        Function checks if /proc/net/if_inet6 is present. If IPv6 stack is
        enabled, it exists and contains the interfaces configuration.

        :raises: RuntimeError when IPv6 stack is disabled
        z�IPv6 stack has to be enabled in the kernel and some interface has to have ::1 address assigned. Typically this is 'lo' interface. If you do not wish to use IPv6 globally, disable it on the specific interfaces in sysctl.conf except 'lo' interface.z::1T)Zallow_loopbackNz"no interface for ::1 address foundz�IPv6 stack is enabled in the kernel but there is no interface that has ::1 address assigned. Add ::1 address resolution to 'lo' interface. You might need to enable IPv6 on the interface 'lo' in sysctl.conf.)
rr r!rZIF_INET6r+r
ZCheckedIPAddressZget_matching_interface�
ValueError)rZ
localhost6rrr�check_ipv6_stack_enabled�s��z,RedHatTaskNamespace.check_ipv6_stack_enabledc
Csnztjtjdgtjd�}Wn@tjyZ}z&|jdkrDWYd}~dS�WYd}~nd}~00|�d���SdS)zsCheck if running inside a container

        :returns: container runtime or None
        :rtype: str, None
        z--container)�stderr�Nzutf-8)	�
subprocessZcheck_outputrZSYSTEMD_DETECT_VIRTZSTDOUTr�
returncode�decode�strip)r�output�errr�detect_container�s�

z$RedHatTaskNamespace.detect_containercCst�}|�||||�dS�N)r
Zunconfigure)r�fstore�
statestoreZwas_sssd_installedZwas_sssd_configured�auth_configrrr�$restore_pre_ipa_client_configuration�s�z8RedHatTaskNamespace.restore_pre_ipa_client_configurationcCs�z>ttjd��}dd�|D�}Wd�n1s20YWntyTg}Yn0|�d�|��ttjd��}|�|�Wd�n1s�0YdS)N�rcSs"g|]}|�����d�s|�qS)Z	NISDOMAIN)r4�upper�
startswith)�.0�linerrr�
<listcomp>�s�z5RedHatTaskNamespace.set_nisdomain.<locals>.<listcomp>z
NISDOMAIN={}
�w)�openrZSYSCONF_NETWORK�IOErrorr"�format�
writelines)rZ	nisdomain�f�contentrrr�
set_nisdomain�s�(
z!RedHatTaskNamespace.set_nisdomainTcCst�}|�|||||�dSr8)r
Z	configure)r�sssd�	mkhomedirr:ZsudoZsubidr;rrr�modify_nsswitch_pam_stack�sz-RedHatTaskNamespace.modify_nsswitch_pam_stackcCsdS)NFrr*rrr�is_nosssd_supported�sz'RedHatTaskNamespace.is_nosssd_supportedcCst�}|�|�dSr8)r
Zbackup�rr r;rrr�backup_auth_configuration�sz-RedHatTaskNamespace.backup_auth_configurationcCst�}|�|�dSr8)r
ZrestorerOrrr�restore_auth_configuration�sz.RedHatTaskNamespace.restore_auth_configurationcCs�|�dd�}tjdddg}|r(|�d�|�d�t�|�dD]}|�d|�q@|�d	d
d�|�d	dd�|�d	dt|��d
S)zg
        Migrate the pam stack configuration from authconfig to an authselect
        profile.
        Z
authconfigrLZselectrKz	with-sudozwith-mkhomedirz--force)ZldapZkrb5rKZsssdauthrLZ
authselectZprofileZ
features_list�N)	Z	get_stater�
AUTHSELECTr"r
r#�
restore_state�backup_state�bool)rr:rL�authselect_cmd�confrrr�migrate_auth_configuration�s


�z.RedHatTaskNamespace.migrate_auth_configurationc
CsXzt�tjg�Wn2tyD}zt�d|�WYd}~dSd}~00t�d�dSdS)Nz1Could not update systemwide CA trust database: %sFzSystemwide CA database updated.T)r
r#rZUPDATE_CA_TRUSTr�logger�error�info�rr6rrr�reload_systemwide_ca_stores�
z.RedHatTaskNamespace.reload_systemwide_ca_storecCst|�tj|�|�tj�g�Sr8)�any�write_p11kit_certsr�IPA_P11_KIT�remove_ca_certificates_bundle�SYSTEMWIDE_IPA_CA_CRT)r�ca_certsrrr�platform_insert_ca_certss��z,RedHatTaskNamespace.platform_insert_ca_certscCs0ddlm}ddlm}t|�}zt|d�}Wn tyNt�d|��Yn0|���|�	d�zt
�|��d�Wn ty�t�d|��Yn0t
�}|D�]f\}}	}
}}z|j}
|j}|j}|j}Wn&tt|fy�t�d	|	��Yn0tj�|	�}tj�|
�}
tj�|�}tj�|�}tj�|�}d
t||
|||d�}|
du�r\|d
7}n|
du�rn|d7}|dj|�|jj��d�d�7}|�	|�|jdur�||vr�z
|j}Wn"t�y�t�d|	��Yn0tj�|�}dt|||d�}|�	|�|�|�q�Wd�n1�s"0YdS)Nr)�x509)�CertificateErrorrCzFailed to open %sz.# This file was created by IPA. Do not edit.

�zFailed to set mode of %sz!Failed to decode certificate "%s"z�[p11-kit-object-v1]
class: certificate
certificate-type: x-509
certificate-category: authority
label: "%(label)s"
subject: "%(subject)s"
issuer: "%(issuer)s"
serial-number: "%(serial_number)s"
x-public-key-info: "%(public_key_info)s"
)�label�subject�issuer�
serial_number�public_key_infoTztrusted: true
Fzx-distrusted: true
z{pem}

�ascii)Zpemz,Failed to encode extended key usage for "%s"z�[p11-kit-object-v1]
class: x-certificate-extension
label: "ExtendedKeyUsage for %(label)s"
x-public-key-info: "%(public_key_info)s"
object-id: 2.5.29.37
value: "%(value)s"

)rirm�value) ZipalibrfZ
ipalib.errorsrgrrDrErZr[�writer�fchmod�fileno�setZ
subject_bytesZissuer_bytesZserial_number_bytesZpublic_key_info_bytesrr-�urllib�parse�quote�dictrFZpublic_bytesZEncodingZPEMr3Zextended_key_usageZextended_key_usage_bytes�add)r�filenamerdrfrgr rHZhas_ekuZcertZnicknameZtrustedZ_ext_key_usageZ_serialrjrkrlrmri�objZ
ext_key_usagerorrrr`s�

�	��


�

�
���	
,z&RedHatTaskNamespace.write_p11kit_certscCst|�tj�|�tj�g�Sr8)r_rbrrarcr*rrr�platform_remove_ca_certsvs

�z,RedHatTaskNamespace.platform_remove_ca_certscCsFt|�}|��sdSz|��Wn ty@t�d|��Yn0dS)NFzCould not remove %sT)r�is_file�unlink�	ExceptionrZr[)rryr rrrrb|sz1RedHatTaskNamespace.remove_ca_certificates_bundlecCs6tj}tj�|�r|�|�t��}|�dd|�dS)N�network�hostname)	r�ETC_HOSTNAMErr r!�backup_file�socketZgethostnamerU)rr9r:r$�old_hostnamerrr�backup_hostname�s

z#RedHatTaskNamespace.backup_hostnamec
Cs�|�dd�}|durhz|�|�WnDtjyf}z*t�dt���t�d||�WYd}~n
d}~00t	j
}|�|�r�|�|�dS)Nrr�z%sz3Failed to restore this machine hostname to %s (%s).)
rT�set_hostnamer
rrZ�debug�	traceback�
format_excr[rr��has_file�restore_file)rr9r:r�r6r$rrr�restore_hostname�s�
z$RedHatTaskNamespace.restore_hostnameNcCsdd�}|��sdSi}i}|��D]�\}}|dur6q$zHtjtj|gdd�}|j��d}	|durl|||	�|	|kr||||<Wq$tjy�}
z"t	�
d||
�|||<WYd}
~
q$d}
~
00q$|r�||�}zt�|�Wntjy�|�|�Yn0|�rtj
j|d�||��d	��dS)
NcSs&tjdg}|�dd�|��D��|S)Nz-PcSsg|]}d|�qS)z%s=%sr)r@�updaterrrrB��zXRedHatTaskNamespace.set_selinux_booleans.<locals>.get_setsebool_args.<locals>.<listcomp>)rZ	SETSEBOOL�extend�items)Zchangesr'rrr�get_setsebool_args�s
zDRedHatTaskNamespace.set_selinux_booleans.<locals>.get_setsebool_argsFT)Zcapture_output�z#Cannot get SELinux boolean '%s': %s� )ZfailedZcommand)rr�r
r#rZ	GETSEBOOLr5�splitrrZr[r��	ipapython�errorsZSetseboolError�join)rZrequired_settingsZbackup_funcr�Zupdated_varsZfailed_varsZsetting�state�resultZoriginal_stater6r'rrr�set_selinux_booleans�sB�
 �z(RedHatTaskNamespace.set_selinux_booleanscCst|�S)z�
        :param version: textual version
        :return: object implementing proper __cmp__ method for version compare
        )r)�versionrrr�parse_ipa_version�sz%RedHatTaskNamespace.parse_ipa_versionc	Csntj�tj�st�tjd�t�tj�tj	d�tj
ttjtj
tjd��t�tj
d�|�tj
�|��dS)zACreate systemd config for httpd service to work with IPA
        i�zipa-httpd.conf.template)�KDCPROXY_CONFIG�IPA_HTTPD_KDCPROXY�KRB5CC_HTTPDrhN)rr r!rZSYSTEMD_SYSTEM_HTTPD_D_DIR�mkdirr
�copy_template_filer��USR_SHARE_IPA_DIR�SYSTEMD_SYSTEM_HTTPD_IPA_CONFrwr�r�r��chmodr(�systemd_daemon_reloadr*rrr� configure_httpd_service_ipa_conf�s��
z4RedHatTaskNamespace.configure_httpd_service_ipa_confcCst�tjddg�dS)z#Tell systemd to reload config filesz--systemz
daemon-reloadN)r
r#rZ	SYSTEMCTLr*rrrr��sz)RedHatTaskNamespace.systemd_daemon_reloadc
CsLt�tj�tjd�tjttj	t
j|tjd��t�
tjd�|�tj�dS)Nzgssproxy.conf.template)�HTTP_KEYTAB�
HTTPD_USERZIPAAPI_USERZSWEEPER_SOCKETi�)r
r�rr r�rr�Z
GSSPROXY_CONFrwr�rr�Z IPA_CCACHE_SWEEPER_GSSPROXY_SOCKr�r()rZipauserrrr�configure_http_gssproxy_conf�s��z0RedHatTaskNamespace.configure_http_gssproxy_confcCs�tj}tjjdkrtj}ntj}|dus0|dur>t�	d�dSt
j�|�}t
j�
|�s`t
�|�t�t
j�tjd�|t|d��t
�|d�|�|�dS)zmConfigure WSGI for correct Python version (Fedora)

        See https://pagure.io/freeipa/issue/7394
        r�Nz+Nothing to do for configure_httpd_wsgi_confzipa-httpd-wsgi.conf.template)ZWSGI_MODULErh)rZHTTPD_IPA_WSGI_MODULES_CONF�sys�version_info�majorrZMOD_WSGI_PYTHON2ZMOD_WSGI_PYTHON3rZr\rr �dirname�isdir�makedirsr
r�r�r�rwr�r()rrXZwsgi_moduleZconfdirrrr�configure_httpd_wsgi_conf�s&

��z-RedHatTaskNamespace.configure_httpd_wsgi_confc
Cspzt�tj�WnRtyb}z:|jtjkr<t�dtj�nt�	dtj|�WYd}~dSd}~00|�
�dS)z.Remove systemd config for httpd service of IPAz+Trying to remove %s but file does not existzError removing %s: %sN)rr}rr�r)�errno�ENOENTrZr�r[r�r]rrr�remove_httpd_service_ipa_confs��z1RedHatTaskNamespace.remove_httpd_service_ipa_confcCst�tjddd�dS)NZSSLProtocolF�r	Z
set_directiverZHTTPD_SSL_CONFr*rrr�configure_httpd_protocol/s
�z,RedHatTaskNamespace.configure_httpd_protocolcCst�tjd|g�dS)Nzset-hostname)r
r#rZBIN_HOSTNAMECTL)rr�rrrr�5sz RedHatTaskNamespace.set_hostnamecCsjzRttjd��2}|����dkr2Wd�WdSWd�n1sF0YWntydYn0dS)aA
        Checks whether this host is FIPS-enabled.

        Returns a boolean indicating if the host is FIPS-enabled, i.e. if the
        file /proc/sys/crypto/fips_enabled contains a non-0 value. Otherwise,
        or if the file /proc/sys/crypto/fips_enabled does not exist,
        the function returns False.
        r=�0NTF)rDrZPROC_FIPS_ENABLED�readr4rE)rrHrrr�is_fips_enabled8s	4z#RedHatTaskNamespace.is_fips_enabledcCs(t�tjddd�t�tjddd�dS)NZErrorLogzlogs/error_logFZTransferLogzlogs/access_logr�r*rrr�setup_httpd_loggingKs��z'RedHatTaskNamespace.setup_httpd_logging)�resolve1_enabledr9cs�|rt|t�sJ�|r t|t�s$J�t�j||||d�ddlm}|durd|�tj�sd|�	tj�|d}|�
�}|r�t�dtj
�|r�d}nd}tj|d	�|�d	�|�d
�}	ttj
d��*}
t�|
��d�|
�|	�Wd�n1s�0Y|��|�s�|�s�t�d
tj�dd�d�|��g}	|D]}|	�d�|���q0ttjd�� }
|
�d�|	��Wd�n1�s|0YdS)z�Configure global DNS resolver (e.g. /etc/resolv.conf)

        :param nameservers: list of IP addresses
        :param searchdomains: list of search domaons
        :param fstore: optional file store for backup
        )�nameservers�
searchdomainsr�r9r��
knownservicesN�NetworkManagerz$Network Manager is enabled, write %szsystemd-resolved�default�,)�
dnsprocessingZserversZsearchesrCrhzLNeither Network Manager nor systemd-resolved are enabled, write %s directly.z!# auto-generated by IPA installerz	search {}r�z
nameserver {}�
)�
isinstance�list�super�configure_dns_resolver�ipaplatform.servicesr�r�rZRESOLV_CONFr��
is_enabledrZr��NETWORK_MANAGER_IPA_CONF�NM_IPA_CONFrFr�rDrrqrrrp�reload_or_restartr")rr�r�r�r9r��nmZ
nm_enabledr�ZcfgrHZ
nameserver��	__class__rrr�SsT���(��z*RedHatTaskNamespace.configure_dns_resolvercsPt�j|d�ddlm}|d}tj�tj�rLt�	tj�|�
�rL|��dS)zxUnconfigure global DNS resolver (e.g. /etc/resolv.conf)

        :param fstore: optional file store for restore
        )r9rr�r�N)r��unconfigure_dns_resolverr�r�rr �isfilerr�r}r�r�)rr9r�r�r�rrr��sz,RedHatTaskNamespace.unconfigure_dns_resolverc
	Cs"g}tD�]\}}}tj�tjd�|��}tj�|�r�t|��}|�	�}Wd�n1s\0Yd|v}	|	s�|�
|�s�t�d|�|�
|�t|d��P}|�d�|�d�|��|�d�d	�|���t�|��d
�Wd�n1s�0Y|�|�t�d|�|�|�q|S)z5Disable global p11-kit configuration for NSS
        �	{}.moduleNZIPAzBacking up existing '%s'.rCz# created by IPA installer
zmodule: {}
zdisable-in: {}
z, rhz#Created PKCS#11 module config '%s'.)�PKCS11_MODULESrr r�r�ETC_PKCS11_MODULES_DIRrFr�rDr�r�rZr�r�rprqrrr(r")
rr9�	filenames�name�moduleZdisabled_inryrHrIZis_ipa_filerrr�configure_pkcs11_modules�s,�
&

.
z,RedHatTaskNamespace.configure_pkcs11_modulesc	Csng}tD]`\}}}tj�tjd�|��}zt�|�WntyHYn0|�	|�|�
|�r|�|�q|S)z5Restore global p11-kit configuration for NSS
        r�)r�rr r�rr�rFr}r)r"r�r�)rr9r�r��_moduleZ_disabled_inryrrr�restore_pkcs11_modules�s�

z*RedHatTaskNamespace.restore_pkcs11_modulescCstdd�tD��S)z<Return the list of module config files setup by IPA
        css*|]"\}}}tj�tjd�|��VqdS)r�N)rr r�rr�rF)r@r�r�Z	_disabledrrr�	<genexpr>�s�
�z9RedHatTaskNamespace.get_pkcs11_modules.<locals>.<genexpr>)�tupler�r*rrr�get_pkcs11_modules�s�z&RedHatTaskNamespace.get_pkcs11_modulescCsdS)z(sudo enablement is handled by authselectNr)rZ_fstorerrr�enable_sssd_sudo�sz$RedHatTaskNamespace.enable_sssd_sudocsXtt|��|�tjddg}zt�|�Wn(tjyRt�	d�t�	d�Yn0dS)zDisable ldap-based automountzdisable-featurezwith-custom-automountz/Unable to disable with-custom-automount featurezRIt may happen if the configuration was done using authconfig instead of authselectN)
r�r�disable_ldap_automountrrSr
r#rrZr\)rr:rWr�rrr��s�
z*RedHatTaskNamespace.disable_ldap_automountcs(t���}|��s|Stdd�|D��S)Ncss|]}|�d�s|VqdS)ZcamelliaN)r?)r@r6rrrr��r�z=RedHatTaskNamespace.get_supported_enctypes.<locals>.<genexpr>)r��get_supported_enctypesr�r�)rZenctypesr�rrr��s
z*RedHatTaskNamespace.get_supported_enctypes)F)TF)N)N)-rrrr(rrZ
RESTORECONr,r.r7r<rJrMrNrPrQrYr^rer`r{rbr�r�r��staticmethodr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r��
__classcell__rrr�rrasR
	�
X
	
,
	�E
r).�__doc__Z
__future__rrrZloggingr�pathlibrr�r�r�rtr1r��textwrapZctypes.utilr�	functoolsrrZpyasn1.errorrr�r	r
Zipapython.errorsZipaplatform.constantsrZipaplatform.pathsrZipaplatform.redhat.authconfigr
Zipaplatform.base.tasksrrZ	getLoggerrrZZLIBSOFTHSM2_SOr��dedentr�rrZtasksrrrr�<module>sH
�