|
| 1 | +# -*- python -*- |
| 2 | +# |
| 3 | +# GSSAPI SASL Code for LDAP Auth |
| 4 | +# |
| 5 | +# This implements the RFC 4752 SASL Mechanism GSSAPI for ldaptor |
| 6 | +# |
| 7 | +# (c) 2016 CONTACT SOFTWARE GmbH (www.contact-software.com) |
| 8 | +# |
| 9 | +# MIT License. |
| 10 | +# |
| 11 | +# Permission is hereby granted, free of charge, to any person obtaining |
| 12 | +# a copy of this software and associated documentation files (the |
| 13 | +# "Software"), to deal in the Software without restriction, including |
| 14 | +# without limitation the rights to use, copy, modify, merge, publish, |
| 15 | +# distribute, sublicense, and/or sell copies of the Software, and to |
| 16 | +# permit persons to whom the Software is furnished to do so, subject to |
| 17 | +# the following conditions: |
| 18 | +# |
| 19 | +# The above copyright notice and this permission notice shall be |
| 20 | +# included in all copies or substantial portions of the Software. |
| 21 | +# |
| 22 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 23 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 24 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 25 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 26 | +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 27 | +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 28 | +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 29 | +# |
| 30 | + |
| 31 | +import sys |
| 32 | +import base64 |
| 33 | +import struct |
| 34 | + |
| 35 | +if sys.platform == 'win32': |
| 36 | + import kerberos_sspi as kerberos |
| 37 | +else: |
| 38 | + import kerberos |
| 39 | + |
| 40 | +__all__ = [ |
| 41 | + 'SASL_GSSAPIClientContext', |
| 42 | + 'SASL_GSSAPIServerContext', |
| 43 | +] |
| 44 | + |
| 45 | + |
| 46 | +# RFC 4752 Sect. 3 |
| 47 | +SASL_MECHANISM = 'GSSAPI' |
| 48 | + |
| 49 | + |
| 50 | +class SASL_GSSAPIClientContext(object): |
| 51 | + |
| 52 | + def __init__(self, service, host): |
| 53 | + spn = "%s@%s" % (service, host) |
| 54 | + self.client = None |
| 55 | + self.response = "" |
| 56 | + self.cres = kerberos.AUTH_GSS_CONTINUE |
| 57 | + self.ctx = None |
| 58 | + |
| 59 | + flags = (kerberos.GSS_C_CONF_FLAG |
| 60 | + | kerberos.GSS_C_INTEG_FLAG |
| 61 | + | kerberos.GSS_C_REPLAY_FLAG |
| 62 | + | kerberos.GSS_C_SEQUENCE_FLAG) |
| 63 | + |
| 64 | + errc, self.client = kerberos.authGSSClientInit(spn, gssflags=flags) |
| 65 | + |
| 66 | + self._round = 0 |
| 67 | + |
| 68 | + def send(self, token_in): |
| 69 | + if not self.ctx: |
| 70 | + self.ctx = self._coro() |
| 71 | + # Move to first waiting yield |
| 72 | + self.ctx.send(None) |
| 73 | + return self.ctx.send(token_in) |
| 74 | + |
| 75 | + def __del__(self): |
| 76 | + # TODO: Find a nicer way to trigger cleanup |
| 77 | + self.ctx = None |
| 78 | + client = self.client |
| 79 | + self.client = None |
| 80 | + if client: |
| 81 | + kerberos.authGSSClientClean(client) |
| 82 | + |
| 83 | + def start(self): |
| 84 | + ctx = self._coro() |
| 85 | + # Move to first waiting yield |
| 86 | + ctx.send(None) |
| 87 | + return ctx |
| 88 | + |
| 89 | + def _handle_sasl_gssapi(self, token_in): |
| 90 | + if sys.platform == "win32": |
| 91 | + return self._handle_sasl_gssapi_win32(token_in) |
| 92 | + else: |
| 93 | + return self._handle_sasl_gssapi_unix(token_in) |
| 94 | + |
| 95 | + def _handle_sasl_gssapi_win32(self, token_in): |
| 96 | + # TODO: Simplify if kerberos_sspi gets fixed and authGSSClientUnwrap() |
| 97 | + # works as it should. |
| 98 | + # (https://github.com/may-day/kerberos-sspi/pull/3) |
| 99 | + code = kerberos.authGSSClientUnwrap( |
| 100 | + self.ctx, base64.encodestring(token_in)) |
| 101 | + if code == -1: |
| 102 | + raise RuntimeError("SASL GSSAPI Auth failed") |
| 103 | + |
| 104 | + data = kerberos.authGSSClientResponse(self.ctx) |
| 105 | + data = self._process_security_options(data) |
| 106 | + import sspicon |
| 107 | + import win32security |
| 108 | + ca = self.ctx['csa'] |
| 109 | + context = self.ctx |
| 110 | + pkg_size_info = ca.ctxt.QueryContextAttributes(sspicon.SECPKG_ATTR_SIZES) |
| 111 | + trailersize = pkg_size_info['SecurityTrailer'] |
| 112 | + blocksize = pkg_size_info['BlockSize'] |
| 113 | + |
| 114 | + encbuf = win32security.PySecBufferDescType() |
| 115 | + encbuf.append(win32security.PySecBufferType(trailersize, sspicon.SECBUFFER_TOKEN)) |
| 116 | + encbuf.append(win32security.PySecBufferType(len(data), sspicon.SECBUFFER_DATA)) |
| 117 | + encbuf.append(win32security.PySecBufferType(blocksize, sspicon.SECBUFFER_PADDING)) |
| 118 | + encbuf[1].Buffer = data |
| 119 | + ca.ctxt.EncryptMessage(0, encbuf, ca._get_next_seq_num()) |
| 120 | + |
| 121 | + context["response"] = encbuf[0].Buffer+encbuf[1].Buffer+encbuf[2].Buffer |
| 122 | + self.response = kerberos.authGSSClientResponse(self.ctx) |
| 123 | + |
| 124 | + def _handle_sasl_gssapi_unix(self, token_in): |
| 125 | + # TODO: Probably needs a fix similar to kerberos_sspi |
| 126 | + pass |
| 127 | + |
| 128 | + def _process_security_options(self, data, user=None): |
| 129 | + """ |
| 130 | + Handle the security layer settings |
| 131 | + """ |
| 132 | + conf_and_size = data[:struct.calcsize("!L")] # network unsigned long |
| 133 | + size = struct.unpack("!L", conf_and_size)[0] & 0x00ffffff |
| 134 | + conf = struct.unpack("B", conf_and_size[0])[0] # B .. unsigned char |
| 135 | + |
| 136 | + # FIXME: Debug prints... |
| 137 | + print "N" if conf & kerberos.GSS_AUTH_P_NONE else "-" |
| 138 | + print "I" if conf & kerberos.GSS_AUTH_P_INTEGRITY else "-" |
| 139 | + print "P" if conf & kerberos.GSS_AUTH_P_PRIVACY else "-" |
| 140 | + print "Maximum GSS token size is %d" % size |
| 141 | + |
| 142 | + # Tell the truth, we do not handle any security layer |
| 143 | + # (aka GSS_AUTH_P_NONE). RFC 4752 demands that the |
| 144 | + # max client message size is zero in this case. |
| 145 | + max_size_client_message = 0 |
| 146 | + security_layer = kerberos.GSS_AUTH_P_NONE |
| 147 | + data = struct.pack("!L", security_layer << 24 + |
| 148 | + (max_size_client_message & 0x00ffffff)) |
| 149 | + if user: |
| 150 | + data = data + user.encode("utf-8") |
| 151 | + return data |
| 152 | + |
| 153 | + def _coro(self): |
| 154 | + """ |
| 155 | + Statemachine for the SASL progress |
| 156 | + """ |
| 157 | + token_in = yield |
| 158 | + while self.cres == kerberos.AUTH_GSS_CONTINUE: |
| 159 | + self.cres = kerberos.authGSSClientStep( |
| 160 | + self.ctx, |
| 161 | + base64.encodestring(token_in) if token_in is not None else None |
| 162 | + ) |
| 163 | + if self.cres == -1: |
| 164 | + break |
| 165 | + self.response = kerberos.authGSSClientResponse(self.ctx) |
| 166 | + self._round += 1 |
| 167 | + token_in = yield (SASL_MECHANISM, base64.decodestring(self.response)) |
| 168 | + if self.cres == kerberos.AUTH_GSS_COMPLETE: |
| 169 | + token_in = yield (SASL_MECHANISM, base64.decodestring(self.response)) |
| 170 | + self._handle_sasl_gssapi(token_in) |
| 171 | + yield (SASL_MECHANISM, base64.decodestring(self.response)) |
| 172 | + else: |
| 173 | + raise RuntimeError("Unexpected extra token. Auth Failed") |
| 174 | + |
| 175 | + |
| 176 | +class SASL_GSSAPIServerContext(object): |
| 177 | + def __init__(self): |
| 178 | + pass |
0 commit comments