| 1 |
from abc import ABCMeta, abstractmethod |
| 2 |
from enum import IntEnum |
| 3 |
from typing import Union, Any, List, Tuple, Optional |
| 4 |
|
| 5 |
from bonsai._bonsai import ldapconnection, ldapsearchiter |
| 6 |
from .ldapdn import LDAPDN |
| 7 |
from .ldapentry import LDAPEntry |
| 8 |
from .errors import UnwillingToPerform, NotAllowedOnNonleaf |
| 9 |
|
| 10 |
MYPY = False |
| 11 |
|
| 12 |
if MYPY: |
| 13 |
from .ldapclient import LDAPClient |
| 14 |
|
| 15 |
|
| 16 |
class LDAPSearchScope(IntEnum): |
| 17 |
""" Enumeration for LDAP search scopes. """ |
| 18 |
|
| 19 |
BASE = 0 |
| 20 |
ONELEVEL = 1 |
| 21 |
ONE = ONELEVEL |
| 22 |
SUBTREE = 2 |
| 23 |
SUB = SUBTREE |
| 24 |
|
| 25 |
|
| 26 |
class BaseLDAPConnection(ldapconnection, metaclass=ABCMeta): |
| 27 |
def __init__(self, client: "LDAPClient", is_async: bool = False) -> None: |
| 28 |
self.__client = client |
| 29 |
super().__init__(client, is_async) |
| 30 |
|
| 31 |
def __enter__(self) -> "BaseLDAPConnection": |
| 32 |
""" Context manager entry point. """ |
| 33 |
return self |
| 34 |
|
| 35 |
def __exit__(self, type, value, traceback) -> None: |
| 36 |
""" Context manager exit point. """ |
| 37 |
self.close() |
| 38 |
|
| 39 |
def add(self, entry: LDAPEntry, timeout: Optional[float] = None) -> Any: |
| 40 |
return self._evaluate(super().add(entry), timeout) |
| 41 |
|
| 42 |
def delete( |
| 43 |
self, |
| 44 |
dname: Union[str, LDAPDN], |
| 45 |
timeout: Optional[float] = None, |
| 46 |
recursive: bool = False, |
| 47 |
) -> Any: |
| 48 |
if isinstance(dname, LDAPDN): |
| 49 |
dname = str(dname) |
| 50 |
return self._evaluate(super().delete(dname, recursive), timeout) |
| 51 |
|
| 52 |
def open(self, timeout: Optional[float] = None) -> "BaseLDAPConnection": |
| 53 |
return self._evaluate(super().open(), timeout) |
| 54 |
|
| 55 |
def modify_password( |
| 56 |
self, |
| 57 |
user: Optional[Union[str, LDAPDN]] = None, |
| 58 |
new_password: Optional[str] = None, |
| 59 |
old_password: Optional[str] = None, |
| 60 |
timeout: Optional[float] = None, |
| 61 |
) -> Any: |
| 62 |
if isinstance(user, LDAPDN): |
| 63 |
user = str(user) |
| 64 |
return self._evaluate( |
| 65 |
super().modify_password(user, new_password, old_password), timeout |
| 66 |
) |
| 67 |
|
| 68 |
def __base_search( |
| 69 |
self, |
| 70 |
base: Optional[Union[str, LDAPDN]] = None, |
| 71 |
scope: Optional[Union[LDAPSearchScope, int]] = None, |
| 72 |
filter_exp: Optional[str] = None, |
| 73 |
attrlist: Optional[List[str]] = None, |
| 74 |
timeout: Optional[float] = None, |
| 75 |
sizelimit: int = 0, |
| 76 |
attrsonly: bool = False, |
| 77 |
sort_order: Optional[List[str]] = None, |
| 78 |
page_size: int = 0, |
| 79 |
offset: int = 0, |
| 80 |
before_count: int = 0, |
| 81 |
after_count: int = 0, |
| 82 |
est_list_count: int = 0, |
| 83 |
attrvalue: Optional[str] = None, |
| 84 |
) -> Any: |
| 85 |
|
| 86 |
_base = str(base) if base is not None else str(self.__client.url.basedn) |
| 87 |
_scope = scope if scope is not None else self.__client.url.scope_num |
| 88 |
_filter = filter_exp if filter_exp is not None else self.__client.url.filter_exp |
| 89 |
_attrlist = attrlist if attrlist is not None else self.__client.url.attributes |
| 90 |
_timeout = timeout if timeout is not None else 0.0 |
| 91 |
if sort_order is not None: |
| 92 |
_sort_order = self.__create_sort_list(sort_order) |
| 93 |
else: |
| 94 |
_sort_order = [] |
| 95 |
msg_id = super().search( |
| 96 |
_base, |
| 97 |
_scope, |
| 98 |
_filter, |
| 99 |
_attrlist, |
| 100 |
_timeout, |
| 101 |
sizelimit, |
| 102 |
attrsonly, |
| 103 |
_sort_order, |
| 104 |
page_size, |
| 105 |
offset, |
| 106 |
before_count, |
| 107 |
after_count, |
| 108 |
est_list_count, |
| 109 |
attrvalue, |
| 110 |
) |
| 111 |
return self._evaluate(msg_id, timeout) |
| 112 |
|
| 113 |
@staticmethod |
| 114 |
def __create_sort_list(sort_list: List[str]) -> List[Tuple[str, bool]]: |
| 115 |
""" |
| 116 |
Set a list of attribute names to sort entries in a search result. For |
| 117 |
reverse order set '-' before to the attribute name. |
| 118 |
|
| 119 |
:param list sort_list: List of attribute names. |
| 120 |
:raises ValueError: if any element of the list is not a string or an \ |
| 121 |
empty string, and if any of the attributes is in the list more then \ |
| 122 |
once. |
| 123 |
""" |
| 124 |
sort_attrs = [] |
| 125 |
for attr in sort_list: |
| 126 |
if not isinstance(attr, str) or len(attr) == 0: |
| 127 |
raise ValueError( |
| 128 |
"All element of sort_list must be" " a non empty string." |
| 129 |
) |
| 130 |
if attr[0] == "-": |
| 131 |
|
| 132 |
sort_attrs.append((attr[1:], True)) |
| 133 |
else: |
| 134 |
sort_attrs.append((attr, False)) |
| 135 |
if len(sort_list) > len(set(map(lambda x: x[0].lower, sort_attrs))): |
| 136 |
raise ValueError("Attribute names must be different" " from each other.") |
| 137 |
return sort_attrs |
| 138 |
|
| 139 |
def search( |
| 140 |
self, |
| 141 |
base: Optional[Union[str, LDAPDN]] = None, |
| 142 |
scope: Optional[Union[LDAPSearchScope, int]] = None, |
| 143 |
filter_exp: Optional[str] = None, |
| 144 |
attrlist: Optional[List[str]] = None, |
| 145 |
timeout: Optional[float] = None, |
| 146 |
sizelimit: int = 0, |
| 147 |
attrsonly: bool = False, |
| 148 |
sort_order: Optional[List[str]] = None, |
| 149 |
) -> Any: |
| 150 |
return self.__base_search( |
| 151 |
base, scope, filter_exp, attrlist, timeout, sizelimit, attrsonly, sort_order |
| 152 |
) |
| 153 |
|
| 154 |
def paged_search( |
| 155 |
self, |
| 156 |
base: Optional[Union[str, LDAPDN]] = None, |
| 157 |
scope: Optional[Union[LDAPSearchScope, int]] = None, |
| 158 |
filter_exp: Optional[str] = None, |
| 159 |
attrlist: Optional[List[str]] = None, |
| 160 |
timeout: Optional[float] = None, |
| 161 |
sizelimit: int = 0, |
| 162 |
attrsonly: bool = False, |
| 163 |
sort_order: Optional[List[str]] = None, |
| 164 |
page_size: int = 1, |
| 165 |
) -> Any: |
| 166 |
chase_referrals = self.__client.server_chase_referrals |
| 167 |
try: |
| 168 |
self.__client.set_server_chase_referrals(False) |
| 169 |
return self.__base_search( |
| 170 |
base, |
| 171 |
scope, |
| 172 |
filter_exp, |
| 173 |
attrlist, |
| 174 |
timeout, |
| 175 |
sizelimit, |
| 176 |
attrsonly, |
| 177 |
sort_order, |
| 178 |
page_size, |
| 179 |
) |
| 180 |
finally: |
| 181 |
self.__client.set_server_chase_referrals(chase_referrals) |
| 182 |
|
| 183 |
def virtual_list_search( |
| 184 |
self, |
| 185 |
base: Optional[Union[str, LDAPDN]] = None, |
| 186 |
scope: Optional[Union[LDAPSearchScope, int]] = None, |
| 187 |
filter_exp: Optional[str] = None, |
| 188 |
attrlist: Optional[List[str]] = None, |
| 189 |
timeout: Optional[float] = None, |
| 190 |
sizelimit: int = 0, |
| 191 |
attrsonly: bool = False, |
| 192 |
sort_order: Optional[List[str]] = None, |
| 193 |
offset: int = 1, |
| 194 |
before_count: int = 0, |
| 195 |
after_count: int = 0, |
| 196 |
est_list_count: int = 0, |
| 197 |
attrvalue: Optional[str] = None, |
| 198 |
) -> Any: |
| 199 |
if sort_order is None and (offset != 0 or attrvalue is not None): |
| 200 |
raise UnwillingToPerform( |
| 201 |
"Sort control is required with" " virtual list view." |
| 202 |
) |
| 203 |
return self.__base_search( |
| 204 |
base, |
| 205 |
scope, |
| 206 |
filter_exp, |
| 207 |
attrlist, |
| 208 |
timeout, |
| 209 |
sizelimit, |
| 210 |
attrsonly, |
| 211 |
sort_order, |
| 212 |
0, |
| 213 |
offset, |
| 214 |
before_count, |
| 215 |
after_count, |
| 216 |
est_list_count, |
| 217 |
attrvalue, |
| 218 |
) |
| 219 |
|
| 220 |
def whoami(self, timeout: Optional[float] = None) -> Any: |
| 221 |
return self._evaluate(super().whoami(), timeout) |
| 222 |
|
| 223 |
@abstractmethod |
| 224 |
def _evaluate(self, msg_id: int, timeout: Optional[float] = None) -> Any: |
| 225 |
pass |
| 226 |
|
| 227 |
|
| 228 |
class LDAPConnection(BaseLDAPConnection): |
| 229 |
""" |
| 230 |
Handles synchronous connection to an LDAP server. |
| 231 |
|
| 232 |
:param LDAPClient client: a client object. |
| 233 |
""" |
| 234 |
|
| 235 |
def __init__(self, client: "LDAPClient") -> None: |
| 236 |
super().__init__(client, False) |
| 237 |
|
| 238 |
def _evaluate(self, msg_id: int, timeout: Optional[float] = None) -> Any: |
| 239 |
""" |
| 240 |
It returns the result of the LDAP operation. |
| 241 |
|
| 242 |
:param int msg_id: the ID of the LDAP operation. |
| 243 |
:param float timeout: time limit in seconds for the operation. |
| 244 |
:return: the result of the operation. |
| 245 |
""" |
| 246 |
return self.get_result(msg_id, timeout) |
| 247 |
|
| 248 |
def add(self, entry: LDAPEntry, timeout: Optional[float] = None) -> bool: |
| 249 |
""" |
| 250 |
Add new entry to the directory server. |
| 251 |
|
| 252 |
:param LDAPEntry entry: the new entry. |
| 253 |
:param float timeout: time limit in seconds for the operation. |
| 254 |
:return: True, if the operation is finished. |
| 255 |
:rtype: bool |
| 256 |
""" |
| 257 |
return super().add(entry, timeout) |
| 258 |
|
| 259 |
def delete( |
| 260 |
self, |
| 261 |
dname: Union[str, LDAPDN], |
| 262 |
timeout: Optional[float] = None, |
| 263 |
recursive: bool = False, |
| 264 |
) -> bool: |
| 265 |
""" |
| 266 |
Remove entry from the directory server. |
| 267 |
|
| 268 |
:param str|LDAPDN dname: the string or LDAPDN format of the \ |
| 269 |
entry's DN. |
| 270 |
:param float timeout: time limit in seconds for the operation. |
| 271 |
:param bool recursive: remove every entry of the given subtree \ |
| 272 |
recursively. |
| 273 |
:return: True, if the operation is finished. |
| 274 |
:rtype: bool |
| 275 |
""" |
| 276 |
try: |
| 277 |
return super().delete(dname, timeout, recursive) |
| 278 |
except NotAllowedOnNonleaf as exc: |
| 279 |
if recursive: |
| 280 |
results = self.search( |
| 281 |
dname, LDAPSearchScope.ONELEVEL, attrlist=["1.1"], timeout=timeout |
| 282 |
) |
| 283 |
for res in results: |
| 284 |
self.delete(res.dn, timeout, True) |
| 285 |
return self.delete(dname, timeout, False) |
| 286 |
else: |
| 287 |
raise exc |
| 288 |
|
| 289 |
def open(self, timeout: Optional[float] = None) -> "LDAPConnection": |
| 290 |
""" |
| 291 |
Open the LDAP connection. |
| 292 |
|
| 293 |
:param float timeout: time limit in seconds for the operation. |
| 294 |
:return: The :class:`LDAPConnection` object itself. |
| 295 |
:rtype: :class:`LDAPConnection`. |
| 296 |
""" |
| 297 |
return super().open(timeout) |
| 298 |
|
| 299 |
def search( |
| 300 |
self, |
| 301 |
base: Optional[Union[str, LDAPDN]] = None, |
| 302 |
scope: Optional[Union[LDAPSearchScope, int]] = None, |
| 303 |
filter_exp: Optional[str] = None, |
| 304 |
attrlist: Optional[List[str]] = None, |
| 305 |
timeout: Optional[float] = None, |
| 306 |
sizelimit: int = 0, |
| 307 |
attrsonly: bool = False, |
| 308 |
sort_order: Optional[List[str]] = None, |
| 309 |
) -> List[LDAPEntry]: |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
return super().search( |
| 314 |
base, scope, filter_exp, attrlist, timeout, sizelimit, attrsonly, sort_order |
| 315 |
) |
| 316 |
|
| 317 |
def paged_search( |
| 318 |
self, |
| 319 |
base: Optional[Union[str, LDAPDN]] = None, |
| 320 |
scope: Optional[Union[LDAPSearchScope, int]] = None, |
| 321 |
filter_exp: Optional[str] = None, |
| 322 |
attrlist: Optional[List[str]] = None, |
| 323 |
timeout: Optional[float] = None, |
| 324 |
sizelimit: int = 0, |
| 325 |
attrsonly: bool = False, |
| 326 |
sort_order: Optional[List[str]] = None, |
| 327 |
page_size: int = 1, |
| 328 |
) -> ldapsearchiter: |
| 329 |
return super().paged_search( |
| 330 |
base, |
| 331 |
scope, |
| 332 |
filter_exp, |
| 333 |
attrlist, |
| 334 |
timeout, |
| 335 |
sizelimit, |
| 336 |
attrsonly, |
| 337 |
sort_order, |
| 338 |
page_size, |
| 339 |
) |
| 340 |
|
| 341 |
def virtual_list_search( |
| 342 |
self, |
| 343 |
base: Optional[Union[str, LDAPDN]] = None, |
| 344 |
scope: Optional[Union[LDAPSearchScope, int]] = None, |
| 345 |
filter_exp: Optional[str] = None, |
| 346 |
attrlist: Optional[List[str]] = None, |
| 347 |
timeout: Optional[float] = None, |
| 348 |
sizelimit: int = 0, |
| 349 |
attrsonly: bool = False, |
| 350 |
sort_order: Optional[List[str]] = None, |
| 351 |
offset: int = 1, |
| 352 |
before_count: int = 0, |
| 353 |
after_count: int = 0, |
| 354 |
est_list_count: int = 0, |
| 355 |
attrvalue: Optional[str] = None, |
| 356 |
) -> Tuple[List[LDAPEntry], dict]: |
| 357 |
return super().virtual_list_search( |
| 358 |
base, |
| 359 |
scope, |
| 360 |
filter_exp, |
| 361 |
attrlist, |
| 362 |
timeout, |
| 363 |
sizelimit, |
| 364 |
attrsonly, |
| 365 |
sort_order, |
| 366 |
offset, |
| 367 |
before_count, |
| 368 |
after_count, |
| 369 |
est_list_count, |
| 370 |
attrvalue, |
| 371 |
) |
| 372 |
|
| 373 |
def modify_password( |
| 374 |
self, |
| 375 |
user: Optional[Union[str, LDAPDN]] = None, |
| 376 |
new_password: Optional[str] = None, |
| 377 |
old_password: Optional[str] = None, |
| 378 |
timeout: Optional[float] = None, |
| 379 |
) -> Optional[str]: |
| 380 |
""" |
| 381 |
Set a new password for the given user. |
| 382 |
|
| 383 |
:param str|LDAPDN user: the identification of the user. If not set, \ |
| 384 |
the owner of the current LDAP session will be associated. |
| 385 |
:param str new_password: the new password. If not set, the server \ |
| 386 |
will generate one and the new password will be returned by this method. |
| 387 |
:param str old_password: the current password of the user. |
| 388 |
:param float timeout: time limit in seconds for the operation. |
| 389 |
|
| 390 |
:return: if the `new_password` is not set, then the generated \ |
| 391 |
password, True otherwise. |
| 392 |
:rtype: str|bool |
| 393 |
""" |
| 394 |
return super().modify_password(user, new_password, old_password, timeout) |
| 395 |
|
| 396 |
def whoami(self, timeout: Optional[float] = None) -> str: |
| 397 |
""" |
| 398 |
This method can be used to obtain authorization identity. |
| 399 |
|
| 400 |
:param float timeout: time limit in seconds for the operation. |
| 401 |
|
| 402 |
:return: the authorization ID. |
| 403 |
:rtype: str |
| 404 |
""" |
| 405 |
return super().whoami(timeout) |