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 /* There's no Py_ssize_t in 2.4, apparently */
32 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
33 typedef int Py_ssize_t;
34 typedef inquiry lenfunc;
35 typedef intargfunc ssizeargfunc;
38 #ifndef Py_RETURN_NONE
39 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
42 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
44 if (ret == LDB_ERR_PYTHON_EXCEPTION)
45 return; /* Python exception should already be set, just keep that */
47 PyErr_SetObject(error,
48 Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
49 ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
52 static PyObject *PyExc_LdbError;
54 extern PyTypeObject PyLdbMessage;
55 extern PyTypeObject PyLdbModule;
56 extern PyTypeObject PyLdbDn;
57 extern PyTypeObject PyLdb;
58 extern PyTypeObject PyLdbMessageElement;
59 extern PyTypeObject PyLdbTree;
61 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx);
63 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
64 struct ldb_message_element *el,
67 struct ldb_val new_val;
68 TALLOC_CTX *mem_ctx = talloc_new(NULL);
73 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
81 * Create a Python object from a ldb_result.
83 * @param result LDB result to convert
84 * @return Python object with converted result (a list object)
86 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
93 ret = PyList_New(result->count);
94 for (i = 0; i < result->count; i++) {
95 PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i]));
101 * Create a LDB Result from a Python object.
102 * If conversion fails, NULL will be returned and a Python exception set.
104 * @param mem_ctx Memory context in which to allocate the LDB Result
105 * @param obj Python object to convert
106 * @return a ldb_result, or NULL if the conversion failed
108 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx,
111 struct ldb_result *res;
117 res = talloc_zero(mem_ctx, struct ldb_result);
118 res->count = PyList_Size(obj);
119 res->msgs = talloc_array(res, struct ldb_message *, res->count);
120 for (i = 0; i < res->count; i++) {
121 PyObject *item = PyList_GetItem(obj, i);
122 res->msgs[i] = PyLdbMessage_AsMessage(item);
127 static PyObject *py_ldb_dn_validate(PyLdbDnObject *self)
129 return PyBool_FromLong(ldb_dn_validate(self->dn));
132 static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self)
134 return PyBool_FromLong(ldb_dn_is_valid(self->dn));
137 static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self)
139 return PyBool_FromLong(ldb_dn_is_special(self->dn));
142 static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self)
144 return PyBool_FromLong(ldb_dn_is_null(self->dn));
147 static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self)
149 return PyString_FromString(ldb_dn_get_casefold(self->dn));
152 static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self)
154 return PyString_FromString(ldb_dn_get_linearized(self->dn));
157 static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self)
159 return PyString_FromString(ldb_dn_canonical_string(self->dn, self->dn));
162 static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self)
164 return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn));
167 static PyObject *py_ldb_dn_repr(PyLdbDnObject *self)
169 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn))));
172 static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args)
176 if (!PyArg_ParseTuple(args, "s", &name))
179 return ldb_dn_check_special(self->dn, name)?Py_True:Py_False;
182 static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
185 ret = ldb_dn_compare(dn1->dn, dn2->dn);
186 if (ret < 0) ret = -1;
187 if (ret > 0) ret = 1;
191 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
193 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
194 struct ldb_dn *parent;
195 PyLdbDnObject *py_ret;
196 TALLOC_CTX *mem_ctx = talloc_new(NULL);
198 parent = ldb_dn_get_parent(mem_ctx, dn);
199 if (parent == NULL) {
200 talloc_free(mem_ctx);
204 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
205 if (py_ret == NULL) {
207 talloc_free(mem_ctx);
210 py_ret->mem_ctx = mem_ctx;
212 return (PyObject *)py_ret;
215 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
217 static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
220 struct ldb_dn *dn, *other;
221 if (!PyArg_ParseTuple(args, "O", &py_other))
224 dn = PyLdbDn_AsDn((PyObject *)self);
226 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
229 return ldb_dn_add_child(dn, other)?Py_True:Py_False;
232 static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
235 struct ldb_dn *other, *dn;
236 if (!PyArg_ParseTuple(args, "O", &py_other))
239 dn = PyLdbDn_AsDn((PyObject *)self);
241 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
244 return ldb_dn_add_base(dn, other)?Py_True:Py_False;
247 static PyMethodDef py_ldb_dn_methods[] = {
248 { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS,
249 "S.validate() -> bool\n"
250 "Validate DN is correct." },
251 { "is_valid", (PyCFunction)py_ldb_dn_is_valid, METH_NOARGS,
252 "S.is_valid() -> bool\n" },
253 { "is_special", (PyCFunction)py_ldb_dn_is_special, METH_NOARGS,
254 "S.is_special() -> bool\n"
255 "Check whether this is a special LDB DN." },
256 { "is_null", (PyCFunction)py_ldb_dn_is_null, METH_NOARGS,
257 "Check whether this is a null DN." },
258 { "get_casefold", (PyCFunction)py_ldb_dn_get_casefold, METH_NOARGS,
260 { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS,
262 { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
263 "S.canonical_str() -> string\n"
264 "Canonical version of this DN (like a posix path)." },
265 { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
266 "S.canonical_ex_str() -> string\n"
267 "Canonical version of this DN (like a posix path, with terminating newline)." },
268 { "check_special", (PyCFunction)py_ldb_dn_is_special, METH_VARARGS,
270 { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS,
272 "Get the parent for this DN." },
273 { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS,
274 "S.add_child(dn) -> None\n"
275 "Add a child DN to this DN." },
276 { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
277 "S.add_base(dn) -> None\n"
278 "Add a base DN to this DN." },
279 { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
284 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
286 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
289 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
291 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self),
293 PyLdbDnObject *py_ret;
295 if (!PyObject_AsDn(NULL, py_other, NULL, &other))
298 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
299 if (py_ret == NULL) {
303 py_ret->mem_ctx = talloc_new(NULL);
304 py_ret->dn = ldb_dn_copy(py_ret->mem_ctx, dn);
305 ldb_dn_add_child(py_ret->dn, other);
306 return (PyObject *)py_ret;
309 static PySequenceMethods py_ldb_dn_seq = {
310 .sq_length = (lenfunc)py_ldb_dn_len,
311 .sq_concat = (binaryfunc)py_ldb_dn_concat,
314 static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
319 struct ldb_context *ldb_ctx;
321 PyLdbDnObject *py_ret;
322 const char * const kwnames[] = { "ldb", "dn", NULL };
324 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os",
325 discard_const_p(char *, kwnames),
329 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
331 mem_ctx = talloc_new(NULL);
332 if (mem_ctx == NULL) {
337 ret = ldb_dn_new(mem_ctx, ldb_ctx, str);
339 if (ret == NULL || !ldb_dn_validate(ret)) {
340 talloc_free(mem_ctx);
341 PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
345 py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0);
347 talloc_free(mem_ctx);
351 py_ret->mem_ctx = mem_ctx;
353 return (PyObject *)py_ret;
356 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
358 talloc_free(self->mem_ctx);
359 self->ob_type->tp_free(self);
362 PyTypeObject PyLdbDn = {
364 .tp_methods = py_ldb_dn_methods,
365 .tp_str = (reprfunc)py_ldb_dn_get_linearized,
366 .tp_repr = (reprfunc)py_ldb_dn_repr,
367 .tp_compare = (cmpfunc)py_ldb_dn_compare,
368 .tp_as_sequence = &py_ldb_dn_seq,
369 .tp_doc = "A LDB distinguished name.",
370 .tp_new = py_ldb_dn_new,
371 .tp_dealloc = (destructor)py_ldb_dn_dealloc,
372 .tp_basicsize = sizeof(PyLdbObject),
373 .tp_flags = Py_TPFLAGS_DEFAULT,
377 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
378 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
380 PyObject *fn = (PyObject *)context;
381 PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap));
384 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
388 if (!PyArg_ParseTuple(args, "O", &cb))
392 /* FIXME: Where do we DECREF cb ? */
393 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
398 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
401 if (!PyArg_ParseTuple(args, "I", &perms))
404 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
409 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
412 if (!PyArg_ParseTuple(args, "s", &modules_dir))
415 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
420 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
422 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
426 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
428 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
432 static PyObject *py_ldb_transaction_prepare_commit(PyLdbObject *self)
434 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_prepare_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
438 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
440 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
444 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
446 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
450 static PyObject *py_ldb_repr(PyLdbObject *self)
452 return PyString_FromFormat("<ldb connection>");
455 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
457 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
460 return PyLdbDn_FromDn(dn);
464 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
466 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
469 return PyLdbDn_FromDn(dn);
472 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
474 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
477 return PyLdbDn_FromDn(dn);
480 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
482 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
485 return PyLdbDn_FromDn(dn);
488 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list,
489 const char *paramname)
493 if (!PyList_Check(list)) {
494 PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
497 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
498 for (i = 0; i < PyList_Size(list); i++) {
499 PyObject *item = PyList_GetItem(list, i);
500 if (!PyString_Check(item)) {
501 PyErr_Format(PyExc_TypeError, "%s should be strings", paramname);
504 ret[i] = talloc_strndup(ret, PyString_AsString(item),
505 PyString_Size(item));
511 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
513 const char * const kwnames[] = { "url", "flags", "options", NULL };
515 PyObject *py_options = Py_None;
516 const char **options;
519 struct ldb_context *ldb;
521 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__",
522 discard_const_p(char *, kwnames),
523 &url, &flags, &py_options))
526 ldb = PyLdb_AsLdbContext(self);
528 if (py_options == Py_None) {
531 options = PyList_AsStringList(ldb, py_options, "options");
537 ret = ldb_connect(ldb, url, flags, options);
538 if (ret != LDB_SUCCESS) {
539 PyErr_SetLdbError(PyExc_LdbError, ret, ldb);
544 talloc_free(options);
548 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
551 struct ldb_context *ldb;
552 ret = (PyLdbObject *)type->tp_alloc(type, 0);
557 ret->mem_ctx = talloc_new(NULL);
558 ldb = ldb_init(ret->mem_ctx, NULL);
566 return (PyObject *)ret;
569 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
573 PyObject *py_options = Py_None;
575 const char **options;
576 const char * const kwnames[] = { "url", "flags", "options", NULL };
578 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO",
579 discard_const_p(char *, kwnames),
580 &url, &flags, &py_options))
583 if (py_options == Py_None) {
586 options = PyList_AsStringList(NULL, py_options, "options");
591 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
592 talloc_free(options);
594 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
599 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
602 PyObject *py_controls = Py_None;
603 struct ldb_context *ldb_ctx;
604 struct ldb_request *req;
605 struct ldb_control **parsed_controls;
606 struct ldb_message *msg;
610 if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls))
613 mem_ctx = talloc_new(NULL);
614 if (mem_ctx == NULL) {
618 ldb_ctx = PyLdb_AsLdbContext(self);
620 if (py_controls == Py_None) {
621 parsed_controls = NULL;
623 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
624 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
625 talloc_free(controls);
628 if (!PyLdbMessage_Check(py_msg)) {
629 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
630 talloc_free(mem_ctx);
633 msg = PyLdbMessage_AsMessage(py_msg);
635 ret = ldb_msg_sanity_check(ldb_ctx, msg);
636 if (ret != LDB_SUCCESS) {
637 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
638 talloc_free(mem_ctx);
642 ret = ldb_build_mod_req(&req, ldb_ctx, mem_ctx, msg, parsed_controls,
643 NULL, ldb_op_default_callback, NULL);
644 if (ret != LDB_SUCCESS) {
645 PyErr_SetString(PyExc_TypeError, "failed to build request");
646 talloc_free(mem_ctx);
650 /* do request and autostart a transaction */
651 /* Then let's LDB handle the message error in case of pb as they are meaningful */
653 ret = ldb_transaction_start(ldb_ctx);
654 if (ret != LDB_SUCCESS) {
655 talloc_free(mem_ctx);
656 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
659 ret = ldb_request(ldb_ctx, req);
660 if (ret == LDB_SUCCESS) {
661 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
664 if (ret == LDB_SUCCESS) {
665 ret = ldb_transaction_commit(ldb_ctx);
667 ldb_transaction_cancel(ldb_ctx);
668 if (ldb_ctx->err_string == NULL) {
669 /* no error string was setup by the backend */
670 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
674 talloc_free(mem_ctx);
675 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
682 * Obtain a ldb message from a Python Dictionary object.
684 * @param mem_ctx Memory context
685 * @param py_obj Python Dictionary object
686 * @param ldb_ctx LDB context
687 * @param mod_flags Flags to be set on every message element
688 * @return ldb_message on success or NULL on failure
690 static struct ldb_message *PyDict_AsMessage(TALLOC_CTX *mem_ctx,
692 struct ldb_context *ldb_ctx,
693 unsigned int mod_flags)
695 struct ldb_message *msg;
696 unsigned int msg_pos = 0;
697 Py_ssize_t dict_pos = 0;
698 PyObject *key, *value;
699 struct ldb_message_element *msg_el;
700 PyObject *dn_value = PyDict_GetItemString(py_obj, "dn");
702 msg = ldb_msg_new(mem_ctx);
703 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_obj));
706 if (!PyObject_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
707 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
710 if (msg->dn == NULL) {
711 PyErr_SetString(PyExc_TypeError, "dn set but not found");
715 PyErr_SetString(PyExc_TypeError, "no dn set");
719 while (PyDict_Next(py_obj, &dict_pos, &key, &value)) {
720 char *key_str = PyString_AsString(key);
721 if (strcmp(key_str, "dn") != 0) {
722 msg_el = PyObject_AsMessageElement(msg->elements, value,
724 if (msg_el == NULL) {
725 PyErr_SetString(PyExc_TypeError, "unable to import element");
728 memcpy(&msg->elements[msg_pos], msg_el, sizeof(*msg_el));
733 msg->num_elements = msg_pos;
738 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
742 struct ldb_context *ldb_ctx;
743 struct ldb_request *req;
744 struct ldb_message *msg = NULL;
745 PyObject *py_controls = Py_None;
747 struct ldb_control **parsed_controls;
749 if (!PyArg_ParseTuple(args, "O|O", &py_obj, &py_controls ))
752 mem_ctx = talloc_new(NULL);
753 if (mem_ctx == NULL) {
757 ldb_ctx = PyLdb_AsLdbContext(self);
759 if (py_controls == Py_None) {
760 parsed_controls = NULL;
762 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
763 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
764 talloc_free(controls);
767 if (PyLdbMessage_Check(py_obj)) {
768 msg = PyLdbMessage_AsMessage(py_obj);
769 } else if (PyDict_Check(py_obj)) {
770 msg = PyDict_AsMessage(mem_ctx, py_obj, ldb_ctx, LDB_FLAG_MOD_ADD);
772 PyErr_SetString(PyExc_TypeError,
773 "Dictionary or LdbMessage object expected!");
777 /* we should have a PyErr already set */
778 talloc_free(mem_ctx);
782 ret = ldb_msg_sanity_check(ldb_ctx, msg);
783 if (ret != LDB_SUCCESS) {
784 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
785 talloc_free(mem_ctx);
789 ret = ldb_build_add_req(&req, ldb_ctx, mem_ctx, msg, parsed_controls,
790 NULL, ldb_op_default_callback, NULL);
791 if (ret != LDB_SUCCESS) {
792 PyErr_SetString(PyExc_TypeError, "failed to build request");
793 talloc_free(mem_ctx);
797 /* do request and autostart a transaction */
798 /* Then let's LDB handle the message error in case of pb as they are meaningful */
800 ret = ldb_transaction_start(ldb_ctx);
801 if (ret != LDB_SUCCESS) {
802 talloc_free(mem_ctx);
803 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
806 ret = ldb_request(ldb_ctx, req);
807 if (ret == LDB_SUCCESS) {
808 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
811 if (ret == LDB_SUCCESS) {
812 ret = ldb_transaction_commit(ldb_ctx);
814 ldb_transaction_cancel(ldb_ctx);
815 if (ldb_ctx->err_string == NULL) {
816 /* no error string was setup by the backend */
817 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
821 talloc_free(mem_ctx);
822 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
827 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
832 struct ldb_context *ldb_ctx;
833 struct ldb_request *req;
834 PyObject *py_controls = Py_None;
836 struct ldb_control **parsed_controls;
838 if (!PyArg_ParseTuple(args, "O|O", &py_dn, &py_controls))
841 mem_ctx = talloc_new(NULL);
842 if (mem_ctx == NULL) {
846 ldb_ctx = PyLdb_AsLdbContext(self);
848 if (py_controls == Py_None) {
849 parsed_controls = NULL;
851 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
852 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
853 talloc_free(controls);
856 if (!PyObject_AsDn(mem_ctx, py_dn, ldb_ctx, &dn)) {
857 talloc_free(mem_ctx);
861 ret = ldb_build_del_req(&req, ldb_ctx, mem_ctx, dn, parsed_controls,
862 NULL, ldb_op_default_callback, NULL);
863 if (ret != LDB_SUCCESS) {
864 PyErr_SetString(PyExc_TypeError, "failed to build request");
865 talloc_free(mem_ctx);
869 /* do request and autostart a transaction */
870 /* Then let's LDB handle the message error in case of pb as they are meaningful */
872 ret = ldb_transaction_start(ldb_ctx);
873 if (ret != LDB_SUCCESS) {
874 talloc_free(mem_ctx);
875 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
878 ret = ldb_request(ldb_ctx, req);
879 if (ret == LDB_SUCCESS) {
880 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
883 if (ret == LDB_SUCCESS) {
884 ret = ldb_transaction_commit(ldb_ctx);
886 ldb_transaction_cancel(ldb_ctx);
887 if (ldb_ctx->err_string == NULL) {
888 /* no error string was setup by the backend */
889 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
893 talloc_free(mem_ctx);
894 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
899 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
901 PyObject *py_dn1, *py_dn2;
902 struct ldb_dn *dn1, *dn2;
904 struct ldb_context *ldb;
906 PyObject *py_controls = Py_None;
907 struct ldb_control **parsed_controls;
908 struct ldb_context *ldb_ctx;
909 struct ldb_request *req;
911 ldb_ctx = PyLdb_AsLdbContext(self);
913 if (!PyArg_ParseTuple(args, "OO|O", &py_dn1, &py_dn2, &py_controls))
917 mem_ctx = talloc_new(NULL);
918 if (mem_ctx == NULL) {
922 ldb = PyLdb_AsLdbContext(self);
924 if (py_controls == Py_None) {
925 parsed_controls = NULL;
927 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
928 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
929 talloc_free(controls);
933 if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
934 talloc_free(mem_ctx);
938 if (!PyObject_AsDn(mem_ctx, py_dn2, ldb, &dn2)) {
939 talloc_free(mem_ctx);
943 ret = ldb_build_rename_req(&req, ldb_ctx, mem_ctx, dn1, dn2, parsed_controls,
944 NULL, ldb_op_default_callback, NULL);
945 if (ret != LDB_SUCCESS) {
946 PyErr_SetString(PyExc_TypeError, "failed to build request");
947 talloc_free(mem_ctx);
951 /* do request and autostart a transaction */
952 /* Then let's LDB handle the message error in case of pb as they are meaningful */
954 ret = ldb_transaction_start(ldb_ctx);
955 if (ret != LDB_SUCCESS) {
956 talloc_free(mem_ctx);
957 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
960 ret = ldb_request(ldb_ctx, req);
961 if (ret == LDB_SUCCESS) {
962 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
965 if (ret == LDB_SUCCESS) {
966 ret = ldb_transaction_commit(ldb_ctx);
968 ldb_transaction_cancel(ldb_ctx);
969 if (ldb_ctx->err_string == NULL) {
970 /* no error string was setup by the backend */
971 ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
975 talloc_free(mem_ctx);
976 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
981 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
984 if (!PyArg_ParseTuple(args, "s", &name))
987 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
992 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
994 char *attribute, *syntax;
997 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
1000 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
1002 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
1007 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
1012 /* We don't want this attached to the 'ldb' any more */
1013 return Py_BuildValue(discard_const_p(char, "(iO)"),
1015 PyLdbMessage_FromMessage(ldif->msg));
1020 static PyObject *py_ldb_write_ldif(PyLdbObject *self, PyObject *args)
1024 struct ldb_ldif ldif;
1027 TALLOC_CTX *mem_ctx;
1029 if (!PyArg_ParseTuple(args, "Oi", &py_msg, &changetype))
1032 if (!PyLdbMessage_Check(py_msg)) {
1033 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for msg");
1037 ldif.msg = PyLdbMessage_AsMessage(py_msg);
1038 ldif.changetype = changetype;
1040 mem_ctx = talloc_new(NULL);
1042 string = ldb_ldif_write_string(PyLdb_AsLdbContext(self), mem_ctx, &ldif);
1044 PyErr_SetString(PyExc_KeyError, "Failed to generate LDIF");
1048 ret = PyString_FromString(string);
1050 talloc_free(mem_ctx);
1055 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
1058 struct ldb_ldif *ldif;
1061 TALLOC_CTX *mem_ctx;
1063 if (!PyArg_ParseTuple(args, "s", &s))
1066 mem_ctx = talloc_new(NULL);
1071 list = PyList_New(0);
1072 while (s && *s != '\0') {
1073 ldif = ldb_ldif_read_string(self->ldb_ctx, &s);
1074 talloc_steal(mem_ctx, ldif);
1076 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
1078 PyErr_SetString(PyExc_ValueError, "unable to parse ldif string");
1079 talloc_free(mem_ctx);
1083 talloc_free(mem_ctx); /* The pyobject already has a reference to the things it needs */
1084 return PyObject_GetIter(list);
1087 static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args)
1090 PyObject *py_msg_old;
1091 PyObject *py_msg_new;
1092 struct ldb_message *diff;
1093 struct ldb_context *ldb;
1096 if (!PyArg_ParseTuple(args, "OO", &py_msg_old, &py_msg_new))
1099 if (!PyLdbMessage_Check(py_msg_old)) {
1100 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for old message");
1104 if (!PyLdbMessage_Check(py_msg_new)) {
1105 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for new message");
1109 ldb = PyLdb_AsLdbContext(self);
1110 ldb_ret = ldb_msg_difference(ldb, ldb,
1111 PyLdbMessage_AsMessage(py_msg_old),
1112 PyLdbMessage_AsMessage(py_msg_new),
1114 if (ldb_ret != LDB_SUCCESS) {
1115 PyErr_SetString(PyExc_RuntimeError, "Failed to generate the Ldb Message diff");
1119 py_ret = PyLdbMessage_FromMessage(diff);
1121 talloc_unlink(ldb, diff);
1126 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
1128 const struct ldb_schema_attribute *a;
1129 struct ldb_val old_val;
1130 struct ldb_val new_val;
1131 TALLOC_CTX *mem_ctx;
1136 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
1139 mem_ctx = talloc_new(NULL);
1141 old_val.data = (uint8_t *)PyString_AsString(val);
1142 old_val.length = PyString_Size(val);
1144 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
1150 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
1151 talloc_free(mem_ctx);
1155 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
1157 talloc_free(mem_ctx);
1162 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
1164 PyObject *py_base = Py_None;
1165 int scope = LDB_SCOPE_DEFAULT;
1167 PyObject *py_attrs = Py_None;
1168 PyObject *py_controls = Py_None;
1169 const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
1171 struct ldb_result *res;
1172 struct ldb_request *req;
1174 struct ldb_context *ldb_ctx;
1175 struct ldb_control **parsed_controls;
1176 struct ldb_dn *base;
1178 TALLOC_CTX *mem_ctx;
1180 /* type "int" rather than "enum" for "scope" is intentional */
1181 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
1182 discard_const_p(char *, kwnames),
1183 &py_base, &scope, &expr, &py_attrs, &py_controls))
1187 mem_ctx = talloc_new(NULL);
1188 if (mem_ctx == NULL) {
1192 ldb_ctx = PyLdb_AsLdbContext(self);
1194 if (py_attrs == Py_None) {
1197 attrs = PyList_AsStringList(mem_ctx, py_attrs, "attrs");
1198 if (attrs == NULL) {
1199 talloc_free(mem_ctx);
1204 if (py_base == Py_None) {
1205 base = ldb_get_default_basedn(ldb_ctx);
1207 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
1213 if (py_controls == Py_None) {
1214 parsed_controls = NULL;
1216 const char **controls = PyList_AsStringList(mem_ctx, py_controls, "controls");
1217 parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
1218 talloc_free(controls);
1221 res = talloc_zero(mem_ctx, struct ldb_result);
1224 talloc_free(mem_ctx);
1228 ret = ldb_build_search_req(&req, ldb_ctx, mem_ctx,
1235 ldb_search_default_callback,
1238 if (ret != LDB_SUCCESS) {
1239 talloc_free(mem_ctx);
1240 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
1244 talloc_steal(req, attrs);
1246 ret = ldb_request(ldb_ctx, req);
1248 if (ret == LDB_SUCCESS) {
1249 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1252 if (ret != LDB_SUCCESS) {
1253 talloc_free(mem_ctx);
1254 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
1258 py_ret = PyLdbResult_FromResult(res);
1260 talloc_free(mem_ctx);
1265 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
1270 if (!PyArg_ParseTuple(args, "s", &name))
1273 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
1278 /* FIXME: More interpretation */
1283 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
1288 if (!PyArg_ParseTuple(args, "sO", &name, &data))
1291 /* FIXME: More interpretation */
1293 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
1298 static PyObject *py_ldb_modules(PyLdbObject *self)
1300 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
1301 PyObject *ret = PyList_New(0);
1302 struct ldb_module *mod;
1304 for (mod = ldb->modules; mod; mod = mod->next) {
1305 PyList_Append(ret, PyLdbModule_FromModule(mod));
1311 static PyObject *py_ldb_sequence_number(PyLdbObject *self, PyObject *args)
1313 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
1317 if (!PyArg_ParseTuple(args, "i", &type))
1320 /* FIXME: More interpretation */
1322 ret = ldb_sequence_number(ldb, type, &value);
1324 if (ret != LDB_SUCCESS) {
1325 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
1328 return PyLong_FromLongLong(value);
1330 static PyMethodDef py_ldb_methods[] = {
1331 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
1332 "S.set_debug(callback) -> None\n"
1333 "Set callback for LDB debug messages.\n"
1334 "The callback should accept a debug level and debug text." },
1335 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
1336 "S.set_create_perms(mode) -> None\n"
1337 "Set mode to use when creating new LDB files." },
1338 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
1339 "S.set_modules_dir(path) -> None\n"
1340 "Set path LDB should search for modules" },
1341 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
1342 "S.transaction_start() -> None\n"
1343 "Start a new transaction." },
1344 { "transaction_prepare_commit", (PyCFunction)py_ldb_transaction_prepare_commit, METH_NOARGS,
1345 "S.transaction_prepare_commit() -> None\n"
1346 "prepare to commit a new transaction (2-stage commit)." },
1347 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
1348 "S.transaction_commit() -> None\n"
1349 "commit a new transaction." },
1350 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
1351 "S.transaction_cancel() -> None\n"
1352 "cancel a new transaction." },
1353 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
1355 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
1357 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
1359 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
1361 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
1363 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
1364 "S.connect(url, flags=0, options=None) -> None\n"
1365 "Connect to a LDB URL." },
1366 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
1367 "S.modify(message) -> None\n"
1368 "Modify an entry." },
1369 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
1370 "S.add(message) -> None\n"
1372 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
1373 "S.delete(dn) -> None\n"
1374 "Remove an entry." },
1375 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
1376 "S.rename(old_dn, new_dn) -> None\n"
1377 "Rename an entry." },
1378 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
1379 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1380 "Search in a database.\n"
1382 ":param base: Optional base DN to search\n"
1383 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1384 ":param expression: Optional search expression\n"
1385 ":param attrs: Attributes to return (defaults to all)\n"
1386 ":param controls: Optional list of controls\n"
1387 ":return: Iterator over Message objects\n"
1389 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
1391 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
1393 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
1395 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
1396 "S.parse_ldif(ldif) -> iter(messages)\n"
1397 "Parse a string formatted using LDIF." },
1398 { "write_ldif", (PyCFunction)py_ldb_write_ldif, METH_VARARGS,
1399 "S.write_ldif(message, changetype) -> ldif\n"
1400 "Print the message as a string formatted using LDIF." },
1401 { "msg_diff", (PyCFunction)py_ldb_msg_diff, METH_VARARGS,
1402 "S.msg_diff(Message) -> Message\n"
1403 "Return an LDB Message of the difference between two Message objects." },
1404 { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
1405 "S.get_opaque(name) -> value\n"
1406 "Get an opaque value set on this LDB connection. \n"
1407 ":note: The returned value may not be useful in Python."
1409 { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
1410 "S.set_opaque(name, value) -> None\n"
1411 "Set an opaque value on this LDB connection. \n"
1412 ":note: Passing incorrect values may cause crashes." },
1413 { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
1414 "S.modules() -> list\n"
1415 "Return the list of modules on this LDB connection " },
1416 { "sequence_number", (PyCFunction)py_ldb_sequence_number, METH_VARARGS,
1417 "S.sequence_number(type) -> value\n"
1418 "Return the value of the sequence according to the requested type" },
1422 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
1424 PyLdbModuleObject *ret;
1426 ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
1431 ret->mem_ctx = talloc_new(NULL);
1432 ret->mod = talloc_reference(ret->mem_ctx, mod);
1433 return (PyObject *)ret;
1436 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
1438 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
1441 static PyGetSetDef py_ldb_getset[] = {
1442 { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL },
1446 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
1448 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1450 struct ldb_result *result;
1454 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn)) {
1458 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL,
1460 if (ret != LDB_SUCCESS) {
1461 PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
1465 count = result->count;
1467 talloc_free(result);
1470 PyErr_Format(PyExc_RuntimeError,
1471 "Searching for [%s] dn gave %u results!",
1472 ldb_dn_get_linearized(dn),
1480 static PySequenceMethods py_ldb_seq = {
1481 .sq_contains = (objobjproc)py_ldb_contains,
1484 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
1488 ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1493 ret->mem_ctx = talloc_new(NULL);
1494 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1495 return (PyObject *)ret;
1498 static void py_ldb_dealloc(PyLdbObject *self)
1500 talloc_free(self->mem_ctx);
1501 self->ob_type->tp_free(self);
1504 PyTypeObject PyLdb = {
1505 .tp_name = "ldb.Ldb",
1506 .tp_methods = py_ldb_methods,
1507 .tp_repr = (reprfunc)py_ldb_repr,
1508 .tp_new = py_ldb_new,
1509 .tp_init = (initproc)py_ldb_init,
1510 .tp_dealloc = (destructor)py_ldb_dealloc,
1511 .tp_getset = py_ldb_getset,
1512 .tp_getattro = PyObject_GenericGetAttr,
1513 .tp_basicsize = sizeof(PyLdbObject),
1514 .tp_doc = "Connection to a LDB database.",
1515 .tp_as_sequence = &py_ldb_seq,
1516 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1519 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
1521 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1524 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
1526 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1529 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1531 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1535 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1537 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1541 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1543 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1547 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1549 PyObject *py_base, *py_tree, *py_attrs, *py_ret;
1551 struct ldb_request *req;
1552 const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1553 struct ldb_module *mod;
1554 const char * const*attrs;
1556 /* type "int" rather than "enum" for "scope" is intentional */
1557 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
1558 discard_const_p(char *, kwnames),
1559 &py_base, &scope, &py_tree, &py_attrs))
1564 if (py_attrs == Py_None) {
1567 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
1572 ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base),
1573 scope, NULL /* expr */, attrs,
1574 NULL /* controls */, NULL, NULL, NULL);
1576 talloc_steal(req, attrs);
1578 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1580 req->op.search.res = NULL;
1582 ret = mod->ops->search(mod, req);
1584 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1586 py_ret = PyLdbResult_FromResult(req->op.search.res);
1594 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1596 struct ldb_request *req;
1597 PyObject *py_message;
1599 struct ldb_module *mod;
1601 if (!PyArg_ParseTuple(args, "O", &py_message))
1604 req = talloc_zero(NULL, struct ldb_request);
1605 req->operation = LDB_ADD;
1606 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1608 mod = PyLdbModule_AsModule(self);
1609 ret = mod->ops->add(mod, req);
1611 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1616 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
1619 struct ldb_request *req;
1620 PyObject *py_message;
1621 struct ldb_module *mod;
1623 if (!PyArg_ParseTuple(args, "O", &py_message))
1626 req = talloc_zero(NULL, struct ldb_request);
1627 req->operation = LDB_MODIFY;
1628 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1630 mod = PyLdbModule_AsModule(self);
1631 ret = mod->ops->modify(mod, req);
1633 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1638 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
1641 struct ldb_request *req;
1644 if (!PyArg_ParseTuple(args, "O", &py_dn))
1647 req = talloc_zero(NULL, struct ldb_request);
1648 req->operation = LDB_DELETE;
1649 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1651 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1653 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1658 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1661 struct ldb_request *req;
1662 PyObject *py_dn1, *py_dn2;
1664 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1667 req = talloc_zero(NULL, struct ldb_request);
1669 req->operation = LDB_RENAME;
1670 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1671 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1673 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1675 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1680 static PyMethodDef py_ldb_module_methods[] = {
1681 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1682 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1683 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1684 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1685 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1686 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1687 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1688 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1692 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
1694 talloc_free(self->mem_ctx);
1695 self->ob_type->tp_free(self);
1698 PyTypeObject PyLdbModule = {
1699 .tp_name = "ldb.LdbModule",
1700 .tp_methods = py_ldb_module_methods,
1701 .tp_repr = (reprfunc)py_ldb_module_repr,
1702 .tp_str = (reprfunc)py_ldb_module_str,
1703 .tp_basicsize = sizeof(PyLdbModuleObject),
1704 .tp_dealloc = (destructor)py_ldb_module_dealloc,
1705 .tp_flags = Py_TPFLAGS_DEFAULT,
1710 * Create a ldb_message_element from a Python object.
1712 * This will accept any sequence objects that contains strings, or
1715 * A reference to set_obj will be borrowed.
1717 * @param mem_ctx Memory context
1718 * @param set_obj Python object to convert
1719 * @param flags ldb_message_element flags to set
1720 * @param attr_name Name of the attribute
1721 * @return New ldb_message_element, allocated as child of mem_ctx
1723 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1726 const char *attr_name)
1728 struct ldb_message_element *me;
1730 if (PyLdbMessageElement_Check(set_obj)) {
1731 PyLdbMessageElementObject *set_obj_as_me = (PyLdbMessageElementObject *)set_obj;
1732 /* We have to talloc_reference() the memory context, not the pointer which may not actually be it's own context */
1733 if (talloc_reference(mem_ctx, set_obj_as_me->mem_ctx)) {
1734 return PyLdbMessageElement_AsMessageElement(set_obj);
1739 me = talloc(mem_ctx, struct ldb_message_element);
1741 me->name = talloc_strdup(me, attr_name);
1743 if (PyString_Check(set_obj)) {
1745 me->values = talloc_array(me, struct ldb_val, me->num_values);
1746 me->values[0].length = PyString_Size(set_obj);
1747 me->values[0].data = talloc_memdup(me,
1748 (uint8_t *)PyString_AsString(set_obj), me->values[0].length+1);
1749 } else if (PySequence_Check(set_obj)) {
1751 me->num_values = PySequence_Size(set_obj);
1752 me->values = talloc_array(me, struct ldb_val, me->num_values);
1753 for (i = 0; i < me->num_values; i++) {
1754 PyObject *obj = PySequence_GetItem(set_obj, i);
1755 if (!PyString_Check(obj)) {
1756 PyErr_Format(PyExc_TypeError,
1757 "Expected string as element %zd in list", i);
1762 me->values[i].length = PyString_Size(obj);
1763 me->values[i].data = talloc_memdup(me,
1764 (uint8_t *)PyString_AsString(obj), me->values[i].length+1);
1775 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
1776 struct ldb_message_element *me)
1781 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1782 result = PyList_New(me->num_values);
1784 for (i = 0; i < me->num_values; i++) {
1785 PyList_SetItem(result, i,
1786 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1792 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1795 if (!PyArg_ParseTuple(args, "I", &i))
1797 if (i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1800 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1801 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1804 static PyObject *py_ldb_msg_element_flags(PyLdbMessageElementObject *self, PyObject *args)
1806 struct ldb_message_element *el;
1808 el = PyLdbMessageElement_AsMessageElement(self);
1809 return PyInt_FromLong(el->flags);
1812 static PyObject *py_ldb_msg_element_set_flags(PyLdbMessageElementObject *self, PyObject *args)
1815 struct ldb_message_element *el;
1816 if (!PyArg_ParseTuple(args, "i", &flags))
1819 el = PyLdbMessageElement_AsMessageElement(self);
1824 static PyMethodDef py_ldb_msg_element_methods[] = {
1825 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1826 { "set_flags", (PyCFunction)py_ldb_msg_element_set_flags, METH_VARARGS, NULL },
1827 { "flags", (PyCFunction)py_ldb_msg_element_flags, METH_NOARGS, NULL },
1831 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1833 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1836 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1838 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1839 if (idx < 0 || idx >= el->num_values) {
1840 PyErr_SetString(PyExc_IndexError, "Out of range");
1843 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1846 static PySequenceMethods py_ldb_msg_element_seq = {
1847 .sq_length = (lenfunc)py_ldb_msg_element_len,
1848 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1851 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1853 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1854 PyLdbMessageElement_AsMessageElement(other));
1857 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1859 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1862 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
1864 PyLdbMessageElementObject *ret;
1865 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1870 ret->mem_ctx = talloc_new(NULL);
1871 if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1876 return (PyObject *)ret;
1879 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1881 PyObject *py_elements = NULL;
1882 struct ldb_message_element *el;
1885 const char * const kwnames[] = { "elements", "flags", "name", NULL };
1886 PyLdbMessageElementObject *ret;
1887 TALLOC_CTX *mem_ctx;
1889 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois",
1890 discard_const_p(char *, kwnames),
1891 &py_elements, &flags, &name))
1894 mem_ctx = talloc_new(NULL);
1895 if (mem_ctx == NULL) {
1900 el = talloc_zero(mem_ctx, struct ldb_message_element);
1902 if (py_elements != NULL) {
1904 if (PyString_Check(py_elements)) {
1906 el->values = talloc_array(el, struct ldb_val, 1);
1907 el->values[0].length = PyString_Size(py_elements);
1908 el->values[0].data = talloc_memdup(el,
1909 (uint8_t *)PyString_AsString(py_elements), el->values[0].length+1);
1910 } else if (PySequence_Check(py_elements)) {
1911 el->num_values = PySequence_Size(py_elements);
1912 el->values = talloc_array(el, struct ldb_val, el->num_values);
1913 for (i = 0; i < el->num_values; i++) {
1914 PyObject *item = PySequence_GetItem(py_elements, i);
1915 if (!PyString_Check(item)) {
1916 PyErr_Format(PyExc_TypeError,
1917 "Expected string as element %zd in list", i);
1918 talloc_free(mem_ctx);
1921 el->values[i].length = PyString_Size(item);
1922 el->values[i].data = talloc_memdup(el,
1923 (uint8_t *)PyString_AsString(item), el->values[i].length+1);
1926 PyErr_SetString(PyExc_TypeError,
1927 "Expected string or list");
1928 talloc_free(mem_ctx);
1934 el->name = talloc_strdup(el, name);
1936 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1939 talloc_free(mem_ctx);
1943 ret->mem_ctx = mem_ctx;
1945 return (PyObject *)ret;
1948 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1950 char *element_str = NULL;
1952 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1955 for (i = 0; i < el->num_values; i++) {
1956 PyObject *o = py_ldb_msg_element_find(self, i);
1957 if (element_str == NULL)
1958 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1960 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1963 if (element_str != NULL) {
1964 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1965 talloc_free(element_str);
1967 ret = PyString_FromString("MessageElement([])");
1973 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
1975 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1977 if (el->num_values == 1)
1978 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1983 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
1985 talloc_free(self->mem_ctx);
1986 self->ob_type->tp_free(self);
1989 PyTypeObject PyLdbMessageElement = {
1990 .tp_name = "ldb.MessageElement",
1991 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1992 .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1993 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1994 .tp_str = (reprfunc)py_ldb_msg_element_str,
1995 .tp_methods = py_ldb_msg_element_methods,
1996 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1997 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1998 .tp_as_sequence = &py_ldb_msg_element_seq,
1999 .tp_new = py_ldb_msg_element_new,
2000 .tp_flags = Py_TPFLAGS_DEFAULT,
2004 static PyObject *py_ldb_msg_from_dict(PyTypeObject *type, PyObject *args)
2009 struct ldb_message *msg;
2010 struct ldb_context *ldb_ctx;
2011 unsigned int mod_flags = LDB_FLAG_MOD_REPLACE;
2013 if (!PyArg_ParseTuple(args, "O!O!|I",
2014 &PyLdb, &py_ldb, &PyDict_Type, &py_dict,
2019 /* mask only flags we are going to use */
2020 mod_flags = LDB_FLAG_MOD_TYPE(mod_flags);
2022 PyErr_SetString(PyExc_ValueError,
2023 "FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE"
2024 " expected as mod_flag value");
2028 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
2030 msg = PyDict_AsMessage(ldb_ctx, py_dict, ldb_ctx, mod_flags);
2035 py_ret = PyLdbMessage_FromMessage(msg);
2037 talloc_unlink(ldb_ctx, msg);
2042 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
2045 if (!PyArg_ParseTuple(args, "s", &name))
2048 ldb_msg_remove_attr(self->msg, name);
2053 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
2055 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2056 Py_ssize_t i, j = 0;
2057 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
2058 if (msg->dn != NULL) {
2059 PyList_SetItem(obj, j, PyString_FromString("dn"));
2062 for (i = 0; i < msg->num_elements; i++) {
2063 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
2069 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
2071 struct ldb_message_element *el;
2073 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2074 if (!PyString_Check(py_name)) {
2075 PyErr_SetNone(PyExc_TypeError);
2078 name = PyString_AsString(py_name);
2079 if (!strcmp(name, "dn"))
2080 return PyLdbDn_FromDn(msg->dn);
2081 el = ldb_msg_find_element(msg, name);
2085 return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg->elements);
2088 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
2090 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
2092 PyErr_SetString(PyExc_KeyError, "No such element");
2098 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
2100 PyObject *name, *ret;
2101 if (!PyArg_ParseTuple(args, "O", &name))
2104 ret = py_ldb_msg_getitem_helper(self, name);
2106 if (PyErr_Occurred())
2113 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
2115 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2116 Py_ssize_t i, j = 0;
2117 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
2118 if (msg->dn != NULL) {
2119 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
2122 for (i = 0; i < msg->num_elements; i++, j++) {
2123 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], msg->elements)));
2128 static PyMethodDef py_ldb_msg_methods[] = {
2129 { "from_dict", (PyCFunction)py_ldb_msg_from_dict, METH_CLASS | METH_VARARGS,
2130 "Message.from_dict(ldb, dict, mod_flag) -> ldb.Message\n"
2131 "Class method to create ldb.Message object from Dictionary.\n"
2132 "mod_flag is one of FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE.\n"
2133 "mod_flag defaults to FLAG_MOD_REPLACE"},
2134 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
2135 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
2136 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
2137 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
2141 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
2143 PyObject *list, *iter;
2145 list = py_ldb_msg_keys(self);
2146 iter = PyObject_GetIter(list);
2151 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
2155 if (!PyString_Check(name)) {
2156 PyErr_SetNone(PyExc_TypeError);
2160 attr_name = PyString_AsString(name);
2161 if (value == NULL) {
2163 ldb_msg_remove_attr(self->msg, attr_name);
2165 struct ldb_message_element *el = PyObject_AsMessageElement(self->msg,
2166 value, 0, attr_name);
2169 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
2170 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
2175 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
2177 return PyLdbMessage_AsMessage(self)->num_elements;
2180 static PyMappingMethods py_ldb_msg_mapping = {
2181 .mp_length = (lenfunc)py_ldb_msg_length,
2182 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
2183 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
2186 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2188 const char * const kwnames[] = { "dn", NULL };
2189 struct ldb_message *ret;
2190 TALLOC_CTX *mem_ctx;
2191 PyObject *pydn = NULL;
2192 PyLdbMessageObject *py_ret;
2194 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
2195 discard_const_p(char *, kwnames),
2199 mem_ctx = talloc_new(NULL);
2200 if (mem_ctx == NULL) {
2205 ret = ldb_msg_new(mem_ctx);
2207 talloc_free(mem_ctx);
2214 if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) {
2215 talloc_free(mem_ctx);
2218 ret->dn = talloc_reference(ret, dn);
2221 py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
2222 if (py_ret == NULL) {
2224 talloc_free(mem_ctx);
2228 py_ret->mem_ctx = mem_ctx;
2230 return (PyObject *)py_ret;
2233 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
2235 PyLdbMessageObject *ret;
2237 ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
2242 ret->mem_ctx = talloc_new(NULL);
2243 ret->msg = talloc_reference(ret->mem_ctx, msg);
2244 return (PyObject *)ret;
2247 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
2249 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2250 return PyLdbDn_FromDn(msg->dn);
2253 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
2255 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
2256 if (!PyLdbDn_Check(value)) {
2257 PyErr_SetNone(PyExc_TypeError);
2261 msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value));
2265 static PyGetSetDef py_ldb_msg_getset[] = {
2266 { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
2270 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
2272 PyObject *dict = PyDict_New(), *ret;
2273 if (PyDict_Update(dict, (PyObject *)self) != 0)
2275 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
2280 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
2282 talloc_free(self->mem_ctx);
2283 self->ob_type->tp_free(self);
2286 static int py_ldb_msg_compare(PyLdbMessageObject *py_msg1,
2287 PyLdbMessageObject *py_msg2)
2289 struct ldb_message *msg1 = PyLdbMessage_AsMessage(py_msg1),
2290 *msg2 = PyLdbMessage_AsMessage(py_msg2);
2294 if ((msg1->dn != NULL) || (msg2->dn != NULL)) {
2295 ret = ldb_dn_compare(msg1->dn, msg2->dn);
2301 ret = msg1->num_elements - msg2->num_elements;
2306 for (i = 0; i < msg1->num_elements; i++) {
2307 ret = ldb_msg_element_compare_name(&msg1->elements[i],
2308 &msg2->elements[i]);
2313 ret = ldb_msg_element_compare(&msg1->elements[i],
2314 &msg2->elements[i]);
2323 PyTypeObject PyLdbMessage = {
2324 .tp_name = "ldb.Message",
2325 .tp_methods = py_ldb_msg_methods,
2326 .tp_getset = py_ldb_msg_getset,
2327 .tp_as_mapping = &py_ldb_msg_mapping,
2328 .tp_basicsize = sizeof(PyLdbMessageObject),
2329 .tp_dealloc = (destructor)py_ldb_msg_dealloc,
2330 .tp_new = py_ldb_msg_new,
2331 .tp_repr = (reprfunc)py_ldb_msg_repr,
2332 .tp_flags = Py_TPFLAGS_DEFAULT,
2333 .tp_iter = (getiterfunc)py_ldb_msg_iter,
2334 .tp_compare = (cmpfunc)py_ldb_msg_compare,
2337 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
2339 PyLdbTreeObject *ret;
2341 ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
2347 ret->mem_ctx = talloc_new(NULL);
2348 ret->tree = talloc_reference(ret->mem_ctx, tree);
2349 return (PyObject *)ret;
2352 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
2354 talloc_free(self->mem_ctx);
2355 self->ob_type->tp_free(self);
2358 PyTypeObject PyLdbTree = {
2359 .tp_name = "ldb.Tree",
2360 .tp_basicsize = sizeof(PyLdbTreeObject),
2361 .tp_dealloc = (destructor)py_ldb_tree_dealloc,
2362 .tp_flags = Py_TPFLAGS_DEFAULT,
2366 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
2368 PyObject *py_ldb = (PyObject *)mod->private_data;
2369 PyObject *py_result, *py_base, *py_attrs, *py_tree;
2371 py_base = PyLdbDn_FromDn(req->op.search.base);
2373 if (py_base == NULL)
2374 return LDB_ERR_OPERATIONS_ERROR;
2376 py_tree = PyLdbTree_FromTree(req->op.search.tree);
2378 if (py_tree == NULL)
2379 return LDB_ERR_OPERATIONS_ERROR;
2381 if (req->op.search.attrs == NULL) {
2385 for (len = 0; req->op.search.attrs[len]; len++);
2386 py_attrs = PyList_New(len);
2387 for (i = 0; i < len; i++)
2388 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
2391 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
2392 discard_const_p(char, "OiOO"),
2393 py_base, req->op.search.scope, py_tree, py_attrs);
2395 Py_DECREF(py_attrs);
2399 if (py_result == NULL) {
2400 return LDB_ERR_PYTHON_EXCEPTION;
2403 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
2404 if (req->op.search.res == NULL) {
2405 return LDB_ERR_PYTHON_EXCEPTION;
2408 Py_DECREF(py_result);
2413 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
2415 PyObject *py_ldb = (PyObject *)mod->private_data;
2416 PyObject *py_result, *py_msg;
2418 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
2420 if (py_msg == NULL) {
2421 return LDB_ERR_OPERATIONS_ERROR;
2424 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
2425 discard_const_p(char, "O"),
2430 if (py_result == NULL) {
2431 return LDB_ERR_PYTHON_EXCEPTION;
2434 Py_DECREF(py_result);
2439 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
2441 PyObject *py_ldb = (PyObject *)mod->private_data;
2442 PyObject *py_result, *py_msg;
2444 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
2446 if (py_msg == NULL) {
2447 return LDB_ERR_OPERATIONS_ERROR;
2450 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
2451 discard_const_p(char, "O"),
2456 if (py_result == NULL) {
2457 return LDB_ERR_PYTHON_EXCEPTION;
2460 Py_DECREF(py_result);
2465 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
2467 PyObject *py_ldb = (PyObject *)mod->private_data;
2468 PyObject *py_result, *py_dn;
2470 py_dn = PyLdbDn_FromDn(req->op.del.dn);
2473 return LDB_ERR_OPERATIONS_ERROR;
2475 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
2476 discard_const_p(char, "O"),
2479 if (py_result == NULL) {
2480 return LDB_ERR_PYTHON_EXCEPTION;
2483 Py_DECREF(py_result);
2488 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
2490 PyObject *py_ldb = (PyObject *)mod->private_data;
2491 PyObject *py_result, *py_olddn, *py_newdn;
2493 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
2495 if (py_olddn == NULL)
2496 return LDB_ERR_OPERATIONS_ERROR;
2498 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
2500 if (py_newdn == NULL)
2501 return LDB_ERR_OPERATIONS_ERROR;
2503 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
2504 discard_const_p(char, "OO"),
2505 py_olddn, py_newdn);
2507 Py_DECREF(py_olddn);
2508 Py_DECREF(py_newdn);
2510 if (py_result == NULL) {
2511 return LDB_ERR_PYTHON_EXCEPTION;
2514 Py_DECREF(py_result);
2519 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
2521 PyObject *py_ldb = (PyObject *)mod->private_data;
2522 PyObject *py_result;
2524 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
2525 discard_const_p(char, ""));
2527 return LDB_ERR_OPERATIONS_ERROR;
2530 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
2532 PyObject *py_ldb = (PyObject *)mod->private_data;
2533 PyObject *py_result;
2535 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
2536 discard_const_p(char, ""));
2538 return LDB_ERR_OPERATIONS_ERROR;
2541 static int py_module_start_transaction(struct ldb_module *mod)
2543 PyObject *py_ldb = (PyObject *)mod->private_data;
2544 PyObject *py_result;
2546 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
2547 discard_const_p(char, ""));
2549 if (py_result == NULL) {
2550 return LDB_ERR_PYTHON_EXCEPTION;
2553 Py_DECREF(py_result);
2558 static int py_module_end_transaction(struct ldb_module *mod)
2560 PyObject *py_ldb = (PyObject *)mod->private_data;
2561 PyObject *py_result;
2563 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
2564 discard_const_p(char, ""));
2566 if (py_result == NULL) {
2567 return LDB_ERR_PYTHON_EXCEPTION;
2570 Py_DECREF(py_result);
2575 static int py_module_del_transaction(struct ldb_module *mod)
2577 PyObject *py_ldb = (PyObject *)mod->private_data;
2578 PyObject *py_result;
2580 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
2581 discard_const_p(char, ""));
2583 if (py_result == NULL) {
2584 return LDB_ERR_PYTHON_EXCEPTION;
2587 Py_DECREF(py_result);
2592 static int py_module_destructor(struct ldb_module *mod)
2594 Py_DECREF((PyObject *)mod->private_data);
2598 static int py_module_init(struct ldb_module *mod)
2600 PyObject *py_class = (PyObject *)mod->ops->private_data;
2601 PyObject *py_result, *py_next, *py_ldb;
2603 py_ldb = PyLdb_FromLdbContext(mod->ldb);
2606 return LDB_ERR_OPERATIONS_ERROR;
2608 py_next = PyLdbModule_FromModule(mod->next);
2610 if (py_next == NULL)
2611 return LDB_ERR_OPERATIONS_ERROR;
2613 py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
2616 if (py_result == NULL) {
2617 return LDB_ERR_PYTHON_EXCEPTION;
2620 mod->private_data = py_result;
2622 talloc_set_destructor(mod, py_module_destructor);
2624 return ldb_next_init(mod);
2627 static PyObject *py_register_module(PyObject *module, PyObject *args)
2630 struct ldb_module_ops *ops;
2633 if (!PyArg_ParseTuple(args, "O", &input))
2636 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
2642 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name"))));
2645 ops->private_data = input;
2646 ops->init_context = py_module_init;
2647 ops->search = py_module_search;
2648 ops->add = py_module_add;
2649 ops->modify = py_module_modify;
2650 ops->del = py_module_del;
2651 ops->rename = py_module_rename;
2652 ops->request = py_module_request;
2653 ops->extended = py_module_extended;
2654 ops->start_transaction = py_module_start_transaction;
2655 ops->end_transaction = py_module_end_transaction;
2656 ops->del_transaction = py_module_del_transaction;
2658 ret = ldb_register_module(ops);
2660 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
2665 static PyObject *py_timestring(PyObject *module, PyObject *args)
2667 /* most times "time_t" is a signed integer type with 32 or 64 bit:
2668 * http://stackoverflow.com/questions/471248/what-is-ultimately-a-time-t-typedef-to */
2672 if (!PyArg_ParseTuple(args, "l", &t_val))
2674 tresult = ldb_timestring(NULL, (time_t) t_val);
2675 ret = PyString_FromString(tresult);
2676 talloc_free(tresult);
2680 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
2683 if (!PyArg_ParseTuple(args, "s", &str))
2686 return PyInt_FromLong(ldb_string_to_time(str));
2689 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
2692 if (!PyArg_ParseTuple(args, "s", &name))
2694 return PyBool_FromLong(ldb_valid_attr_name(name));
2697 static PyMethodDef py_ldb_global_methods[] = {
2698 { "register_module", py_register_module, METH_VARARGS,
2699 "S.register_module(module) -> None\n"
2700 "Register a LDB module."},
2701 { "timestring", py_timestring, METH_VARARGS,
2702 "S.timestring(int) -> string\n"
2703 "Generate a LDAP time string from a UNIX timestamp" },
2704 { "string_to_time", py_string_to_time, METH_VARARGS,
2705 "S.string_to_time(string) -> int\n"
2706 "Parse a LDAP time string into a UNIX timestamp." },
2707 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2708 "S.valid_attr_name(name) -> bool\n"
2709 "Check whether the supplied name is a valid attribute name." },
2710 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2719 if (PyType_Ready(&PyLdbDn) < 0)
2722 if (PyType_Ready(&PyLdbMessage) < 0)
2725 if (PyType_Ready(&PyLdbMessageElement) < 0)
2728 if (PyType_Ready(&PyLdb) < 0)
2731 if (PyType_Ready(&PyLdbModule) < 0)
2734 if (PyType_Ready(&PyLdbTree) < 0)
2737 m = Py_InitModule3("ldb", py_ldb_global_methods,
2738 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2742 PyModule_AddObject(m, "SEQ_HIGHEST_SEQ", PyInt_FromLong(LDB_SEQ_HIGHEST_SEQ));
2743 PyModule_AddObject(m, "SEQ_HIGHEST_TIMESTAMP", PyInt_FromLong(LDB_SEQ_HIGHEST_TIMESTAMP));
2744 PyModule_AddObject(m, "SEQ_NEXT", PyInt_FromLong(LDB_SEQ_NEXT));
2745 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2746 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2747 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2748 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2750 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2751 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2752 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2753 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2755 PyModule_AddObject(m, "FLAG_MOD_ADD", PyInt_FromLong(LDB_FLAG_MOD_ADD));
2756 PyModule_AddObject(m, "FLAG_MOD_REPLACE", PyInt_FromLong(LDB_FLAG_MOD_REPLACE));
2757 PyModule_AddObject(m, "FLAG_MOD_DELETE", PyInt_FromLong(LDB_FLAG_MOD_DELETE));
2759 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2760 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2761 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2762 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2763 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2764 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2765 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2766 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2767 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2768 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2769 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2770 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2771 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2772 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2773 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2774 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2775 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2776 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2777 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2778 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2779 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2780 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2781 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2782 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2783 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2784 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2785 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2786 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2787 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2788 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2789 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2790 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2791 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2792 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2793 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2794 PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2795 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2796 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2797 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2799 PyModule_AddObject(m, "FLG_RDONLY", PyInt_FromLong(LDB_FLG_RDONLY));
2800 PyModule_AddObject(m, "FLG_NOSYNC", PyInt_FromLong(LDB_FLG_NOSYNC));
2801 PyModule_AddObject(m, "FLG_RECONNECT", PyInt_FromLong(LDB_FLG_RECONNECT));
2802 PyModule_AddObject(m, "FLG_NOMMAP", PyInt_FromLong(LDB_FLG_NOMMAP));
2804 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2806 PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL);
2807 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2810 Py_INCREF(&PyLdbDn);
2811 Py_INCREF(&PyLdbModule);
2812 Py_INCREF(&PyLdbMessage);
2813 Py_INCREF(&PyLdbMessageElement);
2814 Py_INCREF(&PyLdbTree);
2816 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2817 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2818 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2819 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2820 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2821 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);
2823 PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));