1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
# -*- coding: utf-8 -*-
# This software is distributed under the two-clause BSD license.
# Copyright (c) The django-ldapdb project
import django
import ldap
import ldap.controls
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.base.client import BaseDatabaseClient
from django.db.backends.base.creation import BaseDatabaseCreation
from django.db.backends.base.features import BaseDatabaseFeatures
from django.db.backends.base.introspection import BaseDatabaseIntrospection
from django.db.backends.base.operations import BaseDatabaseOperations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.backends.base.validation import BaseDatabaseValidation
class DatabaseCreation(BaseDatabaseCreation):
def create_test_db(self, *args, **kwargs):
"""
Creates a test database, prompting the user for confirmation if the
database already exists. Returns the name of the test database created.
"""
pass
def destroy_test_db(self, *args, **kwargs):
"""
Destroy a test database, prompting the user for confirmation if the
database already exists. Returns the name of the test database created.
"""
pass
class DatabaseCursor(object):
def __init__(self, ldap_connection):
self.connection = ldap_connection
def __enter__(self):
return self
def __exit__(self):
pass
def close(self):
pass
class DatabaseFeatures(BaseDatabaseFeatures):
can_use_chunked_reads = False
supports_transactions = False
supports_column_check_constraints = False
supports_table_check_constraints = False
supports_ignore_conflicts = False
uses_savepoints = False
supports_partial_indexes = False
def __init__(self, connection):
self.connection = connection
class DatabaseIntrospection(BaseDatabaseIntrospection):
def get_table_list(self, cursor):
return []
class DatabaseOperations(BaseDatabaseOperations):
compiler_module = "ldapdb.backends.ldap.compiler"
def quote_name(self, name):
return name
def no_limit_value(self):
return -1
def sql_flush(self, style, tables, *, allow_cascade=False, reset_sequences=False):
return []
if django.VERSION < (3, 1):
# Backwards compatibility
def sql_flush(self, style, tables, sequences, allow_cascade=False): # noqa
return []
class DatabaseValidation(BaseDatabaseValidation):
pass
class LdapDatabase(object):
# Base class for all exceptions
Error = ldap.LDAPError
class DatabaseError(Error):
"""Database-side errors."""
class OperationalError(
DatabaseError,
ldap.ADMINLIMIT_EXCEEDED,
ldap.AUTH_METHOD_NOT_SUPPORTED,
ldap.AUTH_UNKNOWN,
ldap.BUSY,
ldap.CONFIDENTIALITY_REQUIRED,
ldap.CONNECT_ERROR,
ldap.INAPPROPRIATE_AUTH,
ldap.INVALID_CREDENTIALS,
ldap.OPERATIONS_ERROR,
ldap.RESULTS_TOO_LARGE,
ldap.SASL_BIND_IN_PROGRESS,
ldap.SERVER_DOWN,
ldap.SIZELIMIT_EXCEEDED,
ldap.STRONG_AUTH_NOT_SUPPORTED,
ldap.STRONG_AUTH_REQUIRED,
ldap.TIMELIMIT_EXCEEDED,
ldap.TIMEOUT,
ldap.UNAVAILABLE,
ldap.UNAVAILABLE_CRITICAL_EXTENSION,
ldap.UNWILLING_TO_PERFORM,
):
"""Exceptions related to the database operations, out of the programmer control."""
class IntegrityError(
DatabaseError,
ldap.AFFECTS_MULTIPLE_DSAS,
ldap.ALREADY_EXISTS,
ldap.CONSTRAINT_VIOLATION,
ldap.TYPE_OR_VALUE_EXISTS,
):
"""Exceptions related to database Integrity."""
class DataError(
DatabaseError,
ldap.INVALID_DN_SYNTAX,
ldap.INVALID_SYNTAX,
ldap.NOT_ALLOWED_ON_NONLEAF,
ldap.NOT_ALLOWED_ON_RDN,
ldap.OBJECT_CLASS_VIOLATION,
ldap.UNDEFINED_TYPE,
):
"""Exceptions related to invalid data"""
class InterfaceError(
ldap.CLIENT_LOOP,
ldap.DECODING_ERROR,
ldap.ENCODING_ERROR,
ldap.LOCAL_ERROR,
ldap.LOOP_DETECT,
ldap.NO_MEMORY,
ldap.PROTOCOL_ERROR,
ldap.REFERRAL_LIMIT_EXCEEDED,
ldap.USER_CANCELLED,
Error,
):
"""Exceptions related to the python-ldap interface."""
class InternalError(
DatabaseError,
ldap.ALIAS_DEREF_PROBLEM,
ldap.ALIAS_PROBLEM,
):
"""Exceptions encountered within the database."""
class ProgrammingError(
DatabaseError,
ldap.CONTROL_NOT_FOUND,
ldap.FILTER_ERROR,
ldap.INAPPROPRIATE_MATCHING,
ldap.NAMING_VIOLATION,
ldap.NO_SUCH_ATTRIBUTE,
ldap.NO_SUCH_OBJECT,
ldap.PARAM_ERROR,
):
"""Invalid data send by the programmer."""
class NotSupportedError(
DatabaseError,
ldap.NOT_SUPPORTED,
):
"""Exception for unsupported actions."""
class LdapSchemaEditor(BaseDatabaseSchemaEditor):
def create_model(self, cursor):
pass
class LdapClient(BaseDatabaseClient):
executable_name = 'ldapsearch'
class DatabaseWrapper(BaseDatabaseWrapper):
vendor = 'ldap'
Database = LdapDatabase
SchemaEditorClass = LdapSchemaEditor
# Hook for sibling classes
client_class = LdapClient
creation_class = DatabaseCreation
features_class = DatabaseFeatures
introspection_class = DatabaseIntrospection
ops_class = DatabaseOperations
validation_class = DatabaseValidation
# NOTE: These are copied from the mysql DatabaseWrapper
operators = {
'exact': '= %s',
'iexact': 'LIKE %s',
'contains': 'LIKE BINARY %s',
'icontains': 'LIKE %s',
'regex': 'REGEXP BINARY %s',
'iregex': 'REGEXP %s',
'gt': '> %s',
'gte': '>= %s',
'lt': '< %s',
'lte': '<= %s',
'startswith': 'LIKE BINARY %s',
'endswith': 'LIKE BINARY %s',
'istartswith': 'LIKE %s',
'iendswith': 'LIKE %s',
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Charset used for LDAP text *values*
self.charset = "utf-8"
self.settings_dict['SUPPORTS_TRANSACTIONS'] = True
self.autocommit = True
# Default page size of 1000 items, ActiveDirectory's default
# See https://support.microsoft.com/en-us/help/315071/how-to-view-and-set-ldap-policy-in-active-directory-by-using-ntdsutil.exe # noqa
self.page_size = 1000
def close(self):
if hasattr(self, 'validate_thread_sharing'):
# django >= 1.4
self.validate_thread_sharing()
if self.connection is not None:
if hasattr(self.connection, '_l'):
self.connection.unbind_s()
self.connection = None
def get_connection_params(self):
"""Compute appropriate parameters for establishing a new connection.
Computed at system startup.
"""
return {
'uri': self.settings_dict['NAME'],
'tls': self.settings_dict.get('TLS', False),
'bind_dn': self.settings_dict['USER'],
'bind_pw': self.settings_dict['PASSWORD'],
'retry_max': self.settings_dict.get('RETRY_MAX', 1),
'retry_delay': self.settings_dict.get('RETRY_DELAY', 60.0),
'options': {
k if isinstance(k, int) else k.lower(): v
for k, v in self.settings_dict.get('CONNECTION_OPTIONS', {}).items()
},
}
def ensure_connection(self):
super().ensure_connection()
# Do a test bind, which will revive the connection if interrupted, or reconnect
conn_params = self.get_connection_params()
try:
self.connection.simple_bind_s(
conn_params['bind_dn'],
conn_params['bind_pw'],
)
except ldap.SERVER_DOWN:
self.connect()
def get_new_connection(self, conn_params):
"""Build a connection from its parameters."""
connection = ldap.ldapobject.ReconnectLDAPObject(
uri=conn_params['uri'],
retry_max=conn_params['retry_max'],
retry_delay=conn_params['retry_delay'],
bytes_mode=False)
options = conn_params['options']
for opt, value in options.items():
if opt == 'query_timeout':
connection.timeout = int(value)
elif opt == 'page_size':
self.page_size = int(value)
else:
connection.set_option(opt, value)
if conn_params['tls']:
connection.start_tls_s()
connection.simple_bind_s(
conn_params['bind_dn'],
conn_params['bind_pw'],
)
return connection
def init_connection_state(self):
"""Initialize python-side connection state."""
pass
def _commit(self):
pass
def create_cursor(self, name=None):
return DatabaseCursor(self.connection)
def _rollback(self):
pass
def _set_autocommit(self, autocommit):
pass
def add_s(self, dn, modlist):
with self.cursor() as cursor:
return cursor.connection.add_s(dn, modlist)
def delete_s(self, dn):
cursor = self._cursor()
with self.cursor() as cursor:
return cursor.connection.delete_s(dn)
def modify_s(self, dn, modlist):
with self.cursor() as cursor:
return cursor.connection.modify_s(dn, modlist)
def rename_s(self, dn, newrdn):
cursor = self._cursor()
with self.cursor() as cursor:
return cursor.connection.rename_s(dn, newrdn)
def search_s(self, base, scope, filterstr='(objectClass=*)', attrlist=None):
with self.cursor() as cursor:
query_timeout = cursor.connection.timeout
# Request pagination; don't fail if the server doesn't support it.
ldap_control = ldap.controls.SimplePagedResultsControl(
criticality=False,
size=self.page_size,
cookie='',
)
# Fetch results
page = 0
while True:
msgid = cursor.connection.search_ext(
base=base,
scope=scope,
filterstr=filterstr,
attrlist=attrlist,
serverctrls=[ldap_control],
timeout=query_timeout,
)
_res_type, results, _res_msgid, server_controls = cursor.connection.result3(
msgid,
timeout=query_timeout,
)
page_controls = [ctrl for ctrl in server_controls if ctrl.controlType == ldap.CONTROL_PAGEDRESULTS]
for dn, attrs in results:
# skip referrals
if dn is not None:
yield dn, attrs
page_control = page_controls[0]
page += 1
if page_control.cookie:
ldap_control.cookie = page_control.cookie
else:
# End of pages
break
|