codekingpro/portable-devtools
114k
1"""
2"""
3
4# Created on 2013.05.31
5#
6# Author: Giovanni Cannata
7#
8# Copyright 2013 - 2020 Giovanni Cannata
9#
10# This file is part of ldap3.
11#
12# ldap3 is free software: you can redistribute it and/or modify
13# it under the terms of the GNU Lesser General Public License as published
14# by the Free Software Foundation, either version 3 of the License, or
15# (at your option) any later version.
16#
17# ldap3 is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU Lesser General Public License for more details.
21#
22# You should have received a copy of the GNU Lesser General Public License
23# along with ldap3 in the COPYING and COPYING.LESSER files.
24# If not, see <http://www.gnu.org/licenses/>.
25
26from .. import SIMPLE, ANONYMOUS, SASL, STRING_TYPES
27from ..core.results import RESULT_CODES
28from ..core.exceptions import LDAPUserNameIsMandatoryError, LDAPPasswordIsMandatoryError, LDAPUnknownAuthenticationMethodError, LDAPUserNameNotAllowedError
29from ..protocol.sasl.sasl import validate_simple_password
30from ..protocol.rfc4511 import Version, AuthenticationChoice, Simple, BindRequest, ResultCode, SaslCredentials, BindResponse, \
31 LDAPDN, LDAPString, Referral, ServerSaslCreds, SicilyPackageDiscovery, SicilyNegotiate, SicilyResponse
32from ..protocol.convert import authentication_choice_to_dict, referrals_to_list
33from ..utils.conv import to_unicode, to_raw
34
35# noinspection PyUnresolvedReferences
36def bind_operation(version,
37 authentication,
38 name='',
39 password=None,
40 sasl_mechanism=None,
41 sasl_credentials=None,
42 auto_encode=False):
43 # BindRequest ::= [APPLICATION 0] SEQUENCE {
44 # version INTEGER (1 .. 127),
45 # name LDAPDN,
46 # authentication AuthenticationChoice }
47 request = BindRequest()
48 request['version'] = Version(version)
49 if name is None:
50 name = ''
51 if isinstance(name, STRING_TYPES):
52 request['name'] = to_unicode(name) if auto_encode else name
53 if authentication == SIMPLE:
54 if not name:
55 raise LDAPUserNameIsMandatoryError('user name is mandatory in simple bind')
56 if password:
57 request['authentication'] = AuthenticationChoice().setComponentByName('simple', Simple(validate_simple_password(password)))
58 else:
59 raise LDAPPasswordIsMandatoryError('password is mandatory in simple bind')
60 elif authentication == SASL:
61 sasl_creds = SaslCredentials()
62 sasl_creds['mechanism'] = sasl_mechanism
63 if sasl_credentials is not None:
64 sasl_creds['credentials'] = sasl_credentials
65 # else:
66 # sasl_creds['credentials'] = None
67 request['authentication'] = AuthenticationChoice().setComponentByName('sasl', sasl_creds)
68 elif authentication == ANONYMOUS:
69 if name:
70 raise LDAPUserNameNotAllowedError('user name not allowed in anonymous bind')
71 request['name'] = ''
72 request['authentication'] = AuthenticationChoice().setComponentByName('simple', Simple(''))
73 elif authentication == 'SICILY_PACKAGE_DISCOVERY': # https://msdn.microsoft.com/en-us/library/cc223501.aspx
74 request['name'] = ''
75 request['authentication'] = AuthenticationChoice().setComponentByName('sicilyPackageDiscovery', SicilyPackageDiscovery(''))
76 elif authentication == 'SICILY_NEGOTIATE_NTLM': # https://msdn.microsoft.com/en-us/library/cc223501.aspx
77 request['name'] = 'NTLM'
78 request['authentication'] = AuthenticationChoice().setComponentByName('sicilyNegotiate', SicilyNegotiate(name.create_negotiate_message())) # ntlm client in self.name
79 elif authentication == 'SICILY_RESPONSE_NTLM': # https://msdn.microsoft.com/en-us/library/cc223501.aspx
80 name.parse_challenge_message(password) # server_creds returned by server in password
81 server_creds = name.create_authenticate_message()
82 if server_creds:
83 request['name'] = ''
84 request['authentication'] = AuthenticationChoice().setComponentByName('sicilyResponse', SicilyResponse(server_creds))
85 else:
86 request = None
87 else:
88 raise LDAPUnknownAuthenticationMethodError('unknown authentication method')
89
90 return request
91
92
93def bind_request_to_dict(request):
94 return {'version': int(request['version']),
95 'name': str(request['name']),
96 'authentication': authentication_choice_to_dict(request['authentication'])}
97
98
99def bind_response_operation(result_code,
100 matched_dn='',
101 diagnostic_message='',
102 referral=None,
103 server_sasl_credentials=None):
104 # BindResponse ::= [APPLICATION 1] SEQUENCE {
105 # COMPONENTS OF LDAPResult,
106 # serverSaslCreds [7] OCTET STRING OPTIONAL }
107 response = BindResponse()
108 response['resultCode'] = ResultCode(result_code)
109 response['matchedDN'] = LDAPDN(matched_dn)
110 response['diagnosticMessage'] = LDAPString(diagnostic_message)
111 if referral:
112 response['referral'] = Referral(referral)
113
114 if server_sasl_credentials:
115 response['serverSaslCreds'] = ServerSaslCreds(server_sasl_credentials)
116
117 return response
118
119
120def bind_response_to_dict(response):
121 return {'result': int(response['resultCode']),
122 'description': ResultCode().getNamedValues().getName(response['resultCode']),
123 'dn': str(response['matchedDN']),
124 'message': str(response['diagnosticMessage']),
125 'referrals': referrals_to_list(response['referral']) if response['referral'] is not None and response['referral'].hasValue() else [],
126 'saslCreds': bytes(response['serverSaslCreds']) if response['serverSaslCreds'] is not None and response['serverSaslCreds'].hasValue() else None}
127
128
129def sicily_bind_response_to_dict(response):
130 return {'result': int(response['resultCode']),
131 'description': ResultCode().getNamedValues().getName(response['resultCode']),
132 'server_creds': bytes(response['matchedDN']),
133 'error_message': str(response['diagnosticMessage'])}
134
135
136def bind_response_to_dict_fast(response):
137 response_dict = dict()
138 response_dict['result'] = int(response[0][3]) # resultCode
139 response_dict['description'] = RESULT_CODES[response_dict['result']]
140 response_dict['dn'] = to_unicode(response[1][3], from_server=True) # matchedDN
141 response_dict['message'] = to_unicode(response[2][3], from_server=True) # diagnosticMessage
142 response_dict['referrals'] = None # referrals
143 response_dict['saslCreds'] = None # saslCreds
144 for r in response[3:]:
145 if r[2] == 3: # referrals
146 response_dict['referrals'] = referrals_to_list(r[3]) # referrals
147 else:
148 response_dict['saslCreds'] = bytes(r[3]) # saslCreds
149
150 return response_dict
151
152
153def sicily_bind_response_to_dict_fast(response):
154 response_dict = dict()
155 response_dict['result'] = int(response[0][3]) # resultCode
156 response_dict['description'] = RESULT_CODES[response_dict['result']]
157 response_dict['server_creds'] = bytes(response[1][3]) # server_creds
158 response_dict['error_message'] = to_unicode(response[2][3], from_server=True) # error_message
159
160 return response_dict
161 