|
| 1 | +from gssapi.raw.cython_types cimport * |
| 2 | +from gssapi.raw.oids cimport OID |
| 3 | +from gssapi.raw.cython_converters cimport c_create_oid_set |
| 4 | +GSSAPI="BASE" # This ensures that a full module is generated by Cython |
| 5 | + |
| 6 | +from gssapi.raw.cython_converters cimport c_get_mech_oid_set |
| 7 | + |
| 8 | +from gssapi.raw.named_tuples import InquireAttrsResult, DisplayAttrResult |
| 9 | +from gssapi.raw.misc import GSSError |
| 10 | + |
| 11 | +cdef extern from "python_gssapi_ext.h": |
| 12 | + OM_uint32 gss_indicate_mechs_by_attrs( |
| 13 | + OM_uint32 *minor_status, |
| 14 | + const gss_OID_set desired_mech_attrs, |
| 15 | + const gss_OID_set except_mech_attrs, |
| 16 | + const gss_OID_set critical_mech_attrs, |
| 17 | + gss_OID_set *mechs) nogil |
| 18 | + |
| 19 | + OM_uint32 gss_inquire_attrs_for_mech( |
| 20 | + OM_uint32 *minor_status, |
| 21 | + const gss_OID mech, |
| 22 | + gss_OID_set *mech_attrs, |
| 23 | + gss_OID_set *known_mech_attrs) nogil |
| 24 | + |
| 25 | + OM_uint32 gss_display_mech_attr( |
| 26 | + OM_uint32 *minor_status, |
| 27 | + const gss_OID mech_attr, |
| 28 | + gss_buffer_t name, |
| 29 | + gss_buffer_t short_desc, |
| 30 | + gss_buffer_t long_desc) nogil |
| 31 | + |
| 32 | + |
| 33 | +def indicate_mechs_by_attrs(desired_mech_attrs=None, except_mech_attrs=None, |
| 34 | + critical_mech_attrs=None): |
| 35 | + """ |
| 36 | + indicate_mechs_by_attrs(desired_mech_attrs=None, except_mech_attrs=None, |
| 37 | + critical_mech_attrs=None) |
| 38 | + Get a set of mechanisms that have the specified attributes. |
| 39 | +
|
| 40 | + Args: |
| 41 | + desired_mech_attrs ([OID]): Attributes that the output mechs MUST |
| 42 | + offer |
| 43 | + except_mech_attrs ([OID]): Attributes that the output mechs MUST NOT |
| 44 | + offer |
| 45 | + critical_mech_attrs ([OID]): Attributes that the output mechs MUST |
| 46 | + understand and offer |
| 47 | +
|
| 48 | + Returns: |
| 49 | + [MechType]: a set of mechs which satisfy the given criteria |
| 50 | +
|
| 51 | + Raises: |
| 52 | + GSSError |
| 53 | + """ |
| 54 | + cdef OM_uint32 maj_stat, min_stat |
| 55 | + cdef gss_OID_set desired_attrs = GSS_C_NO_OID_SET |
| 56 | + cdef gss_OID_set except_attrs = GSS_C_NO_OID_SET |
| 57 | + cdef gss_OID_set critical_attrs = GSS_C_NO_OID_SET |
| 58 | + cdef gss_OID_set mechs |
| 59 | + |
| 60 | + if desired_mech_attrs is not None: |
| 61 | + desired_attrs = c_get_mech_oid_set(desired_mech_attrs) |
| 62 | + |
| 63 | + if except_mech_attrs is not None: |
| 64 | + except_attrs = c_get_mech_oid_set(except_mech_attrs) |
| 65 | + |
| 66 | + if critical_mech_attrs is not None: |
| 67 | + critical_attrs = c_get_mech_oid_set(critical_mech_attrs) |
| 68 | + |
| 69 | + with nogil: |
| 70 | + maj_stat = gss_indicate_mechs_by_attrs(&min_stat, desired_attrs, |
| 71 | + except_attrs, critical_attrs, |
| 72 | + &mechs) |
| 73 | + |
| 74 | + if maj_stat == GSS_S_COMPLETE: |
| 75 | + return c_create_oid_set(mechs) |
| 76 | + else: |
| 77 | + raise GSSError(maj_stat, min_stat) |
| 78 | + |
| 79 | + |
| 80 | +def inquire_attrs_for_mech(OID mech): |
| 81 | + """ |
| 82 | + inquire_attrs_for_mech(mech) |
| 83 | + Gets the set of attrs supported and known by a mechanism. |
| 84 | +
|
| 85 | + Args: |
| 86 | + mech (MechType): Mechanism to inquire about |
| 87 | +
|
| 88 | + Returns: |
| 89 | + InquireAttrsResult: the results of inquiry; a mech's attributes and |
| 90 | + known attributes |
| 91 | +
|
| 92 | + Raises: |
| 93 | + GSSError |
| 94 | + """ |
| 95 | + cdef OM_uint32 maj_stat, min_stat |
| 96 | + cdef gss_OID m = GSS_C_NO_OID |
| 97 | + cdef gss_OID_set mech_attrs = GSS_C_NO_OID_SET |
| 98 | + cdef gss_OID_set known_mech_attrs = GSS_C_NO_OID_SET |
| 99 | + |
| 100 | + if mech is not None: |
| 101 | + m = &mech.raw_oid |
| 102 | + |
| 103 | + with nogil: |
| 104 | + maj_stat = gss_inquire_attrs_for_mech(&min_stat, m, &mech_attrs, |
| 105 | + &known_mech_attrs) |
| 106 | + |
| 107 | + if maj_stat == GSS_S_COMPLETE: |
| 108 | + return InquireAttrsResult(c_create_oid_set(mech_attrs), |
| 109 | + c_create_oid_set(known_mech_attrs)) |
| 110 | + else: |
| 111 | + raise GSSError(maj_stat, min_stat) |
| 112 | + |
| 113 | + |
| 114 | +def display_mech_attr(OID attr): |
| 115 | + """ |
| 116 | + display_mech_attrs(attr) |
| 117 | + Returns information about attributes in human readable form. |
| 118 | +
|
| 119 | + Args: |
| 120 | + attr (OID): Mechanism attribute to retrive names and descriptions of |
| 121 | +
|
| 122 | + Returns: |
| 123 | + DisplayAttrResult: the results of displaying the attribute; mech name, |
| 124 | + short description, and long description. |
| 125 | +
|
| 126 | + Raises: |
| 127 | + GSSError |
| 128 | + """ |
| 129 | + cdef OM_uint32 maj_stat, min_stat |
| 130 | + cdef gss_OID a = GSS_C_NO_OID |
| 131 | + cdef gss_buffer_desc name |
| 132 | + cdef gss_buffer_desc short_desc |
| 133 | + cdef gss_buffer_desc long_desc |
| 134 | + |
| 135 | + if attr is not None: |
| 136 | + a = &attr.raw_oid |
| 137 | + |
| 138 | + with nogil: |
| 139 | + maj_stat = gss_display_mech_attr(&min_stat, a, &name, &short_desc, |
| 140 | + &long_desc) |
| 141 | + |
| 142 | + if maj_stat == GSS_S_COMPLETE: |
| 143 | + out_name = name.value[:name.length] |
| 144 | + out_short = short_desc.value[:short_desc.length] |
| 145 | + out_long = long_desc.value[:long_desc.length] |
| 146 | + |
| 147 | + gss_release_buffer(&min_stat, &name) |
| 148 | + gss_release_buffer(&min_stat, &short_desc) |
| 149 | + gss_release_buffer(&min_stat, &long_desc) |
| 150 | + |
| 151 | + return DisplayAttrResult(out_name, out_short, out_long) |
| 152 | + else: |
| 153 | + raise GSSError(maj_stat, min_stat) |
0 commit comments