2 Unix SMB/CIFS implementation.
4 Python interface to ldb.
6 Copyright (C) 2005,2006 Tim Potter <tpot@samba.org>
7 Copyright (C) 2006 Simo Sorce <idra@samba.org>
8 Copyright (C) 2007-2009 Jelmer Vernooij <jelmer@samba.org>
10 ** NOTE! The following LGPL license applies to the ldb
11 ** library. This does NOT imply that all of Samba is released
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
29 #include "ldb_private.h"
33 /* There's no Py_ssize_t in 2.4, apparently */
34 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
35 typedef int Py_ssize_t;
36 typedef inquiry lenfunc;
37 typedef intargfunc ssizeargfunc;
40 #ifndef Py_RETURN_NONE
41 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
44 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
46 if (ret == LDB_ERR_PYTHON_EXCEPTION)
47 return; /* Python exception should already be set, just keep that */
49 PyErr_SetObject(error,
50 Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
51 ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
54 static PyObject *PyExc_LdbError;
56 PyAPI_DATA(PyTypeObject) PyLdbMessage;
57 PyAPI_DATA(PyTypeObject) PyLdbModule;
58 PyAPI_DATA(PyTypeObject) PyLdbDn;
59 PyAPI_DATA(PyTypeObject) PyLdb;
60 PyAPI_DATA(PyTypeObject) PyLdbMessageElement;
61 PyAPI_DATA(PyTypeObject) PyLdbTree;
63 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
64 struct ldb_message_element *el,
67 struct ldb_val new_val;
68 TALLOC_CTX *mem_ctx = talloc_new(NULL);
73 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
81 * Obtain a ldb DN from a Python object.
83 * @param mem_ctx Memory context
84 * @param object Python object
85 * @param ldb_ctx LDB context
86 * @return Whether or not the conversion succeeded
88 bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object,
89 struct ldb_context *ldb_ctx, struct ldb_dn **dn)
93 if (ldb_ctx != NULL && PyString_Check(object)) {
94 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
99 if (PyLdbDn_Check(object)) {
100 *dn = PyLdbDn_AsDn(object);
104 PyErr_SetString(PyExc_TypeError, "Expected DN");
109 * Create a Python object from a ldb_result.
111 * @param result LDB result to convert
112 * @return Python object with converted result (a list object)
114 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
118 if (result == NULL) {
121 ret = PyList_New(result->count);
122 for (i = 0; i < result->count; i++) {
123 PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i])
130 * Create a LDB Result from a Python object.
131 * If conversion fails, NULL will be returned and a Python exception set.
133 * @param mem_ctx Memory context in which to allocate the LDB Result
134 * @param obj Python object to convert
135 * @return a ldb_result, or NULL if the conversion failed
137 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx,
140 struct ldb_result *res;
146 res = talloc_zero(mem_ctx, struct ldb_result);
147 res->count = PyList_Size(obj);
148 res->msgs = talloc_array(res, struct ldb_message *, res->count);
149 for (i = 0; i < res->count; i++) {
150 PyObject *item = PyList_GetItem(obj, i);
151 res->msgs[i] = PyLdbMessage_AsMessage(item);
156 static PyObject *py_ldb_dn_validate(PyLdbDnObject *self)
158 return PyBool_FromLong(ldb_dn_validate(self->dn));
161 static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self)
163 return PyBool_FromLong(ldb_dn_is_valid(self->dn));
166 static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self)
168 return PyBool_FromLong(ldb_dn_is_special(self->dn));
171 static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self)
173 return PyBool_FromLong(ldb_dn_is_null(self->dn));
176 static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self)
178 return PyString_FromString(ldb_dn_get_casefold(self->dn));
181 static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self)
183 return PyString_FromString(ldb_dn_get_linearized(self->dn));
186 static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self)
188 return PyString_FromString(ldb_dn_canonical_string(self->dn, self->dn));
191 static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self)
193 return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn));
196 static PyObject *py_ldb_dn_repr(PyLdbDnObject *self)
198 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn))));
201 static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args)
205 if (!PyArg_ParseTuple(args, "s", &name))
208 return ldb_dn_check_special(self->dn, name)?Py_True:Py_False;
211 static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
213 return ldb_dn_compare(dn1->dn, dn2->dn);
216 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
218 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
219 struct ldb_dn *parent;
220 PyLdbDnObject *py_ret;
221 TALLOC_CTX *mem_ctx = talloc_new(NULL);
223 parent = ldb_dn_get_parent(mem_ctx, dn);
224 if (parent == NULL) {
225 talloc_free(mem_ctx);
229 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
230 if (py_ret == NULL) {
232 talloc_free(mem_ctx);
235 py_ret->mem_ctx = mem_ctx;
237 return (PyObject *)py_ret;
240 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
242 static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
245 struct ldb_dn *dn, *other;
246 if (!PyArg_ParseTuple(args, "O", &py_other))
249 dn = PyLdbDn_AsDn((PyObject *)self);
251 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
254 return ldb_dn_add_child(dn, other)?Py_True:Py_False;
257 static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
260 struct ldb_dn *other, *dn;
261 if (!PyArg_ParseTuple(args, "O", &py_other))
264 dn = PyLdbDn_AsDn((PyObject *)self);
266 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
269 return ldb_dn_add_base(dn, other)?Py_True:Py_False;
272 static PyMethodDef py_ldb_dn_methods[] = {
273 { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS,
274 "S.validate() -> bool\n"
275 "Validate DN is correct." },
276 { "is_valid", (PyCFunction)py_ldb_dn_is_valid, METH_NOARGS,
277 "S.is_valid() -> bool\n" },
278 { "is_special", (PyCFunction)py_ldb_dn_is_special, METH_NOARGS,
279 "S.is_special() -> bool\n"
280 "Check whether this is a special LDB DN." },
281 { "is_null", (PyCFunction)py_ldb_dn_is_null, METH_NOARGS,
282 "Check whether this is a null DN." },
283 { "get_casefold", (PyCFunction)py_ldb_dn_get_casefold, METH_NOARGS,
285 { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS,
287 { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
288 "S.canonical_str() -> string\n"
289 "Canonical version of this DN (like a posix path)." },
290 { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
291 "S.canonical_ex_str() -> string\n"
292 "Canonical version of this DN (like a posix path, with terminating newline)." },
293 { "check_special", (PyCFunction)py_ldb_dn_is_special, METH_VARARGS,
295 { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS,
297 "Get the parent for this DN." },
298 { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS,
299 "S.add_child(dn) -> None\n"
300 "Add a child DN to this DN." },
301 { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
302 "S.add_base(dn) -> None\n"
303 "Add a base DN to this DN." },
304 { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
309 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
311 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
314 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
316 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self),
318 PyLdbDnObject *py_ret;
320 if (!PyObject_AsDn(NULL, py_other, NULL, &other))
323 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
324 if (py_ret == NULL) {
328 py_ret->mem_ctx = talloc_new(NULL);
329 py_ret->dn = ldb_dn_copy(py_ret->mem_ctx, dn);
330 ldb_dn_add_child(py_ret->dn, other);
331 return (PyObject *)py_ret;
334 static PySequenceMethods py_ldb_dn_seq = {
335 .sq_length = (lenfunc)py_ldb_dn_len,
336 .sq_concat = (binaryfunc)py_ldb_dn_concat,
339 static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
344 struct ldb_context *ldb_ctx;
346 PyLdbDnObject *py_ret;
347 const char * const kwnames[] = { "ldb", "dn", NULL };
349 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os",
350 discard_const_p(char *, kwnames),
354 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
356 mem_ctx = talloc_new(NULL);
357 if (mem_ctx == NULL) {
362 ret = ldb_dn_new(mem_ctx, ldb_ctx, str);
364 if (ret == NULL || !ldb_dn_validate(ret)) {
365 talloc_free(mem_ctx);
366 PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
370 py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0);
372 talloc_free(mem_ctx);
376 py_ret->mem_ctx = mem_ctx;
378 return (PyObject *)py_ret;
381 PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
383 PyLdbDnObject *py_ret;
389 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
390 if (py_ret == NULL) {
394 py_ret->mem_ctx = talloc_new(NULL);
395 py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
396 return (PyObject *)py_ret;
399 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
401 talloc_free(self->mem_ctx);
402 self->ob_type->tp_free(self);
405 PyTypeObject PyLdbDn = {
407 .tp_methods = py_ldb_dn_methods,
408 .tp_str = (reprfunc)py_ldb_dn_get_linearized,
409 .tp_repr = (reprfunc)py_ldb_dn_repr,
410 .tp_compare = (cmpfunc)py_ldb_dn_compare,
411 .tp_as_sequence = &py_ldb_dn_seq,
412 .tp_doc = "A LDB distinguished name.",
413 .tp_new = py_ldb_dn_new,
414 .tp_dealloc = (destructor)py_ldb_dn_dealloc,
415 .tp_basicsize = sizeof(PyLdbObject),
416 .tp_flags = Py_TPFLAGS_DEFAULT,
420 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
421 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
423 PyObject *fn = (PyObject *)context;
424 PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap));
427 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
431 if (!PyArg_ParseTuple(args, "O", &cb))
435 /* FIXME: Where do we DECREF cb ? */
436 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
441 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
444 if (!PyArg_ParseTuple(args, "I", &perms))
447 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
452 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
455 if (!PyArg_ParseTuple(args, "s", &modules_dir))
458 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
463 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
465 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
469 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
471 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
475 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
477 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
481 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
483 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
487 static PyObject *py_ldb_repr(PyLdbObject *self)
489 return PyString_FromFormat("<ldb connection>");
492 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
494 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
497 return PyLdbDn_FromDn(dn);
501 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
503 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
506 return PyLdbDn_FromDn(dn);
509 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
511 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
514 return PyLdbDn_FromDn(dn);
517 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
519 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
522 return PyLdbDn_FromDn(dn);
525 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list,
526 const char *paramname)
530 if (!PyList_Check(list)) {
531 PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
534 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
535 for (i = 0; i < PyList_Size(list); i++) {
536 PyObject *item = PyList_GetItem(list, i);
537 if (!PyString_Check(item)) {
538 PyErr_Format(PyExc_TypeError, "%s should be strings", paramname);
541 ret[i] = PyString_AsString(item);
547 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
549 const char * const kwnames[] = { "url", "flags", "options", NULL };
551 PyObject *py_options = Py_None;
552 const char **options;
555 struct ldb_context *ldb;
557 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__",
558 discard_const_p(char *, kwnames),
559 &url, &flags, &py_options))
562 ldb = PyLdb_AsLdbContext(self);
564 if (py_options == Py_None) {
567 options = PyList_AsStringList(ldb, py_options, "options");
573 ret = ldb_connect(ldb, url, flags, options);
574 if (ret != LDB_SUCCESS) {
575 PyErr_SetLdbError(PyExc_LdbError, ret, ldb);
580 talloc_free(options);
584 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
587 struct ldb_context *ldb;
588 ret = (PyLdbObject *)type->tp_alloc(type, 0);
593 ret->mem_ctx = talloc_new(NULL);
594 ldb = ldb_init(ret->mem_ctx, NULL);
602 return (PyObject *)ret;
605 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
609 PyObject *py_options = Py_None;
611 const char **options;
612 const char * const kwnames[] = { "url", "flags", "options", NULL };
614 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO",
615 discard_const_p(char *, kwnames),
616 &url, &flags, &py_options))
619 if (py_options == Py_None) {
622 options = PyList_AsStringList(NULL, py_options, "options");
627 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
628 talloc_free(options);
630 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
635 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
639 if (!PyArg_ParseTuple(args, "O", &py_msg))
642 if (!PyLdbMessage_Check(py_msg)) {
643 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
647 ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
648 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
653 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
657 Py_ssize_t dict_pos, msg_pos;
658 struct ldb_message_element *msgel;
659 struct ldb_message *msg;
660 PyObject *key, *value;
663 if (!PyArg_ParseTuple(args, "O", &py_msg))
666 mem_ctx = talloc_new(NULL);
668 if (PyDict_Check(py_msg)) {
669 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
670 msg = ldb_msg_new(mem_ctx);
671 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
672 msg_pos = dict_pos = 0;
674 if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
675 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
676 talloc_free(mem_ctx);
679 if (msg->dn == NULL) {
680 PyErr_SetString(PyExc_TypeError, "dn set but not found");
681 talloc_free(mem_ctx);
686 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
687 char *key_str = PyString_AsString(key);
688 if (strcmp(key_str, "dn") != 0) {
689 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
691 PyErr_SetString(PyExc_TypeError, "unable to import element");
692 talloc_free(mem_ctx);
695 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
700 if (msg->dn == NULL) {
701 PyErr_SetString(PyExc_TypeError, "no dn set");
702 talloc_free(mem_ctx);
706 msg->num_elements = msg_pos;
708 msg = PyLdbMessage_AsMessage(py_msg);
711 ret = ldb_add(PyLdb_AsLdbContext(self), msg);
712 talloc_free(mem_ctx);
713 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
718 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
723 struct ldb_context *ldb;
724 if (!PyArg_ParseTuple(args, "O", &py_dn))
727 ldb = PyLdb_AsLdbContext(self);
729 if (!PyObject_AsDn(NULL, py_dn, ldb, &dn))
732 ret = ldb_delete(ldb, dn);
733 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
738 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
740 PyObject *py_dn1, *py_dn2;
741 struct ldb_dn *dn1, *dn2;
743 struct ldb_context *ldb;
745 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
748 mem_ctx = talloc_new(NULL);
749 if (mem_ctx == NULL) {
753 ldb = PyLdb_AsLdbContext(self);
754 if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
755 talloc_free(mem_ctx);
759 if (!PyObject_AsDn(mem_ctx, py_dn2, ldb, &dn2)) {
760 talloc_free(mem_ctx);
764 ret = ldb_rename(ldb, dn1, dn2);
765 talloc_free(mem_ctx);
766 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
771 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
774 if (!PyArg_ParseTuple(args, "s", &name))
777 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
782 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
784 char *attribute, *syntax;
787 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
790 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
792 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
797 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
802 /* We don't want this attached to the 'ldb' any more */
803 return Py_BuildValue(discard_const_p(char, "(iO)"),
805 PyLdbMessage_FromMessage(ldif->msg));
810 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
813 struct ldb_ldif *ldif;
818 if (!PyArg_ParseTuple(args, "s", &s))
821 mem_ctx = talloc_new(NULL);
826 list = PyList_New(0);
827 while (s && *s != '\0') {
828 ldif = ldb_ldif_read_string(self->ldb_ctx, &s);
829 talloc_steal(mem_ctx, ldif);
831 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
833 PyErr_SetString(PyExc_ValueError, "unable to parse ldif string");
834 talloc_free(mem_ctx);
838 talloc_free(mem_ctx); /* The pyobject already has a reference to the things it needs */
839 return PyObject_GetIter(list);
842 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
844 const struct ldb_schema_attribute *a;
845 struct ldb_val old_val;
846 struct ldb_val new_val;
852 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
855 mem_ctx = talloc_new(NULL);
857 old_val.data = (uint8_t *)PyString_AsString(val);
858 old_val.length = PyString_Size(val);
860 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
866 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
867 talloc_free(mem_ctx);
871 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
873 talloc_free(mem_ctx);
878 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
880 PyObject *py_base = Py_None;
881 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
883 PyObject *py_attrs = Py_None;
884 PyObject *py_controls = Py_None;
885 const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
887 struct ldb_result *res;
888 struct ldb_request *req;
890 struct ldb_context *ldb_ctx;
891 struct ldb_control **parsed_controls;
895 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
896 discard_const_p(char *, kwnames),
897 &py_base, &scope, &expr, &py_attrs, &py_controls))
900 ldb_ctx = PyLdb_AsLdbContext(self);
902 if (py_attrs == Py_None) {
905 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
910 if (py_base == Py_None) {
911 base = ldb_get_default_basedn(ldb_ctx);
913 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
919 if (py_controls == Py_None) {
920 parsed_controls = NULL;
922 const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
923 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
924 talloc_free(controls);
927 res = talloc_zero(ldb_ctx, struct ldb_result);
934 ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
941 ldb_search_default_callback,
944 talloc_steal(req, attrs);
946 if (ret != LDB_SUCCESS) {
948 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
952 ret = ldb_request(ldb_ctx, req);
954 if (ret == LDB_SUCCESS) {
955 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
960 if (ret != LDB_SUCCESS) {
962 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
966 py_ret = PyLdbResult_FromResult(res);
973 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
978 if (!PyArg_ParseTuple(args, "s", &name))
981 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
986 /* FIXME: More interpretation */
991 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
996 if (!PyArg_ParseTuple(args, "sO", &name, &data))
999 /* FIXME: More interpretation */
1001 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
1006 static PyObject *py_ldb_modules(PyLdbObject *self)
1008 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
1009 PyObject *ret = PyList_New(0);
1010 struct ldb_module *mod;
1012 for (mod = ldb->modules; mod; mod = mod->next) {
1013 PyList_Append(ret, PyLdbModule_FromModule(mod));
1019 static PyMethodDef py_ldb_methods[] = {
1020 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
1021 "S.set_debug(callback) -> None\n"
1022 "Set callback for LDB debug messages.\n"
1023 "The callback should accept a debug level and debug text." },
1024 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
1025 "S.set_create_perms(mode) -> None\n"
1026 "Set mode to use when creating new LDB files." },
1027 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
1028 "S.set_modules_dir(path) -> None\n"
1029 "Set path LDB should search for modules" },
1030 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
1031 "S.transaction_start() -> None\n"
1032 "Start a new transaction." },
1033 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
1034 "S.transaction_commit() -> None\n"
1035 "commit a new transaction." },
1036 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
1037 "S.transaction_cancel() -> None\n"
1038 "cancel a new transaction." },
1039 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
1041 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
1043 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
1045 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
1047 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
1049 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
1050 "S.connect(url, flags=0, options=None) -> None\n"
1051 "Connect to a LDB URL." },
1052 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
1053 "S.modify(message) -> None\n"
1054 "Modify an entry." },
1055 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
1056 "S.add(message) -> None\n"
1058 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
1059 "S.delete(dn) -> None\n"
1060 "Remove an entry." },
1061 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
1062 "S.rename(old_dn, new_dn) -> None\n"
1063 "Rename an entry." },
1064 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
1065 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1066 "Search in a database.\n"
1068 ":param base: Optional base DN to search\n"
1069 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1070 ":param expression: Optional search expression\n"
1071 ":param attrs: Attributes to return (defaults to all)\n"
1072 ":param controls: Optional list of controls\n"
1073 ":return: Iterator over Message objects\n"
1075 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
1077 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
1079 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
1081 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
1082 "S.parse_ldif(ldif) -> iter(messages)\n"
1083 "Parse a string formatted using LDIF." },
1084 { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
1085 "S.get_opaque(name) -> value\n"
1086 "Get an opaque value set on this LDB connection. \n"
1087 ":note: The returned value may not be useful in Python."
1089 { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
1090 "S.set_opaque(name, value) -> None\n"
1091 "Set an opaque value on this LDB connection. \n"
1092 ":note: Passing incorrect values may cause crashes." },
1093 { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
1094 "S.modules() -> list\n"
1095 "Return the list of modules on this LDB connection " },
1099 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
1101 PyLdbModuleObject *ret;
1103 ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
1108 ret->mem_ctx = talloc_new(NULL);
1109 ret->mod = talloc_reference(ret->mem_ctx, mod);
1110 return (PyObject *)ret;
1113 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
1115 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
1118 static PyGetSetDef py_ldb_getset[] = {
1119 { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL },
1123 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
1125 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1127 struct ldb_result *result;
1131 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
1134 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
1135 if (ret != LDB_SUCCESS) {
1136 PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
1140 count = result->count;
1142 talloc_free(result);
1147 static PySequenceMethods py_ldb_seq = {
1148 .sq_contains = (objobjproc)py_ldb_contains,
1151 PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
1155 ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1160 ret->mem_ctx = talloc_new(NULL);
1161 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1162 return (PyObject *)ret;
1165 static void py_ldb_dealloc(PyLdbObject *self)
1167 talloc_free(self->mem_ctx);
1168 self->ob_type->tp_free(self);
1171 PyTypeObject PyLdb = {
1173 .tp_methods = py_ldb_methods,
1174 .tp_repr = (reprfunc)py_ldb_repr,
1175 .tp_new = py_ldb_new,
1176 .tp_init = (initproc)py_ldb_init,
1177 .tp_dealloc = (destructor)py_ldb_dealloc,
1178 .tp_getset = py_ldb_getset,
1179 .tp_getattro = PyObject_GenericGetAttr,
1180 .tp_basicsize = sizeof(PyLdbObject),
1181 .tp_doc = "Connection to a LDB database.",
1182 .tp_as_sequence = &py_ldb_seq,
1183 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1186 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
1188 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1191 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
1193 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1196 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1198 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1202 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1204 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1208 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1210 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1214 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1216 PyObject *py_base, *py_tree, *py_attrs, *py_ret;
1218 struct ldb_request *req;
1219 const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1220 struct ldb_module *mod;
1221 const char * const*attrs;
1223 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
1224 discard_const_p(char *, kwnames),
1225 &py_base, &scope, &py_tree, &py_attrs))
1230 if (py_attrs == Py_None) {
1233 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
1238 ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base),
1239 scope, NULL /* expr */, attrs,
1240 NULL /* controls */, NULL, NULL, NULL);
1242 talloc_steal(req, attrs);
1244 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1246 req->op.search.res = NULL;
1248 ret = mod->ops->search(mod, req);
1250 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1252 py_ret = PyLdbResult_FromResult(req->op.search.res);
1260 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1262 struct ldb_request *req;
1263 PyObject *py_message;
1265 struct ldb_module *mod;
1267 if (!PyArg_ParseTuple(args, "O", &py_message))
1270 req = talloc_zero(NULL, struct ldb_request);
1271 req->operation = LDB_ADD;
1272 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1274 mod = PyLdbModule_AsModule(self);
1275 ret = mod->ops->add(mod, req);
1277 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1282 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
1285 struct ldb_request *req;
1286 PyObject *py_message;
1287 struct ldb_module *mod;
1289 if (!PyArg_ParseTuple(args, "O", &py_message))
1292 req = talloc_zero(NULL, struct ldb_request);
1293 req->operation = LDB_MODIFY;
1294 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1296 mod = PyLdbModule_AsModule(self);
1297 ret = mod->ops->modify(mod, req);
1299 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1304 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
1307 struct ldb_request *req;
1310 if (!PyArg_ParseTuple(args, "O", &py_dn))
1313 req = talloc_zero(NULL, struct ldb_request);
1314 req->operation = LDB_DELETE;
1315 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1317 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1319 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1324 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1327 struct ldb_request *req;
1328 PyObject *py_dn1, *py_dn2;
1330 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1333 req = talloc_zero(NULL, struct ldb_request);
1335 req->operation = LDB_RENAME;
1336 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1337 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1339 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1341 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1346 static PyMethodDef py_ldb_module_methods[] = {
1347 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1348 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1349 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1350 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1351 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1352 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1353 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1354 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1358 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
1360 talloc_free(self->mem_ctx);
1361 self->ob_type->tp_free(self);
1364 PyTypeObject PyLdbModule = {
1365 .tp_name = "LdbModule",
1366 .tp_methods = py_ldb_module_methods,
1367 .tp_repr = (reprfunc)py_ldb_module_repr,
1368 .tp_str = (reprfunc)py_ldb_module_str,
1369 .tp_basicsize = sizeof(PyLdbModuleObject),
1370 .tp_dealloc = (destructor)py_ldb_module_dealloc,
1371 .tp_flags = Py_TPFLAGS_DEFAULT,
1376 * Create a ldb_message_element from a Python object.
1378 * This will accept any sequence objects that contains strings, or
1381 * A reference to set_obj will be borrowed.
1383 * @param mem_ctx Memory context
1384 * @param set_obj Python object to convert
1385 * @param flags ldb_message_element flags to set
1386 * @param attr_name Name of the attribute
1387 * @return New ldb_message_element, allocated as child of mem_ctx
1389 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1390 PyObject *set_obj, int flags,
1391 const char *attr_name)
1393 struct ldb_message_element *me;
1395 if (PyLdbMessageElement_Check(set_obj))
1396 return PyLdbMessageElement_AsMessageElement(set_obj);
1398 me = talloc(mem_ctx, struct ldb_message_element);
1400 me->name = attr_name;
1402 if (PyString_Check(set_obj)) {
1404 me->values = talloc_array(me, struct ldb_val, me->num_values);
1405 me->values[0].length = PyString_Size(set_obj);
1406 me->values[0].data = (uint8_t *)PyString_AsString(set_obj);
1407 } else if (PySequence_Check(set_obj)) {
1409 me->num_values = PySequence_Size(set_obj);
1410 me->values = talloc_array(me, struct ldb_val, me->num_values);
1411 for (i = 0; i < me->num_values; i++) {
1412 PyObject *obj = PySequence_GetItem(set_obj, i);
1414 me->values[i].length = PyString_Size(obj);
1415 me->values[i].data = (uint8_t *)PyString_AsString(obj);
1426 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
1427 struct ldb_message_element *me)
1432 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1433 result = PyList_New(me->num_values);
1435 for (i = 0; i < me->num_values; i++) {
1436 PyList_SetItem(result, i,
1437 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1443 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1446 if (!PyArg_ParseTuple(args, "i", &i))
1448 if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1451 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1452 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1455 static PyMethodDef py_ldb_msg_element_methods[] = {
1456 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1460 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1462 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1465 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1467 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1468 if (idx < 0 || idx >= el->num_values) {
1469 PyErr_SetString(PyExc_IndexError, "Out of range");
1472 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1475 static PySequenceMethods py_ldb_msg_element_seq = {
1476 .sq_length = (lenfunc)py_ldb_msg_element_len,
1477 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1480 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1482 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1483 PyLdbMessageElement_AsMessageElement(other));
1486 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1488 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1491 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
1493 PyLdbMessageElementObject *ret;
1494 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1499 ret->mem_ctx = talloc_new(NULL);
1500 if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1505 return (PyObject *)ret;
1508 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1510 PyObject *py_elements = NULL;
1511 struct ldb_message_element *el;
1514 const char * const kwnames[] = { "elements", "flags", "name", NULL };
1515 PyLdbMessageElementObject *ret;
1516 TALLOC_CTX *mem_ctx;
1518 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois",
1519 discard_const_p(char *, kwnames),
1520 &py_elements, &flags, &name))
1523 mem_ctx = talloc_new(NULL);
1524 if (mem_ctx == NULL) {
1529 el = talloc_zero(mem_ctx, struct ldb_message_element);
1531 if (py_elements != NULL) {
1533 if (PyString_Check(py_elements)) {
1535 el->values = talloc_array(el, struct ldb_val, 1);
1536 el->values[0].data = (uint8_t *)PyString_AsString(py_elements);
1537 el->values[0].length = PyString_Size(py_elements);
1538 } else if (PySequence_Check(py_elements)) {
1539 el->num_values = PySequence_Size(py_elements);
1540 el->values = talloc_array(el, struct ldb_val, el->num_values);
1541 for (i = 0; i < el->num_values; i++) {
1542 PyObject *item = PySequence_GetItem(py_elements, i);
1543 el->values[i].data = (uint8_t *)PyString_AsString(item);
1544 el->values[i].length = PyString_Size(item);
1547 PyErr_SetString(PyExc_TypeError,
1548 "Expected string or list");
1549 talloc_free(mem_ctx);
1555 el->name = talloc_strdup(el, name);
1557 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1560 talloc_free(mem_ctx);
1564 ret->mem_ctx = mem_ctx;
1566 return (PyObject *)ret;
1569 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1571 char *element_str = NULL;
1573 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1576 for (i = 0; i < el->num_values; i++) {
1577 PyObject *o = py_ldb_msg_element_find(self, i);
1578 if (element_str == NULL)
1579 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1581 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1584 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1586 talloc_free(element_str);
1591 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
1593 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1595 if (el->num_values == 1)
1596 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1601 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
1603 talloc_free(self->mem_ctx);
1604 self->ob_type->tp_free(self);
1607 PyTypeObject PyLdbMessageElement = {
1608 .tp_name = "MessageElement",
1609 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1610 .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1611 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1612 .tp_str = (reprfunc)py_ldb_msg_element_str,
1613 .tp_methods = py_ldb_msg_element_methods,
1614 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1615 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1616 .tp_as_sequence = &py_ldb_msg_element_seq,
1617 .tp_new = py_ldb_msg_element_new,
1618 .tp_flags = Py_TPFLAGS_DEFAULT,
1621 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
1624 if (!PyArg_ParseTuple(args, "s", &name))
1627 ldb_msg_remove_attr(self->msg, name);
1632 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
1634 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1636 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1637 if (msg->dn != NULL) {
1638 PyList_SetItem(obj, j, PyString_FromString("dn"));
1641 for (i = 0; i < msg->num_elements; i++) {
1642 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1648 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
1650 struct ldb_message_element *el;
1651 char *name = PyString_AsString(py_name);
1652 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1653 if (!strcmp(name, "dn"))
1654 return PyLdbDn_FromDn(msg->dn);
1655 el = ldb_msg_find_element(msg, name);
1659 return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg);
1662 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
1664 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
1666 PyErr_SetString(PyExc_KeyError, "No such element");
1672 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
1674 PyObject *name, *ret;
1675 if (!PyArg_ParseTuple(args, "O", &name))
1678 ret = py_ldb_msg_getitem_helper(self, name);
1684 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
1686 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1688 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
1690 if (msg->dn != NULL) {
1691 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
1694 for (i = 0; i < msg->num_elements; i++, j++) {
1695 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], self->msg)));
1700 static PyMethodDef py_ldb_msg_methods[] = {
1701 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
1702 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
1703 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
1704 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
1708 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
1710 PyObject *list, *iter;
1712 list = py_ldb_msg_keys(self);
1713 iter = PyObject_GetIter(list);
1718 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
1720 char *attr_name = PyString_AsString(name);
1721 if (value == NULL) {
1722 ldb_msg_remove_attr(self->msg, attr_name);
1724 struct ldb_message_element *el = PyObject_AsMessageElement(NULL,
1725 value, 0, attr_name);
1728 talloc_steal(self->msg, el);
1729 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
1730 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
1735 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
1737 return PyLdbMessage_AsMessage(self)->num_elements;
1740 static PyMappingMethods py_ldb_msg_mapping = {
1741 .mp_length = (lenfunc)py_ldb_msg_length,
1742 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
1743 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
1746 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1748 const char * const kwnames[] = { "dn", NULL };
1749 struct ldb_message *ret;
1750 TALLOC_CTX *mem_ctx;
1751 PyObject *pydn = NULL;
1752 PyLdbMessageObject *py_ret;
1754 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
1755 discard_const_p(char *, kwnames),
1759 mem_ctx = talloc_new(NULL);
1760 if (mem_ctx == NULL) {
1765 ret = ldb_msg_new(mem_ctx);
1767 talloc_free(mem_ctx);
1774 if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) {
1775 talloc_free(mem_ctx);
1778 ret->dn = talloc_reference(ret, dn);
1781 py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
1782 if (py_ret == NULL) {
1784 talloc_free(mem_ctx);
1788 py_ret->mem_ctx = mem_ctx;
1790 return (PyObject *)py_ret;
1793 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
1795 PyLdbMessageObject *ret;
1797 ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
1802 ret->mem_ctx = talloc_new(NULL);
1803 ret->msg = talloc_reference(ret->mem_ctx, msg);
1804 return (PyObject *)ret;
1807 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
1809 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1810 return PyLdbDn_FromDn(msg->dn);
1813 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
1815 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1816 if (!PyLdbDn_Check(value)) {
1817 PyErr_SetNone(PyExc_TypeError);
1821 msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value));
1825 static PyGetSetDef py_ldb_msg_getset[] = {
1826 { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
1830 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
1832 PyObject *dict = PyDict_New(), *ret;
1833 if (PyDict_Update(dict, (PyObject *)self) != 0)
1835 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
1840 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
1842 talloc_free(self->mem_ctx);
1843 self->ob_type->tp_free(self);
1846 PyTypeObject PyLdbMessage = {
1847 .tp_name = "Message",
1848 .tp_methods = py_ldb_msg_methods,
1849 .tp_getset = py_ldb_msg_getset,
1850 .tp_as_mapping = &py_ldb_msg_mapping,
1851 .tp_basicsize = sizeof(PyLdbMessageObject),
1852 .tp_dealloc = (destructor)py_ldb_msg_dealloc,
1853 .tp_new = py_ldb_msg_new,
1854 .tp_repr = (reprfunc)py_ldb_msg_repr,
1855 .tp_flags = Py_TPFLAGS_DEFAULT,
1856 .tp_iter = (getiterfunc)py_ldb_msg_iter,
1859 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
1861 PyLdbTreeObject *ret;
1863 ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
1869 ret->mem_ctx = talloc_new(NULL);
1870 ret->tree = talloc_reference(ret->mem_ctx, tree);
1871 return (PyObject *)ret;
1874 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
1876 talloc_free(self->mem_ctx);
1877 self->ob_type->tp_free(self);
1880 PyTypeObject PyLdbTree = {
1882 .tp_basicsize = sizeof(PyLdbTreeObject),
1883 .tp_dealloc = (destructor)py_ldb_tree_dealloc,
1884 .tp_flags = Py_TPFLAGS_DEFAULT,
1888 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
1890 PyObject *py_ldb = (PyObject *)mod->private_data;
1891 PyObject *py_result, *py_base, *py_attrs, *py_tree;
1893 py_base = PyLdbDn_FromDn(req->op.search.base);
1895 if (py_base == NULL)
1896 return LDB_ERR_OPERATIONS_ERROR;
1898 py_tree = PyLdbTree_FromTree(req->op.search.tree);
1900 if (py_tree == NULL)
1901 return LDB_ERR_OPERATIONS_ERROR;
1903 if (req->op.search.attrs == NULL) {
1907 for (len = 0; req->op.search.attrs[len]; len++);
1908 py_attrs = PyList_New(len);
1909 for (i = 0; i < len; i++)
1910 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1913 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
1914 discard_const_p(char, "OiOO"),
1915 py_base, req->op.search.scope, py_tree, py_attrs);
1917 Py_DECREF(py_attrs);
1921 if (py_result == NULL) {
1922 return LDB_ERR_PYTHON_EXCEPTION;
1925 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
1926 if (req->op.search.res == NULL) {
1927 return LDB_ERR_PYTHON_EXCEPTION;
1930 Py_DECREF(py_result);
1935 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1937 PyObject *py_ldb = (PyObject *)mod->private_data;
1938 PyObject *py_result, *py_msg;
1940 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
1942 if (py_msg == NULL) {
1943 return LDB_ERR_OPERATIONS_ERROR;
1946 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
1947 discard_const_p(char, "O"),
1952 if (py_result == NULL) {
1953 return LDB_ERR_PYTHON_EXCEPTION;
1956 Py_DECREF(py_result);
1961 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1963 PyObject *py_ldb = (PyObject *)mod->private_data;
1964 PyObject *py_result, *py_msg;
1966 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
1968 if (py_msg == NULL) {
1969 return LDB_ERR_OPERATIONS_ERROR;
1972 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
1973 discard_const_p(char, "O"),
1978 if (py_result == NULL) {
1979 return LDB_ERR_PYTHON_EXCEPTION;
1982 Py_DECREF(py_result);
1987 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
1989 PyObject *py_ldb = (PyObject *)mod->private_data;
1990 PyObject *py_result, *py_dn;
1992 py_dn = PyLdbDn_FromDn(req->op.del.dn);
1995 return LDB_ERR_OPERATIONS_ERROR;
1997 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
1998 discard_const_p(char, "O"),
2001 if (py_result == NULL) {
2002 return LDB_ERR_PYTHON_EXCEPTION;
2005 Py_DECREF(py_result);
2010 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
2012 PyObject *py_ldb = (PyObject *)mod->private_data;
2013 PyObject *py_result, *py_olddn, *py_newdn;
2015 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
2017 if (py_olddn == NULL)
2018 return LDB_ERR_OPERATIONS_ERROR;
2020 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
2022 if (py_newdn == NULL)
2023 return LDB_ERR_OPERATIONS_ERROR;
2025 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
2026 discard_const_p(char, "OO"),
2027 py_olddn, py_newdn);
2029 Py_DECREF(py_olddn);
2030 Py_DECREF(py_newdn);
2032 if (py_result == NULL) {
2033 return LDB_ERR_PYTHON_EXCEPTION;
2036 Py_DECREF(py_result);
2041 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
2043 PyObject *py_ldb = (PyObject *)mod->private_data;
2044 PyObject *py_result;
2046 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
2047 discard_const_p(char, ""));
2049 return LDB_ERR_OPERATIONS_ERROR;
2052 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
2054 PyObject *py_ldb = (PyObject *)mod->private_data;
2055 PyObject *py_result;
2057 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
2058 discard_const_p(char, ""));
2060 return LDB_ERR_OPERATIONS_ERROR;
2063 static int py_module_start_transaction(struct ldb_module *mod)
2065 PyObject *py_ldb = (PyObject *)mod->private_data;
2066 PyObject *py_result;
2068 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
2069 discard_const_p(char, ""));
2071 if (py_result == NULL) {
2072 return LDB_ERR_PYTHON_EXCEPTION;
2075 Py_DECREF(py_result);
2080 static int py_module_end_transaction(struct ldb_module *mod)
2082 PyObject *py_ldb = (PyObject *)mod->private_data;
2083 PyObject *py_result;
2085 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
2086 discard_const_p(char, ""));
2088 if (py_result == NULL) {
2089 return LDB_ERR_PYTHON_EXCEPTION;
2092 Py_DECREF(py_result);
2097 static int py_module_del_transaction(struct ldb_module *mod)
2099 PyObject *py_ldb = (PyObject *)mod->private_data;
2100 PyObject *py_result;
2102 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
2103 discard_const_p(char, ""));
2105 if (py_result == NULL) {
2106 return LDB_ERR_PYTHON_EXCEPTION;
2109 Py_DECREF(py_result);
2114 static int py_module_destructor(struct ldb_module *mod)
2116 Py_DECREF((PyObject *)mod->private_data);
2120 static int py_module_init(struct ldb_module *mod)
2122 PyObject *py_class = (PyObject *)mod->ops->private_data;
2123 PyObject *py_result, *py_next, *py_ldb;
2125 py_ldb = PyLdb_FromLdbContext(mod->ldb);
2128 return LDB_ERR_OPERATIONS_ERROR;
2130 py_next = PyLdbModule_FromModule(mod->next);
2132 if (py_next == NULL)
2133 return LDB_ERR_OPERATIONS_ERROR;
2135 py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
2138 if (py_result == NULL) {
2139 return LDB_ERR_PYTHON_EXCEPTION;
2142 mod->private_data = py_result;
2144 talloc_set_destructor(mod, py_module_destructor);
2146 return ldb_next_init(mod);
2149 static PyObject *py_register_module(PyObject *module, PyObject *args)
2152 struct ldb_module_ops *ops;
2155 if (!PyArg_ParseTuple(args, "O", &input))
2158 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
2164 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name"))));
2167 ops->private_data = input;
2168 ops->init_context = py_module_init;
2169 ops->search = py_module_search;
2170 ops->add = py_module_add;
2171 ops->modify = py_module_modify;
2172 ops->del = py_module_del;
2173 ops->rename = py_module_rename;
2174 ops->request = py_module_request;
2175 ops->extended = py_module_extended;
2176 ops->start_transaction = py_module_start_transaction;
2177 ops->end_transaction = py_module_end_transaction;
2178 ops->del_transaction = py_module_del_transaction;
2180 ret = ldb_register_module(ops);
2182 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
2187 static PyObject *py_timestring(PyObject *module, PyObject *args)
2192 if (!PyArg_ParseTuple(args, "L", &t))
2194 tresult = ldb_timestring(NULL, t);
2195 ret = PyString_FromString(tresult);
2196 talloc_free(tresult);
2200 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
2203 if (!PyArg_ParseTuple(args, "s", &str))
2206 return PyInt_FromLong(ldb_string_to_time(str));
2209 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
2212 if (!PyArg_ParseTuple(args, "s", &name))
2214 return PyBool_FromLong(ldb_valid_attr_name(name));
2217 static PyMethodDef py_ldb_global_methods[] = {
2218 { "register_module", py_register_module, METH_VARARGS,
2219 "S.register_module(module) -> None\n"
2220 "Register a LDB module."},
2221 { "timestring", py_timestring, METH_VARARGS,
2222 "S.timestring(int) -> string\n"
2223 "Generate a LDAP time string from a UNIX timestamp" },
2224 { "string_to_time", py_string_to_time, METH_VARARGS,
2225 "S.string_to_time(string) -> int\n"
2226 "Parse a LDAP time string into a UNIX timestamp." },
2227 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2228 "S.valid_attr_name(name) -> bool\n"
2229 "Check whether the supplied name is a valid attribute name." },
2230 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2239 if (PyType_Ready(&PyLdbDn) < 0)
2242 if (PyType_Ready(&PyLdbMessage) < 0)
2245 if (PyType_Ready(&PyLdbMessageElement) < 0)
2248 if (PyType_Ready(&PyLdb) < 0)
2251 if (PyType_Ready(&PyLdbModule) < 0)
2254 if (PyType_Ready(&PyLdbTree) < 0)
2257 m = Py_InitModule3("ldb", py_ldb_global_methods,
2258 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2262 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2263 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2264 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2265 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2267 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2268 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2269 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2270 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2272 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2273 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2274 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2275 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2276 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2277 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2278 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2279 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2280 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2281 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2282 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2283 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2284 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2285 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2286 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2287 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2288 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2289 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2290 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2291 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2292 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2293 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2294 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2295 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2296 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2297 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2298 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2299 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2300 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2301 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2302 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2303 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2304 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2305 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2306 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2307 PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2308 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2309 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2311 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2313 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2315 PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL);
2316 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2319 Py_INCREF(&PyLdbDn);
2320 Py_INCREF(&PyLdbModule);
2321 Py_INCREF(&PyLdbMessage);
2322 Py_INCREF(&PyLdbMessageElement);
2323 Py_INCREF(&PyLdbTree);
2325 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2326 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2327 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2328 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2329 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2330 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);