Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kerberos.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """
  2. """
  3. # Created on 2015.04.08
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2015 - 2018 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. # original code by Hugh Cole-Baker, modified by Peter Foley
  25. # it needs the gssapi package
  26. import socket
  27. from ...core.exceptions import LDAPPackageUnavailableError, LDAPCommunicationError
  28. try:
  29. # noinspection PyPackageRequirements,PyUnresolvedReferences
  30. import gssapi
  31. except ImportError:
  32. raise LDAPPackageUnavailableError('package gssapi missing')
  33. from .sasl import send_sasl_negotiation, abort_sasl_negotiation
  34. NO_SECURITY_LAYER = 1
  35. INTEGRITY_PROTECTION = 2
  36. CONFIDENTIALITY_PROTECTION = 4
  37. def sasl_gssapi(connection, controls):
  38. """
  39. Performs a bind using the Kerberos v5 ("GSSAPI") SASL mechanism
  40. from RFC 4752. Does not support any security layers, only authentication!
  41. sasl_credentials can be empty or a tuple with one or two elements.
  42. The first element determines which service principal to request a ticket for and can be one of the following:
  43. - None or False, to use the hostname from the Server object
  44. - True to perform a reverse DNS lookup to retrieve the canonical hostname for the hosts IP address
  45. - A string containing the hostname
  46. The optional second element is what authorization ID to request.
  47. - If omitted or None, the authentication ID is used as the authorization ID
  48. - If a string, the authorization ID to use. Should start with "dn:" or "user:".
  49. """
  50. target_name = None
  51. authz_id = b""
  52. if connection.sasl_credentials:
  53. if len(connection.sasl_credentials) >= 1 and connection.sasl_credentials[0]:
  54. if connection.sasl_credentials[0] is True:
  55. hostname = socket.gethostbyaddr(connection.socket.getpeername()[0])[0]
  56. target_name = gssapi.Name('ldap@' + hostname, gssapi.NameType.hostbased_service)
  57. else:
  58. target_name = gssapi.Name('ldap@' + connection.sasl_credentials[0], gssapi.NameType.hostbased_service)
  59. if len(connection.sasl_credentials) >= 2 and connection.sasl_credentials[1]:
  60. authz_id = connection.sasl_credentials[1].encode("utf-8")
  61. if target_name is None:
  62. target_name = gssapi.Name('ldap@' + connection.server.host, gssapi.NameType.hostbased_service)
  63. creds = gssapi.Credentials(name=gssapi.Name(connection.user), usage='initiate') if connection.user else None
  64. ctx = gssapi.SecurityContext(name=target_name, mech=gssapi.MechType.kerberos, creds=creds)
  65. in_token = None
  66. try:
  67. while True:
  68. out_token = ctx.step(in_token)
  69. if out_token is None:
  70. out_token = ''
  71. result = send_sasl_negotiation(connection, controls, out_token)
  72. in_token = result['saslCreds']
  73. try:
  74. # This raised an exception in gssapi<1.1.2 if the context was
  75. # incomplete, but was fixed in
  76. # https://github.com/pythongssapi/python-gssapi/pull/70
  77. if ctx.complete:
  78. break
  79. except gssapi.exceptions.MissingContextError:
  80. pass
  81. unwrapped_token = ctx.unwrap(in_token)
  82. if len(unwrapped_token.message) != 4:
  83. raise LDAPCommunicationError("Incorrect response from server")
  84. server_security_layers = unwrapped_token.message[0]
  85. if not isinstance(server_security_layers, int):
  86. server_security_layers = ord(server_security_layers)
  87. if server_security_layers in (0, NO_SECURITY_LAYER):
  88. if unwrapped_token.message[1:] != '\x00\x00\x00':
  89. raise LDAPCommunicationError("Server max buffer size must be 0 if no security layer")
  90. if not (server_security_layers & NO_SECURITY_LAYER):
  91. raise LDAPCommunicationError("Server requires a security layer, but this is not implemented")
  92. client_security_layers = bytearray([NO_SECURITY_LAYER, 0, 0, 0])
  93. out_token = ctx.wrap(bytes(client_security_layers)+authz_id, False)
  94. return send_sasl_negotiation(connection, controls, out_token.message)
  95. except (gssapi.exceptions.GSSError, LDAPCommunicationError):
  96. abort_sasl_negotiation(connection, controls)
  97. raise