U
vfp @ sJ d Z ddlZddlZddlZdZdZdZzDddlZeedrTej dkrTdZej
fZndZejjej
jjfZW n^ eefk
r z(ddlZddlZddlZd ZejfZW n ek
r d
ZdZY nX Y nX ddlmZ ddlmZ dd
lmZ dddZG dd dZG dd deZedk r&eZG dd deZ G dd deZ!dS )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-gssapiMITPYTHON-GSSAPI-NEWSSPIF)MSG_USERAUTH_REQUEST)SSHException)__version_info__c C sL t dkrt| |S t dkr$t| |S t dkr@tjdkr@t| |S tddS )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 `_
: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_NEWosname _SSH_SSPIImportError)auth_methodgss_deleg_credsr r A/opt/alt/python38/lib/python3.8/site-packages/paramiko/ssh_gss.pyGSSAuthN s
r c @ sJ e Zd ZdZdd Zdd Zdd Zdd d
Zdd Zd
d Z dd Z
dS )_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| _dS )
: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
Nzssh-connectionz1.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_statusZcc_fileselfr r r r r __init__t s z_SSH_GSSAuth.__init__c C s | dr|| _dS )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
zssh-N)findr )r$ servicer r r set_service s
z_SSH_GSSAuth.set_servicec C s
|| _ dS )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_usernameclientc C s\ ddl m} ddlm} | d}||| j}| t|}|dkrP|| 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)Zpyasn1.type.univr, pyasn1.codec.derr- _make_uint32encoder len)r$ moder, r- ZOIDsZkrb5_OIDZOID_lenr r r ssh_gss_oids s
z_SSH_GSSAuth.ssh_gss_oidsc C s0 ddl m} ||\}}| | jkr,dS dS )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)structpack)r$ integerr r r r1 s z_SSH_GSSAuth._make_uint32c C s | t|}||7 }|tdt7 }|| 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 Zmicr 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 Zd ZdZdd ZdddZddd Zdd
dZddd
Ze dd Z
dd ZdS )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 | || | jr,tjtjtjtjf| _ntjtjtjf| _dS r N) r r% r gssapiZC_PROT_READY_FLAGZC_INTEG_FLAGZ
C_MUTUAL_FLAGZC_DELEG_FLAG
_gss_flagsr# r r r r% s z_SSH_GSSAPI_OLD.__init__Nc
C s ddl m} || _|| _td| j tj}t }| j|_ |dkrTtj
| j}n4|
|\} }
| | jkrztdntj
| j}d}z:|dkrtj|||j d| _| j|}n| j|}W n6 tjk
r dt d | j}t|Y nX | jj| _|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@NUnsupported mechanism OID.)Z peer_nameZ mech_typeZ req_flagsz
{} Target: {}r. )r0 r7 r r rI NameZC_NT_HOSTBASED_SERVICEContextrJ flagsZOIDZmech_from_stringr r8 r9 r ZInitContextr stepGSSExceptionformatsysexc_infoestablishedr )
r$ targetr: r)
recv_tokenr7 targ_namectx krb5_mechr; r<