pyldb: Support getting the parent of special DNs without segfaulting.
[Samba/eduardoll.git] / source4 / lib / ldb / pyldb.c
blobab2a1215b849de46f2d19c820554a7bf6bdb969e
1 /*
2 Unix SMB/CIFS implementation.
4 Python interface to ldb.
6 Copyright (C) 2005,2006 Tim Potter <tpot@samba.org>
7 Copyright (C) 2006 Simo Sorce <idra@samba.org>
8 Copyright (C) 2007-2009 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
12 ** under the LGPL
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/>.
28 #include "replace.h"
29 #include "ldb_private.h"
30 #include <Python.h>
31 #include "pyldb.h"
33 /* There's no Py_ssize_t in 2.4, apparently */
34 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
35 typedef int Py_ssize_t;
36 typedef inquiry lenfunc;
37 typedef intargfunc ssizeargfunc;
38 #endif
40 #ifndef Py_RETURN_NONE
41 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
42 #endif
44 static PyObject *PyExc_LdbError;
46 PyAPI_DATA(PyTypeObject) PyLdbMessage;
47 PyAPI_DATA(PyTypeObject) PyLdbModule;
48 PyAPI_DATA(PyTypeObject) PyLdbDn;
49 PyAPI_DATA(PyTypeObject) PyLdb;
50 PyAPI_DATA(PyTypeObject) PyLdbMessageElement;
51 PyAPI_DATA(PyTypeObject) PyLdbTree;
53 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
54 struct ldb_message_element *el,
55 struct ldb_val *val)
57 struct ldb_val new_val;
58 TALLOC_CTX *mem_ctx = talloc_new(NULL);
59 PyObject *ret;
61 new_val = *val;
63 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
65 talloc_free(mem_ctx);
67 return ret;
70 /**
71 * Obtain a ldb DN from a Python object.
73 * @param mem_ctx Memory context
74 * @param object Python object
75 * @param ldb_ctx LDB context
76 * @return Whether or not the conversion succeeded
78 bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object,
79 struct ldb_context *ldb_ctx, struct ldb_dn **dn)
81 struct ldb_dn *odn;
83 if (ldb_ctx != NULL && PyString_Check(object)) {
84 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
85 *dn = odn;
86 return true;
89 if (PyLdbDn_Check(object)) {
90 *dn = PyLdbDn_AsDn(object);
91 return true;
94 PyErr_SetString(PyExc_TypeError, "Expected DN");
95 return false;
98 /**
99 * Create a Python object from a ldb_result.
101 * @param result LDB result to convert
102 * @return Python object with converted result (a list object)
104 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
106 PyObject *ret;
107 int i;
108 if (result == NULL) {
109 Py_RETURN_NONE;
111 ret = PyList_New(result->count);
112 for (i = 0; i < result->count; i++) {
113 PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i])
116 return ret;
120 * Create a LDB Result from a Python object.
121 * If conversion fails, NULL will be returned and a Python exception set.
123 * @param mem_ctx Memory context in which to allocate the LDB Result
124 * @param obj Python object to convert
125 * @return a ldb_result, or NULL if the conversion failed
127 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx,
128 PyObject *obj)
130 struct ldb_result *res;
131 int i;
133 if (obj == Py_None)
134 return NULL;
136 res = talloc_zero(mem_ctx, struct ldb_result);
137 res->count = PyList_Size(obj);
138 res->msgs = talloc_array(res, struct ldb_message *, res->count);
139 for (i = 0; i < res->count; i++) {
140 PyObject *item = PyList_GetItem(obj, i);
141 res->msgs[i] = PyLdbMessage_AsMessage(item);
143 return res;
146 static PyObject *py_ldb_dn_validate(PyLdbDnObject *self)
148 return PyBool_FromLong(ldb_dn_validate(self->dn));
151 static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self)
153 return PyBool_FromLong(ldb_dn_is_valid(self->dn));
156 static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self)
158 return PyBool_FromLong(ldb_dn_is_special(self->dn));
161 static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self)
163 return PyBool_FromLong(ldb_dn_is_null(self->dn));
166 static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self)
168 return PyString_FromString(ldb_dn_get_casefold(self->dn));
171 static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self)
173 return PyString_FromString(ldb_dn_get_linearized(self->dn));
176 static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self)
178 return PyString_FromString(ldb_dn_canonical_string(self->dn, self->dn));
181 static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self)
183 return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn));
186 static PyObject *py_ldb_dn_repr(PyLdbDnObject *self)
188 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn))));
191 static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args)
193 char *name;
195 if (!PyArg_ParseTuple(args, "s", &name))
196 return NULL;
198 return ldb_dn_check_special(self->dn, name)?Py_True:Py_False;
201 static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
203 return ldb_dn_compare(dn1->dn, dn2->dn);
206 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
208 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
209 struct ldb_dn *parent;
211 parent = ldb_dn_get_parent(NULL, dn);
213 if (parent == NULL) {
214 Py_RETURN_NONE;
215 } else {
216 return PyLdbDn_FromDn(parent);
220 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
222 static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
224 PyObject *py_other;
225 struct ldb_dn *dn, *other;
226 if (!PyArg_ParseTuple(args, "O", &py_other))
227 return NULL;
229 dn = PyLdbDn_AsDn((PyObject *)self);
231 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
232 return NULL;
234 return ldb_dn_add_child(dn, other)?Py_True:Py_False;
237 static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
239 PyObject *py_other;
240 struct ldb_dn *other, *dn;
241 if (!PyArg_ParseTuple(args, "O", &py_other))
242 return NULL;
244 dn = PyLdbDn_AsDn((PyObject *)self);
246 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
247 return NULL;
249 return ldb_dn_add_base(dn, other)?Py_True:Py_False;
252 static PyMethodDef py_ldb_dn_methods[] = {
253 { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS,
254 "S.validate() -> bool\n"
255 "Validate DN is correct." },
256 { "is_valid", (PyCFunction)py_ldb_dn_is_valid, METH_NOARGS,
257 "S.is_valid() -> bool\n" },
258 { "is_special", (PyCFunction)py_ldb_dn_is_special, METH_NOARGS,
259 "S.is_special() -> bool\n"
260 "Check whether this is a special LDB DN." },
261 { "is_null", (PyCFunction)py_ldb_dn_is_null, METH_NOARGS,
262 "Check whether this is a null DN." },
263 { "get_casefold", (PyCFunction)py_ldb_dn_get_casefold, METH_NOARGS,
264 NULL },
265 { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS,
266 NULL },
267 { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
268 "S.canonical_str() -> string\n"
269 "Canonical version of this DN (like a posix path)." },
270 { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
271 "S.canonical_ex_str() -> string\n"
272 "Canonical version of this DN (like a posix path, with terminating newline)." },
273 { "check_special", (PyCFunction)py_ldb_dn_is_special, METH_VARARGS,
274 NULL },
275 { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS,
276 "S.parent() -> dn\n"
277 "Get the parent for this DN." },
278 { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS,
279 "S.add_child(dn) -> None\n"
280 "Add a child DN to this DN." },
281 { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
282 "S.add_base(dn) -> None\n"
283 "Add a base DN to this DN." },
284 { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
285 NULL },
286 { NULL }
289 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
291 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
294 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
296 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self),
297 *other;
298 struct ldb_dn *ret = ldb_dn_copy(NULL, dn);
299 if (!PyObject_AsDn(NULL, py_other, NULL, &other))
300 return NULL;
301 ldb_dn_add_child(ret, other);
302 return PyLdbDn_FromDn(ret);
305 static PySequenceMethods py_ldb_dn_seq = {
306 .sq_length = (lenfunc)py_ldb_dn_len,
307 .sq_concat = (binaryfunc)py_ldb_dn_concat,
310 static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
312 struct ldb_dn *ret;
313 char *str;
314 PyObject *py_ldb;
315 struct ldb_context *ldb_ctx;
316 PyLdbDnObject *py_ret;
317 const char * const kwnames[] = { "ldb", "dn", NULL };
319 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os",
320 discard_const_p(char *, kwnames),
321 &py_ldb, &str))
322 return NULL;
324 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
326 ret = ldb_dn_new(ldb_ctx, ldb_ctx, str);
327 /* ldb_dn_new() doesn't accept NULL as memory context, so
328 we do it this way... */
329 talloc_steal(NULL, ret);
331 if (ret == NULL || !ldb_dn_validate(ret)) {
332 PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
333 return NULL;
336 py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0);
337 if (ret == NULL) {
338 PyErr_NoMemory();
339 return NULL;
341 py_ret->dn = ret;
342 return (PyObject *)py_ret;
345 PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
347 PyLdbDnObject *py_ret;
348 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
349 if (py_ret == NULL) {
350 PyErr_NoMemory();
351 return NULL;
353 py_ret->mem_ctx = talloc_new(NULL);
354 py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
355 return (PyObject *)py_ret;
358 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
360 talloc_free(self->mem_ctx);
361 self->ob_type->tp_free(self);
364 PyTypeObject PyLdbDn = {
365 .tp_name = "Dn",
366 .tp_methods = py_ldb_dn_methods,
367 .tp_str = (reprfunc)py_ldb_dn_get_linearized,
368 .tp_repr = (reprfunc)py_ldb_dn_repr,
369 .tp_compare = (cmpfunc)py_ldb_dn_compare,
370 .tp_as_sequence = &py_ldb_dn_seq,
371 .tp_doc = "A LDB distinguished name.",
372 .tp_new = py_ldb_dn_new,
373 .tp_dealloc = (destructor)py_ldb_dn_dealloc,
374 .tp_basicsize = sizeof(PyLdbObject),
375 .tp_flags = Py_TPFLAGS_DEFAULT,
378 /* Debug */
379 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
380 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
382 PyObject *fn = (PyObject *)context;
383 PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap));
386 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
388 PyObject *cb;
390 if (!PyArg_ParseTuple(args, "O", &cb))
391 return NULL;
393 Py_INCREF(cb);
394 /* FIXME: Where do we DECREF cb ? */
395 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
397 Py_RETURN_NONE;
400 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
402 unsigned int perms;
403 if (!PyArg_ParseTuple(args, "I", &perms))
404 return NULL;
406 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
408 Py_RETURN_NONE;
411 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
413 char *modules_dir;
414 if (!PyArg_ParseTuple(args, "s", &modules_dir))
415 return NULL;
417 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
419 Py_RETURN_NONE;
422 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
424 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
425 Py_RETURN_NONE;
428 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
430 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
431 Py_RETURN_NONE;
434 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
436 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
437 Py_RETURN_NONE;
440 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
442 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
443 Py_RETURN_NONE;
446 static PyObject *py_ldb_repr(PyLdbObject *self)
448 return PyString_FromFormat("<ldb connection>");
451 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
453 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
454 if (dn == NULL)
455 Py_RETURN_NONE;
456 return PyLdbDn_FromDn(dn);
460 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
462 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
463 if (dn == NULL)
464 Py_RETURN_NONE;
465 return PyLdbDn_FromDn(dn);
468 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
470 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
471 if (dn == NULL)
472 Py_RETURN_NONE;
473 return PyLdbDn_FromDn(dn);
476 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
478 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
479 if (dn == NULL)
480 Py_RETURN_NONE;
481 return PyLdbDn_FromDn(dn);
484 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list,
485 const char *paramname)
487 const char **ret;
488 int i;
489 if (!PyList_Check(list)) {
490 PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
491 return NULL;
493 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
494 for (i = 0; i < PyList_Size(list); i++) {
495 PyObject *item = PyList_GetItem(list, i);
496 if (!PyString_Check(item)) {
497 PyErr_Format(PyExc_TypeError, "%s should be strings", paramname);
498 return NULL;
500 ret[i] = PyString_AsString(item);
502 ret[i] = NULL;
503 return ret;
506 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
508 const char * const kwnames[] = { "url", "flags", "options", NULL };
509 char *url = NULL;
510 PyObject *py_options = Py_None;
511 const char **options;
512 int flags = 0;
513 int ret;
514 struct ldb_context *ldb;
516 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__",
517 discard_const_p(char *, kwnames),
518 &url, &flags, &py_options))
519 return -1;
521 ldb = PyLdb_AsLdbContext(self);
523 if (py_options == Py_None) {
524 options = NULL;
525 } else {
526 options = PyList_AsStringList(ldb, py_options, "options");
527 if (options == NULL)
528 return -1;
531 if (url != NULL) {
532 ret = ldb_connect(ldb, url, flags, options);
533 if (ret != LDB_SUCCESS) {
534 PyErr_SetLdbError(PyExc_LdbError, ret, ldb);
535 return -1;
539 talloc_free(options);
540 return 0;
543 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
545 PyLdbObject *ret;
546 struct ldb_context *ldb;
547 ldb = ldb_init(NULL, NULL);
548 if (ldb == NULL) {
549 PyErr_NoMemory();
550 return NULL;
553 ret = (PyLdbObject *)type->tp_alloc(type, 0);
554 if (ret == NULL) {
555 PyErr_NoMemory();
556 return NULL;
558 ret->ldb_ctx = ldb;
559 return (PyObject *)ret;
562 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
564 char *url;
565 int flags = 0;
566 PyObject *py_options = Py_None;
567 int ret;
568 const char **options;
569 const char * const kwnames[] = { "url", "flags", "options", NULL };
571 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO",
572 discard_const_p(char *, kwnames),
573 &url, &flags, &py_options))
574 return NULL;
576 if (py_options == Py_None) {
577 options = NULL;
578 } else {
579 options = PyList_AsStringList(NULL, py_options, "options");
580 if (options == NULL)
581 return NULL;
584 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
585 talloc_free(options);
587 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
589 Py_RETURN_NONE;
592 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
594 PyObject *py_msg;
595 int ret;
596 if (!PyArg_ParseTuple(args, "O", &py_msg))
597 return NULL;
599 if (!PyLdbMessage_Check(py_msg)) {
600 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
601 return NULL;
604 ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
605 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
607 Py_RETURN_NONE;
610 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
612 PyObject *py_msg;
613 int ret;
614 Py_ssize_t dict_pos, msg_pos;
615 struct ldb_message_element *msgel;
616 struct ldb_message *msg;
617 PyObject *key, *value;
619 if (!PyArg_ParseTuple(args, "O", &py_msg))
620 return NULL;
622 if (PyDict_Check(py_msg)) {
623 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
624 msg = ldb_msg_new(NULL);
625 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
626 msg_pos = dict_pos = 0;
627 if (dn_value) {
628 if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
629 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
630 return NULL;
632 if (msg->dn == NULL) {
633 PyErr_SetString(PyExc_TypeError, "dn set but not found");
634 return NULL;
638 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
639 char *key_str = PyString_AsString(key);
640 if (strcmp(key_str, "dn") != 0) {
641 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
642 if (msgel == NULL) {
643 PyErr_SetString(PyExc_TypeError, "unable to import element");
644 return NULL;
646 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
647 msg_pos++;
651 if (msg->dn == NULL) {
652 PyErr_SetString(PyExc_TypeError, "no dn set");
653 return NULL;
656 msg->num_elements = msg_pos;
657 } else {
658 msg = PyLdbMessage_AsMessage(py_msg);
661 ret = ldb_add(PyLdb_AsLdbContext(self), msg);
662 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
664 Py_RETURN_NONE;
667 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
669 PyObject *py_dn;
670 struct ldb_dn *dn;
671 int ret;
672 struct ldb_context *ldb;
673 if (!PyArg_ParseTuple(args, "O", &py_dn))
674 return NULL;
676 ldb = PyLdb_AsLdbContext(self);
678 if (!PyObject_AsDn(NULL, py_dn, ldb, &dn))
679 return NULL;
681 ret = ldb_delete(ldb, dn);
682 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
684 Py_RETURN_NONE;
687 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
689 PyObject *py_dn1, *py_dn2;
690 struct ldb_dn *dn1, *dn2;
691 int ret;
692 struct ldb_context *ldb;
693 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
694 return NULL;
696 ldb = PyLdb_AsLdbContext(self);
697 if (!PyObject_AsDn(NULL, py_dn1, ldb, &dn1))
698 return NULL;
700 if (!PyObject_AsDn(NULL, py_dn2, ldb, &dn2))
701 return NULL;
703 ret = ldb_rename(ldb, dn1, dn2);
704 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
706 Py_RETURN_NONE;
709 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
711 char *name;
712 if (!PyArg_ParseTuple(args, "s", &name))
713 return NULL;
715 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
717 Py_RETURN_NONE;
720 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
722 char *attribute, *syntax;
723 unsigned int flags;
724 int ret;
725 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
726 return NULL;
728 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
730 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
732 Py_RETURN_NONE;
735 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
737 if (ldif == NULL) {
738 Py_RETURN_NONE;
739 } else {
740 /* We don't want this attached to the 'ldb' any more */
741 talloc_steal(NULL, ldif);
742 return Py_BuildValue(discard_const_p(char, "(iO)"),
743 ldif->changetype,
744 PyLdbMessage_FromMessage(ldif->msg));
749 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
751 PyObject *list;
752 struct ldb_ldif *ldif;
753 const char *s;
755 if (!PyArg_ParseTuple(args, "s", &s))
756 return NULL;
758 list = PyList_New(0);
759 while ((ldif = ldb_ldif_read_string(self->ldb_ctx, &s)) != NULL) {
760 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
762 return PyObject_GetIter(list);
765 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
767 const struct ldb_schema_attribute *a;
768 struct ldb_val old_val;
769 struct ldb_val new_val;
770 TALLOC_CTX *mem_ctx;
771 PyObject *ret;
772 char *element_name;
773 PyObject *val;
775 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
776 return NULL;
778 mem_ctx = talloc_new(NULL);
780 old_val.data = (uint8_t *)PyString_AsString(val);
781 old_val.length = PyString_Size(val);
783 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
785 if (a == NULL) {
786 Py_RETURN_NONE;
789 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
790 talloc_free(mem_ctx);
791 Py_RETURN_NONE;
794 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
796 talloc_free(mem_ctx);
798 return ret;
801 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
803 PyObject *py_base = Py_None;
804 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
805 char *expr = NULL;
806 PyObject *py_attrs = Py_None;
807 PyObject *py_controls = Py_None;
808 const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
809 int ret;
810 struct ldb_result *res;
811 struct ldb_request *req;
812 const char **attrs;
813 struct ldb_context *ldb_ctx;
814 struct ldb_control **parsed_controls;
815 struct ldb_dn *base;
817 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
818 discard_const_p(char *, kwnames),
819 &py_base, &scope, &expr, &py_attrs, &py_controls))
820 return NULL;
822 ldb_ctx = PyLdb_AsLdbContext(self);
824 if (py_attrs == Py_None) {
825 attrs = NULL;
826 } else {
827 attrs = PyList_AsStringList(ldb_ctx, py_attrs, "attrs");
828 if (attrs == NULL)
829 return NULL;
832 if (py_base == Py_None) {
833 base = ldb_get_default_basedn(ldb_ctx);
834 } else {
835 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base))
836 return NULL;
839 if (py_controls == Py_None) {
840 parsed_controls = NULL;
841 } else {
842 const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
843 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
844 talloc_free(controls);
847 res = talloc_zero(ldb_ctx, struct ldb_result);
848 if (res == NULL) {
849 PyErr_NoMemory();
850 return NULL;
853 ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
854 base,
855 scope,
856 expr,
857 attrs,
858 parsed_controls,
859 res,
860 ldb_search_default_callback,
861 NULL);
863 if (ret != LDB_SUCCESS) {
864 talloc_free(res);
865 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
866 return NULL;
869 ret = ldb_request(ldb_ctx, req);
871 if (ret == LDB_SUCCESS) {
872 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
875 talloc_free(req);
877 if (ret != LDB_SUCCESS) {
878 talloc_free(res);
879 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
880 return NULL;
883 return PyLdbResult_FromResult(res);
886 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
888 char *name;
889 void *data;
891 if (!PyArg_ParseTuple(args, "s", &name))
892 return NULL;
894 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
896 if (data == NULL)
897 Py_RETURN_NONE;
899 /* FIXME: More interpretation */
901 return Py_True;
904 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
906 char *name;
907 PyObject *data;
909 if (!PyArg_ParseTuple(args, "sO", &name, &data))
910 return NULL;
912 /* FIXME: More interpretation */
914 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
916 Py_RETURN_NONE;
919 static PyObject *py_ldb_modules(PyLdbObject *self)
921 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
922 PyObject *ret = PyList_New(0);
923 struct ldb_module *mod;
925 for (mod = ldb->modules; mod; mod = mod->next) {
926 PyList_Append(ret, PyLdbModule_FromModule(mod));
929 return ret;
932 static PyMethodDef py_ldb_methods[] = {
933 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
934 "S.set_debug(callback) -> None\n"
935 "Set callback for LDB debug messages.\n"
936 "The callback should accept a debug level and debug text." },
937 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
938 "S.set_create_perms(mode) -> None\n"
939 "Set mode to use when creating new LDB files." },
940 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
941 "S.set_modules_dir(path) -> None\n"
942 "Set path LDB should search for modules" },
943 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
944 "S.transaction_start() -> None\n"
945 "Start a new transaction." },
946 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
947 "S.transaction_commit() -> None\n"
948 "commit a new transaction." },
949 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
950 "S.transaction_cancel() -> None\n"
951 "cancel a new transaction." },
952 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
953 NULL },
954 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
955 NULL },
956 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
957 NULL },
958 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
959 NULL },
960 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
961 NULL },
962 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
963 "S.connect(url, flags=0, options=None) -> None\n"
964 "Connect to a LDB URL." },
965 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
966 "S.modify(message) -> None\n"
967 "Modify an entry." },
968 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
969 "S.add(message) -> None\n"
970 "Add an entry." },
971 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
972 "S.delete(dn) -> None\n"
973 "Remove an entry." },
974 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
975 "S.rename(old_dn, new_dn) -> None\n"
976 "Rename an entry." },
977 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
978 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
979 "Search in a database.\n"
980 "\n"
981 ":param base: Optional base DN to search\n"
982 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
983 ":param expression: Optional search expression\n"
984 ":param attrs: Attributes to return (defaults to all)\n"
985 ":param controls: Optional list of controls\n"
986 ":return: Iterator over Message objects\n"
988 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
989 NULL },
990 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
991 NULL },
992 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
993 NULL },
994 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
995 "S.parse_ldif(ldif) -> iter(messages)\n"
996 "Parse a string formatted using LDIF." },
997 { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
998 "S.get_opaque(name) -> value\n"
999 "Get an opaque value set on this LDB connection. \n"
1000 ":note: The returned value may not be useful in Python."
1002 { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
1003 "S.set_opaque(name, value) -> None\n"
1004 "Set an opaque value on this LDB connection. \n"
1005 ":note: Passing incorrect values may cause crashes." },
1006 { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
1007 "S.modules() -> list\n"
1008 "Return the list of modules on this LDB connection " },
1009 { NULL },
1012 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
1014 PyLdbModuleObject *ret;
1016 ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
1017 if (ret == NULL) {
1018 PyErr_NoMemory();
1019 return NULL;
1021 ret->mem_ctx = talloc_new(NULL);
1022 ret->mod = talloc_reference(ret->mem_ctx, mod);
1023 return (PyObject *)ret;
1026 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
1028 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
1031 static PyGetSetDef py_ldb_getset[] = {
1032 { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL },
1033 { NULL }
1036 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
1038 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1039 struct ldb_dn *dn;
1040 struct ldb_result *result;
1041 int ret;
1042 int count;
1044 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
1045 return -1;
1047 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
1048 if (ret != LDB_SUCCESS) {
1049 PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
1050 return -1;
1053 count = result->count;
1055 talloc_free(result);
1057 return count;
1060 static PySequenceMethods py_ldb_seq = {
1061 .sq_contains = (objobjproc)py_ldb_contains,
1064 PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
1066 PyLdbObject *ret;
1068 ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1069 if (ret == NULL) {
1070 PyErr_NoMemory();
1071 return NULL;
1073 ret->mem_ctx = talloc_new(NULL);
1074 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1075 return (PyObject *)ret;
1078 static void py_ldb_dealloc(PyLdbObject *self)
1080 talloc_free(self->mem_ctx);
1081 self->ob_type->tp_free(self);
1084 PyTypeObject PyLdb = {
1085 .tp_name = "Ldb",
1086 .tp_methods = py_ldb_methods,
1087 .tp_repr = (reprfunc)py_ldb_repr,
1088 .tp_new = py_ldb_new,
1089 .tp_init = (initproc)py_ldb_init,
1090 .tp_dealloc = (destructor)py_ldb_dealloc,
1091 .tp_getset = py_ldb_getset,
1092 .tp_getattro = PyObject_GenericGetAttr,
1093 .tp_basicsize = sizeof(PyLdbObject),
1094 .tp_doc = "Connection to a LDB database.",
1095 .tp_as_sequence = &py_ldb_seq,
1096 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1099 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
1101 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1104 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
1106 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1109 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1111 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1112 Py_RETURN_NONE;
1115 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1117 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1118 Py_RETURN_NONE;
1121 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1123 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1124 Py_RETURN_NONE;
1127 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1129 PyObject *py_base, *py_tree, *py_attrs;
1130 int ret, scope;
1131 struct ldb_request *req;
1132 const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1133 struct ldb_module *mod;
1135 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
1136 discard_const_p(char *, kwnames),
1137 &py_base, &scope, &py_tree, &py_attrs))
1138 return NULL;
1140 mod = self->mod;
1142 ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base),
1143 scope, NULL /* expr */, py_attrs == Py_None?NULL:PyList_AsStringList(req, py_attrs, "attrs"),
1144 NULL /* controls */, NULL, NULL, NULL);
1145 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1147 ret = mod->ops->search(mod, req);
1148 talloc_free(req);
1150 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1152 return PyLdbResult_FromResult(req->op.search.res);
1156 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1158 struct ldb_request *req;
1159 PyObject *py_message;
1160 int ret;
1161 struct ldb_module *mod;
1163 if (!PyArg_ParseTuple(args, "O", &py_message))
1164 return NULL;
1166 req = talloc_zero(NULL, struct ldb_request);
1167 req->operation = LDB_ADD;
1168 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1170 mod = PyLdbModule_AsModule(self);
1171 ret = mod->ops->add(mod, req);
1173 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1175 Py_RETURN_NONE;
1178 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
1180 int ret;
1181 struct ldb_request *req;
1182 PyObject *py_message;
1183 struct ldb_module *mod;
1185 if (!PyArg_ParseTuple(args, "O", &py_message))
1186 return NULL;
1188 req = talloc_zero(NULL, struct ldb_request);
1189 req->operation = LDB_MODIFY;
1190 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1192 mod = PyLdbModule_AsModule(self);
1193 ret = mod->ops->modify(mod, req);
1195 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1197 Py_RETURN_NONE;
1200 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
1202 int ret;
1203 struct ldb_request *req;
1204 PyObject *py_dn;
1206 if (!PyArg_ParseTuple(args, "O", &py_dn))
1207 return NULL;
1209 req = talloc_zero(NULL, struct ldb_request);
1210 req->operation = LDB_DELETE;
1211 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1213 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1215 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1217 Py_RETURN_NONE;
1220 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1222 int ret;
1223 struct ldb_request *req;
1224 PyObject *py_dn1, *py_dn2;
1226 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1227 return NULL;
1229 req = talloc_zero(NULL, struct ldb_request);
1231 req->operation = LDB_RENAME;
1232 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1233 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1235 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1237 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1239 Py_RETURN_NONE;
1242 static PyMethodDef py_ldb_module_methods[] = {
1243 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1244 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1245 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1246 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1247 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1248 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1249 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1250 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1251 { NULL },
1254 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
1256 talloc_free(self->mem_ctx);
1257 self->ob_type->tp_free(self);
1260 PyTypeObject PyLdbModule = {
1261 .tp_name = "LdbModule",
1262 .tp_methods = py_ldb_module_methods,
1263 .tp_repr = (reprfunc)py_ldb_module_repr,
1264 .tp_str = (reprfunc)py_ldb_module_str,
1265 .tp_basicsize = sizeof(PyLdbModuleObject),
1266 .tp_dealloc = (destructor)py_ldb_module_dealloc,
1267 .tp_flags = Py_TPFLAGS_DEFAULT,
1272 * Create a ldb_message_element from a Python object.
1274 * This will accept any sequence objects that contains strings, or
1275 * a string object.
1277 * A reference to set_obj will be borrowed.
1279 * @param mem_ctx Memory context
1280 * @param set_obj Python object to convert
1281 * @param flags ldb_message_element flags to set
1282 * @param attr_name Name of the attribute
1283 * @return New ldb_message_element, allocated as child of mem_ctx
1285 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1286 PyObject *set_obj, int flags,
1287 const char *attr_name)
1289 struct ldb_message_element *me;
1291 if (PyLdbMessageElement_Check(set_obj))
1292 return PyLdbMessageElement_AsMessageElement(set_obj);
1294 me = talloc(mem_ctx, struct ldb_message_element);
1296 me->name = attr_name;
1297 me->flags = flags;
1298 if (PyString_Check(set_obj)) {
1299 me->num_values = 1;
1300 me->values = talloc_array(me, struct ldb_val, me->num_values);
1301 me->values[0].length = PyString_Size(set_obj);
1302 me->values[0].data = (uint8_t *)PyString_AsString(set_obj);
1303 } else if (PySequence_Check(set_obj)) {
1304 int i;
1305 me->num_values = PySequence_Size(set_obj);
1306 me->values = talloc_array(me, struct ldb_val, me->num_values);
1307 for (i = 0; i < me->num_values; i++) {
1308 PyObject *obj = PySequence_GetItem(set_obj, i);
1310 me->values[i].length = PyString_Size(obj);
1311 me->values[i].data = (uint8_t *)PyString_AsString(obj);
1313 } else {
1314 talloc_free(me);
1315 me = NULL;
1318 return me;
1322 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
1323 struct ldb_message_element *me)
1325 int i;
1326 PyObject *result;
1328 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1329 result = PyList_New(me->num_values);
1331 for (i = 0; i < me->num_values; i++) {
1332 PyList_SetItem(result, i,
1333 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1336 return result;
1339 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1341 int i;
1342 if (!PyArg_ParseTuple(args, "i", &i))
1343 return NULL;
1344 if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1345 Py_RETURN_NONE;
1347 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1348 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1351 static PyMethodDef py_ldb_msg_element_methods[] = {
1352 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1353 { NULL },
1356 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1358 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1361 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1363 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1364 if (idx < 0 || idx >= el->num_values) {
1365 PyErr_SetString(PyExc_IndexError, "Out of range");
1366 return NULL;
1368 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1371 static PySequenceMethods py_ldb_msg_element_seq = {
1372 .sq_length = (lenfunc)py_ldb_msg_element_len,
1373 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1376 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1378 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1379 PyLdbMessageElement_AsMessageElement(other));
1382 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1384 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1387 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
1389 PyLdbMessageElementObject *ret;
1390 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1391 if (ret == NULL) {
1392 PyErr_NoMemory();
1393 return NULL;
1395 ret->mem_ctx = talloc_new(NULL);
1396 if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1397 PyErr_NoMemory();
1398 return NULL;
1400 ret->el = el;
1401 return (PyObject *)ret;
1404 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1406 PyObject *py_elements = NULL;
1407 struct ldb_message_element *el;
1408 int flags = 0;
1409 char *name = NULL;
1410 const char * const kwnames[] = { "elements", "flags", "name", NULL };
1411 PyLdbMessageElementObject *ret;
1413 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois",
1414 discard_const_p(char *, kwnames),
1415 &py_elements, &flags, &name))
1416 return NULL;
1418 el = talloc_zero(NULL, struct ldb_message_element);
1420 if (py_elements != NULL) {
1421 int i;
1422 if (PyString_Check(py_elements)) {
1423 el->num_values = 1;
1424 el->values = talloc_array(el, struct ldb_val, 1);
1425 el->values[0].data = (uint8_t *)PyString_AsString(py_elements);
1426 el->values[0].length = PyString_Size(py_elements);
1427 } else if (PySequence_Check(py_elements)) {
1428 el->num_values = PySequence_Size(py_elements);
1429 el->values = talloc_array(el, struct ldb_val, el->num_values);
1430 for (i = 0; i < el->num_values; i++) {
1431 PyObject *item = PySequence_GetItem(py_elements, i);
1432 el->values[i].data = (uint8_t *)PyString_AsString(item);
1433 el->values[i].length = PyString_Size(item);
1435 } else {
1436 PyErr_SetString(PyExc_TypeError,
1437 "Expected string or list");
1438 talloc_free(el);
1439 return NULL;
1443 el->flags = flags;
1444 el->name = talloc_strdup(el, name);
1446 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1447 if (ret == NULL) {
1448 PyErr_NoMemory();
1449 talloc_free(el);
1450 return NULL;
1453 ret->mem_ctx = talloc_new(NULL);
1454 ret->el = talloc_reference(ret->mem_ctx, el);
1455 return (PyObject *)ret;
1458 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1460 char *element_str = NULL;
1461 int i;
1462 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1463 PyObject *ret;
1465 for (i = 0; i < el->num_values; i++) {
1466 PyObject *o = py_ldb_msg_element_find(self, i);
1467 if (element_str == NULL)
1468 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1469 else
1470 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1473 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1475 talloc_free(element_str);
1477 return ret;
1480 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
1482 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1484 if (el->num_values == 1)
1485 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1486 else
1487 Py_RETURN_NONE;
1490 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
1492 talloc_free(self->mem_ctx);
1493 self->ob_type->tp_free(self);
1496 PyTypeObject PyLdbMessageElement = {
1497 .tp_name = "MessageElement",
1498 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1499 .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1500 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1501 .tp_str = (reprfunc)py_ldb_msg_element_str,
1502 .tp_methods = py_ldb_msg_element_methods,
1503 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1504 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1505 .tp_as_sequence = &py_ldb_msg_element_seq,
1506 .tp_new = py_ldb_msg_element_new,
1507 .tp_flags = Py_TPFLAGS_DEFAULT,
1510 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
1512 char *name;
1513 if (!PyArg_ParseTuple(args, "s", &name))
1514 return NULL;
1516 ldb_msg_remove_attr(self->msg, name);
1518 Py_RETURN_NONE;
1521 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
1523 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1524 int i, j = 0;
1525 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1526 if (msg->dn != NULL) {
1527 PyList_SetItem(obj, j, PyString_FromString("dn"));
1528 j++;
1530 for (i = 0; i < msg->num_elements; i++) {
1531 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1532 j++;
1534 return obj;
1537 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
1539 struct ldb_message_element *el;
1540 char *name = PyString_AsString(py_name);
1541 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1542 if (!strcmp(name, "dn"))
1543 return PyLdbDn_FromDn(msg->dn);
1544 el = ldb_msg_find_element(msg, name);
1545 if (el == NULL) {
1546 return NULL;
1548 return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg);
1551 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
1553 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
1554 if (ret == NULL) {
1555 PyErr_SetString(PyExc_KeyError, "No such element");
1556 return NULL;
1558 return ret;
1561 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
1563 PyObject *name, *ret;
1564 if (!PyArg_ParseTuple(args, "O", &name))
1565 return NULL;
1567 ret = py_ldb_msg_getitem_helper(self, name);
1568 if (ret == NULL)
1569 Py_RETURN_NONE;
1570 return ret;
1573 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
1575 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1576 int i, j;
1577 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
1578 j = 0;
1579 if (msg->dn != NULL) {
1580 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
1581 j++;
1583 for (i = 0; i < msg->num_elements; i++, j++) {
1584 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], self->msg)));
1586 return l;
1589 static PyMethodDef py_ldb_msg_methods[] = {
1590 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
1591 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
1592 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
1593 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
1594 { NULL },
1597 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
1599 PyObject *list, *iter;
1601 list = py_ldb_msg_keys(self);
1602 iter = PyObject_GetIter(list);
1603 Py_DECREF(list);
1604 return iter;
1607 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
1609 char *attr_name = PyString_AsString(name);
1610 if (value == NULL) {
1611 ldb_msg_remove_attr(self->msg, attr_name);
1612 } else {
1613 struct ldb_message_element *el = PyObject_AsMessageElement(NULL,
1614 value, 0, attr_name);
1615 if (el == NULL)
1616 return -1;
1617 talloc_steal(self->msg, el);
1618 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
1619 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
1621 return 0;
1624 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
1626 return PyLdbMessage_AsMessage(self)->num_elements;
1629 static PyMappingMethods py_ldb_msg_mapping = {
1630 .mp_length = (lenfunc)py_ldb_msg_length,
1631 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
1632 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
1635 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1637 const char * const kwnames[] = { "dn", NULL };
1638 struct ldb_message *ret;
1639 PyObject *pydn = NULL;
1640 PyLdbMessageObject *py_ret;
1642 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
1643 discard_const_p(char *, kwnames),
1644 &pydn))
1645 return NULL;
1647 ret = ldb_msg_new(NULL);
1648 if (ret == NULL) {
1649 PyErr_NoMemory();
1650 return NULL;
1653 if (pydn != NULL)
1654 if (!PyObject_AsDn(NULL, pydn, NULL, &ret->dn))
1655 return NULL;
1657 py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
1658 if (py_ret == NULL) {
1659 PyErr_NoMemory();
1660 return NULL;
1663 py_ret->mem_ctx = talloc_new(NULL);
1664 py_ret->msg = talloc_reference(py_ret->mem_ctx, ret);
1665 return (PyObject *)py_ret;
1668 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
1670 PyLdbMessageObject *ret;
1672 ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
1673 if (ret == NULL) {
1674 PyErr_NoMemory();
1675 return NULL;
1677 ret->mem_ctx = talloc_new(NULL);
1678 ret->msg = talloc_reference(ret->mem_ctx, msg);
1679 return (PyObject *)ret;
1682 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
1684 return PyLdbDn_FromDn(PyLdbMessage_AsMessage(self)->dn);
1687 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
1689 PyLdbMessage_AsMessage(self)->dn = PyLdbDn_AsDn(value);
1690 return 0;
1693 static PyGetSetDef py_ldb_msg_getset[] = {
1694 { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
1695 { NULL }
1698 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
1700 PyObject *dict = PyDict_New(), *ret;
1701 if (PyDict_Update(dict, (PyObject *)self) != 0)
1702 return NULL;
1703 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
1704 Py_DECREF(dict);
1705 return ret;
1708 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
1710 talloc_free(self->mem_ctx);
1711 self->ob_type->tp_free(self);
1714 PyTypeObject PyLdbMessage = {
1715 .tp_name = "Message",
1716 .tp_methods = py_ldb_msg_methods,
1717 .tp_getset = py_ldb_msg_getset,
1718 .tp_as_mapping = &py_ldb_msg_mapping,
1719 .tp_basicsize = sizeof(PyLdbMessageObject),
1720 .tp_dealloc = (destructor)py_ldb_msg_dealloc,
1721 .tp_new = py_ldb_msg_new,
1722 .tp_repr = (reprfunc)py_ldb_msg_repr,
1723 .tp_flags = Py_TPFLAGS_DEFAULT,
1724 .tp_iter = (getiterfunc)py_ldb_msg_iter,
1727 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
1729 PyLdbTreeObject *ret;
1731 ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
1732 if (ret == NULL) {
1733 PyErr_NoMemory();
1734 return NULL;
1737 ret->mem_ctx = talloc_new(NULL);
1738 ret->tree = talloc_reference(ret->mem_ctx, tree);
1739 return (PyObject *)ret;
1742 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
1744 talloc_free(self->mem_ctx);
1745 self->ob_type->tp_free(self);
1748 PyTypeObject PyLdbTree = {
1749 .tp_name = "Tree",
1750 .tp_basicsize = sizeof(PyLdbTreeObject),
1751 .tp_dealloc = (destructor)py_ldb_tree_dealloc,
1752 .tp_flags = Py_TPFLAGS_DEFAULT,
1755 /* Ldb_module */
1756 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
1758 PyObject *py_ldb = (PyObject *)mod->private_data;
1759 PyObject *py_result, *py_base, *py_attrs, *py_tree;
1761 py_base = PyLdbDn_FromDn(req->op.search.base);
1763 if (py_base == NULL)
1764 return LDB_ERR_OPERATIONS_ERROR;
1766 py_tree = PyLdbTree_FromTree(req->op.search.tree);
1768 if (py_tree == NULL)
1769 return LDB_ERR_OPERATIONS_ERROR;
1771 if (req->op.search.attrs == NULL) {
1772 py_attrs = Py_None;
1773 } else {
1774 int i, len;
1775 for (len = 0; req->op.search.attrs[len]; len++);
1776 py_attrs = PyList_New(len);
1777 for (i = 0; i < len; i++)
1778 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1781 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
1782 discard_const_p(char, "OiOO"),
1783 py_base, req->op.search.scope, py_tree, py_attrs);
1785 Py_DECREF(py_attrs);
1786 Py_DECREF(py_tree);
1787 Py_DECREF(py_base);
1789 if (py_result == NULL) {
1790 return LDB_ERR_PYTHON_EXCEPTION;
1793 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
1794 if (req->op.search.res == NULL) {
1795 return LDB_ERR_PYTHON_EXCEPTION;
1798 Py_DECREF(py_result);
1800 return LDB_SUCCESS;
1803 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1805 PyObject *py_ldb = (PyObject *)mod->private_data;
1806 PyObject *py_result, *py_msg;
1808 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
1810 if (py_msg == NULL) {
1811 return LDB_ERR_OPERATIONS_ERROR;
1814 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
1815 discard_const_p(char, "O"),
1816 py_msg);
1818 Py_DECREF(py_msg);
1820 if (py_result == NULL) {
1821 return LDB_ERR_PYTHON_EXCEPTION;
1824 Py_DECREF(py_result);
1826 return LDB_SUCCESS;
1829 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1831 PyObject *py_ldb = (PyObject *)mod->private_data;
1832 PyObject *py_result, *py_msg;
1834 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
1836 if (py_msg == NULL) {
1837 return LDB_ERR_OPERATIONS_ERROR;
1840 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
1841 discard_const_p(char, "O"),
1842 py_msg);
1844 Py_DECREF(py_msg);
1846 if (py_result == NULL) {
1847 return LDB_ERR_PYTHON_EXCEPTION;
1850 Py_DECREF(py_result);
1852 return LDB_SUCCESS;
1855 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
1857 PyObject *py_ldb = (PyObject *)mod->private_data;
1858 PyObject *py_result, *py_dn;
1860 py_dn = PyLdbDn_FromDn(req->op.del.dn);
1862 if (py_dn == NULL)
1863 return LDB_ERR_OPERATIONS_ERROR;
1865 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
1866 discard_const_p(char, "O"),
1867 py_dn);
1869 if (py_result == NULL) {
1870 return LDB_ERR_PYTHON_EXCEPTION;
1873 Py_DECREF(py_result);
1875 return LDB_SUCCESS;
1878 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
1880 PyObject *py_ldb = (PyObject *)mod->private_data;
1881 PyObject *py_result, *py_olddn, *py_newdn;
1883 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
1885 if (py_olddn == NULL)
1886 return LDB_ERR_OPERATIONS_ERROR;
1888 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
1890 if (py_newdn == NULL)
1891 return LDB_ERR_OPERATIONS_ERROR;
1893 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
1894 discard_const_p(char, "OO"),
1895 py_olddn, py_newdn);
1897 Py_DECREF(py_olddn);
1898 Py_DECREF(py_newdn);
1900 if (py_result == NULL) {
1901 return LDB_ERR_PYTHON_EXCEPTION;
1904 Py_DECREF(py_result);
1906 return LDB_SUCCESS;
1909 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
1911 PyObject *py_ldb = (PyObject *)mod->private_data;
1912 PyObject *py_result;
1914 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
1915 discard_const_p(char, ""));
1917 return LDB_ERR_OPERATIONS_ERROR;
1920 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
1922 PyObject *py_ldb = (PyObject *)mod->private_data;
1923 PyObject *py_result;
1925 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
1926 discard_const_p(char, ""));
1928 return LDB_ERR_OPERATIONS_ERROR;
1931 static int py_module_start_transaction(struct ldb_module *mod)
1933 PyObject *py_ldb = (PyObject *)mod->private_data;
1934 PyObject *py_result;
1936 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
1937 discard_const_p(char, ""));
1939 if (py_result == NULL) {
1940 return LDB_ERR_PYTHON_EXCEPTION;
1943 Py_DECREF(py_result);
1945 return LDB_SUCCESS;
1948 static int py_module_end_transaction(struct ldb_module *mod)
1950 PyObject *py_ldb = (PyObject *)mod->private_data;
1951 PyObject *py_result;
1953 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
1954 discard_const_p(char, ""));
1956 if (py_result == NULL) {
1957 return LDB_ERR_PYTHON_EXCEPTION;
1960 Py_DECREF(py_result);
1962 return LDB_SUCCESS;
1965 static int py_module_del_transaction(struct ldb_module *mod)
1967 PyObject *py_ldb = (PyObject *)mod->private_data;
1968 PyObject *py_result;
1970 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
1971 discard_const_p(char, ""));
1973 if (py_result == NULL) {
1974 return LDB_ERR_PYTHON_EXCEPTION;
1977 Py_DECREF(py_result);
1979 return LDB_SUCCESS;
1982 static int py_module_destructor(struct ldb_module *mod)
1984 Py_DECREF((PyObject *)mod->private_data);
1985 return 0;
1988 static int py_module_init(struct ldb_module *mod)
1990 PyObject *py_class = (PyObject *)mod->ops->private_data;
1991 PyObject *py_result, *py_next, *py_ldb;
1993 py_ldb = PyLdb_FromLdbContext(mod->ldb);
1995 if (py_ldb == NULL)
1996 return LDB_ERR_OPERATIONS_ERROR;
1998 py_next = PyLdbModule_FromModule(mod->next);
2000 if (py_next == NULL)
2001 return LDB_ERR_OPERATIONS_ERROR;
2003 py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
2004 py_ldb, py_next);
2006 if (py_result == NULL) {
2007 return LDB_ERR_PYTHON_EXCEPTION;
2010 mod->private_data = py_result;
2012 talloc_set_destructor(mod, py_module_destructor);
2014 return ldb_next_init(mod);
2017 static PyObject *py_register_module(PyObject *module, PyObject *args)
2019 int ret;
2020 struct ldb_module_ops *ops;
2021 PyObject *input;
2023 if (!PyArg_ParseTuple(args, "O", &input))
2024 return NULL;
2026 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
2027 if (ops == NULL) {
2028 PyErr_NoMemory();
2029 return NULL;
2032 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name"))));
2034 Py_INCREF(input);
2035 ops->private_data = input;
2036 ops->init_context = py_module_init;
2037 ops->search = py_module_search;
2038 ops->add = py_module_add;
2039 ops->modify = py_module_modify;
2040 ops->del = py_module_del;
2041 ops->rename = py_module_rename;
2042 ops->request = py_module_request;
2043 ops->extended = py_module_extended;
2044 ops->start_transaction = py_module_start_transaction;
2045 ops->end_transaction = py_module_end_transaction;
2046 ops->del_transaction = py_module_del_transaction;
2048 ret = ldb_register_module(ops);
2050 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
2052 Py_RETURN_NONE;
2055 static PyObject *py_timestring(PyObject *module, PyObject *args)
2057 time_t t;
2058 char *tresult;
2059 PyObject *ret;
2060 if (!PyArg_ParseTuple(args, "L", &t))
2061 return NULL;
2062 tresult = ldb_timestring(NULL, t);
2063 ret = PyString_FromString(tresult);
2064 talloc_free(tresult);
2065 return ret;
2068 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
2070 char *str;
2071 if (!PyArg_ParseTuple(args, "s", &str))
2072 return NULL;
2074 return PyInt_FromLong(ldb_string_to_time(str));
2077 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
2079 char *name;
2080 if (!PyArg_ParseTuple(args, "s", &name))
2081 return NULL;
2082 return PyBool_FromLong(ldb_valid_attr_name(name));
2085 static PyMethodDef py_ldb_global_methods[] = {
2086 { "register_module", py_register_module, METH_VARARGS,
2087 "S.register_module(module) -> None\n"
2088 "Register a LDB module."},
2089 { "timestring", py_timestring, METH_VARARGS,
2090 "S.timestring(int) -> string\n"
2091 "Generate a LDAP time string from a UNIX timestamp" },
2092 { "string_to_time", py_string_to_time, METH_VARARGS,
2093 "S.string_to_time(string) -> int\n"
2094 "Parse a LDAP time string into a UNIX timestamp." },
2095 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2096 "S.valid_attr_name(name) -> bool\n"
2097 "Check whether the supplied name is a valid attribute name." },
2098 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2099 NULL },
2100 { NULL }
2103 void initldb(void)
2105 PyObject *m;
2107 if (PyType_Ready(&PyLdbDn) < 0)
2108 return;
2110 if (PyType_Ready(&PyLdbMessage) < 0)
2111 return;
2113 if (PyType_Ready(&PyLdbMessageElement) < 0)
2114 return;
2116 if (PyType_Ready(&PyLdb) < 0)
2117 return;
2119 if (PyType_Ready(&PyLdbModule) < 0)
2120 return;
2122 if (PyType_Ready(&PyLdbTree) < 0)
2123 return;
2125 m = Py_InitModule3("ldb", py_ldb_global_methods,
2126 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2127 if (m == NULL)
2128 return;
2130 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2131 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2132 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2133 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2135 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2136 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2137 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2138 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2140 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2141 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2142 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2143 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2144 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2145 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2146 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2147 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2148 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2149 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2150 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2151 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2152 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2153 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2154 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2155 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2156 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2157 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2158 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2159 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2160 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2161 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2162 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2163 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2164 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2165 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2166 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2167 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2168 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2169 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2170 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2171 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2172 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2173 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2174 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2175 PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2176 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2177 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2179 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2181 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2183 PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL);
2184 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2186 Py_INCREF(&PyLdb);
2187 Py_INCREF(&PyLdbDn);
2188 Py_INCREF(&PyLdbModule);
2189 Py_INCREF(&PyLdbMessage);
2190 Py_INCREF(&PyLdbMessageElement);
2191 Py_INCREF(&PyLdbTree);
2193 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2194 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2195 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2196 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2197 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2198 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);