slapo_py_hooks is an openldap overlay which allows you to write update hooks in Python.
Released under the MIT license.
- You must have downloaded the openldap source, extracted it, and at least run configure.
- Run
make OPENLDAP_DIR=/path/to/openldap/dir
- Copy
py_update_hook.so
somewhere where slapd will be able to read it.
- Near the other moduleload lines, add
moduleload /path/to/py_update_hook.so
- In the section for the database, add the following:
overlay py_update_hook
- use this overlay for this databasepy_filename /path/to/python/script.py
- specify the path to the file containing the update hook. Ifoverlay py_update_hook
is specified, this directive is required.py_function SomeFunctionName
- specify an alternate function name for the hook. The default isupdate
.
- Your hook function/file will have access to additional globals:
Modification
: a namedtuple type described below- Various openldap constants, including:
LDAP_MOD_ADD
,LDAP_MOD_DELETE
,LDAP_MOD_REPLACE
,SLAP_MOD_INTERNAL
, andSLAP_MOD_MANAGING
- Your hook function is called before any ACL checks. Be careful!
- Your function should be named
update
unless you overridepy_function
in slapd.conf - Your function should take a single argument, an object with the following
attributes:
dn
: a string containing the DN of the entry being modified.auth_dn
: a string containing the DN of the authenticated user.entry
: a dict{attribute_name: [value, ...]}
containing the current attributes of the entry.modifications
: a list ofModification
namedtuples, each of which contains the following:name
: a string containing the attribute namevalues
: a list of strings containing values to add/removeop
: an int indicating the type of modification; one of:LDAP_MOD_ADD
,LDAP_MOD_DELETE
, orLDAP_MOD_REPLACE
flags
: an int containing a bitmask of flags, such asSLAP_MOD_INTERNAL
which means that ACL checks should not be performed for this attribute
- You may add or remove entries from the modifications list. Any added
modification may either be a
Modification
namedtuple or a normal tuple containing(name, values, op, flags)
. - You can either return
None
which indicates that processing the request should continue (with the possibly modified list of modifications) or a tuple of(int_status, str_error_message)
which causes that error to be returned to the client. If your code raises an exception, a status ofLDAP_OTHER
is returned to the client and the exception information is logged (but not returned to the client).