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-2010 Jelmer Vernooij <jelmer@samba.org>
9 Copyright (C) 2009-2010 Matthias Dieter Wallnöfer
11 ** NOTE! The following LGPL license applies to the ldb
12 ** library. This does NOT imply that all of Samba is released
15 This library is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Lesser General Public
17 License as published by the Free Software Foundation; either
18 version 3 of the License, or (at your option) any later version.
20 This library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 #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 extern PyTypeObject PyLdbMessage;
57 extern PyTypeObject PyLdbModule;
58 extern PyTypeObject PyLdbDn;
59 extern PyTypeObject PyLdb;
60 staticforward PyTypeObject PyLdbMessageElement;
61 extern PyTypeObject PyLdbTree;
63 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx);
65 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
66 struct ldb_message_element *el,
69 struct ldb_val new_val;
70 TALLOC_CTX *mem_ctx = talloc_new(NULL);
75 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
83 * Create a Python object from a ldb_result.
85 * @param result LDB result to convert
86 * @return Python object with converted result (a list object)
88 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
95 ret = PyList_New(result->count);
96 for (i = 0; i < result->count; i++) {
97 PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i]));
103 * Create a LDB Result from a Python object.
104 * If conversion fails, NULL will be returned and a Python exception set.
106 * @param mem_ctx Memory context in which to allocate the LDB Result
107 * @param obj Python object to convert
108 * @return a ldb_result, or NULL if the conversion failed
110 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx,
113 struct ldb_result *res;
119 res = talloc_zero(mem_ctx, struct ldb_result);
120 res->count = PyList_Size(obj);
121 res->msgs = talloc_array(res, struct ldb_message *, res->count);
122 for (i = 0; i < res->count; i++) {
123 PyObject *item = PyList_GetItem(obj, i);
124 res->msgs[i] = PyLdbMessage_AsMessage(item);
129 static PyObject *py_ldb_dn_validate(PyLdbDnObject *self)
131 return PyBool_FromLong(ldb_dn_validate(self->dn));
134 static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self)
136 return PyBool_FromLong(ldb_dn_is_valid(self->dn));
139 static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self)
141 return PyBool_FromLong(ldb_dn_is_special(self->dn));
144 static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self)
146 return PyBool_FromLong(ldb_dn_is_null(self->dn));
149 static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self)
151 return PyString_FromString(ldb_dn_get_casefold(self->dn));
154 static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self)
156 return PyString_FromString(ldb_dn_get_linearized(self->dn));
159 static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self)
161 return PyString_FromString(ldb_dn_canonical_string(self->dn, self->dn));
164 static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self)
166 return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn));
169 static PyObject *py_ldb_dn_repr(PyLdbDnObject *self)
171 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn))));
174 static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args)
178 if (!PyArg_ParseTuple(args, "s", &name))
181 return ldb_dn_check_special(self->dn, name)?Py_True:Py_False;
184 static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
187 ret = ldb_dn_compare(dn1->dn, dn2->dn);
188 if (ret < 0) ret = -1;
189 if (ret > 0) ret = 1;
193 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
195 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
196 struct ldb_dn *parent;
197 PyLdbDnObject *py_ret;
198 TALLOC_CTX *mem_ctx = talloc_new(NULL);
200 parent = ldb_dn_get_parent(mem_ctx, dn);
201 if (parent == NULL) {
202 talloc_free(mem_ctx);
206 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
207 if (py_ret == NULL) {
209 talloc_free(mem_ctx);
212 py_ret->mem_ctx = mem_ctx;
214 return (PyObject *)py_ret;
217 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
219 static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
222 struct ldb_dn *dn, *other;
223 if (!PyArg_ParseTuple(args, "O", &py_other))
226 dn = PyLdbDn_AsDn((PyObject *)self);
228 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
231 return ldb_dn_add_child(dn, other)?Py_True:Py_False;
234 static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
237 struct ldb_dn *other, *dn;
238 if (!PyArg_ParseTuple(args, "O", &py_other))
241 dn = PyLdbDn_AsDn((PyObject *)self);
243 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
246 return ldb_dn_add_base(dn, other)?Py_True:Py_False;
249 static PyMethodDef py_ldb_dn_methods[] = {
250 { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS,
251 "S.validate() -> bool\n"
252 "Validate DN is correct." },
253 { "is_valid", (PyCFunction)py_ldb_dn_is_valid, METH_NOARGS,
254 "S.is_valid() -> bool\n" },
255 { "is_special", (PyCFunction)py_ldb_dn_is_special, METH_NOARGS,
256 "S.is_special() -> bool\n"
257 "Check whether this is a special LDB DN." },
258 { "is_null", (PyCFunction)py_ldb_dn_is_null, METH_NOARGS,
259 "Check whether this is a null DN." },
260 { "get_casefold", (PyCFunction)py_ldb_dn_get_casefold, METH_NOARGS,
262 { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS,
264 { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
265 "S.canonical_str() -> string\n"
266 "Canonical version of this DN (like a posix path)." },
267 { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
268 "S.canonical_ex_str() -> string\n"
269 "Canonical version of this DN (like a posix path, with terminating newline)." },
270 { "check_special", (PyCFunction)py_ldb_dn_is_special, METH_VARARGS,
272 { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS,
274 "Get the parent for this DN." },
275 { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS,
276 "S.add_child(dn) -> None\n"
277 "Add a child DN to this DN." },
278 { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
279 "S.add_base(dn) -> None\n"
280 "Add a base DN to this DN." },
281 { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
286 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
288 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
291 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
293 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self),
295 PyLdbDnObject *py_ret;
297 if (!PyObject_AsDn(NULL, py_other, NULL, &other))
300 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
301 if (py_ret == NULL) {
305 py_ret->mem_ctx = talloc_new(NULL);
306 py_ret->dn = ldb_dn_copy(py_ret->mem_ctx, dn);
307 ldb_dn_add_child(py_ret->dn, other);
308 return (PyObject *)py_ret;
311 static PySequenceMethods py_ldb_dn_seq = {
312 .sq_length = (lenfunc)py_ldb_dn_len,
313 .sq_concat = (binaryfunc)py_ldb_dn_concat,
316 static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
321 struct ldb_context *ldb_ctx;
323 PyLdbDnObject *py_ret;
324 const char * const kwnames[] = { "ldb", "dn", NULL };
326 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os",
327 discard_const_p(char *, kwnames),
331 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
333 mem_ctx = talloc_new(NULL);
334 if (mem_ctx == NULL) {
339 ret = ldb_dn_new(mem_ctx, ldb_ctx, str);
341 if (ret == NULL || !ldb_dn_validate(ret)) {
342 talloc_free(mem_ctx);
343 PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
347 py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0);
349 talloc_free(mem_ctx);
353 py_ret->mem_ctx = mem_ctx;
355 return (PyObject *)py_ret;
358 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
360 talloc_free(self->mem_ctx);
364 PyTypeObject PyLdbDn = {
366 .tp_methods = py_ldb_dn_methods,
367 .tp_str = (reprfunc)py_ldb_dn_get_linearized,
368 .tp_repr = (reprfunc)py_ldb_dn_repr,
369 .tp_compare = (cmpfunc)py_ldb_dn_compare,
370 .tp_as_sequence = &py_ldb_dn_seq,
371 .tp_doc = "A LDB distinguished name.",
372 .tp_new = py_ldb_dn_new,
373 .tp_dealloc = (destructor)py_ldb_dn_dealloc,
374 .tp_basicsize = sizeof(PyLdbObject),
375 .tp_flags = Py_TPFLAGS_DEFAULT,
379 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
380 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
382 PyObject *fn = (PyObject *)context;
383 PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap));
386 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
390 if (!PyArg_ParseTuple(args, "O", &cb))
394 /* FIXME: Where do we DECREF cb ? */
395 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
400 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
403 if (!PyArg_ParseTuple(args, "I", &perms))
406 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
411 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
414 if (!PyArg_ParseTuple(args, "s", &modules_dir))
417 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
422 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
424 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
428 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
430 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
434 static PyObject *py_ldb_transaction_prepare_commit(PyLdbObject *self)
436 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_prepare_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
440 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
442 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
446 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
448 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
452 static PyObject *py_ldb_repr(PyLdbObject *self)
454 return PyString_FromFormat("<ldb connection>");
457 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
459 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
462 return PyLdbDn_FromDn(dn);
466 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
468 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
471 return PyLdbDn_FromDn(dn);
474 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
476 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
479 return PyLdbDn_FromDn(dn);
482 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
484 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
487 return PyLdbDn_FromDn(dn);
490 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list,
491 const char *paramname)
495 if (!PyList_Check(list)) {
496 PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
499 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
500 for (i = 0; i < PyList_Size(list); i++) {
501 PyObject *item = PyList_GetItem(list, i);
502 if (!PyString_Check(item)) {
503 PyErr_Format(PyExc_TypeError, "%s should be strings", paramname);
506 ret[i] = talloc_strndup(ret, PyString_AsString(item),
507 PyString_Size(item));
513 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
515 const char * const kwnames[] = { "url", "flags", "options", NULL };
517 PyObject *py_options = Py_None;
518 const char **options;
521 struct ldb_context *ldb;
523 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__",
524 discard_const_p(char *, kwnames),
525 &url, &flags, &py_options))
528 ldb = PyLdb_AsLdbContext(self);
530 if (py_options == Py_None) {
533 options = PyList_AsStringList(ldb, py_options, "options");
539 ret = ldb_connect(ldb, url, flags, options);
540 if (ret != LDB_SUCCESS) {
541 PyErr_SetLdbError(PyExc_LdbError, ret, ldb);
546 talloc_free(options);
550 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
553 struct ldb_context *ldb;
554 ret = (PyLdbObject *)type->tp_alloc(type, 0);
559 ret->mem_ctx = talloc_new(NULL);
560 ldb = ldb_init(ret->mem_ctx, NULL);
568 return (PyObject *)ret;
571 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
575 PyObject *py_options = Py_None;
577 const char **options;
578 const char * const kwnames[] = { "url", "flags", "options", NULL };
580 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO",
581 discard_const_p(char *, kwnames),
582 &url, &flags, &py_options))
585 if (py_options == Py_None) {
588 options = PyList_AsStringList(NULL, py_options, "options");
593 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
594 talloc_free(options);
596 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
601 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
604 PyObject *py_controls = Py_None;
605 struct ldb_context *ldb_ctx;
606 struct ldb_request *req;
607 struct ldb_control **parsed_controls;
608 struct ldb_message *msg;
612 if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls))
615 mem_ctx = talloc_new(NULL);
616 if (mem_ctx == NULL) {
620 ldb_ctx = PyLdb_AsLdbContext(self);
622 if (py_controls == Py_None) {
623 parsed_controls = NULL;
625 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
626 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
627 talloc_free(controls);
630 if (!PyLdbMessage_Check(py_msg)) {
631 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
632 talloc_free(mem_ctx);
635 msg = PyLdbMessage_AsMessage(py_msg);
637 ret = ldb_msg_sanity_check(ldb_ctx, msg);
638 if (ret != LDB_SUCCESS) {
639 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
640 talloc_free(mem_ctx);
644 ret = ldb_build_mod_req(&req, ldb_ctx, mem_ctx, msg, parsed_controls,
645 NULL, ldb_op_default_callback, NULL);
646 if (ret != LDB_SUCCESS) {
647 PyErr_SetString(PyExc_TypeError, "failed to build request");
648 talloc_free(mem_ctx);
652 /* do request and autostart a transaction */
653 /* Then let's LDB handle the message error in case of pb as they are meaningful */
655 ret = ldb_transaction_start(ldb_ctx);
656 if (ret != LDB_SUCCESS) {
657 talloc_free(mem_ctx);
658 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
661 ret = ldb_request(ldb_ctx, req);
662 if (ret == LDB_SUCCESS) {
663 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
666 if (ret == LDB_SUCCESS) {
667 ret = ldb_transaction_commit(ldb_ctx);
669 ldb_transaction_cancel(ldb_ctx);
670 if (ldb_ctx->err_string == NULL) {
671 /* no error string was setup by the backend */
672 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
676 talloc_free(mem_ctx);
677 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
684 * Obtain a ldb message from a Python Dictionary object.
686 * @param mem_ctx Memory context
687 * @param py_obj Python Dictionary object
688 * @param ldb_ctx LDB context
689 * @param mod_flags Flags to be set on every message element
690 * @return ldb_message on success or NULL on failure
692 static struct ldb_message *PyDict_AsMessage(TALLOC_CTX *mem_ctx,
694 struct ldb_context *ldb_ctx,
695 unsigned int mod_flags)
697 struct ldb_message *msg;
698 unsigned int msg_pos = 0;
699 Py_ssize_t dict_pos = 0;
700 PyObject *key, *value;
701 struct ldb_message_element *msg_el;
702 PyObject *dn_value = PyDict_GetItemString(py_obj, "dn");
704 msg = ldb_msg_new(mem_ctx);
705 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_obj));
708 if (!PyObject_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
709 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
712 if (msg->dn == NULL) {
713 PyErr_SetString(PyExc_TypeError, "dn set but not found");
717 PyErr_SetString(PyExc_TypeError, "no dn set");
721 while (PyDict_Next(py_obj, &dict_pos, &key, &value)) {
722 char *key_str = PyString_AsString(key);
723 if (strcmp(key_str, "dn") != 0) {
724 msg_el = PyObject_AsMessageElement(msg->elements, value,
726 if (msg_el == NULL) {
727 PyErr_SetString(PyExc_TypeError, "unable to import element");
730 memcpy(&msg->elements[msg_pos], msg_el, sizeof(*msg_el));
735 msg->num_elements = msg_pos;
740 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
744 struct ldb_context *ldb_ctx;
745 struct ldb_request *req;
746 struct ldb_message *msg = NULL;
747 PyObject *py_controls = Py_None;
749 struct ldb_control **parsed_controls;
751 if (!PyArg_ParseTuple(args, "O|O", &py_obj, &py_controls ))
754 mem_ctx = talloc_new(NULL);
755 if (mem_ctx == NULL) {
759 ldb_ctx = PyLdb_AsLdbContext(self);
761 if (py_controls == Py_None) {
762 parsed_controls = NULL;
764 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
765 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
766 talloc_free(controls);
769 if (PyLdbMessage_Check(py_obj)) {
770 msg = PyLdbMessage_AsMessage(py_obj);
771 } else if (PyDict_Check(py_obj)) {
772 msg = PyDict_AsMessage(mem_ctx, py_obj, ldb_ctx, LDB_FLAG_MOD_ADD);
774 PyErr_SetString(PyExc_TypeError,
775 "Dictionary or LdbMessage object expected!");
779 /* we should have a PyErr already set */
780 talloc_free(mem_ctx);
784 ret = ldb_msg_sanity_check(ldb_ctx, msg);
785 if (ret != LDB_SUCCESS) {
786 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
787 talloc_free(mem_ctx);
791 ret = ldb_build_add_req(&req, ldb_ctx, mem_ctx, msg, parsed_controls,
792 NULL, ldb_op_default_callback, NULL);
793 if (ret != LDB_SUCCESS) {
794 PyErr_SetString(PyExc_TypeError, "failed to build request");
795 talloc_free(mem_ctx);
799 /* do request and autostart a transaction */
800 /* Then let's LDB handle the message error in case of pb as they are meaningful */
802 ret = ldb_transaction_start(ldb_ctx);
803 if (ret != LDB_SUCCESS) {
804 talloc_free(mem_ctx);
805 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
808 ret = ldb_request(ldb_ctx, req);
809 if (ret == LDB_SUCCESS) {
810 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
813 if (ret == LDB_SUCCESS) {
814 ret = ldb_transaction_commit(ldb_ctx);
816 ldb_transaction_cancel(ldb_ctx);
817 if (ldb_ctx->err_string == NULL) {
818 /* no error string was setup by the backend */
819 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
823 talloc_free(mem_ctx);
824 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
829 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
834 struct ldb_context *ldb_ctx;
835 struct ldb_request *req;
836 PyObject *py_controls = Py_None;
838 struct ldb_control **parsed_controls;
840 if (!PyArg_ParseTuple(args, "O|O", &py_dn, &py_controls))
843 mem_ctx = talloc_new(NULL);
844 if (mem_ctx == NULL) {
848 ldb_ctx = PyLdb_AsLdbContext(self);
850 if (py_controls == Py_None) {
851 parsed_controls = NULL;
853 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
854 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
855 talloc_free(controls);
858 if (!PyObject_AsDn(mem_ctx, py_dn, ldb_ctx, &dn)) {
859 talloc_free(mem_ctx);
863 ret = ldb_build_del_req(&req, ldb_ctx, mem_ctx, dn, parsed_controls,
864 NULL, ldb_op_default_callback, NULL);
865 if (ret != LDB_SUCCESS) {
866 PyErr_SetString(PyExc_TypeError, "failed to build request");
867 talloc_free(mem_ctx);
871 /* do request and autostart a transaction */
872 /* Then let's LDB handle the message error in case of pb as they are meaningful */
874 ret = ldb_transaction_start(ldb_ctx);
875 if (ret != LDB_SUCCESS) {
876 talloc_free(mem_ctx);
877 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
880 ret = ldb_request(ldb_ctx, req);
881 if (ret == LDB_SUCCESS) {
882 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
885 if (ret == LDB_SUCCESS) {
886 ret = ldb_transaction_commit(ldb_ctx);
888 ldb_transaction_cancel(ldb_ctx);
889 if (ldb_ctx->err_string == NULL) {
890 /* no error string was setup by the backend */
891 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
895 talloc_free(mem_ctx);
896 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
901 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
903 PyObject *py_dn1, *py_dn2;
904 struct ldb_dn *dn1, *dn2;
906 struct ldb_context *ldb;
908 PyObject *py_controls = Py_None;
909 struct ldb_control **parsed_controls;
910 struct ldb_context *ldb_ctx;
911 struct ldb_request *req;
913 ldb_ctx = PyLdb_AsLdbContext(self);
915 if (!PyArg_ParseTuple(args, "OO|O", &py_dn1, &py_dn2, &py_controls))
919 mem_ctx = talloc_new(NULL);
920 if (mem_ctx == NULL) {
924 ldb = PyLdb_AsLdbContext(self);
926 if (py_controls == Py_None) {
927 parsed_controls = NULL;
929 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
930 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
931 talloc_free(controls);
935 if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
936 talloc_free(mem_ctx);
940 if (!PyObject_AsDn(mem_ctx, py_dn2, ldb, &dn2)) {
941 talloc_free(mem_ctx);
945 ret = ldb_build_rename_req(&req, ldb_ctx, mem_ctx, dn1, dn2, parsed_controls,
946 NULL, ldb_op_default_callback, NULL);
947 if (ret != LDB_SUCCESS) {
948 PyErr_SetString(PyExc_TypeError, "failed to build request");
949 talloc_free(mem_ctx);
953 /* do request and autostart a transaction */
954 /* Then let's LDB handle the message error in case of pb as they are meaningful */
956 ret = ldb_transaction_start(ldb_ctx);
957 if (ret != LDB_SUCCESS) {
958 talloc_free(mem_ctx);
959 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
962 ret = ldb_request(ldb_ctx, req);
963 if (ret == LDB_SUCCESS) {
964 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
967 if (ret == LDB_SUCCESS) {
968 ret = ldb_transaction_commit(ldb_ctx);
970 ldb_transaction_cancel(ldb_ctx);
971 if (ldb_ctx->err_string == NULL) {
972 /* no error string was setup by the backend */
973 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
977 talloc_free(mem_ctx);
978 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
983 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
986 if (!PyArg_ParseTuple(args, "s", &name))
989 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
994 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
996 char *attribute, *syntax;
999 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
1002 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
1004 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
1009 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
1014 /* We don't want this attached to the 'ldb' any more */
1015 return Py_BuildValue(discard_const_p(char, "(iO)"),
1017 PyLdbMessage_FromMessage(ldif->msg));
1022 static PyObject *py_ldb_write_ldif(PyLdbObject *self, PyObject *args)
1026 struct ldb_ldif ldif;
1029 TALLOC_CTX *mem_ctx;
1031 if (!PyArg_ParseTuple(args, "Oi", &py_msg, &changetype))
1034 if (!PyLdbMessage_Check(py_msg)) {
1035 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for msg");
1039 ldif.msg = PyLdbMessage_AsMessage(py_msg);
1040 ldif.changetype = changetype;
1042 mem_ctx = talloc_new(NULL);
1044 string = ldb_ldif_write_string(PyLdb_AsLdbContext(self), mem_ctx, &ldif);
1046 PyErr_SetString(PyExc_KeyError, "Failed to generate LDIF");
1050 ret = PyString_FromString(string);
1052 talloc_free(mem_ctx);
1057 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
1060 struct ldb_ldif *ldif;
1063 TALLOC_CTX *mem_ctx;
1065 if (!PyArg_ParseTuple(args, "s", &s))
1068 mem_ctx = talloc_new(NULL);
1073 list = PyList_New(0);
1074 while (s && *s != '\0') {
1075 ldif = ldb_ldif_read_string(self->ldb_ctx, &s);
1076 talloc_steal(mem_ctx, ldif);
1078 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
1080 PyErr_SetString(PyExc_ValueError, "unable to parse ldif string");
1081 talloc_free(mem_ctx);
1085 talloc_free(mem_ctx); /* The pyobject already has a reference to the things it needs */
1086 return PyObject_GetIter(list);
1089 static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args)
1092 PyObject *py_msg_old;
1093 PyObject *py_msg_new;
1094 struct ldb_message *diff;
1095 struct ldb_context *ldb;
1098 if (!PyArg_ParseTuple(args, "OO", &py_msg_old, &py_msg_new))
1101 if (!PyLdbMessage_Check(py_msg_old)) {
1102 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for old message");
1106 if (!PyLdbMessage_Check(py_msg_new)) {
1107 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for new message");
1111 ldb = PyLdb_AsLdbContext(self);
1112 ldb_ret = ldb_msg_difference(ldb, ldb,
1113 PyLdbMessage_AsMessage(py_msg_old),
1114 PyLdbMessage_AsMessage(py_msg_new),
1116 if (ldb_ret != LDB_SUCCESS) {
1117 PyErr_SetString(PyExc_RuntimeError, "Failed to generate the Ldb Message diff");
1121 py_ret = PyLdbMessage_FromMessage(diff);
1123 talloc_unlink(ldb, diff);
1128 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
1130 const struct ldb_schema_attribute *a;
1131 struct ldb_val old_val;
1132 struct ldb_val new_val;
1133 TALLOC_CTX *mem_ctx;
1138 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
1141 mem_ctx = talloc_new(NULL);
1143 old_val.data = (uint8_t *)PyString_AsString(val);
1144 old_val.length = PyString_Size(val);
1146 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
1152 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
1153 talloc_free(mem_ctx);
1157 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
1159 talloc_free(mem_ctx);
1164 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
1166 PyObject *py_base = Py_None;
1167 int scope = LDB_SCOPE_DEFAULT;
1169 PyObject *py_attrs = Py_None;
1170 PyObject *py_controls = Py_None;
1171 const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
1173 struct ldb_result *res;
1174 struct ldb_request *req;
1176 struct ldb_context *ldb_ctx;
1177 struct ldb_control **parsed_controls;
1178 struct ldb_dn *base;
1180 TALLOC_CTX *mem_ctx;
1182 /* type "int" rather than "enum" for "scope" is intentional */
1183 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
1184 discard_const_p(char *, kwnames),
1185 &py_base, &scope, &expr, &py_attrs, &py_controls))
1189 mem_ctx = talloc_new(NULL);
1190 if (mem_ctx == NULL) {
1194 ldb_ctx = PyLdb_AsLdbContext(self);
1196 if (py_attrs == Py_None) {
1199 attrs = PyList_AsStringList(mem_ctx, py_attrs, "attrs");
1200 if (attrs == NULL) {
1201 talloc_free(mem_ctx);
1206 if (py_base == Py_None) {
1207 base = ldb_get_default_basedn(ldb_ctx);
1209 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
1215 if (py_controls == Py_None) {
1216 parsed_controls = NULL;
1218 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
1219 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
1220 talloc_free(controls);
1223 res = talloc_zero(mem_ctx, struct ldb_result);
1226 talloc_free(mem_ctx);
1230 ret = ldb_build_search_req(&req, ldb_ctx, mem_ctx,
1237 ldb_search_default_callback,
1240 if (ret != LDB_SUCCESS) {
1241 talloc_free(mem_ctx);
1242 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
1246 talloc_steal(req, attrs);
1248 ret = ldb_request(ldb_ctx, req);
1250 if (ret == LDB_SUCCESS) {
1251 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1254 if (ret != LDB_SUCCESS) {
1255 talloc_free(mem_ctx);
1256 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
1260 py_ret = PyLdbResult_FromResult(res);
1262 talloc_free(mem_ctx);
1267 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
1272 if (!PyArg_ParseTuple(args, "s", &name))
1275 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
1280 /* FIXME: More interpretation */
1285 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
1290 if (!PyArg_ParseTuple(args, "sO", &name, &data))
1293 /* FIXME: More interpretation */
1295 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
1300 static PyObject *py_ldb_modules(PyLdbObject *self)
1302 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
1303 PyObject *ret = PyList_New(0);
1304 struct ldb_module *mod;
1306 for (mod = ldb->modules; mod; mod = mod->next) {
1307 PyList_Append(ret, PyLdbModule_FromModule(mod));
1313 static PyObject *py_ldb_sequence_number(PyLdbObject *self, PyObject *args)
1315 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
1319 if (!PyArg_ParseTuple(args, "i", &type))
1322 /* FIXME: More interpretation */
1324 ret = ldb_sequence_number(ldb, type, &value);
1326 if (ret != LDB_SUCCESS) {
1327 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
1330 return PyLong_FromLongLong(value);
1332 static PyMethodDef py_ldb_methods[] = {
1333 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
1334 "S.set_debug(callback) -> None\n"
1335 "Set callback for LDB debug messages.\n"
1336 "The callback should accept a debug level and debug text." },
1337 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
1338 "S.set_create_perms(mode) -> None\n"
1339 "Set mode to use when creating new LDB files." },
1340 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
1341 "S.set_modules_dir(path) -> None\n"
1342 "Set path LDB should search for modules" },
1343 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
1344 "S.transaction_start() -> None\n"
1345 "Start a new transaction." },
1346 { "transaction_prepare_commit", (PyCFunction)py_ldb_transaction_prepare_commit, METH_NOARGS,
1347 "S.transaction_prepare_commit() -> None\n"
1348 "prepare to commit a new transaction (2-stage commit)." },
1349 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
1350 "S.transaction_commit() -> None\n"
1351 "commit a new transaction." },
1352 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
1353 "S.transaction_cancel() -> None\n"
1354 "cancel a new transaction." },
1355 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
1357 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
1359 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
1361 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
1363 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
1365 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
1366 "S.connect(url, flags=0, options=None) -> None\n"
1367 "Connect to a LDB URL." },
1368 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
1369 "S.modify(message) -> None\n"
1370 "Modify an entry." },
1371 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
1372 "S.add(message) -> None\n"
1374 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
1375 "S.delete(dn) -> None\n"
1376 "Remove an entry." },
1377 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
1378 "S.rename(old_dn, new_dn) -> None\n"
1379 "Rename an entry." },
1380 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
1381 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1382 "Search in a database.\n"
1384 ":param base: Optional base DN to search\n"
1385 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1386 ":param expression: Optional search expression\n"
1387 ":param attrs: Attributes to return (defaults to all)\n"
1388 ":param controls: Optional list of controls\n"
1389 ":return: Iterator over Message objects\n"
1391 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
1393 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
1395 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
1397 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
1398 "S.parse_ldif(ldif) -> iter(messages)\n"
1399 "Parse a string formatted using LDIF." },
1400 { "write_ldif", (PyCFunction)py_ldb_write_ldif, METH_VARARGS,
1401 "S.write_ldif(message, changetype) -> ldif\n"
1402 "Print the message as a string formatted using LDIF." },
1403 { "msg_diff", (PyCFunction)py_ldb_msg_diff, METH_VARARGS,
1404 "S.msg_diff(Message) -> Message\n"
1405 "Return an LDB Message of the difference between two Message objects." },
1406 { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
1407 "S.get_opaque(name) -> value\n"
1408 "Get an opaque value set on this LDB connection. \n"
1409 ":note: The returned value may not be useful in Python."
1411 { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
1412 "S.set_opaque(name, value) -> None\n"
1413 "Set an opaque value on this LDB connection. \n"
1414 ":note: Passing incorrect values may cause crashes." },
1415 { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
1416 "S.modules() -> list\n"
1417 "Return the list of modules on this LDB connection " },
1418 { "sequence_number", (PyCFunction)py_ldb_sequence_number, METH_VARARGS,
1419 "S.sequence_number(type) -> value\n"
1420 "Return the value of the sequence according to the requested type" },
1424 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
1426 PyLdbModuleObject *ret;
1428 ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
1433 ret->mem_ctx = talloc_new(NULL);
1434 ret->mod = talloc_reference(ret->mem_ctx, mod);
1435 return (PyObject *)ret;
1438 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
1440 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
1443 static PyGetSetDef py_ldb_getset[] = {
1444 { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL },
1448 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
1450 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1452 struct ldb_result *result;
1456 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn)) {
1460 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL,
1462 if (ret != LDB_SUCCESS) {
1463 PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
1467 count = result->count;
1469 talloc_free(result);
1472 PyErr_Format(PyExc_RuntimeError,
1473 "Searching for [%s] dn gave %u results!",
1474 ldb_dn_get_linearized(dn),
1482 static PySequenceMethods py_ldb_seq = {
1483 .sq_contains = (objobjproc)py_ldb_contains,
1486 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
1490 ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1495 ret->mem_ctx = talloc_new(NULL);
1496 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1497 return (PyObject *)ret;
1500 static void py_ldb_dealloc(PyLdbObject *self)
1502 talloc_free(self->mem_ctx);
1506 PyTypeObject PyLdb = {
1507 .tp_name = "ldb.Ldb",
1508 .tp_methods = py_ldb_methods,
1509 .tp_repr = (reprfunc)py_ldb_repr,
1510 .tp_new = py_ldb_new,
1511 .tp_init = (initproc)py_ldb_init,
1512 .tp_dealloc = (destructor)py_ldb_dealloc,
1513 .tp_getset = py_ldb_getset,
1514 .tp_getattro = PyObject_GenericGetAttr,
1515 .tp_basicsize = sizeof(PyLdbObject),
1516 .tp_doc = "Connection to a LDB database.",
1517 .tp_as_sequence = &py_ldb_seq,
1518 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1521 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
1523 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1526 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
1528 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1531 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1533 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1537 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1539 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1543 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1545 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1549 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1551 PyObject *py_base, *py_tree, *py_attrs, *py_ret;
1553 struct ldb_request *req;
1554 const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1555 struct ldb_module *mod;
1556 const char * const*attrs;
1558 /* type "int" rather than "enum" for "scope" is intentional */
1559 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
1560 discard_const_p(char *, kwnames),
1561 &py_base, &scope, &py_tree, &py_attrs))
1566 if (py_attrs == Py_None) {
1569 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
1574 ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base),
1575 scope, NULL /* expr */, attrs,
1576 NULL /* controls */, NULL, NULL, NULL);
1578 talloc_steal(req, attrs);
1580 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1582 req->op.search.res = NULL;
1584 ret = mod->ops->search(mod, req);
1586 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1588 py_ret = PyLdbResult_FromResult(req->op.search.res);
1596 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1598 struct ldb_request *req;
1599 PyObject *py_message;
1601 struct ldb_module *mod;
1603 if (!PyArg_ParseTuple(args, "O", &py_message))
1606 req = talloc_zero(NULL, struct ldb_request);
1607 req->operation = LDB_ADD;
1608 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1610 mod = PyLdbModule_AsModule(self);
1611 ret = mod->ops->add(mod, req);
1613 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1618 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
1621 struct ldb_request *req;
1622 PyObject *py_message;
1623 struct ldb_module *mod;
1625 if (!PyArg_ParseTuple(args, "O", &py_message))
1628 req = talloc_zero(NULL, struct ldb_request);
1629 req->operation = LDB_MODIFY;
1630 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1632 mod = PyLdbModule_AsModule(self);
1633 ret = mod->ops->modify(mod, req);
1635 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1640 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
1643 struct ldb_request *req;
1646 if (!PyArg_ParseTuple(args, "O", &py_dn))
1649 req = talloc_zero(NULL, struct ldb_request);
1650 req->operation = LDB_DELETE;
1651 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1653 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1655 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1660 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1663 struct ldb_request *req;
1664 PyObject *py_dn1, *py_dn2;
1666 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1669 req = talloc_zero(NULL, struct ldb_request);
1671 req->operation = LDB_RENAME;
1672 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1673 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1675 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1677 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1682 static PyMethodDef py_ldb_module_methods[] = {
1683 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1684 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1685 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1686 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1687 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1688 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1689 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1690 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1694 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
1696 talloc_free(self->mem_ctx);
1700 PyTypeObject PyLdbModule = {
1701 .tp_name = "ldb.LdbModule",
1702 .tp_methods = py_ldb_module_methods,
1703 .tp_repr = (reprfunc)py_ldb_module_repr,
1704 .tp_str = (reprfunc)py_ldb_module_str,
1705 .tp_basicsize = sizeof(PyLdbModuleObject),
1706 .tp_dealloc = (destructor)py_ldb_module_dealloc,
1707 .tp_flags = Py_TPFLAGS_DEFAULT,
1712 * Create a ldb_message_element from a Python object.
1714 * This will accept any sequence objects that contains strings, or
1717 * A reference to set_obj will be borrowed.
1719 * @param mem_ctx Memory context
1720 * @param set_obj Python object to convert
1721 * @param flags ldb_message_element flags to set
1722 * @param attr_name Name of the attribute
1723 * @return New ldb_message_element, allocated as child of mem_ctx
1725 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1728 const char *attr_name)
1730 struct ldb_message_element *me;
1732 if (PyLdbMessageElement_Check(set_obj)) {
1733 PyLdbMessageElementObject *set_obj_as_me = (PyLdbMessageElementObject *)set_obj;
1734 /* We have to talloc_reference() the memory context, not the pointer which may not actually be it's own context */
1735 if (talloc_reference(mem_ctx, set_obj_as_me->mem_ctx)) {
1736 return PyLdbMessageElement_AsMessageElement(set_obj);
1741 me = talloc(mem_ctx, struct ldb_message_element);
1743 me->name = talloc_strdup(me, attr_name);
1745 if (PyString_Check(set_obj)) {
1747 me->values = talloc_array(me, struct ldb_val, me->num_values);
1748 me->values[0].length = PyString_Size(set_obj);
1749 me->values[0].data = talloc_memdup(me,
1750 (uint8_t *)PyString_AsString(set_obj), me->values[0].length+1);
1751 } else if (PySequence_Check(set_obj)) {
1753 me->num_values = PySequence_Size(set_obj);
1754 me->values = talloc_array(me, struct ldb_val, me->num_values);
1755 for (i = 0; i < me->num_values; i++) {
1756 PyObject *obj = PySequence_GetItem(set_obj, i);
1757 if (!PyString_Check(obj)) {
1758 PyErr_Format(PyExc_TypeError,
1759 "Expected string as element %zd in list", i);
1764 me->values[i].length = PyString_Size(obj);
1765 me->values[i].data = talloc_memdup(me,
1766 (uint8_t *)PyString_AsString(obj), me->values[i].length+1);
1777 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
1778 struct ldb_message_element *me)
1783 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1784 result = PyList_New(me->num_values);
1786 for (i = 0; i < me->num_values; i++) {
1787 PyList_SetItem(result, i,
1788 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1794 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1797 if (!PyArg_ParseTuple(args, "I", &i))
1799 if (i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1802 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1803 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1806 static PyObject *py_ldb_msg_element_flags(PyLdbMessageElementObject *self, PyObject *args)
1808 struct ldb_message_element *el;
1810 el = PyLdbMessageElement_AsMessageElement(self);
1811 return PyInt_FromLong(el->flags);
1814 static PyObject *py_ldb_msg_element_set_flags(PyLdbMessageElementObject *self, PyObject *args)
1817 struct ldb_message_element *el;
1818 if (!PyArg_ParseTuple(args, "i", &flags))
1821 el = PyLdbMessageElement_AsMessageElement(self);
1826 static PyMethodDef py_ldb_msg_element_methods[] = {
1827 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1828 { "set_flags", (PyCFunction)py_ldb_msg_element_set_flags, METH_VARARGS, NULL },
1829 { "flags", (PyCFunction)py_ldb_msg_element_flags, METH_NOARGS, NULL },
1833 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1835 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1838 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1840 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1841 if (idx < 0 || idx >= el->num_values) {
1842 PyErr_SetString(PyExc_IndexError, "Out of range");
1845 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1848 static PySequenceMethods py_ldb_msg_element_seq = {
1849 .sq_length = (lenfunc)py_ldb_msg_element_len,
1850 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1853 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1855 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1856 PyLdbMessageElement_AsMessageElement(other));
1859 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1861 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1864 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
1866 PyLdbMessageElementObject *ret;
1867 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1872 ret->mem_ctx = talloc_new(NULL);
1873 if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1878 return (PyObject *)ret;
1881 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1883 PyObject *py_elements = NULL;
1884 struct ldb_message_element *el;
1887 const char * const kwnames[] = { "elements", "flags", "name", NULL };
1888 PyLdbMessageElementObject *ret;
1889 TALLOC_CTX *mem_ctx;
1891 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois",
1892 discard_const_p(char *, kwnames),
1893 &py_elements, &flags, &name))
1896 mem_ctx = talloc_new(NULL);
1897 if (mem_ctx == NULL) {
1902 el = talloc_zero(mem_ctx, struct ldb_message_element);
1904 if (py_elements != NULL) {
1906 if (PyString_Check(py_elements)) {
1908 el->values = talloc_array(el, struct ldb_val, 1);
1909 el->values[0].length = PyString_Size(py_elements);
1910 el->values[0].data = talloc_memdup(el,
1911 (uint8_t *)PyString_AsString(py_elements), el->values[0].length+1);
1912 } else if (PySequence_Check(py_elements)) {
1913 el->num_values = PySequence_Size(py_elements);
1914 el->values = talloc_array(el, struct ldb_val, el->num_values);
1915 for (i = 0; i < el->num_values; i++) {
1916 PyObject *item = PySequence_GetItem(py_elements, i);
1917 if (!PyString_Check(item)) {
1918 PyErr_Format(PyExc_TypeError,
1919 "Expected string as element %zd in list", i);
1920 talloc_free(mem_ctx);
1923 el->values[i].length = PyString_Size(item);
1924 el->values[i].data = talloc_memdup(el,
1925 (uint8_t *)PyString_AsString(item), el->values[i].length+1);
1928 PyErr_SetString(PyExc_TypeError,
1929 "Expected string or list");
1930 talloc_free(mem_ctx);
1936 el->name = talloc_strdup(el, name);
1938 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1941 talloc_free(mem_ctx);
1945 ret->mem_ctx = mem_ctx;
1947 return (PyObject *)ret;
1950 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1952 char *element_str = NULL;
1954 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1957 for (i = 0; i < el->num_values; i++) {
1958 PyObject *o = py_ldb_msg_element_find(self, i);
1959 if (element_str == NULL)
1960 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1962 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1965 if (element_str != NULL) {
1966 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1967 talloc_free(element_str);
1969 ret = PyString_FromString("MessageElement([])");
1975 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
1977 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1979 if (el->num_values == 1)
1980 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1985 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
1987 talloc_free(self->mem_ctx);
1991 static PyTypeObject PyLdbMessageElement = {
1992 .tp_name = "ldb.MessageElement",
1993 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1994 .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1995 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1996 .tp_str = (reprfunc)py_ldb_msg_element_str,
1997 .tp_methods = py_ldb_msg_element_methods,
1998 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1999 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
2000 .tp_as_sequence = &py_ldb_msg_element_seq,
2001 .tp_new = py_ldb_msg_element_new,
2002 .tp_flags = Py_TPFLAGS_DEFAULT,
2006 static PyObject *py_ldb_msg_from_dict(PyTypeObject *type, PyObject *args)
2011 struct ldb_message *msg;
2012 struct ldb_context *ldb_ctx;
2013 unsigned int mod_flags = LDB_FLAG_MOD_REPLACE;
2015 if (!PyArg_ParseTuple(args, "O!O!|I",
2016 &PyLdb, &py_ldb, &PyDict_Type, &py_dict,
2021 /* mask only flags we are going to use */
2022 mod_flags = LDB_FLAG_MOD_TYPE(mod_flags);
2024 PyErr_SetString(PyExc_ValueError,
2025 "FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE"
2026 " expected as mod_flag value");
2030 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
2032 msg = PyDict_AsMessage(ldb_ctx, py_dict, ldb_ctx, mod_flags);
2037 py_ret = PyLdbMessage_FromMessage(msg);
2039 talloc_unlink(ldb_ctx, msg);
2044 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
2047 if (!PyArg_ParseTuple(args, "s", &name))
2050 ldb_msg_remove_attr(self->msg, name);
2055 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
2057 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2058 Py_ssize_t i, j = 0;
2059 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
2060 if (msg->dn != NULL) {
2061 PyList_SetItem(obj, j, PyString_FromString("dn"));
2064 for (i = 0; i < msg->num_elements; i++) {
2065 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
2071 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
2073 struct ldb_message_element *el;
2075 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2076 if (!PyString_Check(py_name)) {
2077 PyErr_SetNone(PyExc_TypeError);
2080 name = PyString_AsString(py_name);
2081 if (!strcmp(name, "dn"))
2082 return PyLdbDn_FromDn(msg->dn);
2083 el = ldb_msg_find_element(msg, name);
2087 return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg->elements);
2090 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
2092 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
2094 PyErr_SetString(PyExc_KeyError, "No such element");
2100 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
2102 PyObject *name, *ret;
2103 if (!PyArg_ParseTuple(args, "O", &name))
2106 ret = py_ldb_msg_getitem_helper(self, name);
2108 if (PyErr_Occurred())
2115 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
2117 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2118 Py_ssize_t i, j = 0;
2119 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
2120 if (msg->dn != NULL) {
2121 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
2124 for (i = 0; i < msg->num_elements; i++, j++) {
2125 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], msg->elements)));
2130 static PyObject *py_ldb_msg_elements(PyLdbMessageObject *self)
2132 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2134 PyObject *l = PyList_New(msg->num_elements);
2135 for (i = 0; i < msg->num_elements; i++) {
2136 PyList_SetItem(l, i, PyLdbMessageElement_FromMessageElement(&msg->elements[i], msg->elements));
2141 static PyObject *py_ldb_msg_add(PyLdbMessageObject *self, PyObject *args)
2143 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2144 PyObject *py_element;
2146 struct ldb_message_element *el;
2148 if (!PyArg_ParseTuple(args, "O!", &PyLdbMessageElement, &py_element))
2151 el = talloc_reference(msg, PyLdbMessageElement_AsMessageElement(py_element));
2157 ret = ldb_msg_add(msg, el, el->flags);
2158 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
2163 static PyMethodDef py_ldb_msg_methods[] = {
2164 { "from_dict", (PyCFunction)py_ldb_msg_from_dict, METH_CLASS | METH_VARARGS,
2165 "Message.from_dict(ldb, dict, mod_flag=FLAG_MOD_REPLACE) -> ldb.Message\n"
2166 "Class method to create ldb.Message object from Dictionary.\n"
2167 "mod_flag is one of FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE."},
2168 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS,
2169 "S.keys() -> list\n\n"
2170 "Return sequence of all attribute names." },
2171 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS,
2172 "S.remove(name)\n\n"
2173 "Remove all entries for attributes with the specified name."},
2174 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
2175 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
2176 { "elements", (PyCFunction)py_ldb_msg_elements, METH_NOARGS, NULL },
2177 { "add", (PyCFunction)py_ldb_msg_add, METH_VARARGS,
2178 "S.append(element)\n\n"
2179 "Add an element to this message." },
2183 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
2185 PyObject *list, *iter;
2187 list = py_ldb_msg_keys(self);
2188 iter = PyObject_GetIter(list);
2193 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
2197 if (!PyString_Check(name)) {
2198 PyErr_SetNone(PyExc_TypeError);
2202 attr_name = PyString_AsString(name);
2203 if (value == NULL) {
2205 ldb_msg_remove_attr(self->msg, attr_name);
2207 struct ldb_message_element *el = PyObject_AsMessageElement(self->msg,
2208 value, 0, attr_name);
2211 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
2212 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
2217 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
2219 return PyLdbMessage_AsMessage(self)->num_elements;
2222 static PyMappingMethods py_ldb_msg_mapping = {
2223 .mp_length = (lenfunc)py_ldb_msg_length,
2224 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
2225 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
2228 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2230 const char * const kwnames[] = { "dn", NULL };
2231 struct ldb_message *ret;
2232 TALLOC_CTX *mem_ctx;
2233 PyObject *pydn = NULL;
2234 PyLdbMessageObject *py_ret;
2236 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
2237 discard_const_p(char *, kwnames),
2241 mem_ctx = talloc_new(NULL);
2242 if (mem_ctx == NULL) {
2247 ret = ldb_msg_new(mem_ctx);
2249 talloc_free(mem_ctx);
2256 if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) {
2257 talloc_free(mem_ctx);
2260 ret->dn = talloc_reference(ret, dn);
2263 py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
2264 if (py_ret == NULL) {
2266 talloc_free(mem_ctx);
2270 py_ret->mem_ctx = mem_ctx;
2272 return (PyObject *)py_ret;
2275 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
2277 PyLdbMessageObject *ret;
2279 ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
2284 ret->mem_ctx = talloc_new(NULL);
2285 ret->msg = talloc_reference(ret->mem_ctx, msg);
2286 return (PyObject *)ret;
2289 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
2291 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2292 return PyLdbDn_FromDn(msg->dn);
2295 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
2297 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2298 if (!PyLdbDn_Check(value)) {
2299 PyErr_SetNone(PyExc_TypeError);
2303 msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value));
2307 static PyGetSetDef py_ldb_msg_getset[] = {
2308 { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
2312 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
2314 PyObject *dict = PyDict_New(), *ret;
2315 if (PyDict_Update(dict, (PyObject *)self) != 0)
2317 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
2322 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
2324 talloc_free(self->mem_ctx);
2328 static int py_ldb_msg_compare(PyLdbMessageObject *py_msg1,
2329 PyLdbMessageObject *py_msg2)
2331 struct ldb_message *msg1 = PyLdbMessage_AsMessage(py_msg1),
2332 *msg2 = PyLdbMessage_AsMessage(py_msg2);
2336 if ((msg1->dn != NULL) || (msg2->dn != NULL)) {
2337 ret = ldb_dn_compare(msg1->dn, msg2->dn);
2343 ret = msg1->num_elements - msg2->num_elements;
2348 for (i = 0; i < msg1->num_elements; i++) {
2349 ret = ldb_msg_element_compare_name(&msg1->elements[i],
2350 &msg2->elements[i]);
2355 ret = ldb_msg_element_compare(&msg1->elements[i],
2356 &msg2->elements[i]);
2365 PyTypeObject PyLdbMessage = {
2366 .tp_name = "ldb.Message",
2367 .tp_methods = py_ldb_msg_methods,
2368 .tp_getset = py_ldb_msg_getset,
2369 .tp_as_mapping = &py_ldb_msg_mapping,
2370 .tp_basicsize = sizeof(PyLdbMessageObject),
2371 .tp_dealloc = (destructor)py_ldb_msg_dealloc,
2372 .tp_new = py_ldb_msg_new,
2373 .tp_repr = (reprfunc)py_ldb_msg_repr,
2374 .tp_flags = Py_TPFLAGS_DEFAULT,
2375 .tp_iter = (getiterfunc)py_ldb_msg_iter,
2376 .tp_compare = (cmpfunc)py_ldb_msg_compare,
2379 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
2381 PyLdbTreeObject *ret;
2383 ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
2389 ret->mem_ctx = talloc_new(NULL);
2390 ret->tree = talloc_reference(ret->mem_ctx, tree);
2391 return (PyObject *)ret;
2394 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
2396 talloc_free(self->mem_ctx);
2400 PyTypeObject PyLdbTree = {
2401 .tp_name = "ldb.Tree",
2402 .tp_basicsize = sizeof(PyLdbTreeObject),
2403 .tp_dealloc = (destructor)py_ldb_tree_dealloc,
2404 .tp_flags = Py_TPFLAGS_DEFAULT,
2408 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
2410 PyObject *py_ldb = (PyObject *)mod->private_data;
2411 PyObject *py_result, *py_base, *py_attrs, *py_tree;
2413 py_base = PyLdbDn_FromDn(req->op.search.base);
2415 if (py_base == NULL)
2416 return LDB_ERR_OPERATIONS_ERROR;
2418 py_tree = PyLdbTree_FromTree(req->op.search.tree);
2420 if (py_tree == NULL)
2421 return LDB_ERR_OPERATIONS_ERROR;
2423 if (req->op.search.attrs == NULL) {
2427 for (len = 0; req->op.search.attrs[len]; len++);
2428 py_attrs = PyList_New(len);
2429 for (i = 0; i < len; i++)
2430 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
2433 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
2434 discard_const_p(char, "OiOO"),
2435 py_base, req->op.search.scope, py_tree, py_attrs);
2437 Py_DECREF(py_attrs);
2441 if (py_result == NULL) {
2442 return LDB_ERR_PYTHON_EXCEPTION;
2445 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
2446 if (req->op.search.res == NULL) {
2447 return LDB_ERR_PYTHON_EXCEPTION;
2450 Py_DECREF(py_result);
2455 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
2457 PyObject *py_ldb = (PyObject *)mod->private_data;
2458 PyObject *py_result, *py_msg;
2460 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
2462 if (py_msg == NULL) {
2463 return LDB_ERR_OPERATIONS_ERROR;
2466 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
2467 discard_const_p(char, "O"),
2472 if (py_result == NULL) {
2473 return LDB_ERR_PYTHON_EXCEPTION;
2476 Py_DECREF(py_result);
2481 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
2483 PyObject *py_ldb = (PyObject *)mod->private_data;
2484 PyObject *py_result, *py_msg;
2486 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
2488 if (py_msg == NULL) {
2489 return LDB_ERR_OPERATIONS_ERROR;
2492 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
2493 discard_const_p(char, "O"),
2498 if (py_result == NULL) {
2499 return LDB_ERR_PYTHON_EXCEPTION;
2502 Py_DECREF(py_result);
2507 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
2509 PyObject *py_ldb = (PyObject *)mod->private_data;
2510 PyObject *py_result, *py_dn;
2512 py_dn = PyLdbDn_FromDn(req->op.del.dn);
2515 return LDB_ERR_OPERATIONS_ERROR;
2517 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
2518 discard_const_p(char, "O"),
2521 if (py_result == NULL) {
2522 return LDB_ERR_PYTHON_EXCEPTION;
2525 Py_DECREF(py_result);
2530 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
2532 PyObject *py_ldb = (PyObject *)mod->private_data;
2533 PyObject *py_result, *py_olddn, *py_newdn;
2535 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
2537 if (py_olddn == NULL)
2538 return LDB_ERR_OPERATIONS_ERROR;
2540 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
2542 if (py_newdn == NULL)
2543 return LDB_ERR_OPERATIONS_ERROR;
2545 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
2546 discard_const_p(char, "OO"),
2547 py_olddn, py_newdn);
2549 Py_DECREF(py_olddn);
2550 Py_DECREF(py_newdn);
2552 if (py_result == NULL) {
2553 return LDB_ERR_PYTHON_EXCEPTION;
2556 Py_DECREF(py_result);
2561 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
2563 PyObject *py_ldb = (PyObject *)mod->private_data;
2564 PyObject *py_result;
2566 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
2567 discard_const_p(char, ""));
2569 return LDB_ERR_OPERATIONS_ERROR;
2572 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
2574 PyObject *py_ldb = (PyObject *)mod->private_data;
2575 PyObject *py_result;
2577 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
2578 discard_const_p(char, ""));
2580 return LDB_ERR_OPERATIONS_ERROR;
2583 static int py_module_start_transaction(struct ldb_module *mod)
2585 PyObject *py_ldb = (PyObject *)mod->private_data;
2586 PyObject *py_result;
2588 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
2589 discard_const_p(char, ""));
2591 if (py_result == NULL) {
2592 return LDB_ERR_PYTHON_EXCEPTION;
2595 Py_DECREF(py_result);
2600 static int py_module_end_transaction(struct ldb_module *mod)
2602 PyObject *py_ldb = (PyObject *)mod->private_data;
2603 PyObject *py_result;
2605 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
2606 discard_const_p(char, ""));
2608 if (py_result == NULL) {
2609 return LDB_ERR_PYTHON_EXCEPTION;
2612 Py_DECREF(py_result);
2617 static int py_module_del_transaction(struct ldb_module *mod)
2619 PyObject *py_ldb = (PyObject *)mod->private_data;
2620 PyObject *py_result;
2622 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
2623 discard_const_p(char, ""));
2625 if (py_result == NULL) {
2626 return LDB_ERR_PYTHON_EXCEPTION;
2629 Py_DECREF(py_result);
2634 static int py_module_destructor(struct ldb_module *mod)
2636 Py_DECREF((PyObject *)mod->private_data);
2640 static int py_module_init(struct ldb_module *mod)
2642 PyObject *py_class = (PyObject *)mod->ops->private_data;
2643 PyObject *py_result, *py_next, *py_ldb;
2645 py_ldb = PyLdb_FromLdbContext(mod->ldb);
2648 return LDB_ERR_OPERATIONS_ERROR;
2650 py_next = PyLdbModule_FromModule(mod->next);
2652 if (py_next == NULL)
2653 return LDB_ERR_OPERATIONS_ERROR;
2655 py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
2658 if (py_result == NULL) {
2659 return LDB_ERR_PYTHON_EXCEPTION;
2662 mod->private_data = py_result;
2664 talloc_set_destructor(mod, py_module_destructor);
2666 return ldb_next_init(mod);
2669 static PyObject *py_register_module(PyObject *module, PyObject *args)
2672 struct ldb_module_ops *ops;
2675 if (!PyArg_ParseTuple(args, "O", &input))
2678 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
2684 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name"))));
2687 ops->private_data = input;
2688 ops->init_context = py_module_init;
2689 ops->search = py_module_search;
2690 ops->add = py_module_add;
2691 ops->modify = py_module_modify;
2692 ops->del = py_module_del;
2693 ops->rename = py_module_rename;
2694 ops->request = py_module_request;
2695 ops->extended = py_module_extended;
2696 ops->start_transaction = py_module_start_transaction;
2697 ops->end_transaction = py_module_end_transaction;
2698 ops->del_transaction = py_module_del_transaction;
2700 ret = ldb_register_module(ops);
2702 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
2707 static PyObject *py_timestring(PyObject *module, PyObject *args)
2709 /* most times "time_t" is a signed integer type with 32 or 64 bit:
2710 * http://stackoverflow.com/questions/471248/what-is-ultimately-a-time-t-typedef-to */
2714 if (!PyArg_ParseTuple(args, "l", &t_val))
2716 tresult = ldb_timestring(NULL, (time_t) t_val);
2717 ret = PyString_FromString(tresult);
2718 talloc_free(tresult);
2722 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
2725 if (!PyArg_ParseTuple(args, "s", &str))
2728 return PyInt_FromLong(ldb_string_to_time(str));
2731 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
2734 if (!PyArg_ParseTuple(args, "s", &name))
2736 return PyBool_FromLong(ldb_valid_attr_name(name));
2739 static PyMethodDef py_ldb_global_methods[] = {
2740 { "register_module", py_register_module, METH_VARARGS,
2741 "S.register_module(module) -> None\n"
2742 "Register a LDB module."},
2743 { "timestring", py_timestring, METH_VARARGS,
2744 "S.timestring(int) -> string\n"
2745 "Generate a LDAP time string from a UNIX timestamp" },
2746 { "string_to_time", py_string_to_time, METH_VARARGS,
2747 "S.string_to_time(string) -> int\n"
2748 "Parse a LDAP time string into a UNIX timestamp." },
2749 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2750 "S.valid_attr_name(name) -> bool\n"
2751 "Check whether the supplied name is a valid attribute name." },
2752 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2761 if (PyType_Ready(&PyLdbDn) < 0)
2764 if (PyType_Ready(&PyLdbMessage) < 0)
2767 if (PyType_Ready(&PyLdbMessageElement) < 0)
2770 if (PyType_Ready(&PyLdb) < 0)
2773 if (PyType_Ready(&PyLdbModule) < 0)
2776 if (PyType_Ready(&PyLdbTree) < 0)
2779 m = Py_InitModule3("ldb", py_ldb_global_methods,
2780 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2784 PyModule_AddObject(m, "SEQ_HIGHEST_SEQ", PyInt_FromLong(LDB_SEQ_HIGHEST_SEQ));
2785 PyModule_AddObject(m, "SEQ_HIGHEST_TIMESTAMP", PyInt_FromLong(LDB_SEQ_HIGHEST_TIMESTAMP));
2786 PyModule_AddObject(m, "SEQ_NEXT", PyInt_FromLong(LDB_SEQ_NEXT));
2787 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2788 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2789 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2790 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2792 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2793 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2794 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2795 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2797 PyModule_AddObject(m, "FLAG_MOD_ADD", PyInt_FromLong(LDB_FLAG_MOD_ADD));
2798 PyModule_AddObject(m, "FLAG_MOD_REPLACE", PyInt_FromLong(LDB_FLAG_MOD_REPLACE));
2799 PyModule_AddObject(m, "FLAG_MOD_DELETE", PyInt_FromLong(LDB_FLAG_MOD_DELETE));
2801 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2802 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2803 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2804 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2805 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2806 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2807 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2808 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2809 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2810 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2811 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2812 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2813 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2814 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2815 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2816 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2817 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2818 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2819 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2820 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2821 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2822 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2823 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2824 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2825 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2826 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2827 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2828 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2829 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2830 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2831 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2832 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2833 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2834 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2835 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2836 PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2837 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2838 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2839 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2841 PyModule_AddObject(m, "FLG_RDONLY", PyInt_FromLong(LDB_FLG_RDONLY));
2842 PyModule_AddObject(m, "FLG_NOSYNC", PyInt_FromLong(LDB_FLG_NOSYNC));
2843 PyModule_AddObject(m, "FLG_RECONNECT", PyInt_FromLong(LDB_FLG_RECONNECT));
2844 PyModule_AddObject(m, "FLG_NOMMAP", PyInt_FromLong(LDB_FLG_NOMMAP));
2846 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2848 PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL);
2849 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2852 Py_INCREF(&PyLdbDn);
2853 Py_INCREF(&PyLdbModule);
2854 Py_INCREF(&PyLdbMessage);
2855 Py_INCREF(&PyLdbMessageElement);
2856 Py_INCREF(&PyLdbTree);
2858 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2859 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2860 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2861 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2862 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2863 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);
2865 PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));