| 1 | """ |
| 2 | .. module:: LDAPClient |
| 3 | :platform: Unix, Windows |
| 4 | :synopsis: For managing LDAP connections. |
| 5 | |
| 6 | """ |
| 7 | from typing import Any, Union, List, Optional, Dict, Type |
| 8 | |
| 9 | from .ldapurl import LDAPURL |
| 10 | from .ldapconnection import BaseLDAPConnection, LDAPConnection |
| 11 | from .ldapconnection import LDAPSearchScope |
| 12 | from .ldapentry import LDAPEntry |
| 13 | from .asyncio import AIOLDAPConnection |
| 14 | |
| 15 | |
| 16 | class LDAPClient: |
| 17 | """ |
| 18 | A class for configuring the connection to the directory server. |
| 19 | |
| 20 | :param str|LDAPURL url: an LDAP URL. |
| 21 | :param bool tls: Set `True` to use TLS connection. |
| 22 | :raises TypeError: if the `url` parameter is not string \ |
| 23 | or not a valid LDAP URL. |
| 24 | """ |
| 25 | |
| 26 | def __init__(self, url: Union[LDAPURL, str] = "ldap://", tls: bool = False) -> None: |
| 27 | """Init method.""" |
| 28 | self.__tls = tls |
| 29 | self.set_url(url) |
| 30 | self.__credentials: Optional[Dict[str, Optional[str]]] = None |
| 31 | self.__raw_list: List[str] = [] |
| 32 | self.__mechanism = "SIMPLE" |
| 33 | self.__cert_policy = -1 |
| 34 | self.__ca_cert: Optional[str] = "" |
| 35 | self.__ca_cert_dir: Optional[str] = "" |
| 36 | self.__client_cert: Optional[str] = "" |
| 37 | self.__client_key: Optional[str] = "" |
| 38 | self.__async_conn: Type[BaseLDAPConnection] = AIOLDAPConnection |
| 39 | self.__ppolicy_ctrl = False |
| 40 | self.__ext_dn: Optional[int] = None |
| 41 | self.__sd_flags: Optional[int] = None |
| 42 | self.__auto_acquire = True |
| 43 | self.__chase_referrals = False |
| 44 | self.__ignore_referrals = True |
| 45 | self.__managedsait_ctrl = False |
| 46 | self.__sasl_sec_props: Optional[str] = None |
| 47 | |
| 48 | def set_raw_attributes(self, raw_list: List[str]) -> None: |
| 49 | """ |
| 50 | By default the values of the LDAPEntry are in string format. The |
| 51 | values of the listed LDAP attribute's names in `raw_list` will be |
| 52 | kept in bytearray format. |
| 53 | |
| 54 | :param list raw_list: a list of LDAP attribute's names. \ |
| 55 | The elements must be string and unique. |
| 56 | |
| 57 | :raises TypeError: if any of the list's element is not a \ |
| 58 | string. |
| 59 | :raises ValueError: if the item in the lit is not a unique \ |
| 60 | element. |
| 61 | """ |
| 62 | for elem in raw_list: |
| 63 | if not isinstance(elem, str): |
| 64 | raise TypeError("All element of raw_list must be string.") |
| 65 | if len(raw_list) > len(set(map(str.lower, raw_list))): |
| 66 | raise ValueError("Attribute names must be different from each other.") |
| 67 | self.__raw_list = raw_list |
| 68 | |
| 69 | def set_credentials( |
| 70 | self, |
| 71 | mechanism: str, |
| 72 | user: Optional[str] = None, |
| 73 | password: Optional[str] = None, |
| 74 | realm: Optional[str] = None, |
| 75 | authz_id: Optional[str] = None, |
| 76 | keytab: Optional[str] = None, |
| 77 | ) -> None: |
| 78 | """ |
| 79 | Set binding mechanism and credential information. All parameters \ |
| 80 | are optional except the `mechanism`. Different mechanism applies \ |
| 81 | different credential information and ignores the rest. For example: |
| 82 | |
| 83 | * *SIMPLE* uses the `user` (as bind DN) and `password`. |
| 84 | * *EXTERNAL* only uses the `authz_id` as authorization ID. |
| 85 | |
| 86 | For other use-cases see this section about \ |
| 87 | :ref:`authentication mechanisms <auth-mechs>`. |
| 88 | |
| 89 | :param str mechanism: the name of the binding mechanism. |
| 90 | :param str user: the identification of the binding user. |
| 91 | :param str password: the password of the user. |
| 92 | :param str realm: the (Kerberos) realm of the user. |
| 93 | :param str authz_id: the authorization ID for the user. |
| 94 | :param str keytab: path to a Kerberos keytab for authentication. |
| 95 | :raises TypeError: if mechanism is not string, or any of the other \ |
| 96 | parameters are not string or None, or both of the password and \ |
| 97 | keytab parameters are set. |
| 98 | """ |
| 99 | if not isinstance(mechanism, str): |
| 100 | raise TypeError("The mechanism must be a string.") |
| 101 | mechanism = mechanism.upper() |
| 102 | creds = { |
| 103 | "user": user, |
| 104 | "password": password, |
| 105 | "realm": realm, |
| 106 | "authz_id": authz_id, |
| 107 | "keytab": keytab, |
| 108 | } |
| 109 | if list(filter(lambda x: not isinstance(x, (str, type(None))), creds.values())): |
| 110 | raise TypeError("Every parameter must be a string or None.") |
| 111 | if password is not None and keytab is not None: |
| 112 | raise TypeError( |
| 113 | "Either the password or the keytab should be set, but not both." |
| 114 | ) |
| 115 | self.__mechanism = mechanism.upper() |
| 116 | self.__credentials = creds |
| 117 | |
| 118 | def set_cert_policy(self, policy: str) -> None: |
| 119 | """ |
| 120 | Set policy about server certification. |
| 121 | |
| 122 | :param str policy: the cert policy could be one of the following \ |
| 123 | strings: |
| 124 | |
| 125 | - `try` or `demand`: the server cert will be verified, and if it \ |
| 126 | fail, then the :meth:`LDAPClient.connect` will raise an error. |
| 127 | - `never` or `allow`: the server cert will be used without any \ |
| 128 | verification. |
| 129 | |
| 130 | :raises TypeError: if the `policy` parameter is not a string. |
| 131 | :raises ValueError: if the `policy` not one of the four above. |
| 132 | |
| 133 | .. warning:: |
| 134 | Set off the cert verification is dangerous. Without verification \ |
| 135 | there is a chance of man-in-the-middle attack. |
| 136 | """ |
| 137 | tls_options = {"never": 0, "demand": 2, "allow": 3, "try": 4} |
| 138 | if not isinstance(policy, str): |
| 139 | raise TypeError("Policy parameter must be string.") |
| 140 | policy = policy.lower() |
| 141 | if policy not in tls_options.keys(): |
| 142 | raise ValueError("'%s' is an invalid policy." % policy) |
| 143 | self.__cert_policy = tls_options[policy] |
| 144 | |
| 145 | def set_ca_cert(self, name: Optional[str]) -> None: |
| 146 | """ |
| 147 | Set the name of CA certificate. If the underlying libldap library \ |
| 148 | uses the Mozilla NSS as TLS library the `name` should be the same \ |
| 149 | one in the cert/key database (that specified with \ |
| 150 | :meth:`LDAPClient.set_ca_cert_dir`), otherwise it can be the name \ |
| 151 | of the CA cert file. |
| 152 | |
| 153 | .. note:: |
| 154 | This method has no effect on MS Windows, because WinLDAP \ |
| 155 | searches for the corresponding CA certificate in the cert \ |
| 156 | store. This means that the necessary certificates have to be \ |
| 157 | installed manually in to the cert store. |
| 158 | |
| 159 | :param str name: the name of the CA cert. |
| 160 | :raises TypeError: if `name` parameter is not a string or not None. |
| 161 | """ |
| 162 | if name is not None and not isinstance(name, str): |
| 163 | raise TypeError("Name parameter must be string or None.") |
| 164 | self.__ca_cert = name |
| 165 | |
| 166 | def set_ca_cert_dir(self, path: Optional[str]) -> None: |
| 167 | """ |
| 168 | Set the directory of the CA cert. If the underlying libldap library \ |
| 169 | uses the Mozilla NSS as TLS library the `path` should be the path to \ |
| 170 | the existing cert/key database, otherwise it can be the path of the \ |
| 171 | CA cert file. |
| 172 | |
| 173 | .. note:: |
| 174 | This method has no effect on MS Windows, because WinLDAP \ |
| 175 | searches for the corresponding CA certificate in the cert \ |
| 176 | store. This means that the necessary certifications have to be \ |
| 177 | installed manually in to the cert store. |
| 178 | |
| 179 | :param str path: the path to the CA directory. |
| 180 | :raises TypeError: if `path` parameter is not a string or not None. |
| 181 | """ |
| 182 | if path is not None and not isinstance(path, str): |
| 183 | raise TypeError("Path parameter must be string or None.") |
| 184 | self.__ca_cert_dir = path |
| 185 | |
| 186 | def set_client_cert(self, name: Optional[str]) -> None: |
| 187 | """ |
| 188 | Set the name of client certificate. If the underlying libldap library \ |
| 189 | uses the Mozilla NSS as TLS library the `name` should be the same one \ |
| 190 | in the cert/key database (that specified with \ |
| 191 | :meth:`LDAPClient.set_ca_cert_dir`), otherwise it can be the name \ |
| 192 | of the client certificate file. |
| 193 | |
| 194 | .. note:: |
| 195 | This method has no effect on MS Windows, because WinLDAP \ |
| 196 | searches for the corresponding client certificate based on \ |
| 197 | the servert's CA cert in the cert store. This means that the \ |
| 198 | necessary certificates have to be installed manually in to \ |
| 199 | the cert store. |
| 200 | |
| 201 | :param str name: the name of the client cert. |
| 202 | :raises TypeError: if `name` parameter is not a string or not None. |
| 203 | """ |
| 204 | if name is not None and not isinstance(name, str): |
| 205 | raise TypeError("Name parameter must be string or None.") |
| 206 | self.__client_cert = name |
| 207 | |
| 208 | def set_client_key(self, name: Optional[str]) -> None: |
| 209 | """ |
| 210 | Set the file that contains the private key that matches the \ |
| 211 | certificate of the client that specified with \ |
| 212 | :meth:`LDAPClient.set_client_cert`). |
| 213 | |
| 214 | .. note:: |
| 215 | This method has no effect on MS Windows, because WinLDAP \ |
| 216 | searches for the corresponding client certificate based on \ |
| 217 | the servert's CA cert in the cert store. This means that the \ |
| 218 | necessary certificates have to be installed manually in to \ |
| 219 | the cert store. |
| 220 | |
| 221 | :param str name: the name of the CA cert. |
| 222 | :raises TypeError: if `name` parameter is not a string or not None. |
| 223 | """ |
| 224 | if name is not None and not isinstance(name, str): |
| 225 | raise TypeError("Name parameter must be string or None.") |
| 226 | self.__client_key = name |
| 227 | |
| 228 | def set_async_connection_class(self, conn: Type[BaseLDAPConnection]) -> None: |
| 229 | """ |
| 230 | Set the LDAP connection class for asynchronous connection. The \ |
| 231 | default connection class is :class:`bonsai.asyncio.AIOLDAPConnection` |
| 232 | that uses the asyncio event loop. |
| 233 | |
| 234 | :param BaseLDAPConnection conn: the new asynchronous connection class \ |
| 235 | that is a subclass of LDAPConnection. |
| 236 | :raises TypeError: if `conn` parameter is not a subclass \ |
| 237 | of :class:`BaseLDAPConnection`. |
| 238 | """ |
| 239 | if not issubclass(conn, BaseLDAPConnection): |
| 240 | raise TypeError("Class must be a subclass of BaseLDAPConnection. ") |
| 241 | self.__async_conn = conn |
| 242 | |
| 243 | def set_password_policy(self, ppolicy: bool) -> None: |
| 244 | """ |
| 245 | Enable password policy control, if it is provided by the directory \ |
| 246 | server. Setting it `True` will change the return value of \ |
| 247 | :meth:`LDAPClient.connect` and :meth:`LDAPConnection.open` to a \ |
| 248 | tuple of `(conn, ctrl)` where the `conn` is an \ |
| 249 | :class:`LDAPConnection`, the `ctrl` is a dict of returned password \ |
| 250 | policy control response that contains the oid, the remaining seconds \ |
| 251 | of password expiration, and the number of remaining grace logins. \ |
| 252 | If the password policy control is not available on the server or not \ |
| 253 | supported by the platform the second item in the returned tuple is \ |
| 254 | `None`, instead of a dictionary. |
| 255 | |
| 256 | By enabling the password policy control the server can send \ |
| 257 | additional error messages related to the user's account and \ |
| 258 | password during conneting to the server and changing entries. |
| 259 | |
| 260 | :param bool ppolicy: enabling/disabling password policy control. |
| 261 | :raises TypeError: If the parameter is not a bool type. |
| 262 | """ |
| 263 | if not isinstance(ppolicy, bool): |
| 264 | raise TypeError("Parameter must be bool.") |
| 265 | self.__ppolicy_ctrl = ppolicy |
| 266 | |
| 267 | def set_extended_dn(self, extdn_format: Optional[int]) -> None: |
| 268 | """ |
| 269 | Set the format of extended distinguished name for \ |
| 270 | LDAP_SERVER_EXTENDED_DN_OID control which extends the entries' |
| 271 | distingushed name with GUID and SID attributes. If the server |
| 272 | supports the control, the LDAPEntry objects' `extended_dn` attribute |
| 273 | will be set (as a string) and the `dn` attribute will be kept in |
| 274 | the simple format. |
| 275 | |
| 276 | Setting 0 specifies that the GUID and SID values be returned in \ |
| 277 | hexadecimal string format, while setting 1 will return the GUID and \ |
| 278 | SID values in standard string format. Passing `None` will remove the \ |
| 279 | control in a format of `<GUID=xxxx>;<SID=yyyy>;distinguishedName`. |
| 280 | |
| 281 | :param int extdn_format: the format of the extended dn. It can be 0, \ |
| 282 | 1 or `None`. |
| 283 | :raises TypeError: if the parameter is not int or None. |
| 284 | :raises ValueError: if the parameter is not 0, 1 or None. |
| 285 | """ |
| 286 | if extdn_format is not None and not isinstance(extdn_format, int): |
| 287 | raise TypeError("Parameter's type must be int or None.") |
| 288 | if extdn_format not in (0, 1, None): |
| 289 | raise ValueError("Parameter must be 0, 1 or None.") |
| 290 | self.__ext_dn = extdn_format |
| 291 | |
| 292 | def set_sd_flags(self, flags: Optional[int]) -> None: |
| 293 | """ |
| 294 | Set the flags for retreiving specific portion of the Windows |
| 295 | security descriptor. |
| 296 | |
| 297 | :param int|None flags: the flags for the security descriptor. |
| 298 | :raises TypeError: if the parameter is not int or None. |
| 299 | """ |
| 300 | if flags is not None and not isinstance(flags, int): |
| 301 | raise TypeError("Parameter's type must be int or None.") |
| 302 | self.__sd_flags = flags |
| 303 | |
| 304 | def set_auto_page_acquire(self, val: bool) -> None: |
| 305 | """ |
| 306 | Turn on or off the automatic page acquiring during a paged |
| 307 | LDAP search. By turning automatic page acquiring on, it is |
| 308 | unnecessary to call :meth:`ldapsearchiter.acquire_next_page`. |
| 309 | It will be implicitly called during iteration. |
| 310 | |
| 311 | :param bool val: enabling/disabling auto page acquiring. |
| 312 | :raises TypeError: If the parameter is not a bool type. |
| 313 | """ |
| 314 | if not isinstance(val, bool): |
| 315 | raise TypeError("Parameter's type must be bool.") |
| 316 | self.__auto_acquire = val |
| 317 | |
| 318 | def set_ignore_referrals(self, val: bool) -> None: |
| 319 | """ |
| 320 | Turn on or off ignoring LDAP referrals in search result. When |
| 321 | enabled (default), then possible LDAP referrals won't be |
| 322 | represented in the search result. When disabled and |
| 323 | :attr:`LDAPClient.server_chase_referrals` is also disabled, than |
| 324 | search result can contain :class:`LDAPReference` objects along with |
| 325 | :class:`LDAPEntry` objects. |
| 326 | |
| 327 | :param bool val: enabling/disabling LDAP referrals in search result. |
| 328 | :raises TypeError: If the parameter is not a bool type. |
| 329 | """ |
| 330 | if not isinstance(val, bool): |
| 331 | raise TypeError("Parameter's type must be bool.") |
| 332 | self.__ignore_referrals = val |
| 333 | |
| 334 | def set_server_chase_referrals(self, val: bool) -> None: |
| 335 | """ |
| 336 | Turn on or off chasing LDAP referrals by the server. By |
| 337 | turning off server-side referral chasing along with |
| 338 | :attr:`LDAPClient.ignore_referrals`, search result can contain |
| 339 | :class:`LDAPReference` objects mixed with :class:`LDAPEntry` |
| 340 | objects. |
| 341 | |
| 342 | :param bool val: enabling/disabling LDAP referrals chasing. |
| 343 | :raises TypeError: If the parameter is not a bool type. |
| 344 | """ |
| 345 | if not isinstance(val, bool): |
| 346 | raise TypeError("Parameter's type must be bool.") |
| 347 | self.__chase_referrals = val |
| 348 | |
| 349 | def set_managedsait(self, val: bool) -> None: |
| 350 | """ |
| 351 | Set ManageDsaIT control for LDAP operations. With ManageDsaIT an |
| 352 | LDAP referral can be searched, added and modified as a common |
| 353 | LDAP entry. |
| 354 | |
| 355 | :param bool val: enabling/disabling ManageDsaIT control. |
| 356 | :raises TypeError: If the parameter is not a bool type. |
| 357 | """ |
| 358 | if not isinstance(val, bool): |
| 359 | raise TypeError("Parameter's type must be bool.") |
| 360 | self.__managedsait_ctrl = val |
| 361 | |
| 362 | def set_url(self, url: Union[LDAPURL, str]) -> None: |
| 363 | """ |
| 364 | Set LDAP url for the client. |
| 365 | |
| 366 | :param LDAPURL|str url: the LDAP url. |
| 367 | """ |
| 368 | if isinstance(url, str): |
| 369 | self.__url = LDAPURL(url) |
| 370 | elif isinstance(url, LDAPURL): |
| 371 | self.__url = url |
| 372 | else: |
| 373 | raise TypeError("The url parameter must be string or an LDAPURL.") |
| 374 | if self.__url.scheme != "ldaps" and self.__tls: |
| 375 | self.__tls = True |
| 376 | else: |
| 377 | self.__tls = False |
| 378 | |
| 379 | def set_sasl_security_properties( |
| 380 | self, |
| 381 | no_anonymous: Optional[bool] = None, |
| 382 | no_dict: Optional[bool] = None, |
| 383 | no_plain: Optional[bool] = None, |
| 384 | forward_sec: Optional[bool] = None, |
| 385 | pass_cred: Optional[bool] = None, |
| 386 | min_ssf: Optional[int] = None, |
| 387 | max_ssf: Optional[int] = None, |
| 388 | max_bufsize: Optional[int] = None, |
| 389 | ) -> None: |
| 390 | """ |
| 391 | Set security properties for SASL mechanism. |
| 392 | |
| 393 | .. note:: |
| 394 | This method has no effect on MS Windows. |
| 395 | |
| 396 | :param bool no_anonymous: Flag for forbiding anonymous logins. |
| 397 | :param bool no_dict: Flag for making the mechanism not susceptable \ |
| 398 | to passive dictionary attack. |
| 399 | :param bool no_plain: Flag for making mechanism not susceptable to \ |
| 400 | simple passive (eavesdropping) attack. |
| 401 | :param bool forward_sec: Flag for setting forward secrecy between \ |
| 402 | sessions. |
| 403 | :param bool pass_cred: Flag for requiring mechanisms which pass \ |
| 404 | client credentials. |
| 405 | :param int min_ssf: The minimum security strength factor. |
| 406 | :param int max_ssf: The maximum security strength factor. |
| 407 | :param int max_bufsize: The maximum buffer size. |
| 408 | :raises TypeError: If the flag parameters are not bool types or \ |
| 409 | the min_ssf, max_ssf and max_bufsize parameters are not int. |
| 410 | :raises ValueError: If the min_ssf, max_ssf and max_bufsize \ |
| 411 | parameters are less than 0. |
| 412 | """ |
| 413 | sasl_sec_props = {} |
| 414 | if any( |
| 415 | not isinstance(param, bool) and param is not None |
| 416 | for param in (no_anonymous, no_dict, no_plain, forward_sec, pass_cred) |
| 417 | ): |
| 418 | raise TypeError( |
| 419 | "Any set flag parameter (no_anonymous, no_dict, no_plain," |
| 420 | " forward_sec, pass_cred) must be bool." |
| 421 | ) |
| 422 | if min_ssf is not None and not isinstance(min_ssf, int): |
| 423 | raise TypeError("The min_ssf parameter must be int.") |
| 424 | if max_ssf is not None and not isinstance(max_ssf, int): |
| 425 | raise TypeError("The max_ssf parameter must be int.") |
| 426 | if max_bufsize is not None and not isinstance(max_bufsize, int): |
| 427 | raise TypeError("The max_bufsize parameter must be int.") |
| 428 | if no_anonymous is True: |
| 429 | sasl_sec_props["noanonymous"] = True |
| 430 | if no_dict is True: |
| 431 | sasl_sec_props["nodict"] = True |
| 432 | if no_plain is True: |
| 433 | sasl_sec_props["noplain"] = True |
| 434 | if forward_sec is True: |
| 435 | sasl_sec_props["forwardsec"] = True |
| 436 | if pass_cred is True: |
| 437 | sasl_sec_props["passcred"] = True |
| 438 | if min_ssf is not None: |
| 439 | if min_ssf >= 0: |
| 440 | sasl_sec_props["minssf"] = min_ssf |
| 441 | else: |
| 442 | raise ValueError("The min_ssf parameter must be greater or equal to 0.") |
| 443 | if max_ssf is not None: |
| 444 | if max_ssf >= 0: |
| 445 | sasl_sec_props["maxssf"] = max_ssf |
| 446 | else: |
| 447 | raise ValueError("The max_ssf parameter must be greater or equal to 0.") |
| 448 | if max_bufsize is not None: |
| 449 | if max_bufsize >= 0: |
| 450 | sasl_sec_props["maxbufsize"] = max_bufsize |
| 451 | else: |
| 452 | raise ValueError( |
| 453 | "The max_bufsize parameter must be greater or equal to 0." |
| 454 | ) |
| 455 | self.__sasl_sec_props = ",".join( |
| 456 | key if val is True else "{0}={1}".format(key, val) |
| 457 | for key, val in sasl_sec_props.items() |
| 458 | ) |
| 459 | |
| 460 | @property |
| 461 | def url(self) -> LDAPURL: |
| 462 | """The URL of the directory server.""" |
| 463 | return self.__url |
| 464 | |
| 465 | @url.setter |
| 466 | def url(self, value: Union[LDAPURL, str]) -> None: |
| 467 | self.set_url(value) |
| 468 | |
| 469 | @property |
| 470 | def mechanism(self) -> str: |
| 471 | """The chosen mechanism for authentication. It cannot be set.""" |
| 472 | return self.__mechanism |
| 473 | |
| 474 | @property |
| 475 | def credentials(self) -> Optional[Dict[str, Optional[str]]]: |
| 476 | """A dict with the credential information. It cannot be set.""" |
| 477 | return self.__credentials |
| 478 | |
| 479 | @property |
| 480 | def tls(self) -> bool: |
| 481 | """A bool about TLS connection is required. It cannot be set.""" |
| 482 | return self.__tls |
| 483 | |
| 484 | @property |
| 485 | def cert_policy(self) -> int: |
| 486 | """The certification policy.""" |
| 487 | return self.__cert_policy |
| 488 | |
| 489 | @cert_policy.setter |
| 490 | def cert_policy(self, value: str) -> None: |
| 491 | self.set_cert_policy(value) |
| 492 | |
| 493 | @property |
| 494 | def ca_cert(self) -> Optional[str]: |
| 495 | """The name of the CA certificate.""" |
| 496 | return self.__ca_cert |
| 497 | |
| 498 | @ca_cert.setter |
| 499 | def ca_cert(self, value: Optional[str] = None) -> None: |
| 500 | self.set_ca_cert(value) |
| 501 | |
| 502 | @property |
| 503 | def ca_cert_dir(self) -> Optional[str]: |
| 504 | """The path to the CA certificate.""" |
| 505 | return self.__ca_cert_dir |
| 506 | |
| 507 | @ca_cert_dir.setter |
| 508 | def ca_cert_dir(self, value: Optional[str]) -> None: |
| 509 | self.set_ca_cert_dir(value) |
| 510 | |
| 511 | @property |
| 512 | def client_cert(self) -> Optional[str]: |
| 513 | """The name of the client certificate.""" |
| 514 | return self.__client_cert |
| 515 | |
| 516 | @client_cert.setter |
| 517 | def client_cert(self, value: Optional[str]) -> None: |
| 518 | self.set_client_cert(value) |
| 519 | |
| 520 | @property |
| 521 | def client_key(self) -> Optional[str]: |
| 522 | """The key file to the client's certificate.""" |
| 523 | return self.__client_key |
| 524 | |
| 525 | @client_key.setter |
| 526 | def client_key(self, value: Optional[str]) -> None: |
| 527 | self.set_client_key(value) |
| 528 | |
| 529 | @property |
| 530 | def raw_attributes(self) -> List[str]: |
| 531 | """A list of attributes that should be kept in byte format.""" |
| 532 | return self.__raw_list |
| 533 | |
| 534 | @raw_attributes.setter |
| 535 | def raw_attributes(self, value: List[str]) -> None: |
| 536 | self.set_raw_attributes(value) |
| 537 | |
| 538 | @property |
| 539 | def password_policy(self) -> bool: |
| 540 | """The status of using password policy.""" |
| 541 | return self.__ppolicy_ctrl |
| 542 | |
| 543 | @password_policy.setter |
| 544 | def password_policy(self, value: bool) -> None: |
| 545 | self.set_password_policy(value) |
| 546 | |
| 547 | @property |
| 548 | def extended_dn_format(self) -> Optional[int]: |
| 549 | """ |
| 550 | Format of the extended distinguished name. 0 means hexadecimal string |
| 551 | format, 1 standard string format. If it is `None`, then it's not set. |
| 552 | """ |
| 553 | return self.__ext_dn |
| 554 | |
| 555 | @extended_dn_format.setter |
| 556 | def extended_dn_format(self, value: Optional[int]) -> None: |
| 557 | self.set_extended_dn(value) |
| 558 | |
| 559 | @property |
| 560 | def sd_flags(self) -> Optional[int]: |
| 561 | """The flags for the Windows security descriptor.""" |
| 562 | return self.__sd_flags |
| 563 | |
| 564 | @sd_flags.setter |
| 565 | def sd_flags(self, value: Optional[int]) -> None: |
| 566 | self.set_sd_flags(value) |
| 567 | |
| 568 | @property |
| 569 | def auto_page_acquire(self) -> bool: |
| 570 | """ |
| 571 | The status of automatic page acquiring. |
| 572 | `True` by default. |
| 573 | """ |
| 574 | return self.__auto_acquire |
| 575 | |
| 576 | @auto_page_acquire.setter |
| 577 | def auto_page_acquire(self, value: bool) -> None: |
| 578 | self.set_auto_page_acquire(value) |
| 579 | |
| 580 | @property |
| 581 | def ignore_referrals(self) -> bool: |
| 582 | """ |
| 583 | The status of ignoring referrals in search results. |
| 584 | `True` by default. |
| 585 | """ |
| 586 | return self.__ignore_referrals |
| 587 | |
| 588 | @ignore_referrals.setter |
| 589 | def ignore_referrals(self, value: bool) -> None: |
| 590 | self.set_ignore_referrals(value) |
| 591 | |
| 592 | @property |
| 593 | def server_chase_referrals(self) -> bool: |
| 594 | """ |
| 595 | The status of chasing referrals by the server. `False` by default. |
| 596 | """ |
| 597 | return self.__chase_referrals |
| 598 | |
| 599 | @server_chase_referrals.setter |
| 600 | def server_chase_referrals(self, value: bool) -> None: |
| 601 | self.set_server_chase_referrals(value) |
| 602 | |
| 603 | @property |
| 604 | def managedsait(self) -> bool: |
| 605 | """The status of using ManageDsaIT control.""" |
| 606 | return self.__managedsait_ctrl |
| 607 | |
| 608 | @managedsait.setter |
| 609 | def managedsait(self, value: bool) -> None: |
| 610 | self.set_managedsait(value) |
| 611 | |
| 612 | @property |
| 613 | def sasl_security_properties(self) -> Optional[str]: |
| 614 | """The SASL security properties.""" |
| 615 | return self.__sasl_sec_props |
| 616 | |
| 617 | def get_rootDSE(self) -> Optional[LDAPEntry]: |
| 618 | """ |
| 619 | Returns the server's root DSE entry. The root DSE may contain |
| 620 | information about the vendor, the naming contexts, the request |
| 621 | controls the server supports, the supported SASL mechanisms, |
| 622 | features, schema location, and other information. |
| 623 | |
| 624 | :return: the root DSE entry. |
| 625 | :rtype: :class:`LDAPEntry` |
| 626 | """ |
| 627 | attrs = [ |
| 628 | "namingContexts", |
| 629 | "altServer", |
| 630 | "supportedExtension", |
| 631 | "supportedControl", |
| 632 | "supportedSASLMechanisms", |
| 633 | "supportedLDAPVersion", |
| 634 | ] |
| 635 | tls_options = {0: "never", 2: "demand", 3: "allow", 4: "try", -1: None} |
| 636 | this = self.__class__(self.url, self.tls) |
| 637 | cert_policy = tls_options[self.cert_policy] |
| 638 | if cert_policy is not None: |
| 639 | this.cert_policy = cert_policy |
| 640 | this.ca_cert = self.ca_cert |
| 641 | this.ca_cert_dir = self.ca_cert_dir |
| 642 | this.client_cert = self.client_cert |
| 643 | this.client_key = self.client_key |
| 644 | with LDAPConnection(this).open() as conn: |
| 645 | try: |
| 646 | root_dse = conn.search( |
| 647 | "", LDAPSearchScope.BASE, "(objectclass=*)", attrs, None, False |
| 648 | )[0] |
| 649 | return root_dse |
| 650 | except IndexError: |
| 651 | return None |
| 652 | |
| 653 | def connect( |
| 654 | self, is_async: bool = False, timeout: Optional[float] = None, **kwargs: Any |
| 655 | ) -> BaseLDAPConnection: |
| 656 | """ |
| 657 | Open a connection to the LDAP server. |
| 658 | |
| 659 | :param bool is_async: Set `True` to use asynchronous connection. |
| 660 | :param float timeout: time limit in seconds for the operation. |
| 661 | :param \\*\\*kwargs: additional keyword arguments that are passed to |
| 662 | the async connection object (e.g. an eventloop |
| 663 | object as `loop` parameter). |
| 664 | :return: an LDAP connection. |
| 665 | :rtype: :class:`LDAPConnection` |
| 666 | """ |
| 667 | if is_async: |
| 668 | return self.__async_conn(self, **kwargs).open(timeout) |
| 669 | else: |
| 670 | return LDAPConnection(self).open(timeout) |