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),
326 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
327 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
329 PyObject *fn = context;
330 PyObject_CallFunction(fn, (char *)"(i,O)", level, PyString_FromFormatV(fmt, ap));
333 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
337 if (!PyArg_ParseTuple(args, "O", &cb))
341 /* FIXME: Where do we DECREF cb ? */
342 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_set_debug(self->ptr, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
347 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
350 if (!PyArg_ParseTuple(args, "I", &perms))
353 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
358 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
361 if (!PyArg_ParseTuple(args, "s", &modules_dir))
364 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
369 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
371 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
375 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
377 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
381 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
383 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
387 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
389 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
393 static PyObject *py_ldb_repr(PyLdbObject *self)
395 return PyString_FromFormat("<ldb connection>");
398 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
400 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
403 return PyLdbDn_FromDn(dn);
407 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
409 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
412 return PyLdbDn_FromDn(dn);
416 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
418 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
421 return PyLdbDn_FromDn(dn);
425 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
427 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
430 return PyLdbDn_FromDn(dn);
433 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list)
437 if (!PyList_Check(list)) {
438 PyErr_SetString(PyExc_TypeError, "options is not a list");
441 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
442 for (i = 0; i < PyList_Size(list); i++) {
443 PyObject *item = PyList_GetItem(list, i);
444 if (!PyString_Check(item)) {
445 PyErr_SetString(PyExc_TypeError, "options should be strings");
448 ret[i] = PyString_AsString(item);
454 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
456 struct ldb_context *ldb;
457 const char *kwnames[] = { "url", "flags", "options", NULL };
459 PyObject *py_options = Py_None;
460 const char **options;
464 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO", (char **)kwnames,
465 &url, &flags, &py_options))
468 ldb = ldb_init(NULL, event_context_init(NULL));
474 if (py_options == Py_None) {
477 options = PyList_AsStringList(ldb, py_options);
483 ret = ldb_connect(ldb, url, flags, options);
484 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
487 talloc_free(options);
489 return py_talloc_import(&PyLdb, ldb);
492 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
496 PyObject *py_options = Py_None;
498 const char **options;
499 const char *kwnames[] = { "url", "flags", "options", NULL };
500 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO", (char **)kwnames, &url, &flags,
504 if (py_options == Py_None) {
507 options = PyList_AsStringList(NULL, py_options);
512 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
513 talloc_free(options);
515 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
520 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
524 if (!PyArg_ParseTuple(args, "O", &py_msg))
527 if (!PyLdbMessage_Check(py_msg)) {
528 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
532 ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
533 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
538 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
542 Py_ssize_t dict_pos, msg_pos;
543 struct ldb_message_element *msgel;
544 struct ldb_message *msg;
545 PyObject *key, *value;
547 if (!PyArg_ParseTuple(args, "O", &py_msg))
550 if (PyDict_Check(py_msg)) {
551 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
552 msg = ldb_msg_new(NULL);
553 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
554 msg_pos = dict_pos = 0;
556 if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
557 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
560 if (msg->dn == NULL) {
561 PyErr_SetString(PyExc_TypeError, "dn set but not found");
566 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
567 char *key_str = PyString_AsString(key);
568 if (strcmp(key_str, "dn") != 0) {
569 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
571 PyErr_SetString(PyExc_TypeError, "unable to import element");
574 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
579 if (msg->dn == NULL) {
580 PyErr_SetString(PyExc_TypeError, "no dn set");
584 msg->num_elements = msg_pos;
586 msg = PyLdbMessage_AsMessage(py_msg);
589 ret = ldb_add(PyLdb_AsLdbContext(self), msg);
590 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
597 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
602 if (!PyArg_ParseTuple(args, "O", &py_dn))
605 if (!PyObject_AsDn(NULL, py_dn, PyLdb_AsLdbContext(self), &dn))
608 ret = ldb_delete(PyLdb_AsLdbContext(self), dn);
609 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
614 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
616 PyObject *py_dn1, *py_dn2;
617 struct ldb_dn *dn1, *dn2;
619 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
622 if (!PyLdbDn_Check(py_dn1) || !PyLdbDn_Check(py_dn2)) {
623 PyErr_SetString(PyExc_TypeError, "Expected Ldb Dn");
627 if (!PyObject_AsDn(NULL, py_dn1, PyLdb_AsLdbContext(self), &dn1))
630 if (!PyObject_AsDn(NULL, py_dn2, PyLdb_AsLdbContext(self), &dn2))
633 ret = ldb_rename(PyLdb_AsLdbContext(self), dn1, dn2);
634 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
639 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
642 if (!PyArg_ParseTuple(args, "s", &name))
645 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
650 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
652 char *attribute, *syntax;
655 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
658 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
660 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
665 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
670 /* We don't want this attached to the 'ldb' any more */
671 talloc_steal(NULL, ldif);
672 return Py_BuildValue((char *)"(iO)", ldif->changetype,
673 PyLdbMessage_FromMessage(ldif->msg));
678 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
681 struct ldb_ldif *ldif;
684 if (!PyArg_ParseTuple(args, "s", &s))
687 list = PyList_New(0);
688 while ((ldif = ldb_ldif_read_string(self->ptr, &s)) != NULL) {
689 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
691 return PyObject_GetIter(list);
694 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
696 const struct ldb_schema_attribute *a;
697 struct ldb_val old_val;
698 struct ldb_val new_val;
704 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
707 mem_ctx = talloc_new(NULL);
709 old_val.data = (uint8_t *)PyString_AsString(val);
710 old_val.length = PyString_Size(val);
712 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
718 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
719 talloc_free(mem_ctx);
723 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
725 talloc_free(mem_ctx);
730 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
732 PyObject *py_base = Py_None;
733 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
735 PyObject *py_attrs = Py_None;
736 PyObject *py_controls = Py_None;
737 const char *kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
739 struct ldb_result *res;
740 struct ldb_request *req;
742 struct ldb_context *ldb_ctx;
743 struct ldb_control **parsed_controls;
746 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OisOO", (char **)kwnames,
747 &py_base, &scope, &expr, &py_attrs, &py_controls))
750 ldb_ctx = PyLdb_AsLdbContext(self);
752 if (py_attrs == Py_None) {
755 attrs = PyList_AsStringList(ldb_ctx, py_attrs);
760 if (py_base == Py_None) {
761 base = ldb_get_default_basedn(ldb_ctx);
763 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base))
767 if (py_controls == Py_None) {
768 parsed_controls = NULL;
770 const char **controls = PyList_AsStringList(ldb_ctx, py_controls);
771 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
772 talloc_free(controls);
775 res = talloc_zero(ldb_ctx, struct ldb_result);
781 ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
788 ldb_search_default_callback,
791 if (ret != LDB_SUCCESS) {
793 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb_ctx);
797 ret = ldb_request(ldb_ctx, req);
799 if (ret == LDB_SUCCESS) {
800 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
805 return PyLdbResult_FromResult(res);
808 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
813 if (!PyArg_ParseTuple(args, "s", &name))
816 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
818 /* FIXME: More interpretation */
823 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
828 if (!PyArg_ParseTuple(args, "sO", &name, &data))
831 /* FIXME: More interpretation */
833 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
838 static PyMethodDef py_ldb_methods[] = {
839 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
840 "S.set_debug(callback) -> None\n"
841 "Set callback for LDB debug messages.\n"
842 "The callback should accept a debug level and debug text." },
843 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
844 "S.set_create_perms(mode) -> None\n"
845 "Set mode to use when creating new LDB files." },
846 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
847 "S.set_modules_dir(path) -> None\n"
848 "Set path LDB should search for modules" },
849 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
850 "S.transaction_start() -> None\n"
851 "Start a new transaction." },
852 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
853 "S.transaction_commit() -> None\n"
854 "commit a new transaction." },
855 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
856 "S.transaction_cancel() -> None\n"
857 "cancel a new transaction." },
858 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
860 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
862 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
864 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
866 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
868 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
869 "S.connect(url, flags=0, options=None) -> None\n"
870 "Connect to a LDB URL." },
871 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
872 "S.modify(message) -> None\n"
873 "Modify an entry." },
874 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
875 "S.add(message) -> None\n"
877 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
878 "S.delete(dn) -> None\n"
879 "Remove an entry." },
880 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
881 "S.rename(old_dn, new_dn) -> None\n"
882 "Rename an entry." },
883 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
884 "Search in a database.\n"
886 ":param base: Optional base DN to search\n"
887 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
888 ":param expression: Optional search expression\n"
889 ":param attrs: Attributes to return (defaults to all)\n"
890 ":param controls: Optional list of controls\n"
891 ":return: Iterator over Message objects\n"
893 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
895 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
897 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
899 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
900 "S.parse_ldif(ldif) -> iter(messages)\n"
901 "Parse a string formatted using LDIF." },
902 { "get_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
903 "S.get_opaque(name) -> value\n"
904 "Get an opaque value set on this LDB connection. \n"
905 ":note: The returned value may not be useful in Python."
907 { "set_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
908 "S.set_opaque(name, value) -> None\n"
909 "Set an opaque value on this LDB connection. \n"
910 ":note: Passing incorrect values may cause crashes." },
914 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
916 return py_talloc_import(&PyLdbModule, mod);
919 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
921 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
924 static PyGetSetDef py_ldb_getset[] = {
925 { (char *)"firstmodule", (getter)py_ldb_get_firstmodule, NULL, NULL },
929 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
931 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
933 struct ldb_result *result;
937 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
940 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
941 if (ret != LDB_SUCCESS) {
942 PyErr_SetLdbError(ret, ldb_ctx);
946 count = result->count;
953 static PySequenceMethods py_ldb_seq = {
954 .sq_contains = (objobjproc)py_ldb_contains,
957 PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
959 return py_talloc_import(&PyLdb, ldb_ctx);
962 PyTypeObject PyLdb = {
964 .tp_methods = py_ldb_methods,
965 .tp_repr = (reprfunc)py_ldb_repr,
966 .tp_new = py_ldb_new,
967 .tp_dealloc = py_talloc_dealloc,
968 .tp_getset = py_ldb_getset,
969 .tp_basicsize = sizeof(PyLdbObject),
970 .tp_doc = "Connection to a LDB database.",
971 .tp_as_sequence = &py_ldb_seq,
972 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
975 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
977 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
980 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
982 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
985 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
987 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
991 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
993 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
997 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
999 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1003 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1005 PyObject *py_base, *py_tree, *py_attrs;
1007 struct ldb_request *req;
1008 const char *kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1009 struct ldb_module *mod;
1010 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO", (char **)kwnames,
1011 &py_base, &scope, &py_tree, &py_attrs))
1014 req = talloc_zero(NULL, struct ldb_request);
1016 req->operation = LDB_SEARCH;
1017 req->op.search.base = PyLdbDn_AsDn(py_base);
1018 req->op.search.scope = scope;
1019 req->op.search.tree = PyLdbTree_AsTree(py_tree);
1020 if (py_attrs == Py_None) {
1021 req->op.search.attrs = NULL;
1023 req->op.search.attrs = PyList_AsStringList(req, py_attrs);
1026 req->op.search.res = talloc_zero(NULL, struct ldb_result);
1030 ret = mod->ops->search(mod, req);
1033 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1035 return PyLdbResult_FromResult(req->op.search.res);
1039 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1041 struct ldb_request *req;
1042 PyObject *py_message;
1045 if (!PyArg_ParseTuple(args, "O", &py_message))
1048 req = talloc_zero(NULL, struct ldb_request);
1049 req->operation = LDB_ADD;
1050 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1052 ret = PyLdbModule_AsModule(self)->ops->add(PyLdbModule_AsModule(self), req);
1054 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1059 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
1062 struct ldb_request *req;
1063 PyObject *py_message;
1065 if (!PyArg_ParseTuple(args, "O", &py_message))
1068 req = talloc_zero(NULL, struct ldb_request);
1069 req->operation = LDB_MODIFY;
1070 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1072 ret = PyLdbModule_AsModule(self)->ops->modify(PyLdbModule_AsModule(self), req);
1074 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1079 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
1082 struct ldb_request *req;
1085 if (!PyArg_ParseTuple(args, "O", &py_dn))
1088 req = talloc_zero(NULL, struct ldb_request);
1089 req->operation = LDB_DELETE;
1090 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1092 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1094 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1099 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1102 struct ldb_request *req;
1103 PyObject *py_dn1, *py_dn2;
1105 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1108 req = talloc_zero(NULL, struct ldb_request);
1110 req->operation = LDB_RENAME;
1111 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1112 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1114 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1116 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1121 static PyMethodDef py_ldb_module_methods[] = {
1122 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1123 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1124 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1125 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1126 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1127 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1128 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1129 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1133 PyTypeObject PyLdbModule = {
1134 .tp_name = "LdbModule",
1135 .tp_methods = py_ldb_module_methods,
1136 .tp_repr = (reprfunc)py_ldb_module_repr,
1137 .tp_str = (reprfunc)py_ldb_module_str,
1138 .tp_basicsize = sizeof(py_talloc_Object),
1139 .tp_dealloc = py_talloc_dealloc,
1142 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1143 PyObject *set_obj, int flags,
1144 const char *attr_name)
1146 struct ldb_message_element *me;
1148 if (PyLdbMessageElement_Check(set_obj))
1149 return PyLdbMessageElement_AsMessageElement(set_obj);
1151 me = talloc(mem_ctx, struct ldb_message_element);
1153 me->name = attr_name;
1155 if (PyString_Check(set_obj)) {
1157 me->values = talloc_array(me, struct ldb_val, me->num_values);
1158 me->values[0].length = PyString_Size(set_obj);
1159 me->values[0].data = (uint8_t *)talloc_strdup(me->values,
1160 PyString_AsString(set_obj));
1161 } else if (PySequence_Check(set_obj)) {
1163 me->num_values = PySequence_Size(set_obj);
1164 me->values = talloc_array(me, struct ldb_val, me->num_values);
1165 for (i = 0; i < me->num_values; i++) {
1166 PyObject *obj = PySequence_GetItem(set_obj, i);
1167 me->values[i].length = PyString_Size(obj);
1168 me->values[i].data = (uint8_t *)PyString_AsString(obj);
1179 PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
1180 struct ldb_message_element *me)
1185 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1186 result = PyList_New(me->num_values);
1188 for (i = 0; i < me->num_values; i++) {
1189 PyList_SetItem(result, i,
1190 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1196 PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1199 if (!PyArg_ParseTuple(args, "i", &i))
1201 if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1204 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1205 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1208 static PyMethodDef py_ldb_msg_element_methods[] = {
1209 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1213 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1215 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1218 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1220 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1221 if (idx < 0 || idx >= el->num_values) {
1222 PyErr_SetString(PyExc_IndexError, "Out of range");
1225 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1228 static PySequenceMethods py_ldb_msg_element_seq = {
1229 .sq_length = (lenfunc)py_ldb_msg_element_len,
1230 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1233 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1235 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1236 PyLdbMessageElement_AsMessageElement(other));
1239 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1241 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1244 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el)
1246 return py_talloc_import(&PyLdbMessageElement, el);
1249 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1251 PyObject *py_elements = NULL;
1252 struct ldb_message_element *el;
1255 const char *kwnames[] = { "elements", "flags", "name", NULL };
1256 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois", (char **)kwnames, &py_elements, &flags, &name))
1259 el = talloc_zero(NULL, struct ldb_message_element);
1261 if (py_elements != NULL) {
1263 if (!PySequence_Check(py_elements)) {
1265 el->values = talloc_array(el, struct ldb_val, 1);
1266 el->values[0].data = (uint8_t *)PyString_AsString(py_elements);
1267 el->values[0].length = PyString_Size(py_elements);
1269 el->num_values = PySequence_Size(py_elements);
1270 el->values = talloc_array(el, struct ldb_val, el->num_values);
1271 for (i = 0; i < el->num_values; i++) {
1272 PyObject *item = PySequence_GetItem(py_elements, i);
1273 el->values[i].data = (uint8_t *)PyString_AsString(item);
1274 el->values[i].length = PyString_Size(item);
1280 el->name = talloc_strdup(el, name);
1282 return py_talloc_import(&PyLdbMessageElement, el);
1285 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1287 char *element_str = NULL;
1289 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1292 for (i = 0; i < el->num_values; i++) {
1293 PyObject *o = py_ldb_msg_element_find(self, i);
1294 if (element_str == NULL)
1295 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1297 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1300 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1302 talloc_free(element_str);
1307 PyTypeObject PyLdbMessageElement = {
1308 .tp_name = "MessageElement",
1309 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1310 .tp_dealloc = py_talloc_dealloc,
1311 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1312 .tp_methods = py_ldb_msg_element_methods,
1313 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1314 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1315 .tp_as_sequence = &py_ldb_msg_element_seq,
1316 .tp_new = py_ldb_msg_element_new,
1319 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
1322 if (!PyArg_ParseTuple(args, "s", &name))
1325 ldb_msg_remove_attr(self->ptr, name);
1330 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
1332 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1334 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1335 if (msg->dn != NULL) {
1336 PyList_SetItem(obj, j, PyString_FromString("dn"));
1339 for (i = 0; i < msg->num_elements; i++) {
1340 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1346 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
1348 struct ldb_message_element *el;
1349 char *name = PyString_AsString(py_name);
1350 if (!strcmp(name, "dn"))
1351 return PyLdbDn_FromDn(PyLdbMessage_AsMessage(self)->dn);
1352 el = ldb_msg_find_element(PyLdbMessage_AsMessage(self), name);
1356 return (PyObject *)PyLdbMessageElement_FromMessageElement(el);
1359 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
1361 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
1363 PyErr_SetString(PyExc_KeyError, "No such element");
1369 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
1371 PyObject *name, *ret;
1372 if (!PyArg_ParseTuple(args, "O", &name))
1375 ret = py_ldb_msg_getitem_helper(self, name);
1381 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
1383 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1385 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
1387 if (msg->dn != NULL) {
1388 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
1391 for (i = 0; i < msg->num_elements; i++, j++) {
1392 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i])));
1397 static PyMethodDef py_ldb_msg_methods[] = {
1398 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
1399 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
1400 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
1401 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
1405 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
1407 char *attr_name = PyString_AsString(name);
1408 if (value == NULL) {
1409 ldb_msg_remove_attr(self->ptr, attr_name);
1411 struct ldb_message_element *el = PyObject_AsMessageElement(NULL,
1412 value, 0, attr_name);
1415 talloc_steal(self->ptr, el);
1416 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
1417 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
1422 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
1424 return PyLdbMessage_AsMessage(self)->num_elements;
1427 static PyMappingMethods py_ldb_msg_mapping = {
1428 .mp_length = (lenfunc)py_ldb_msg_length,
1429 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
1430 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
1433 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1435 const char *kwnames[] = { "dn", NULL };
1436 struct ldb_message *ret;
1437 PyObject *pydn = NULL;
1438 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", (char **)kwnames, &pydn))
1441 ret = ldb_msg_new(NULL);
1448 if (!PyObject_AsDn(NULL, pydn, NULL, &ret->dn))
1451 return py_talloc_import(&PyLdbMessage, ret);
1454 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
1456 return py_talloc_import(&PyLdbMessage, msg);
1459 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
1461 return PyLdbDn_FromDn(PyLdbMessage_AsMessage(self)->dn);
1464 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
1466 PyLdbMessage_AsMessage(self)->dn = PyLdbDn_AsDn(value);
1470 static PyGetSetDef py_ldb_msg_getset[] = {
1471 { (char *)"dn", (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
1475 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
1477 PyObject *dict = PyDict_New(), *ret;
1478 if (PyDict_Update(dict, (PyObject *)self) != 0)
1480 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
1485 PyTypeObject PyLdbMessage = {
1486 .tp_name = "Message",
1487 .tp_methods = py_ldb_msg_methods,
1488 .tp_getset = py_ldb_msg_getset,
1489 .tp_as_mapping = &py_ldb_msg_mapping,
1490 .tp_basicsize = sizeof(PyLdbMessageObject),
1491 .tp_dealloc = py_talloc_dealloc,
1492 .tp_new = py_ldb_msg_new,
1493 .tp_repr = (reprfunc)py_ldb_msg_repr,
1496 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
1498 return py_talloc_import(&PyLdbTree, tree);
1501 PyTypeObject PyLdbTree = {
1503 .tp_basicsize = sizeof(PyLdbTreeObject),
1504 .tp_dealloc = py_talloc_dealloc,
1508 int py_module_search(struct ldb_module *mod, struct ldb_request *req)
1510 PyObject *py_ldb = mod->private_data;
1511 PyObject *py_result, *py_base, *py_attrs, *py_tree;
1513 py_base = PyLdbDn_FromDn(req->op.search.base);
1515 if (py_base == NULL)
1516 return LDB_ERR_OPERATIONS_ERROR;
1518 py_tree = PyLdbTree_FromTree(req->op.search.tree);
1520 if (py_tree == NULL)
1521 return LDB_ERR_OPERATIONS_ERROR;
1523 if (req->op.search.attrs == NULL) {
1527 for (len = 0; req->op.search.attrs[len]; len++);
1528 py_attrs = PyList_New(len);
1529 for (i = 0; i < len; i++)
1530 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1533 py_result = PyObject_CallMethod(py_ldb, (char *)"search", (char *)"OiOO", py_base, req->op.search.scope, py_tree, py_attrs);
1535 Py_DECREF(py_attrs);
1539 if (py_result == NULL) {
1540 return LDB_ERR_OPERATIONS_ERROR;
1543 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
1544 if (req->op.search.res == NULL) {
1545 return LDB_ERR_OPERATIONS_ERROR;
1548 Py_DECREF(py_result);
1553 int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1555 PyObject *py_ldb = mod->private_data;
1556 PyObject *py_result, *py_msg;
1558 py_msg = PyLdbMessage_FromMessage((struct ldb_message *)req->op.add.message);
1560 if (py_msg == NULL) {
1561 return LDB_ERR_OPERATIONS_ERROR;
1564 py_result = PyObject_CallMethod(py_ldb, (char *)"add", (char *)"O", py_msg);
1568 if (py_result == NULL) {
1569 return LDB_ERR_OPERATIONS_ERROR;
1572 Py_DECREF(py_result);
1577 int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1579 PyObject *py_ldb = mod->private_data;
1580 PyObject *py_result, *py_msg;
1582 py_msg = PyLdbMessage_FromMessage((struct ldb_message *)req->op.mod.message);
1584 if (py_msg == NULL) {
1585 return LDB_ERR_OPERATIONS_ERROR;
1588 py_result = PyObject_CallMethod(py_ldb, (char *)"modify", (char *)"O", py_msg);
1592 if (py_result == NULL) {
1593 return LDB_ERR_OPERATIONS_ERROR;
1596 Py_DECREF(py_result);
1601 int py_module_del(struct ldb_module *mod, struct ldb_request *req)
1603 PyObject *py_ldb = mod->private_data;
1604 PyObject *py_result, *py_dn;
1606 py_dn = PyLdbDn_FromDn(req->op.del.dn);
1609 return LDB_ERR_OPERATIONS_ERROR;
1611 py_result = PyObject_CallMethod(py_ldb, (char *)"delete", (char *)"O", py_dn);
1613 if (py_result == NULL) {
1614 return LDB_ERR_OPERATIONS_ERROR;
1617 Py_DECREF(py_result);
1622 int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
1624 PyObject *py_ldb = mod->private_data;
1625 PyObject *py_result, *py_olddn, *py_newdn;
1627 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
1629 if (py_olddn == NULL)
1630 return LDB_ERR_OPERATIONS_ERROR;
1632 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
1634 if (py_newdn == NULL)
1635 return LDB_ERR_OPERATIONS_ERROR;
1637 py_result = PyObject_CallMethod(py_ldb, (char *)"rename", (char *)"OO", py_olddn, py_newdn);
1639 Py_DECREF(py_olddn);
1640 Py_DECREF(py_newdn);
1642 if (py_result == NULL) {
1643 return LDB_ERR_OPERATIONS_ERROR;
1646 Py_DECREF(py_result);
1651 int py_module_request(struct ldb_module *mod, struct ldb_request *req)
1653 PyObject *py_ldb = mod->private_data;
1654 PyObject *py_result;
1656 py_result = PyObject_CallMethod(py_ldb, (char *)"request", (char *)"");
1658 return LDB_ERR_OPERATIONS_ERROR;
1661 int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
1663 PyObject *py_ldb = mod->private_data;
1664 PyObject *py_result;
1666 py_result = PyObject_CallMethod(py_ldb, (char *)"extended", (char *)"");
1668 return LDB_ERR_OPERATIONS_ERROR;
1671 int py_module_start_transaction(struct ldb_module *mod)
1673 PyObject *py_ldb = mod->private_data;
1674 PyObject *py_result;
1676 py_result = PyObject_CallMethod(py_ldb, (char *)"start_transaction", (char *)"");
1678 if (py_result == NULL) {
1679 return LDB_ERR_OPERATIONS_ERROR;
1682 Py_DECREF(py_result);
1687 int py_module_end_transaction(struct ldb_module *mod)
1689 PyObject *py_ldb = mod->private_data;
1690 PyObject *py_result;
1692 py_result = PyObject_CallMethod(py_ldb, (char *)"end_transaction", (char *)"");
1694 if (py_result == NULL) {
1695 return LDB_ERR_OPERATIONS_ERROR;
1698 Py_DECREF(py_result);
1703 int py_module_del_transaction(struct ldb_module *mod)
1705 PyObject *py_ldb = mod->private_data;
1706 PyObject *py_result;
1708 py_result = PyObject_CallMethod(py_ldb, (char *)"del_transaction", (char *)"");
1710 if (py_result == NULL) {
1711 return LDB_ERR_OPERATIONS_ERROR;
1714 Py_DECREF(py_result);
1719 static int py_module_destructor(struct ldb_module *mod)
1721 Py_DECREF((PyObject *)mod->private_data);
1725 int py_module_init (struct ldb_module *mod)
1727 PyObject *py_class = mod->ops->private_data;
1728 PyObject *py_result, *py_next, *py_ldb;
1730 py_ldb = PyLdb_FromLdbContext(mod->ldb);
1733 return LDB_ERR_OPERATIONS_ERROR;
1735 py_next = PyLdbModule_FromModule(mod->next);
1737 if (py_next == NULL)
1738 return LDB_ERR_OPERATIONS_ERROR;
1740 py_result = PyObject_CallFunction(py_class, (char *)"OO", py_ldb, py_next);
1742 if (py_result == NULL) {
1743 return LDB_ERR_OPERATIONS_ERROR;
1746 mod->private_data = py_result;
1748 talloc_set_destructor(mod, py_module_destructor);
1750 return ldb_next_init(mod);
1753 static PyObject *py_register_module(PyObject *module, PyObject *args)
1756 struct ldb_module_ops *ops;
1759 if (!PyArg_ParseTuple(args, "O", &input))
1762 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
1768 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, (char *)"name")));
1771 ops->private_data = input;
1772 ops->init_context = py_module_init;
1773 ops->search = py_module_search;
1774 ops->add = py_module_add;
1775 ops->modify = py_module_modify;
1776 ops->del = py_module_del;
1777 ops->rename = py_module_rename;
1778 ops->request = py_module_request;
1779 ops->extended = py_module_extended;
1780 ops->start_transaction = py_module_start_transaction;
1781 ops->end_transaction = py_module_end_transaction;
1782 ops->del_transaction = py_module_del_transaction;
1784 ret = ldb_register_module(ops);
1786 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1791 static PyObject *py_timestring(PyObject *module, PyObject *args)
1796 if (!PyArg_ParseTuple(args, "L", &t))
1798 tresult = ldb_timestring(NULL, t);
1799 ret = PyString_FromString(tresult);
1800 talloc_free(tresult);
1804 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
1807 if (!PyArg_ParseTuple(args, "s", &str))
1810 return PyInt_FromLong(ldb_string_to_time(str));
1813 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
1816 if (!PyArg_ParseTuple(args, "s", &name))
1818 return PyBool_FromLong(ldb_valid_attr_name(name));
1821 static PyMethodDef py_ldb_global_methods[] = {
1822 { "register_module", py_register_module, METH_VARARGS,
1823 "S.register_module(module) -> None\n"
1824 "Register a LDB module."},
1825 { "timestring", py_timestring, METH_VARARGS,
1826 "S.timestring(int) -> string\n"
1827 "Generate a LDAP time string from a UNIX timestamp" },
1828 { "string_to_time", py_string_to_time, METH_VARARGS,
1829 "S.string_to_time(string) -> int\n"
1830 "Parse a LDAP time string into a UNIX timestamp." },
1831 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
1832 "S.valid_attr_name(name) -> bool\n"
1833 "Check whether the supplied name is a valid attribute name." },
1834 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
1843 if (PyType_Ready(&PyLdbDn) < 0)
1846 if (PyType_Ready(&PyLdbMessage) < 0)
1849 if (PyType_Ready(&PyLdbMessageElement) < 0)
1852 if (PyType_Ready(&PyLdb) < 0)
1855 if (PyType_Ready(&PyLdbModule) < 0)
1858 if (PyType_Ready(&PyLdbTree) < 0)
1861 m = Py_InitModule3("ldb", py_ldb_global_methods,
1862 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
1866 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
1867 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
1868 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
1869 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
1871 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
1872 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
1873 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
1874 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
1876 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
1878 PyExc_LdbError = PyErr_NewException((char *)"_ldb.LdbError", NULL, NULL);
1879 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
1882 Py_INCREF(&PyLdbDn);
1883 Py_INCREF(&PyLdbModule);
1884 Py_INCREF(&PyLdbMessage);
1885 Py_INCREF(&PyLdbMessageElement);
1886 Py_INCREF(&PyLdbTree);
1888 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
1889 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
1890 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
1891 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
1892 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
1893 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);
1898 /* ldb_message_element */
1899 %rename(MessageElement) ldb_message_element;
1900 %feature("docstring") ldb_message_element "Message element.";
1901 typedef struct ldb_message_element {
1903 PyObject *__set__(void)
1905 return ldb_msg_element_to_set(NULL, $self);
1908 ldb_message_element(PyObject *set_obj, int flags=0, const char *name = NULL)
1910 return PyObject_AsMessageElement(NULL, set_obj, flags, name);
1915 def __getitem__(self, i):
1918 raise KeyError("no such value")
1922 return "MessageElement([%s])" % (",".join(repr(x) for x in self.__set__()))
1924 } ldb_message_element;
1928 typedef struct ldb_message {
1932 ldb_msg(ldb_dn *dn = NULL) {
1933 ret->dn = talloc_reference(ret, dn);
1937 void __setitem__(const char *attr_name, ldb_message_element *val)
1939 struct ldb_message_element *el;
1941 ldb_msg_remove_attr($self, attr_name);
1943 el = talloc($self, struct ldb_message_element);
1944 el->name = talloc_strdup(el, attr_name);
1945 el->num_values = val->num_values;
1946 el->values = talloc_reference(el, val->values);
1948 ldb_msg_add($self, el, val->flags);
1951 void __setitem__(const char *attr_name, PyObject *val)
1955 def get(self, key, default=None):
1958 return self.find_element(key)
1960 def iteritems(self):
1961 for k in self.keys():
1965 return list(self.iteritems())
1968 return "Message(%s)" % repr(dict(self.iteritems()))
1973 typedef struct ldb_context {
1975 def itermodules(self):
1976 m = self.firstmodule
1977 while m is not None:
1982 return list(self.itermodules())