Avoid using a utility header for Python replacements included in Samba,
[Samba/ekacnet.git] / source4 / lib / ldb / pyldb.c
blob89ef7aeddf2e861fc2a5c9cd6234bcd34220817b
1 /*
2 Unix SMB/CIFS implementation.
4 Swig 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-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
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 "ldb_includes.h"
29 #include <Python.h>
30 #include "pyldb.h"
32 /* There's no Py_ssize_t in 2.4, apparently */
33 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
34 typedef int Py_ssize_t;
35 typedef inquiry lenfunc;
36 typedef intargfunc ssizeargfunc;
37 #endif
39 #ifndef Py_RETURN_NONE
40 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
41 #endif
43 /* Picked out of thin air. To do this properly, we should probably have some part of the
44 * errors in LDB be allocated to bindings ? */
45 #define LDB_ERR_PYTHON_EXCEPTION 142
47 static PyObject *PyExc_LdbError;
49 void PyErr_SetLdbError(int ret, struct ldb_context *ldb_ctx)
51 if (ret == LDB_ERR_PYTHON_EXCEPTION)
52 return; /* Python exception should already be set, just keep that */
53 PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", ret, ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
55 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
56 struct ldb_message_element *el,
57 struct ldb_val *val)
59 const struct ldb_schema_attribute *a;
60 struct ldb_val new_val;
61 TALLOC_CTX *mem_ctx = talloc_new(NULL);
62 PyObject *ret;
64 new_val = *val;
66 if (ldb_ctx != NULL) {
67 a = ldb_schema_attribute_by_name(ldb_ctx, el->name);
69 if (a != NULL) {
70 if (a->syntax->ldif_write_fn(ldb_ctx, mem_ctx, val, &new_val) != 0) {
71 talloc_free(mem_ctx);
72 return NULL;
77 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
79 talloc_free(mem_ctx);
81 return ret;
84 bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object,
85 struct ldb_context *ldb_ctx, struct ldb_dn **dn)
87 struct ldb_dn *odn;
89 if (ldb_ctx != NULL && PyString_Check(object)) {
90 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
91 *dn = odn;
92 return true;
95 if (PyLdbDn_Check(object)) {
96 *dn = PyLdbDn_AsDn(object);
97 return true;
100 PyErr_SetString(PyExc_TypeError, "Expected DN");
101 return false;
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;
119 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx, PyObject *obj)
121 struct ldb_result *res;
122 int i;
124 if (obj == Py_None)
125 return NULL;
127 res = talloc_zero(mem_ctx, struct ldb_result);
128 res->count = PyList_Size(obj);
129 res->msgs = talloc_array(res, struct ldb_message *, res->count);
130 for (i = 0; i < res->count; i++) {
131 PyObject *item = PyList_GetItem(obj, i);
132 res->msgs[i] = PyLdbMessage_AsMessage(item);
134 return res;
137 static PyObject *py_ldb_dn_validate(PyLdbDnObject *self)
139 return PyBool_FromLong(ldb_dn_validate(self->dn));
142 static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self)
144 return PyBool_FromLong(ldb_dn_is_valid(self->dn));
147 static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self)
149 return PyBool_FromLong(ldb_dn_is_special(self->dn));
152 static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self)
154 return PyBool_FromLong(ldb_dn_is_null(self->dn));
157 static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self)
159 return PyString_FromString(ldb_dn_get_casefold(self->dn));
162 static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self)
164 return PyString_FromString(ldb_dn_get_linearized(self->dn));
167 static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self)
169 return PyString_FromString(ldb_dn_canonical_string(self->dn, self->dn));
172 static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self)
174 return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn));
177 static PyObject *py_ldb_dn_repr(PyLdbDnObject *self)
179 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn))));
182 static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args)
184 char *name;
186 if (!PyArg_ParseTuple(args, "s", &name))
187 return NULL;
189 return ldb_dn_check_special(self->dn, name)?Py_True:Py_False;
192 static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
194 return ldb_dn_compare(dn1->dn, dn2->dn);
197 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
199 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
200 return PyLdbDn_FromDn(ldb_dn_get_parent(NULL, dn));
203 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
205 static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
207 PyObject *py_other;
208 struct ldb_dn *dn, *other;
209 if (!PyArg_ParseTuple(args, "O", &py_other))
210 return NULL;
212 dn = PyLdbDn_AsDn((PyObject *)self);
214 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
215 return NULL;
217 return ldb_dn_add_child(dn, other)?Py_True:Py_False;
220 static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
222 PyObject *py_other;
223 struct ldb_dn *other, *dn;
224 if (!PyArg_ParseTuple(args, "O", &py_other))
225 return NULL;
227 dn = PyLdbDn_AsDn((PyObject *)self);
229 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
230 return NULL;
232 return ldb_dn_add_base(dn, other)?Py_True:Py_False;
235 static PyMethodDef py_ldb_dn_methods[] = {
236 { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS,
237 "S.validate() -> bool\n"
238 "Validate DN is correct." },
239 { "is_valid", (PyCFunction)py_ldb_dn_is_valid, METH_NOARGS,
240 "S.is_valid() -> bool\n" },
241 { "is_special", (PyCFunction)py_ldb_dn_is_special, METH_NOARGS,
242 "S.is_special() -> bool\n"
243 "Check whether this is a special LDB DN." },
244 { "is_null", (PyCFunction)py_ldb_dn_is_null, METH_NOARGS,
245 "Check whether this is a null DN." },
246 { "get_casefold", (PyCFunction)py_ldb_dn_get_casefold, METH_NOARGS,
247 NULL },
248 { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS,
249 NULL },
250 { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
251 "S.canonical_str() -> string\n"
252 "Canonical version of this DN (like a posix path)." },
253 { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
254 "S.canonical_ex_str() -> string\n"
255 "Canonical version of this DN (like a posix path, with terminating newline)." },
256 { "check_special", (PyCFunction)py_ldb_dn_is_special, METH_VARARGS,
257 NULL },
258 { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS,
259 "S.parent() -> dn\n"
260 "Get the parent for this DN." },
261 { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS,
262 "S.add_child(dn) -> None\n"
263 "Add a child DN to this DN." },
264 { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
265 "S.add_base(dn) -> None\n"
266 "Add a base DN to this DN." },
267 { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
268 NULL },
269 { NULL }
272 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
274 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
277 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
279 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self),
280 *other;
281 struct ldb_dn *ret = ldb_dn_copy(NULL, dn);
282 if (!PyObject_AsDn(NULL, py_other, NULL, &other))
283 return NULL;
284 ldb_dn_add_child(ret, other);
285 return PyLdbDn_FromDn(ret);
288 static PySequenceMethods py_ldb_dn_seq = {
289 .sq_length = (lenfunc)py_ldb_dn_len,
290 .sq_concat = (binaryfunc)py_ldb_dn_concat,
293 static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
295 struct ldb_dn *ret;
296 char *str;
297 PyObject *py_ldb;
298 struct ldb_context *ldb_ctx;
299 PyLdbDnObject *py_ret;
300 const char *kwnames[] = { "ldb", "dn", NULL };
302 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os", (char **)kwnames, &py_ldb, &str))
303 return NULL;
305 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
307 ret = ldb_dn_new(ldb_ctx, ldb_ctx, str);
308 /* ldb_dn_new() doesn't accept NULL as memory context, so
309 we do it this way... */
310 talloc_steal(NULL, ret);
312 if (ret == NULL || !ldb_dn_validate(ret)) {
313 PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
314 return NULL;
317 py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0);
318 if (ret == NULL) {
319 PyErr_NoMemory();
320 return NULL;
322 py_ret->dn = ret;
323 return (PyObject *)py_ret;
326 PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
328 PyLdbDnObject *py_ret;
329 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
330 if (py_ret == NULL) {
331 PyErr_NoMemory();
332 return NULL;
334 py_ret->mem_ctx = talloc_new(NULL);
335 py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
336 return (PyObject *)py_ret;
339 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
341 talloc_free(self->mem_ctx);
342 self->ob_type->tp_free(self);
345 PyTypeObject PyLdbDn = {
346 .tp_name = "Dn",
347 .tp_methods = py_ldb_dn_methods,
348 .tp_str = (reprfunc)py_ldb_dn_get_linearized,
349 .tp_repr = (reprfunc)py_ldb_dn_repr,
350 .tp_compare = (cmpfunc)py_ldb_dn_compare,
351 .tp_as_sequence = &py_ldb_dn_seq,
352 .tp_doc = "A LDB distinguished name.",
353 .tp_new = py_ldb_dn_new,
354 .tp_dealloc = (destructor)py_ldb_dn_dealloc,
355 .tp_basicsize = sizeof(PyLdbObject),
356 .tp_flags = Py_TPFLAGS_DEFAULT,
359 /* Debug */
360 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
361 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
363 PyObject *fn = context;
364 PyObject_CallFunction(fn, (char *)"(i,O)", level, PyString_FromFormatV(fmt, ap));
367 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
369 PyObject *cb;
371 if (!PyArg_ParseTuple(args, "O", &cb))
372 return NULL;
374 Py_INCREF(cb);
375 /* FIXME: Where do we DECREF cb ? */
376 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
378 Py_RETURN_NONE;
381 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
383 unsigned int perms;
384 if (!PyArg_ParseTuple(args, "I", &perms))
385 return NULL;
387 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
389 Py_RETURN_NONE;
392 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
394 char *modules_dir;
395 if (!PyArg_ParseTuple(args, "s", &modules_dir))
396 return NULL;
398 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
400 Py_RETURN_NONE;
403 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
405 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
406 Py_RETURN_NONE;
409 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
411 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
412 Py_RETURN_NONE;
415 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
417 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
418 Py_RETURN_NONE;
421 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
423 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
424 Py_RETURN_NONE;
427 static PyObject *py_ldb_repr(PyLdbObject *self)
429 return PyString_FromFormat("<ldb connection>");
432 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
434 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
435 if (dn == NULL)
436 Py_RETURN_NONE;
437 return PyLdbDn_FromDn(dn);
441 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
443 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
444 if (dn == NULL)
445 Py_RETURN_NONE;
446 return PyLdbDn_FromDn(dn);
450 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
452 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
453 if (dn == NULL)
454 Py_RETURN_NONE;
455 return PyLdbDn_FromDn(dn);
459 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
461 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
462 if (dn == NULL)
463 Py_RETURN_NONE;
464 return PyLdbDn_FromDn(dn);
467 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list)
469 const char **ret;
470 int i;
471 if (!PyList_Check(list)) {
472 PyErr_SetString(PyExc_TypeError, "options is not a list");
473 return NULL;
475 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
476 for (i = 0; i < PyList_Size(list); i++) {
477 PyObject *item = PyList_GetItem(list, i);
478 if (!PyString_Check(item)) {
479 PyErr_SetString(PyExc_TypeError, "options should be strings");
480 return NULL;
482 ret[i] = PyString_AsString(item);
484 ret[i] = NULL;
485 return ret;
488 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
490 const char *kwnames[] = { "url", "flags", "options", NULL };
491 char *url = NULL;
492 PyObject *py_options = Py_None;
493 const char **options;
494 int flags = 0;
495 int ret;
496 struct ldb_context *ldb;
498 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__", (char **)kwnames,
499 &url, &flags, &py_options))
500 return -1;
502 ldb = PyLdb_AsLdbContext(self);
504 if (py_options == Py_None) {
505 options = NULL;
506 } else {
507 options = PyList_AsStringList(ldb, py_options);
508 if (options == NULL)
509 return -1;
512 if (url != NULL) {
513 ret = ldb_connect(ldb, url, flags, options);
514 if (ret != LDB_SUCCESS) {
515 PyErr_SetLdbError(ret, ldb);
516 return -1;
520 talloc_free(options);
521 return 0;
524 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
526 PyLdbObject *ret;
527 struct ldb_context *ldb;
528 ldb = ldb_init(NULL, event_context_init(NULL));
529 if (ldb == NULL) {
530 PyErr_NoMemory();
531 return NULL;
534 ret = (PyLdbObject *)type->tp_alloc(type, 0);
535 if (ret == NULL) {
536 PyErr_NoMemory();
537 return NULL;
539 ret->ldb_ctx = ldb;
540 return (PyObject *)ret;
543 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
545 char *url;
546 int flags = 0;
547 PyObject *py_options = Py_None;
548 int ret;
549 const char **options;
550 const char *kwnames[] = { "url", "flags", "options", NULL };
551 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO", (char **)kwnames, &url, &flags,
552 &py_options))
553 return NULL;
555 if (py_options == Py_None) {
556 options = NULL;
557 } else {
558 options = PyList_AsStringList(NULL, py_options);
559 if (options == NULL)
560 return NULL;
563 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
564 talloc_free(options);
566 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
568 Py_RETURN_NONE;
571 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
573 PyObject *py_msg;
574 int ret;
575 if (!PyArg_ParseTuple(args, "O", &py_msg))
576 return NULL;
578 if (!PyLdbMessage_Check(py_msg)) {
579 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
580 return NULL;
583 ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
584 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
586 Py_RETURN_NONE;
589 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
591 PyObject *py_msg;
592 int ret;
593 Py_ssize_t dict_pos, msg_pos;
594 struct ldb_message_element *msgel;
595 struct ldb_message *msg;
596 PyObject *key, *value;
598 if (!PyArg_ParseTuple(args, "O", &py_msg))
599 return NULL;
601 if (PyDict_Check(py_msg)) {
602 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
603 msg = ldb_msg_new(NULL);
604 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
605 msg_pos = dict_pos = 0;
606 if (dn_value) {
607 if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
608 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
609 return NULL;
611 if (msg->dn == NULL) {
612 PyErr_SetString(PyExc_TypeError, "dn set but not found");
613 return NULL;
617 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
618 char *key_str = PyString_AsString(key);
619 if (strcmp(key_str, "dn") != 0) {
620 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
621 if (msgel == NULL) {
622 PyErr_SetString(PyExc_TypeError, "unable to import element");
623 return NULL;
625 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
626 msg_pos++;
630 if (msg->dn == NULL) {
631 PyErr_SetString(PyExc_TypeError, "no dn set");
632 return NULL;
635 msg->num_elements = msg_pos;
636 } else {
637 msg = PyLdbMessage_AsMessage(py_msg);
640 ret = ldb_add(PyLdb_AsLdbContext(self), msg);
641 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
643 Py_RETURN_NONE;
648 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
650 PyObject *py_dn;
651 struct ldb_dn *dn;
652 int ret;
653 struct ldb_context *ldb;
654 if (!PyArg_ParseTuple(args, "O", &py_dn))
655 return NULL;
657 ldb = PyLdb_AsLdbContext(self);
659 if (!PyObject_AsDn(NULL, py_dn, ldb, &dn))
660 return NULL;
662 ret = ldb_delete(ldb, dn);
663 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
665 Py_RETURN_NONE;
668 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
670 PyObject *py_dn1, *py_dn2;
671 struct ldb_dn *dn1, *dn2;
672 int ret;
673 struct ldb_context *ldb;
674 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
675 return NULL;
677 ldb = PyLdb_AsLdbContext(self);
678 if (!PyObject_AsDn(NULL, py_dn1, ldb, &dn1))
679 return NULL;
681 if (!PyObject_AsDn(NULL, py_dn2, ldb, &dn2))
682 return NULL;
684 ret = ldb_rename(ldb, dn1, dn2);
685 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
687 Py_RETURN_NONE;
690 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
692 char *name;
693 if (!PyArg_ParseTuple(args, "s", &name))
694 return NULL;
696 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
698 Py_RETURN_NONE;
701 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
703 char *attribute, *syntax;
704 unsigned int flags;
705 int ret;
706 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
707 return NULL;
709 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
711 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
713 Py_RETURN_NONE;
716 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
718 if (ldif == NULL) {
719 Py_RETURN_NONE;
720 } else {
721 /* We don't want this attached to the 'ldb' any more */
722 talloc_steal(NULL, ldif);
723 return Py_BuildValue((char *)"(iO)", ldif->changetype,
724 PyLdbMessage_FromMessage(ldif->msg));
729 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
731 PyObject *list;
732 struct ldb_ldif *ldif;
733 const char *s;
735 if (!PyArg_ParseTuple(args, "s", &s))
736 return NULL;
738 list = PyList_New(0);
739 while ((ldif = ldb_ldif_read_string(self->ldb_ctx, &s)) != NULL) {
740 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
742 return PyObject_GetIter(list);
745 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
747 const struct ldb_schema_attribute *a;
748 struct ldb_val old_val;
749 struct ldb_val new_val;
750 TALLOC_CTX *mem_ctx;
751 PyObject *ret;
752 char *element_name;
753 PyObject *val;
755 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
756 return NULL;
758 mem_ctx = talloc_new(NULL);
760 old_val.data = (uint8_t *)PyString_AsString(val);
761 old_val.length = PyString_Size(val);
763 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
765 if (a == NULL) {
766 Py_RETURN_NONE;
769 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
770 talloc_free(mem_ctx);
771 Py_RETURN_NONE;
774 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
776 talloc_free(mem_ctx);
778 return ret;
781 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
783 PyObject *py_base = Py_None;
784 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
785 char *expr = NULL;
786 PyObject *py_attrs = Py_None;
787 PyObject *py_controls = Py_None;
788 const char *kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
789 int ret;
790 struct ldb_result *res;
791 struct ldb_request *req;
792 const char **attrs;
793 struct ldb_context *ldb_ctx;
794 struct ldb_control **parsed_controls;
795 struct ldb_dn *base;
797 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO", (char **)kwnames,
798 &py_base, &scope, &expr, &py_attrs, &py_controls))
799 return NULL;
801 ldb_ctx = PyLdb_AsLdbContext(self);
803 if (py_attrs == Py_None) {
804 attrs = NULL;
805 } else {
806 attrs = PyList_AsStringList(ldb_ctx, py_attrs);
807 if (attrs == NULL)
808 return NULL;
811 if (py_base == Py_None) {
812 base = ldb_get_default_basedn(ldb_ctx);
813 } else {
814 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base))
815 return NULL;
818 if (py_controls == Py_None) {
819 parsed_controls = NULL;
820 } else {
821 const char **controls = PyList_AsStringList(ldb_ctx, py_controls);
822 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
823 talloc_free(controls);
826 res = talloc_zero(ldb_ctx, struct ldb_result);
827 if (res == NULL) {
828 PyErr_NoMemory();
829 return NULL;
832 ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
833 base,
834 scope,
835 expr,
836 attrs,
837 parsed_controls,
838 res,
839 ldb_search_default_callback,
840 NULL);
842 if (ret != LDB_SUCCESS) {
843 talloc_free(res);
844 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb_ctx);
845 return NULL;
848 ret = ldb_request(ldb_ctx, req);
850 if (ret == LDB_SUCCESS) {
851 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
854 talloc_free(req);
856 if (ret != LDB_SUCCESS) {
857 talloc_free(res);
858 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb_ctx);
859 return NULL;
862 return PyLdbResult_FromResult(res);
865 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
867 char *name;
868 void *data;
870 if (!PyArg_ParseTuple(args, "s", &name))
871 return NULL;
873 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
875 if (data == NULL)
876 Py_RETURN_NONE;
878 /* FIXME: More interpretation */
880 return Py_True;
883 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
885 char *name;
886 PyObject *data;
888 if (!PyArg_ParseTuple(args, "sO", &name, &data))
889 return NULL;
891 /* FIXME: More interpretation */
893 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
895 Py_RETURN_NONE;
898 static PyObject *py_ldb_modules(PyLdbObject *self)
900 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
901 PyObject *ret = PyList_New(0);
902 struct ldb_module *mod;
904 for (mod = ldb->modules; mod; mod = mod->next) {
905 PyList_Append(ret, PyLdbModule_FromModule(mod));
908 return ret;
911 static PyMethodDef py_ldb_methods[] = {
912 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
913 "S.set_debug(callback) -> None\n"
914 "Set callback for LDB debug messages.\n"
915 "The callback should accept a debug level and debug text." },
916 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
917 "S.set_create_perms(mode) -> None\n"
918 "Set mode to use when creating new LDB files." },
919 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
920 "S.set_modules_dir(path) -> None\n"
921 "Set path LDB should search for modules" },
922 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
923 "S.transaction_start() -> None\n"
924 "Start a new transaction." },
925 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
926 "S.transaction_commit() -> None\n"
927 "commit a new transaction." },
928 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
929 "S.transaction_cancel() -> None\n"
930 "cancel a new transaction." },
931 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
932 NULL },
933 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
934 NULL },
935 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
936 NULL },
937 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
938 NULL },
939 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
940 NULL },
941 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
942 "S.connect(url, flags=0, options=None) -> None\n"
943 "Connect to a LDB URL." },
944 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
945 "S.modify(message) -> None\n"
946 "Modify an entry." },
947 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
948 "S.add(message) -> None\n"
949 "Add an entry." },
950 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
951 "S.delete(dn) -> None\n"
952 "Remove an entry." },
953 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
954 "S.rename(old_dn, new_dn) -> None\n"
955 "Rename an entry." },
956 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
957 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
958 "Search in a database.\n"
959 "\n"
960 ":param base: Optional base DN to search\n"
961 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
962 ":param expression: Optional search expression\n"
963 ":param attrs: Attributes to return (defaults to all)\n"
964 ":param controls: Optional list of controls\n"
965 ":return: Iterator over Message objects\n"
967 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
968 NULL },
969 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
970 NULL },
971 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
972 NULL },
973 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
974 "S.parse_ldif(ldif) -> iter(messages)\n"
975 "Parse a string formatted using LDIF." },
976 { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
977 "S.get_opaque(name) -> value\n"
978 "Get an opaque value set on this LDB connection. \n"
979 ":note: The returned value may not be useful in Python."
981 { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
982 "S.set_opaque(name, value) -> None\n"
983 "Set an opaque value on this LDB connection. \n"
984 ":note: Passing incorrect values may cause crashes." },
985 { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
986 "S.modules() -> list\n"
987 "Return the list of modules on this LDB connection " },
988 { NULL },
991 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
993 PyLdbModuleObject *ret;
995 ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
996 if (ret == NULL) {
997 PyErr_NoMemory();
998 return NULL;
1000 ret->mem_ctx = talloc_new(NULL);
1001 ret->mod = talloc_reference(ret->mem_ctx, mod);
1002 return (PyObject *)ret;
1005 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
1007 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
1010 static PyGetSetDef py_ldb_getset[] = {
1011 { (char *)"firstmodule", (getter)py_ldb_get_firstmodule, NULL, NULL },
1012 { NULL }
1015 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
1017 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1018 struct ldb_dn *dn;
1019 struct ldb_result *result;
1020 int ret;
1021 int count;
1023 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
1024 return -1;
1026 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
1027 if (ret != LDB_SUCCESS) {
1028 PyErr_SetLdbError(ret, ldb_ctx);
1029 return -1;
1032 count = result->count;
1034 talloc_free(result);
1036 return count;
1039 static PySequenceMethods py_ldb_seq = {
1040 .sq_contains = (objobjproc)py_ldb_contains,
1043 PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
1045 PyLdbObject *ret;
1047 ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1048 if (ret == NULL) {
1049 PyErr_NoMemory();
1050 return NULL;
1052 ret->mem_ctx = talloc_new(NULL);
1053 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1054 return (PyObject *)ret;
1057 static void py_ldb_dealloc(PyLdbObject *self)
1059 talloc_free(self->mem_ctx);
1060 self->ob_type->tp_free(self);
1063 PyTypeObject PyLdb = {
1064 .tp_name = "Ldb",
1065 .tp_methods = py_ldb_methods,
1066 .tp_repr = (reprfunc)py_ldb_repr,
1067 .tp_new = py_ldb_new,
1068 .tp_init = (initproc)py_ldb_init,
1069 .tp_dealloc = (destructor)py_ldb_dealloc,
1070 .tp_getset = py_ldb_getset,
1071 .tp_getattro = PyObject_GenericGetAttr,
1072 .tp_basicsize = sizeof(PyLdbObject),
1073 .tp_doc = "Connection to a LDB database.",
1074 .tp_as_sequence = &py_ldb_seq,
1075 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1078 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
1080 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1083 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
1085 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1088 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1090 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1091 Py_RETURN_NONE;
1094 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1096 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1097 Py_RETURN_NONE;
1100 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1102 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1103 Py_RETURN_NONE;
1106 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1108 PyObject *py_base, *py_tree, *py_attrs;
1109 int ret, scope;
1110 struct ldb_request *req;
1111 const char *kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1112 struct ldb_module *mod;
1113 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO", (char **)kwnames,
1114 &py_base, &scope, &py_tree, &py_attrs))
1115 return NULL;
1117 mod = self->mod;
1119 ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base),
1120 scope, NULL /* expr */, py_attrs == Py_None?NULL:PyList_AsStringList(req, py_attrs),
1121 NULL /* controls */, NULL, NULL, NULL);
1122 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1124 ret = mod->ops->search(mod, req);
1125 talloc_free(req);
1127 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1129 return PyLdbResult_FromResult(req->op.search.res);
1133 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1135 struct ldb_request *req;
1136 PyObject *py_message;
1137 int ret;
1138 struct ldb_module *mod;
1140 if (!PyArg_ParseTuple(args, "O", &py_message))
1141 return NULL;
1143 req = talloc_zero(NULL, struct ldb_request);
1144 req->operation = LDB_ADD;
1145 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1147 mod = PyLdbModule_AsModule(self);
1148 ret = mod->ops->add(mod, req);
1150 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1152 Py_RETURN_NONE;
1155 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
1157 int ret;
1158 struct ldb_request *req;
1159 PyObject *py_message;
1160 struct ldb_module *mod;
1162 if (!PyArg_ParseTuple(args, "O", &py_message))
1163 return NULL;
1165 req = talloc_zero(NULL, struct ldb_request);
1166 req->operation = LDB_MODIFY;
1167 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1169 mod = PyLdbModule_AsModule(self);
1170 ret = mod->ops->modify(mod, req);
1172 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1174 Py_RETURN_NONE;
1177 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
1179 int ret;
1180 struct ldb_request *req;
1181 PyObject *py_dn;
1183 if (!PyArg_ParseTuple(args, "O", &py_dn))
1184 return NULL;
1186 req = talloc_zero(NULL, struct ldb_request);
1187 req->operation = LDB_DELETE;
1188 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1190 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1192 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1194 Py_RETURN_NONE;
1197 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1199 int ret;
1200 struct ldb_request *req;
1201 PyObject *py_dn1, *py_dn2;
1203 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1204 return NULL;
1206 req = talloc_zero(NULL, struct ldb_request);
1208 req->operation = LDB_RENAME;
1209 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1210 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1212 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1214 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1216 Py_RETURN_NONE;
1219 static PyMethodDef py_ldb_module_methods[] = {
1220 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1221 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1222 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1223 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1224 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1225 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1226 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1227 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1228 { NULL },
1231 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
1233 talloc_free(self->mem_ctx);
1234 self->ob_type->tp_free(self);
1237 PyTypeObject PyLdbModule = {
1238 .tp_name = "LdbModule",
1239 .tp_methods = py_ldb_module_methods,
1240 .tp_repr = (reprfunc)py_ldb_module_repr,
1241 .tp_str = (reprfunc)py_ldb_module_str,
1242 .tp_basicsize = sizeof(PyLdbModuleObject),
1243 .tp_dealloc = (destructor)py_ldb_module_dealloc,
1244 .tp_flags = Py_TPFLAGS_DEFAULT,
1247 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1248 PyObject *set_obj, int flags,
1249 const char *attr_name)
1251 struct ldb_message_element *me;
1253 if (PyLdbMessageElement_Check(set_obj))
1254 return PyLdbMessageElement_AsMessageElement(set_obj);
1256 me = talloc(mem_ctx, struct ldb_message_element);
1258 me->name = attr_name;
1259 me->flags = flags;
1260 if (PyString_Check(set_obj)) {
1261 me->num_values = 1;
1262 me->values = talloc_array(me, struct ldb_val, me->num_values);
1263 me->values[0].length = PyString_Size(set_obj);
1264 me->values[0].data = (uint8_t *)talloc_strndup(me->values,
1265 PyString_AsString(set_obj),
1266 me->values[0].length);
1267 } else if (PySequence_Check(set_obj)) {
1268 int i;
1269 me->num_values = PySequence_Size(set_obj);
1270 me->values = talloc_array(me, struct ldb_val, me->num_values);
1271 for (i = 0; i < me->num_values; i++) {
1272 PyObject *obj = PySequence_GetItem(set_obj, i);
1274 me->values[i].length = PyString_Size(obj);
1275 me->values[i].data = (uint8_t *)PyString_AsString(obj);
1277 } else {
1278 talloc_free(me);
1279 me = NULL;
1282 return me;
1286 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
1287 struct ldb_message_element *me)
1289 int i;
1290 PyObject *result;
1292 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1293 result = PyList_New(me->num_values);
1295 for (i = 0; i < me->num_values; i++) {
1296 PyList_SetItem(result, i,
1297 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1300 return result;
1303 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1305 int i;
1306 if (!PyArg_ParseTuple(args, "i", &i))
1307 return NULL;
1308 if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1309 Py_RETURN_NONE;
1311 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1312 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1315 static PyMethodDef py_ldb_msg_element_methods[] = {
1316 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1317 { NULL },
1320 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1322 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1325 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1327 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1328 if (idx < 0 || idx >= el->num_values) {
1329 PyErr_SetString(PyExc_IndexError, "Out of range");
1330 return NULL;
1332 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1335 static PySequenceMethods py_ldb_msg_element_seq = {
1336 .sq_length = (lenfunc)py_ldb_msg_element_len,
1337 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1340 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1342 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1343 PyLdbMessageElement_AsMessageElement(other));
1346 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1348 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1351 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
1353 PyLdbMessageElementObject *ret;
1354 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1355 if (ret == NULL) {
1356 PyErr_NoMemory();
1357 return NULL;
1359 ret->mem_ctx = talloc_new(NULL);
1360 if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1361 PyErr_NoMemory();
1362 return NULL;
1364 ret->el = el;
1365 return (PyObject *)ret;
1368 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1370 PyObject *py_elements = NULL;
1371 struct ldb_message_element *el;
1372 int flags = 0;
1373 char *name = NULL;
1374 const char *kwnames[] = { "elements", "flags", "name", NULL };
1375 PyLdbMessageElementObject *ret;
1376 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois", (char **)kwnames, &py_elements, &flags, &name))
1377 return NULL;
1379 el = talloc_zero(NULL, struct ldb_message_element);
1381 if (py_elements != NULL) {
1382 int i;
1383 if (PyString_Check(py_elements)) {
1384 el->num_values = 1;
1385 el->values = talloc_array(el, struct ldb_val, 1);
1386 el->values[0].data = (uint8_t *)PyString_AsString(py_elements);
1387 el->values[0].length = PyString_Size(py_elements);
1388 } else if (PySequence_Check(py_elements)) {
1389 el->num_values = PySequence_Size(py_elements);
1390 el->values = talloc_array(el, struct ldb_val, el->num_values);
1391 for (i = 0; i < el->num_values; i++) {
1392 PyObject *item = PySequence_GetItem(py_elements, i);
1393 el->values[i].data = (uint8_t *)PyString_AsString(item);
1394 el->values[i].length = PyString_Size(item);
1396 } else {
1397 PyErr_SetString(PyExc_TypeError,
1398 "Expected string or list");
1399 talloc_free(el);
1400 return NULL;
1404 el->flags = flags;
1405 el->name = talloc_strdup(el, name);
1407 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1408 if (ret == NULL) {
1409 PyErr_NoMemory();
1410 talloc_free(el);
1411 return NULL;
1414 ret->mem_ctx = talloc_new(NULL);
1415 ret->el = talloc_reference(ret->mem_ctx, el);
1416 return (PyObject *)ret;
1419 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1421 char *element_str = NULL;
1422 int i;
1423 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1424 PyObject *ret;
1426 for (i = 0; i < el->num_values; i++) {
1427 PyObject *o = py_ldb_msg_element_find(self, i);
1428 if (element_str == NULL)
1429 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1430 else
1431 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1434 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1436 talloc_free(element_str);
1438 return ret;
1441 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
1443 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1445 if (el->num_values == 1)
1446 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1447 else
1448 Py_RETURN_NONE;
1451 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
1453 talloc_free(self->mem_ctx);
1454 self->ob_type->tp_free(self);
1457 PyTypeObject PyLdbMessageElement = {
1458 .tp_name = "MessageElement",
1459 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1460 .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1461 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1462 .tp_str = (reprfunc)py_ldb_msg_element_str,
1463 .tp_methods = py_ldb_msg_element_methods,
1464 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1465 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1466 .tp_as_sequence = &py_ldb_msg_element_seq,
1467 .tp_new = py_ldb_msg_element_new,
1468 .tp_flags = Py_TPFLAGS_DEFAULT,
1471 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
1473 char *name;
1474 if (!PyArg_ParseTuple(args, "s", &name))
1475 return NULL;
1477 ldb_msg_remove_attr(self->msg, name);
1479 Py_RETURN_NONE;
1482 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
1484 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1485 int i, j = 0;
1486 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1487 if (msg->dn != NULL) {
1488 PyList_SetItem(obj, j, PyString_FromString("dn"));
1489 j++;
1491 for (i = 0; i < msg->num_elements; i++) {
1492 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1493 j++;
1495 return obj;
1498 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
1500 struct ldb_message_element *el;
1501 char *name = PyString_AsString(py_name);
1502 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1503 if (!strcmp(name, "dn"))
1504 return PyLdbDn_FromDn(msg->dn);
1505 el = ldb_msg_find_element(msg, name);
1506 if (el == NULL) {
1507 return NULL;
1509 return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg);
1512 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
1514 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
1515 if (ret == NULL) {
1516 PyErr_SetString(PyExc_KeyError, "No such element");
1517 return NULL;
1519 return ret;
1522 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
1524 PyObject *name, *ret;
1525 if (!PyArg_ParseTuple(args, "O", &name))
1526 return NULL;
1528 ret = py_ldb_msg_getitem_helper(self, name);
1529 if (ret == NULL)
1530 Py_RETURN_NONE;
1531 return ret;
1534 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
1536 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1537 int i, j;
1538 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
1539 j = 0;
1540 if (msg->dn != NULL) {
1541 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
1542 j++;
1544 for (i = 0; i < msg->num_elements; i++, j++) {
1545 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], self->msg)));
1547 return l;
1550 static PyMethodDef py_ldb_msg_methods[] = {
1551 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
1552 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
1553 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
1554 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
1555 { NULL },
1558 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
1560 PyObject *list, *iter;
1562 list = py_ldb_msg_keys(self);
1563 iter = PyObject_GetIter(list);
1564 Py_DECREF(list);
1565 return iter;
1568 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
1570 char *attr_name = PyString_AsString(name);
1571 if (value == NULL) {
1572 ldb_msg_remove_attr(self->msg, attr_name);
1573 } else {
1574 struct ldb_message_element *el = PyObject_AsMessageElement(NULL,
1575 value, 0, attr_name);
1576 if (el == NULL)
1577 return -1;
1578 talloc_steal(self->msg, el);
1579 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
1580 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
1582 return 0;
1585 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
1587 return PyLdbMessage_AsMessage(self)->num_elements;
1590 static PyMappingMethods py_ldb_msg_mapping = {
1591 .mp_length = (lenfunc)py_ldb_msg_length,
1592 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
1593 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
1596 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1598 const char *kwnames[] = { "dn", NULL };
1599 struct ldb_message *ret;
1600 PyObject *pydn = NULL;
1601 PyLdbMessageObject *py_ret;
1602 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", (char **)kwnames, &pydn))
1603 return NULL;
1605 ret = ldb_msg_new(NULL);
1606 if (ret == NULL) {
1607 PyErr_NoMemory();
1608 return NULL;
1611 if (pydn != NULL)
1612 if (!PyObject_AsDn(NULL, pydn, NULL, &ret->dn))
1613 return NULL;
1615 py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
1616 if (py_ret == NULL) {
1617 PyErr_NoMemory();
1618 return NULL;
1621 py_ret->mem_ctx = talloc_new(NULL);
1622 py_ret->msg = talloc_reference(py_ret->mem_ctx, ret);
1623 return (PyObject *)py_ret;
1626 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
1628 PyLdbMessageObject *ret;
1630 ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
1631 if (ret == NULL) {
1632 PyErr_NoMemory();
1633 return NULL;
1635 ret->mem_ctx = talloc_new(NULL);
1636 ret->msg = talloc_reference(ret->mem_ctx, msg);
1637 return (PyObject *)ret;
1640 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
1642 return PyLdbDn_FromDn(PyLdbMessage_AsMessage(self)->dn);
1645 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
1647 PyLdbMessage_AsMessage(self)->dn = PyLdbDn_AsDn(value);
1648 return 0;
1651 static PyGetSetDef py_ldb_msg_getset[] = {
1652 { (char *)"dn", (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
1653 { NULL }
1656 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
1658 PyObject *dict = PyDict_New(), *ret;
1659 if (PyDict_Update(dict, (PyObject *)self) != 0)
1660 return NULL;
1661 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
1662 Py_DECREF(dict);
1663 return ret;
1666 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
1668 talloc_free(self->mem_ctx);
1669 self->ob_type->tp_free(self);
1672 PyTypeObject PyLdbMessage = {
1673 .tp_name = "Message",
1674 .tp_methods = py_ldb_msg_methods,
1675 .tp_getset = py_ldb_msg_getset,
1676 .tp_as_mapping = &py_ldb_msg_mapping,
1677 .tp_basicsize = sizeof(PyLdbMessageObject),
1678 .tp_dealloc = (destructor)py_ldb_msg_dealloc,
1679 .tp_new = py_ldb_msg_new,
1680 .tp_repr = (reprfunc)py_ldb_msg_repr,
1681 .tp_flags = Py_TPFLAGS_DEFAULT,
1682 .tp_iter = (getiterfunc)py_ldb_msg_iter,
1685 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
1687 PyLdbTreeObject *ret;
1689 ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
1690 if (ret == NULL) {
1691 PyErr_NoMemory();
1692 return NULL;
1695 ret->mem_ctx = talloc_new(NULL);
1696 ret->tree = talloc_reference(ret->mem_ctx, tree);
1697 return (PyObject *)ret;
1700 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
1702 talloc_free(self->mem_ctx);
1703 self->ob_type->tp_free(self);
1706 PyTypeObject PyLdbTree = {
1707 .tp_name = "Tree",
1708 .tp_basicsize = sizeof(PyLdbTreeObject),
1709 .tp_dealloc = (destructor)py_ldb_tree_dealloc,
1710 .tp_flags = Py_TPFLAGS_DEFAULT,
1713 /* Ldb_module */
1714 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
1716 PyObject *py_ldb = mod->private_data;
1717 PyObject *py_result, *py_base, *py_attrs, *py_tree;
1719 py_base = PyLdbDn_FromDn(req->op.search.base);
1721 if (py_base == NULL)
1722 return LDB_ERR_OPERATIONS_ERROR;
1724 py_tree = PyLdbTree_FromTree(req->op.search.tree);
1726 if (py_tree == NULL)
1727 return LDB_ERR_OPERATIONS_ERROR;
1729 if (req->op.search.attrs == NULL) {
1730 py_attrs = Py_None;
1731 } else {
1732 int i, len;
1733 for (len = 0; req->op.search.attrs[len]; len++);
1734 py_attrs = PyList_New(len);
1735 for (i = 0; i < len; i++)
1736 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1739 py_result = PyObject_CallMethod(py_ldb, (char *)"search", (char *)"OiOO", py_base, req->op.search.scope, py_tree, py_attrs);
1741 Py_DECREF(py_attrs);
1742 Py_DECREF(py_tree);
1743 Py_DECREF(py_base);
1745 if (py_result == NULL) {
1746 return LDB_ERR_PYTHON_EXCEPTION;
1749 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
1750 if (req->op.search.res == NULL) {
1751 return LDB_ERR_PYTHON_EXCEPTION;
1754 Py_DECREF(py_result);
1756 return LDB_SUCCESS;
1759 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1761 PyObject *py_ldb = mod->private_data;
1762 PyObject *py_result, *py_msg;
1764 py_msg = PyLdbMessage_FromMessage((struct ldb_message *)req->op.add.message);
1766 if (py_msg == NULL) {
1767 return LDB_ERR_OPERATIONS_ERROR;
1770 py_result = PyObject_CallMethod(py_ldb, (char *)"add", (char *)"O", py_msg);
1772 Py_DECREF(py_msg);
1774 if (py_result == NULL) {
1775 return LDB_ERR_PYTHON_EXCEPTION;
1778 Py_DECREF(py_result);
1780 return LDB_SUCCESS;
1783 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1785 PyObject *py_ldb = mod->private_data;
1786 PyObject *py_result, *py_msg;
1788 py_msg = PyLdbMessage_FromMessage((struct ldb_message *)req->op.mod.message);
1790 if (py_msg == NULL) {
1791 return LDB_ERR_OPERATIONS_ERROR;
1794 py_result = PyObject_CallMethod(py_ldb, (char *)"modify", (char *)"O", py_msg);
1796 Py_DECREF(py_msg);
1798 if (py_result == NULL) {
1799 return LDB_ERR_PYTHON_EXCEPTION;
1802 Py_DECREF(py_result);
1804 return LDB_SUCCESS;
1807 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
1809 PyObject *py_ldb = mod->private_data;
1810 PyObject *py_result, *py_dn;
1812 py_dn = PyLdbDn_FromDn(req->op.del.dn);
1814 if (py_dn == NULL)
1815 return LDB_ERR_OPERATIONS_ERROR;
1817 py_result = PyObject_CallMethod(py_ldb, (char *)"delete", (char *)"O", py_dn);
1819 if (py_result == NULL) {
1820 return LDB_ERR_PYTHON_EXCEPTION;
1823 Py_DECREF(py_result);
1825 return LDB_SUCCESS;
1828 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
1830 PyObject *py_ldb = mod->private_data;
1831 PyObject *py_result, *py_olddn, *py_newdn;
1833 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
1835 if (py_olddn == NULL)
1836 return LDB_ERR_OPERATIONS_ERROR;
1838 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
1840 if (py_newdn == NULL)
1841 return LDB_ERR_OPERATIONS_ERROR;
1843 py_result = PyObject_CallMethod(py_ldb, (char *)"rename", (char *)"OO", py_olddn, py_newdn);
1845 Py_DECREF(py_olddn);
1846 Py_DECREF(py_newdn);
1848 if (py_result == NULL) {
1849 return LDB_ERR_PYTHON_EXCEPTION;
1852 Py_DECREF(py_result);
1854 return LDB_SUCCESS;
1857 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
1859 PyObject *py_ldb = mod->private_data;
1860 PyObject *py_result;
1862 py_result = PyObject_CallMethod(py_ldb, (char *)"request", (char *)"");
1864 return LDB_ERR_OPERATIONS_ERROR;
1867 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
1869 PyObject *py_ldb = mod->private_data;
1870 PyObject *py_result;
1872 py_result = PyObject_CallMethod(py_ldb, (char *)"extended", (char *)"");
1874 return LDB_ERR_OPERATIONS_ERROR;
1877 static int py_module_start_transaction(struct ldb_module *mod)
1879 PyObject *py_ldb = mod->private_data;
1880 PyObject *py_result;
1882 py_result = PyObject_CallMethod(py_ldb, (char *)"start_transaction", (char *)"");
1884 if (py_result == NULL) {
1885 return LDB_ERR_PYTHON_EXCEPTION;
1888 Py_DECREF(py_result);
1890 return LDB_SUCCESS;
1893 static int py_module_end_transaction(struct ldb_module *mod)
1895 PyObject *py_ldb = mod->private_data;
1896 PyObject *py_result;
1898 py_result = PyObject_CallMethod(py_ldb, (char *)"end_transaction", (char *)"");
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_del_transaction(struct ldb_module *mod)
1911 PyObject *py_ldb = mod->private_data;
1912 PyObject *py_result;
1914 py_result = PyObject_CallMethod(py_ldb, (char *)"del_transaction", (char *)"");
1916 if (py_result == NULL) {
1917 return LDB_ERR_PYTHON_EXCEPTION;
1920 Py_DECREF(py_result);
1922 return LDB_SUCCESS;
1925 static int py_module_destructor(struct ldb_module *mod)
1927 Py_DECREF((PyObject *)mod->private_data);
1928 return 0;
1931 static int py_module_init (struct ldb_module *mod)
1933 PyObject *py_class = mod->ops->private_data;
1934 PyObject *py_result, *py_next, *py_ldb;
1936 py_ldb = PyLdb_FromLdbContext(mod->ldb);
1938 if (py_ldb == NULL)
1939 return LDB_ERR_OPERATIONS_ERROR;
1941 py_next = PyLdbModule_FromModule(mod->next);
1943 if (py_next == NULL)
1944 return LDB_ERR_OPERATIONS_ERROR;
1946 py_result = PyObject_CallFunction(py_class, (char *)"OO", py_ldb, py_next);
1948 if (py_result == NULL) {
1949 return LDB_ERR_PYTHON_EXCEPTION;
1952 mod->private_data = py_result;
1954 talloc_set_destructor(mod, py_module_destructor);
1956 return ldb_next_init(mod);
1959 static PyObject *py_register_module(PyObject *module, PyObject *args)
1961 int ret;
1962 struct ldb_module_ops *ops;
1963 PyObject *input;
1965 if (!PyArg_ParseTuple(args, "O", &input))
1966 return NULL;
1968 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
1969 if (ops == NULL) {
1970 PyErr_NoMemory();
1971 return NULL;
1974 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, (char *)"name")));
1976 Py_INCREF(input);
1977 ops->private_data = input;
1978 ops->init_context = py_module_init;
1979 ops->search = py_module_search;
1980 ops->add = py_module_add;
1981 ops->modify = py_module_modify;
1982 ops->del = py_module_del;
1983 ops->rename = py_module_rename;
1984 ops->request = py_module_request;
1985 ops->extended = py_module_extended;
1986 ops->start_transaction = py_module_start_transaction;
1987 ops->end_transaction = py_module_end_transaction;
1988 ops->del_transaction = py_module_del_transaction;
1990 ret = ldb_register_module(ops);
1992 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1994 Py_RETURN_NONE;
1997 static PyObject *py_timestring(PyObject *module, PyObject *args)
1999 time_t t;
2000 char *tresult;
2001 PyObject *ret;
2002 if (!PyArg_ParseTuple(args, "L", &t))
2003 return NULL;
2004 tresult = ldb_timestring(NULL, t);
2005 ret = PyString_FromString(tresult);
2006 talloc_free(tresult);
2007 return ret;
2010 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
2012 char *str;
2013 if (!PyArg_ParseTuple(args, "s", &str))
2014 return NULL;
2016 return PyInt_FromLong(ldb_string_to_time(str));
2019 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
2021 char *name;
2022 if (!PyArg_ParseTuple(args, "s", &name))
2023 return NULL;
2024 return PyBool_FromLong(ldb_valid_attr_name(name));
2027 static PyMethodDef py_ldb_global_methods[] = {
2028 { "register_module", py_register_module, METH_VARARGS,
2029 "S.register_module(module) -> None\n"
2030 "Register a LDB module."},
2031 { "timestring", py_timestring, METH_VARARGS,
2032 "S.timestring(int) -> string\n"
2033 "Generate a LDAP time string from a UNIX timestamp" },
2034 { "string_to_time", py_string_to_time, METH_VARARGS,
2035 "S.string_to_time(string) -> int\n"
2036 "Parse a LDAP time string into a UNIX timestamp." },
2037 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2038 "S.valid_attr_name(name) -> bool\n"
2039 "Check whether the supplied name is a valid attribute name." },
2040 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2041 NULL },
2042 { NULL }
2045 void initldb(void)
2047 PyObject *m;
2049 if (PyType_Ready(&PyLdbDn) < 0)
2050 return;
2052 if (PyType_Ready(&PyLdbMessage) < 0)
2053 return;
2055 if (PyType_Ready(&PyLdbMessageElement) < 0)
2056 return;
2058 if (PyType_Ready(&PyLdb) < 0)
2059 return;
2061 if (PyType_Ready(&PyLdbModule) < 0)
2062 return;
2064 if (PyType_Ready(&PyLdbTree) < 0)
2065 return;
2067 m = Py_InitModule3("ldb", py_ldb_global_methods,
2068 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2069 if (m == NULL)
2070 return;
2072 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2073 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2074 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2075 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2077 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2078 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2079 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2080 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2082 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2083 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2084 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2085 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2086 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2087 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2088 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2089 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2090 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2091 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2092 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2093 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2094 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2095 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2096 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2097 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2098 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2099 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2100 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2101 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2102 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2103 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2104 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2105 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2106 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2107 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2108 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2109 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2110 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2111 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2112 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2113 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2114 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2115 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2116 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2117 PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2118 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2119 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2121 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2123 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2125 PyExc_LdbError = PyErr_NewException((char *)"_ldb.LdbError", NULL, NULL);
2126 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2128 Py_INCREF(&PyLdb);
2129 Py_INCREF(&PyLdbDn);
2130 Py_INCREF(&PyLdbModule);
2131 Py_INCREF(&PyLdbMessage);
2132 Py_INCREF(&PyLdbMessageElement);
2133 Py_INCREF(&PyLdbTree);
2135 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2136 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2137 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2138 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2139 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2140 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);