HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/conch/__pycache__/checkers.cpython-38.pyc
U

��W[M�@s"dZddlmZmZddlZddlZddlZzddlZWnek
rPdZYn
Xddl	Z	zddl
Z
Wnek
r~dZ
YnXddlmZm
Z
mZddlmZddlmZddlmZddlmZdd	lmZmZdd
lmZmZddlmZddlm Z m!Z!m"Z"dd
l#m$Z$m%Z%m&Z&ddl'm(Z(ddl)m*Z*ddl+m,Z,dd�Z-dd�Z.dd�Z/e
e�Gdd�d��Z0e
e�Gdd�d��Z1e
e�Gdd�d��Z2e(edddd�de3d�Gd d!�d!e�Z4ej5j6fd"d#�Z7d$d%�Z8e
e4�Gd&d'�d'e9��Z:e
e4�Gd(d)�d)e9��Z;e
e�Gd*d+�d+e9��Z<dS),zO
Provide L{ICredentialsChecker} implementations to be used in Conch protocols.
�)�absolute_import�divisionN)�
providedBy�implementer�	Interface)�Version)�error)�keys)�ICredentialsChecker)�IUsernamePassword�ISSHPrivateKey)�UnauthorizedLogin�UnhandledCredentials)�defer)�_keys�_PY3�_b64decodebytes)�failure�reflect�log)�deprecatedModuleAttribute)�runAsEffectiveUser)�FilePathcCst�||�|kS)z�
    Check that the password, when crypted, matches the stored crypted password.

    @param crypted: The stored crypted password.
    @type crypted: L{str}
    @param pw: The password the user has given.
    @type pw: L{str}

    @rtype: L{bool}
    )�crypt)�cryptedZpw�r�8/usr/lib/python3/dist-packages/twisted/conch/checkers.py�verifyCryptedPassword.srcCstdkrdSt�|�S)a
    Look up a user in the /etc/passwd database using the pwd module.  If the
    pwd module is not available, return None.

    @param username: the username of the user to return the passwd database
        information for.
    @type username: L{str}
    N)�pwd�getpwnam)�usernamerrr�
_pwdGetByName=s	r!cCs"tdk	rtj}ndStdd||�S)z�
    Look up a user in the /etc/shadow database using the spwd module. If it is
    not available, return L{None}.

    @param username: the username of the user to return the shadow database
        information for.
    @type username: L{str}
    Nr)�spwdZgetspnamr)r �frrr�_shadowGetByNameLs	r$c@s(eZdZdZefZddd�Zdd�ZdS)�UNIXPasswordDatabaseab
    A checker which validates users out of the UNIX password databases, or
    databases of a compatible format.

    @ivar _getByNameFunctions: a C{list} of functions which are called in order
        to valid a user.  The default value is such that the C{/etc/passwd}
        database will be tried first, followed by the C{/etc/shadow} database.
    NcCs|dkrttg}||_dS�N)r!r$�_getByNameFunctions)�selfZgetByNameFunctionsrrr�__init__iszUNIXPasswordDatabase.__init__c	Cs�tr&|j�t���}|j�t���}n|j}|j}|jD]l}z||�}Wn&tk
rnt�	t
d��YSX|dk	r8|d}|dkr�q8t||�r8t�|j�Sq8t�	t
d��S)Nzinvalid username��zunable to verify password)
rr �decode�sys�getfilesystemencoding�passwordr'�KeyErrorr�failr
rZsucceed)r(�credentialsr r/�funcZpwnamrrrr�requestAvatarIdos"

z$UNIXPasswordDatabase.requestAvatarId)N)�__name__�
__module__�__qualname__�__doc__r�credentialInterfacesr)r4rrrrr%]s
r%c@sBeZdZdZefZeZdd�Zdd�Z	dd�Z
dd	�Zd
d�ZdS)
�SSHPublicKeyDatabasez�
    Checker that authenticates SSH public keys, based on public keys listed in
    authorized_keys and authorized_keys2 files in user .ssh/ directories.
    cCs,t�|j|�}|�|j|�|�|j�|Sr&)r�
maybeDeferred�checkKey�addCallback�_cbRequestAvatarIdZ
addErrback�_ebRequestAvatarId�r(r2�drrrr4�sz$SSHPublicKeyDatabase.requestAvatarIdcCs�|st�td��S|js&t�t���Sz*tj�|j	�}|�
|j|j�rN|jWSWn"t
��t�td��YSXt�td��S)aC
        Check whether the credentials themselves are valid, now that we know
        if the key matches the user.

        @param validKey: A boolean indicating whether or not the public key
            matches a key in the user's authorized_keys file.

        @param credentials: The credentials offered by the user.
        @type credentials: L{ISSHPrivateKey} provider

        @raise UnauthorizedLogin: (as a failure) if the key does not match the
            user in C{credentials}. Also raised if the user provides an invalid
            signature.

        @raise ValidPublicKey: (as a failure) if the key matches the user but
            the credentials do not include a signature. See
            L{error.ValidPublicKey} for more information.

        @return: The user's username, if authentication was successful.
        zinvalid keyzerror while verifying keyzunable to verify key)r�Failurer
�	signaturer�ValidPublicKeyr	�Key�
fromString�blob�verify�sigDatar r�err)r(ZvalidKeyr2�pubKeyrrrr>�sz'SSHPublicKeyDatabase._cbRequestAvatarIdcs8|j�|j�}t|j��d��ddg}�fdd�|D�S)a�
        Return a list of L{FilePath} instances for I{authorized_keys} files
        which might contain information about authorized keys for the given
        credentials.

        On OpenSSH servers, the default location of the file containing the
        list of authorized public keys is
        U{$HOME/.ssh/authorized_keys<http://www.openbsd.org/cgi-bin/man.cgi?query=sshd_config>}.

        I{$HOME/.ssh/authorized_keys2} is also returned, though it has been
        U{deprecated by OpenSSH since
        2001<http://marc.info/?m=100508718416162>}.

        @return: A list of L{FilePath} instances to files with the authorized keys.
        �.ssh�authorized_keys�authorized_keys2csg|]}��|��qSr��child��.0r#��rootrr�
<listcomp>�sz?SSHPublicKeyDatabase.getAuthorizedKeysFiles.<locals>.<listcomp>)�_userdbrr r�pw_dirrP)r(r2�pwent�filesrrSr�getAuthorizedKeysFiles�sz+SSHPublicKeyDatabase.getAuthorizedKeysFilesc	Cs�|j�|j�dd�\}}|�|�D]�}|��s2q$z|��}Wn@tk
r~}z"|jtjkrlt	|||j�}n�W5d}~XYnX|�l|D]`}|�
�}t|�dkr�q�z*t|d�|j
kr�WW5QR�dSWq�tjk
r�Yq�Yq�Xq�W5QRXq$dS)zg
        Retrieve files containing authorized keys and check against user
        credentials.
        ��Nr*TF)rVrr rZ�exists�open�IOError�errnoZEACCESr�split�lenrrG�binascii�Error)	r(r2Zouid�ogid�filepath�lines�e�l�l2rrrr<�s*zSSHPublicKeyDatabase.checkKeycCs&|�t�s"t�|�t�td��S|S)Nzunable to get avatar id)Zcheckr
r�msgrrB)r(r#rrrr?�s

z'SSHPublicKeyDatabase._ebRequestAvatarIdN)
r5r6r7r8rr9rrVr4r>rZr<r?rrrrr:�s$r:c@sHeZdZdZdd�Zdd�Zee�Zdd�Zdd	�Z	d
d�Z
dd
�ZdS)�SSHProtocolCheckera�
    SSHProtocolChecker is a checker that requires multiple authentications
    to succeed.  To add a checker, call my registerChecker method with
    the checker and the interface.

    After each successful authenticate, I call my areDone method with the
    avatar id.  To get a list of the successful credentials for an avatar id,
    use C{SSHProcotolChecker.successfulCredentials[avatarId]}.  If L{areDone}
    returns True, the authentication has succeeded.
    cCsi|_i|_dSr&)�checkers�successfulCredentials�r(rrrr)szSSHProtocolChecker.__init__cCs
t|j�Sr&)rrmrorrr�get_credentialInterfaces
sz+SSHProtocolChecker.get_credentialInterfacescGs"|s
|j}|D]}||j|<qdSr&)r9rm)r(Zcheckerr9ZcredentialInterfacerrr�registerCheckersz"SSHProtocolChecker.registerCheckerc	Csft|�}|D]8}|j�|�}|dk	rt�|j|�}|�|j|�Sqt�t	dd�
ttj
|����S)a�
        Part of the L{ICredentialsChecker} interface.  Called by a portal with
        some credentials to check if they'll authenticate a user.  We check the
        interfaces that the credentials provide against our list of acceptable
        checkers.  If one of them matches, we ask that checker to verify the
        credentials.  If they're valid, we call our L{_cbGoodAuthentication}
        method to continue.

        @param credentials: the credentials the L{Portal} wants us to verify
        NzNo checker for %sz, )rrm�getrr;r4r=�_cbGoodAuthenticationr1r�join�maprZqual)r(r2Zifac�i�crArrrr4s�
�z"SSHProtocolChecker.requestAvatarIdcCsF||jkrg|j|<|j|�|�|�|�r:|j|=|St���dS)a
        Called if a checker has verified the credentials.  We call our
        L{areDone} method to see if the whole of the successful authentications
        are enough.  If they are, we return the avatar ID returned by the first
        checker.
        N)rn�append�areDonerZNotEnoughAuthentication)r(�avatarIdr2rrrrs/s


z(SSHProtocolChecker._cbGoodAuthenticationcCsdS)a
        Override to determine if the authentication is finished for a given
        avatarId.

        @param avatarId: the avatar returned by the first checker.  For
            this checker to function correctly, all the checkers must
            return the same avatar ID.
        Tr)r(rzrrrry@s	zSSHProtocolChecker.areDoneN)r5r6r7r8r)rp�propertyr9rqr4rsryrrrrrl�srlZTwisted�z�Please use twisted.conch.checkers.SSHPublicKeyChecker, initialized with an instance of twisted.conch.checkers.UNIXAuthorizedKeysFiles instead.c@seZdZdZdd�ZdS)�IAuthorizedKeysDBzb
    An object that provides valid authorized ssh keys mapped to usernames.

    @since: 15.0
    cCsdS)aL
        Gets an iterable of authorized keys that are valid for the given
        C{avatarId}.

        @param avatarId: the ID of the avatar
        @type avatarId: valid return value of
            L{twisted.cred.checkers.ICredentialsChecker.requestAvatarId}

        @return: an iterable of L{twisted.conch.ssh.keys.Key}
        Nr)rzrrr�getAuthorizedKeys\sz#IAuthorizedKeysDB.getAuthorizedKeysN)r5r6r7r8r~rrrrr}Vsr}ccsj|D]`}|��}|r|�d�sz||�VWqtjk
rb}zt�d�||��W5d}~XYqXqdS)a~
    Reads keys from an authorized keys file.  Any non-comment line that cannot
    be parsed as a key will be ignored, although that particular line will
    be logged.

    @param fileobj: something from which to read lines which can be parsed
        as keys
    @type fileobj: L{file}-like object

    @param parseKey: a callable that takes a string and returns a
        L{twisted.conch.ssh.keys.Key}, mainly to be used for testing.  The
        default is L{twisted.conch.ssh.keys.Key.fromString}.
    @type parseKey: L{callable}

    @return: an iterable of L{twisted.conch.ssh.keys.Key}
    @rtype: iterable

    @since: 15.0
    �#z*Unable to parse line "{0}" as a key: {1!s}N)�strip�
startswithr	ZBadKeyErrorrrk�format)Zfileobj�parseKey�linerhrrr�readAuthorizedKeyFilejs�r�ccs�|D]v}|��rz.|���}t||�D]
}|Vq&W5QRXWqttfk
rx}zt�d�|j|��W5d}~XYqXqdS)a�
    Helper function that turns an iterable of filepaths into a generator of
    keys.  If any file cannot be read, a message is logged but it is
    otherwise ignored.

    @param filepaths: iterable of L{twisted.python.filepath.FilePath}.
    @type filepaths: iterable

    @param parseKey: a callable that takes a string and returns a
        L{twisted.conch.ssh.keys.Key}
    @type parseKey: L{callable}

    @return: generator of L{twisted.conch.ssh.keys.Key}
    @rtype: generator

    @since: 15.0
    zUnable to read {0}: {1!s}N)	r]r^r�r_�OSErrorrrkr��path)Z	filepathsr��fpr#�keyrhrrr�_keysFromFilepaths�s
r�c@s eZdZdZdd�Zdd�ZdS)�InMemorySSHKeyDBz�
    Object that provides SSH public keys based on a dictionary of usernames
    mapped to L{twisted.conch.ssh.keys.Key}s.

    @since: 15.0
    cCs
||_dS)z�
        Initializes a new L{InMemorySSHKeyDB}.

        @param mapping: mapping of usernames to iterables of
            L{twisted.conch.ssh.keys.Key}s
        @type mapping: L{dict}

        N)�_mapping)r(�mappingrrrr)�s	zInMemorySSHKeyDB.__init__cCs|j�|g�Sr&)r�rr)r(r rrrr~�sz"InMemorySSHKeyDB.getAuthorizedKeysN)r5r6r7r8r)r~rrrrr��sr�c@s*eZdZdZdejjfdd�Zdd�ZdS)�UNIXAuthorizedKeysFilesa
    Object that provides SSH public keys based on public keys listed in
    authorized_keys and authorized_keys2 files in UNIX user .ssh/ directories.
    If any of the files cannot be read, a message is logged but that file is
    otherwise ignored.

    @since: 15.0
    NcCs||_||_|dkrt|_dS)a�
        Initializes a new L{UNIXAuthorizedKeysFiles}.

        @param userdb: access to the Unix user account and password database
            (default is the Python module L{pwd})
        @type userdb: L{pwd}-like object

        @param parseKey: a callable that takes a string and returns a
            L{twisted.conch.ssh.keys.Key}, mainly to be used for testing.  The
            default is L{twisted.conch.ssh.keys.Key.fromString}.
        @type parseKey: L{callable}
        N)rV�	_parseKeyr)r(Zuserdbr�rrrr)�s
z UNIXAuthorizedKeysFiles.__init__csZz|j�|�}Wntk
r&YdSXt|j��d��ddg}t�fdd�|D�|j�S)NrrLrMrNc3s|]}��|�VqdSr&rOrQrSrr�	<genexpr>�sz<UNIXAuthorizedKeysFiles.getAuthorizedKeys.<locals>.<genexpr>)rVrr0rrWrPr�r�)r(r ZpasswdrYrrSrr~�s�z)UNIXAuthorizedKeysFiles.getAuthorizedKeys)	r5r6r7r8r	rErFr)r~rrrrr��sr�c@s>eZdZdZefZdd�Zdd�Zdd�Zdd	�Z	d
d�Z
dS)
�SSHPublicKeyCheckera?
    Checker that authenticates SSH public keys, based on public keys listed in
    authorized_keys and authorized_keys2 files in user .ssh/ directories.

    Initializing this checker with a L{UNIXAuthorizedKeysFiles} should be
    used instead of L{twisted.conch.checkers.SSHPublicKeyDatabase}.

    @since: 15.0
    cCs
||_dS)z�
        Initializes a L{SSHPublicKeyChecker}.

        @param keydb: a provider of L{IAuthorizedKeysDB}
        @type keydb: L{IAuthorizedKeysDB} provider
        N)�_keydb)r(Zkeydbrrrr)�szSSHPublicKeyChecker.__init__cCs.t�|j|�}|�|j|�|�|j|�|Sr&)rr;�_sanityCheckKeyr=�	_checkKey�
_verifyKeyr@rrrr4sz#SSHPublicKeyChecker.requestAvatarIdcCs|jst���tj�|j�S)aW
        Checks whether the provided credentials are a valid SSH key with a
        signature (does not actually verify the signature).

        @param credentials: the credentials offered by the user
        @type credentials: L{ISSHPrivateKey} provider

        @raise ValidPublicKey: the credentials do not include a signature. See
            L{error.ValidPublicKey} for more information.

        @raise BadKeyError: The key included with the credentials is not
            recognized as a key.

        @return: the key in the credentials
        @rtype: L{twisted.conch.ssh.keys.Key}
        )rCrrDr	rErFrG)r(r2rrrr�sz#SSHPublicKeyChecker._sanityCheckKeycs0t�fdd�|j�|j�D��r$�Std��dS)ab
        Checks the public key against all authorized keys (if any) for the
        user.

        @param pubKey: the key in the credentials (just to prevent it from
            having to be calculated again)
        @type pubKey:

        @param credentials: the credentials offered by the user
        @type credentials: L{ISSHPrivateKey} provider

        @raise UnauthorizedLogin: If the key is not authorized, or if there
            was any error obtaining a list of authorized keys for the user.

        @return: C{pubKey} if the key is authorized
        @rtype: L{twisted.conch.ssh.keys.Key}
        c3s|]}|�kVqdSr&r)rRr��rKrrr�0sz0SSHPublicKeyChecker._checkKey.<locals>.<genexpr>zKey not authorizedN)�anyr�r~r r
�r(rKr2rr�rr�s
�zSSHPublicKeyChecker._checkKeycCsFz|�|j|j�r|jWSWnt��td��YnXtd��dS)a~
        Checks whether the credentials themselves are valid, now that we know
        if the key matches the user.

        @param pubKey: the key in the credentials (just to prevent it from
            having to be calculated again)
        @type pubKey: L{twisted.conch.ssh.keys.Key}

        @param credentials: the credentials offered by the user
        @type credentials: L{ISSHPrivateKey} provider

        @raise UnauthorizedLogin: If the key signature is invalid or there
            was any error verifying the signature.

        @return: The user's username, if authentication was successful
        @rtype: L{bytes}
        zError while verifying keyzKey signature invalid.N)rHrCrIr rrJr
r�rrrr�7szSSHPublicKeyChecker._verifyKeyN)r5r6r7r8rr9r)r4r�r�r�rrrrr��s	
r�)=r8Z
__future__rrr-rcr`r�ImportErrorrr"Zzope.interfacerrrZincrementalrZ
twisted.conchrZtwisted.conch.sshr	Ztwisted.cred.checkersr
Ztwisted.cred.credentialsrrZtwisted.cred.errorr
rZtwisted.internetrZtwisted.python.compatrrrZtwisted.pythonrrrZtwisted.python.deprecaterZtwisted.python.utilrZtwisted.python.filepathrrr!r$r%r:rlr5r}rErFr�r��objectr�r�r�rrrr�<module>sd

-oQ�	)