|
| 1 | +GSSAPI="BASE" # This ensures that a full module is generated by Cython |
| 2 | + |
| 3 | +from gssapi.raw.cython_types cimport * |
| 4 | +from gssapi.raw.names cimport Name |
| 5 | +from gssapi.raw.oids cimport OID |
| 6 | + |
| 7 | +from gssapi.raw.misc import GSSError |
| 8 | +from gssapi import _utils |
| 9 | + |
| 10 | +from posix.types cimport uid_t |
| 11 | + |
| 12 | +cdef extern from "python_gssapi_ext.h": |
| 13 | + OM_uint32 gss_localname(OM_uint32 *minor, |
| 14 | + const gss_name_t name, |
| 15 | + const gss_OID mech_type, |
| 16 | + gss_buffer_t localname) nogil |
| 17 | + |
| 18 | + int gss_userok(const gss_name_t name, |
| 19 | + const char *username) nogil |
| 20 | + |
| 21 | + OM_uint32 gss_authorize_localname(OM_uint32 *minor, |
| 22 | + const gss_name_t name, |
| 23 | + const gss_name_t user) nogil |
| 24 | + |
| 25 | + OM_uint32 gss_pname_to_uid(OM_uint32 *minor, |
| 26 | + const gss_name_t name, |
| 27 | + const gss_OID mech_type, |
| 28 | + uid_t *uid_out) nogil |
| 29 | + |
| 30 | + |
| 31 | +def localname(Name name not None, OID mech=None): |
| 32 | + """Get the local name for a GSSAPI name. |
| 33 | +
|
| 34 | + This method determines the local name associated with a GSSAPI |
| 35 | + name, optionally for a given mechanism. |
| 36 | +
|
| 37 | + Args: |
| 38 | + name (Name): the GSSAPI name to map to a local name |
| 39 | + mech (~gssapi.OID): the mechanism to use for the mapping |
| 40 | + (or None for the default) |
| 41 | +
|
| 42 | + Returns: |
| 43 | + bytes: the local name |
| 44 | +
|
| 45 | + Raises: |
| 46 | + ~gssapi.exceptions.GSSError |
| 47 | + """ |
| 48 | + cdef gss_OID m = GSS_C_NO_OID |
| 49 | + if mech is not None: |
| 50 | + m = &mech.raw_oid |
| 51 | + |
| 52 | + cdef gss_buffer_desc output = gss_buffer_desc(0, NULL) |
| 53 | + |
| 54 | + cdef OM_uint32 maj_stat, min_stat |
| 55 | + |
| 56 | + with nogil: |
| 57 | + maj_stat = gss_localname(&min_stat, name.raw_name, m, &output) |
| 58 | + |
| 59 | + if maj_stat == GSS_S_COMPLETE: |
| 60 | + py_output = (<char*>output.value)[:output.length] |
| 61 | + gss_release_buffer(&min_stat, &output) |
| 62 | + return py_output |
| 63 | + else: |
| 64 | + raise GSSError(maj_stat, min_stat) |
| 65 | + |
| 66 | + |
| 67 | +def userok(Name name not None, username not None): |
| 68 | + """Determine whether a GSSAPI name is authorized to act as a local user. |
| 69 | +
|
| 70 | + This method determines whether a given GSSAPI name is authorized |
| 71 | + to act as the given local username. This is a simple wrapper |
| 72 | + around :func:`authorize_localname` that only supports system |
| 73 | + usernames as local names. |
| 74 | +
|
| 75 | + Args: |
| 76 | + name (Name): the GSSAPI name to check |
| 77 | + username (Union[bytes, str]): the local username to check against |
| 78 | +
|
| 79 | + Returns: |
| 80 | + bool: whether or not the name is authorized to act as the user |
| 81 | + """ |
| 82 | + cdef int res |
| 83 | + |
| 84 | + if isinstance(username, str): |
| 85 | + username = username.encode(_utils._get_encoding()) |
| 86 | + |
| 87 | + cdef char *c_username = username |
| 88 | + |
| 89 | + with nogil: |
| 90 | + res = gss_userok(name.raw_name, c_username) |
| 91 | + |
| 92 | + return res == 1 |
| 93 | + |
| 94 | + |
| 95 | +def authorize_localname(Name name not None, Name user not None): |
| 96 | + """Determine whether a GSSAPI name is authorized to act as a local name. |
| 97 | +
|
| 98 | + This method determines whether a given GSSAPI name is authorized |
| 99 | + to act as the given local name. |
| 100 | +
|
| 101 | + Args: |
| 102 | + name (Name): the mechanism name to check |
| 103 | + user (Name): the local name to check against |
| 104 | +
|
| 105 | + Returns: |
| 106 | + bool: whether or not the name is authorized |
| 107 | +
|
| 108 | + Raises: |
| 109 | + ~gssapi.exceptions.GSSError |
| 110 | + """ |
| 111 | + cdef OM_uint32 maj_stat, min_stat |
| 112 | + |
| 113 | + with nogil: |
| 114 | + maj_stat = gss_authorize_localname(&min_stat, name.raw_name, |
| 115 | + user.raw_name) |
| 116 | + |
| 117 | + if maj_stat == GSS_S_COMPLETE: |
| 118 | + return True |
| 119 | + else: |
| 120 | + raise GSSError(maj_stat, min_stat) |
| 121 | + |
| 122 | + |
| 123 | +def pname_to_uid(Name name not None, OID mech=None): |
| 124 | + """Get the local UID for a GSSAPI name. |
| 125 | +
|
| 126 | + This method determines the local UID associated with a GSSAPI |
| 127 | + name, optionally for a given mechanism. |
| 128 | +
|
| 129 | + Note: |
| 130 | + This function is not available on Windows. |
| 131 | +
|
| 132 | + Args: |
| 133 | + name (Name): the GSSAPI name to map to a local UID |
| 134 | + mech (~gssapi.OID): the mechanism to use for the mapping |
| 135 | + (or None for the default) |
| 136 | +
|
| 137 | + Returns: |
| 138 | + int: the local UID |
| 139 | +
|
| 140 | + Raises: |
| 141 | + ~gssapi.exceptions.GSSError |
| 142 | + """ |
| 143 | + cdef gss_OID m = GSS_C_NO_OID |
| 144 | + if mech is not None: |
| 145 | + m = &mech.raw_oid |
| 146 | + |
| 147 | + cdef uid_t uid_out |
| 148 | + cdef OM_uint32 maj_stat, min_stat |
| 149 | + |
| 150 | + with nogil: |
| 151 | + maj_stat = gss_pname_to_uid(&min_stat, name.raw_name, m, &uid_out) |
| 152 | + |
| 153 | + if maj_stat == GSS_S_COMPLETE: |
| 154 | + return uid_out |
| 155 | + else: |
| 156 | + raise GSSError(maj_stat, min_stat) |
0 commit comments