2 Unix SMB/CIFS implementation.
6 Copyright (C) 2005,2006 Tim Potter <tpot@samba.org>
7 Copyright (C) 2006 Simo Sorce <idra@samba.org>
8 Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
10 ** NOTE! The following LGPL license applies to the ldb
11 ** library. This does NOT imply that all of Samba is released
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
33 #include "ldb_errors.h"
34 #include "ldb_private.h"
36 PyObject *PyExc_LdbError;
38 void PyErr_SetLdbError(int ret, struct ldb_context *ldb_ctx)
40 PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", ret, ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
42 PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
43 struct ldb_message_element *el,
46 const struct ldb_schema_attribute *a;
47 struct ldb_val new_val;
48 TALLOC_CTX *mem_ctx = talloc_new(NULL);
53 if (ldb_ctx != NULL) {
54 a = ldb_schema_attribute_by_name(ldb_ctx, el->name);
57 if (a->syntax->ldif_write_fn(ldb_ctx, mem_ctx, val, &new_val) != 0) {
64 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
71 bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object,
72 struct ldb_context *ldb_ctx, struct ldb_dn **dn)
76 if (ldb_ctx != NULL && PyString_Check(object)) {
77 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
82 if (PyLdbDn_Check(object)) {
83 *dn = PyLdbDn_AsDn(object);
87 PyErr_SetString(PyExc_TypeError, "Expected DN");
91 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
98 ret = PyList_New(result->count);
99 for (i = 0; i < result->count; i++) {
100 PyList_SetItem(ret, i,
101 PyLdbMessage_FromMessage(result->msgs[i])
107 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx, PyObject *obj)
109 struct ldb_result *res;
115 res = talloc_zero(mem_ctx, struct ldb_result);
116 res->count = PyList_Size(obj);
117 res->msgs = talloc_array(res, struct ldb_message *, res->count);
118 for (i = 0; i < res->count; i++) {
119 PyObject *item = PyList_GetItem(obj, i);
120 res->msgs[i] = PyLdbMessage_AsMessage(item);
125 static PyObject *py_ldb_dn_validate(PyLdbDnObject *self)
127 return PyBool_FromLong(ldb_dn_validate(self->ptr));
130 static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self)
132 return PyBool_FromLong(ldb_dn_is_valid(self->ptr));
135 static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self)
137 return PyBool_FromLong(ldb_dn_is_special(self->ptr));
140 static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self)
142 return PyBool_FromLong(ldb_dn_is_null(self->ptr));
145 static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self)
147 return PyString_FromString(ldb_dn_get_casefold(self->ptr));
150 static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self)
152 return PyString_FromString(ldb_dn_get_linearized(self->ptr));
155 static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self)
157 return PyString_FromString(ldb_dn_canonical_string(self->ptr, self->ptr));
160 static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self)
162 return PyString_FromString(ldb_dn_canonical_ex_string(self->ptr, self->ptr));
165 static PyObject *py_ldb_dn_repr(PyLdbDnObject *self)
167 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->ptr))));
170 static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args)
174 if (!PyArg_ParseTuple(args, "s", &name))
177 return ldb_dn_check_special(self->ptr, name)?Py_True:Py_False;
180 static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
182 return ldb_dn_compare(dn1->ptr, dn2->ptr);
185 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
187 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
188 return PyLdbDn_FromDn(ldb_dn_get_parent(NULL, dn));
191 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
193 static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
196 struct ldb_dn *dn, *other;
197 if (!PyArg_ParseTuple(args, "O", &py_other))
200 dn = PyLdbDn_AsDn((PyObject *)self);
202 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
205 return ldb_dn_add_child(dn, other)?Py_True:Py_False;
208 static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
211 struct ldb_dn *other, *dn;
212 if (!PyArg_ParseTuple(args, "O", &py_other))
215 dn = PyLdbDn_AsDn((PyObject *)self);
217 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
220 return ldb_dn_add_base(dn, other)?Py_True:Py_False;
223 static PyMethodDef py_ldb_dn_methods[] = {
224 { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS,
225 "S.validate() -> bool\n"
226 "Validate DN is correct." },
227 { "is_valid", (PyCFunction)py_ldb_dn_is_valid, METH_NOARGS,
228 "S.is_valid() -> bool\n" },
229 { "is_special", (PyCFunction)py_ldb_dn_is_special, METH_NOARGS,
230 "S.is_special() -> bool\n"
231 "Check whether this is a special LDB DN." },
232 { "is_null", (PyCFunction)py_ldb_dn_is_null, METH_NOARGS,
233 "Check whether this is a null DN." },
234 { "get_casefold", (PyCFunction)py_ldb_dn_get_casefold, METH_NOARGS,
236 { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS,
238 { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
239 "S.canonical_str() -> string\n"
240 "Canonical version of this DN (like a posix path)." },
241 { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
242 "S.canonical_ex_str() -> string\n"
243 "Canonical version of this DN (like a posix path, with terminating newline)." },
244 { "check_special", (PyCFunction)py_ldb_dn_is_special, METH_VARARGS,
246 { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS,
248 "Get the parent for this DN." },
249 { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS,
250 "S.add_child(dn) -> None\n"
251 "Add a child DN to this DN." },
252 { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
253 "S.add_base(dn) -> None\n"
254 "Add a base DN to this DN." },
255 { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
260 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
262 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
265 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
267 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self),
269 struct ldb_dn *ret = ldb_dn_copy(NULL, dn);
270 if (!PyObject_AsDn(NULL, py_other, NULL, &other))
272 ldb_dn_add_child(ret, other);
273 return PyLdbDn_FromDn(ret);
276 static PySequenceMethods py_ldb_dn_seq = {
277 .sq_length = (lenfunc)py_ldb_dn_len,
278 .sq_concat = (binaryfunc)py_ldb_dn_concat,
281 static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
286 struct ldb_context *ldb_ctx;
287 const char *kwnames[] = { "ldb", "dn", NULL };
289 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os", (char **)kwnames, &py_ldb, &str))
292 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
294 ret = ldb_dn_new(ldb_ctx, ldb_ctx, str);
295 /* ldb_dn_new() doesn't accept NULL as memory context, so
296 we do it this way... */
297 talloc_steal(NULL, ret);
299 if (ret == NULL || !ldb_dn_validate(ret)) {
300 PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
304 return py_talloc_import(&PyLdbDn, ret);
307 PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
309 return py_talloc_import(&PyLdbDn, dn);
312 PyTypeObject PyLdbDn = {
314 .tp_methods = py_ldb_dn_methods,
315 .tp_str = (reprfunc)py_ldb_dn_get_linearized,
316 .tp_repr = (reprfunc)py_ldb_dn_repr,
317 .tp_compare = (cmpfunc)py_ldb_dn_compare,
318 .tp_as_sequence = &py_ldb_dn_seq,
319 .tp_doc = "A LDB distinguished name.",
320 .tp_new = py_ldb_dn_new,
321 .tp_dealloc = py_talloc_dealloc,
322 .tp_basicsize = sizeof(PyLdbObject),
323 .tp_flags = Py_TPFLAGS_DEFAULT,
327 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
328 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
330 PyObject *fn = context;
331 PyObject_CallFunction(fn, (char *)"(i,O)", level, PyString_FromFormatV(fmt, ap));
334 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
338 if (!PyArg_ParseTuple(args, "O", &cb))
342 /* FIXME: Where do we DECREF cb ? */
343 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_set_debug(self->ptr, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
348 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
351 if (!PyArg_ParseTuple(args, "I", &perms))
354 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
359 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
362 if (!PyArg_ParseTuple(args, "s", &modules_dir))
365 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
370 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
372 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
376 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
378 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
382 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
384 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
388 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
390 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
394 static PyObject *py_ldb_repr(PyLdbObject *self)
396 return PyString_FromFormat("<ldb connection>");
399 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
401 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
404 return PyLdbDn_FromDn(dn);
408 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
410 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
413 return PyLdbDn_FromDn(dn);
417 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
419 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
422 return PyLdbDn_FromDn(dn);
426 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
428 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
431 return PyLdbDn_FromDn(dn);
434 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list)
438 if (!PyList_Check(list)) {
439 PyErr_SetString(PyExc_TypeError, "options is not a list");
442 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
443 for (i = 0; i < PyList_Size(list); i++) {
444 PyObject *item = PyList_GetItem(list, i);
445 if (!PyString_Check(item)) {
446 PyErr_SetString(PyExc_TypeError, "options should be strings");
449 ret[i] = PyString_AsString(item);
455 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
457 const char *kwnames[] = { "url", "flags", "options", NULL };
459 PyObject *py_options = Py_None;
460 const char **options;
463 struct ldb_context *ldb;
465 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__", (char **)kwnames,
466 &url, &flags, &py_options))
469 ldb = PyLdb_AsLdbContext(self);
471 if (py_options == Py_None) {
474 options = PyList_AsStringList(ldb, py_options);
480 ret = ldb_connect(ldb, url, flags, options);
481 if (ret != LDB_SUCCESS) {
482 PyErr_SetLdbError(ret, ldb);
487 talloc_free(options);
491 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
493 struct ldb_context *ldb;
494 ldb = ldb_init(NULL, event_context_init(NULL));
500 return py_talloc_import(type, ldb);
503 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
507 PyObject *py_options = Py_None;
509 const char **options;
510 const char *kwnames[] = { "url", "flags", "options", NULL };
511 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO", (char **)kwnames, &url, &flags,
515 if (py_options == Py_None) {
518 options = PyList_AsStringList(NULL, py_options);
523 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
524 talloc_free(options);
526 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
531 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
535 if (!PyArg_ParseTuple(args, "O", &py_msg))
538 if (!PyLdbMessage_Check(py_msg)) {
539 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
543 ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
544 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
549 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
553 Py_ssize_t dict_pos, msg_pos;
554 struct ldb_message_element *msgel;
555 struct ldb_message *msg;
556 PyObject *key, *value;
558 if (!PyArg_ParseTuple(args, "O", &py_msg))
561 if (PyDict_Check(py_msg)) {
562 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
563 msg = ldb_msg_new(NULL);
564 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
565 msg_pos = dict_pos = 0;
567 if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
568 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
571 if (msg->dn == NULL) {
572 PyErr_SetString(PyExc_TypeError, "dn set but not found");
577 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
578 char *key_str = PyString_AsString(key);
579 if (strcmp(key_str, "dn") != 0) {
580 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
582 PyErr_SetString(PyExc_TypeError, "unable to import element");
585 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
590 if (msg->dn == NULL) {
591 PyErr_SetString(PyExc_TypeError, "no dn set");
595 msg->num_elements = msg_pos;
597 msg = PyLdbMessage_AsMessage(py_msg);
600 ret = ldb_add(PyLdb_AsLdbContext(self), msg);
601 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
608 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
613 if (!PyArg_ParseTuple(args, "O", &py_dn))
616 if (!PyObject_AsDn(NULL, py_dn, PyLdb_AsLdbContext(self), &dn))
619 ret = ldb_delete(PyLdb_AsLdbContext(self), dn);
620 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
625 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
627 PyObject *py_dn1, *py_dn2;
628 struct ldb_dn *dn1, *dn2;
630 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
633 if (!PyLdbDn_Check(py_dn1) || !PyLdbDn_Check(py_dn2)) {
634 PyErr_SetString(PyExc_TypeError, "Expected Ldb Dn");
638 if (!PyObject_AsDn(NULL, py_dn1, PyLdb_AsLdbContext(self), &dn1))
641 if (!PyObject_AsDn(NULL, py_dn2, PyLdb_AsLdbContext(self), &dn2))
644 ret = ldb_rename(PyLdb_AsLdbContext(self), dn1, dn2);
645 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
650 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
653 if (!PyArg_ParseTuple(args, "s", &name))
656 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
661 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
663 char *attribute, *syntax;
666 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
669 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
671 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
676 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
681 /* We don't want this attached to the 'ldb' any more */
682 talloc_steal(NULL, ldif);
683 return Py_BuildValue((char *)"(iO)", ldif->changetype,
684 PyLdbMessage_FromMessage(ldif->msg));
689 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
692 struct ldb_ldif *ldif;
695 if (!PyArg_ParseTuple(args, "s", &s))
698 list = PyList_New(0);
699 while ((ldif = ldb_ldif_read_string(self->ptr, &s)) != NULL) {
700 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
702 return PyObject_GetIter(list);
705 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
707 const struct ldb_schema_attribute *a;
708 struct ldb_val old_val;
709 struct ldb_val new_val;
715 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
718 mem_ctx = talloc_new(NULL);
720 old_val.data = (uint8_t *)PyString_AsString(val);
721 old_val.length = PyString_Size(val);
723 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
729 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
730 talloc_free(mem_ctx);
734 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
736 talloc_free(mem_ctx);
741 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
743 PyObject *py_base = Py_None;
744 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
746 PyObject *py_attrs = Py_None;
747 PyObject *py_controls = Py_None;
748 const char *kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
750 struct ldb_result *res;
751 struct ldb_request *req;
753 struct ldb_context *ldb_ctx;
754 struct ldb_control **parsed_controls;
757 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OisOO", (char **)kwnames,
758 &py_base, &scope, &expr, &py_attrs, &py_controls))
761 ldb_ctx = PyLdb_AsLdbContext(self);
763 if (py_attrs == Py_None) {
766 attrs = PyList_AsStringList(ldb_ctx, py_attrs);
771 if (py_base == Py_None) {
772 base = ldb_get_default_basedn(ldb_ctx);
774 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base))
778 if (py_controls == Py_None) {
779 parsed_controls = NULL;
781 const char **controls = PyList_AsStringList(ldb_ctx, py_controls);
782 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
783 talloc_free(controls);
786 res = talloc_zero(ldb_ctx, struct ldb_result);
792 ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
799 ldb_search_default_callback,
802 if (ret != LDB_SUCCESS) {
804 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb_ctx);
808 ret = ldb_request(ldb_ctx, req);
810 if (ret == LDB_SUCCESS) {
811 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
816 return PyLdbResult_FromResult(res);
819 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
824 if (!PyArg_ParseTuple(args, "s", &name))
827 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
829 /* FIXME: More interpretation */
834 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
839 if (!PyArg_ParseTuple(args, "sO", &name, &data))
842 /* FIXME: More interpretation */
844 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
849 static PyMethodDef py_ldb_methods[] = {
850 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
851 "S.set_debug(callback) -> None\n"
852 "Set callback for LDB debug messages.\n"
853 "The callback should accept a debug level and debug text." },
854 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
855 "S.set_create_perms(mode) -> None\n"
856 "Set mode to use when creating new LDB files." },
857 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
858 "S.set_modules_dir(path) -> None\n"
859 "Set path LDB should search for modules" },
860 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
861 "S.transaction_start() -> None\n"
862 "Start a new transaction." },
863 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
864 "S.transaction_commit() -> None\n"
865 "commit a new transaction." },
866 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
867 "S.transaction_cancel() -> None\n"
868 "cancel a new transaction." },
869 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
871 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
873 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
875 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
877 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
879 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
880 "S.connect(url, flags=0, options=None) -> None\n"
881 "Connect to a LDB URL." },
882 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
883 "S.modify(message) -> None\n"
884 "Modify an entry." },
885 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
886 "S.add(message) -> None\n"
888 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
889 "S.delete(dn) -> None\n"
890 "Remove an entry." },
891 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
892 "S.rename(old_dn, new_dn) -> None\n"
893 "Rename an entry." },
894 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
895 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
896 "Search in a database.\n"
898 ":param base: Optional base DN to search\n"
899 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
900 ":param expression: Optional search expression\n"
901 ":param attrs: Attributes to return (defaults to all)\n"
902 ":param controls: Optional list of controls\n"
903 ":return: Iterator over Message objects\n"
905 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
907 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
909 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
911 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
912 "S.parse_ldif(ldif) -> iter(messages)\n"
913 "Parse a string formatted using LDIF." },
914 { "get_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
915 "S.get_opaque(name) -> value\n"
916 "Get an opaque value set on this LDB connection. \n"
917 ":note: The returned value may not be useful in Python."
919 { "set_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
920 "S.set_opaque(name, value) -> None\n"
921 "Set an opaque value on this LDB connection. \n"
922 ":note: Passing incorrect values may cause crashes." },
926 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
928 return py_talloc_import(&PyLdbModule, mod);
931 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
933 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
936 static PyGetSetDef py_ldb_getset[] = {
937 { (char *)"firstmodule", (getter)py_ldb_get_firstmodule, NULL, NULL },
941 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
943 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
945 struct ldb_result *result;
949 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
952 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
953 if (ret != LDB_SUCCESS) {
954 PyErr_SetLdbError(ret, ldb_ctx);
958 count = result->count;
965 static PySequenceMethods py_ldb_seq = {
966 .sq_contains = (objobjproc)py_ldb_contains,
969 PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
971 return py_talloc_import(&PyLdb, ldb_ctx);
974 PyTypeObject PyLdb = {
976 .tp_methods = py_ldb_methods,
977 .tp_repr = (reprfunc)py_ldb_repr,
978 .tp_new = py_ldb_new,
979 .tp_init = (initproc)py_ldb_init,
980 .tp_dealloc = py_talloc_dealloc,
981 .tp_getset = py_ldb_getset,
982 .tp_getattro = PyObject_GenericGetAttr,
983 .tp_basicsize = sizeof(PyLdbObject),
984 .tp_doc = "Connection to a LDB database.",
985 .tp_as_sequence = &py_ldb_seq,
986 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
989 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
991 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
994 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
996 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
999 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1001 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1005 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1007 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1011 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1013 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1017 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1019 PyObject *py_base, *py_tree, *py_attrs;
1021 struct ldb_request *req;
1022 const char *kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1023 struct ldb_module *mod;
1024 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO", (char **)kwnames,
1025 &py_base, &scope, &py_tree, &py_attrs))
1028 req = talloc_zero(NULL, struct ldb_request);
1030 req->operation = LDB_SEARCH;
1031 req->op.search.base = PyLdbDn_AsDn(py_base);
1032 req->op.search.scope = scope;
1033 req->op.search.tree = PyLdbTree_AsTree(py_tree);
1034 if (py_attrs == Py_None) {
1035 req->op.search.attrs = NULL;
1037 req->op.search.attrs = PyList_AsStringList(req, py_attrs);
1040 req->op.search.res = talloc_zero(NULL, struct ldb_result);
1044 ret = mod->ops->search(mod, req);
1047 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1049 return PyLdbResult_FromResult(req->op.search.res);
1053 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1055 struct ldb_request *req;
1056 PyObject *py_message;
1059 if (!PyArg_ParseTuple(args, "O", &py_message))
1062 req = talloc_zero(NULL, struct ldb_request);
1063 req->operation = LDB_ADD;
1064 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1066 ret = PyLdbModule_AsModule(self)->ops->add(PyLdbModule_AsModule(self), req);
1068 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1073 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
1076 struct ldb_request *req;
1077 PyObject *py_message;
1079 if (!PyArg_ParseTuple(args, "O", &py_message))
1082 req = talloc_zero(NULL, struct ldb_request);
1083 req->operation = LDB_MODIFY;
1084 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1086 ret = PyLdbModule_AsModule(self)->ops->modify(PyLdbModule_AsModule(self), req);
1088 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1093 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
1096 struct ldb_request *req;
1099 if (!PyArg_ParseTuple(args, "O", &py_dn))
1102 req = talloc_zero(NULL, struct ldb_request);
1103 req->operation = LDB_DELETE;
1104 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1106 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1108 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1113 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1116 struct ldb_request *req;
1117 PyObject *py_dn1, *py_dn2;
1119 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1122 req = talloc_zero(NULL, struct ldb_request);
1124 req->operation = LDB_RENAME;
1125 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1126 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1128 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1130 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1135 static PyMethodDef py_ldb_module_methods[] = {
1136 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1137 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1138 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1139 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1140 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1141 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1142 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1143 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1147 PyTypeObject PyLdbModule = {
1148 .tp_name = "LdbModule",
1149 .tp_methods = py_ldb_module_methods,
1150 .tp_repr = (reprfunc)py_ldb_module_repr,
1151 .tp_str = (reprfunc)py_ldb_module_str,
1152 .tp_basicsize = sizeof(py_talloc_Object),
1153 .tp_dealloc = py_talloc_dealloc,
1154 .tp_flags = Py_TPFLAGS_DEFAULT,
1157 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1158 PyObject *set_obj, int flags,
1159 const char *attr_name)
1161 struct ldb_message_element *me;
1163 if (PyLdbMessageElement_Check(set_obj))
1164 return PyLdbMessageElement_AsMessageElement(set_obj);
1166 me = talloc(mem_ctx, struct ldb_message_element);
1168 me->name = attr_name;
1170 if (PyString_Check(set_obj)) {
1172 me->values = talloc_array(me, struct ldb_val, me->num_values);
1173 me->values[0].length = PyString_Size(set_obj);
1174 me->values[0].data = (uint8_t *)talloc_strdup(me->values,
1175 PyString_AsString(set_obj));
1176 } else if (PySequence_Check(set_obj)) {
1178 me->num_values = PySequence_Size(set_obj);
1179 me->values = talloc_array(me, struct ldb_val, me->num_values);
1180 for (i = 0; i < me->num_values; i++) {
1181 PyObject *obj = PySequence_GetItem(set_obj, i);
1182 me->values[i].length = PyString_Size(obj);
1183 me->values[i].data = (uint8_t *)PyString_AsString(obj);
1194 PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
1195 struct ldb_message_element *me)
1200 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1201 result = PyList_New(me->num_values);
1203 for (i = 0; i < me->num_values; i++) {
1204 PyList_SetItem(result, i,
1205 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1211 PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1214 if (!PyArg_ParseTuple(args, "i", &i))
1216 if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1219 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1220 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1223 static PyMethodDef py_ldb_msg_element_methods[] = {
1224 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1228 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1230 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1233 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1235 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1236 if (idx < 0 || idx >= el->num_values) {
1237 PyErr_SetString(PyExc_IndexError, "Out of range");
1240 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1243 static PySequenceMethods py_ldb_msg_element_seq = {
1244 .sq_length = (lenfunc)py_ldb_msg_element_len,
1245 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1248 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1250 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1251 PyLdbMessageElement_AsMessageElement(other));
1254 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1256 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1259 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el)
1261 return py_talloc_import(&PyLdbMessageElement, el);
1264 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1266 PyObject *py_elements = NULL;
1267 struct ldb_message_element *el;
1270 const char *kwnames[] = { "elements", "flags", "name", NULL };
1271 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois", (char **)kwnames, &py_elements, &flags, &name))
1274 el = talloc_zero(NULL, struct ldb_message_element);
1276 if (py_elements != NULL) {
1278 if (!PySequence_Check(py_elements)) {
1280 el->values = talloc_array(el, struct ldb_val, 1);
1281 el->values[0].data = (uint8_t *)PyString_AsString(py_elements);
1282 el->values[0].length = PyString_Size(py_elements);
1284 el->num_values = PySequence_Size(py_elements);
1285 el->values = talloc_array(el, struct ldb_val, el->num_values);
1286 for (i = 0; i < el->num_values; i++) {
1287 PyObject *item = PySequence_GetItem(py_elements, i);
1288 el->values[i].data = (uint8_t *)PyString_AsString(item);
1289 el->values[i].length = PyString_Size(item);
1295 el->name = talloc_strdup(el, name);
1297 return py_talloc_import(&PyLdbMessageElement, el);
1300 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1302 char *element_str = NULL;
1304 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1307 for (i = 0; i < el->num_values; i++) {
1308 PyObject *o = py_ldb_msg_element_find(self, i);
1309 if (element_str == NULL)
1310 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1312 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1315 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1317 talloc_free(element_str);
1322 PyTypeObject PyLdbMessageElement = {
1323 .tp_name = "MessageElement",
1324 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1325 .tp_dealloc = py_talloc_dealloc,
1326 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1327 .tp_methods = py_ldb_msg_element_methods,
1328 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1329 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1330 .tp_as_sequence = &py_ldb_msg_element_seq,
1331 .tp_new = py_ldb_msg_element_new,
1332 .tp_flags = Py_TPFLAGS_DEFAULT,
1335 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
1338 if (!PyArg_ParseTuple(args, "s", &name))
1341 ldb_msg_remove_attr(self->ptr, name);
1346 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
1348 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1350 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1351 if (msg->dn != NULL) {
1352 PyList_SetItem(obj, j, PyString_FromString("dn"));
1355 for (i = 0; i < msg->num_elements; i++) {
1356 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1362 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
1364 struct ldb_message_element *el;
1365 char *name = PyString_AsString(py_name);
1366 if (!strcmp(name, "dn"))
1367 return PyLdbDn_FromDn(PyLdbMessage_AsMessage(self)->dn);
1368 el = ldb_msg_find_element(PyLdbMessage_AsMessage(self), name);
1372 return (PyObject *)PyLdbMessageElement_FromMessageElement(el);
1375 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
1377 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
1379 PyErr_SetString(PyExc_KeyError, "No such element");
1385 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
1387 PyObject *name, *ret;
1388 if (!PyArg_ParseTuple(args, "O", &name))
1391 ret = py_ldb_msg_getitem_helper(self, name);
1397 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
1399 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1401 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
1403 if (msg->dn != NULL) {
1404 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
1407 for (i = 0; i < msg->num_elements; i++, j++) {
1408 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i])));
1413 static PyMethodDef py_ldb_msg_methods[] = {
1414 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
1415 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
1416 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
1417 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
1421 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
1423 char *attr_name = PyString_AsString(name);
1424 if (value == NULL) {
1425 ldb_msg_remove_attr(self->ptr, attr_name);
1427 struct ldb_message_element *el = PyObject_AsMessageElement(NULL,
1428 value, 0, attr_name);
1431 talloc_steal(self->ptr, el);
1432 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
1433 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
1438 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
1440 return PyLdbMessage_AsMessage(self)->num_elements;
1443 static PyMappingMethods py_ldb_msg_mapping = {
1444 .mp_length = (lenfunc)py_ldb_msg_length,
1445 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
1446 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
1449 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1451 const char *kwnames[] = { "dn", NULL };
1452 struct ldb_message *ret;
1453 PyObject *pydn = NULL;
1454 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", (char **)kwnames, &pydn))
1457 ret = ldb_msg_new(NULL);
1464 if (!PyObject_AsDn(NULL, pydn, NULL, &ret->dn))
1467 return py_talloc_import(type, ret);
1470 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
1472 return py_talloc_import(&PyLdbMessage, msg);
1475 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
1477 return PyLdbDn_FromDn(PyLdbMessage_AsMessage(self)->dn);
1480 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
1482 PyLdbMessage_AsMessage(self)->dn = PyLdbDn_AsDn(value);
1486 static PyGetSetDef py_ldb_msg_getset[] = {
1487 { (char *)"dn", (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
1491 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
1493 PyObject *dict = PyDict_New(), *ret;
1494 if (PyDict_Update(dict, (PyObject *)self) != 0)
1496 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
1501 PyTypeObject PyLdbMessage = {
1502 .tp_name = "Message",
1503 .tp_methods = py_ldb_msg_methods,
1504 .tp_getset = py_ldb_msg_getset,
1505 .tp_as_mapping = &py_ldb_msg_mapping,
1506 .tp_basicsize = sizeof(PyLdbMessageObject),
1507 .tp_dealloc = py_talloc_dealloc,
1508 .tp_new = py_ldb_msg_new,
1509 .tp_repr = (reprfunc)py_ldb_msg_repr,
1510 .tp_flags = Py_TPFLAGS_DEFAULT,
1513 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
1515 return py_talloc_import(&PyLdbTree, tree);
1518 PyTypeObject PyLdbTree = {
1520 .tp_basicsize = sizeof(PyLdbTreeObject),
1521 .tp_dealloc = py_talloc_dealloc,
1522 .tp_flags = Py_TPFLAGS_DEFAULT,
1526 int py_module_search(struct ldb_module *mod, struct ldb_request *req)
1528 PyObject *py_ldb = mod->private_data;
1529 PyObject *py_result, *py_base, *py_attrs, *py_tree;
1531 py_base = PyLdbDn_FromDn(req->op.search.base);
1533 if (py_base == NULL)
1534 return LDB_ERR_OPERATIONS_ERROR;
1536 py_tree = PyLdbTree_FromTree(req->op.search.tree);
1538 if (py_tree == NULL)
1539 return LDB_ERR_OPERATIONS_ERROR;
1541 if (req->op.search.attrs == NULL) {
1545 for (len = 0; req->op.search.attrs[len]; len++);
1546 py_attrs = PyList_New(len);
1547 for (i = 0; i < len; i++)
1548 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1551 py_result = PyObject_CallMethod(py_ldb, (char *)"search", (char *)"OiOO", py_base, req->op.search.scope, py_tree, py_attrs);
1553 Py_DECREF(py_attrs);
1557 if (py_result == NULL) {
1558 return LDB_ERR_OPERATIONS_ERROR;
1561 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
1562 if (req->op.search.res == NULL) {
1563 return LDB_ERR_OPERATIONS_ERROR;
1566 Py_DECREF(py_result);
1571 int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1573 PyObject *py_ldb = mod->private_data;
1574 PyObject *py_result, *py_msg;
1576 py_msg = PyLdbMessage_FromMessage((struct ldb_message *)req->op.add.message);
1578 if (py_msg == NULL) {
1579 return LDB_ERR_OPERATIONS_ERROR;
1582 py_result = PyObject_CallMethod(py_ldb, (char *)"add", (char *)"O", py_msg);
1586 if (py_result == NULL) {
1587 return LDB_ERR_OPERATIONS_ERROR;
1590 Py_DECREF(py_result);
1595 int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1597 PyObject *py_ldb = mod->private_data;
1598 PyObject *py_result, *py_msg;
1600 py_msg = PyLdbMessage_FromMessage((struct ldb_message *)req->op.mod.message);
1602 if (py_msg == NULL) {
1603 return LDB_ERR_OPERATIONS_ERROR;
1606 py_result = PyObject_CallMethod(py_ldb, (char *)"modify", (char *)"O", py_msg);
1610 if (py_result == NULL) {
1611 return LDB_ERR_OPERATIONS_ERROR;
1614 Py_DECREF(py_result);
1619 int py_module_del(struct ldb_module *mod, struct ldb_request *req)
1621 PyObject *py_ldb = mod->private_data;
1622 PyObject *py_result, *py_dn;
1624 py_dn = PyLdbDn_FromDn(req->op.del.dn);
1627 return LDB_ERR_OPERATIONS_ERROR;
1629 py_result = PyObject_CallMethod(py_ldb, (char *)"delete", (char *)"O", py_dn);
1631 if (py_result == NULL) {
1632 return LDB_ERR_OPERATIONS_ERROR;
1635 Py_DECREF(py_result);
1640 int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
1642 PyObject *py_ldb = mod->private_data;
1643 PyObject *py_result, *py_olddn, *py_newdn;
1645 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
1647 if (py_olddn == NULL)
1648 return LDB_ERR_OPERATIONS_ERROR;
1650 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
1652 if (py_newdn == NULL)
1653 return LDB_ERR_OPERATIONS_ERROR;
1655 py_result = PyObject_CallMethod(py_ldb, (char *)"rename", (char *)"OO", py_olddn, py_newdn);
1657 Py_DECREF(py_olddn);
1658 Py_DECREF(py_newdn);
1660 if (py_result == NULL) {
1661 return LDB_ERR_OPERATIONS_ERROR;
1664 Py_DECREF(py_result);
1669 int py_module_request(struct ldb_module *mod, struct ldb_request *req)
1671 PyObject *py_ldb = mod->private_data;
1672 PyObject *py_result;
1674 py_result = PyObject_CallMethod(py_ldb, (char *)"request", (char *)"");
1676 return LDB_ERR_OPERATIONS_ERROR;
1679 int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
1681 PyObject *py_ldb = mod->private_data;
1682 PyObject *py_result;
1684 py_result = PyObject_CallMethod(py_ldb, (char *)"extended", (char *)"");
1686 return LDB_ERR_OPERATIONS_ERROR;
1689 int py_module_start_transaction(struct ldb_module *mod)
1691 PyObject *py_ldb = mod->private_data;
1692 PyObject *py_result;
1694 py_result = PyObject_CallMethod(py_ldb, (char *)"start_transaction", (char *)"");
1696 if (py_result == NULL) {
1697 return LDB_ERR_OPERATIONS_ERROR;
1700 Py_DECREF(py_result);
1705 int py_module_end_transaction(struct ldb_module *mod)
1707 PyObject *py_ldb = mod->private_data;
1708 PyObject *py_result;
1710 py_result = PyObject_CallMethod(py_ldb, (char *)"end_transaction", (char *)"");
1712 if (py_result == NULL) {
1713 return LDB_ERR_OPERATIONS_ERROR;
1716 Py_DECREF(py_result);
1721 int py_module_del_transaction(struct ldb_module *mod)
1723 PyObject *py_ldb = mod->private_data;
1724 PyObject *py_result;
1726 py_result = PyObject_CallMethod(py_ldb, (char *)"del_transaction", (char *)"");
1728 if (py_result == NULL) {
1729 return LDB_ERR_OPERATIONS_ERROR;
1732 Py_DECREF(py_result);
1737 static int py_module_destructor(struct ldb_module *mod)
1739 Py_DECREF((PyObject *)mod->private_data);
1743 int py_module_init (struct ldb_module *mod)
1745 PyObject *py_class = mod->ops->private_data;
1746 PyObject *py_result, *py_next, *py_ldb;
1748 py_ldb = PyLdb_FromLdbContext(mod->ldb);
1751 return LDB_ERR_OPERATIONS_ERROR;
1753 py_next = PyLdbModule_FromModule(mod->next);
1755 if (py_next == NULL)
1756 return LDB_ERR_OPERATIONS_ERROR;
1758 py_result = PyObject_CallFunction(py_class, (char *)"OO", py_ldb, py_next);
1760 if (py_result == NULL) {
1761 return LDB_ERR_OPERATIONS_ERROR;
1764 mod->private_data = py_result;
1766 talloc_set_destructor(mod, py_module_destructor);
1768 return ldb_next_init(mod);
1771 static PyObject *py_register_module(PyObject *module, PyObject *args)
1774 struct ldb_module_ops *ops;
1777 if (!PyArg_ParseTuple(args, "O", &input))
1780 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
1786 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, (char *)"name")));
1789 ops->private_data = input;
1790 ops->init_context = py_module_init;
1791 ops->search = py_module_search;
1792 ops->add = py_module_add;
1793 ops->modify = py_module_modify;
1794 ops->del = py_module_del;
1795 ops->rename = py_module_rename;
1796 ops->request = py_module_request;
1797 ops->extended = py_module_extended;
1798 ops->start_transaction = py_module_start_transaction;
1799 ops->end_transaction = py_module_end_transaction;
1800 ops->del_transaction = py_module_del_transaction;
1802 ret = ldb_register_module(ops);
1804 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1809 static PyObject *py_timestring(PyObject *module, PyObject *args)
1814 if (!PyArg_ParseTuple(args, "L", &t))
1816 tresult = ldb_timestring(NULL, t);
1817 ret = PyString_FromString(tresult);
1818 talloc_free(tresult);
1822 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
1825 if (!PyArg_ParseTuple(args, "s", &str))
1828 return PyInt_FromLong(ldb_string_to_time(str));
1831 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
1834 if (!PyArg_ParseTuple(args, "s", &name))
1836 return PyBool_FromLong(ldb_valid_attr_name(name));
1839 static PyMethodDef py_ldb_global_methods[] = {
1840 { "register_module", py_register_module, METH_VARARGS,
1841 "S.register_module(module) -> None\n"
1842 "Register a LDB module."},
1843 { "timestring", py_timestring, METH_VARARGS,
1844 "S.timestring(int) -> string\n"
1845 "Generate a LDAP time string from a UNIX timestamp" },
1846 { "string_to_time", py_string_to_time, METH_VARARGS,
1847 "S.string_to_time(string) -> int\n"
1848 "Parse a LDAP time string into a UNIX timestamp." },
1849 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
1850 "S.valid_attr_name(name) -> bool\n"
1851 "Check whether the supplied name is a valid attribute name." },
1852 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
1861 if (PyType_Ready(&PyLdbDn) < 0)
1864 if (PyType_Ready(&PyLdbMessage) < 0)
1867 if (PyType_Ready(&PyLdbMessageElement) < 0)
1870 if (PyType_Ready(&PyLdb) < 0)
1873 if (PyType_Ready(&PyLdbModule) < 0)
1876 if (PyType_Ready(&PyLdbTree) < 0)
1879 m = Py_InitModule3("ldb", py_ldb_global_methods,
1880 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
1884 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
1885 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
1886 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
1887 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
1889 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
1890 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
1891 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
1892 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
1894 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
1895 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
1896 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
1897 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
1898 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
1899 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
1900 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
1901 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
1902 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
1903 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
1904 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
1905 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
1906 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
1907 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
1908 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
1909 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
1910 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
1911 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
1912 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
1913 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
1914 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
1915 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
1916 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
1917 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
1918 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
1919 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
1920 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
1921 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
1922 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
1923 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
1924 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
1925 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
1926 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
1927 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
1928 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
1929 PyModule_AddObject(m, "ERR_ENTYR_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
1930 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
1931 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
1933 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
1935 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
1937 PyExc_LdbError = PyErr_NewException((char *)"_ldb.LdbError", NULL, NULL);
1938 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
1941 Py_INCREF(&PyLdbDn);
1942 Py_INCREF(&PyLdbModule);
1943 Py_INCREF(&PyLdbMessage);
1944 Py_INCREF(&PyLdbMessageElement);
1945 Py_INCREF(&PyLdbTree);
1947 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
1948 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
1949 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
1950 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
1951 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
1952 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);
1957 /* ldb_message_element */
1958 %rename(MessageElement) ldb_message_element;
1959 %feature("docstring") ldb_message_element "Message element.";
1960 typedef struct ldb_message_element {
1962 PyObject *__set__(void)
1964 return ldb_msg_element_to_set(NULL, $self);
1967 ldb_message_element(PyObject *set_obj, int flags=0, const char *name = NULL)
1969 return PyObject_AsMessageElement(NULL, set_obj, flags, name);
1974 def __getitem__(self, i):
1977 raise KeyError("no such value")
1981 return "MessageElement([%s])" % (",".join(repr(x) for x in self.__set__()))
1983 } ldb_message_element;
1987 typedef struct ldb_message {
1991 ldb_msg(ldb_dn *dn = NULL) {
1992 ret->dn = talloc_reference(ret, dn);
1996 void __setitem__(const char *attr_name, ldb_message_element *val)
1998 struct ldb_message_element *el;
2000 ldb_msg_remove_attr($self, attr_name);
2002 el = talloc($self, struct ldb_message_element);
2003 el->name = talloc_strdup(el, attr_name);
2004 el->num_values = val->num_values;
2005 el->values = talloc_reference(el, val->values);
2007 ldb_msg_add($self, el, val->flags);
2010 void __setitem__(const char *attr_name, PyObject *val)
2014 def get(self, key, default=None):
2017 return self.find_element(key)
2019 def iteritems(self):
2020 for k in self.keys():
2024 return list(self.iteritems())
2027 return "Message(%s)" % repr(dict(self.iteritems()))
2032 typedef struct ldb_context {
2034 def itermodules(self):
2035 m = self.firstmodule
2036 while m is not None:
2041 return list(self.itermodules())