HEX
Server: LiteSpeed
System: Linux shams.tasjeel.ae 5.14.0-611.5.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Nov 11 08:09:09 EST 2025 x86_64
User: infowars (1469)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: //usr/lib/python3.9/site-packages/ipalib/install/__pycache__/certmonger.cpython-39.opt-1.pyc
a

�N(ip�@s�ddlmZmZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
ddlmZddlmZddlmZddlmZe�e�Zd	Zd
Zd
ZdZdZd
ZdgZGdd�d�Z Gdd�de �Z!dd�Z"dd�Z#dd�Z$dd�Z%dd�Z&dd�Z'dd �Z(d!d"�Z)dBd&d'�Z*dCd(d)�Z+dDd*d+�Z,dEd,d-�Z-dFd.d/�Z.dGd0d1�Z/d2d3�Z0d4d5�Z1d6d7�Z2d8d9�Z3dHd;d<�Z4d=d>�Z5dId@dA�Z6dS)J�)�print_function�absolute_importN)�api)�CA_DBUS_TIMEOUT)�DN)�Sleeper)�paths)�services�/org/fedorahosted/certmonger�org.fedorahosted.certmongerz#org.fedorahosted.certmonger.request�org.fedorahosted.certmonger.ca�org.freedesktop.DBus.Propertiesztemplate-hostnamec@seZdZdZddd�ZdS)�_cm_dbus_objectz>
    Auxiliary class for convenient DBus object handling.
    NFcCs�|dus|dus|dur td��|dur,|}||_||_||_||_||_|�||�|_t�	|j|�|_
|r|t�	|jt�|_dS)aY
        bus - DBus bus object, result of dbus.SystemBus() or dbus.SessionBus()
              Object is accesible over this DBus bus instance.
        object_path - path to requested object on DBus bus
        object_dbus_interface
        parent_dbus_interface
        property_interface - create DBus property interface? True or False
        Nz5bus, object_path and dbus_interface must not be None.)
�RuntimeError�bus�parent�pathZobj_dbus_ifZparent_dbus_if�
get_object�obj�dbus�	Interface�obj_if�DBUS_PROPERTY_IF�prop_if)�selfrrZobject_pathZobject_dbus_interfaceZparent_dbus_interfaceZproperty_interface�r�=/usr/lib/python3.9/site-packages/ipalib/install/certmonger.py�__init__Vs
�z_cm_dbus_object.__init__)NF)�__name__�
__module__�__qualname__�__doc__rrrrrrRs�rcs<eZdZdZdZdd�Zdd�Zdd�Z�fd	d
�Z�Z	S)�_certmongera
    Create a connection to certmonger.
    By default use SystemBus. When not available use private connection
    over Unix socket.
    This solution is really ugly and should be removed as soon as DBus
    SystemBus is available at system install time.
    i,cCsttj�t��d�}t�tjddd|g�|_	t
d|jd�D]&}tj�|�rTd|St
�d�q8|��td��dS)	N�
certmongerz-nz-Lz-Pr�zunix:path=%sz%Failed to start certmonger: Timed out)�osr�join�tempfileZmkdtemp�
subprocess�PopenrZ
CERTMONGER�_proc�range�timeout�exists�time�sleep�_stop_private_connr)rZ
sock_filename�_trrr�_start_private_connzs�z_certmonger._start_private_conncCsj|jrf|j��}|durdS|j��td|jd�D]&}|j��}|durPdSt�d�q4t�d�dS)Nrr$zFailed to stop certmonger.)	r*ZpollZ	terminater+r,r.r/�logger�error)rZretcoder1rrrr0�s


z_certmonger._stop_private_conncCs|��dS)N)r0)rrrr�__del__�sz_certmonger.__del__c
s�d|_d|_zt��|_Wn�tjy�}z~|��}|dvrJt�d|��z|��|_	tj
�|j	�|_Wn4tjy�}zt�d|��WYd}~n
d}~00WYd}~n�d}~00z|j�t
�Wn�tj�yxztjj��Wn4t�y}zt�d|��WYd}~n
d}~00td|jd�D]H}z|j�t
�W�qtWntj�y\Yn0t�d�td���q*Yn0tt|��|jdtt�dS)N)z#org.freedesktop.DBus.Error.NoServerz'org.freedesktop.DBus.Error.FileNotFoundz2Failed to connect to certmonger over SystemBus: %sz7Failed to connect to certmonger over private socket: %szFailed to start certmonger: %srr$zFailed to start certmonger)r*Z_busr�	SystemBus�
DBusExceptionZ
get_dbus_namer3r4r2Z
_private_sockZ
connectionZ
ConnectionZget_name_owner�DBUS_CM_NAMEr	Z
knownservicesr#�start�	Exceptionr+r,r.r/r�superr"r�DBUS_CM_PATH�
DBUS_CM_IF)r�eZerr_namer1��	__class__rrr�sL�
�.
�z_certmonger.__init__)
rrr r!r,r2r0r5r�
__classcell__rrr?rr"ps
r"cCs*t|t�std��t�}g}g}d|vrD|j�|d�}|rN|g}n
|j��}|D]�}t|j||t	t
d�}|D]�}|dkr�|j��}|dur�td|�
d���t|j||tt
�}|||j��kr�qRqn|tvr�t||�}	|j�t	|�}
|	�|
�s�qRqn|j�t	|�}|||krnqRqn|�|�qR|S)z�
    Get all requests that matches the provided criteria.

    :param criteria: dict of criteria; see module doc for details

    z"criteria" must be dict.�nicknameT�ca-nameNz!certmonger CA '%s' is not defined)�
isinstance�dict�	TypeErrorr"rZfind_request_by_nicknameZget_requestsrr�DBUS_CM_REQUEST_IFr=�get_car�get�
DBUS_CM_CA_IF�get_nickname�ARRAY_PROPERTIES�setr�Get�issubset�append)�criteria�cm�requestsZrequests_pathsZrequest_path�requestZ	criterion�ca_path�ca�expectZgot�valuerrr�
_get_requests�sH

�
��
rYcCs@t|�}t|�dkrdSt|�dkr,|dStdt|���dS)z�
    Find request that matches criteria.  Return ``None`` if no match.
    Raise ``RuntimeError`` if there is more than one matching request.

    :param criteria: dict of criteria; see module doc for details

    rN�z1Criteria expected to be met by 1 request, got %s.)rY�lenr)rQrSrrr�_get_request�s�r\c
Cs�ztt|d��}Wn2tyD}zt�d|��WYd}~n
d}~00|r�|dkrx|j��}t|j||t	t
�}|j��S|j�
t|�SndSdS)z"
    Get property of request.
    )rB�Failed to get request: %sNrC)r\rErr3r4rrHrrrJr=rKrrNrG)�
request_id�	directiverTr>rUrVrrr�get_request_values
�
r`c
CsZzt|�}Wn2ty>}zt�d|��WYd}~n
d}~00|rR|j�td�SdSdS)a 
    If you don't know the certmonger request_id then try to find it by looking
    through all the requests.

    Return ``None`` if no match.  Raise ``RuntimeError`` if there is
    more than one matching request.

    :param criteria: dict of criteria; see module doc for details

    r]NrB)r\rr3r4rrNrG)rQrTr>rrr�get_request_idsracCs<g}dd||d�}t|�}|D]}|�|j�td��q|S)zZ
    Return a list containing the request ids for a given NSS database
    directory.
    �NSSDB)zcert-storagezkey-storage�
cert-databasezkey-databaserB)rYrPrrNrG)�dirZreqidrQrSrTrrr�get_requests_for_dir-s�rec
Cs\ztd|i�}Wn2tyB}zt�d|��WYd}~n
d}~00|rX|j�||i�dS)z;
    Add a new directive to a certmonger request file.
    rBr]N)r\rr3r4r�modify)r^r_rXrTr>rrr�add_request_value<srgcCst|d|g�dS)z�
    In order for a certmonger request to be renewable it needs a principal.

    When an existing certificate is added via start-tracking it won't have
    a principal.
    ztemplate-principalN�rg)r^�	principalrrr�
add_principalIsrjcCst|d|�dS)z�
    In order for a certmonger request to be renwable it needs the subject
    set in the request file.

    When an existing certificate is added via start-tracking it won't have
    a subject_template set.
    ztemplate-subjectNrh)r^�subjectrrr�add_subjectSsrl�IPArbFcCsdt||||||||||	|
||�
}tjj}|r8||kr8|}t��|}zt||�}Wn4ty�}zt�d|�d}WYd}~n
d}~00t	|d�}|dkr�|dur�t�d|�|St�d|||�|dvr�t�d	|��q@|s�q@t��|kr�t�d
|��q@|dk�r t�d|�t�
d�qDt�d
|�t�
d�t|�qD|
�rPt|d�td�
||���dS)aRequest certificate, wait and possibly resubmit failing requests

    Submit a cert request to certmonger and wait until the request has
    finished.

    With timeout, a failed request is resubmitted. During parallel replica
    installation, a request sometimes fails with CA_REJECTED or
    CA_UNREACHABLE. The error occurs when the master is either busy or some
    information haven't been replicated yet. Even a stuck request can be
    recovered, e.g. when permission and group information have been
    replicated.
    zwait_for_request raised %sZTIMEOUTNzca-error�
MONITORINGzCert request %s was successfulzCert request %s failed: %s (%s)>�CA_REJECTED�CA_UNREACHABLEzGiving up on cert request %sz$Request %s reached resubmit deadlinez'%s not in final state, continue waiting�
z"Sleep and resubmit cert request %s)r^z$Certificate issuance failed ({}: {}))�request_certr�env�certmonger_wait_timeoutr.�wait_for_requestrr3�debugr`r/�resubmit_request�
stop_tracking�format)�certpathrkrirB�passwd_fname�dnsrV�profile�pre_command�post_command�storage�permsZresubmit_timeoutZstop_tracking_on_error�nss_userZreq_idZcertmonger_timeoutZdeadline�stater>Zca_errorrrr�request_and_wait_for_cert^sN�
�




�r�c

Cs�|
dkr&|\}
}tttt|����}n|}
|}t�}|j�|�}|sRtd�|���t|
|
|
|||d�}|rz||d<||d<|r�|g|d<|r�t	|t
tf�s�t|��||d<|r�||d<|r�||d	<|r�||d
<t
j}|r�tj�|�s�||}||d<|	�rtj�|	��s||	}	|	|d<|�r6|d
|d<|d|d<|jj|tjjd�}z0|d
�rpt|j||dttd�}ntd��Wn4t�y�}zt�d|��WYd}~n
d}~00|j��S)z�
    Execute certmonger to request a server certificate.

    ``dns``
        A sequence of DNS names to appear in SAN request extension.
    ``perms``
        A tuple of (cert, key) permissions in e.g., (0644,0660)
    �FILE�{} CA not found)�KEY_STORAGE�CERT_STORAGE�
CERT_LOCATION�KEY_LOCATIONZSUBJECT�CA�
CERT_NICKNAME�KEY_NICKNAMEZ	PRINCIPAL�DNS�KEY_PIN_FILE�
ca-profile�nss-user�cert-presave-command�cert-postsave-commandrz
cert-permsrZz	key-perms�r,T�add_request() returned Falsez"Failed to create a new request: %sN)�strr�reversedr"r�find_ca_by_nicknamerryrErD�list�tuplerFr�CERTMONGER_COMMAND_TEMPLATEr%r�isabs�add_requestrrsrtrrrGr=r:r3r4rK)rzrkrirBr{r|rVr}r~rr�r�r��certfile�keyfilerRrUZrequest_parameters�certmonger_cmd_template�resultrTr>rrrrr�sj
�
�
�rrc
Cs�|dkr|\}}
n|}|}
t�}tj}|j�|�}|sDtd�|���d||||
|d�}|rj||d<||d<|rv||d<|r�tj�	|�|d<|r�tj�
|�s�||}||d	<|r�tj�
|�s�||}||d
<|r�||d<|	dvr�|	|d
<|	|d<|
du�rt|
�dk�r|
|d<|�r||d<t�
d|�|jj|tjjd�}z0|d�rbt|j||dttd�}ntd��Wn4t�y�}zt�d|��WYd}~n
d}~00|j�td�S)a�
    Tell certmonger to track the given certificate in either a file or an NSS
    database. The certificate access can be protected by a password_file.

    This uses the generic certmonger command getcert so we can specify
    a different helper.

    :param certpath:
        The path to an NSS database or a tuple (PEM certificate, private key).
    :param ca:
        Nickanme of the CA for which the given certificate should be tracked.
    :param nickname:
        Nickname of the NSS certificate in ``certpath`` to be tracked.
    :param pin:
        The passphrase for either NSS database containing ``nickname`` or
        for the encrypted key in the ``certpath`` tuple.
    :param pinfile:
        Similar to ``pin`` parameter except this is a path to a file containing
        the required passphrase.
    :param pre_command:
        Specifies a command for certmonger to run before it renews a
        certificate. This command must reside in /usr/lib/ipa/certmonger
        to work with SELinux.
    :param post_command:
        Specifies a command for certmonger to run after it has renewed a
        certificate. This command must reside in /usr/lib/ipa/certmonger
        to work with SELinux.
    :param storage:
        One of "NSSDB" or "FILE", describes whether certmonger should use
        NSS or OpenSSL backend to track the certificate in ``certpath``
    :param profile:
        Which certificate profile should be used.
    :param token_name:
        Hardware token name for HSM support
    :param dns:
        List of DNS names
    :param nss_user:
        login of the private key owner
    :returns: certificate tracking nickname.
    r�r�T)ZTRACKr�r�r�r�r�r�r�ZKEY_PINr�r�r�r�>N�internalz	key-tokenz
cert-tokenNrr�r�zstart tracking %sr�rZr�zFailed to add new request: %srB)r"rr�rr�rryr%r�abspathr�r[r3rvr�rrsrtrrrGr=r:r4rrN)rzrVrB�pinZpinfiler~rr}r�Z
token_namer|r�r�r�rRr�rU�paramsr�rTr>rrr�start_tracking�sl,
��
�r�c
Cs�|dur |dur |dur td��|dur8|dur8td��t�}|rJ||d<|rV||d<|rb||d<|rn||d<zt|�}Wn2ty�}zt�d|��WYd}~n
d}~00|r�|jj�|j�dS)	zo
    Stop tracking the current request using either the request_id or nickname.

    Returns True or False
    Nz5One of request_id, nickname and certfile is required.z'Can't specify both secdir and certfile.rcrBz
cert-nicknamez	cert-filer])	rrEr\r3r4rrZremove_requestr)Zsecdirr^rBr�rQrTr>rrrrxXs(rxcCsji}|dur"t�}|j�|�|d<|dur2||d<|durB||d<t|�dkrftd|i�}|j�|�dS)Nr��template-profile� template-ms-certificate-templaterrB)r"rr�r[r\rf)r^rVr}�template_v2�updaterRrTrrrrfvsrfc	Cs�td|i�}|r�i}|dur2t�}|j�|�|d<|durB||d<|durR||d<|rfd|d<d|d	<d
tfdtffD]D\}}	z|j�t|�}
Wnt	j
y�YqvYqv0|
rv|	|
�||<qvt|�dkr�|j�|�|j�
�dS)
a�
    :param request_id: the certmonger numeric request ID
    :param ca: the nickname for the certmonger CA, e.g. IPA or SelfSign
    :param profile: the profile to use, e.g. SubCA.  For requests using the
                    Dogtag CA, this is the profile to use.  This also causes
                    the Microsoft certificate tempalte name extension to the
                    CSR (for telling AD CS what template to use).
    :param template_v2: Microsoft V2 template specifier extension value.
                        Format: <oid>:<major-version>[:<minor-version>]
    :param is_ca: boolean that if True adds the CA basic constraint
    rBNr�r�r�Tztemplate-is-ca���ztemplate-ca-path-lengthzkey-sizezkey-typer)r\r"rr��intr�rrNrGrr7r[rfZresubmit)r^rVr}r�Zis_carTr�rR�keyZconvertrXrrrrw�s.
rwcCs&t�}|j�d�}t|j||ttd�S)z�
    Look through all the certmonger CA files to find the one that
    has id=IPA

    We can use find_request_value because the ca files have the
    same file format.
    rmT)r"rr�rrrJr=)rRrUrrr�_find_IPA_ca�sr�cCsNt�}|rJ|j�td�}|rJdt�|�vrJd|��|f}|j�td|�dS)a
    If the hostname we were passed to use in ipa-client-install doesn't
    match the value of gethostname() then we need to append
    -k host/HOSTNAME@REALM to the ca helper defined for
    /usr/libexec/certmonger/ipa-submit.

    We also need to restore this on uninstall.
    �external-helper�-kz%s -k %sN)r�rrNrJ�shlex�split�strip�Set)rirV�
ext_helperrrr�add_principal_to_cas�s	r�cCsLt�}|rH|j�td�}|rHdt�|�vrHt�|�d}|j�td|�dS)zE
    Remove any -k principal options from the ipa_submit helper.
    r�r�rN)r�rrNrJr�r�r�)rVr�rrr�remove_principal_from_cas�sr�c	Cs|t��}|�dd�}t�|d�}|�|�}|s>td�|���n:|�d|�}t�|d�}|�dd�}|jdd|t	d�|SdS)	zi
    Modify certmonger CA helper.

    Applies the new helper and return the previous configuration.
    rr
z{} is not configuredr
rr�r�N)
rr6rrr�rryrNr�r)	Zca_name�helperrrZifacerZca_objZca_ifaceZ
old_helperrrr�modify_ca_helper�s(�
���r�r�cCs�|r|dkrd|}ttjd��L}|D]6}|�dd�\}}||kr&|��Wd�Sq&Wd�n1sr0YdS)z�
    Dogtag stores its NSS pin in a file formatted as token:PIN.

    The caller is expected to handle any exceptions raised.
    r�z	hardware-�r�=rZN)�openrZPKI_TOMCAT_PASSWORD_CONFr�r�)�token�f�line�tokr�rrr�get_pin�s:r�cCs g}|D]}|�t|��q|S)a
    Given a set of directories and nicknames verify that we are no longer
    tracking certificates.

    dirs is a list of directories to test for. We will return a tuple
    of nicknames for any tracked certificates found.

    This can only check for NSS-based certificates.
    )�extendre)�dirsZreqidsrdrrr�check_state	s
r��xcCsRtd|td�d�}d}tt|d��}||kr8t�d|�|dvrBqN|}|�q|S)Ng�?zrequest timed out)r/r,Zraises�statusz!certmonger request is in state %r>ZNEED_CArpZCA_UNCONFIGUREDZ
NEED_GUIDANCErnro)rrr�r`r3rv)r^r,r/Z
last_stater�rrrrus�ru)NNNrmNNNrbNrFN)
NNNrmNNNrbNN)rmNNNNNNrbNNN)NNNN)NNN)NNNF)r�)r�)7Z
__future__rrZloggingr%r.rr�r(r'ZipalibrZipalib.constantsrZipapython.dnrZipapython.ipautilrZipaplatform.pathsrZipaplatformr	Z	getLoggerrr3r<r=r8rGrJrrLrr"rYr\r`rarergrjrlr�rrr�rxrfrwr�r�r�r�r�r�rurrrr�<module>sx
O1

�
G�
K�
j

�
0