| 1 |
API documentation |
| 2 |
***************** |
| 3 |
|
| 4 |
bonsai |
| 5 |
====== |
| 6 |
|
| 7 |
.. automodule:: bonsai |
| 8 |
:noindex: |
| 9 |
|
| 10 |
:class:`LDAPClient` |
| 11 |
------------------- |
| 12 |
.. autoclass:: LDAPClient(url, tls=False) |
| 13 |
.. automethod:: LDAPClient.connect(is_async=False, timeout=None, **kwargs) |
| 14 |
.. automethod:: LDAPClient.get_rootDSE() |
| 15 |
|
| 16 |
An example of getting the root DSE: |
| 17 |
|
| 18 |
>>> client = bonsai.LDAPClient() |
| 19 |
>>> client.get_rootDSE() |
| 20 |
{'namingContexts': ['dc=bonsai,dc=test'], 'supportedControl': ['2.16.840.1.113730.3.4.18', |
| 21 |
'2.16.840.1.113730.3.4.2', '1.3.6.1.4.1.4203.1.10.1', '1.2.840.113556.1.4.319', |
| 22 |
'1.2.826.0.1.3344810.2.3', '1.3.6.1.1.13.2', '1.3.6.1.1.13.1', '1.3.6.1.1.12'], |
| 23 |
'supportedLDAPVersion': ['3'], 'supportedExtension': ['1.3.6.1.4.1.1466.20037', |
| 24 |
'1.3.6.1.4.1.4203.1.11.1', '1.3.6.1.4.1.4203.1.11.3', '1.3.6.1.1.8'], |
| 25 |
'supportedSASLMechanisms': ['DIGEST-MD5', 'NTLM', 'CRAM-MD5']} |
| 26 |
|
| 27 |
.. automethod:: LDAPClient.set_async_connection_class(conn) |
| 28 |
|
| 29 |
An example to change the default async connection class to a Gevent-based one: |
| 30 |
|
| 31 |
>>> import bonsai |
| 32 |
>>> from bonsai.gevent import GeventLDAPConnection |
| 33 |
>>> client = bonsai.LDAPClient() |
| 34 |
>>> client.set_async_connection_class(GeventLDAPConnection) |
| 35 |
>>> client.connect(True) |
| 36 |
<bonsai.gevent.geventconnection.GeventLDAPConnection object at 0x7f9b1789c6d8> |
| 37 |
|
| 38 |
.. automethod:: LDAPClient.set_auto_page_acquire(val) |
| 39 |
.. automethod:: LDAPClient.set_ca_cert(name) |
| 40 |
.. automethod:: LDAPClient.set_ca_cert_dir(path) |
| 41 |
.. automethod:: LDAPClient.set_cert_policy(policy) |
| 42 |
.. automethod:: LDAPClient.set_client_cert(name) |
| 43 |
.. automethod:: LDAPClient.set_client_key(name) |
| 44 |
|
| 45 |
.. automethod:: LDAPClient.set_credentials(mechanism, user=None, password=None, realm=None, authz_id=None, keytab=None) |
| 46 |
|
| 47 |
>>> from bonsai import LDAPClient |
| 48 |
>>> client = LDAPClient() |
| 49 |
>>> client.set_credentials("SIMPLE", user="cn=user,dc=bonsai,dc=test", password="secret") |
| 50 |
>>> client.connect() |
| 51 |
<bonsai.LDAPConnection object at 0x7fadf8976440> |
| 52 |
|
| 53 |
.. automethod:: LDAPClient.set_extended_dn(extdn_format) |
| 54 |
|
| 55 |
An example: |
| 56 |
|
| 57 |
>>> client = bonsai.LDAPClient() |
| 58 |
>>> client.set_extended_dn(1) |
| 59 |
>>> result = conn.search("ou=nerdherd,dc=bonsai,dc=test", 1) |
| 60 |
>>> result[0].extended_dn |
| 61 |
<GUID=899e4e01-e88d-4dea-ba64-119ed386b61c>;<SID=S-1-5-21-101232111302-1767724339-724445543-12345>;cn=chuck,ou=nerdherd,dc=bonsai,dc=test |
| 62 |
>>> result[0].dn |
| 63 |
<LDAPDN cn=chuck,ou=nerdherd,dc=bonsai,dc=test> |
| 64 |
|
| 65 |
.. note:: If the extended dn control is not supported the LDAPEntry's extended_dn attribute |
| 66 |
will be None. The LDAP_SERVER_EXTENDED_DN_OID is defined as '1.2.840.113556.1.4.529'. |
| 67 |
|
| 68 |
.. automethod:: LDAPClient.set_ignore_referrals(val) |
| 69 |
|
| 70 |
.. automethod:: LDAPClient.set_managedsait(val) |
| 71 |
|
| 72 |
.. automethod:: LDAPClient.set_password_policy(ppolicy) |
| 73 |
|
| 74 |
An example: |
| 75 |
|
| 76 |
>>> import bonsai |
| 77 |
>>> client = bonsai.LDAPClient() |
| 78 |
>>> client.set_credentials("SIMPLE", "cn=user,dc=bonsai,dc=test", "secret") |
| 79 |
>>> client.set_password_policy(True) |
| 80 |
>>> conn, ctrl = client.connect() |
| 81 |
>>> conn |
| 82 |
<bonsai.ldapconnection.LDAPConnection object at 0x7fa552ab4e28> |
| 83 |
>>> ctrl |
| 84 |
{'grace': 1, 'expire': 3612, 'oid': '1.3.6.1.4.1.42.2.27.8.5.1'}) |
| 85 |
|
| 86 |
.. note:: Password policy control cannot be used on MS Windows with WinLDAP. |
| 87 |
In this case after opening a connection the control dictionary will always be |
| 88 |
`None`. |
| 89 |
|
| 90 |
.. automethod:: LDAPClient.set_raw_attributes(raw_list) |
| 91 |
|
| 92 |
An example: |
| 93 |
|
| 94 |
>>> client = bonsai.LDAPClient() |
| 95 |
>>> client.set_raw_attributes(["cn", "sn"]) |
| 96 |
>>> conn = client.connect() |
| 97 |
>>> conn.search("cn=jeff,ou=nerdherd,dc=bonsai,dc=test", 0, attrlist=['cn', 'sn', 'gn']) |
| 98 |
[{'dn': <LDAPDN cn=jeff,ou=nerdherd,dc=bonsai,dc=test>, 'sn': [b'Barnes'], 'cn': [b'jeff'], |
| 99 |
'givenName': ['Jeff']}] |
| 100 |
|
| 101 |
.. automethod:: LDAPClient.set_sasl_security_properties(no_anonymous=None, no_dict=None, no_plain=None, forward_sec=None, pass_cred=None, min_ssf=None, max_ssf=None, max_bufsize=None) |
| 102 |
.. automethod:: LDAPClient.set_sd_flags(flags) |
| 103 |
.. automethod:: LDAPClient.set_server_chase_referrals(val) |
| 104 |
.. automethod:: LDAPClient.set_url(url) |
| 105 |
|
| 106 |
.. autoattribute:: LDAPClient.auto_page_acquire |
| 107 |
.. autoattribute:: LDAPClient.ca_cert |
| 108 |
.. autoattribute:: LDAPClient.ca_cert_dir |
| 109 |
.. autoattribute:: LDAPClient.cert_policy |
| 110 |
.. autoattribute:: LDAPClient.client_cert |
| 111 |
.. autoattribute:: LDAPClient.client_key |
| 112 |
.. autoattribute:: LDAPClient.credentials |
| 113 |
.. autoattribute:: LDAPClient.extended_dn_format |
| 114 |
.. autoattribute:: LDAPClient.ignore_referrals |
| 115 |
.. autoattribute:: LDAPClient.managedsait |
| 116 |
.. autoattribute:: LDAPClient.mechanism |
| 117 |
.. autoattribute:: LDAPClient.password_policy |
| 118 |
.. autoattribute:: LDAPClient.raw_attributes |
| 119 |
.. autoattribute:: LDAPClient.sd_flags |
| 120 |
.. autoattribute:: LDAPClient.server_chase_referrals |
| 121 |
|
| 122 |
*Changed in version 1.3.0:* Default value from *True* to *False*. |
| 123 |
|
| 124 |
.. autoattribute:: LDAPClient.tls |
| 125 |
.. autoattribute:: LDAPClient.url |
| 126 |
|
| 127 |
:class:`LDAPConnection` |
| 128 |
----------------------- |
| 129 |
.. autoclass:: LDAPConnection |
| 130 |
|
| 131 |
.. method:: LDAPConnection.abandon(msg_id) |
| 132 |
|
| 133 |
Abandon an ongoing asynchronous operation associated with the given message id. |
| 134 |
Note that there is no guarantee that the LDAP server will be able to honor the request, which |
| 135 |
means the operation could be performed anyway. Nevertheless, it is a good programming paradigm |
| 136 |
to abandon unwanted operations (e.g after a timeout is exceeded). |
| 137 |
|
| 138 |
:param int msg_id: the ID of an ongoing LDAP operation. |
| 139 |
|
| 140 |
.. automethod:: LDAPConnection.add(entry, timeout=None) |
| 141 |
|
| 142 |
.. method:: LDAPConnection.close(abandon_requests=False) |
| 143 |
|
| 144 |
Close LDAP connection. |
| 145 |
|
| 146 |
When `abandon_requests` parameter is set to `true`, then abandon operation will be called on |
| 147 |
every unfinished request. |
| 148 |
|
| 149 |
.. automethod:: LDAPConnection.delete(dname, timeout=None, recursive=False) |
| 150 |
|
| 151 |
.. method:: LDAPConnection.fileno() |
| 152 |
|
| 153 |
Return the file descriptor of the underlying socket that is used for the LDAP connection. |
| 154 |
|
| 155 |
:return: The file descriptor. |
| 156 |
:rtype: int |
| 157 |
|
| 158 |
.. method:: LDAPConnection.get_result(msg_id, timeout=None) |
| 159 |
|
| 160 |
Get the result of an ongoing asynchronous operation associated with the given message id. |
| 161 |
The method blocks the caller until the given `timeout` parameter is passed or the result |
| 162 |
is arrived. If the operation is not finished until the timeout, it returns None. If the |
| 163 |
`timeout` is None, it returns immediately. |
| 164 |
|
| 165 |
:param int msg_id: the ID of an ongoing LDAP operation. |
| 166 |
:param float timeout: time limit in seconds for waiting on the result. |
| 167 |
:return: the result of the operation. |
| 168 |
:rtype: depending on the type of the operation. |
| 169 |
:raises bonsai.InvalidMessageID: if the message ID is invalid or the associated operation is |
| 170 |
already finished |
| 171 |
|
| 172 |
.. automethod:: LDAPConnection.open(timeout=None) |
| 173 |
.. automethod:: LDAPConnection.modify_password(user=None, new_password=None, old_password=None, timeout=None) |
| 174 |
.. seealso:: |
| 175 |
RFC about the LDAP Password Modify extended operation `RFC3062`_. |
| 176 |
|
| 177 |
.. _RFC3062: https://www.ietf.org/rfc/rfc3062.txt |
| 178 |
|
| 179 |
.. method:: LDAPConnection.search(base=None, scope=None, filter_exp=None, attrlist=None, timeout=None,\ |
| 180 |
sizelimit=0, attrsonly=False, sort_order=None) |
| 181 |
|
| 182 |
Perform a search on the directory server. A base DN and a search scope is always necessary to |
| 183 |
perform a search, but these values - along with the attribute's list and search filter - can |
| 184 |
also be set with the :class:`LDAPClient` LDAP URL parameter. The parameters, which are passed |
| 185 |
to the :meth:`LDAPConnection.search` method will overrule the previously set ones with the |
| 186 |
LDAP URL. |
| 187 |
|
| 188 |
Setting `sort_order` will invoke server side sorting LDAP control, based on the provided attribute list. |
| 189 |
|
| 190 |
>>> from bonsai import LDAPClient |
| 191 |
>>> client = LDAPClient("ldap://localhost") # without additional parameters |
| 192 |
>>> conn = client.connect() |
| 193 |
>>> conn.search("ou=nerdherd,dc=bonsai,dc=test", 1, "(cn=ch*)", ["cn", "sn", "gn"]) |
| 194 |
[{'dn': <LDAPDN cn=chuck,ou=nerdherd,dc=bonsai,dc=test>, 'sn': ['Bartowski'], 'cn': ['chuck'], |
| 195 |
'givenName': ['Chuck']}] |
| 196 |
>>> client = LDAPClient("ldap://localhost/ou=nerdherd,dc=bonsai,dc=test?cn,sn,gn?one?(cn=ch*)") # with additional parameters |
| 197 |
>>> conn = client.connect() |
| 198 |
>>> conn.search() |
| 199 |
[{'dn': <LDAPDN cn=chuck,ou=nerdherd,dc=bonsai,dc=test>, 'sn': ['Bartowski'], 'cn': ['chuck'], |
| 200 |
'givenName': ['Chuck']}] |
| 201 |
>>> conn.search(filter_exp="(cn=j*)") |
| 202 |
[{'dn': <LDAPDN cn=jeff,ou=nerdherd,dc=bonsai,dc=test>, 'sn': ['Barnes'], 'cn': ['jeff'], |
| 203 |
'givenName': ['Jeff']}] |
| 204 |
|
| 205 |
:param str base: the base DN of the search. |
| 206 |
:param int scope: the scope of the search. An :class:`LDAPSearchScope` also can be used as |
| 207 |
value. |
| 208 |
:param str filter_exp: string to filter the search in LDAP search filter syntax. |
| 209 |
:param list attrlist: list of attribute's names to receive only those attributes from the |
| 210 |
directory server. |
| 211 |
:param float timeout: time limit in seconds for the search. |
| 212 |
:param int sizelimit: the number of entries to limit the search. |
| 213 |
:param bool attrsonly: if it's set True, search result will contain only the name of the |
| 214 |
attributes without their values. |
| 215 |
:param list sort_order: list of attribute's names to use for server-side ordering, start name |
| 216 |
with '-' for descending order. |
| 217 |
:return: the search result. |
| 218 |
:rtype: list |
| 219 |
|
| 220 |
.. method:: LDAPConnection.paged_search(base=None, scope=None, filter_exp=None, attrlist=None,\ |
| 221 |
timeout=None, sizelimit=0, attrsonly=False,\ |
| 222 |
sort_order=None, page_size=1) |
| 223 |
|
| 224 |
Perform a search that returns a paged search result. The number of entries on a page is limited |
| 225 |
with the `page_size` parameter. The return value is an :class:`ldapsearchiter` which is an |
| 226 |
iterable object. By default, after returning the last entry on the page it automatically requests |
| 227 |
the next page from the server until the final page is delivered. This functionality can be |
| 228 |
disabled by setting the :attr:`LDAPClient.auto_page_acquire` to `false`. Then the next page |
| 229 |
can be acquired manually by calling the :meth:`ldapsearchiter.acquire_next_page` method. |
| 230 |
|
| 231 |
:param str base: the base DN of the search. |
| 232 |
:param int scope: the scope of the search. An :class:`LDAPSearchScope` also can be used as |
| 233 |
value. |
| 234 |
:param str filter_exp: string to filter the search in LDAP search filter syntax. |
| 235 |
:param list attrlist: list of attribute's names to receive only those attributes from the |
| 236 |
directory server. |
| 237 |
:param float timeout: time limit in seconds for the search. |
| 238 |
:param int sizelimit: the number of entries to limit the search. |
| 239 |
:param bool attrsonly: if it's set True, search result will contain only the name of the |
| 240 |
attributes without their values. |
| 241 |
:param list sort_order: list of attribute's names to use for server-side ordering, start name |
| 242 |
with '-' for descending order. |
| 243 |
:param int page_size: the number of entries on a page. |
| 244 |
:return: the search result. |
| 245 |
:rtype: ldapsearchiter |
| 246 |
|
| 247 |
.. method:: LDAPConnection.virtual_list_search(base=None, scope=None, filter_exp=None, attrlist=None,\ |
| 248 |
timeout=None, sizelimit=0, attrsonly=False,\ |
| 249 |
sort_order=None, offset=1, before_count=0,\ |
| 250 |
after_count=0, est_list_count=0, attrvalue=None) |
| 251 |
|
| 252 |
Perform a search using virtual list view control. To perform the search the server side sort |
| 253 |
control has to be set with `sort_order`. The result set will be shifted to the `offset` or |
| 254 |
`attrvalue` and contains the specific number of entries after and before, set with |
| 255 |
`after_count` and `before_count`. The `est_list_count` is an estimation of the entire searched |
| 256 |
list that helps to the server to position the target entry. |
| 257 |
|
| 258 |
The result of the operation is a tuple of a list and a dictionary. The dictionary contains |
| 259 |
the VLV server response: the target position and the real list size. Thi list contains the |
| 260 |
searched entries. |
| 261 |
|
| 262 |
For further details using these controls please see :ref:`ldap-controls`. |
| 263 |
|
| 264 |
:param str base: the base DN of the search. |
| 265 |
:param int scope: the scope of the search. An :class:`LDAPSearchScope` also can be used as |
| 266 |
value. |
| 267 |
:param str filter_exp: string to filter the search in LDAP search filter syntax. |
| 268 |
:param list attrlist: list of attribute's names to receive only those attributes from the |
| 269 |
directory server. |
| 270 |
:param float timeout: time limit in seconds for the search. |
| 271 |
:param int sizelimit: the number of entries to limit the search. |
| 272 |
:param bool attrsonly: if it's set True, search result will contain only the name of the |
| 273 |
attributes without their values. |
| 274 |
:param list sort_order: list of attribute's names to use for server-side ordering, start name |
| 275 |
with '-' for descending order. |
| 276 |
:param int offset: an offset of the search result to select a target entry for virtual list |
| 277 |
view (VLV). |
| 278 |
:param int before_count: the number of entries before the target entry for VLV. |
| 279 |
:param int after_count: the number of entries after the target entry for VLV. |
| 280 |
:param int est_list_count: the estimated content count of the entire list for VLV. |
| 281 |
:param attrvalue: an attribute value (of the attribute that is used for sorting) for |
| 282 |
identifying the target entry for VLV. |
| 283 |
:return: the search result. |
| 284 |
:rtype: (list, dict) |
| 285 |
|
| 286 |
.. automethod:: LDAPConnection.whoami(timeout=None) |
| 287 |
.. seealso:: |
| 288 |
RFC about the LDAP Who am I extended operation `RFC4532`_. |
| 289 |
|
| 290 |
.. _RFC4532: https://tools.ietf.org/html/rfc4532 |
| 291 |
|
| 292 |
.. attribute:: LDAPConnection.closed |
| 293 |
|
| 294 |
A readonly attribute about the connection's state. |
| 295 |
|
| 296 |
.. attribute:: LDAPConnection.is_async |
| 297 |
|
| 298 |
A readonly attribute to define that the connections is asynchronous. |
| 299 |
|
| 300 |
:class:`LDAPDN` |
| 301 |
--------------- |
| 302 |
|
| 303 |
Class for representing LDAP distinguished names. |
| 304 |
|
| 305 |
.. seealso:: |
| 306 |
|
| 307 |
RFC about **LDAP: String Representation of Distinguished Names** `RFC4514`_. |
| 308 |
|
| 309 |
.. _RFC4514: https://tools.ietf.org/html/rfc4514 |
| 310 |
|
| 311 |
Example for working with LDAPDN objects. |
| 312 |
|
| 313 |
>>> import bonsai |
| 314 |
>>> dn = bonsai.LDAPDN("cn=testuser,dc=bonsai,dc=test") |
| 315 |
>>> dn |
| 316 |
<LDAPDN cn=testuser,dc=bonsai,dc=test> |
| 317 |
>>> dn.rdns # Get RDNs in tuple format. |
| 318 |
((('cn', 'testuser'),), (('dc', 'bonsai'),), (('dc', 'test'),)) |
| 319 |
>>> str(dn) # Convert to string. |
| 320 |
'cn=testuser,dc=bonsai,dc=test' |
| 321 |
>>> dn[1] # Get the second RDN. |
| 322 |
'dc=bonsai' |
| 323 |
>>> dn[0] # Get the first RDN. |
| 324 |
'cn=testuser' |
| 325 |
>>> dn[1] = "ou=nerdherd,dc=bonsai" # Change the second RDN. |
| 326 |
>>> dn |
| 327 |
<LDAPDN cn=testuser,ou=nerdherd,dc=bonsai,dc=test> |
| 328 |
>>> other_dn = bonsai.LDAPDN("cn=testuser,ou=nerdherd,dc=bonsai,dc=test") |
| 329 |
>>> dn == other_dn |
| 330 |
True |
| 331 |
>>> dn[1:3] # Get the second and third RDN. |
| 332 |
'ou=nerdherd,dc=bonsai' |
| 333 |
>>> dn[1:3] = 'ou=buymore,dc=bonsai' # Change them. |
| 334 |
>>> dn |
| 335 |
<LDAPDN cn=testuser,ou=buymore,dc=bonsai,dc=test> |
| 336 |
|
| 337 |
.. autoclass:: LDAPDN |
| 338 |
.. automethod:: LDAPDN.__getitem__(idx) |
| 339 |
.. automethod:: LDAPDN.__setitem__(idx, value) |
| 340 |
.. automethod:: LDAPDN.__eq__(other) |
| 341 |
.. automethod:: LDAPDN.__str__ |
| 342 |
.. autoattribute:: LDAPDN.rdns |
| 343 |
|
| 344 |
:class:`LDAPEntry` |
| 345 |
------------------ |
| 346 |
.. class:: LDAPEntry(dn[, conn]) |
| 347 |
|
| 348 |
.. automethod:: LDAPEntry.change_attribute(name, optype, *values) |
| 349 |
|
| 350 |
.. note:: |
| 351 |
It is possible to create an inconsistent state that the server will reject. To |
| 352 |
get a clean state use :meth:`LDAPEntry.clear_attribute_changes`. |
| 353 |
|
| 354 |
.. automethod:: LDAPEntry.clear |
| 355 |
.. automethod:: LDAPEntry.clear_attribute_changes(name) |
| 356 |
.. automethod:: LDAPEntry.delete(timeout=None, recursive=False) |
| 357 |
.. automethod:: LDAPEntry.get(key, default=None) |
| 358 |
.. automethod:: LDAPEntry.items(exclude_dn=False) |
| 359 |
.. automethod:: LDAPEntry.keys(exclude_dn=False) |
| 360 |
|
| 361 |
.. note:: |
| 362 |
Be aware when the `exclude_dn` argument of :meth:`LDAPEntry.items`, :meth:`LDAPEntry.keys` |
| 363 |
or :meth:`LDAPEntry.values` is set to `True` you lose the benefits of `dict views`_ and |
| 364 |
get a generator object that will be sensitive of adding and removing items to the entry. |
| 365 |
|
| 366 |
.. automethod:: LDAPEntry.modify(timeout=None) |
| 367 |
.. automethod:: LDAPEntry.rename(newdn, timeout=None, delete_old_rdn=True) |
| 368 |
.. automethod:: LDAPEntry.update(*args, **kwds) |
| 369 |
.. automethod:: LDAPEntry.values(exclude_dn=False) |
| 370 |
|
| 371 |
.. attribute:: LDAPEntry.connection |
| 372 |
|
| 373 |
The LDAPConnection object of the entry. Needs to be set for any LDAP operations. |
| 374 |
|
| 375 |
.. attribute:: LDAPEntry.dn |
| 376 |
|
| 377 |
The distinguished name of the entry. |
| 378 |
|
| 379 |
>>> from bonsai import LDAPEntry |
| 380 |
>>> anna = LDAPEntry('cn=anna,ou=nerdherd,dc=bonsai,dc=test') |
| 381 |
>>> anna.dn |
| 382 |
<LDAPDN cn=anna,ou=nerdherd,dc=bonsai,dc=test> |
| 383 |
>>> str(anna.dn) |
| 384 |
'cn=anna,ou=nerdherd,dc=bonsai,dc=test' |
| 385 |
|
| 386 |
.. autoattribute:: LDAPEntry.extended_dn |
| 387 |
|
| 388 |
|
| 389 |
:class:`LDAPModOp` |
| 390 |
------------------ |
| 391 |
|
| 392 |
.. autoclass:: LDAPModOp |
| 393 |
|
| 394 |
.. autoattribute:: LDAPModOp.ADD |
| 395 |
.. autoattribute:: LDAPModOp.DELETE |
| 396 |
.. autoattribute:: LDAPModOp.REPLACE |
| 397 |
|
| 398 |
:class:`LDAPReference` |
| 399 |
---------------------- |
| 400 |
|
| 401 |
.. autoclass:: LDAPReference(client, references) |
| 402 |
|
| 403 |
.. autoattribute:: LDAPReference.client |
| 404 |
.. autoattribute:: LDAPReference.references |
| 405 |
|
| 406 |
:class:`LDAPSearchScope` |
| 407 |
------------------------ |
| 408 |
|
| 409 |
.. autoclass:: LDAPSearchScope |
| 410 |
|
| 411 |
.. autoattribute:: LDAPSearchScope.BASE |
| 412 |
.. autoattribute:: LDAPSearchScope.ONELEVEL |
| 413 |
.. autoattribute:: LDAPSearchScope.ONE |
| 414 |
.. autoattribute:: LDAPSearchScope.SUBTREE |
| 415 |
.. autoattribute:: LDAPSearchScope.SUB |
| 416 |
|
| 417 |
:class:`LDAPURL` |
| 418 |
---------------- |
| 419 |
.. seealso:: |
| 420 |
|
| 421 |
RFC about **LDAP: Uniform Resource Locator** `RFC4516`_. |
| 422 |
|
| 423 |
.. _RFC4516: http://tools.ietf.org/html/rfc4516 |
| 424 |
|
| 425 |
.. autoclass:: LDAPURL(str) |
| 426 |
|
| 427 |
An example of a valid LDAP URL with port number, base DN, list of |
| 428 |
attributes and search filter: |
| 429 |
|
| 430 |
>>> from bonsai import LDAPURL |
| 431 |
>>> url = LDAPURL("ldap://localhost:789/ou=nerdherd,dc=bonsai,dc=test?cn,sn,gn?sub?(cn=c*)") |
| 432 |
>>> url |
| 433 |
<LDAPURL ldap://localhost:789/ou=nerdherd,dc=bonsai,dc=test?cn,sn,gn?sub?(cn=c*)> |
| 434 |
>>> url.basedn |
| 435 |
<LDAPDN ou=nerdherd,dc=bonsai,dc=test> |
| 436 |
>>> url.attributes |
| 437 |
['cn', 'sn', 'gn'] |
| 438 |
|
| 439 |
.. automethod:: LDAPURL.get_address |
| 440 |
|
| 441 |
>>> import bonsai |
| 442 |
>>> url = bonsai.LDAPURL("ldaps://example.com/cn=test,dc=bonsai,dc=test??sub") |
| 443 |
>>> url |
| 444 |
<LDAPURL ldaps://example.com:636/cn=test,dc=bonsai,dc=test??sub> |
| 445 |
>>> url.get_address() |
| 446 |
'ldaps://example.com:636' |
| 447 |
|
| 448 |
.. automethod:: LDAPURL.__str__ |
| 449 |
.. autoattribute:: LDAPURL.attributes |
| 450 |
.. autoattribute:: LDAPURL.basedn |
| 451 |
.. autoattribute:: LDAPURL.host |
| 452 |
.. autoattribute:: LDAPURL.port |
| 453 |
.. autoattribute:: LDAPURL.filter_exp |
| 454 |
.. autoattribute:: LDAPURL.scope |
| 455 |
.. autoattribute:: LDAPURL.scope_num |
| 456 |
.. autoattribute:: LDAPURL.scheme |
| 457 |
|
| 458 |
:class:`LDAPValueList` |
| 459 |
---------------------- |
| 460 |
|
| 461 |
.. autoclass:: LDAPValueList(items) |
| 462 |
.. automethod:: LDAPValueList.__contains__(item) |
| 463 |
.. automethod:: LDAPValueList.__delitem__(idx) |
| 464 |
.. automethod:: LDAPValueList.__setitem__(idx, value) |
| 465 |
.. automethod:: LDAPValueList.__add__(other) |
| 466 |
.. automethod:: LDAPValueList.__iadd__(other) |
| 467 |
.. automethod:: LDAPValueList.__mul__(value) |
| 468 |
.. automethod:: LDAPValueList.append(item) |
| 469 |
.. automethod:: LDAPValueList.extend(items) |
| 470 |
.. automethod:: LDAPValueList.insert(idx, value) |
| 471 |
.. automethod:: LDAPValueList.remove(value) |
| 472 |
.. automethod:: LDAPValueList.pop(idx=-1) |
| 473 |
.. automethod:: LDAPValueList.clear |
| 474 |
.. automethod:: LDAPValueList.copy |
| 475 |
.. autoattribute:: LDAPValueList.status |
| 476 |
|
| 477 |
bonsai.active_directory |
| 478 |
======================= |
| 479 |
|
| 480 |
.. automodule:: bonsai.active_directory |
| 481 |
|
| 482 |
:class:`ACE` |
| 483 |
------------ |
| 484 |
|
| 485 |
.. autoclass:: ACE(ace_type, flags, mask, trustee_sid, object_type, inherited_object_type, application_data) |
| 486 |
|
| 487 |
.. note:: |
| 488 |
For more details, see offical Microsoft docs about `ACE`_. |
| 489 |
|
| 490 |
.. automethod:: ACE.from_binary(data) |
| 491 |
.. automethod:: ACE.to_binary() |
| 492 |
.. automethod:: ACE.__eq__(other) |
| 493 |
.. automethod:: ACE.__str__() |
| 494 |
.. automethod:: ACE.set_access_rights(rights) |
| 495 |
.. automethod:: ACE.set_application_data(data) |
| 496 |
.. automethod:: ACE.set_flags(flags) |
| 497 |
.. automethod:: ACE.set_object_type(object_type) |
| 498 |
.. automethod:: ACE.set_inherited_object_type(object_type) |
| 499 |
.. automethod:: ACE.set_type(ace_type) |
| 500 |
.. automethod:: ACE.set_trustee_sid(sid) |
| 501 |
.. autoattribute:: ACE.application_data |
| 502 |
.. autoattribute:: ACE.flags |
| 503 |
.. autoattribute:: ACE.is_inherited |
| 504 |
.. autoattribute:: ACE.inherited_object_type |
| 505 |
.. autoattribute:: ACE.mask |
| 506 |
.. autoattribute:: ACE.object_type |
| 507 |
.. autoattribute:: ACE.size |
| 508 |
.. autoattribute:: ACE.trustee_sid |
| 509 |
|
| 510 |
:class:`ACEFlag` |
| 511 |
---------------- |
| 512 |
|
| 513 |
.. autoclass:: ACEFlag |
| 514 |
:members: |
| 515 |
:undoc-members: |
| 516 |
:member-order: bysource |
| 517 |
|
| 518 |
:class:`ACERight` |
| 519 |
----------------- |
| 520 |
|
| 521 |
.. autoclass:: ACERight |
| 522 |
:members: |
| 523 |
:undoc-members: |
| 524 |
:member-order: bysource |
| 525 |
|
| 526 |
:class:`ACEType` |
| 527 |
---------------- |
| 528 |
|
| 529 |
.. autoclass:: ACEType |
| 530 |
:members: |
| 531 |
:undoc-members: |
| 532 |
:member-order: bysource |
| 533 |
|
| 534 |
:class:`ACL` |
| 535 |
------------ |
| 536 |
|
| 537 |
.. autoclass:: ACL(revision, aces) |
| 538 |
|
| 539 |
.. note:: |
| 540 |
For more details, see offical Microsoft docs about `ACL`_. |
| 541 |
|
| 542 |
.. automethod:: ACL.from_binary(data) |
| 543 |
.. automethod:: ACL.to_binary() |
| 544 |
.. automethod:: ACL.set_aces(aces) |
| 545 |
.. autoattribute:: ACL.aces |
| 546 |
.. autoattribute:: ACL.revision |
| 547 |
.. autoattribute:: ACL.size |
| 548 |
|
| 549 |
:class:`ACLRevision` |
| 550 |
-------------------- |
| 551 |
|
| 552 |
.. autoclass:: ACLRevision |
| 553 |
:members: |
| 554 |
:undoc-members: |
| 555 |
:member-order: bysource |
| 556 |
|
| 557 |
:class:`SecurityDescriptor` |
| 558 |
--------------------------- |
| 559 |
|
| 560 |
.. autoclass:: SecurityDescriptor(control, owner_sid, group_sid, sacl, dacl, revision = 1, sbz1 = 0) |
| 561 |
|
| 562 |
.. note:: |
| 563 |
For more details, see offical Microsoft docs about `SECURITY_DESCRIPTOR`_. |
| 564 |
|
| 565 |
.. automethod:: SecurityDescriptor.from_binary(data) |
| 566 |
.. automethod:: SecurityDescriptor.to_binary() |
| 567 |
.. automethod:: SecurityDescriptor.set_control(control) |
| 568 |
.. automethod:: SecurityDescriptor.set_owner_sid(owner_sid) |
| 569 |
.. automethod:: SecurityDescriptor.set_group_sid(group_sid) |
| 570 |
.. automethod:: SecurityDescriptor.set_dacl(dacl) |
| 571 |
.. automethod:: SecurityDescriptor.set_sacl(sacl) |
| 572 |
.. autoattribute:: SecurityDescriptor.control |
| 573 |
.. autoattribute:: SecurityDescriptor.dacl |
| 574 |
.. autoattribute:: SecurityDescriptor.group_sid |
| 575 |
.. autoattribute:: SecurityDescriptor.owner_sid |
| 576 |
.. autoattribute:: SecurityDescriptor.revision |
| 577 |
.. autoattribute:: SecurityDescriptor.sacl |
| 578 |
.. autoattribute:: SecurityDescriptor.sbz1 |
| 579 |
|
| 580 |
:class:`SID` |
| 581 |
------------ |
| 582 |
|
| 583 |
.. autoclass:: SID(str_rep = None, bytes_le = None) |
| 584 |
|
| 585 |
.. note:: |
| 586 |
For more details, see offical Microsoft docs about `SID`_. |
| 587 |
|
| 588 |
.. automethod:: SID.__eq__(other) |
| 589 |
.. automethod:: SID.__repr__() |
| 590 |
.. automethod:: SID.__str__() |
| 591 |
.. autoattribute:: SID.bytes_le |
| 592 |
.. autoattribute:: SID.identifier_authority |
| 593 |
.. autoattribute:: SID.revision |
| 594 |
.. autoattribute:: SID.sddl_alias |
| 595 |
.. autoattribute:: SID.size |
| 596 |
.. autoattribute:: SID.subauthorities |
| 597 |
|
| 598 |
|
| 599 |
:class:`UserAccountControl` |
| 600 |
--------------------------- |
| 601 |
|
| 602 |
.. autoclass:: UserAccountControl(flags) |
| 603 |
|
| 604 |
.. note:: |
| 605 |
For more details, see offical Microsoft docs about `UserAccountControl`_. |
| 606 |
|
| 607 |
.. autoattribute:: UserAccountControl.properties |
| 608 |
.. autoattribute:: UserAccountControl.value |
| 609 |
|
| 610 |
bonsai.asyncio |
| 611 |
============== |
| 612 |
|
| 613 |
.. automodule:: bonsai.asyncio |
| 614 |
|
| 615 |
|
| 616 |
:class:`AIOLDAPConnection` |
| 617 |
-------------------------- |
| 618 |
|
| 619 |
.. autoclass:: AIOLDAPConnection |
| 620 |
|
| 621 |
.. note:: |
| 622 |
The default asyncio event loop is changed with Python 3.8 on Windows to |
| 623 |
`ProactorEventLoop`. Unfortunately, bonsai's asyncio connection requires |
| 624 |
the old `SelectorEventLoop`. Make sure to change it back before using |
| 625 |
the module: |
| 626 |
|
| 627 |
.. code-block:: python |
| 628 |
|
| 629 |
if sys.platform == 'win32': |
| 630 |
import asyncio |
| 631 |
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) |
| 632 |
|
| 633 |
Getting ``NotImplementedError`` from the ``add_reader`` method of the |
| 634 |
event loop may indicate that it has not been properly set. |
| 635 |
|
| 636 |
:class:`AIOConnectionPool` |
| 637 |
-------------------------- |
| 638 |
|
| 639 |
.. autoclass:: AIOConnectionPool |
| 640 |
|
| 641 |
bonsai.gevent |
| 642 |
============= |
| 643 |
|
| 644 |
.. automodule:: bonsai.gevent |
| 645 |
|
| 646 |
:class:`GeventLDAPConnection` |
| 647 |
----------------------------- |
| 648 |
|
| 649 |
.. autoclass:: bonsai.gevent.GeventLDAPConnection |
| 650 |
|
| 651 |
bonsai.ldif |
| 652 |
=========== |
| 653 |
|
| 654 |
.. automodule:: bonsai |
| 655 |
|
| 656 |
:class:`LDIFReader` |
| 657 |
------------------- |
| 658 |
|
| 659 |
.. autoclass:: LDIFReader(input_file, autoload=True, max_length=76) |
| 660 |
|
| 661 |
Example of reading an LDIF file: |
| 662 |
|
| 663 |
.. code-block:: python |
| 664 |
|
| 665 |
import bonsai |
| 666 |
|
| 667 |
with open("./users.ldif") as fileobj: |
| 668 |
reader = bonsai.LDIFReader(fileobj) |
| 669 |
for entry in reader: |
| 670 |
print(entry.dn) |
| 671 |
|
| 672 |
.. autoattribute:: LDIFReader.autoload |
| 673 |
.. autoattribute:: LDIFReader.input_file |
| 674 |
.. autoattribute:: LDIFReader.resource_handlers |
| 675 |
|
| 676 |
:class:`LDIFWriter` |
| 677 |
------------------- |
| 678 |
|
| 679 |
.. autoclass:: LDIFWriter(output_file, max_length=76) |
| 680 |
.. automethod:: LDIFWriter.write_entry(entry) |
| 681 |
|
| 682 |
>>> import bonsai |
| 683 |
>>> output = open("./out.ldif", "w") |
| 684 |
>>> writer = bonsai.LDIFWriter(output) |
| 685 |
>>> entry = bonsai.LDAPEntry("cn=test") |
| 686 |
>>> entry["cn"] = "test" |
| 687 |
>>> writer.write_entry(entry) |
| 688 |
|
| 689 |
.. automethod:: LDIFWriter.write_entries(entries, write_version=True) |
| 690 |
|
| 691 |
>>> client = bonsai.LDAPClient() |
| 692 |
>>> conn = client.connect() |
| 693 |
>>> res = conn.search("ou=nerdherd,dc=bonsai,dc=test", bonsai.LDAPSearchScope.ONE) |
| 694 |
>>> output = open("./out.ldif", "w") |
| 695 |
>>> writer = bonsai.LDIFWriter(output) |
| 696 |
>>> writer.write_entries(res) |
| 697 |
|
| 698 |
.. automethod:: LDIFWriter.write_changes(entry) |
| 699 |
.. autoattribute:: LDIFWriter.output_file |
| 700 |
|
| 701 |
bonsai.pool |
| 702 |
=========== |
| 703 |
|
| 704 |
:class:`ConnectionPool` |
| 705 |
----------------------- |
| 706 |
|
| 707 |
.. autoclass:: bonsai.pool.ConnectionPool |
| 708 |
|
| 709 |
>>> import bonsai |
| 710 |
>>> from bonsai.pool import ConnectionPool |
| 711 |
>>> client = bonsai.LDAPClient() |
| 712 |
>>> pool = ConnectionPool(client, 1, 2) |
| 713 |
>>> pool.open() |
| 714 |
>>> conn = pool.get() |
| 715 |
>>> conn.whomai() |
| 716 |
'anonymus' |
| 717 |
>>> pool.put(conn) |
| 718 |
>>> pool.close() |
| 719 |
|
| 720 |
.. automethod:: bonsai.pool.ConnectionPool.close |
| 721 |
.. automethod:: bonsai.pool.ConnectionPool.get |
| 722 |
.. automethod:: bonsai.pool.ConnectionPool.open |
| 723 |
.. automethod:: bonsai.pool.ConnectionPool.put |
| 724 |
.. automethod:: bonsai.pool.ConnectionPool.spawn |
| 725 |
|
| 726 |
Example usage: |
| 727 |
|
| 728 |
.. code-block:: python |
| 729 |
|
| 730 |
import bonsai |
| 731 |
from bonsai.pool import ConnectionPool |
| 732 |
|
| 733 |
client = bonsai.LDAPClient() |
| 734 |
pool = ConnectionPool(client) |
| 735 |
with pool.spawn() as conn: |
| 736 |
print(conn.whoami()) |
| 737 |
|
| 738 |
:class:`ThreadedConnectionPool` |
| 739 |
------------------------------- |
| 740 |
|
| 741 |
.. autoclass:: bonsai.pool.ThreadedConnectionPool |
| 742 |
.. automethod:: bonsai.pool.ThreadedConnectionPool.get |
| 743 |
|
| 744 |
bonsai.tornado |
| 745 |
============== |
| 746 |
|
| 747 |
:class:`TornadoLDAPConnection` |
| 748 |
------------------------------ |
| 749 |
|
| 750 |
.. autoclass:: bonsai.tornado.TornadoLDAPConnection |
| 751 |
|
| 752 |
bonsai.trio |
| 753 |
============== |
| 754 |
|
| 755 |
:class:`TrioLDAPConnection` |
| 756 |
------------------------------ |
| 757 |
|
| 758 |
.. autoclass:: bonsai.trio.TrioLDAPConnection |
| 759 |
|
| 760 |
_bonsai |
| 761 |
======= |
| 762 |
|
| 763 |
:class:`ldapsearchiter` |
| 764 |
----------------------- |
| 765 |
|
| 766 |
Helper class for paged search result. |
| 767 |
|
| 768 |
.. method:: ldapsearchiter.acquire_next_page |
| 769 |
|
| 770 |
Request the next page of result. Returns with the message ID of the search operation. |
| 771 |
This method can only be used if the :attr:`LDAPClient.auto_page_acquire` is `False`. |
| 772 |
|
| 773 |
:return: an ID of the next search operation. |
| 774 |
:rtype: int. |
| 775 |
|
| 776 |
Errors |
| 777 |
====== |
| 778 |
.. autoclass:: bonsai.LDAPError |
| 779 |
.. autoclass:: bonsai.LDIFError |
| 780 |
.. autoclass:: bonsai.AffectsMultipleDSA |
| 781 |
.. autoclass:: bonsai.AlreadyExists |
| 782 |
.. autoclass:: bonsai.AuthenticationError |
| 783 |
.. autoclass:: bonsai.AuthMethodNotSupported |
| 784 |
.. autoclass:: bonsai.ConnectionError |
| 785 |
.. autoclass:: bonsai.ClosedConnection |
| 786 |
.. autoclass:: bonsai.InsufficientAccess |
| 787 |
.. autoclass:: bonsai.InvalidDN |
| 788 |
.. autoclass:: bonsai.InvalidMessageID |
| 789 |
.. autoclass:: bonsai.NoSuchAttribute |
| 790 |
.. autoclass:: bonsai.NoSuchObjectError |
| 791 |
.. autoclass:: bonsai.NotAllowedOnNonleaf |
| 792 |
.. autoclass:: bonsai.ObjectClassViolation |
| 793 |
.. autoclass:: bonsai.ProtocolError |
| 794 |
.. autoclass:: bonsai.SizeLimitError |
| 795 |
.. autoclass:: bonsai.TimeoutError |
| 796 |
.. autoclass:: bonsai.TypeOrValueExists |
| 797 |
.. autoclass:: bonsai.UnwillingToPerform |
| 798 |
.. autoclass:: bonsai.PasswordPolicyError() |
| 799 |
.. autoclass:: bonsai.AccountLocked() |
| 800 |
.. autoclass:: bonsai.ChangeAfterReset() |
| 801 |
.. autoclass:: bonsai.InsufficientPasswordQuality() |
| 802 |
.. autoclass:: bonsai.MustSupplyOldPassword() |
| 803 |
.. autoclass:: bonsai.PasswordExpired() |
| 804 |
.. autoclass:: bonsai.PasswordInHistory() |
| 805 |
.. autoclass:: bonsai.PasswordModNotAllowed() |
| 806 |
.. autoclass:: bonsai.PasswordTooShort() |
| 807 |
.. autoclass:: bonsai.PasswordTooYoung() |
| 808 |
.. autoclass:: bonsai.pool.PoolError |
| 809 |
.. autoclass:: bonsai.pool.ClosedPool |
| 810 |
.. autoclass:: bonsai.pool.EmptyPool |
| 811 |
|
| 812 |
Utility functions |
| 813 |
================= |
| 814 |
.. autofunction:: bonsai.utils.escape_attribute_value(attrval) |
| 815 |
|
| 816 |
>>> import bonsai |
| 817 |
>>> bonsai.escape_attribute_value(",cn=escaped") |
| 818 |
'\\,cn\\=escaped' |
| 819 |
|
| 820 |
.. autofunction:: bonsai.utils.escape_filter_exp(filter_exp) |
| 821 |
|
| 822 |
>>> import bonsai |
| 823 |
>>> filter_exp = "(&(userID={0})(objectClass=user))" |
| 824 |
>>> untrusted_input = "*)(userID=*))(|(userID=*" |
| 825 |
>>> filter_exp.format(untrusted_input) |
| 826 |
'(&(userID=*)(userID=*))(|(userID=*)(objectClass=user))' |
| 827 |
>>> filter_exp.format(bonsai.escape_filter_exp(untrusted_input)) |
| 828 |
'(&(userID=\\2A\\29\\28userID=\\2A\\29\\29\\28|\\28userID=\\2A)(objectClass=user))' |
| 829 |
|
| 830 |
.. function:: bonsai.get_tls_impl_name |
| 831 |
|
| 832 |
Return the identification of the underlying TLS implementation that is used by |
| 833 |
the LDAP library: |
| 834 |
|
| 835 |
>>> bonsai.get_tls_impl_name() |
| 836 |
"MozNSS" |
| 837 |
|
| 838 |
The possible return values are: `GnuTLS`, `OpenSSL`, `MozNSS` and `SChannel`. |
| 839 |
|
| 840 |
:return: A identification of TLS implementation. |
| 841 |
:rtype: str |
| 842 |
|
| 843 |
.. function:: bonsai.get_vendor_info |
| 844 |
|
| 845 |
Return the vendor's name and the version number of the LDAP library: |
| 846 |
|
| 847 |
>>> bonsai.get_vendor_info() |
| 848 |
("OpenLDAP", 20440) |
| 849 |
|
| 850 |
:return: A tuple of the vendor's name and the library's version. |
| 851 |
:rtype: tuple |
| 852 |
|
| 853 |
.. function:: bonsai.has_krb5_support |
| 854 |
|
| 855 |
:return: True if the module is built with the optional Kerberos/GSSAPI headers. |
| 856 |
:rtype: bool |
| 857 |
|
| 858 |
.. function:: bonsai.set_connect_async(allow) |
| 859 |
|
| 860 |
Disable/enable asynchronous connection for the underlying socket, which means |
| 861 |
that the socket is set to be non-blocking when it's enabled. The default setting |
| 862 |
is `False` on every platform. This is an OpenLDAP specific setting (see |
| 863 |
`LDAP_OPT_CONNECT_ASYNC` option in the OpenLDAP documentation for further details). |
| 864 |
|
| 865 |
:param bool allow: Enabling/disabling async connect mode. |
| 866 |
|
| 867 |
.. warning:: Experience shows that this is a delicate setting. Even with a newer OpenLDAP, |
| 868 |
the TLS library version used by libldap might be unable to handle non-blocking |
| 869 |
sockets correctly. |
| 870 |
|
| 871 |
.. function:: bonsai.set_debug(debug, level=0) |
| 872 |
|
| 873 |
Set debug mode for the module. Turning it on will provide traceback information |
| 874 |
of C function calls on the standard output. |
| 875 |
|
| 876 |
If the module uses OpenLDAP, then setting the `level` parameter to a non-zero |
| 877 |
integer will also give additional info about the libldap function calls. |
| 878 |
|
| 879 |
:param bool debug: Enabling/disabling debug mode. |
| 880 |
:param int level: The debug level (for OpenLDAP only). |
| 881 |
|
| 882 |
.. _dict views: https://docs.python.org/3/library/stdtypes.html#dict-views |
| 883 |
|
| 884 |
.. _ACE: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/d06e5a81-176e-46c6-9cf7-9137aad4455e |
| 885 |
.. _ACL: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/20233ed8-a6c6-4097-aafa-dd545ed24428 |
| 886 |
.. _SECURITY_DESCRIPTOR: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/7d4dac05-9cef-4563-a058-f108abecce1d |
| 887 |
.. _SID: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/78eb9013-1c3a-4970-ad1f-2b1dad588a25 |
| 888 |
.. _UserAccountControl: https://docs.microsoft.com/en-us/troubleshoot/windows-server/identity/useraccountcontrol-manipulate-account-properties |