
    7gp                        d Z ddlZddlZddlZdZdZdZ	 ddlZ eed          rej	        dk    rdZej
        fZndZej        j        ej        j        j        fZn4# eef$ r* 	 ddlZddlZddlZd	Zej        fZn# e$ r d
ZdZY nw xY wY nw xY wddlmZ ddlmZ ddlmZ ddZ G d d          Z G d de          Zedk     reZ G d de          Z  G d de          Z!dS )z
This module provides GSS-API / SSPI  authentication as defined in :rfc:`4462`.

.. note:: Credential delegation is not supported in server mode.

.. seealso:: :doc:`/api/kex_gss`

.. versionadded:: 1.15
    NT 	__title__zpython-gssapiMITPYTHON-GSSAPI-NEWSSPIF)MSG_USERAUTH_REQUEST)SSHException)__version_info__c                     t           dk    rt          | |          S t           dk    rt          | |          S t           dk    r t          j        dk    rt          | |          S t          d          )a  
    Provide SSH2 GSS-API / SSPI authentication.

    :param str auth_method: The name of the SSH authentication mechanism
                            (gssapi-with-mic or gss-keyex)
    :param bool gss_deleg_creds: Delegate client credentials or not.
                                 We delegate credentials by default.
    :return: Either an `._SSH_GSSAPI_OLD` or `._SSH_GSSAPI_NEW` (Unix)
             object or an `_SSH_SSPI` (Windows) object
    :rtype: object

    :raises: ``ImportError`` -- If no GSS-API / SSPI module could be imported.

    :see: `RFC 4462 <http://www.ietf.org/rfc/rfc4462.txt>`_
    :note: Check for the available API and return either an `._SSH_GSSAPI_OLD`
           (MIT GSSAPI using python-gssapi package) object, an
           `._SSH_GSSAPI_NEW` (MIT GSSAPI using gssapi package) object
           or an `._SSH_SSPI` (MS SSPI) object.
           If there is no supported API available,
           ``None`` will be returned.
    r   r   r   ntz)Unable to import a GSS-API / SSPI module!)_API_SSH_GSSAPI_OLD_SSH_GSSAPI_NEWosname	_SSH_SSPIImportError)auth_methodgss_deleg_credss     J/var/www/html/syslog/venv/lib/python3.11/site-packages/paramiko/ssh_gss.pyGSSAuthr   N   sk    , u}}{O<<<	$	$	${O<<<	BGtOOo666EFFF    c                   >    e Zd ZdZd Zd Zd ZddZd Zd Z	d	 Z
d
S )_SSH_GSSAuthzs
    Contains the shared variables and methods of `._SSH_GSSAPI_OLD`,
    `._SSH_GSSAPI_NEW` and `._SSH_SSPI`.
    c                     || _         || _        d| _        d| _        d| _        d| _        	 d| _        d| _        d| _        d| _	        d| _
        d| _        dS )
        :param str auth_method: The name of the SSH authentication mechanism
                                (gssapi-with-mic or gss-keyex)
        :param bool gss_deleg_creds: Delegate client credentials or not
        Nzssh-connectionz1.2.840.113554.1.2.2F)_auth_method_gss_deleg_creds	_gss_host	_username_session_id_service
_krb5_mech	_gss_ctxt_gss_ctxt_status_gss_srv_ctxt_gss_srv_ctxt_statuscc_fileselfr   r   s      r   __init__z_SSH_GSSAuth.__init__t   sm     ( /(	 1  % "$)!r   c                 B    |                     d          r	|| _        dS dS )z
        This is just a setter to use a non default service.
        I added this method, because RFC 4462 doesn't specify "ssh-connection"
        as the only service value.

        :param str service: The desired SSH service
        zssh-N)findr"   )r*   services     r   set_servicez_SSH_GSSAuth.set_service   s-     << 	$#DMMM	$ 	$r   c                     || _         dS )z
        Setter for C{username}. If GSS-API Key Exchange is performed, the
        username is not set by C{ssh_init_sec_context}.

        :param str username: The name of the user who attempts to login
        N)r    )r*   usernames     r   set_usernamez_SSH_GSSAuth.set_username   s     "r   clientc                     ddl m} ddlm} |                     d          }|                     || j                            }|                     t          |                    }|dk    r||z   S ||z   |z   S )a  
        This method returns a single OID, because we only support the
        Kerberos V5 mechanism.

        :param str mode: Client for client mode and server for server mode
        :return: A byte sequence containing the number of supported
                 OIDs, the length of the OID and the actual OID encoded with
                 DER
        :note: In server mode we just return the OID length and the DER encoded
               OID.
        r   )ObjectIdentifier)encoder   server)pyasn1.type.univr5   pyasn1.codec.derr6   _make_uint32encoder#   len)r*   moder5   r6   OIDskrb5_OIDOID_lens          r   ssh_gss_oidsz_SSH_GSSAuth.ssh_gss_oids   s     	655555,,,,,,  ##>>"2"24?"C"CDD##CMM228X%%g~((r   c                     ddl m} |                    |          \  }}|                                | j        k    rdS dS )z
        Check if the given OID is the Kerberos V5 OID (server mode).

        :param str desired_mech: The desired GSS-API mechanism of the client
        :return: ``True`` if the given OID is supported, otherwise C{False}
        r   decoderFT)r:   rE   decode__str__r#   )r*   desired_mechrE   mech__s        r   ssh_check_mechz_SSH_GSSAuth.ssh_check_mech   sJ     	-,,,,,>>,//b<<>>T_,,5tr   c                 ,    t          j        d|          S )z
        Create a 32 bit unsigned integer (The byte sequence of an integer).

        :param int integer: The integer value to convert
        :return: The byte sequence of an 32 bit integer
        z!I)structpack)r*   integers     r   r;   z_SSH_GSSAuth._make_uint32   s     {4)))r   c                    |                      t          |                    }||z  }|t          j        dt                    z  }||                      t          |                    z  }||                                z  }||                      t          |                    z  }||                                z  }||                      t          |                    z  }||                                z  }|S )a  
        Create the SSH2 MIC filed for gssapi-with-mic.

        :param str session_id: The SSH session ID
        :param str username: The name of the user who attempts to login
        :param str service: The requested SSH service
        :param str auth_method: The requested SSH authentication mechanism
        :return: The MIC as defined in RFC 4462. The contents of the
                 MIC field are:
                 string    session_identifier,
                 byte      SSH_MSG_USERAUTH_REQUEST,
                 string    user-name,
                 string    service (ssh-connection),
                 string    authentication-method
                           (gssapi-with-mic or gssapi-keyex)
        B)r;   r=   rM   rN   r   r<   )r*   
session_idr1   r.   r   mics         r   _ssh_build_micz_SSH_GSSAuth._ssh_build_mic   s    " J00zv{3 4555t  X///x   t  W...w~~t  [!1!1222{!!###
r   N)r3   )__name__
__module____qualname____doc__r+   r/   r2   rB   rK   r;   rT   r   r   r   r   r   n   s         
  6	$ 	$ 	$" " ") ) ) ),   * * *    r   r   c                   V    e Zd ZdZd Z	 ddZddZddZddZe	d	             Z
d
 ZdS )r   z
    Implementation of the GSS-API MIT Kerberos Authentication for SSH2,
    using the older (unmaintained) python-gssapi package.

    :see: `.GSSAuth`
    c                    t                               | ||           | j        r5t          j        t          j        t          j        t          j        f| _        dS t          j        t          j        t          j        f| _        dS r   N)	r   r+   r   gssapiC_PROT_READY_FLAGC_INTEG_FLAGC_MUTUAL_FLAGC_DELEG_FLAG
_gss_flagsr)   s      r   r+   z_SSH_GSSAPI_OLD.__init__   sm     	dKAAA  	(#$#	DOOO (#$DOOOr   Nc                 d   ddl m} || _        || _        t	          j        d| j        z   t          j                  }t	          j                    }| j        |_	        |%t          j
                            | j                  }nh|                    |          \  }	}
|	                                | j        k    rt          d          t          j
                            | j                  }d}	 |<t	          j        |||j	                  | _        | j                            |          }n| j                            |          }nY# t          j        $ rG d                    t+          j                    d         | j                  }t	          j        |          w xY w| j        j        | _        |S )	a  
        Initialize a GSS-API context.

        :param str username: The name of the user who attempts to login
        :param str target: The hostname of the target to connect to
        :param str desired_mech: The negotiated GSS-API mechanism
                                 ("pseudo negotiated" mechanism, because we
                                 support just the krb5 mechanism :-))
        :param str recv_token: The GSS-API token received from the Server
        :raises:
            `.SSHException` -- Is raised if the desired mechanism of the client
            is not supported
        :return: A ``String`` if the GSS-API has returned a token or
            ``None`` if no token was returned
        r   rD   host@NUnsupported mechanism OID.)	peer_name	mech_type	req_flagsz{} Target: {}r7   )r:   rE   r    r   r\   NameC_NT_HOSTBASED_SERVICEContextra   flagsOIDmech_from_stringr#   rF   rG   r	   InitContextr$   stepGSSExceptionformatsysexc_infoestablishedr%   )r*   targetrH   r1   
recv_tokenrE   	targ_namectx	krb5_mechrI   rJ   tokenmessages                r   ssh_init_sec_contextz$_SSH_GSSAPI_OLD.ssh_init_sec_context  s   $ 	-,,,,,!Kdn$f&C
 
	 nO	
33DODDII~~l33HD"||~~00"#?@@@"J77HH		/!!'!3''!i" " "
 ++E22++J77" 	/ 	/ 	/%,,S\^^A->OOG%g...	/ !% :s   -AE AFFc                     || _         |sG|                     | j         | j        | j        | j                  }| j                            |          }n| j                            | j                   }|S )a  
        Create the MIC token for a SSH2 message.

        :param str session_id: The SSH session ID
        :param bool gss_kex: Generate the MIC for GSS-API Key Exchange or not
        :return: gssapi-with-mic:
                 Returns the MIC token from GSS-API for the message we created
                 with ``_ssh_build_mic``.
                 gssapi-keyex:
                 Returns the MIC token from GSS-API with the SSH session ID as
                 message.
        )r!   rT   r    r"   r   r$   get_micr&   r*   rR   gss_kex	mic_field	mic_tokens        r   ssh_get_micz_SSH_GSSAPI_OLD.ssh_get_micA  sz     & 
	E++ !	 I ..y99II *2243CDDIr   c                     || _         || _        | j        t          j                    | _        | j                            |          }| j        j        | _        |S )  
        Accept a GSS-API context (server mode).

        :param str hostname: The servers hostname
        :param str username: The name of the user who attempts to login
        :param str recv_token: The GSS-API Token received from the server,
                               if it's not the initial call.
        :return: A ``String`` if the GSS-API has returned a token or ``None``
                if no token was returned
        )r   r    r&   r\   AcceptContextro   rt   r'   r*   hostnamerv   r1   rz   s        r   ssh_accept_sec_contextz&_SSH_GSSAPI_OLD.ssh_accept_sec_context\  sU     "!%!'!5!7!7D"''
33$($6$B!r   c                    || _         || _        | j        I|                     | j         | j        | j        | j                  }| j                            ||           dS | j                            | j         |           dS )at  
        Verify the MIC token for a SSH2 message.

        :param str mic_token: The MIC token received from the client
        :param str session_id: The SSH session ID
        :param str username: The name of the user who attempts to login
        :return: None if the MIC check was successful
        :raises: ``gssapi.GSSException`` -- if the MIC check failed
        N)r!   r    rT   r"   r   r&   
verify_micr$   r*   r   rR   r1   r   s        r   ssh_check_micz_SSH_GSSAPI_OLD.ssh_check_micp  s     &!>%++ !	 I )))Y????? N%%d&6	BBBBBr   c                 "    | j         j        dS dS )
        Checks if credentials are delegated (server mode).

        :return: ``True`` if credentials are delegated, otherwise ``False``
        NTF)r&   delegated_credr*   s    r   credentials_delegatedz%_SSH_GSSAPI_OLD.credentials_delegated  s     ,84ur   c                     t           )a~  
        Save the Client token in a file. This is used by the SSH server
        to store the client credentials if credentials are delegated
        (server mode).

        :param str client_token: The GSS-API token received form the client
        :raises:
            ``NotImplementedError`` -- Credential delegation is currently not
            supported in server mode
        NotImplementedErrorr*   client_tokens     r   save_client_credsz!_SSH_GSSAPI_OLD.save_client_creds  
     "!r   NNNFNrU   rV   rW   rX   r+   r|   r   r   r   propertyr   r   r   r   r   r   r      s           . DH2 2 2 2h   6   (C C C C4   X" " " " "r   r   )      c                   V    e Zd ZdZd Z	 ddZddZddZddZe	d	             Z
d
 ZdS )r   z
    Implementation of the GSS-API MIT Kerberos Authentication for SSH2,
    using the newer, currently maintained gssapi package.

    :see: `.GSSAuth`
    c                 L   t                               | ||           | j        rIt          j        j        t          j        j        t          j        j        t          j        j        f| _	        dS t          j        j        t          j        j        t          j        j        f| _	        dS r[   )
r   r+   r   r\   RequirementFlagprotection_ready	integritymutual_authenticationdelegate_to_peerra   r)   s      r   r+   z_SSH_GSSAPI_NEW.__init__  s     	dKAAA  	&7&0&<&7	DOOO &7&0&<DOOOr   Nc                    ddl m} || _        || _        t	          j        d| j        z   t          j        j                  }|D|                    |          \  }}|	                                | j
        k    rt          d          t          j        j        }	d}
|=t	          j        || j        |	d          | _        | j                            |
          }
n| j                            |          }
| j        j        | _        |
S )	ae  
        Initialize a GSS-API context.

        :param str username: The name of the user who attempts to login
        :param str target: The hostname of the target to connect to
        :param str desired_mech: The negotiated GSS-API mechanism
                                 ("pseudo negotiated" mechanism, because we
                                 support just the krb5 mechanism :-))
        :param str recv_token: The GSS-API token received from the Server
        :raises: `.SSHException` -- Is raised if the desired mechanism of the
                 client is not supported
        :raises: ``gssapi.exceptions.GSSError`` if there is an error signaled
                                                by the GSS-API implementation
        :return: A ``String`` if the GSS-API has returned a token or ``None``
                 if no token was returned
        r   rD   rc   )	name_typeNrd   initiate)r   rk   rI   usage)r:   rE   r    r   r\   rh   NameTypehostbased_servicerF   rG   r#   r	   MechTypekerberosSecurityContextra   r$   ro   completer%   )r*   ru   rH   r1   rv   rE   rw   rI   rJ   ry   rz   s              r   r|   z$_SSH_GSSAPI_NEW.ssh_init_sec_context  s	   & 	-,,,,,!Kdn$o7
 
 
	 #~~l33HD"||~~00"#?@@@O,	#3o 	  DN N''..EEN''
33E $ 7r   Fc                     || _         |sG|                     | j         | j        | j        | j                  }| j                            |          }n| j                            | j                   }|S )a  
        Create the MIC token for a SSH2 message.

        :param str session_id: The SSH session ID
        :param bool gss_kex: Generate the MIC for GSS-API Key Exchange or not
        :return: gssapi-with-mic:
                 Returns the MIC token from GSS-API for the message we created
                 with ``_ssh_build_mic``.
                 gssapi-keyex:
                 Returns the MIC token from GSS-API with the SSH session ID as
                 message.
        :rtype: str
        )r!   rT   r    r"   r   r$   get_signaturer&   r   s        r   r   z_SSH_GSSAPI_NEW.ssh_get_mic  sz     & 
	K++ !	 I 44Y??II *889IJJIr   c                     || _         || _        | j        t          j        d          | _        | j                            |          }| j        j        | _        |S )r   Naccept)r   )r   r    r&   r\   r   ro   r   r'   r   s        r   r   z&_SSH_GSSAPI_NEW.ssh_accept_sec_context  sZ     "!%!'!7h!G!G!GD"''
33$($6$?!r   c                    || _         || _        | j        I|                     | j         | j        | j        | j                  }| j                            ||           dS | j                            | j         |           dS )a{  
        Verify the MIC token for a SSH2 message.

        :param str mic_token: The MIC token received from the client
        :param str session_id: The SSH session ID
        :param str username: The name of the user who attempts to login
        :return: None if the MIC check was successful
        :raises: ``gssapi.exceptions.GSSError`` -- if the MIC check failed
        N)r!   r    rT   r"   r   r&   verify_signaturer$   r   s        r   r   z_SSH_GSSAPI_NEW.ssh_check_mic$  s     &!>%++ !	 I //	9EEEEE N++D,<iHHHHHr   c                 "    | j         j        dS dS )z
        Checks if credentials are delegated (server mode).

        :return: ``True`` if credentials are delegated, otherwise ``False``
        :rtype: bool
        NTF)r&   delegated_credsr   s    r   r   z%_SSH_GSSAPI_NEW.credentials_delegated>  s     -94ur   c                     t           )aw  
        Save the Client token in a file. This is used by the SSH server
        to store the client credentials if credentials are delegated
        (server mode).

        :param str client_token: The GSS-API token received form the client
        :raises: ``NotImplementedError`` -- Credential delegation is currently
                 not supported in server mode
        r   r   s     r   r   z!_SSH_GSSAPI_NEW.save_client_credsJ  s
     "!r   r   r   r   r   r   r   r   r   r     s           . DH, , , ,\   8   (I I I I4 	 	 X	
" 
" 
" 
" 
"r   r   c                   T    e Zd ZdZd Z	 ddZddZd ZddZe	d	             Z
d
 ZdS )r   zf
    Implementation of the Microsoft SSPI Kerberos Authentication for SSH2.

    :see: `.GSSAuth`
    c                     t                               | ||           | j        r-t          j        t          j        z  t          j        z  | _        dS t          j        t          j        z  | _        dS r[   )r   r+   r   sspiconISC_REQ_INTEGRITYISC_REQ_MUTUAL_AUTHISC_REQ_DELEGATEra   r)   s      r   r+   z_SSH_SSPI.__init__^  si     	dKAAA  		)-.*+ OOO )G,GG OOOr   Nc                    ddl m} || _        || _        d}d| j        z   }|D|                    |          \  }}	|                                | j        k    rt          d          	 |!t          j	        d| j
        |          | _        | j                            |          \  }}
|
d         j        }
n@# t          j        $ r.}|xj        d                    | j                  z  c_         d}~ww xY w|dk    r	 d	| _        d}
	 |
S )
a  
        Initialize a SSPI context.

        :param str username: The name of the user who attempts to login
        :param str target: The FQDN of the target to connect to
        :param str desired_mech: The negotiated SSPI mechanism
                                 ("pseudo negotiated" mechanism, because we
                                 support just the krb5 mechanism :-))
        :param recv_token: The SSPI token received from the Server
        :raises:
            `.SSHException` -- Is raised if the desired mechanism of the client
            is not supported
        :return: A ``String`` if the SSPI has returned a token or ``None`` if
                 no token was returned
        r   rD   host/Nrd   Kerberos)scflags	targetspnz, Target: {}T)r:   rE   r    r   rF   rG   r#   r	   sspi
ClientAuthra   r$   	authorizeBuffer
pywintypeserrorstrerrorrq   r%   )r*   ru   rH   r1   rv   rE   r   rw   rI   rJ   rz   es               r   r|   z_SSH_SSPI.ssh_init_sec_contextq  s8   $ 	-,,,,,!dn,	#~~l33HD"||~~00"#?@@@		!!%9" " "  >33J??LE5!HOEE 	 	 	JJ.//???JJ	 A:: %)D!E s   (AB6 6C3)C..C3Fc                     || _         |sG|                     | j         | j        | j        | j                  }| j                            |          }n| j                            | j                   }|S )a  
        Create the MIC token for a SSH2 message.

        :param str session_id: The SSH session ID
        :param bool gss_kex: Generate the MIC for Key Exchange with SSPI or not
        :return: gssapi-with-mic:
                 Returns the MIC token from SSPI for the message we created
                 with ``_ssh_build_mic``.
                 gssapi-keyex:
                 Returns the MIC token from SSPI with the SSH session ID as
                 message.
        )r!   rT   r    r"   r   r$   signr&   r   s        r   r   z_SSH_SSPI.ssh_get_mic  sz     & 
	B++ !	 I ++I66II *//0@AAIr   c                     || _         || _        d| j         z   }t          j        d|          | _        | j                            |          \  }}|d         j        }|dk    r	d| _        d}|S )a  
        Accept a SSPI context (server mode).

        :param str hostname: The servers FQDN
        :param str username: The name of the user who attempts to login
        :param str recv_token: The SSPI Token received from the server,
                               if it's not the initial call.
        :return: A ``String`` if the SSPI has returned a token or ``None`` if
                 no token was returned
        r   r   )spnr   TN)r   r    r   
ServerAuthr&   r   r   r'   )r*   r   r1   rv   rw   r   rz   s          r   r   z _SSH_SSPI.ssh_accept_sec_context  su     "!dn,	!_ZYGGG)33J??uaA::(,D%Er   c                     || _         || _        |I|                     | j         | j        | j        | j                  }| j                            ||           dS | j                            | j         |           dS )ak  
        Verify the MIC token for a SSH2 message.

        :param str mic_token: The MIC token received from the client
        :param str session_id: The SSH session ID
        :param str username: The name of the user who attempts to login
        :return: None if the MIC check was successful
        :raises: ``sspi.error`` -- if the MIC check failed
        N)r!   r    rT   r"   r   r&   verifyr$   r   s        r   r   z_SSH_SSPI.ssh_check_mic  s     &!++ !	 I %%i;;;;; N!!$"2I>>>>>r   c                 F    | j         t          j        z  o| j        p| j         S )r   )ra   r   r   r'   r   s    r   r   z_SSH_SSPI.credentials_delegated  s(     !99 
%8	
r   c                     t           )a{  
        Save the Client token in a file. This is used by the SSH server
        to store the client credentails if credentials are delegated
        (server mode).

        :param str client_token: The SSPI token received form the client
        :raises:
            ``NotImplementedError`` -- Credential delegation is currently not
            supported in server mode
        r   r   s     r   r   z_SSH_SSPI.save_client_creds  r   r   r   r   r   r   r   r   r   r   r   W  s           ( DH2 2 2 2h   6  ,? ? ? ?< 
 
 X
" " " " "r   r   )T)"rX   rM   r   rr   GSS_AUTH_AVAILABLEGSS_EXCEPTIONSr   r\   hasattrr   rp   
exceptionsGeneralErrorrawmiscGSSErrorr   OSErrorr   r   r   r   paramiko.commonr   paramiko.ssh_exceptionr	   paramiko._versionr
   r   r   r   _SSH_GSSAPIr   r   r   r   r   <module>r      s^  ,   				 



    MMMwv{## 	
(8O(K(K -/"*JO$
 	W 
 
 
	$*,   "
 1 0 0 0 0 0 / / / / / / . . . . . .G G G G@~ ~ ~ ~ ~ ~ ~ ~Bq" q" q" q" q"l q" q" q"h f!Kl" l" l" l" l"l l" l" l"^s" s" s" s" s" s" s" s" s" s"s6   AA B$A;:B;	BBBBB