|
| 1 | +GSSAPI="BASE" # This ensures that a full module is generated by Cythin |
| 2 | + |
| 3 | +# Due to a bug in MIT Kerberos, add_cred_with_password was not properly |
| 4 | +# exported for some time. In order to work around this, |
| 5 | +# add_cred_with_password is in its own file. For more information, see: |
| 6 | +# https://github.com/krb5/krb5/pull/244 |
| 7 | + |
| 8 | +from gssapi.raw.cython_types cimport * |
| 9 | +from gssapi.raw.cython_converters cimport c_get_mech_oid_set |
| 10 | +from gssapi.raw.cython_converters cimport c_create_oid_set |
| 11 | +from gssapi.raw.cython_converters cimport c_py_ttl_to_c, c_c_ttl_to_py |
| 12 | +from gssapi.raw.creds cimport Creds |
| 13 | +from gssapi.raw.names cimport Name |
| 14 | +from gssapi.raw.oids cimport OID |
| 15 | + |
| 16 | +from gssapi.raw.misc import GSSError |
| 17 | +from gssapi.raw.named_tuples import AddCredResult |
| 18 | + |
| 19 | +cdef extern from "gssapi/gssapi_ext.h": |
| 20 | + OM_uint32 gss_add_cred_with_password(OM_uint32 *min_stat, |
| 21 | + const gss_cred_id_t input_cred_handle, |
| 22 | + const gss_name_t desired_name, |
| 23 | + const gss_OID desired_mech, |
| 24 | + const gss_buffer_t password, |
| 25 | + gss_cred_usage_t cred_usage, |
| 26 | + OM_uint32 initiator_ttl, |
| 27 | + OM_uint32 acceptor_ttl, |
| 28 | + gss_cred_id_t *output_creds, |
| 29 | + gss_OID_set *actual_mechs, |
| 30 | + OM_uint32 *actual_init_ttl, |
| 31 | + OM_uint32 *actual_accept_ttl) nogil |
| 32 | + |
| 33 | + |
| 34 | +def add_cred_with_password(Creds input_cred not None, Name name not None, |
| 35 | + OID mech not None, password not None, |
| 36 | + usage="initiate", init_lifetime=None, |
| 37 | + accept_lifetime=None): |
| 38 | + |
| 39 | + """ |
| 40 | + Add a credential-element to a credential using provided password. |
| 41 | +
|
| 42 | + This function is originally from Solaris and is not documented by either |
| 43 | + MIT or Heimdal. |
| 44 | +
|
| 45 | + Args: |
| 46 | + input_cred (Creds): the credentials to add to |
| 47 | + name (Name): the name to acquire credentials for |
| 48 | + mech (MechType): the desired mechanism. Note that this is both |
| 49 | + singular and required |
| 50 | + password (str): the password used to acquire credentialss with |
| 51 | + usage (str): the usage type for the credentials: may be |
| 52 | + 'initiate', 'accept', or 'both' |
| 53 | + init_lifetime (int): the lifetime for the credentials to remain valid |
| 54 | + when using them to initiate security contexts (or None for |
| 55 | + indefinite) |
| 56 | + accept_lifetime (int): the lifetime for the credentials to remain |
| 57 | + valid when using them to accept security contexts (or None for |
| 58 | + indefinite) |
| 59 | +
|
| 60 | + Returns: |
| 61 | + AddCredResult: the actual mechanisms with which the credentials may be |
| 62 | + used, the actual initiator TTL, and the actual acceptor TTL (the TTLs |
| 63 | + may be None for indefinite or not supported) |
| 64 | +
|
| 65 | + Raises: |
| 66 | + GSSError |
| 67 | + """ |
| 68 | + |
| 69 | + cdef gss_buffer_desc password_buffer = gss_buffer_desc(len(password), |
| 70 | + password) |
| 71 | + |
| 72 | + cdef gss_cred_usage_t c_usage |
| 73 | + if usage == "initiate": |
| 74 | + c_usage = GSS_C_INITIATE |
| 75 | + elif usage == "accept": |
| 76 | + c_usage = GSS_C_ACCEPT |
| 77 | + else: |
| 78 | + c_usage = GSS_C_BOTH |
| 79 | + |
| 80 | + cdef OM_uint32 input_initiator_ttl = c_py_ttl_to_c(init_lifetime) |
| 81 | + cdef OM_uint32 input_acceptor_ttl = c_py_ttl_to_c(accept_lifetime) |
| 82 | + |
| 83 | + cdef gss_cred_id_t creds |
| 84 | + cdef gss_OID_set actual_mechs |
| 85 | + cdef OM_uint32 actual_initiator_ttl |
| 86 | + cdef OM_uint32 actual_acceptor_ttl |
| 87 | + |
| 88 | + cdef OM_uint32 maj_stat, min_stat |
| 89 | + |
| 90 | + with nogil: |
| 91 | + maj_stat = gss_add_cred_with_password( |
| 92 | + &min_stat, input_cred.raw_creds, name.raw_name, &mech.raw_oid, |
| 93 | + &password_buffer, c_usage, input_initiator_ttl, |
| 94 | + input_acceptor_ttl, &creds, &actual_mechs, &actual_initiator_ttl, |
| 95 | + &actual_acceptor_ttl) |
| 96 | + |
| 97 | + cdef Creds rc |
| 98 | + if maj_stat == GSS_S_COMPLETE: |
| 99 | + rc = Creds() |
| 100 | + rc.raw_creds = creds |
| 101 | + return AddCredResult(rc, c_create_oid_set(actual_mechs), |
| 102 | + c_c_ttl_to_py(actual_initiator_ttl), |
| 103 | + c_c_ttl_to_py(actual_acceptor_ttl)) |
| 104 | + else: |
| 105 | + raise GSSError(maj_stat, min_stat) |
0 commit comments