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/>.
31 #include "ldb_private.h"
34 /* There's no Py_ssize_t in 2.4, apparently */
35 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
36 typedef int Py_ssize_t;
37 typedef inquiry lenfunc;
38 typedef intargfunc ssizeargfunc;
41 #ifndef Py_RETURN_NONE
42 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
45 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
47 if (ret == LDB_ERR_PYTHON_EXCEPTION)
48 return; /* Python exception should already be set, just keep that */
50 PyErr_SetObject(error,
51 Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
52 ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
55 static PyObject *PyExc_LdbError;
57 PyAPI_DATA(PyTypeObject) PyLdbMessage;
58 PyAPI_DATA(PyTypeObject) PyLdbModule;
59 PyAPI_DATA(PyTypeObject) PyLdbDn;
60 PyAPI_DATA(PyTypeObject) PyLdb;
61 PyAPI_DATA(PyTypeObject) PyLdbMessageElement;
62 PyAPI_DATA(PyTypeObject) PyLdbTree;
64 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx);
66 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
67 struct ldb_message_element *el,
70 struct ldb_val new_val;
71 TALLOC_CTX *mem_ctx = talloc_new(NULL);
76 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
84 * Create a Python object from a ldb_result.
86 * @param result LDB result to convert
87 * @return Python object with converted result (a list object)
89 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
96 ret = PyList_New(result->count);
97 for (i = 0; i < result->count; i++) {
98 PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i])
105 * Create a LDB Result from a Python object.
106 * If conversion fails, NULL will be returned and a Python exception set.
108 * @param mem_ctx Memory context in which to allocate the LDB Result
109 * @param obj Python object to convert
110 * @return a ldb_result, or NULL if the conversion failed
112 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx,
115 struct ldb_result *res;
121 res = talloc_zero(mem_ctx, struct ldb_result);
122 res->count = PyList_Size(obj);
123 res->msgs = talloc_array(res, struct ldb_message *, res->count);
124 for (i = 0; i < res->count; i++) {
125 PyObject *item = PyList_GetItem(obj, i);
126 res->msgs[i] = PyLdbMessage_AsMessage(item);
131 static PyObject *py_ldb_dn_validate(PyLdbDnObject *self)
133 return PyBool_FromLong(ldb_dn_validate(self->dn));
136 static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self)
138 return PyBool_FromLong(ldb_dn_is_valid(self->dn));
141 static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self)
143 return PyBool_FromLong(ldb_dn_is_special(self->dn));
146 static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self)
148 return PyBool_FromLong(ldb_dn_is_null(self->dn));
151 static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self)
153 return PyString_FromString(ldb_dn_get_casefold(self->dn));
156 static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self)
158 return PyString_FromString(ldb_dn_get_linearized(self->dn));
161 static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self)
163 return PyString_FromString(ldb_dn_canonical_string(self->dn, self->dn));
166 static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self)
168 return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn));
171 static PyObject *py_ldb_dn_repr(PyLdbDnObject *self)
173 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn))));
176 static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args)
180 if (!PyArg_ParseTuple(args, "s", &name))
183 return ldb_dn_check_special(self->dn, name)?Py_True:Py_False;
186 static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
189 ret = ldb_dn_compare(dn1->dn, dn2->dn);
190 if (ret < 0) ret = -1;
191 if (ret > 0) ret = 1;
195 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
197 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
198 struct ldb_dn *parent;
199 PyLdbDnObject *py_ret;
200 TALLOC_CTX *mem_ctx = talloc_new(NULL);
202 parent = ldb_dn_get_parent(mem_ctx, dn);
203 if (parent == NULL) {
204 talloc_free(mem_ctx);
208 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
209 if (py_ret == NULL) {
211 talloc_free(mem_ctx);
214 py_ret->mem_ctx = mem_ctx;
216 return (PyObject *)py_ret;
219 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
221 static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
224 struct ldb_dn *dn, *other;
225 if (!PyArg_ParseTuple(args, "O", &py_other))
228 dn = PyLdbDn_AsDn((PyObject *)self);
230 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
233 return ldb_dn_add_child(dn, other)?Py_True:Py_False;
236 static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
239 struct ldb_dn *other, *dn;
240 if (!PyArg_ParseTuple(args, "O", &py_other))
243 dn = PyLdbDn_AsDn((PyObject *)self);
245 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
248 return ldb_dn_add_base(dn, other)?Py_True:Py_False;
251 static PyMethodDef py_ldb_dn_methods[] = {
252 { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS,
253 "S.validate() -> bool\n"
254 "Validate DN is correct." },
255 { "is_valid", (PyCFunction)py_ldb_dn_is_valid, METH_NOARGS,
256 "S.is_valid() -> bool\n" },
257 { "is_special", (PyCFunction)py_ldb_dn_is_special, METH_NOARGS,
258 "S.is_special() -> bool\n"
259 "Check whether this is a special LDB DN." },
260 { "is_null", (PyCFunction)py_ldb_dn_is_null, METH_NOARGS,
261 "Check whether this is a null DN." },
262 { "get_casefold", (PyCFunction)py_ldb_dn_get_casefold, METH_NOARGS,
264 { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS,
266 { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
267 "S.canonical_str() -> string\n"
268 "Canonical version of this DN (like a posix path)." },
269 { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
270 "S.canonical_ex_str() -> string\n"
271 "Canonical version of this DN (like a posix path, with terminating newline)." },
272 { "check_special", (PyCFunction)py_ldb_dn_is_special, METH_VARARGS,
274 { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS,
276 "Get the parent for this DN." },
277 { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS,
278 "S.add_child(dn) -> None\n"
279 "Add a child DN to this DN." },
280 { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
281 "S.add_base(dn) -> None\n"
282 "Add a base DN to this DN." },
283 { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
288 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
290 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
293 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
295 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self),
297 PyLdbDnObject *py_ret;
299 if (!PyObject_AsDn(NULL, py_other, NULL, &other))
302 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
303 if (py_ret == NULL) {
307 py_ret->mem_ctx = talloc_new(NULL);
308 py_ret->dn = ldb_dn_copy(py_ret->mem_ctx, dn);
309 ldb_dn_add_child(py_ret->dn, other);
310 return (PyObject *)py_ret;
313 static PySequenceMethods py_ldb_dn_seq = {
314 .sq_length = (lenfunc)py_ldb_dn_len,
315 .sq_concat = (binaryfunc)py_ldb_dn_concat,
318 static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
323 struct ldb_context *ldb_ctx;
325 PyLdbDnObject *py_ret;
326 const char * const kwnames[] = { "ldb", "dn", NULL };
328 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os",
329 discard_const_p(char *, kwnames),
333 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
335 mem_ctx = talloc_new(NULL);
336 if (mem_ctx == NULL) {
341 ret = ldb_dn_new(mem_ctx, ldb_ctx, str);
343 if (ret == NULL || !ldb_dn_validate(ret)) {
344 talloc_free(mem_ctx);
345 PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
349 py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0);
351 talloc_free(mem_ctx);
355 py_ret->mem_ctx = mem_ctx;
357 return (PyObject *)py_ret;
360 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
362 talloc_free(self->mem_ctx);
363 self->ob_type->tp_free(self);
366 PyTypeObject PyLdbDn = {
368 .tp_methods = py_ldb_dn_methods,
369 .tp_str = (reprfunc)py_ldb_dn_get_linearized,
370 .tp_repr = (reprfunc)py_ldb_dn_repr,
371 .tp_compare = (cmpfunc)py_ldb_dn_compare,
372 .tp_as_sequence = &py_ldb_dn_seq,
373 .tp_doc = "A LDB distinguished name.",
374 .tp_new = py_ldb_dn_new,
375 .tp_dealloc = (destructor)py_ldb_dn_dealloc,
376 .tp_basicsize = sizeof(PyLdbObject),
377 .tp_flags = Py_TPFLAGS_DEFAULT,
381 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
382 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
384 PyObject *fn = (PyObject *)context;
385 PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap));
388 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
392 if (!PyArg_ParseTuple(args, "O", &cb))
396 /* FIXME: Where do we DECREF cb ? */
397 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
402 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
405 if (!PyArg_ParseTuple(args, "I", &perms))
408 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
413 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
416 if (!PyArg_ParseTuple(args, "s", &modules_dir))
419 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
424 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
426 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
430 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
432 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
436 static PyObject *py_ldb_transaction_prepare_commit(PyLdbObject *self)
438 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_prepare_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
442 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
444 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
448 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
450 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
454 static PyObject *py_ldb_repr(PyLdbObject *self)
456 return PyString_FromFormat("<ldb connection>");
459 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
461 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
464 return PyLdbDn_FromDn(dn);
468 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
470 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
473 return PyLdbDn_FromDn(dn);
476 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
478 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
481 return PyLdbDn_FromDn(dn);
484 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
486 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
489 return PyLdbDn_FromDn(dn);
492 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list,
493 const char *paramname)
497 if (!PyList_Check(list)) {
498 PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
501 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
502 for (i = 0; i < PyList_Size(list); i++) {
503 PyObject *item = PyList_GetItem(list, i);
504 if (!PyString_Check(item)) {
505 PyErr_Format(PyExc_TypeError, "%s should be strings", paramname);
508 ret[i] = talloc_strndup(ret, PyString_AsString(item),
509 PyString_Size(item));
515 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
517 const char * const kwnames[] = { "url", "flags", "options", NULL };
519 PyObject *py_options = Py_None;
520 const char **options;
523 struct ldb_context *ldb;
525 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__",
526 discard_const_p(char *, kwnames),
527 &url, &flags, &py_options))
530 ldb = PyLdb_AsLdbContext(self);
532 if (py_options == Py_None) {
535 options = PyList_AsStringList(ldb, py_options, "options");
541 ret = ldb_connect(ldb, url, flags, options);
542 if (ret != LDB_SUCCESS) {
543 PyErr_SetLdbError(PyExc_LdbError, ret, ldb);
548 talloc_free(options);
552 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
555 struct ldb_context *ldb;
556 ret = (PyLdbObject *)type->tp_alloc(type, 0);
561 ret->mem_ctx = talloc_new(NULL);
562 ldb = ldb_init(ret->mem_ctx, NULL);
570 return (PyObject *)ret;
573 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
577 PyObject *py_options = Py_None;
579 const char **options;
580 const char * const kwnames[] = { "url", "flags", "options", NULL };
582 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO",
583 discard_const_p(char *, kwnames),
584 &url, &flags, &py_options))
587 if (py_options == Py_None) {
590 options = PyList_AsStringList(NULL, py_options, "options");
595 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
596 talloc_free(options);
598 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
603 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
606 PyObject *py_controls = Py_None;
607 struct ldb_context *ldb_ctx;
608 struct ldb_request *req;
609 struct ldb_control **parsed_controls;
610 struct ldb_message *msg;
614 if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls))
617 mem_ctx = talloc_new(NULL);
618 if (mem_ctx == NULL) {
622 ldb_ctx = PyLdb_AsLdbContext(self);
624 if (py_controls == Py_None) {
625 parsed_controls = NULL;
627 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
628 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
629 talloc_free(controls);
632 if (!PyLdbMessage_Check(py_msg)) {
633 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
634 talloc_free(mem_ctx);
637 msg = PyLdbMessage_AsMessage(py_msg);
639 ret = ldb_msg_sanity_check(ldb_ctx, msg);
640 if (ret != LDB_SUCCESS) {
641 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
642 talloc_free(mem_ctx);
646 ret = ldb_build_mod_req(&req, ldb_ctx, mem_ctx, msg, parsed_controls,
647 NULL, ldb_op_default_callback, NULL);
648 if (ret != LDB_SUCCESS) {
649 PyErr_SetString(PyExc_TypeError, "failed to build request");
650 talloc_free(mem_ctx);
654 /* do request and autostart a transaction */
655 /* Then let's LDB handle the message error in case of pb as they are meaningful */
657 ret = ldb_transaction_start(ldb_ctx);
658 if (ret != LDB_SUCCESS) {
659 talloc_free(mem_ctx);
660 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
663 ret = ldb_request(ldb_ctx, req);
664 if (ret == LDB_SUCCESS) {
665 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
668 if (ret == LDB_SUCCESS) {
669 ret = ldb_transaction_commit(ldb_ctx);
671 ldb_transaction_cancel(ldb_ctx);
672 if (ldb_ctx->err_string == NULL) {
673 /* no error string was setup by the backend */
674 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
678 talloc_free(mem_ctx);
679 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
685 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
689 Py_ssize_t dict_pos, msg_pos;
690 struct ldb_message_element *msgel;
691 struct ldb_message *msg;
692 struct ldb_context *ldb_ctx;
693 struct ldb_request *req;
694 PyObject *key, *value;
695 PyObject *py_controls = Py_None;
697 struct ldb_control **parsed_controls;
699 if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls ))
702 mem_ctx = talloc_new(NULL);
703 if (mem_ctx == NULL) {
707 ldb_ctx = PyLdb_AsLdbContext(self);
709 if (py_controls == Py_None) {
710 parsed_controls = NULL;
712 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
713 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
714 talloc_free(controls);
716 if (PyDict_Check(py_msg)) {
717 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
718 msg = ldb_msg_new(mem_ctx);
719 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
720 msg_pos = dict_pos = 0;
722 if (!PyObject_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
723 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
724 talloc_free(mem_ctx);
727 if (msg->dn == NULL) {
728 PyErr_SetString(PyExc_TypeError, "dn set but not found");
729 talloc_free(mem_ctx);
734 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
735 char *key_str = PyString_AsString(key);
736 if (strcmp(key_str, "dn") != 0) {
737 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
739 PyErr_SetString(PyExc_TypeError, "unable to import element");
740 talloc_free(mem_ctx);
743 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
748 if (msg->dn == NULL) {
749 PyErr_SetString(PyExc_TypeError, "no dn set");
750 talloc_free(mem_ctx);
754 msg->num_elements = msg_pos;
756 msg = PyLdbMessage_AsMessage(py_msg);
759 ret = ldb_msg_sanity_check(ldb_ctx, msg);
760 if (ret != LDB_SUCCESS) {
761 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
762 talloc_free(mem_ctx);
766 ret = ldb_build_add_req(&req, ldb_ctx, mem_ctx, msg, parsed_controls,
767 NULL, ldb_op_default_callback, NULL);
768 if (ret != LDB_SUCCESS) {
769 PyErr_SetString(PyExc_TypeError, "failed to build request");
770 talloc_free(mem_ctx);
774 /* do request and autostart a transaction */
775 /* Then let's LDB handle the message error in case of pb as they are meaningful */
777 ret = ldb_transaction_start(ldb_ctx);
778 if (ret != LDB_SUCCESS) {
779 talloc_free(mem_ctx);
780 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
783 ret = ldb_request(ldb_ctx, req);
784 if (ret == LDB_SUCCESS) {
785 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
788 if (ret == LDB_SUCCESS) {
789 ret = ldb_transaction_commit(ldb_ctx);
791 ldb_transaction_cancel(ldb_ctx);
792 if (ldb_ctx->err_string == NULL) {
793 /* no error string was setup by the backend */
794 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
798 talloc_free(mem_ctx);
799 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
804 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
809 struct ldb_context *ldb_ctx;
810 struct ldb_request *req;
811 PyObject *py_controls = Py_None;
813 struct ldb_control **parsed_controls;
815 if (!PyArg_ParseTuple(args, "O|O", &py_dn, &py_controls))
818 mem_ctx = talloc_new(NULL);
819 if (mem_ctx == NULL) {
823 ldb_ctx = PyLdb_AsLdbContext(self);
825 if (py_controls == Py_None) {
826 parsed_controls = NULL;
828 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
829 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
830 talloc_free(controls);
833 if (!PyObject_AsDn(mem_ctx, py_dn, ldb_ctx, &dn)) {
834 talloc_free(mem_ctx);
838 ret = ldb_build_del_req(&req, ldb_ctx, mem_ctx, dn, parsed_controls,
839 NULL, ldb_op_default_callback, NULL);
840 if (ret != LDB_SUCCESS) {
841 PyErr_SetString(PyExc_TypeError, "failed to build request");
842 talloc_free(mem_ctx);
846 /* do request and autostart a transaction */
847 /* Then let's LDB handle the message error in case of pb as they are meaningful */
849 ret = ldb_transaction_start(ldb_ctx);
850 if (ret != LDB_SUCCESS) {
851 talloc_free(mem_ctx);
852 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
855 ret = ldb_request(ldb_ctx, req);
856 if (ret == LDB_SUCCESS) {
857 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
860 if (ret == LDB_SUCCESS) {
861 ret = ldb_transaction_commit(ldb_ctx);
863 ldb_transaction_cancel(ldb_ctx);
864 if (ldb_ctx->err_string == NULL) {
865 /* no error string was setup by the backend */
866 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
870 talloc_free(mem_ctx);
871 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
876 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
878 PyObject *py_dn1, *py_dn2;
879 struct ldb_dn *dn1, *dn2;
881 struct ldb_context *ldb;
883 PyObject *py_controls = Py_None;
884 struct ldb_control **parsed_controls;
885 struct ldb_context *ldb_ctx;
886 struct ldb_request *req;
888 ldb_ctx = PyLdb_AsLdbContext(self);
890 if (!PyArg_ParseTuple(args, "OO|O", &py_dn1, &py_dn2, &py_controls))
894 mem_ctx = talloc_new(NULL);
895 if (mem_ctx == NULL) {
899 ldb = PyLdb_AsLdbContext(self);
901 if (py_controls == Py_None) {
902 parsed_controls = NULL;
904 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
905 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
906 talloc_free(controls);
910 if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
911 talloc_free(mem_ctx);
915 if (!PyObject_AsDn(mem_ctx, py_dn2, ldb, &dn2)) {
916 talloc_free(mem_ctx);
920 ret = ldb_build_rename_req(&req, ldb_ctx, mem_ctx, dn1, dn2, parsed_controls,
921 NULL, ldb_op_default_callback, NULL);
922 if (ret != LDB_SUCCESS) {
923 PyErr_SetString(PyExc_TypeError, "failed to build request");
924 talloc_free(mem_ctx);
928 /* do request and autostart a transaction */
929 /* Then let's LDB handle the message error in case of pb as they are meaningful */
931 ret = ldb_transaction_start(ldb_ctx);
932 if (ret != LDB_SUCCESS) {
933 talloc_free(mem_ctx);
934 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
937 ret = ldb_request(ldb_ctx, req);
938 if (ret == LDB_SUCCESS) {
939 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
942 if (ret == LDB_SUCCESS) {
943 ret = ldb_transaction_commit(ldb_ctx);
945 ldb_transaction_cancel(ldb_ctx);
946 if (ldb_ctx->err_string == NULL) {
947 /* no error string was setup by the backend */
948 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
952 talloc_free(mem_ctx);
953 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
958 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
961 if (!PyArg_ParseTuple(args, "s", &name))
964 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
969 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
971 char *attribute, *syntax;
974 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
977 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
979 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
984 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
989 /* We don't want this attached to the 'ldb' any more */
990 return Py_BuildValue(discard_const_p(char, "(iO)"),
992 PyLdbMessage_FromMessage(ldif->msg));
997 static PyObject *py_ldb_write_ldif(PyLdbMessageObject *self, PyObject *args)
1001 struct ldb_ldif ldif;
1004 TALLOC_CTX *mem_ctx;
1006 if (!PyArg_ParseTuple(args, "Oi", &py_msg, &changetype))
1009 if (!PyLdbMessage_Check(py_msg)) {
1010 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for msg");
1014 ldif.msg = PyLdbMessage_AsMessage(py_msg);
1015 ldif.changetype = changetype;
1017 mem_ctx = talloc_new(NULL);
1019 string = ldb_ldif_write_string(PyLdb_AsLdbContext(self), mem_ctx, &ldif);
1021 PyErr_SetString(PyExc_KeyError, "Failed to generate LDIF");
1025 ret = PyString_FromString(string);
1027 talloc_free(mem_ctx);
1032 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
1035 struct ldb_ldif *ldif;
1038 TALLOC_CTX *mem_ctx;
1040 if (!PyArg_ParseTuple(args, "s", &s))
1043 mem_ctx = talloc_new(NULL);
1048 list = PyList_New(0);
1049 while (s && *s != '\0') {
1050 ldif = ldb_ldif_read_string(self->ldb_ctx, &s);
1051 talloc_steal(mem_ctx, ldif);
1053 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
1055 PyErr_SetString(PyExc_ValueError, "unable to parse ldif string");
1056 talloc_free(mem_ctx);
1060 talloc_free(mem_ctx); /* The pyobject already has a reference to the things it needs */
1061 return PyObject_GetIter(list);
1064 static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args)
1067 PyObject *py_msg_old;
1068 PyObject *py_msg_new;
1069 struct ldb_message *diff;
1070 struct ldb_context *ldb;
1073 if (!PyArg_ParseTuple(args, "OO", &py_msg_old, &py_msg_new))
1076 if (!PyLdbMessage_Check(py_msg_old)) {
1077 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for old message");
1081 if (!PyLdbMessage_Check(py_msg_new)) {
1082 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for new message");
1086 ldb = PyLdb_AsLdbContext(self);
1087 ldb_ret = ldb_msg_difference(ldb, ldb,
1088 PyLdbMessage_AsMessage(py_msg_old),
1089 PyLdbMessage_AsMessage(py_msg_new),
1091 if (ldb_ret != LDB_SUCCESS) {
1092 PyErr_SetString(PyExc_RuntimeError, "Failed to generate the Ldb Message diff");
1096 py_ret = PyLdbMessage_FromMessage(diff);
1098 talloc_unlink(ldb, diff);
1103 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
1105 const struct ldb_schema_attribute *a;
1106 struct ldb_val old_val;
1107 struct ldb_val new_val;
1108 TALLOC_CTX *mem_ctx;
1113 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
1116 mem_ctx = talloc_new(NULL);
1118 old_val.data = (uint8_t *)PyString_AsString(val);
1119 old_val.length = PyString_Size(val);
1121 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
1127 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
1128 talloc_free(mem_ctx);
1132 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
1134 talloc_free(mem_ctx);
1139 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
1141 PyObject *py_base = Py_None;
1142 int scope = LDB_SCOPE_DEFAULT;
1144 PyObject *py_attrs = Py_None;
1145 PyObject *py_controls = Py_None;
1146 const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
1148 struct ldb_result *res;
1149 struct ldb_request *req;
1151 struct ldb_context *ldb_ctx;
1152 struct ldb_control **parsed_controls;
1153 struct ldb_dn *base;
1155 TALLOC_CTX *mem_ctx;
1157 /* type "int" rather than "enum" for "scope" is intentional */
1158 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
1159 discard_const_p(char *, kwnames),
1160 &py_base, &scope, &expr, &py_attrs, &py_controls))
1164 mem_ctx = talloc_new(NULL);
1165 if (mem_ctx == NULL) {
1169 ldb_ctx = PyLdb_AsLdbContext(self);
1171 if (py_attrs == Py_None) {
1174 attrs = PyList_AsStringList(mem_ctx, py_attrs, "attrs");
1175 if (attrs == NULL) {
1176 talloc_free(mem_ctx);
1181 if (py_base == Py_None) {
1182 base = ldb_get_default_basedn(ldb_ctx);
1184 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
1190 if (py_controls == Py_None) {
1191 parsed_controls = NULL;
1193 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
1194 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
1195 talloc_free(controls);
1198 res = talloc_zero(mem_ctx, struct ldb_result);
1201 talloc_free(mem_ctx);
1205 ret = ldb_build_search_req(&req, ldb_ctx, mem_ctx,
1212 ldb_search_default_callback,
1215 if (ret != LDB_SUCCESS) {
1216 talloc_free(mem_ctx);
1217 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
1221 talloc_steal(req, attrs);
1223 ret = ldb_request(ldb_ctx, req);
1225 if (ret == LDB_SUCCESS) {
1226 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1229 if (ret != LDB_SUCCESS) {
1230 talloc_free(mem_ctx);
1231 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
1235 py_ret = PyLdbResult_FromResult(res);
1237 talloc_free(mem_ctx);
1242 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
1247 if (!PyArg_ParseTuple(args, "s", &name))
1250 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
1255 /* FIXME: More interpretation */
1260 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
1265 if (!PyArg_ParseTuple(args, "sO", &name, &data))
1268 /* FIXME: More interpretation */
1270 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
1275 static PyObject *py_ldb_modules(PyLdbObject *self)
1277 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
1278 PyObject *ret = PyList_New(0);
1279 struct ldb_module *mod;
1281 for (mod = ldb->modules; mod; mod = mod->next) {
1282 PyList_Append(ret, PyLdbModule_FromModule(mod));
1288 static PyObject *py_ldb_sequence_number(PyLdbObject *self, PyObject *args)
1290 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
1294 if (!PyArg_ParseTuple(args, "i", &type))
1297 /* FIXME: More interpretation */
1299 ret = ldb_sequence_number(ldb, type, &value);
1301 if (ret != LDB_SUCCESS) {
1302 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
1305 return PyLong_FromLongLong(value);
1307 static PyMethodDef py_ldb_methods[] = {
1308 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
1309 "S.set_debug(callback) -> None\n"
1310 "Set callback for LDB debug messages.\n"
1311 "The callback should accept a debug level and debug text." },
1312 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
1313 "S.set_create_perms(mode) -> None\n"
1314 "Set mode to use when creating new LDB files." },
1315 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
1316 "S.set_modules_dir(path) -> None\n"
1317 "Set path LDB should search for modules" },
1318 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
1319 "S.transaction_start() -> None\n"
1320 "Start a new transaction." },
1321 { "transaction_prepare_commit", (PyCFunction)py_ldb_transaction_prepare_commit, METH_NOARGS,
1322 "S.transaction_prepare_commit() -> None\n"
1323 "prepare to commit a new transaction (2-stage commit)." },
1324 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
1325 "S.transaction_commit() -> None\n"
1326 "commit a new transaction." },
1327 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
1328 "S.transaction_cancel() -> None\n"
1329 "cancel a new transaction." },
1330 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
1332 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
1334 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
1336 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
1338 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
1340 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
1341 "S.connect(url, flags=0, options=None) -> None\n"
1342 "Connect to a LDB URL." },
1343 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
1344 "S.modify(message) -> None\n"
1345 "Modify an entry." },
1346 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
1347 "S.add(message) -> None\n"
1349 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
1350 "S.delete(dn) -> None\n"
1351 "Remove an entry." },
1352 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
1353 "S.rename(old_dn, new_dn) -> None\n"
1354 "Rename an entry." },
1355 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
1356 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1357 "Search in a database.\n"
1359 ":param base: Optional base DN to search\n"
1360 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1361 ":param expression: Optional search expression\n"
1362 ":param attrs: Attributes to return (defaults to all)\n"
1363 ":param controls: Optional list of controls\n"
1364 ":return: Iterator over Message objects\n"
1366 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
1368 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
1370 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
1372 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
1373 "S.parse_ldif(ldif) -> iter(messages)\n"
1374 "Parse a string formatted using LDIF." },
1375 { "write_ldif", (PyCFunction)py_ldb_write_ldif, METH_VARARGS,
1376 "S.write_ldif(message, changetype) -> ldif\n"
1377 "Print the message as a string formatted using LDIF." },
1378 { "msg_diff", (PyCFunction)py_ldb_msg_diff, METH_VARARGS,
1379 "S.msg_diff(Message) -> Message\n"
1380 "Return an LDB Message of the difference between two Message objects." },
1381 { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
1382 "S.get_opaque(name) -> value\n"
1383 "Get an opaque value set on this LDB connection. \n"
1384 ":note: The returned value may not be useful in Python."
1386 { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
1387 "S.set_opaque(name, value) -> None\n"
1388 "Set an opaque value on this LDB connection. \n"
1389 ":note: Passing incorrect values may cause crashes." },
1390 { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
1391 "S.modules() -> list\n"
1392 "Return the list of modules on this LDB connection " },
1393 { "sequence_number", (PyCFunction)py_ldb_sequence_number, METH_VARARGS,
1394 "S.sequence_number(type) -> value\n"
1395 "Return the value of the sequence according to the requested type" },
1399 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
1401 PyLdbModuleObject *ret;
1403 ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
1408 ret->mem_ctx = talloc_new(NULL);
1409 ret->mod = talloc_reference(ret->mem_ctx, mod);
1410 return (PyObject *)ret;
1413 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
1415 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
1418 static PyGetSetDef py_ldb_getset[] = {
1419 { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL },
1423 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
1425 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1427 struct ldb_result *result;
1431 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
1434 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
1435 if (ret != LDB_SUCCESS) {
1436 PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
1440 count = result->count;
1442 talloc_free(result);
1447 static PySequenceMethods py_ldb_seq = {
1448 .sq_contains = (objobjproc)py_ldb_contains,
1451 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
1455 ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1460 ret->mem_ctx = talloc_new(NULL);
1461 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1462 return (PyObject *)ret;
1465 static void py_ldb_dealloc(PyLdbObject *self)
1467 talloc_free(self->mem_ctx);
1468 self->ob_type->tp_free(self);
1471 PyTypeObject PyLdb = {
1472 .tp_name = "ldb.Ldb",
1473 .tp_methods = py_ldb_methods,
1474 .tp_repr = (reprfunc)py_ldb_repr,
1475 .tp_new = py_ldb_new,
1476 .tp_init = (initproc)py_ldb_init,
1477 .tp_dealloc = (destructor)py_ldb_dealloc,
1478 .tp_getset = py_ldb_getset,
1479 .tp_getattro = PyObject_GenericGetAttr,
1480 .tp_basicsize = sizeof(PyLdbObject),
1481 .tp_doc = "Connection to a LDB database.",
1482 .tp_as_sequence = &py_ldb_seq,
1483 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1486 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
1488 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1491 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
1493 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1496 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1498 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1502 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1504 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1508 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1510 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1514 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1516 PyObject *py_base, *py_tree, *py_attrs, *py_ret;
1518 struct ldb_request *req;
1519 const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1520 struct ldb_module *mod;
1521 const char * const*attrs;
1523 /* type "int" rather than "enum" for "scope" is intentional */
1524 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
1525 discard_const_p(char *, kwnames),
1526 &py_base, &scope, &py_tree, &py_attrs))
1531 if (py_attrs == Py_None) {
1534 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
1539 ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base),
1540 scope, NULL /* expr */, attrs,
1541 NULL /* controls */, NULL, NULL, NULL);
1543 talloc_steal(req, attrs);
1545 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1547 req->op.search.res = NULL;
1549 ret = mod->ops->search(mod, req);
1551 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1553 py_ret = PyLdbResult_FromResult(req->op.search.res);
1561 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1563 struct ldb_request *req;
1564 PyObject *py_message;
1566 struct ldb_module *mod;
1568 if (!PyArg_ParseTuple(args, "O", &py_message))
1571 req = talloc_zero(NULL, struct ldb_request);
1572 req->operation = LDB_ADD;
1573 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1575 mod = PyLdbModule_AsModule(self);
1576 ret = mod->ops->add(mod, req);
1578 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1583 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
1586 struct ldb_request *req;
1587 PyObject *py_message;
1588 struct ldb_module *mod;
1590 if (!PyArg_ParseTuple(args, "O", &py_message))
1593 req = talloc_zero(NULL, struct ldb_request);
1594 req->operation = LDB_MODIFY;
1595 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1597 mod = PyLdbModule_AsModule(self);
1598 ret = mod->ops->modify(mod, req);
1600 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1605 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
1608 struct ldb_request *req;
1611 if (!PyArg_ParseTuple(args, "O", &py_dn))
1614 req = talloc_zero(NULL, struct ldb_request);
1615 req->operation = LDB_DELETE;
1616 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1618 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1620 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1625 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1628 struct ldb_request *req;
1629 PyObject *py_dn1, *py_dn2;
1631 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1634 req = talloc_zero(NULL, struct ldb_request);
1636 req->operation = LDB_RENAME;
1637 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1638 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1640 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1642 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1647 static PyMethodDef py_ldb_module_methods[] = {
1648 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1649 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1650 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1651 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1652 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1653 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1654 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1655 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1659 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
1661 talloc_free(self->mem_ctx);
1662 self->ob_type->tp_free(self);
1665 PyTypeObject PyLdbModule = {
1666 .tp_name = "ldb.LdbModule",
1667 .tp_methods = py_ldb_module_methods,
1668 .tp_repr = (reprfunc)py_ldb_module_repr,
1669 .tp_str = (reprfunc)py_ldb_module_str,
1670 .tp_basicsize = sizeof(PyLdbModuleObject),
1671 .tp_dealloc = (destructor)py_ldb_module_dealloc,
1672 .tp_flags = Py_TPFLAGS_DEFAULT,
1677 * Create a ldb_message_element from a Python object.
1679 * This will accept any sequence objects that contains strings, or
1682 * A reference to set_obj will be borrowed.
1684 * @param mem_ctx Memory context
1685 * @param set_obj Python object to convert
1686 * @param flags ldb_message_element flags to set
1687 * @param attr_name Name of the attribute
1688 * @return New ldb_message_element, allocated as child of mem_ctx
1690 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1691 PyObject *set_obj, int flags,
1692 const char *attr_name)
1694 struct ldb_message_element *me;
1696 if (PyLdbMessageElement_Check(set_obj)) {
1697 PyLdbMessageElementObject *set_obj_as_me = (PyLdbMessageElementObject *)set_obj;
1698 /* We have to talloc_reference() the memory context, not the pointer which may not actually be it's own context */
1699 if (talloc_reference(mem_ctx, set_obj_as_me->mem_ctx)) {
1700 return PyLdbMessageElement_AsMessageElement(set_obj);
1705 me = talloc(mem_ctx, struct ldb_message_element);
1707 me->name = talloc_strdup(me, attr_name);
1709 if (PyString_Check(set_obj)) {
1711 me->values = talloc_array(me, struct ldb_val, me->num_values);
1712 me->values[0].length = PyString_Size(set_obj);
1713 me->values[0].data = talloc_memdup(me,
1714 (uint8_t *)PyString_AsString(set_obj), me->values[0].length+1);
1715 } else if (PySequence_Check(set_obj)) {
1717 me->num_values = PySequence_Size(set_obj);
1718 me->values = talloc_array(me, struct ldb_val, me->num_values);
1719 for (i = 0; i < me->num_values; i++) {
1720 PyObject *obj = PySequence_GetItem(set_obj, i);
1721 if (!PyString_Check(obj)) {
1722 PyErr_Format(PyExc_TypeError,
1723 "Expected string as element %d in list", i);
1728 me->values[i].length = PyString_Size(obj);
1729 me->values[i].data = talloc_memdup(me,
1730 (uint8_t *)PyString_AsString(obj), me->values[i].length+1);
1741 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
1742 struct ldb_message_element *me)
1747 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1748 result = PyList_New(me->num_values);
1750 for (i = 0; i < me->num_values; i++) {
1751 PyList_SetItem(result, i,
1752 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1758 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1761 if (!PyArg_ParseTuple(args, "i", &i))
1763 if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1766 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1767 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1770 static PyObject *py_ldb_msg_element_flags(PyLdbMessageElementObject *self, PyObject *args)
1772 struct ldb_message_element *el;
1774 el = PyLdbMessageElement_AsMessageElement(self);
1775 return PyInt_FromLong(el->flags);
1778 static PyObject *py_ldb_msg_element_set_flags(PyLdbMessageElementObject *self, PyObject *args)
1781 struct ldb_message_element *el;
1782 if (!PyArg_ParseTuple(args, "i", &flags))
1785 el = PyLdbMessageElement_AsMessageElement(self);
1790 static PyMethodDef py_ldb_msg_element_methods[] = {
1791 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1792 { "set_flags", (PyCFunction)py_ldb_msg_element_set_flags, METH_VARARGS, NULL },
1793 { "flags", (PyCFunction)py_ldb_msg_element_flags, METH_NOARGS, NULL },
1797 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1799 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1802 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1804 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1805 if (idx < 0 || idx >= el->num_values) {
1806 PyErr_SetString(PyExc_IndexError, "Out of range");
1809 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1812 static PySequenceMethods py_ldb_msg_element_seq = {
1813 .sq_length = (lenfunc)py_ldb_msg_element_len,
1814 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1817 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1819 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1820 PyLdbMessageElement_AsMessageElement(other));
1823 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1825 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1828 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
1830 PyLdbMessageElementObject *ret;
1831 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1836 ret->mem_ctx = talloc_new(NULL);
1837 if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1842 return (PyObject *)ret;
1845 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1847 PyObject *py_elements = NULL;
1848 struct ldb_message_element *el;
1851 const char * const kwnames[] = { "elements", "flags", "name", NULL };
1852 PyLdbMessageElementObject *ret;
1853 TALLOC_CTX *mem_ctx;
1855 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois",
1856 discard_const_p(char *, kwnames),
1857 &py_elements, &flags, &name))
1860 mem_ctx = talloc_new(NULL);
1861 if (mem_ctx == NULL) {
1866 el = talloc_zero(mem_ctx, struct ldb_message_element);
1868 if (py_elements != NULL) {
1870 if (PyString_Check(py_elements)) {
1872 el->values = talloc_array(el, struct ldb_val, 1);
1873 el->values[0].length = PyString_Size(py_elements);
1874 el->values[0].data = talloc_memdup(el,
1875 (uint8_t *)PyString_AsString(py_elements), el->values[0].length+1);
1876 } else if (PySequence_Check(py_elements)) {
1877 el->num_values = PySequence_Size(py_elements);
1878 el->values = talloc_array(el, struct ldb_val, el->num_values);
1879 for (i = 0; i < el->num_values; i++) {
1880 PyObject *item = PySequence_GetItem(py_elements, i);
1881 if (!PyString_Check(item)) {
1882 PyErr_Format(PyExc_TypeError,
1883 "Expected string as element %d in list",
1885 talloc_free(mem_ctx);
1888 el->values[i].length = PyString_Size(item);
1889 el->values[i].data = talloc_memdup(el,
1890 (uint8_t *)PyString_AsString(item), el->values[i].length+1);
1893 PyErr_SetString(PyExc_TypeError,
1894 "Expected string or list");
1895 talloc_free(mem_ctx);
1901 el->name = talloc_strdup(el, name);
1903 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1906 talloc_free(mem_ctx);
1910 ret->mem_ctx = mem_ctx;
1912 return (PyObject *)ret;
1915 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1917 char *element_str = NULL;
1919 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1922 for (i = 0; i < el->num_values; i++) {
1923 PyObject *o = py_ldb_msg_element_find(self, i);
1924 if (element_str == NULL)
1925 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1927 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1930 if (element_str != NULL) {
1931 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1932 talloc_free(element_str);
1934 ret = PyString_FromString("MessageElement([])");
1940 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
1942 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1944 if (el->num_values == 1)
1945 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1950 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
1952 talloc_free(self->mem_ctx);
1953 self->ob_type->tp_free(self);
1956 PyTypeObject PyLdbMessageElement = {
1957 .tp_name = "ldb.MessageElement",
1958 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1959 .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1960 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1961 .tp_str = (reprfunc)py_ldb_msg_element_str,
1962 .tp_methods = py_ldb_msg_element_methods,
1963 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1964 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1965 .tp_as_sequence = &py_ldb_msg_element_seq,
1966 .tp_new = py_ldb_msg_element_new,
1967 .tp_flags = Py_TPFLAGS_DEFAULT,
1970 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
1973 if (!PyArg_ParseTuple(args, "s", &name))
1976 ldb_msg_remove_attr(self->msg, name);
1981 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
1983 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1985 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1986 if (msg->dn != NULL) {
1987 PyList_SetItem(obj, j, PyString_FromString("dn"));
1990 for (i = 0; i < msg->num_elements; i++) {
1991 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1997 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
1999 struct ldb_message_element *el;
2001 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2002 if (!PyString_Check(py_name)) {
2003 PyErr_SetNone(PyExc_TypeError);
2006 name = PyString_AsString(py_name);
2007 if (!strcmp(name, "dn"))
2008 return PyLdbDn_FromDn(msg->dn);
2009 el = ldb_msg_find_element(msg, name);
2013 return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg->elements);
2016 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
2018 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
2020 PyErr_SetString(PyExc_KeyError, "No such element");
2026 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
2028 PyObject *name, *ret;
2029 if (!PyArg_ParseTuple(args, "O", &name))
2032 ret = py_ldb_msg_getitem_helper(self, name);
2034 if (PyErr_Occurred())
2041 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
2043 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2045 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
2047 if (msg->dn != NULL) {
2048 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
2051 for (i = 0; i < msg->num_elements; i++, j++) {
2052 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], msg->elements)));
2057 static PyMethodDef py_ldb_msg_methods[] = {
2058 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
2059 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
2060 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
2061 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
2065 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
2067 PyObject *list, *iter;
2069 list = py_ldb_msg_keys(self);
2070 iter = PyObject_GetIter(list);
2075 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
2079 if (!PyString_Check(name)) {
2080 PyErr_SetNone(PyExc_TypeError);
2084 attr_name = PyString_AsString(name);
2085 if (value == NULL) {
2087 ldb_msg_remove_attr(self->msg, attr_name);
2089 struct ldb_message_element *el = PyObject_AsMessageElement(self->msg,
2090 value, 0, attr_name);
2093 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
2094 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
2099 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
2101 return PyLdbMessage_AsMessage(self)->num_elements;
2104 static PyMappingMethods py_ldb_msg_mapping = {
2105 .mp_length = (lenfunc)py_ldb_msg_length,
2106 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
2107 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
2110 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2112 const char * const kwnames[] = { "dn", NULL };
2113 struct ldb_message *ret;
2114 TALLOC_CTX *mem_ctx;
2115 PyObject *pydn = NULL;
2116 PyLdbMessageObject *py_ret;
2118 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
2119 discard_const_p(char *, kwnames),
2123 mem_ctx = talloc_new(NULL);
2124 if (mem_ctx == NULL) {
2129 ret = ldb_msg_new(mem_ctx);
2131 talloc_free(mem_ctx);
2138 if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) {
2139 talloc_free(mem_ctx);
2142 ret->dn = talloc_reference(ret, dn);
2145 py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
2146 if (py_ret == NULL) {
2148 talloc_free(mem_ctx);
2152 py_ret->mem_ctx = mem_ctx;
2154 return (PyObject *)py_ret;
2157 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
2159 PyLdbMessageObject *ret;
2161 ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
2166 ret->mem_ctx = talloc_new(NULL);
2167 ret->msg = talloc_reference(ret->mem_ctx, msg);
2168 return (PyObject *)ret;
2171 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
2173 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2174 return PyLdbDn_FromDn(msg->dn);
2177 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
2179 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2180 if (!PyLdbDn_Check(value)) {
2181 PyErr_SetNone(PyExc_TypeError);
2185 msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value));
2189 static PyGetSetDef py_ldb_msg_getset[] = {
2190 { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
2194 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
2196 PyObject *dict = PyDict_New(), *ret;
2197 if (PyDict_Update(dict, (PyObject *)self) != 0)
2199 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
2204 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
2206 talloc_free(self->mem_ctx);
2207 self->ob_type->tp_free(self);
2210 static int py_ldb_msg_compare(PyLdbMessageObject *py_msg1,
2211 PyLdbMessageObject *py_msg2)
2213 struct ldb_message *msg1 = PyLdbMessage_AsMessage(py_msg1),
2214 *msg2 = PyLdbMessage_AsMessage(py_msg2);
2218 if ((msg1->dn != NULL) || (msg2->dn != NULL)) {
2219 ret = ldb_dn_compare(msg1->dn, msg2->dn);
2225 ret = msg1->num_elements - msg2->num_elements;
2230 for (i = 0; i < msg1->num_elements; i++) {
2231 ret = ldb_msg_element_compare_name(&msg1->elements[i],
2232 &msg2->elements[i]);
2237 ret = ldb_msg_element_compare(&msg1->elements[i],
2238 &msg2->elements[i]);
2247 PyTypeObject PyLdbMessage = {
2248 .tp_name = "ldb.Message",
2249 .tp_methods = py_ldb_msg_methods,
2250 .tp_getset = py_ldb_msg_getset,
2251 .tp_as_mapping = &py_ldb_msg_mapping,
2252 .tp_basicsize = sizeof(PyLdbMessageObject),
2253 .tp_dealloc = (destructor)py_ldb_msg_dealloc,
2254 .tp_new = py_ldb_msg_new,
2255 .tp_repr = (reprfunc)py_ldb_msg_repr,
2256 .tp_flags = Py_TPFLAGS_DEFAULT,
2257 .tp_iter = (getiterfunc)py_ldb_msg_iter,
2258 .tp_compare = (cmpfunc)py_ldb_msg_compare,
2261 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
2263 PyLdbTreeObject *ret;
2265 ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
2271 ret->mem_ctx = talloc_new(NULL);
2272 ret->tree = talloc_reference(ret->mem_ctx, tree);
2273 return (PyObject *)ret;
2276 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
2278 talloc_free(self->mem_ctx);
2279 self->ob_type->tp_free(self);
2282 PyTypeObject PyLdbTree = {
2283 .tp_name = "ldb.Tree",
2284 .tp_basicsize = sizeof(PyLdbTreeObject),
2285 .tp_dealloc = (destructor)py_ldb_tree_dealloc,
2286 .tp_flags = Py_TPFLAGS_DEFAULT,
2290 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
2292 PyObject *py_ldb = (PyObject *)mod->private_data;
2293 PyObject *py_result, *py_base, *py_attrs, *py_tree;
2295 py_base = PyLdbDn_FromDn(req->op.search.base);
2297 if (py_base == NULL)
2298 return LDB_ERR_OPERATIONS_ERROR;
2300 py_tree = PyLdbTree_FromTree(req->op.search.tree);
2302 if (py_tree == NULL)
2303 return LDB_ERR_OPERATIONS_ERROR;
2305 if (req->op.search.attrs == NULL) {
2309 for (len = 0; req->op.search.attrs[len]; len++);
2310 py_attrs = PyList_New(len);
2311 for (i = 0; i < len; i++)
2312 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
2315 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
2316 discard_const_p(char, "OiOO"),
2317 py_base, req->op.search.scope, py_tree, py_attrs);
2319 Py_DECREF(py_attrs);
2323 if (py_result == NULL) {
2324 return LDB_ERR_PYTHON_EXCEPTION;
2327 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
2328 if (req->op.search.res == NULL) {
2329 return LDB_ERR_PYTHON_EXCEPTION;
2332 Py_DECREF(py_result);
2337 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
2339 PyObject *py_ldb = (PyObject *)mod->private_data;
2340 PyObject *py_result, *py_msg;
2342 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
2344 if (py_msg == NULL) {
2345 return LDB_ERR_OPERATIONS_ERROR;
2348 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
2349 discard_const_p(char, "O"),
2354 if (py_result == NULL) {
2355 return LDB_ERR_PYTHON_EXCEPTION;
2358 Py_DECREF(py_result);
2363 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
2365 PyObject *py_ldb = (PyObject *)mod->private_data;
2366 PyObject *py_result, *py_msg;
2368 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
2370 if (py_msg == NULL) {
2371 return LDB_ERR_OPERATIONS_ERROR;
2374 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
2375 discard_const_p(char, "O"),
2380 if (py_result == NULL) {
2381 return LDB_ERR_PYTHON_EXCEPTION;
2384 Py_DECREF(py_result);
2389 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
2391 PyObject *py_ldb = (PyObject *)mod->private_data;
2392 PyObject *py_result, *py_dn;
2394 py_dn = PyLdbDn_FromDn(req->op.del.dn);
2397 return LDB_ERR_OPERATIONS_ERROR;
2399 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
2400 discard_const_p(char, "O"),
2403 if (py_result == NULL) {
2404 return LDB_ERR_PYTHON_EXCEPTION;
2407 Py_DECREF(py_result);
2412 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
2414 PyObject *py_ldb = (PyObject *)mod->private_data;
2415 PyObject *py_result, *py_olddn, *py_newdn;
2417 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
2419 if (py_olddn == NULL)
2420 return LDB_ERR_OPERATIONS_ERROR;
2422 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
2424 if (py_newdn == NULL)
2425 return LDB_ERR_OPERATIONS_ERROR;
2427 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
2428 discard_const_p(char, "OO"),
2429 py_olddn, py_newdn);
2431 Py_DECREF(py_olddn);
2432 Py_DECREF(py_newdn);
2434 if (py_result == NULL) {
2435 return LDB_ERR_PYTHON_EXCEPTION;
2438 Py_DECREF(py_result);
2443 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
2445 PyObject *py_ldb = (PyObject *)mod->private_data;
2446 PyObject *py_result;
2448 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
2449 discard_const_p(char, ""));
2451 return LDB_ERR_OPERATIONS_ERROR;
2454 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
2456 PyObject *py_ldb = (PyObject *)mod->private_data;
2457 PyObject *py_result;
2459 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
2460 discard_const_p(char, ""));
2462 return LDB_ERR_OPERATIONS_ERROR;
2465 static int py_module_start_transaction(struct ldb_module *mod)
2467 PyObject *py_ldb = (PyObject *)mod->private_data;
2468 PyObject *py_result;
2470 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
2471 discard_const_p(char, ""));
2473 if (py_result == NULL) {
2474 return LDB_ERR_PYTHON_EXCEPTION;
2477 Py_DECREF(py_result);
2482 static int py_module_end_transaction(struct ldb_module *mod)
2484 PyObject *py_ldb = (PyObject *)mod->private_data;
2485 PyObject *py_result;
2487 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
2488 discard_const_p(char, ""));
2490 if (py_result == NULL) {
2491 return LDB_ERR_PYTHON_EXCEPTION;
2494 Py_DECREF(py_result);
2499 static int py_module_del_transaction(struct ldb_module *mod)
2501 PyObject *py_ldb = (PyObject *)mod->private_data;
2502 PyObject *py_result;
2504 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
2505 discard_const_p(char, ""));
2507 if (py_result == NULL) {
2508 return LDB_ERR_PYTHON_EXCEPTION;
2511 Py_DECREF(py_result);
2516 static int py_module_destructor(struct ldb_module *mod)
2518 Py_DECREF((PyObject *)mod->private_data);
2522 static int py_module_init(struct ldb_module *mod)
2524 PyObject *py_class = (PyObject *)mod->ops->private_data;
2525 PyObject *py_result, *py_next, *py_ldb;
2527 py_ldb = PyLdb_FromLdbContext(mod->ldb);
2530 return LDB_ERR_OPERATIONS_ERROR;
2532 py_next = PyLdbModule_FromModule(mod->next);
2534 if (py_next == NULL)
2535 return LDB_ERR_OPERATIONS_ERROR;
2537 py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
2540 if (py_result == NULL) {
2541 return LDB_ERR_PYTHON_EXCEPTION;
2544 mod->private_data = py_result;
2546 talloc_set_destructor(mod, py_module_destructor);
2548 return ldb_next_init(mod);
2551 static PyObject *py_register_module(PyObject *module, PyObject *args)
2554 struct ldb_module_ops *ops;
2557 if (!PyArg_ParseTuple(args, "O", &input))
2560 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
2566 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name"))));
2569 ops->private_data = input;
2570 ops->init_context = py_module_init;
2571 ops->search = py_module_search;
2572 ops->add = py_module_add;
2573 ops->modify = py_module_modify;
2574 ops->del = py_module_del;
2575 ops->rename = py_module_rename;
2576 ops->request = py_module_request;
2577 ops->extended = py_module_extended;
2578 ops->start_transaction = py_module_start_transaction;
2579 ops->end_transaction = py_module_end_transaction;
2580 ops->del_transaction = py_module_del_transaction;
2582 ret = ldb_register_module(ops);
2584 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
2589 static PyObject *py_timestring(PyObject *module, PyObject *args)
2595 if (!PyArg_ParseTuple(args, "l", &val))
2598 tresult = ldb_timestring(NULL, t);
2599 ret = PyString_FromString(tresult);
2600 talloc_free(tresult);
2604 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
2607 if (!PyArg_ParseTuple(args, "s", &str))
2610 return PyInt_FromLong(ldb_string_to_time(str));
2613 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
2616 if (!PyArg_ParseTuple(args, "s", &name))
2618 return PyBool_FromLong(ldb_valid_attr_name(name));
2621 static PyMethodDef py_ldb_global_methods[] = {
2622 { "register_module", py_register_module, METH_VARARGS,
2623 "S.register_module(module) -> None\n"
2624 "Register a LDB module."},
2625 { "timestring", py_timestring, METH_VARARGS,
2626 "S.timestring(int) -> string\n"
2627 "Generate a LDAP time string from a UNIX timestamp" },
2628 { "string_to_time", py_string_to_time, METH_VARARGS,
2629 "S.string_to_time(string) -> int\n"
2630 "Parse a LDAP time string into a UNIX timestamp." },
2631 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2632 "S.valid_attr_name(name) -> bool\n"
2633 "Check whether the supplied name is a valid attribute name." },
2634 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2643 if (PyType_Ready(&PyLdbDn) < 0)
2646 if (PyType_Ready(&PyLdbMessage) < 0)
2649 if (PyType_Ready(&PyLdbMessageElement) < 0)
2652 if (PyType_Ready(&PyLdb) < 0)
2655 if (PyType_Ready(&PyLdbModule) < 0)
2658 if (PyType_Ready(&PyLdbTree) < 0)
2661 m = Py_InitModule3("ldb", py_ldb_global_methods,
2662 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2666 PyModule_AddObject(m, "SEQ_HIGHEST_SEQ", PyInt_FromLong(LDB_SEQ_HIGHEST_SEQ));
2667 PyModule_AddObject(m, "SEQ_HIGHEST_TIMESTAMP", PyInt_FromLong(LDB_SEQ_HIGHEST_TIMESTAMP));
2668 PyModule_AddObject(m, "SEQ_NEXT", PyInt_FromLong(LDB_SEQ_NEXT));
2669 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2670 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2671 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2672 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2674 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2675 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2676 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2677 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2679 PyModule_AddObject(m, "FLAG_MOD_ADD", PyInt_FromLong(LDB_FLAG_MOD_ADD));
2680 PyModule_AddObject(m, "FLAG_MOD_REPLACE", PyInt_FromLong(LDB_FLAG_MOD_REPLACE));
2681 PyModule_AddObject(m, "FLAG_MOD_DELETE", PyInt_FromLong(LDB_FLAG_MOD_DELETE));
2683 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2684 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2685 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2686 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2687 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2688 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2689 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2690 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2691 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2692 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2693 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2694 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2695 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2696 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2697 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2698 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2699 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2700 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2701 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2702 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2703 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2704 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2705 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2706 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2707 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2708 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2709 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2710 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2711 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2712 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2713 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2714 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2715 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2716 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2717 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2718 PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2719 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2720 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2721 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2723 PyModule_AddObject(m, "FLG_RDONLY", PyInt_FromLong(LDB_FLG_RDONLY));
2724 PyModule_AddObject(m, "FLG_NOSYNC", PyInt_FromLong(LDB_FLG_NOSYNC));
2725 PyModule_AddObject(m, "FLG_RECONNECT", PyInt_FromLong(LDB_FLG_RECONNECT));
2726 PyModule_AddObject(m, "FLG_NOMMAP", PyInt_FromLong(LDB_FLG_NOMMAP));
2728 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2730 PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL);
2731 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2734 Py_INCREF(&PyLdbDn);
2735 Py_INCREF(&PyLdbModule);
2736 Py_INCREF(&PyLdbMessage);
2737 Py_INCREF(&PyLdbMessageElement);
2738 Py_INCREF(&PyLdbTree);
2740 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2741 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2742 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2743 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2744 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2745 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);
2747 PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));