2 Unix SMB/CIFS implementation.
4 Python interface to ldb.
6 Copyright (C) 2005,2006 Tim Potter <tpot@samba.org>
7 Copyright (C) 2006 Simo Sorce <idra@samba.org>
8 Copyright (C) 2007-2009 Jelmer Vernooij <jelmer@samba.org>
10 ** NOTE! The following LGPL license applies to the ldb
11 ** library. This does NOT imply that all of Samba is released
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
29 #include "ldb_private.h"
33 /* There's no Py_ssize_t in 2.4, apparently */
34 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
35 typedef int Py_ssize_t;
36 typedef inquiry lenfunc;
37 typedef intargfunc ssizeargfunc;
40 #ifndef Py_RETURN_NONE
41 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
44 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
46 if (ret == LDB_ERR_PYTHON_EXCEPTION)
47 return; /* Python exception should already be set, just keep that */
49 PyErr_SetObject(error,
50 Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
51 ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
54 static PyObject *PyExc_LdbError;
56 PyAPI_DATA(PyTypeObject) PyLdbMessage;
57 PyAPI_DATA(PyTypeObject) PyLdbModule;
58 PyAPI_DATA(PyTypeObject) PyLdbDn;
59 PyAPI_DATA(PyTypeObject) PyLdb;
60 PyAPI_DATA(PyTypeObject) PyLdbMessageElement;
61 PyAPI_DATA(PyTypeObject) PyLdbTree;
63 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
64 struct ldb_message_element *el,
67 struct ldb_val new_val;
68 TALLOC_CTX *mem_ctx = talloc_new(NULL);
73 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
81 * Obtain a ldb DN from a Python object.
83 * @param mem_ctx Memory context
84 * @param object Python object
85 * @param ldb_ctx LDB context
86 * @return Whether or not the conversion succeeded
88 bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object,
89 struct ldb_context *ldb_ctx, struct ldb_dn **dn)
93 if (ldb_ctx != NULL && PyString_Check(object)) {
94 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
99 if (PyLdbDn_Check(object)) {
100 *dn = PyLdbDn_AsDn(object);
104 PyErr_SetString(PyExc_TypeError, "Expected DN");
109 * Create a Python object from a ldb_result.
111 * @param result LDB result to convert
112 * @return Python object with converted result (a list object)
114 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
118 if (result == NULL) {
121 ret = PyList_New(result->count);
122 for (i = 0; i < result->count; i++) {
123 PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i])
130 * Create a LDB Result from a Python object.
131 * If conversion fails, NULL will be returned and a Python exception set.
133 * @param mem_ctx Memory context in which to allocate the LDB Result
134 * @param obj Python object to convert
135 * @return a ldb_result, or NULL if the conversion failed
137 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx,
140 struct ldb_result *res;
146 res = talloc_zero(mem_ctx, struct ldb_result);
147 res->count = PyList_Size(obj);
148 res->msgs = talloc_array(res, struct ldb_message *, res->count);
149 for (i = 0; i < res->count; i++) {
150 PyObject *item = PyList_GetItem(obj, i);
151 res->msgs[i] = PyLdbMessage_AsMessage(item);
156 static PyObject *py_ldb_dn_validate(PyLdbDnObject *self)
158 return PyBool_FromLong(ldb_dn_validate(self->dn));
161 static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self)
163 return PyBool_FromLong(ldb_dn_is_valid(self->dn));
166 static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self)
168 return PyBool_FromLong(ldb_dn_is_special(self->dn));
171 static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self)
173 return PyBool_FromLong(ldb_dn_is_null(self->dn));
176 static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self)
178 return PyString_FromString(ldb_dn_get_casefold(self->dn));
181 static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self)
183 return PyString_FromString(ldb_dn_get_linearized(self->dn));
186 static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self)
188 return PyString_FromString(ldb_dn_canonical_string(self->dn, self->dn));
191 static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self)
193 return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn));
196 static PyObject *py_ldb_dn_repr(PyLdbDnObject *self)
198 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn))));
201 static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args)
205 if (!PyArg_ParseTuple(args, "s", &name))
208 return ldb_dn_check_special(self->dn, name)?Py_True:Py_False;
211 static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
213 return ldb_dn_compare(dn1->dn, dn2->dn);
216 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
218 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
219 struct ldb_dn *parent;
220 PyLdbDnObject *py_ret;
221 TALLOC_CTX *mem_ctx = talloc_new(NULL);
223 parent = ldb_dn_get_parent(mem_ctx, dn);
224 if (parent == NULL) {
225 talloc_free(mem_ctx);
229 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
230 if (py_ret == NULL) {
232 talloc_free(mem_ctx);
235 py_ret->mem_ctx = mem_ctx;
237 return (PyObject *)py_ret;
240 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
242 static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
245 struct ldb_dn *dn, *other;
246 if (!PyArg_ParseTuple(args, "O", &py_other))
249 dn = PyLdbDn_AsDn((PyObject *)self);
251 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
254 return ldb_dn_add_child(dn, other)?Py_True:Py_False;
257 static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
260 struct ldb_dn *other, *dn;
261 if (!PyArg_ParseTuple(args, "O", &py_other))
264 dn = PyLdbDn_AsDn((PyObject *)self);
266 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
269 return ldb_dn_add_base(dn, other)?Py_True:Py_False;
272 static PyMethodDef py_ldb_dn_methods[] = {
273 { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS,
274 "S.validate() -> bool\n"
275 "Validate DN is correct." },
276 { "is_valid", (PyCFunction)py_ldb_dn_is_valid, METH_NOARGS,
277 "S.is_valid() -> bool\n" },
278 { "is_special", (PyCFunction)py_ldb_dn_is_special, METH_NOARGS,
279 "S.is_special() -> bool\n"
280 "Check whether this is a special LDB DN." },
281 { "is_null", (PyCFunction)py_ldb_dn_is_null, METH_NOARGS,
282 "Check whether this is a null DN." },
283 { "get_casefold", (PyCFunction)py_ldb_dn_get_casefold, METH_NOARGS,
285 { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS,
287 { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
288 "S.canonical_str() -> string\n"
289 "Canonical version of this DN (like a posix path)." },
290 { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
291 "S.canonical_ex_str() -> string\n"
292 "Canonical version of this DN (like a posix path, with terminating newline)." },
293 { "check_special", (PyCFunction)py_ldb_dn_is_special, METH_VARARGS,
295 { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS,
297 "Get the parent for this DN." },
298 { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS,
299 "S.add_child(dn) -> None\n"
300 "Add a child DN to this DN." },
301 { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
302 "S.add_base(dn) -> None\n"
303 "Add a base DN to this DN." },
304 { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
309 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
311 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
314 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
316 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self),
318 PyLdbDnObject *py_ret;
320 if (!PyObject_AsDn(NULL, py_other, NULL, &other))
323 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
324 if (py_ret == NULL) {
328 py_ret->mem_ctx = talloc_new(NULL);
329 py_ret->dn = ldb_dn_copy(py_ret->mem_ctx, dn);
330 ldb_dn_add_child(py_ret->dn, other);
331 return (PyObject *)py_ret;
334 static PySequenceMethods py_ldb_dn_seq = {
335 .sq_length = (lenfunc)py_ldb_dn_len,
336 .sq_concat = (binaryfunc)py_ldb_dn_concat,
339 static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
344 struct ldb_context *ldb_ctx;
346 PyLdbDnObject *py_ret;
347 const char * const kwnames[] = { "ldb", "dn", NULL };
349 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os",
350 discard_const_p(char *, kwnames),
354 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
356 mem_ctx = talloc_new(NULL);
357 if (mem_ctx == NULL) {
362 ret = ldb_dn_new(mem_ctx, ldb_ctx, str);
364 if (ret == NULL || !ldb_dn_validate(ret)) {
365 talloc_free(mem_ctx);
366 PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
370 py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0);
372 talloc_free(mem_ctx);
376 py_ret->mem_ctx = mem_ctx;
378 return (PyObject *)py_ret;
381 PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
383 PyLdbDnObject *py_ret;
389 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
390 if (py_ret == NULL) {
394 py_ret->mem_ctx = talloc_new(NULL);
395 py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
396 return (PyObject *)py_ret;
399 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
401 talloc_free(self->mem_ctx);
402 self->ob_type->tp_free(self);
405 PyTypeObject PyLdbDn = {
407 .tp_methods = py_ldb_dn_methods,
408 .tp_str = (reprfunc)py_ldb_dn_get_linearized,
409 .tp_repr = (reprfunc)py_ldb_dn_repr,
410 .tp_compare = (cmpfunc)py_ldb_dn_compare,
411 .tp_as_sequence = &py_ldb_dn_seq,
412 .tp_doc = "A LDB distinguished name.",
413 .tp_new = py_ldb_dn_new,
414 .tp_dealloc = (destructor)py_ldb_dn_dealloc,
415 .tp_basicsize = sizeof(PyLdbObject),
416 .tp_flags = Py_TPFLAGS_DEFAULT,
420 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
421 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
423 PyObject *fn = (PyObject *)context;
424 PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap));
427 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
431 if (!PyArg_ParseTuple(args, "O", &cb))
435 /* FIXME: Where do we DECREF cb ? */
436 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
441 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
444 if (!PyArg_ParseTuple(args, "I", &perms))
447 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
452 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
455 if (!PyArg_ParseTuple(args, "s", &modules_dir))
458 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
463 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
465 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
469 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
471 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
475 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
477 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
481 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
483 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
487 static PyObject *py_ldb_repr(PyLdbObject *self)
489 return PyString_FromFormat("<ldb connection>");
492 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
494 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
497 return PyLdbDn_FromDn(dn);
501 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
503 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
506 return PyLdbDn_FromDn(dn);
509 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
511 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
514 return PyLdbDn_FromDn(dn);
517 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
519 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
522 return PyLdbDn_FromDn(dn);
525 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list,
526 const char *paramname)
530 if (!PyList_Check(list)) {
531 PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
534 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
535 for (i = 0; i < PyList_Size(list); i++) {
536 PyObject *item = PyList_GetItem(list, i);
537 if (!PyString_Check(item)) {
538 PyErr_Format(PyExc_TypeError, "%s should be strings", paramname);
541 ret[i] = talloc_strndup(ret, PyString_AsString(item),
542 PyString_Size(item));
548 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
550 const char * const kwnames[] = { "url", "flags", "options", NULL };
552 PyObject *py_options = Py_None;
553 const char **options;
556 struct ldb_context *ldb;
558 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__",
559 discard_const_p(char *, kwnames),
560 &url, &flags, &py_options))
563 ldb = PyLdb_AsLdbContext(self);
565 if (py_options == Py_None) {
568 options = PyList_AsStringList(ldb, py_options, "options");
574 ret = ldb_connect(ldb, url, flags, options);
575 if (ret != LDB_SUCCESS) {
576 PyErr_SetLdbError(PyExc_LdbError, ret, ldb);
581 talloc_free(options);
585 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
588 struct ldb_context *ldb;
589 ret = (PyLdbObject *)type->tp_alloc(type, 0);
594 ret->mem_ctx = talloc_new(NULL);
595 ldb = ldb_init(ret->mem_ctx, NULL);
603 return (PyObject *)ret;
606 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
610 PyObject *py_options = Py_None;
612 const char **options;
613 const char * const kwnames[] = { "url", "flags", "options", NULL };
615 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO",
616 discard_const_p(char *, kwnames),
617 &url, &flags, &py_options))
620 if (py_options == Py_None) {
623 options = PyList_AsStringList(NULL, py_options, "options");
628 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
629 talloc_free(options);
631 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
636 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
640 if (!PyArg_ParseTuple(args, "O", &py_msg))
643 if (!PyLdbMessage_Check(py_msg)) {
644 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
648 ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
649 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
654 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
658 Py_ssize_t dict_pos, msg_pos;
659 struct ldb_message_element *msgel;
660 struct ldb_message *msg;
661 PyObject *key, *value;
664 if (!PyArg_ParseTuple(args, "O", &py_msg))
667 mem_ctx = talloc_new(NULL);
669 if (PyDict_Check(py_msg)) {
670 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
671 msg = ldb_msg_new(mem_ctx);
672 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
673 msg_pos = dict_pos = 0;
675 if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
676 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
677 talloc_free(mem_ctx);
680 if (msg->dn == NULL) {
681 PyErr_SetString(PyExc_TypeError, "dn set but not found");
682 talloc_free(mem_ctx);
687 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
688 char *key_str = PyString_AsString(key);
689 if (strcmp(key_str, "dn") != 0) {
690 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
692 PyErr_SetString(PyExc_TypeError, "unable to import element");
693 talloc_free(mem_ctx);
696 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
701 if (msg->dn == NULL) {
702 PyErr_SetString(PyExc_TypeError, "no dn set");
703 talloc_free(mem_ctx);
707 msg->num_elements = msg_pos;
709 msg = PyLdbMessage_AsMessage(py_msg);
712 ret = ldb_add(PyLdb_AsLdbContext(self), msg);
713 talloc_free(mem_ctx);
714 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
719 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
724 struct ldb_context *ldb;
725 if (!PyArg_ParseTuple(args, "O", &py_dn))
728 ldb = PyLdb_AsLdbContext(self);
730 if (!PyObject_AsDn(NULL, py_dn, ldb, &dn))
733 ret = ldb_delete(ldb, dn);
734 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
739 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
741 PyObject *py_dn1, *py_dn2;
742 struct ldb_dn *dn1, *dn2;
744 struct ldb_context *ldb;
746 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
749 mem_ctx = talloc_new(NULL);
750 if (mem_ctx == NULL) {
754 ldb = PyLdb_AsLdbContext(self);
755 if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
756 talloc_free(mem_ctx);
760 if (!PyObject_AsDn(mem_ctx, py_dn2, ldb, &dn2)) {
761 talloc_free(mem_ctx);
765 ret = ldb_rename(ldb, dn1, dn2);
766 talloc_free(mem_ctx);
767 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
772 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
775 if (!PyArg_ParseTuple(args, "s", &name))
778 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
783 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
785 char *attribute, *syntax;
788 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
791 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
793 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
798 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
803 /* We don't want this attached to the 'ldb' any more */
804 return Py_BuildValue(discard_const_p(char, "(iO)"),
806 PyLdbMessage_FromMessage(ldif->msg));
811 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
814 struct ldb_ldif *ldif;
819 if (!PyArg_ParseTuple(args, "s", &s))
822 mem_ctx = talloc_new(NULL);
827 list = PyList_New(0);
828 while (s && *s != '\0') {
829 ldif = ldb_ldif_read_string(self->ldb_ctx, &s);
830 talloc_steal(mem_ctx, ldif);
832 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
834 PyErr_SetString(PyExc_ValueError, "unable to parse ldif string");
835 talloc_free(mem_ctx);
839 talloc_free(mem_ctx); /* The pyobject already has a reference to the things it needs */
840 return PyObject_GetIter(list);
843 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
845 const struct ldb_schema_attribute *a;
846 struct ldb_val old_val;
847 struct ldb_val new_val;
853 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
856 mem_ctx = talloc_new(NULL);
858 old_val.data = (uint8_t *)PyString_AsString(val);
859 old_val.length = PyString_Size(val);
861 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
867 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
868 talloc_free(mem_ctx);
872 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
874 talloc_free(mem_ctx);
879 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
881 PyObject *py_base = Py_None;
882 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
884 PyObject *py_attrs = Py_None;
885 PyObject *py_controls = Py_None;
886 const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
888 struct ldb_result *res;
889 struct ldb_request *req;
891 struct ldb_context *ldb_ctx;
892 struct ldb_control **parsed_controls;
896 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
897 discard_const_p(char *, kwnames),
898 &py_base, &scope, &expr, &py_attrs, &py_controls))
901 ldb_ctx = PyLdb_AsLdbContext(self);
903 if (py_attrs == Py_None) {
906 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
911 if (py_base == Py_None) {
912 base = ldb_get_default_basedn(ldb_ctx);
914 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
920 if (py_controls == Py_None) {
921 parsed_controls = NULL;
923 const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
924 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
925 talloc_free(controls);
928 res = talloc_zero(ldb_ctx, struct ldb_result);
935 ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
942 ldb_search_default_callback,
945 talloc_steal(req, attrs);
947 if (ret != LDB_SUCCESS) {
949 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
953 ret = ldb_request(ldb_ctx, req);
955 if (ret == LDB_SUCCESS) {
956 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
961 if (ret != LDB_SUCCESS) {
963 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
967 py_ret = PyLdbResult_FromResult(res);
974 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
979 if (!PyArg_ParseTuple(args, "s", &name))
982 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
987 /* FIXME: More interpretation */
992 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
997 if (!PyArg_ParseTuple(args, "sO", &name, &data))
1000 /* FIXME: More interpretation */
1002 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
1007 static PyObject *py_ldb_modules(PyLdbObject *self)
1009 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
1010 PyObject *ret = PyList_New(0);
1011 struct ldb_module *mod;
1013 for (mod = ldb->modules; mod; mod = mod->next) {
1014 PyList_Append(ret, PyLdbModule_FromModule(mod));
1020 static PyMethodDef py_ldb_methods[] = {
1021 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
1022 "S.set_debug(callback) -> None\n"
1023 "Set callback for LDB debug messages.\n"
1024 "The callback should accept a debug level and debug text." },
1025 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
1026 "S.set_create_perms(mode) -> None\n"
1027 "Set mode to use when creating new LDB files." },
1028 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
1029 "S.set_modules_dir(path) -> None\n"
1030 "Set path LDB should search for modules" },
1031 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
1032 "S.transaction_start() -> None\n"
1033 "Start a new transaction." },
1034 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
1035 "S.transaction_commit() -> None\n"
1036 "commit a new transaction." },
1037 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
1038 "S.transaction_cancel() -> None\n"
1039 "cancel a new transaction." },
1040 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
1042 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
1044 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
1046 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
1048 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
1050 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
1051 "S.connect(url, flags=0, options=None) -> None\n"
1052 "Connect to a LDB URL." },
1053 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
1054 "S.modify(message) -> None\n"
1055 "Modify an entry." },
1056 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
1057 "S.add(message) -> None\n"
1059 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
1060 "S.delete(dn) -> None\n"
1061 "Remove an entry." },
1062 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
1063 "S.rename(old_dn, new_dn) -> None\n"
1064 "Rename an entry." },
1065 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
1066 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1067 "Search in a database.\n"
1069 ":param base: Optional base DN to search\n"
1070 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1071 ":param expression: Optional search expression\n"
1072 ":param attrs: Attributes to return (defaults to all)\n"
1073 ":param controls: Optional list of controls\n"
1074 ":return: Iterator over Message objects\n"
1076 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
1078 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
1080 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
1082 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
1083 "S.parse_ldif(ldif) -> iter(messages)\n"
1084 "Parse a string formatted using LDIF." },
1085 { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
1086 "S.get_opaque(name) -> value\n"
1087 "Get an opaque value set on this LDB connection. \n"
1088 ":note: The returned value may not be useful in Python."
1090 { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
1091 "S.set_opaque(name, value) -> None\n"
1092 "Set an opaque value on this LDB connection. \n"
1093 ":note: Passing incorrect values may cause crashes." },
1094 { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
1095 "S.modules() -> list\n"
1096 "Return the list of modules on this LDB connection " },
1100 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
1102 PyLdbModuleObject *ret;
1104 ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
1109 ret->mem_ctx = talloc_new(NULL);
1110 ret->mod = talloc_reference(ret->mem_ctx, mod);
1111 return (PyObject *)ret;
1114 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
1116 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
1119 static PyGetSetDef py_ldb_getset[] = {
1120 { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL },
1124 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
1126 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1128 struct ldb_result *result;
1132 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
1135 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
1136 if (ret != LDB_SUCCESS) {
1137 PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
1141 count = result->count;
1143 talloc_free(result);
1148 static PySequenceMethods py_ldb_seq = {
1149 .sq_contains = (objobjproc)py_ldb_contains,
1152 PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
1156 ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1161 ret->mem_ctx = talloc_new(NULL);
1162 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1163 return (PyObject *)ret;
1166 static void py_ldb_dealloc(PyLdbObject *self)
1168 talloc_free(self->mem_ctx);
1169 self->ob_type->tp_free(self);
1172 PyTypeObject PyLdb = {
1174 .tp_methods = py_ldb_methods,
1175 .tp_repr = (reprfunc)py_ldb_repr,
1176 .tp_new = py_ldb_new,
1177 .tp_init = (initproc)py_ldb_init,
1178 .tp_dealloc = (destructor)py_ldb_dealloc,
1179 .tp_getset = py_ldb_getset,
1180 .tp_getattro = PyObject_GenericGetAttr,
1181 .tp_basicsize = sizeof(PyLdbObject),
1182 .tp_doc = "Connection to a LDB database.",
1183 .tp_as_sequence = &py_ldb_seq,
1184 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1187 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
1189 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1192 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
1194 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1197 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1199 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1203 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1205 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1209 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1211 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1215 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1217 PyObject *py_base, *py_tree, *py_attrs, *py_ret;
1219 struct ldb_request *req;
1220 const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1221 struct ldb_module *mod;
1222 const char * const*attrs;
1224 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
1225 discard_const_p(char *, kwnames),
1226 &py_base, &scope, &py_tree, &py_attrs))
1231 if (py_attrs == Py_None) {
1234 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
1239 ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base),
1240 scope, NULL /* expr */, attrs,
1241 NULL /* controls */, NULL, NULL, NULL);
1243 talloc_steal(req, attrs);
1245 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1247 req->op.search.res = NULL;
1249 ret = mod->ops->search(mod, req);
1251 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1253 py_ret = PyLdbResult_FromResult(req->op.search.res);
1261 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1263 struct ldb_request *req;
1264 PyObject *py_message;
1266 struct ldb_module *mod;
1268 if (!PyArg_ParseTuple(args, "O", &py_message))
1271 req = talloc_zero(NULL, struct ldb_request);
1272 req->operation = LDB_ADD;
1273 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1275 mod = PyLdbModule_AsModule(self);
1276 ret = mod->ops->add(mod, req);
1278 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1283 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
1286 struct ldb_request *req;
1287 PyObject *py_message;
1288 struct ldb_module *mod;
1290 if (!PyArg_ParseTuple(args, "O", &py_message))
1293 req = talloc_zero(NULL, struct ldb_request);
1294 req->operation = LDB_MODIFY;
1295 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1297 mod = PyLdbModule_AsModule(self);
1298 ret = mod->ops->modify(mod, req);
1300 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1305 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
1308 struct ldb_request *req;
1311 if (!PyArg_ParseTuple(args, "O", &py_dn))
1314 req = talloc_zero(NULL, struct ldb_request);
1315 req->operation = LDB_DELETE;
1316 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1318 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1320 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1325 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1328 struct ldb_request *req;
1329 PyObject *py_dn1, *py_dn2;
1331 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1334 req = talloc_zero(NULL, struct ldb_request);
1336 req->operation = LDB_RENAME;
1337 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1338 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1340 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1342 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1347 static PyMethodDef py_ldb_module_methods[] = {
1348 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1349 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1350 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1351 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1352 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1353 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1354 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1355 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1359 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
1361 talloc_free(self->mem_ctx);
1362 self->ob_type->tp_free(self);
1365 PyTypeObject PyLdbModule = {
1366 .tp_name = "LdbModule",
1367 .tp_methods = py_ldb_module_methods,
1368 .tp_repr = (reprfunc)py_ldb_module_repr,
1369 .tp_str = (reprfunc)py_ldb_module_str,
1370 .tp_basicsize = sizeof(PyLdbModuleObject),
1371 .tp_dealloc = (destructor)py_ldb_module_dealloc,
1372 .tp_flags = Py_TPFLAGS_DEFAULT,
1377 * Create a ldb_message_element from a Python object.
1379 * This will accept any sequence objects that contains strings, or
1382 * A reference to set_obj will be borrowed.
1384 * @param mem_ctx Memory context
1385 * @param set_obj Python object to convert
1386 * @param flags ldb_message_element flags to set
1387 * @param attr_name Name of the attribute
1388 * @return New ldb_message_element, allocated as child of mem_ctx
1390 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1391 PyObject *set_obj, int flags,
1392 const char *attr_name)
1394 struct ldb_message_element *me;
1396 if (PyLdbMessageElement_Check(set_obj))
1397 return PyLdbMessageElement_AsMessageElement(set_obj);
1399 me = talloc(mem_ctx, struct ldb_message_element);
1401 me->name = talloc_strdup(me, attr_name);
1403 if (PyString_Check(set_obj)) {
1405 me->values = talloc_array(me, struct ldb_val, me->num_values);
1406 me->values[0].length = PyString_Size(set_obj);
1407 me->values[0].data = talloc_memdup(me,
1408 (uint8_t *)PyString_AsString(set_obj), me->values[0].length);
1409 } else if (PySequence_Check(set_obj)) {
1411 me->num_values = PySequence_Size(set_obj);
1412 me->values = talloc_array(me, struct ldb_val, me->num_values);
1413 for (i = 0; i < me->num_values; i++) {
1414 PyObject *obj = PySequence_GetItem(set_obj, i);
1416 me->values[i].length = PyString_Size(obj);
1417 me->values[i].data = talloc_memdup(me,
1418 (uint8_t *)PyString_AsString(obj), me->values[i].length);
1429 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
1430 struct ldb_message_element *me)
1435 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1436 result = PyList_New(me->num_values);
1438 for (i = 0; i < me->num_values; i++) {
1439 PyList_SetItem(result, i,
1440 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1446 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1449 if (!PyArg_ParseTuple(args, "i", &i))
1451 if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1454 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1455 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1458 static PyMethodDef py_ldb_msg_element_methods[] = {
1459 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1463 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1465 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1468 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1470 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1471 if (idx < 0 || idx >= el->num_values) {
1472 PyErr_SetString(PyExc_IndexError, "Out of range");
1475 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1478 static PySequenceMethods py_ldb_msg_element_seq = {
1479 .sq_length = (lenfunc)py_ldb_msg_element_len,
1480 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1483 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1485 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1486 PyLdbMessageElement_AsMessageElement(other));
1489 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1491 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1494 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
1496 PyLdbMessageElementObject *ret;
1497 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1502 ret->mem_ctx = talloc_new(NULL);
1503 if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1508 return (PyObject *)ret;
1511 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1513 PyObject *py_elements = NULL;
1514 struct ldb_message_element *el;
1517 const char * const kwnames[] = { "elements", "flags", "name", NULL };
1518 PyLdbMessageElementObject *ret;
1519 TALLOC_CTX *mem_ctx;
1521 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois",
1522 discard_const_p(char *, kwnames),
1523 &py_elements, &flags, &name))
1526 mem_ctx = talloc_new(NULL);
1527 if (mem_ctx == NULL) {
1532 el = talloc_zero(mem_ctx, struct ldb_message_element);
1534 if (py_elements != NULL) {
1536 if (PyString_Check(py_elements)) {
1538 el->values = talloc_array(el, struct ldb_val, 1);
1539 el->values[0].length = PyString_Size(py_elements);
1540 el->values[0].data = talloc_memdup(el,
1541 (uint8_t *)PyString_AsString(py_elements), el->values[0].length);
1542 } else if (PySequence_Check(py_elements)) {
1543 el->num_values = PySequence_Size(py_elements);
1544 el->values = talloc_array(el, struct ldb_val, el->num_values);
1545 for (i = 0; i < el->num_values; i++) {
1546 PyObject *item = PySequence_GetItem(py_elements, i);
1547 el->values[i].data = talloc_memdup(el,
1548 (uint8_t *)PyString_AsString(item), el->values[i].length);
1549 el->values[i].length = PyString_Size(item);
1552 PyErr_SetString(PyExc_TypeError,
1553 "Expected string or list");
1554 talloc_free(mem_ctx);
1560 el->name = talloc_strdup(el, name);
1562 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1565 talloc_free(mem_ctx);
1569 ret->mem_ctx = mem_ctx;
1571 return (PyObject *)ret;
1574 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1576 char *element_str = NULL;
1578 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1581 for (i = 0; i < el->num_values; i++) {
1582 PyObject *o = py_ldb_msg_element_find(self, i);
1583 if (element_str == NULL)
1584 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1586 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1589 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1591 talloc_free(element_str);
1596 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
1598 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1600 if (el->num_values == 1)
1601 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1606 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
1608 talloc_free(self->mem_ctx);
1609 self->ob_type->tp_free(self);
1612 PyTypeObject PyLdbMessageElement = {
1613 .tp_name = "MessageElement",
1614 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1615 .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1616 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1617 .tp_str = (reprfunc)py_ldb_msg_element_str,
1618 .tp_methods = py_ldb_msg_element_methods,
1619 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1620 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1621 .tp_as_sequence = &py_ldb_msg_element_seq,
1622 .tp_new = py_ldb_msg_element_new,
1623 .tp_flags = Py_TPFLAGS_DEFAULT,
1626 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
1629 if (!PyArg_ParseTuple(args, "s", &name))
1632 ldb_msg_remove_attr(self->msg, name);
1637 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
1639 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1641 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1642 if (msg->dn != NULL) {
1643 PyList_SetItem(obj, j, PyString_FromString("dn"));
1646 for (i = 0; i < msg->num_elements; i++) {
1647 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1653 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
1655 struct ldb_message_element *el;
1656 char *name = PyString_AsString(py_name);
1657 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1658 if (!strcmp(name, "dn"))
1659 return PyLdbDn_FromDn(msg->dn);
1660 el = ldb_msg_find_element(msg, name);
1664 return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg);
1667 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
1669 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
1671 PyErr_SetString(PyExc_KeyError, "No such element");
1677 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
1679 PyObject *name, *ret;
1680 if (!PyArg_ParseTuple(args, "O", &name))
1683 ret = py_ldb_msg_getitem_helper(self, name);
1689 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
1691 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1693 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
1695 if (msg->dn != NULL) {
1696 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
1699 for (i = 0; i < msg->num_elements; i++, j++) {
1700 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], self->msg)));
1705 static PyMethodDef py_ldb_msg_methods[] = {
1706 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
1707 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
1708 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
1709 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
1713 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
1715 PyObject *list, *iter;
1717 list = py_ldb_msg_keys(self);
1718 iter = PyObject_GetIter(list);
1723 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
1727 if (!PyString_Check(name)) {
1728 PyErr_SetNone(PyExc_TypeError);
1732 attr_name = PyString_AsString(name);
1733 if (value == NULL) {
1735 ldb_msg_remove_attr(self->msg, attr_name);
1737 struct ldb_message_element *el = PyObject_AsMessageElement(self->msg,
1738 value, 0, attr_name);
1741 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
1742 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
1747 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
1749 return PyLdbMessage_AsMessage(self)->num_elements;
1752 static PyMappingMethods py_ldb_msg_mapping = {
1753 .mp_length = (lenfunc)py_ldb_msg_length,
1754 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
1755 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
1758 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1760 const char * const kwnames[] = { "dn", NULL };
1761 struct ldb_message *ret;
1762 TALLOC_CTX *mem_ctx;
1763 PyObject *pydn = NULL;
1764 PyLdbMessageObject *py_ret;
1766 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
1767 discard_const_p(char *, kwnames),
1771 mem_ctx = talloc_new(NULL);
1772 if (mem_ctx == NULL) {
1777 ret = ldb_msg_new(mem_ctx);
1779 talloc_free(mem_ctx);
1786 if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) {
1787 talloc_free(mem_ctx);
1790 ret->dn = talloc_reference(ret, dn);
1793 py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
1794 if (py_ret == NULL) {
1796 talloc_free(mem_ctx);
1800 py_ret->mem_ctx = mem_ctx;
1802 return (PyObject *)py_ret;
1805 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
1807 PyLdbMessageObject *ret;
1809 ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
1814 ret->mem_ctx = talloc_new(NULL);
1815 ret->msg = talloc_reference(ret->mem_ctx, msg);
1816 return (PyObject *)ret;
1819 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
1821 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1822 return PyLdbDn_FromDn(msg->dn);
1825 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
1827 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1828 if (!PyLdbDn_Check(value)) {
1829 PyErr_SetNone(PyExc_TypeError);
1833 msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value));
1837 static PyGetSetDef py_ldb_msg_getset[] = {
1838 { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
1842 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
1844 PyObject *dict = PyDict_New(), *ret;
1845 if (PyDict_Update(dict, (PyObject *)self) != 0)
1847 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
1852 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
1854 talloc_free(self->mem_ctx);
1855 self->ob_type->tp_free(self);
1858 PyTypeObject PyLdbMessage = {
1859 .tp_name = "Message",
1860 .tp_methods = py_ldb_msg_methods,
1861 .tp_getset = py_ldb_msg_getset,
1862 .tp_as_mapping = &py_ldb_msg_mapping,
1863 .tp_basicsize = sizeof(PyLdbMessageObject),
1864 .tp_dealloc = (destructor)py_ldb_msg_dealloc,
1865 .tp_new = py_ldb_msg_new,
1866 .tp_repr = (reprfunc)py_ldb_msg_repr,
1867 .tp_flags = Py_TPFLAGS_DEFAULT,
1868 .tp_iter = (getiterfunc)py_ldb_msg_iter,
1871 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
1873 PyLdbTreeObject *ret;
1875 ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
1881 ret->mem_ctx = talloc_new(NULL);
1882 ret->tree = talloc_reference(ret->mem_ctx, tree);
1883 return (PyObject *)ret;
1886 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
1888 talloc_free(self->mem_ctx);
1889 self->ob_type->tp_free(self);
1892 PyTypeObject PyLdbTree = {
1894 .tp_basicsize = sizeof(PyLdbTreeObject),
1895 .tp_dealloc = (destructor)py_ldb_tree_dealloc,
1896 .tp_flags = Py_TPFLAGS_DEFAULT,
1900 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
1902 PyObject *py_ldb = (PyObject *)mod->private_data;
1903 PyObject *py_result, *py_base, *py_attrs, *py_tree;
1905 py_base = PyLdbDn_FromDn(req->op.search.base);
1907 if (py_base == NULL)
1908 return LDB_ERR_OPERATIONS_ERROR;
1910 py_tree = PyLdbTree_FromTree(req->op.search.tree);
1912 if (py_tree == NULL)
1913 return LDB_ERR_OPERATIONS_ERROR;
1915 if (req->op.search.attrs == NULL) {
1919 for (len = 0; req->op.search.attrs[len]; len++);
1920 py_attrs = PyList_New(len);
1921 for (i = 0; i < len; i++)
1922 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1925 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
1926 discard_const_p(char, "OiOO"),
1927 py_base, req->op.search.scope, py_tree, py_attrs);
1929 Py_DECREF(py_attrs);
1933 if (py_result == NULL) {
1934 return LDB_ERR_PYTHON_EXCEPTION;
1937 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
1938 if (req->op.search.res == NULL) {
1939 return LDB_ERR_PYTHON_EXCEPTION;
1942 Py_DECREF(py_result);
1947 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1949 PyObject *py_ldb = (PyObject *)mod->private_data;
1950 PyObject *py_result, *py_msg;
1952 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
1954 if (py_msg == NULL) {
1955 return LDB_ERR_OPERATIONS_ERROR;
1958 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
1959 discard_const_p(char, "O"),
1964 if (py_result == NULL) {
1965 return LDB_ERR_PYTHON_EXCEPTION;
1968 Py_DECREF(py_result);
1973 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1975 PyObject *py_ldb = (PyObject *)mod->private_data;
1976 PyObject *py_result, *py_msg;
1978 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
1980 if (py_msg == NULL) {
1981 return LDB_ERR_OPERATIONS_ERROR;
1984 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
1985 discard_const_p(char, "O"),
1990 if (py_result == NULL) {
1991 return LDB_ERR_PYTHON_EXCEPTION;
1994 Py_DECREF(py_result);
1999 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
2001 PyObject *py_ldb = (PyObject *)mod->private_data;
2002 PyObject *py_result, *py_dn;
2004 py_dn = PyLdbDn_FromDn(req->op.del.dn);
2007 return LDB_ERR_OPERATIONS_ERROR;
2009 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
2010 discard_const_p(char, "O"),
2013 if (py_result == NULL) {
2014 return LDB_ERR_PYTHON_EXCEPTION;
2017 Py_DECREF(py_result);
2022 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
2024 PyObject *py_ldb = (PyObject *)mod->private_data;
2025 PyObject *py_result, *py_olddn, *py_newdn;
2027 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
2029 if (py_olddn == NULL)
2030 return LDB_ERR_OPERATIONS_ERROR;
2032 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
2034 if (py_newdn == NULL)
2035 return LDB_ERR_OPERATIONS_ERROR;
2037 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
2038 discard_const_p(char, "OO"),
2039 py_olddn, py_newdn);
2041 Py_DECREF(py_olddn);
2042 Py_DECREF(py_newdn);
2044 if (py_result == NULL) {
2045 return LDB_ERR_PYTHON_EXCEPTION;
2048 Py_DECREF(py_result);
2053 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
2055 PyObject *py_ldb = (PyObject *)mod->private_data;
2056 PyObject *py_result;
2058 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
2059 discard_const_p(char, ""));
2061 return LDB_ERR_OPERATIONS_ERROR;
2064 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
2066 PyObject *py_ldb = (PyObject *)mod->private_data;
2067 PyObject *py_result;
2069 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
2070 discard_const_p(char, ""));
2072 return LDB_ERR_OPERATIONS_ERROR;
2075 static int py_module_start_transaction(struct ldb_module *mod)
2077 PyObject *py_ldb = (PyObject *)mod->private_data;
2078 PyObject *py_result;
2080 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
2081 discard_const_p(char, ""));
2083 if (py_result == NULL) {
2084 return LDB_ERR_PYTHON_EXCEPTION;
2087 Py_DECREF(py_result);
2092 static int py_module_end_transaction(struct ldb_module *mod)
2094 PyObject *py_ldb = (PyObject *)mod->private_data;
2095 PyObject *py_result;
2097 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
2098 discard_const_p(char, ""));
2100 if (py_result == NULL) {
2101 return LDB_ERR_PYTHON_EXCEPTION;
2104 Py_DECREF(py_result);
2109 static int py_module_del_transaction(struct ldb_module *mod)
2111 PyObject *py_ldb = (PyObject *)mod->private_data;
2112 PyObject *py_result;
2114 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
2115 discard_const_p(char, ""));
2117 if (py_result == NULL) {
2118 return LDB_ERR_PYTHON_EXCEPTION;
2121 Py_DECREF(py_result);
2126 static int py_module_destructor(struct ldb_module *mod)
2128 Py_DECREF((PyObject *)mod->private_data);
2132 static int py_module_init(struct ldb_module *mod)
2134 PyObject *py_class = (PyObject *)mod->ops->private_data;
2135 PyObject *py_result, *py_next, *py_ldb;
2137 py_ldb = PyLdb_FromLdbContext(mod->ldb);
2140 return LDB_ERR_OPERATIONS_ERROR;
2142 py_next = PyLdbModule_FromModule(mod->next);
2144 if (py_next == NULL)
2145 return LDB_ERR_OPERATIONS_ERROR;
2147 py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
2150 if (py_result == NULL) {
2151 return LDB_ERR_PYTHON_EXCEPTION;
2154 mod->private_data = py_result;
2156 talloc_set_destructor(mod, py_module_destructor);
2158 return ldb_next_init(mod);
2161 static PyObject *py_register_module(PyObject *module, PyObject *args)
2164 struct ldb_module_ops *ops;
2167 if (!PyArg_ParseTuple(args, "O", &input))
2170 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
2176 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name"))));
2179 ops->private_data = input;
2180 ops->init_context = py_module_init;
2181 ops->search = py_module_search;
2182 ops->add = py_module_add;
2183 ops->modify = py_module_modify;
2184 ops->del = py_module_del;
2185 ops->rename = py_module_rename;
2186 ops->request = py_module_request;
2187 ops->extended = py_module_extended;
2188 ops->start_transaction = py_module_start_transaction;
2189 ops->end_transaction = py_module_end_transaction;
2190 ops->del_transaction = py_module_del_transaction;
2192 ret = ldb_register_module(ops);
2194 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
2199 static PyObject *py_timestring(PyObject *module, PyObject *args)
2204 if (!PyArg_ParseTuple(args, "L", &t))
2206 tresult = ldb_timestring(NULL, t);
2207 ret = PyString_FromString(tresult);
2208 talloc_free(tresult);
2212 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
2215 if (!PyArg_ParseTuple(args, "s", &str))
2218 return PyInt_FromLong(ldb_string_to_time(str));
2221 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
2224 if (!PyArg_ParseTuple(args, "s", &name))
2226 return PyBool_FromLong(ldb_valid_attr_name(name));
2229 static PyMethodDef py_ldb_global_methods[] = {
2230 { "register_module", py_register_module, METH_VARARGS,
2231 "S.register_module(module) -> None\n"
2232 "Register a LDB module."},
2233 { "timestring", py_timestring, METH_VARARGS,
2234 "S.timestring(int) -> string\n"
2235 "Generate a LDAP time string from a UNIX timestamp" },
2236 { "string_to_time", py_string_to_time, METH_VARARGS,
2237 "S.string_to_time(string) -> int\n"
2238 "Parse a LDAP time string into a UNIX timestamp." },
2239 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2240 "S.valid_attr_name(name) -> bool\n"
2241 "Check whether the supplied name is a valid attribute name." },
2242 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2251 if (PyType_Ready(&PyLdbDn) < 0)
2254 if (PyType_Ready(&PyLdbMessage) < 0)
2257 if (PyType_Ready(&PyLdbMessageElement) < 0)
2260 if (PyType_Ready(&PyLdb) < 0)
2263 if (PyType_Ready(&PyLdbModule) < 0)
2266 if (PyType_Ready(&PyLdbTree) < 0)
2269 m = Py_InitModule3("ldb", py_ldb_global_methods,
2270 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2274 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2275 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2276 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2277 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2279 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2280 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2281 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2282 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2284 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2285 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2286 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2287 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2288 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2289 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2290 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2291 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2292 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2293 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2294 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2295 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2296 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2297 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2298 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2299 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2300 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2301 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2302 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2303 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2304 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2305 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2306 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2307 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2308 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2309 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2310 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2311 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2312 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2313 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2314 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2315 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2316 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2317 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2318 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2319 PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2320 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2321 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2323 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2325 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2327 PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL);
2328 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2331 Py_INCREF(&PyLdbDn);
2332 Py_INCREF(&PyLdbModule);
2333 Py_INCREF(&PyLdbMessage);
2334 Py_INCREF(&PyLdbMessageElement);
2335 Py_INCREF(&PyLdbTree);
2337 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2338 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2339 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2340 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2341 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2342 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);