Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

124 řádky
5.4 KiB

  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. The optional third element is a raw gssapi credentials structure which can be used over
  50. the implicit use of a krb ccache.
  51. """
  52. target_name = None
  53. authz_id = b""
  54. raw_creds = None
  55. creds = None
  56. if connection.sasl_credentials:
  57. if len(connection.sasl_credentials) >= 1 and connection.sasl_credentials[0]:
  58. if connection.sasl_credentials[0] is True:
  59. hostname = socket.gethostbyaddr(connection.socket.getpeername()[0])[0]
  60. target_name = gssapi.Name('ldap@' + hostname, gssapi.NameType.hostbased_service)
  61. else:
  62. target_name = gssapi.Name('ldap@' + connection.sasl_credentials[0], gssapi.NameType.hostbased_service)
  63. if len(connection.sasl_credentials) >= 2 and connection.sasl_credentials[1]:
  64. authz_id = connection.sasl_credentials[1].encode("utf-8")
  65. if len(connection.sasl_credentials) >= 3 and connection.sasl_credentials[2]:
  66. raw_creds = connection.sasl_credentials[2]
  67. if target_name is None:
  68. target_name = gssapi.Name('ldap@' + connection.server.host, gssapi.NameType.hostbased_service)
  69. if raw_creds is not None:
  70. creds = gssapi.Credentials(base=raw_creds, usage='initiate', store=connection.cred_store)
  71. else:
  72. creds = gssapi.Credentials(name=gssapi.Name(connection.user), usage='initiate', store=connection.cred_store) if connection.user else None
  73. ctx = gssapi.SecurityContext(name=target_name, mech=gssapi.MechType.kerberos, creds=creds)
  74. in_token = None
  75. try:
  76. while True:
  77. out_token = ctx.step(in_token)
  78. if out_token is None:
  79. out_token = ''
  80. result = send_sasl_negotiation(connection, controls, out_token)
  81. in_token = result['saslCreds']
  82. try:
  83. # This raised an exception in gssapi<1.1.2 if the context was
  84. # incomplete, but was fixed in
  85. # https://github.com/pythongssapi/python-gssapi/pull/70
  86. if ctx.complete:
  87. break
  88. except gssapi.exceptions.MissingContextError:
  89. pass
  90. unwrapped_token = ctx.unwrap(in_token)
  91. if len(unwrapped_token.message) != 4:
  92. raise LDAPCommunicationError("Incorrect response from server")
  93. server_security_layers = unwrapped_token.message[0]
  94. if not isinstance(server_security_layers, int):
  95. server_security_layers = ord(server_security_layers)
  96. if server_security_layers in (0, NO_SECURITY_LAYER):
  97. if unwrapped_token.message[1:] != '\x00\x00\x00':
  98. raise LDAPCommunicationError("Server max buffer size must be 0 if no security layer")
  99. if not (server_security_layers & NO_SECURITY_LAYER):
  100. raise LDAPCommunicationError("Server requires a security layer, but this is not implemented")
  101. client_security_layers = bytearray([NO_SECURITY_LAYER, 0, 0, 0])
  102. out_token = ctx.wrap(bytes(client_security_layers)+authz_id, False)
  103. return send_sasl_negotiation(connection, controls, out_token.message)
  104. except (gssapi.exceptions.GSSError, LDAPCommunicationError):
  105. abort_sasl_negotiation(connection, controls)
  106. raise