2 Unix SMB/CIFS implementation.
6 Copyright (C) 2005,2006 Tim Potter <tpot@samba.org>
7 Copyright (C) 2006 Simo Sorce <idra@samba.org>
8 Copyright (C) 2007-2008 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/>.
28 #include "ldb_includes.h"
31 /* There's no Py_ssize_t in 2.4, apparently */
32 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
33 typedef int Py_ssize_t;
34 typedef inquiry lenfunc;
35 typedef intargfunc sizeargfunc;
38 /* Picked out of thin air. To do this properly, we should probably have some part of the
39 * errors in LDB be allocated to bindings ? */
40 #define LDB_ERR_PYTHON_EXCEPTION 142
42 static PyObject *PyExc_LdbError;
44 void PyErr_SetLdbError(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 */
48 PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", ret, ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
50 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
51 struct ldb_message_element *el,
54 const struct ldb_schema_attribute *a;
55 struct ldb_val new_val;
56 TALLOC_CTX *mem_ctx = talloc_new(NULL);
61 if (ldb_ctx != NULL) {
62 a = ldb_schema_attribute_by_name(ldb_ctx, el->name);
65 if (a->syntax->ldif_write_fn(ldb_ctx, mem_ctx, val, &new_val) != 0) {
72 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
79 bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object,
80 struct ldb_context *ldb_ctx, struct ldb_dn **dn)
84 if (ldb_ctx != NULL && PyString_Check(object)) {
85 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
90 if (PyLdbDn_Check(object)) {
91 *dn = PyLdbDn_AsDn(object);
95 PyErr_SetString(PyExc_TypeError, "Expected DN");
99 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
103 if (result == NULL) {
106 ret = PyList_New(result->count);
107 for (i = 0; i < result->count; i++) {
108 PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i])
114 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx, PyObject *obj)
116 struct ldb_result *res;
122 res = talloc_zero(mem_ctx, struct ldb_result);
123 res->count = PyList_Size(obj);
124 res->msgs = talloc_array(res, struct ldb_message *, res->count);
125 for (i = 0; i < res->count; i++) {
126 PyObject *item = PyList_GetItem(obj, i);
127 res->msgs[i] = PyLdbMessage_AsMessage(item);
132 static PyObject *py_ldb_dn_validate(PyLdbDnObject *self)
134 return PyBool_FromLong(ldb_dn_validate(self->dn));
137 static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self)
139 return PyBool_FromLong(ldb_dn_is_valid(self->dn));
142 static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self)
144 return PyBool_FromLong(ldb_dn_is_special(self->dn));
147 static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self)
149 return PyBool_FromLong(ldb_dn_is_null(self->dn));
152 static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self)
154 return PyString_FromString(ldb_dn_get_casefold(self->dn));
157 static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self)
159 return PyString_FromString(ldb_dn_get_linearized(self->dn));
162 static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self)
164 return PyString_FromString(ldb_dn_canonical_string(self->dn, self->dn));
167 static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self)
169 return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn));
172 static PyObject *py_ldb_dn_repr(PyLdbDnObject *self)
174 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn))));
177 static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args)
181 if (!PyArg_ParseTuple(args, "s", &name))
184 return ldb_dn_check_special(self->dn, name)?Py_True:Py_False;
187 static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
189 return ldb_dn_compare(dn1->dn, dn2->dn);
192 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
194 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
195 return PyLdbDn_FromDn(ldb_dn_get_parent(NULL, dn));
198 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
200 static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
203 struct ldb_dn *dn, *other;
204 if (!PyArg_ParseTuple(args, "O", &py_other))
207 dn = PyLdbDn_AsDn((PyObject *)self);
209 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
212 return ldb_dn_add_child(dn, other)?Py_True:Py_False;
215 static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
218 struct ldb_dn *other, *dn;
219 if (!PyArg_ParseTuple(args, "O", &py_other))
222 dn = PyLdbDn_AsDn((PyObject *)self);
224 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
227 return ldb_dn_add_base(dn, other)?Py_True:Py_False;
230 static PyMethodDef py_ldb_dn_methods[] = {
231 { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS,
232 "S.validate() -> bool\n"
233 "Validate DN is correct." },
234 { "is_valid", (PyCFunction)py_ldb_dn_is_valid, METH_NOARGS,
235 "S.is_valid() -> bool\n" },
236 { "is_special", (PyCFunction)py_ldb_dn_is_special, METH_NOARGS,
237 "S.is_special() -> bool\n"
238 "Check whether this is a special LDB DN." },
239 { "is_null", (PyCFunction)py_ldb_dn_is_null, METH_NOARGS,
240 "Check whether this is a null DN." },
241 { "get_casefold", (PyCFunction)py_ldb_dn_get_casefold, METH_NOARGS,
243 { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS,
245 { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
246 "S.canonical_str() -> string\n"
247 "Canonical version of this DN (like a posix path)." },
248 { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
249 "S.canonical_ex_str() -> string\n"
250 "Canonical version of this DN (like a posix path, with terminating newline)." },
251 { "check_special", (PyCFunction)py_ldb_dn_is_special, METH_VARARGS,
253 { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS,
255 "Get the parent for this DN." },
256 { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS,
257 "S.add_child(dn) -> None\n"
258 "Add a child DN to this DN." },
259 { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
260 "S.add_base(dn) -> None\n"
261 "Add a base DN to this DN." },
262 { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
267 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
269 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
272 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
274 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self),
276 struct ldb_dn *ret = ldb_dn_copy(NULL, dn);
277 if (!PyObject_AsDn(NULL, py_other, NULL, &other))
279 ldb_dn_add_child(ret, other);
280 return PyLdbDn_FromDn(ret);
283 static PySequenceMethods py_ldb_dn_seq = {
284 .sq_length = (lenfunc)py_ldb_dn_len,
285 .sq_concat = (binaryfunc)py_ldb_dn_concat,
288 static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
293 struct ldb_context *ldb_ctx;
294 PyLdbDnObject *py_ret;
295 const char *kwnames[] = { "ldb", "dn", NULL };
297 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os", (char **)kwnames, &py_ldb, &str))
300 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
302 ret = ldb_dn_new(ldb_ctx, ldb_ctx, str);
303 /* ldb_dn_new() doesn't accept NULL as memory context, so
304 we do it this way... */
305 talloc_steal(NULL, ret);
307 if (ret == NULL || !ldb_dn_validate(ret)) {
308 PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
312 py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0);
318 return (PyObject *)py_ret;
321 PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
323 PyLdbDnObject *py_ret;
324 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
325 if (py_ret == NULL) {
329 py_ret->mem_ctx = talloc_new(NULL);
330 py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
331 return (PyObject *)py_ret;
334 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
336 talloc_free(self->mem_ctx);
337 self->ob_type->tp_free(self);
340 PyTypeObject PyLdbDn = {
342 .tp_methods = py_ldb_dn_methods,
343 .tp_str = (reprfunc)py_ldb_dn_get_linearized,
344 .tp_repr = (reprfunc)py_ldb_dn_repr,
345 .tp_compare = (cmpfunc)py_ldb_dn_compare,
346 .tp_as_sequence = &py_ldb_dn_seq,
347 .tp_doc = "A LDB distinguished name.",
348 .tp_new = py_ldb_dn_new,
349 .tp_dealloc = (destructor)py_ldb_dn_dealloc,
350 .tp_basicsize = sizeof(PyLdbObject),
351 .tp_flags = Py_TPFLAGS_DEFAULT,
355 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
356 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
358 PyObject *fn = context;
359 PyObject_CallFunction(fn, (char *)"(i,O)", level, PyString_FromFormatV(fmt, ap));
362 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
366 if (!PyArg_ParseTuple(args, "O", &cb))
370 /* FIXME: Where do we DECREF cb ? */
371 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
376 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
379 if (!PyArg_ParseTuple(args, "I", &perms))
382 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
387 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
390 if (!PyArg_ParseTuple(args, "s", &modules_dir))
393 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
398 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
400 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
404 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
406 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
410 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
412 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
416 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
418 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
422 static PyObject *py_ldb_repr(PyLdbObject *self)
424 return PyString_FromFormat("<ldb connection>");
427 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
429 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
432 return PyLdbDn_FromDn(dn);
436 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
438 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
441 return PyLdbDn_FromDn(dn);
445 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
447 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
450 return PyLdbDn_FromDn(dn);
454 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
456 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
459 return PyLdbDn_FromDn(dn);
462 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list)
466 if (!PyList_Check(list)) {
467 PyErr_SetString(PyExc_TypeError, "options is not a list");
470 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
471 for (i = 0; i < PyList_Size(list); i++) {
472 PyObject *item = PyList_GetItem(list, i);
473 if (!PyString_Check(item)) {
474 PyErr_SetString(PyExc_TypeError, "options should be strings");
477 ret[i] = PyString_AsString(item);
483 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
485 const char *kwnames[] = { "url", "flags", "options", NULL };
487 PyObject *py_options = Py_None;
488 const char **options;
491 struct ldb_context *ldb;
493 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__", (char **)kwnames,
494 &url, &flags, &py_options))
497 ldb = PyLdb_AsLdbContext(self);
499 if (py_options == Py_None) {
502 options = PyList_AsStringList(ldb, py_options);
508 ret = ldb_connect(ldb, url, flags, options);
509 if (ret != LDB_SUCCESS) {
510 PyErr_SetLdbError(ret, ldb);
515 talloc_free(options);
519 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
522 struct ldb_context *ldb;
523 ldb = ldb_init(NULL, event_context_init(NULL));
529 ret = (PyLdbObject *)type->tp_alloc(type, 0);
535 return (PyObject *)ret;
538 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
542 PyObject *py_options = Py_None;
544 const char **options;
545 const char *kwnames[] = { "url", "flags", "options", NULL };
546 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO", (char **)kwnames, &url, &flags,
550 if (py_options == Py_None) {
553 options = PyList_AsStringList(NULL, py_options);
558 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
559 talloc_free(options);
561 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
566 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
570 if (!PyArg_ParseTuple(args, "O", &py_msg))
573 if (!PyLdbMessage_Check(py_msg)) {
574 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
578 ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
579 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
584 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
588 Py_ssize_t dict_pos, msg_pos;
589 struct ldb_message_element *msgel;
590 struct ldb_message *msg;
591 PyObject *key, *value;
593 if (!PyArg_ParseTuple(args, "O", &py_msg))
596 if (PyDict_Check(py_msg)) {
597 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
598 msg = ldb_msg_new(NULL);
599 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
600 msg_pos = dict_pos = 0;
602 if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
603 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
606 if (msg->dn == NULL) {
607 PyErr_SetString(PyExc_TypeError, "dn set but not found");
612 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
613 char *key_str = PyString_AsString(key);
614 if (strcmp(key_str, "dn") != 0) {
615 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
617 PyErr_SetString(PyExc_TypeError, "unable to import element");
620 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
625 if (msg->dn == NULL) {
626 PyErr_SetString(PyExc_TypeError, "no dn set");
630 msg->num_elements = msg_pos;
632 msg = PyLdbMessage_AsMessage(py_msg);
635 ret = ldb_add(PyLdb_AsLdbContext(self), msg);
636 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
643 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
648 struct ldb_context *ldb;
649 if (!PyArg_ParseTuple(args, "O", &py_dn))
652 ldb = PyLdb_AsLdbContext(self);
654 if (!PyObject_AsDn(NULL, py_dn, ldb, &dn))
657 ret = ldb_delete(ldb, dn);
658 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
663 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
665 PyObject *py_dn1, *py_dn2;
666 struct ldb_dn *dn1, *dn2;
668 struct ldb_context *ldb;
669 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
672 ldb = PyLdb_AsLdbContext(self);
673 if (!PyObject_AsDn(NULL, py_dn1, ldb, &dn1))
676 if (!PyObject_AsDn(NULL, py_dn2, ldb, &dn2))
679 ret = ldb_rename(ldb, dn1, dn2);
680 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
685 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
688 if (!PyArg_ParseTuple(args, "s", &name))
691 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
696 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
698 char *attribute, *syntax;
701 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
704 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
706 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
711 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
716 /* We don't want this attached to the 'ldb' any more */
717 talloc_steal(NULL, ldif);
718 return Py_BuildValue((char *)"(iO)", ldif->changetype,
719 PyLdbMessage_FromMessage(ldif->msg));
724 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
727 struct ldb_ldif *ldif;
730 if (!PyArg_ParseTuple(args, "s", &s))
733 list = PyList_New(0);
734 while ((ldif = ldb_ldif_read_string(self->ldb_ctx, &s)) != NULL) {
735 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
737 return PyObject_GetIter(list);
740 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
742 const struct ldb_schema_attribute *a;
743 struct ldb_val old_val;
744 struct ldb_val new_val;
750 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
753 mem_ctx = talloc_new(NULL);
755 old_val.data = (uint8_t *)PyString_AsString(val);
756 old_val.length = PyString_Size(val);
758 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
764 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
765 talloc_free(mem_ctx);
769 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
771 talloc_free(mem_ctx);
776 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
778 PyObject *py_base = Py_None;
779 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
781 PyObject *py_attrs = Py_None;
782 PyObject *py_controls = Py_None;
783 const char *kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
785 struct ldb_result *res;
786 struct ldb_request *req;
788 struct ldb_context *ldb_ctx;
789 struct ldb_control **parsed_controls;
792 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO", (char **)kwnames,
793 &py_base, &scope, &expr, &py_attrs, &py_controls))
796 ldb_ctx = PyLdb_AsLdbContext(self);
798 if (py_attrs == Py_None) {
801 attrs = PyList_AsStringList(ldb_ctx, py_attrs);
806 if (py_base == Py_None) {
807 base = ldb_get_default_basedn(ldb_ctx);
809 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base))
813 if (py_controls == Py_None) {
814 parsed_controls = NULL;
816 const char **controls = PyList_AsStringList(ldb_ctx, py_controls);
817 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
818 talloc_free(controls);
821 res = talloc_zero(ldb_ctx, struct ldb_result);
827 ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
834 ldb_search_default_callback,
837 if (ret != LDB_SUCCESS) {
839 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb_ctx);
843 ret = ldb_request(ldb_ctx, req);
845 if (ret == LDB_SUCCESS) {
846 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
851 return PyLdbResult_FromResult(res);
854 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
859 if (!PyArg_ParseTuple(args, "s", &name))
862 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
867 /* FIXME: More interpretation */
872 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
877 if (!PyArg_ParseTuple(args, "sO", &name, &data))
880 /* FIXME: More interpretation */
882 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
887 static PyObject *py_ldb_modules(PyLdbObject *self)
889 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
890 PyObject *ret = PyList_New(0);
891 struct ldb_module *mod;
893 for (mod = ldb->modules; mod; mod = mod->next) {
894 PyList_Append(ret, PyLdbModule_FromModule(mod));
900 static PyMethodDef py_ldb_methods[] = {
901 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
902 "S.set_debug(callback) -> None\n"
903 "Set callback for LDB debug messages.\n"
904 "The callback should accept a debug level and debug text." },
905 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
906 "S.set_create_perms(mode) -> None\n"
907 "Set mode to use when creating new LDB files." },
908 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
909 "S.set_modules_dir(path) -> None\n"
910 "Set path LDB should search for modules" },
911 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
912 "S.transaction_start() -> None\n"
913 "Start a new transaction." },
914 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
915 "S.transaction_commit() -> None\n"
916 "commit a new transaction." },
917 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
918 "S.transaction_cancel() -> None\n"
919 "cancel a new transaction." },
920 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
922 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
924 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
926 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
928 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
930 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
931 "S.connect(url, flags=0, options=None) -> None\n"
932 "Connect to a LDB URL." },
933 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
934 "S.modify(message) -> None\n"
935 "Modify an entry." },
936 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
937 "S.add(message) -> None\n"
939 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
940 "S.delete(dn) -> None\n"
941 "Remove an entry." },
942 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
943 "S.rename(old_dn, new_dn) -> None\n"
944 "Rename an entry." },
945 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
946 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
947 "Search in a database.\n"
949 ":param base: Optional base DN to search\n"
950 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
951 ":param expression: Optional search expression\n"
952 ":param attrs: Attributes to return (defaults to all)\n"
953 ":param controls: Optional list of controls\n"
954 ":return: Iterator over Message objects\n"
956 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
958 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
960 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
962 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
963 "S.parse_ldif(ldif) -> iter(messages)\n"
964 "Parse a string formatted using LDIF." },
965 { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
966 "S.get_opaque(name) -> value\n"
967 "Get an opaque value set on this LDB connection. \n"
968 ":note: The returned value may not be useful in Python."
970 { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
971 "S.set_opaque(name, value) -> None\n"
972 "Set an opaque value on this LDB connection. \n"
973 ":note: Passing incorrect values may cause crashes." },
974 { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
975 "S.modules() -> list\n"
976 "Return the list of modules on this LDB connection " },
980 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
982 PyLdbModuleObject *ret;
984 ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
989 ret->mem_ctx = talloc_new(NULL);
990 ret->mod = talloc_reference(ret->mem_ctx, mod);
991 return (PyObject *)ret;
994 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
996 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
999 static PyGetSetDef py_ldb_getset[] = {
1000 { (char *)"firstmodule", (getter)py_ldb_get_firstmodule, NULL, NULL },
1004 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
1006 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1008 struct ldb_result *result;
1012 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
1015 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
1016 if (ret != LDB_SUCCESS) {
1017 PyErr_SetLdbError(ret, ldb_ctx);
1021 count = result->count;
1023 talloc_free(result);
1028 static PySequenceMethods py_ldb_seq = {
1029 .sq_contains = (objobjproc)py_ldb_contains,
1032 PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
1036 ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1041 ret->mem_ctx = talloc_new(NULL);
1042 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1043 return (PyObject *)ret;
1046 static void py_ldb_dealloc(PyLdbObject *self)
1048 talloc_free(self->mem_ctx);
1049 self->ob_type->tp_free(self);
1052 PyTypeObject PyLdb = {
1054 .tp_methods = py_ldb_methods,
1055 .tp_repr = (reprfunc)py_ldb_repr,
1056 .tp_new = py_ldb_new,
1057 .tp_init = (initproc)py_ldb_init,
1058 .tp_dealloc = (destructor)py_ldb_dealloc,
1059 .tp_getset = py_ldb_getset,
1060 .tp_getattro = PyObject_GenericGetAttr,
1061 .tp_basicsize = sizeof(PyLdbObject),
1062 .tp_doc = "Connection to a LDB database.",
1063 .tp_as_sequence = &py_ldb_seq,
1064 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1067 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
1069 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1072 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
1074 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1077 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1079 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1083 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1085 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1089 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1091 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1095 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1097 PyObject *py_base, *py_tree, *py_attrs;
1099 struct ldb_request *req;
1100 const char *kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1101 struct ldb_module *mod;
1102 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO", (char **)kwnames,
1103 &py_base, &scope, &py_tree, &py_attrs))
1108 ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base),
1109 scope, NULL /* expr */, py_attrs == Py_None?NULL:PyList_AsStringList(req, py_attrs),
1110 NULL /* controls */, NULL, NULL, NULL);
1111 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1113 ret = mod->ops->search(mod, req);
1116 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1118 return PyLdbResult_FromResult(req->op.search.res);
1122 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1124 struct ldb_request *req;
1125 PyObject *py_message;
1127 struct ldb_module *mod;
1129 if (!PyArg_ParseTuple(args, "O", &py_message))
1132 req = talloc_zero(NULL, struct ldb_request);
1133 req->operation = LDB_ADD;
1134 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1136 mod = PyLdbModule_AsModule(self);
1137 ret = mod->ops->add(mod, req);
1139 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1144 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
1147 struct ldb_request *req;
1148 PyObject *py_message;
1149 struct ldb_module *mod;
1151 if (!PyArg_ParseTuple(args, "O", &py_message))
1154 req = talloc_zero(NULL, struct ldb_request);
1155 req->operation = LDB_MODIFY;
1156 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1158 mod = PyLdbModule_AsModule(self);
1159 ret = mod->ops->modify(mod, req);
1161 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1166 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
1169 struct ldb_request *req;
1172 if (!PyArg_ParseTuple(args, "O", &py_dn))
1175 req = talloc_zero(NULL, struct ldb_request);
1176 req->operation = LDB_DELETE;
1177 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1179 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1181 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1186 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1189 struct ldb_request *req;
1190 PyObject *py_dn1, *py_dn2;
1192 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1195 req = talloc_zero(NULL, struct ldb_request);
1197 req->operation = LDB_RENAME;
1198 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1199 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1201 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1203 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1208 static PyMethodDef py_ldb_module_methods[] = {
1209 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1210 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1211 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1212 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1213 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1214 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1215 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1216 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1220 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
1222 talloc_free(self->mem_ctx);
1223 self->ob_type->tp_free(self);
1226 PyTypeObject PyLdbModule = {
1227 .tp_name = "LdbModule",
1228 .tp_methods = py_ldb_module_methods,
1229 .tp_repr = (reprfunc)py_ldb_module_repr,
1230 .tp_str = (reprfunc)py_ldb_module_str,
1231 .tp_basicsize = sizeof(PyLdbModuleObject),
1232 .tp_dealloc = (destructor)py_ldb_module_dealloc,
1233 .tp_flags = Py_TPFLAGS_DEFAULT,
1236 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1237 PyObject *set_obj, int flags,
1238 const char *attr_name)
1240 struct ldb_message_element *me;
1242 if (PyLdbMessageElement_Check(set_obj))
1243 return PyLdbMessageElement_AsMessageElement(set_obj);
1245 me = talloc(mem_ctx, struct ldb_message_element);
1247 me->name = attr_name;
1249 if (PyString_Check(set_obj)) {
1251 me->values = talloc_array(me, struct ldb_val, me->num_values);
1252 me->values[0].length = PyString_Size(set_obj);
1253 me->values[0].data = (uint8_t *)talloc_strndup(me->values,
1254 PyString_AsString(set_obj),
1255 me->values[0].length);
1256 } else if (PySequence_Check(set_obj)) {
1258 me->num_values = PySequence_Size(set_obj);
1259 me->values = talloc_array(me, struct ldb_val, me->num_values);
1260 for (i = 0; i < me->num_values; i++) {
1261 PyObject *obj = PySequence_GetItem(set_obj, i);
1263 me->values[i].length = PyString_Size(obj);
1264 me->values[i].data = (uint8_t *)PyString_AsString(obj);
1275 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
1276 struct ldb_message_element *me)
1281 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1282 result = PyList_New(me->num_values);
1284 for (i = 0; i < me->num_values; i++) {
1285 PyList_SetItem(result, i,
1286 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1292 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1295 if (!PyArg_ParseTuple(args, "i", &i))
1297 if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1300 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1301 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1304 static PyMethodDef py_ldb_msg_element_methods[] = {
1305 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1309 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1311 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1314 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1316 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1317 if (idx < 0 || idx >= el->num_values) {
1318 PyErr_SetString(PyExc_IndexError, "Out of range");
1321 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1324 static PySequenceMethods py_ldb_msg_element_seq = {
1325 .sq_length = (lenfunc)py_ldb_msg_element_len,
1326 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1329 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1331 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1332 PyLdbMessageElement_AsMessageElement(other));
1335 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1337 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1340 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
1342 PyLdbMessageElementObject *ret;
1343 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1348 ret->mem_ctx = talloc_new(NULL);
1349 if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1354 return (PyObject *)ret;
1357 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1359 PyObject *py_elements = NULL;
1360 struct ldb_message_element *el;
1363 const char *kwnames[] = { "elements", "flags", "name", NULL };
1364 PyLdbMessageElementObject *ret;
1365 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois", (char **)kwnames, &py_elements, &flags, &name))
1368 el = talloc_zero(NULL, struct ldb_message_element);
1370 if (py_elements != NULL) {
1372 if (PyString_Check(py_elements)) {
1374 el->values = talloc_array(el, struct ldb_val, 1);
1375 el->values[0].data = (uint8_t *)PyString_AsString(py_elements);
1376 el->values[0].length = PyString_Size(py_elements);
1377 } else if (PySequence_Check(py_elements)) {
1378 el->num_values = PySequence_Size(py_elements);
1379 el->values = talloc_array(el, struct ldb_val, el->num_values);
1380 for (i = 0; i < el->num_values; i++) {
1381 PyObject *item = PySequence_GetItem(py_elements, i);
1382 el->values[i].data = (uint8_t *)PyString_AsString(item);
1383 el->values[i].length = PyString_Size(item);
1386 PyErr_SetString(PyExc_TypeError,
1387 "Expected string or list");
1394 el->name = talloc_strdup(el, name);
1396 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1403 ret->mem_ctx = talloc_new(NULL);
1404 ret->el = talloc_reference(ret->mem_ctx, el);
1405 return (PyObject *)ret;
1408 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1410 char *element_str = NULL;
1412 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1415 for (i = 0; i < el->num_values; i++) {
1416 PyObject *o = py_ldb_msg_element_find(self, i);
1417 if (element_str == NULL)
1418 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1420 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1423 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1425 talloc_free(element_str);
1430 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
1432 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1434 if (el->num_values == 1)
1435 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1440 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
1442 talloc_free(self->mem_ctx);
1443 self->ob_type->tp_free(self);
1446 PyTypeObject PyLdbMessageElement = {
1447 .tp_name = "MessageElement",
1448 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1449 .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1450 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1451 .tp_str = (reprfunc)py_ldb_msg_element_str,
1452 .tp_methods = py_ldb_msg_element_methods,
1453 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1454 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1455 .tp_as_sequence = &py_ldb_msg_element_seq,
1456 .tp_new = py_ldb_msg_element_new,
1457 .tp_flags = Py_TPFLAGS_DEFAULT,
1460 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
1463 if (!PyArg_ParseTuple(args, "s", &name))
1466 ldb_msg_remove_attr(self->msg, name);
1471 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
1473 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1475 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1476 if (msg->dn != NULL) {
1477 PyList_SetItem(obj, j, PyString_FromString("dn"));
1480 for (i = 0; i < msg->num_elements; i++) {
1481 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1487 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
1489 struct ldb_message_element *el;
1490 char *name = PyString_AsString(py_name);
1491 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1492 if (!strcmp(name, "dn"))
1493 return PyLdbDn_FromDn(msg->dn);
1494 el = ldb_msg_find_element(msg, name);
1498 return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg);
1501 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
1503 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
1505 PyErr_SetString(PyExc_KeyError, "No such element");
1511 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
1513 PyObject *name, *ret;
1514 if (!PyArg_ParseTuple(args, "O", &name))
1517 ret = py_ldb_msg_getitem_helper(self, name);
1523 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
1525 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1527 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
1529 if (msg->dn != NULL) {
1530 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
1533 for (i = 0; i < msg->num_elements; i++, j++) {
1534 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], self->msg)));
1539 static PyMethodDef py_ldb_msg_methods[] = {
1540 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
1541 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
1542 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
1543 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
1547 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
1549 PyObject *list, *iter;
1551 list = py_ldb_msg_keys(self);
1552 iter = PyObject_GetIter(list);
1557 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
1559 char *attr_name = PyString_AsString(name);
1560 if (value == NULL) {
1561 ldb_msg_remove_attr(self->msg, attr_name);
1563 struct ldb_message_element *el = PyObject_AsMessageElement(NULL,
1564 value, 0, attr_name);
1567 talloc_steal(self->msg, el);
1568 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
1569 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
1574 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
1576 return PyLdbMessage_AsMessage(self)->num_elements;
1579 static PyMappingMethods py_ldb_msg_mapping = {
1580 .mp_length = (lenfunc)py_ldb_msg_length,
1581 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
1582 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
1585 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1587 const char *kwnames[] = { "dn", NULL };
1588 struct ldb_message *ret;
1589 PyObject *pydn = NULL;
1590 PyLdbMessageObject *py_ret;
1591 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", (char **)kwnames, &pydn))
1594 ret = ldb_msg_new(NULL);
1601 if (!PyObject_AsDn(NULL, pydn, NULL, &ret->dn))
1604 py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
1605 if (py_ret == NULL) {
1610 py_ret->mem_ctx = talloc_new(NULL);
1611 py_ret->msg = talloc_reference(py_ret->mem_ctx, ret);
1612 return (PyObject *)py_ret;
1615 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
1617 PyLdbMessageObject *ret;
1619 ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
1624 ret->mem_ctx = talloc_new(NULL);
1625 ret->msg = talloc_reference(ret->mem_ctx, msg);
1626 return (PyObject *)ret;
1629 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
1631 return PyLdbDn_FromDn(PyLdbMessage_AsMessage(self)->dn);
1634 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
1636 PyLdbMessage_AsMessage(self)->dn = PyLdbDn_AsDn(value);
1640 static PyGetSetDef py_ldb_msg_getset[] = {
1641 { (char *)"dn", (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
1645 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
1647 PyObject *dict = PyDict_New(), *ret;
1648 if (PyDict_Update(dict, (PyObject *)self) != 0)
1650 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
1655 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
1657 talloc_free(self->mem_ctx);
1658 self->ob_type->tp_free(self);
1661 PyTypeObject PyLdbMessage = {
1662 .tp_name = "Message",
1663 .tp_methods = py_ldb_msg_methods,
1664 .tp_getset = py_ldb_msg_getset,
1665 .tp_as_mapping = &py_ldb_msg_mapping,
1666 .tp_basicsize = sizeof(PyLdbMessageObject),
1667 .tp_dealloc = (destructor)py_ldb_msg_dealloc,
1668 .tp_new = py_ldb_msg_new,
1669 .tp_repr = (reprfunc)py_ldb_msg_repr,
1670 .tp_flags = Py_TPFLAGS_DEFAULT,
1671 .tp_iter = (getiterfunc)py_ldb_msg_iter,
1674 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
1676 PyLdbTreeObject *ret;
1678 ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
1684 ret->mem_ctx = talloc_new(NULL);
1685 ret->tree = talloc_reference(ret->mem_ctx, tree);
1686 return (PyObject *)ret;
1689 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
1691 talloc_free(self->mem_ctx);
1692 self->ob_type->tp_free(self);
1695 PyTypeObject PyLdbTree = {
1697 .tp_basicsize = sizeof(PyLdbTreeObject),
1698 .tp_dealloc = (destructor)py_ldb_tree_dealloc,
1699 .tp_flags = Py_TPFLAGS_DEFAULT,
1703 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
1705 PyObject *py_ldb = mod->private_data;
1706 PyObject *py_result, *py_base, *py_attrs, *py_tree;
1708 py_base = PyLdbDn_FromDn(req->op.search.base);
1710 if (py_base == NULL)
1711 return LDB_ERR_OPERATIONS_ERROR;
1713 py_tree = PyLdbTree_FromTree(req->op.search.tree);
1715 if (py_tree == NULL)
1716 return LDB_ERR_OPERATIONS_ERROR;
1718 if (req->op.search.attrs == NULL) {
1722 for (len = 0; req->op.search.attrs[len]; len++);
1723 py_attrs = PyList_New(len);
1724 for (i = 0; i < len; i++)
1725 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1728 py_result = PyObject_CallMethod(py_ldb, (char *)"search", (char *)"OiOO", py_base, req->op.search.scope, py_tree, py_attrs);
1730 Py_DECREF(py_attrs);
1734 if (py_result == NULL) {
1735 return LDB_ERR_PYTHON_EXCEPTION;
1738 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
1739 if (req->op.search.res == NULL) {
1740 return LDB_ERR_PYTHON_EXCEPTION;
1743 Py_DECREF(py_result);
1748 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1750 PyObject *py_ldb = mod->private_data;
1751 PyObject *py_result, *py_msg;
1753 py_msg = PyLdbMessage_FromMessage((struct ldb_message *)req->op.add.message);
1755 if (py_msg == NULL) {
1756 return LDB_ERR_OPERATIONS_ERROR;
1759 py_result = PyObject_CallMethod(py_ldb, (char *)"add", (char *)"O", py_msg);
1763 if (py_result == NULL) {
1764 return LDB_ERR_PYTHON_EXCEPTION;
1767 Py_DECREF(py_result);
1772 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1774 PyObject *py_ldb = mod->private_data;
1775 PyObject *py_result, *py_msg;
1777 py_msg = PyLdbMessage_FromMessage((struct ldb_message *)req->op.mod.message);
1779 if (py_msg == NULL) {
1780 return LDB_ERR_OPERATIONS_ERROR;
1783 py_result = PyObject_CallMethod(py_ldb, (char *)"modify", (char *)"O", py_msg);
1787 if (py_result == NULL) {
1788 return LDB_ERR_PYTHON_EXCEPTION;
1791 Py_DECREF(py_result);
1796 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
1798 PyObject *py_ldb = mod->private_data;
1799 PyObject *py_result, *py_dn;
1801 py_dn = PyLdbDn_FromDn(req->op.del.dn);
1804 return LDB_ERR_OPERATIONS_ERROR;
1806 py_result = PyObject_CallMethod(py_ldb, (char *)"delete", (char *)"O", py_dn);
1808 if (py_result == NULL) {
1809 return LDB_ERR_PYTHON_EXCEPTION;
1812 Py_DECREF(py_result);
1817 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
1819 PyObject *py_ldb = mod->private_data;
1820 PyObject *py_result, *py_olddn, *py_newdn;
1822 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
1824 if (py_olddn == NULL)
1825 return LDB_ERR_OPERATIONS_ERROR;
1827 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
1829 if (py_newdn == NULL)
1830 return LDB_ERR_OPERATIONS_ERROR;
1832 py_result = PyObject_CallMethod(py_ldb, (char *)"rename", (char *)"OO", py_olddn, py_newdn);
1834 Py_DECREF(py_olddn);
1835 Py_DECREF(py_newdn);
1837 if (py_result == NULL) {
1838 return LDB_ERR_PYTHON_EXCEPTION;
1841 Py_DECREF(py_result);
1846 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
1848 PyObject *py_ldb = mod->private_data;
1849 PyObject *py_result;
1851 py_result = PyObject_CallMethod(py_ldb, (char *)"request", (char *)"");
1853 return LDB_ERR_OPERATIONS_ERROR;
1856 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
1858 PyObject *py_ldb = mod->private_data;
1859 PyObject *py_result;
1861 py_result = PyObject_CallMethod(py_ldb, (char *)"extended", (char *)"");
1863 return LDB_ERR_OPERATIONS_ERROR;
1866 static int py_module_start_transaction(struct ldb_module *mod)
1868 PyObject *py_ldb = mod->private_data;
1869 PyObject *py_result;
1871 py_result = PyObject_CallMethod(py_ldb, (char *)"start_transaction", (char *)"");
1873 if (py_result == NULL) {
1874 return LDB_ERR_PYTHON_EXCEPTION;
1877 Py_DECREF(py_result);
1882 static int py_module_end_transaction(struct ldb_module *mod)
1884 PyObject *py_ldb = mod->private_data;
1885 PyObject *py_result;
1887 py_result = PyObject_CallMethod(py_ldb, (char *)"end_transaction", (char *)"");
1889 if (py_result == NULL) {
1890 return LDB_ERR_PYTHON_EXCEPTION;
1893 Py_DECREF(py_result);
1898 static int py_module_del_transaction(struct ldb_module *mod)
1900 PyObject *py_ldb = mod->private_data;
1901 PyObject *py_result;
1903 py_result = PyObject_CallMethod(py_ldb, (char *)"del_transaction", (char *)"");
1905 if (py_result == NULL) {
1906 return LDB_ERR_PYTHON_EXCEPTION;
1909 Py_DECREF(py_result);
1914 static int py_module_destructor(struct ldb_module *mod)
1916 Py_DECREF((PyObject *)mod->private_data);
1920 static int py_module_init (struct ldb_module *mod)
1922 PyObject *py_class = mod->ops->private_data;
1923 PyObject *py_result, *py_next, *py_ldb;
1925 py_ldb = PyLdb_FromLdbContext(mod->ldb);
1928 return LDB_ERR_OPERATIONS_ERROR;
1930 py_next = PyLdbModule_FromModule(mod->next);
1932 if (py_next == NULL)
1933 return LDB_ERR_OPERATIONS_ERROR;
1935 py_result = PyObject_CallFunction(py_class, (char *)"OO", py_ldb, py_next);
1937 if (py_result == NULL) {
1938 return LDB_ERR_PYTHON_EXCEPTION;
1941 mod->private_data = py_result;
1943 talloc_set_destructor(mod, py_module_destructor);
1945 return ldb_next_init(mod);
1948 static PyObject *py_register_module(PyObject *module, PyObject *args)
1951 struct ldb_module_ops *ops;
1954 if (!PyArg_ParseTuple(args, "O", &input))
1957 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
1963 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, (char *)"name")));
1966 ops->private_data = input;
1967 ops->init_context = py_module_init;
1968 ops->search = py_module_search;
1969 ops->add = py_module_add;
1970 ops->modify = py_module_modify;
1971 ops->del = py_module_del;
1972 ops->rename = py_module_rename;
1973 ops->request = py_module_request;
1974 ops->extended = py_module_extended;
1975 ops->start_transaction = py_module_start_transaction;
1976 ops->end_transaction = py_module_end_transaction;
1977 ops->del_transaction = py_module_del_transaction;
1979 ret = ldb_register_module(ops);
1981 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1986 static PyObject *py_timestring(PyObject *module, PyObject *args)
1991 if (!PyArg_ParseTuple(args, "L", &t))
1993 tresult = ldb_timestring(NULL, t);
1994 ret = PyString_FromString(tresult);
1995 talloc_free(tresult);
1999 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
2002 if (!PyArg_ParseTuple(args, "s", &str))
2005 return PyInt_FromLong(ldb_string_to_time(str));
2008 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
2011 if (!PyArg_ParseTuple(args, "s", &name))
2013 return PyBool_FromLong(ldb_valid_attr_name(name));
2016 static PyMethodDef py_ldb_global_methods[] = {
2017 { "register_module", py_register_module, METH_VARARGS,
2018 "S.register_module(module) -> None\n"
2019 "Register a LDB module."},
2020 { "timestring", py_timestring, METH_VARARGS,
2021 "S.timestring(int) -> string\n"
2022 "Generate a LDAP time string from a UNIX timestamp" },
2023 { "string_to_time", py_string_to_time, METH_VARARGS,
2024 "S.string_to_time(string) -> int\n"
2025 "Parse a LDAP time string into a UNIX timestamp." },
2026 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2027 "S.valid_attr_name(name) -> bool\n"
2028 "Check whether the supplied name is a valid attribute name." },
2029 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2038 if (PyType_Ready(&PyLdbDn) < 0)
2041 if (PyType_Ready(&PyLdbMessage) < 0)
2044 if (PyType_Ready(&PyLdbMessageElement) < 0)
2047 if (PyType_Ready(&PyLdb) < 0)
2050 if (PyType_Ready(&PyLdbModule) < 0)
2053 if (PyType_Ready(&PyLdbTree) < 0)
2056 m = Py_InitModule3("ldb", py_ldb_global_methods,
2057 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2061 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2062 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2063 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2064 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2066 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2067 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2068 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2069 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2071 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2072 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2073 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2074 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2075 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2076 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2077 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2078 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2079 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2080 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2081 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2082 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2083 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2084 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2085 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2086 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2087 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2088 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2089 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2090 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2091 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2092 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2093 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2094 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2095 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2096 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2097 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2098 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2099 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2100 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2101 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2102 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2103 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2104 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2105 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2106 PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2107 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2108 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2110 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2112 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2114 PyExc_LdbError = PyErr_NewException((char *)"_ldb.LdbError", NULL, NULL);
2115 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2118 Py_INCREF(&PyLdbDn);
2119 Py_INCREF(&PyLdbModule);
2120 Py_INCREF(&PyLdbMessage);
2121 Py_INCREF(&PyLdbMessageElement);
2122 Py_INCREF(&PyLdbTree);
2124 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2125 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2126 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2127 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2128 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2129 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);