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 PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
362 PyLdbDnObject *py_ret;
368 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
369 if (py_ret == NULL) {
373 py_ret->mem_ctx = talloc_new(NULL);
374 py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
375 return (PyObject *)py_ret;
378 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
380 talloc_free(self->mem_ctx);
381 self->ob_type->tp_free(self);
384 PyTypeObject PyLdbDn = {
386 .tp_methods = py_ldb_dn_methods,
387 .tp_str = (reprfunc)py_ldb_dn_get_linearized,
388 .tp_repr = (reprfunc)py_ldb_dn_repr,
389 .tp_compare = (cmpfunc)py_ldb_dn_compare,
390 .tp_as_sequence = &py_ldb_dn_seq,
391 .tp_doc = "A LDB distinguished name.",
392 .tp_new = py_ldb_dn_new,
393 .tp_dealloc = (destructor)py_ldb_dn_dealloc,
394 .tp_basicsize = sizeof(PyLdbObject),
395 .tp_flags = Py_TPFLAGS_DEFAULT,
399 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
400 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
402 PyObject *fn = (PyObject *)context;
403 PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap));
406 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
410 if (!PyArg_ParseTuple(args, "O", &cb))
414 /* FIXME: Where do we DECREF cb ? */
415 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
420 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
423 if (!PyArg_ParseTuple(args, "I", &perms))
426 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
431 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
434 if (!PyArg_ParseTuple(args, "s", &modules_dir))
437 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
442 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
444 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
448 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
450 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
454 static PyObject *py_ldb_transaction_prepare_commit(PyLdbObject *self)
456 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_prepare_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
460 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
462 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
466 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
468 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
472 static PyObject *py_ldb_repr(PyLdbObject *self)
474 return PyString_FromFormat("<ldb connection>");
477 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
479 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
482 return PyLdbDn_FromDn(dn);
486 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
488 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
491 return PyLdbDn_FromDn(dn);
494 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
496 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
499 return PyLdbDn_FromDn(dn);
502 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
504 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
507 return PyLdbDn_FromDn(dn);
510 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list,
511 const char *paramname)
515 if (!PyList_Check(list)) {
516 PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
519 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
520 for (i = 0; i < PyList_Size(list); i++) {
521 PyObject *item = PyList_GetItem(list, i);
522 if (!PyString_Check(item)) {
523 PyErr_Format(PyExc_TypeError, "%s should be strings", paramname);
526 ret[i] = talloc_strndup(ret, PyString_AsString(item),
527 PyString_Size(item));
533 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
535 const char * const kwnames[] = { "url", "flags", "options", NULL };
537 PyObject *py_options = Py_None;
538 const char **options;
541 struct ldb_context *ldb;
543 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__",
544 discard_const_p(char *, kwnames),
545 &url, &flags, &py_options))
548 ldb = PyLdb_AsLdbContext(self);
550 if (py_options == Py_None) {
553 options = PyList_AsStringList(ldb, py_options, "options");
559 ret = ldb_connect(ldb, url, flags, options);
560 if (ret != LDB_SUCCESS) {
561 PyErr_SetLdbError(PyExc_LdbError, ret, ldb);
566 talloc_free(options);
570 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
573 struct ldb_context *ldb;
574 ret = (PyLdbObject *)type->tp_alloc(type, 0);
579 ret->mem_ctx = talloc_new(NULL);
580 ldb = ldb_init(ret->mem_ctx, NULL);
588 return (PyObject *)ret;
591 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
595 PyObject *py_options = Py_None;
597 const char **options;
598 const char * const kwnames[] = { "url", "flags", "options", NULL };
600 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO",
601 discard_const_p(char *, kwnames),
602 &url, &flags, &py_options))
605 if (py_options == Py_None) {
608 options = PyList_AsStringList(NULL, py_options, "options");
613 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
614 talloc_free(options);
616 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
621 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
624 PyObject *py_controls = Py_None;
625 struct ldb_context *ldb_ctx;
626 struct ldb_request *req;
627 struct ldb_control **parsed_controls;
628 struct ldb_message *msg;
630 if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls))
633 ldb_ctx = PyLdb_AsLdbContext(self);
635 if (py_controls == Py_None) {
636 parsed_controls = NULL;
638 const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
639 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
640 talloc_free(controls);
643 if (!PyLdbMessage_Check(py_msg)) {
644 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
647 msg = PyLdbMessage_AsMessage(py_msg);
649 ret = ldb_msg_sanity_check(ldb_ctx, msg);
650 if (ret != LDB_SUCCESS) {
651 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
655 ret = ldb_build_mod_req(&req, ldb_ctx, ldb_ctx,
659 ldb_op_default_callback,
662 if (ret != LDB_SUCCESS) {
663 PyErr_SetString(PyExc_TypeError, "failed to build request");
667 /* do request and autostart a transaction */
668 /* Then let's LDB handle the message error in case of pb as they are meaningful */
670 ret = ldb_transaction_start(ldb_ctx);
671 if (ret != LDB_SUCCESS) {
673 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
676 ret = ldb_request(ldb_ctx, req);
677 if (ret == LDB_SUCCESS) {
678 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
681 if (ret == LDB_SUCCESS) {
682 ret = ldb_transaction_commit(ldb_ctx);
684 ldb_transaction_cancel(ldb_ctx);
685 if (ldb_ctx->err_string == NULL) {
686 /* no error string was setup by the backend */
687 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
691 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
697 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
701 Py_ssize_t dict_pos, msg_pos;
702 struct ldb_message_element *msgel;
703 struct ldb_message *msg;
704 struct ldb_context *ldb_ctx;
705 struct ldb_request *req;
706 PyObject *key, *value;
707 PyObject *py_controls = Py_None;
709 struct ldb_control **parsed_controls;
711 if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls ))
714 mem_ctx = talloc_new(NULL);
715 if (mem_ctx == NULL) {
719 ldb_ctx = PyLdb_AsLdbContext(self);
721 if (py_controls == Py_None) {
722 parsed_controls = NULL;
724 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
725 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
726 talloc_free(controls);
728 if (PyDict_Check(py_msg)) {
729 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
730 msg = ldb_msg_new(mem_ctx);
731 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
732 msg_pos = dict_pos = 0;
734 if (!PyObject_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
735 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
736 talloc_free(mem_ctx);
739 if (msg->dn == NULL) {
740 PyErr_SetString(PyExc_TypeError, "dn set but not found");
741 talloc_free(mem_ctx);
746 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
747 char *key_str = PyString_AsString(key);
748 if (strcmp(key_str, "dn") != 0) {
749 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
751 PyErr_SetString(PyExc_TypeError, "unable to import element");
752 talloc_free(mem_ctx);
755 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
760 if (msg->dn == NULL) {
761 PyErr_SetString(PyExc_TypeError, "no dn set");
762 talloc_free(mem_ctx);
766 msg->num_elements = msg_pos;
768 msg = PyLdbMessage_AsMessage(py_msg);
771 ret = ldb_msg_sanity_check(ldb_ctx, msg);
772 if (ret != LDB_SUCCESS) {
773 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
774 talloc_free(mem_ctx);
778 ret = ldb_build_add_req(&req, ldb_ctx, mem_ctx, msg, parsed_controls,
779 NULL, ldb_op_default_callback, NULL);
780 if (ret != LDB_SUCCESS) {
781 PyErr_SetString(PyExc_TypeError, "failed to build request");
782 talloc_free(mem_ctx);
786 /* do request and autostart a transaction */
787 /* Then let's LDB handle the message error in case of pb as they are meaningful */
789 ret = ldb_transaction_start(ldb_ctx);
790 if (ret != LDB_SUCCESS) {
791 talloc_free(mem_ctx);
792 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
795 ret = ldb_request(ldb_ctx, req);
796 if (ret == LDB_SUCCESS) {
797 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
800 if (ret == LDB_SUCCESS) {
801 ret = ldb_transaction_commit(ldb_ctx);
803 ldb_transaction_cancel(ldb_ctx);
804 if (ldb_ctx->err_string == NULL) {
805 /* no error string was setup by the backend */
806 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
810 talloc_free(mem_ctx);
811 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
816 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
821 struct ldb_context *ldb_ctx;
822 struct ldb_request *req;
823 PyObject *py_controls = Py_None;
825 struct ldb_control **parsed_controls;
827 if (!PyArg_ParseTuple(args, "O|O", &py_dn, &py_controls))
830 mem_ctx = talloc_new(NULL);
831 if (mem_ctx == NULL) {
835 ldb_ctx = PyLdb_AsLdbContext(self);
837 if (py_controls == Py_None) {
838 parsed_controls = NULL;
840 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
841 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
842 talloc_free(controls);
845 if (!PyObject_AsDn(mem_ctx, py_dn, ldb_ctx, &dn)) {
846 talloc_free(mem_ctx);
850 ret = ldb_build_del_req(&req, ldb_ctx, mem_ctx, dn, parsed_controls,
851 NULL, ldb_op_default_callback, NULL);
852 if (ret != LDB_SUCCESS) {
853 PyErr_SetString(PyExc_TypeError, "failed to build request");
854 talloc_free(mem_ctx);
858 /* do request and autostart a transaction */
859 /* Then let's LDB handle the message error in case of pb as they are meaningful */
861 ret = ldb_transaction_start(ldb_ctx);
862 if (ret != LDB_SUCCESS) {
863 talloc_free(mem_ctx);
864 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
867 ret = ldb_request(ldb_ctx, req);
868 if (ret == LDB_SUCCESS) {
869 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
872 if (ret == LDB_SUCCESS) {
873 ret = ldb_transaction_commit(ldb_ctx);
875 ldb_transaction_cancel(ldb_ctx);
876 if (ldb_ctx->err_string == NULL) {
877 /* no error string was setup by the backend */
878 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
882 talloc_free(mem_ctx);
883 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
888 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
890 PyObject *py_dn1, *py_dn2;
891 struct ldb_dn *dn1, *dn2;
893 struct ldb_context *ldb;
895 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
898 mem_ctx = talloc_new(NULL);
899 if (mem_ctx == NULL) {
903 ldb = PyLdb_AsLdbContext(self);
904 if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
905 talloc_free(mem_ctx);
909 if (!PyObject_AsDn(mem_ctx, py_dn2, ldb, &dn2)) {
910 talloc_free(mem_ctx);
914 ret = ldb_rename(ldb, dn1, dn2);
915 talloc_free(mem_ctx);
916 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
921 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
924 if (!PyArg_ParseTuple(args, "s", &name))
927 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
932 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
934 char *attribute, *syntax;
937 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
940 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
942 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
947 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
952 /* We don't want this attached to the 'ldb' any more */
953 return Py_BuildValue(discard_const_p(char, "(iO)"),
955 PyLdbMessage_FromMessage(ldif->msg));
960 static PyObject *py_ldb_write_ldif(PyLdbMessageObject *self, PyObject *args)
964 struct ldb_ldif ldif;
969 if (!PyArg_ParseTuple(args, "Oi", &py_msg, &changetype))
972 if (!PyLdbMessage_Check(py_msg)) {
973 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for msg");
977 ldif.msg = PyLdbMessage_AsMessage(py_msg);
978 ldif.changetype = changetype;
980 mem_ctx = talloc_new(NULL);
982 string = ldb_ldif_write_string(PyLdb_AsLdbContext(self), mem_ctx, &ldif);
984 PyErr_SetString(PyExc_KeyError, "Failed to generate LDIF");
988 ret = PyString_FromString(string);
990 talloc_free(mem_ctx);
995 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
998 struct ldb_ldif *ldif;
1001 TALLOC_CTX *mem_ctx;
1003 if (!PyArg_ParseTuple(args, "s", &s))
1006 mem_ctx = talloc_new(NULL);
1011 list = PyList_New(0);
1012 while (s && *s != '\0') {
1013 ldif = ldb_ldif_read_string(self->ldb_ctx, &s);
1014 talloc_steal(mem_ctx, ldif);
1016 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
1018 PyErr_SetString(PyExc_ValueError, "unable to parse ldif string");
1019 talloc_free(mem_ctx);
1023 talloc_free(mem_ctx); /* The pyobject already has a reference to the things it needs */
1024 return PyObject_GetIter(list);
1027 static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args)
1029 PyObject *py_msg_old;
1030 PyObject *py_msg_new;
1031 struct ldb_message *diff;
1034 if (!PyArg_ParseTuple(args, "OO", &py_msg_old, &py_msg_new))
1037 if (!PyLdbMessage_Check(py_msg_old)) {
1038 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for old message");
1042 if (!PyLdbMessage_Check(py_msg_new)) {
1043 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for new message");
1047 diff = ldb_msg_diff(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg_old), PyLdbMessage_AsMessage(py_msg_new));
1049 PyErr_SetString(PyExc_RuntimeError, "Failed to generate the Ldb Message diff");
1053 py_ret = PyLdbMessage_FromMessage(diff);
1058 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
1060 const struct ldb_schema_attribute *a;
1061 struct ldb_val old_val;
1062 struct ldb_val new_val;
1063 TALLOC_CTX *mem_ctx;
1068 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
1071 mem_ctx = talloc_new(NULL);
1073 old_val.data = (uint8_t *)PyString_AsString(val);
1074 old_val.length = PyString_Size(val);
1076 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
1082 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
1083 talloc_free(mem_ctx);
1087 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
1089 talloc_free(mem_ctx);
1094 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
1096 PyObject *py_base = Py_None;
1097 int scope = LDB_SCOPE_DEFAULT;
1099 PyObject *py_attrs = Py_None;
1100 PyObject *py_controls = Py_None;
1101 const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
1103 struct ldb_result *res;
1104 struct ldb_request *req;
1106 struct ldb_context *ldb_ctx;
1107 struct ldb_control **parsed_controls;
1108 struct ldb_dn *base;
1111 /* type "int" rather than "enum" for "scope" is intentional */
1112 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
1113 discard_const_p(char *, kwnames),
1114 &py_base, &scope, &expr, &py_attrs, &py_controls))
1117 ldb_ctx = PyLdb_AsLdbContext(self);
1119 if (py_attrs == Py_None) {
1122 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
1127 if (py_base == Py_None) {
1128 base = ldb_get_default_basedn(ldb_ctx);
1130 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
1136 if (py_controls == Py_None) {
1137 parsed_controls = NULL;
1139 const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
1140 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
1141 talloc_free(controls);
1144 res = talloc_zero(ldb_ctx, struct ldb_result);
1151 ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
1158 ldb_search_default_callback,
1161 talloc_steal(req, attrs);
1163 if (ret != LDB_SUCCESS) {
1165 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
1169 ret = ldb_request(ldb_ctx, req);
1171 if (ret == LDB_SUCCESS) {
1172 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1177 if (ret != LDB_SUCCESS) {
1179 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
1183 py_ret = PyLdbResult_FromResult(res);
1190 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
1195 if (!PyArg_ParseTuple(args, "s", &name))
1198 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
1203 /* FIXME: More interpretation */
1208 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
1213 if (!PyArg_ParseTuple(args, "sO", &name, &data))
1216 /* FIXME: More interpretation */
1218 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
1223 static PyObject *py_ldb_modules(PyLdbObject *self)
1225 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
1226 PyObject *ret = PyList_New(0);
1227 struct ldb_module *mod;
1229 for (mod = ldb->modules; mod; mod = mod->next) {
1230 PyList_Append(ret, PyLdbModule_FromModule(mod));
1236 static PyMethodDef py_ldb_methods[] = {
1237 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
1238 "S.set_debug(callback) -> None\n"
1239 "Set callback for LDB debug messages.\n"
1240 "The callback should accept a debug level and debug text." },
1241 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
1242 "S.set_create_perms(mode) -> None\n"
1243 "Set mode to use when creating new LDB files." },
1244 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
1245 "S.set_modules_dir(path) -> None\n"
1246 "Set path LDB should search for modules" },
1247 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
1248 "S.transaction_start() -> None\n"
1249 "Start a new transaction." },
1250 { "transaction_prepare_commit", (PyCFunction)py_ldb_transaction_prepare_commit, METH_NOARGS,
1251 "S.transaction_prepare_commit() -> None\n"
1252 "prepare to commit a new transaction (2-stage commit)." },
1253 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
1254 "S.transaction_commit() -> None\n"
1255 "commit a new transaction." },
1256 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
1257 "S.transaction_cancel() -> None\n"
1258 "cancel a new transaction." },
1259 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
1261 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
1263 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
1265 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
1267 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
1269 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
1270 "S.connect(url, flags=0, options=None) -> None\n"
1271 "Connect to a LDB URL." },
1272 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
1273 "S.modify(message) -> None\n"
1274 "Modify an entry." },
1275 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
1276 "S.add(message) -> None\n"
1278 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
1279 "S.delete(dn) -> None\n"
1280 "Remove an entry." },
1281 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
1282 "S.rename(old_dn, new_dn) -> None\n"
1283 "Rename an entry." },
1284 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
1285 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1286 "Search in a database.\n"
1288 ":param base: Optional base DN to search\n"
1289 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1290 ":param expression: Optional search expression\n"
1291 ":param attrs: Attributes to return (defaults to all)\n"
1292 ":param controls: Optional list of controls\n"
1293 ":return: Iterator over Message objects\n"
1295 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
1297 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
1299 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
1301 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
1302 "S.parse_ldif(ldif) -> iter(messages)\n"
1303 "Parse a string formatted using LDIF." },
1304 { "write_ldif", (PyCFunction)py_ldb_write_ldif, METH_VARARGS,
1305 "S.write_ldif(message, changetype) -> ldif\n"
1306 "Print the message as a string formatted using LDIF." },
1307 { "msg_diff", (PyCFunction)py_ldb_msg_diff, METH_VARARGS,
1308 "S.msg_diff(Message) -> Message\n"
1309 "Return an LDB Message of the difference between two Message objects." },
1310 { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
1311 "S.get_opaque(name) -> value\n"
1312 "Get an opaque value set on this LDB connection. \n"
1313 ":note: The returned value may not be useful in Python."
1315 { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
1316 "S.set_opaque(name, value) -> None\n"
1317 "Set an opaque value on this LDB connection. \n"
1318 ":note: Passing incorrect values may cause crashes." },
1319 { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
1320 "S.modules() -> list\n"
1321 "Return the list of modules on this LDB connection " },
1325 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
1327 PyLdbModuleObject *ret;
1329 ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
1334 ret->mem_ctx = talloc_new(NULL);
1335 ret->mod = talloc_reference(ret->mem_ctx, mod);
1336 return (PyObject *)ret;
1339 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
1341 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
1344 static PyGetSetDef py_ldb_getset[] = {
1345 { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL },
1349 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
1351 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1353 struct ldb_result *result;
1357 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
1360 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
1361 if (ret != LDB_SUCCESS) {
1362 PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
1366 count = result->count;
1368 talloc_free(result);
1373 static PySequenceMethods py_ldb_seq = {
1374 .sq_contains = (objobjproc)py_ldb_contains,
1377 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
1381 ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1386 ret->mem_ctx = talloc_new(NULL);
1387 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1388 return (PyObject *)ret;
1391 static void py_ldb_dealloc(PyLdbObject *self)
1393 talloc_free(self->mem_ctx);
1394 self->ob_type->tp_free(self);
1397 PyTypeObject PyLdb = {
1398 .tp_name = "ldb.Ldb",
1399 .tp_methods = py_ldb_methods,
1400 .tp_repr = (reprfunc)py_ldb_repr,
1401 .tp_new = py_ldb_new,
1402 .tp_init = (initproc)py_ldb_init,
1403 .tp_dealloc = (destructor)py_ldb_dealloc,
1404 .tp_getset = py_ldb_getset,
1405 .tp_getattro = PyObject_GenericGetAttr,
1406 .tp_basicsize = sizeof(PyLdbObject),
1407 .tp_doc = "Connection to a LDB database.",
1408 .tp_as_sequence = &py_ldb_seq,
1409 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1412 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
1414 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1417 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
1419 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1422 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1424 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1428 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1430 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1434 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1436 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1440 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1442 PyObject *py_base, *py_tree, *py_attrs, *py_ret;
1444 struct ldb_request *req;
1445 const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1446 struct ldb_module *mod;
1447 const char * const*attrs;
1449 /* type "int" rather than "enum" for "scope" is intentional */
1450 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
1451 discard_const_p(char *, kwnames),
1452 &py_base, &scope, &py_tree, &py_attrs))
1457 if (py_attrs == Py_None) {
1460 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
1465 ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base),
1466 scope, NULL /* expr */, attrs,
1467 NULL /* controls */, NULL, NULL, NULL);
1469 talloc_steal(req, attrs);
1471 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1473 req->op.search.res = NULL;
1475 ret = mod->ops->search(mod, req);
1477 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1479 py_ret = PyLdbResult_FromResult(req->op.search.res);
1487 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1489 struct ldb_request *req;
1490 PyObject *py_message;
1492 struct ldb_module *mod;
1494 if (!PyArg_ParseTuple(args, "O", &py_message))
1497 req = talloc_zero(NULL, struct ldb_request);
1498 req->operation = LDB_ADD;
1499 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1501 mod = PyLdbModule_AsModule(self);
1502 ret = mod->ops->add(mod, req);
1504 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1509 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
1512 struct ldb_request *req;
1513 PyObject *py_message;
1514 struct ldb_module *mod;
1516 if (!PyArg_ParseTuple(args, "O", &py_message))
1519 req = talloc_zero(NULL, struct ldb_request);
1520 req->operation = LDB_MODIFY;
1521 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1523 mod = PyLdbModule_AsModule(self);
1524 ret = mod->ops->modify(mod, req);
1526 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1531 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
1534 struct ldb_request *req;
1537 if (!PyArg_ParseTuple(args, "O", &py_dn))
1540 req = talloc_zero(NULL, struct ldb_request);
1541 req->operation = LDB_DELETE;
1542 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1544 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1546 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1551 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1554 struct ldb_request *req;
1555 PyObject *py_dn1, *py_dn2;
1557 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1560 req = talloc_zero(NULL, struct ldb_request);
1562 req->operation = LDB_RENAME;
1563 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1564 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1566 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1568 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1573 static PyMethodDef py_ldb_module_methods[] = {
1574 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1575 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1576 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1577 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1578 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1579 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1580 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1581 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1585 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
1587 talloc_free(self->mem_ctx);
1588 self->ob_type->tp_free(self);
1591 PyTypeObject PyLdbModule = {
1592 .tp_name = "ldb.LdbModule",
1593 .tp_methods = py_ldb_module_methods,
1594 .tp_repr = (reprfunc)py_ldb_module_repr,
1595 .tp_str = (reprfunc)py_ldb_module_str,
1596 .tp_basicsize = sizeof(PyLdbModuleObject),
1597 .tp_dealloc = (destructor)py_ldb_module_dealloc,
1598 .tp_flags = Py_TPFLAGS_DEFAULT,
1603 * Create a ldb_message_element from a Python object.
1605 * This will accept any sequence objects that contains strings, or
1608 * A reference to set_obj will be borrowed.
1610 * @param mem_ctx Memory context
1611 * @param set_obj Python object to convert
1612 * @param flags ldb_message_element flags to set
1613 * @param attr_name Name of the attribute
1614 * @return New ldb_message_element, allocated as child of mem_ctx
1616 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1617 PyObject *set_obj, int flags,
1618 const char *attr_name)
1620 struct ldb_message_element *me;
1622 if (PyLdbMessageElement_Check(set_obj))
1623 return talloc_reference(mem_ctx,
1624 PyLdbMessageElement_AsMessageElement(set_obj));
1626 me = talloc(mem_ctx, struct ldb_message_element);
1628 me->name = talloc_strdup(me, attr_name);
1630 if (PyString_Check(set_obj)) {
1632 me->values = talloc_array(me, struct ldb_val, me->num_values);
1633 me->values[0].length = PyString_Size(set_obj);
1634 me->values[0].data = talloc_memdup(me,
1635 (uint8_t *)PyString_AsString(set_obj), me->values[0].length+1);
1636 } else if (PySequence_Check(set_obj)) {
1638 me->num_values = PySequence_Size(set_obj);
1639 me->values = talloc_array(me, struct ldb_val, me->num_values);
1640 for (i = 0; i < me->num_values; i++) {
1641 PyObject *obj = PySequence_GetItem(set_obj, i);
1643 me->values[i].length = PyString_Size(obj);
1644 me->values[i].data = talloc_memdup(me,
1645 (uint8_t *)PyString_AsString(obj), me->values[i].length+1);
1656 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
1657 struct ldb_message_element *me)
1662 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1663 result = PyList_New(me->num_values);
1665 for (i = 0; i < me->num_values; i++) {
1666 PyList_SetItem(result, i,
1667 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1673 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1676 if (!PyArg_ParseTuple(args, "i", &i))
1678 if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1681 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1682 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1685 static PyObject *py_ldb_msg_element_flags(PyLdbMessageElementObject *self, PyObject *args)
1687 struct ldb_message_element *el;
1689 el = PyLdbMessageElement_AsMessageElement(self);
1690 return PyInt_FromLong(el->flags);
1693 static PyObject *py_ldb_msg_element_set_flags(PyLdbMessageElementObject *self, PyObject *args)
1696 struct ldb_message_element *el;
1697 if (!PyArg_ParseTuple(args, "i", &flags))
1700 el = PyLdbMessageElement_AsMessageElement(self);
1705 static PyMethodDef py_ldb_msg_element_methods[] = {
1706 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1707 { "set_flags", (PyCFunction)py_ldb_msg_element_set_flags, METH_VARARGS, NULL },
1708 { "flags", (PyCFunction)py_ldb_msg_element_flags, METH_NOARGS, NULL },
1712 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1714 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1717 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1719 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1720 if (idx < 0 || idx >= el->num_values) {
1721 PyErr_SetString(PyExc_IndexError, "Out of range");
1724 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1727 static PySequenceMethods py_ldb_msg_element_seq = {
1728 .sq_length = (lenfunc)py_ldb_msg_element_len,
1729 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1732 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1734 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1735 PyLdbMessageElement_AsMessageElement(other));
1738 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1740 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1743 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
1745 PyLdbMessageElementObject *ret;
1746 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1751 ret->mem_ctx = talloc_new(NULL);
1752 if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1757 return (PyObject *)ret;
1760 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1762 PyObject *py_elements = NULL;
1763 struct ldb_message_element *el;
1766 const char * const kwnames[] = { "elements", "flags", "name", NULL };
1767 PyLdbMessageElementObject *ret;
1768 TALLOC_CTX *mem_ctx;
1770 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois",
1771 discard_const_p(char *, kwnames),
1772 &py_elements, &flags, &name))
1775 mem_ctx = talloc_new(NULL);
1776 if (mem_ctx == NULL) {
1781 el = talloc_zero(mem_ctx, struct ldb_message_element);
1783 if (py_elements != NULL) {
1785 if (PyString_Check(py_elements)) {
1787 el->values = talloc_array(el, struct ldb_val, 1);
1788 el->values[0].length = PyString_Size(py_elements);
1789 el->values[0].data = talloc_memdup(el,
1790 (uint8_t *)PyString_AsString(py_elements), el->values[0].length+1);
1791 } else if (PySequence_Check(py_elements)) {
1792 el->num_values = PySequence_Size(py_elements);
1793 el->values = talloc_array(el, struct ldb_val, el->num_values);
1794 for (i = 0; i < el->num_values; i++) {
1795 PyObject *item = PySequence_GetItem(py_elements, i);
1796 if (!PyString_Check(item)) {
1797 PyErr_Format(PyExc_TypeError,
1798 "Expected string as element %d in list",
1800 talloc_free(mem_ctx);
1803 el->values[i].length = PyString_Size(item);
1804 el->values[i].data = talloc_memdup(el,
1805 (uint8_t *)PyString_AsString(item), el->values[i].length+1);
1808 PyErr_SetString(PyExc_TypeError,
1809 "Expected string or list");
1810 talloc_free(mem_ctx);
1816 el->name = talloc_strdup(el, name);
1818 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1821 talloc_free(mem_ctx);
1825 ret->mem_ctx = mem_ctx;
1827 return (PyObject *)ret;
1830 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1832 char *element_str = NULL;
1834 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1837 for (i = 0; i < el->num_values; i++) {
1838 PyObject *o = py_ldb_msg_element_find(self, i);
1839 if (element_str == NULL)
1840 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1842 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1845 if (element_str != NULL) {
1846 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1847 talloc_free(element_str);
1849 ret = PyString_FromString("MessageElement([])");
1855 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
1857 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1859 if (el->num_values == 1)
1860 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1865 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
1867 talloc_free(self->mem_ctx);
1868 self->ob_type->tp_free(self);
1871 PyTypeObject PyLdbMessageElement = {
1872 .tp_name = "ldb.MessageElement",
1873 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1874 .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1875 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1876 .tp_str = (reprfunc)py_ldb_msg_element_str,
1877 .tp_methods = py_ldb_msg_element_methods,
1878 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1879 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1880 .tp_as_sequence = &py_ldb_msg_element_seq,
1881 .tp_new = py_ldb_msg_element_new,
1882 .tp_flags = Py_TPFLAGS_DEFAULT,
1885 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
1888 if (!PyArg_ParseTuple(args, "s", &name))
1891 ldb_msg_remove_attr(self->msg, name);
1896 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
1898 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1900 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1901 if (msg->dn != NULL) {
1902 PyList_SetItem(obj, j, PyString_FromString("dn"));
1905 for (i = 0; i < msg->num_elements; i++) {
1906 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1912 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
1914 struct ldb_message_element *el;
1916 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1917 if (!PyString_Check(py_name)) {
1918 PyErr_SetNone(PyExc_TypeError);
1921 name = PyString_AsString(py_name);
1922 if (!strcmp(name, "dn"))
1923 return PyLdbDn_FromDn(msg->dn);
1924 el = ldb_msg_find_element(msg, name);
1928 return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg);
1931 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
1933 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
1935 PyErr_SetString(PyExc_KeyError, "No such element");
1941 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
1943 PyObject *name, *ret;
1944 if (!PyArg_ParseTuple(args, "O", &name))
1947 ret = py_ldb_msg_getitem_helper(self, name);
1949 if (PyErr_Occurred())
1956 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
1958 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1960 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
1962 if (msg->dn != NULL) {
1963 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
1966 for (i = 0; i < msg->num_elements; i++, j++) {
1967 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], self->msg)));
1972 static PyMethodDef py_ldb_msg_methods[] = {
1973 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
1974 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
1975 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
1976 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
1980 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
1982 PyObject *list, *iter;
1984 list = py_ldb_msg_keys(self);
1985 iter = PyObject_GetIter(list);
1990 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
1994 if (!PyString_Check(name)) {
1995 PyErr_SetNone(PyExc_TypeError);
1999 attr_name = PyString_AsString(name);
2000 if (value == NULL) {
2002 ldb_msg_remove_attr(self->msg, attr_name);
2004 struct ldb_message_element *el = PyObject_AsMessageElement(self->msg,
2005 value, 0, attr_name);
2008 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
2009 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
2014 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
2016 return PyLdbMessage_AsMessage(self)->num_elements;
2019 static PyMappingMethods py_ldb_msg_mapping = {
2020 .mp_length = (lenfunc)py_ldb_msg_length,
2021 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
2022 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
2025 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2027 const char * const kwnames[] = { "dn", NULL };
2028 struct ldb_message *ret;
2029 TALLOC_CTX *mem_ctx;
2030 PyObject *pydn = NULL;
2031 PyLdbMessageObject *py_ret;
2033 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
2034 discard_const_p(char *, kwnames),
2038 mem_ctx = talloc_new(NULL);
2039 if (mem_ctx == NULL) {
2044 ret = ldb_msg_new(mem_ctx);
2046 talloc_free(mem_ctx);
2053 if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) {
2054 talloc_free(mem_ctx);
2057 ret->dn = talloc_reference(ret, dn);
2060 py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
2061 if (py_ret == NULL) {
2063 talloc_free(mem_ctx);
2067 py_ret->mem_ctx = mem_ctx;
2069 return (PyObject *)py_ret;
2072 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
2074 PyLdbMessageObject *ret;
2076 ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
2081 ret->mem_ctx = talloc_new(NULL);
2082 ret->msg = talloc_reference(ret->mem_ctx, msg);
2083 return (PyObject *)ret;
2086 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
2088 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2089 return PyLdbDn_FromDn(msg->dn);
2092 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
2094 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2095 if (!PyLdbDn_Check(value)) {
2096 PyErr_SetNone(PyExc_TypeError);
2100 msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value));
2104 static PyGetSetDef py_ldb_msg_getset[] = {
2105 { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
2109 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
2111 PyObject *dict = PyDict_New(), *ret;
2112 if (PyDict_Update(dict, (PyObject *)self) != 0)
2114 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
2119 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
2121 talloc_free(self->mem_ctx);
2122 self->ob_type->tp_free(self);
2125 static int py_ldb_msg_compare(PyLdbMessageObject *py_msg1,
2126 PyLdbMessageObject *py_msg2)
2128 struct ldb_message *msg1 = PyLdbMessage_AsMessage(py_msg1),
2129 *msg2 = PyLdbMessage_AsMessage(py_msg2);
2133 if ((msg1->dn != NULL) || (msg2->dn != NULL)) {
2134 ret = ldb_dn_compare(msg1->dn, msg2->dn);
2140 ret = msg1->num_elements - msg2->num_elements;
2145 for (i = 0; i < msg1->num_elements; i++) {
2146 ret = ldb_msg_element_compare_name(&msg1->elements[i],
2147 &msg2->elements[i]);
2152 ret = ldb_msg_element_compare(&msg1->elements[i],
2153 &msg2->elements[i]);
2162 PyTypeObject PyLdbMessage = {
2163 .tp_name = "ldb.Message",
2164 .tp_methods = py_ldb_msg_methods,
2165 .tp_getset = py_ldb_msg_getset,
2166 .tp_as_mapping = &py_ldb_msg_mapping,
2167 .tp_basicsize = sizeof(PyLdbMessageObject),
2168 .tp_dealloc = (destructor)py_ldb_msg_dealloc,
2169 .tp_new = py_ldb_msg_new,
2170 .tp_repr = (reprfunc)py_ldb_msg_repr,
2171 .tp_flags = Py_TPFLAGS_DEFAULT,
2172 .tp_iter = (getiterfunc)py_ldb_msg_iter,
2173 .tp_compare = (cmpfunc)py_ldb_msg_compare,
2176 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
2178 PyLdbTreeObject *ret;
2180 ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
2186 ret->mem_ctx = talloc_new(NULL);
2187 ret->tree = talloc_reference(ret->mem_ctx, tree);
2188 return (PyObject *)ret;
2191 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
2193 talloc_free(self->mem_ctx);
2194 self->ob_type->tp_free(self);
2197 PyTypeObject PyLdbTree = {
2198 .tp_name = "ldb.Tree",
2199 .tp_basicsize = sizeof(PyLdbTreeObject),
2200 .tp_dealloc = (destructor)py_ldb_tree_dealloc,
2201 .tp_flags = Py_TPFLAGS_DEFAULT,
2205 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
2207 PyObject *py_ldb = (PyObject *)mod->private_data;
2208 PyObject *py_result, *py_base, *py_attrs, *py_tree;
2210 py_base = PyLdbDn_FromDn(req->op.search.base);
2212 if (py_base == NULL)
2213 return LDB_ERR_OPERATIONS_ERROR;
2215 py_tree = PyLdbTree_FromTree(req->op.search.tree);
2217 if (py_tree == NULL)
2218 return LDB_ERR_OPERATIONS_ERROR;
2220 if (req->op.search.attrs == NULL) {
2224 for (len = 0; req->op.search.attrs[len]; len++);
2225 py_attrs = PyList_New(len);
2226 for (i = 0; i < len; i++)
2227 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
2230 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
2231 discard_const_p(char, "OiOO"),
2232 py_base, req->op.search.scope, py_tree, py_attrs);
2234 Py_DECREF(py_attrs);
2238 if (py_result == NULL) {
2239 return LDB_ERR_PYTHON_EXCEPTION;
2242 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
2243 if (req->op.search.res == NULL) {
2244 return LDB_ERR_PYTHON_EXCEPTION;
2247 Py_DECREF(py_result);
2252 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
2254 PyObject *py_ldb = (PyObject *)mod->private_data;
2255 PyObject *py_result, *py_msg;
2257 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
2259 if (py_msg == NULL) {
2260 return LDB_ERR_OPERATIONS_ERROR;
2263 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
2264 discard_const_p(char, "O"),
2269 if (py_result == NULL) {
2270 return LDB_ERR_PYTHON_EXCEPTION;
2273 Py_DECREF(py_result);
2278 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
2280 PyObject *py_ldb = (PyObject *)mod->private_data;
2281 PyObject *py_result, *py_msg;
2283 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
2285 if (py_msg == NULL) {
2286 return LDB_ERR_OPERATIONS_ERROR;
2289 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
2290 discard_const_p(char, "O"),
2295 if (py_result == NULL) {
2296 return LDB_ERR_PYTHON_EXCEPTION;
2299 Py_DECREF(py_result);
2304 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
2306 PyObject *py_ldb = (PyObject *)mod->private_data;
2307 PyObject *py_result, *py_dn;
2309 py_dn = PyLdbDn_FromDn(req->op.del.dn);
2312 return LDB_ERR_OPERATIONS_ERROR;
2314 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
2315 discard_const_p(char, "O"),
2318 if (py_result == NULL) {
2319 return LDB_ERR_PYTHON_EXCEPTION;
2322 Py_DECREF(py_result);
2327 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
2329 PyObject *py_ldb = (PyObject *)mod->private_data;
2330 PyObject *py_result, *py_olddn, *py_newdn;
2332 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
2334 if (py_olddn == NULL)
2335 return LDB_ERR_OPERATIONS_ERROR;
2337 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
2339 if (py_newdn == NULL)
2340 return LDB_ERR_OPERATIONS_ERROR;
2342 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
2343 discard_const_p(char, "OO"),
2344 py_olddn, py_newdn);
2346 Py_DECREF(py_olddn);
2347 Py_DECREF(py_newdn);
2349 if (py_result == NULL) {
2350 return LDB_ERR_PYTHON_EXCEPTION;
2353 Py_DECREF(py_result);
2358 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
2360 PyObject *py_ldb = (PyObject *)mod->private_data;
2361 PyObject *py_result;
2363 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
2364 discard_const_p(char, ""));
2366 return LDB_ERR_OPERATIONS_ERROR;
2369 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
2371 PyObject *py_ldb = (PyObject *)mod->private_data;
2372 PyObject *py_result;
2374 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
2375 discard_const_p(char, ""));
2377 return LDB_ERR_OPERATIONS_ERROR;
2380 static int py_module_start_transaction(struct ldb_module *mod)
2382 PyObject *py_ldb = (PyObject *)mod->private_data;
2383 PyObject *py_result;
2385 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
2386 discard_const_p(char, ""));
2388 if (py_result == NULL) {
2389 return LDB_ERR_PYTHON_EXCEPTION;
2392 Py_DECREF(py_result);
2397 static int py_module_end_transaction(struct ldb_module *mod)
2399 PyObject *py_ldb = (PyObject *)mod->private_data;
2400 PyObject *py_result;
2402 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
2403 discard_const_p(char, ""));
2405 if (py_result == NULL) {
2406 return LDB_ERR_PYTHON_EXCEPTION;
2409 Py_DECREF(py_result);
2414 static int py_module_del_transaction(struct ldb_module *mod)
2416 PyObject *py_ldb = (PyObject *)mod->private_data;
2417 PyObject *py_result;
2419 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
2420 discard_const_p(char, ""));
2422 if (py_result == NULL) {
2423 return LDB_ERR_PYTHON_EXCEPTION;
2426 Py_DECREF(py_result);
2431 static int py_module_destructor(struct ldb_module *mod)
2433 Py_DECREF((PyObject *)mod->private_data);
2437 static int py_module_init(struct ldb_module *mod)
2439 PyObject *py_class = (PyObject *)mod->ops->private_data;
2440 PyObject *py_result, *py_next, *py_ldb;
2442 py_ldb = PyLdb_FromLdbContext(mod->ldb);
2445 return LDB_ERR_OPERATIONS_ERROR;
2447 py_next = PyLdbModule_FromModule(mod->next);
2449 if (py_next == NULL)
2450 return LDB_ERR_OPERATIONS_ERROR;
2452 py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
2455 if (py_result == NULL) {
2456 return LDB_ERR_PYTHON_EXCEPTION;
2459 mod->private_data = py_result;
2461 talloc_set_destructor(mod, py_module_destructor);
2463 return ldb_next_init(mod);
2466 static PyObject *py_register_module(PyObject *module, PyObject *args)
2469 struct ldb_module_ops *ops;
2472 if (!PyArg_ParseTuple(args, "O", &input))
2475 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
2481 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name"))));
2484 ops->private_data = input;
2485 ops->init_context = py_module_init;
2486 ops->search = py_module_search;
2487 ops->add = py_module_add;
2488 ops->modify = py_module_modify;
2489 ops->del = py_module_del;
2490 ops->rename = py_module_rename;
2491 ops->request = py_module_request;
2492 ops->extended = py_module_extended;
2493 ops->start_transaction = py_module_start_transaction;
2494 ops->end_transaction = py_module_end_transaction;
2495 ops->del_transaction = py_module_del_transaction;
2497 ret = ldb_register_module(ops);
2499 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
2504 static PyObject *py_timestring(PyObject *module, PyObject *args)
2510 if (!PyArg_ParseTuple(args, "l", &val))
2513 tresult = ldb_timestring(NULL, t);
2514 ret = PyString_FromString(tresult);
2515 talloc_free(tresult);
2519 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
2522 if (!PyArg_ParseTuple(args, "s", &str))
2525 return PyInt_FromLong(ldb_string_to_time(str));
2528 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
2531 if (!PyArg_ParseTuple(args, "s", &name))
2533 return PyBool_FromLong(ldb_valid_attr_name(name));
2536 static PyMethodDef py_ldb_global_methods[] = {
2537 { "register_module", py_register_module, METH_VARARGS,
2538 "S.register_module(module) -> None\n"
2539 "Register a LDB module."},
2540 { "timestring", py_timestring, METH_VARARGS,
2541 "S.timestring(int) -> string\n"
2542 "Generate a LDAP time string from a UNIX timestamp" },
2543 { "string_to_time", py_string_to_time, METH_VARARGS,
2544 "S.string_to_time(string) -> int\n"
2545 "Parse a LDAP time string into a UNIX timestamp." },
2546 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2547 "S.valid_attr_name(name) -> bool\n"
2548 "Check whether the supplied name is a valid attribute name." },
2549 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2558 if (PyType_Ready(&PyLdbDn) < 0)
2561 if (PyType_Ready(&PyLdbMessage) < 0)
2564 if (PyType_Ready(&PyLdbMessageElement) < 0)
2567 if (PyType_Ready(&PyLdb) < 0)
2570 if (PyType_Ready(&PyLdbModule) < 0)
2573 if (PyType_Ready(&PyLdbTree) < 0)
2576 m = Py_InitModule3("ldb", py_ldb_global_methods,
2577 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2581 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2582 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2583 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2584 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2586 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2587 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2588 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2589 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2591 PyModule_AddObject(m, "FLAG_MOD_ADD", PyInt_FromLong(LDB_FLAG_MOD_ADD));
2592 PyModule_AddObject(m, "FLAG_MOD_REPLACE", PyInt_FromLong(LDB_FLAG_MOD_REPLACE));
2593 PyModule_AddObject(m, "FLAG_MOD_DELETE", PyInt_FromLong(LDB_FLAG_MOD_DELETE));
2595 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2596 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2597 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2598 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2599 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2600 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2601 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2602 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2603 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2604 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2605 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2606 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2607 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2608 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2609 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2610 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2611 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2612 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2613 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2614 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2615 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2616 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2617 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2618 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2619 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2620 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2621 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2622 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2623 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2624 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2625 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2626 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2627 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2628 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2629 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2630 PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2631 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2632 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2633 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2635 PyModule_AddObject(m, "FLG_RDONLY", PyInt_FromLong(LDB_FLG_RDONLY));
2636 PyModule_AddObject(m, "FLG_NOSYNC", PyInt_FromLong(LDB_FLG_NOSYNC));
2637 PyModule_AddObject(m, "FLG_RECONNECT", PyInt_FromLong(LDB_FLG_RECONNECT));
2638 PyModule_AddObject(m, "FLG_NOMMAP", PyInt_FromLong(LDB_FLG_NOMMAP));
2640 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2642 PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL);
2643 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2646 Py_INCREF(&PyLdbDn);
2647 Py_INCREF(&PyLdbModule);
2648 Py_INCREF(&PyLdbMessage);
2649 Py_INCREF(&PyLdbMessageElement);
2650 Py_INCREF(&PyLdbTree);
2652 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2653 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2654 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2655 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2656 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2657 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);