U
    ccp                     @   sL  d Z ddlZddlZddlZdZdZdZzDddlZeedrTej	dkrTdZej
fZndZejjejjjfZW n^ eefk
r   z(ddlZddlZddlZd	ZejfZW n ek
r   d
ZdZY nX Y nX ddlmZ ddlmZ ddlmZ dddZG dd deZG dd deZedk r(eZ G dd deZ!G d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                 C   sL   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dS )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!N)_API_SSH_GSSAPI_OLD_SSH_GSSAPI_NEWosname	_SSH_SSPIImportError)auth_methodgss_deleg_credsr   r   4/tmp/pip-unpacked-wheel-rglolp_m/paramiko/ssh_gss.pyGSSAuthN   s    


r   c                   @   sJ   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd Zdd Z	dd Z
dS )_SSH_GSSAuthzs
    Contains the shared variables and methods of `._SSH_GSSAPI_OLD`,
    `._SSH_GSSAPI_NEW` and `._SSH_SSPI`.
    c                 C   sL   || _ || _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_statusZcc_fileselfr   r   r   r   r   __init__t   s    z_SSH_GSSAuth.__init__c                 C   s   | dr|| _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$   servicer   r   r   set_service   s    
z_SSH_GSSAuth.set_servicec                 C   s
   || _ 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$   usernamer   r   r   set_username   s    z_SSH_GSSAuth.set_usernameclientc                 C   s\   ddl m} ddlm} | d}||| j}| t|}|dkrP|| S || | 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)Zpyasn1.type.univr,   pyasn1.codec.derr-   _make_uint32encoder   len)r$   moder,   r-   ZOIDsZkrb5_OIDZOID_lenr   r   r   ssh_gss_oids   s    
z_SSH_GSSAuth.ssh_gss_oidsc                 C   s0   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)r0   r7   decode__str__r   )r$   desired_mechr7   mech__r   r   r   ssh_check_mech   s
    z_SSH_GSSAuth.ssh_check_mechc                 C   s   t 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$   integerr   r   r   r1      s    z_SSH_GSSAuth._make_uint32c                 C   s   |  t|}||7 }|tdt7 }||  t|7 }|| 7 }||  t|7 }|| 7 }||  t|7 }|| 7 }|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)r1   r3   r>   r?   r   r2   )r$   
session_idr)   r'   r   Zmicr   r   r   _ssh_build_mic   s    z_SSH_GSSAuth._ssh_build_micN)r+   )__name__
__module____qualname____doc__r%   r(   r*   r5   r=   r1   rC   r   r   r   r   r   n   s   	
	r   c                   @   sT   e Zd ZdZdd ZdddZddd	Zdd
dZdddZe	dd Z
d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                 C   sB   t | || | jr,tjtjtjtjf| _ntjtjtjf| _dS r   N)	r   r%   r   gssapiZC_PROT_READY_FLAGZC_INTEG_FLAGZC_MUTUAL_FLAGZC_DELEG_FLAG
_gss_flagsr#   r   r   r   r%      s    z_SSH_GSSAPI_OLD.__init__Nc                 C   s  ddl m} || _|| _td| j tj}t }| j|_	|dkrTtj
| j}n4||\}	}
|	 | jkrztdntj
| j}d}z:|dkrtj|||j	d| _| j|}n| j|}W n6 tjk
r   dt d | j}t|Y nX | 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   r6   host@NUnsupported mechanism OID.)Z	peer_nameZ	mech_typeZ	req_flagsz{} Target: {}r.   )r0   r7   r   r   rI   NameZC_NT_HOSTBASED_SERVICEContextrJ   flagsZOIDZmech_from_stringr   r8   r9   r   ZInitContextr   stepGSSExceptionformatsysexc_infoestablishedr    )r$   targetr:   r)   
recv_tokenr7   	targ_namectx	krb5_mechr;   r<   tokenmessager   r   r   ssh_init_sec_context  s<     

z$_SSH_GSSAPI_OLD.ssh_init_sec_contextFc                 C   sB   || _ |s0| | 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   rC   r   r   r   r   Zget_micr!   r$   rB   Zgss_kex	mic_field	mic_tokenr   r   r   ssh_get_micA  s    z_SSH_GSSAPI_OLD.ssh_get_micc                 C   s:   || _ || _| jdkr t | _| 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
        N)r   r   r!   rI   ZAcceptContextrP   rU   r"   r$   hostnamerW   r)   r[   r   r   r   ssh_accept_sec_context\  s    


z&_SSH_GSSAPI_OLD.ssh_accept_sec_contextc                 C   sR   || _ || _| jdk	r>| | j | j| j| j}| j|| n| 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   rC   r   r   r!   Z
verify_micr   r$   r`   rB   r)   r_   r   r   r   ssh_check_micp  s    

z_SSH_GSSAPI_OLD.ssh_check_micc                 C   s   | j jdk	rdS dS )
        Checks if credentials are delegated (server mode).

        :return: ``True`` if credentials are delegated, otherwise ``False``
        NTF)r!   Zdelegated_credr$   r   r   r   credentials_delegated  s    z%_SSH_GSSAPI_OLD.credentials_delegatedc                 C   s   t dS )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
        NNotImplementedErrorr$   Zclient_tokenr   r   r   save_client_creds  s    z!_SSH_GSSAPI_OLD.save_client_creds)NNN)F)N)NrD   rE   rF   rG   r%   r]   ra   re   rg   propertyrj   rn   r   r   r   r   r      s        
4




r   )      c                   @   sT   e Zd ZdZdd ZdddZddd	Zdd
dZdddZe	dd Z
d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                 C   sP   t | || | jr4tjjtjjtjjtjjf| _	ntjjtjjtjjf| _	dS rH   )
r   r%   r   rI   ZRequirementFlagZprotection_readyZ	integrityZmutual_authenticationZdelegate_to_peerrJ   r#   r   r   r   r%     s    z_SSH_GSSAPI_NEW.__init__Nc                 C   s   ddl m} || _|| _tjd| j tjjd}|dk	r\||\}}|	 | j
kr\tdtjj}	d}
|dkrtj|| j|	d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   r6   rK   )Z	name_typeNrL   Zinitiate)r   rO   r;   usage)r0   r7   r   r   rI   rM   ZNameTypeZhostbased_servicer8   r9   r   r   ZMechTypeZkerberosSecurityContextrJ   r   rP   completer    )r$   rV   r:   r)   rW   r7   rX   r;   r<   rZ   r[   r   r   r   r]     s0    
z$_SSH_GSSAPI_NEW.ssh_init_sec_contextFc                 C   sB   || _ |s0| | 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   rC   r   r   r   r   Zget_signaturer!   r^   r   r   r   ra     s    z_SSH_GSSAPI_NEW.ssh_get_micc                 C   s>   || _ || _| jdkr$tjdd| _| j|}| jj| _|S )rb   Naccept)rs   )r   r   r!   rI   rt   rP   ru   r"   rc   r   r   r   re     s    

z&_SSH_GSSAPI_NEW.ssh_accept_sec_contextc                 C   sR   || _ || _| jdk	r>| | j | j| j| j}| j|| n| 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   rC   r   r   r!   Zverify_signaturer   rf   r   r   r   rg   $  s    

z_SSH_GSSAPI_NEW.ssh_check_micc                 C   s   | j jdk	rdS dS )z
        Checks if credentials are delegated (server mode).

        :return: ``True`` if credentials are delegated, otherwise ``False``
        :rtype: bool
        NTF)r!   Zdelegated_credsri   r   r   r   rj   >  s    z%_SSH_GSSAPI_NEW.credentials_delegatedc                 C   s   t dS )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
        Nrk   rm   r   r   r   rn   J  s    
z!_SSH_GSSAPI_NEW.save_client_creds)NNN)F)N)Nro   r   r   r   r   r     s        
.



r   c                   @   sR   e Zd ZdZdd ZdddZddd	Zd
d ZdddZe	dd Z
dd ZdS )r   zf
    Implementation of the Microsoft SSPI Kerberos Authentication for SSH2.

    :see: `.GSSAuth`
    c                 C   s<   t | || | jr*tjtjB tjB | _ntjtjB | _dS rH   )r   r%   r   sspiconZISC_REQ_INTEGRITYZISC_REQ_MUTUAL_AUTHISC_REQ_DELEGATErJ   r#   r   r   r   r%   ^  s    
z_SSH_SSPI.__init__Nc              
   C   s   ddl m} || _|| _d}d| j }|dk	rR||\}}	| | jkrRtdz:|dkrptj	d| j
|d| _| j|\}}
|
d j}
W n< tjk
r } z| jd| j7  _ W 5 d}~X Y nX |dkrd	| _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   r6   host/NrL   Kerberos)ZscflagsZ	targetspnz, Target: {}T)r0   r7   r   r   r8   r9   r   r   sspiZ
ClientAuthrJ   r   	authorizeBuffer
pywintypeserrorstrerrorrR   r    )r$   rV   r:   r)   rW   r7   r   rX   r;   r<   r[   er   r   r   r]   q  s2    
  z_SSH_SSPI.ssh_init_sec_contextFc                 C   sB   || _ |s0| | 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   rC   r   r   r   r   signr!   r^   r   r   r   ra     s    z_SSH_SSPI.ssh_get_micc                 C   sV   || _ || _d| j  }tjd|d| _| j|\}}|d j}|dkrRd| _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
        ry   rz   )Zspnr   TN)r   r   r{   Z
ServerAuthr!   r|   r}   r"   )r$   rd   r)   rW   rX   r   r[   r   r   r   re     s    

z _SSH_SSPI.ssh_accept_sec_contextc                 C   sP   || _ || _|dk	r<| | j | j| j| j}| j|| n| 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   rC   r   r   r!   verifyr   rf   r   r   r   rg     s    
z_SSH_SSPI.ssh_check_micc                 C   s   | j tj@ o| jp| j S )rh   )rJ   rw   rx   r"   ri   r   r   r   rj     s    
z_SSH_SSPI.credentials_delegatedc                 C   s   t dS )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
        Nrk   rm   r   r   r   rn     s    z_SSH_SSPI.save_client_creds)NNN)F)Nro   r   r   r   r   r   W  s        
4



r   )T)#rG   r>   r   rS   ZGSS_AUTH_AVAILABLEZGSS_EXCEPTIONSr   rI   hasattrr   rQ   
exceptionsZGeneralErrorrawmiscZGSSErrorr   OSErrorr~   rw   r{   r   Zparamiko.commonr   Zparamiko.ssh_exceptionr   Zparamiko._versionr	   r   objectr   r   Z_SSH_GSSAPIr   r   r   r   r   r   <module>   sL   


   5
 0