WHATSNEW: Update changes since 3.4.9.
[Samba.git] / source4 / lib / ldb / pyldb.c
blob81b960979f6b6f52e6fea28027634b0eeec1042e
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 "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 /* Picked out of thin air. To do this properly, we should probably have some part of the
45 * errors in LDB be allocated to bindings ? */
46 #define LDB_ERR_PYTHON_EXCEPTION 142
48 static PyObject *PyExc_LdbError;
50 void PyErr_SetLdbError(int ret, struct ldb_context *ldb_ctx)
52 if (ret == LDB_ERR_PYTHON_EXCEPTION)
53 return; /* Python exception should already be set, just keep that */
54 PyErr_SetObject(PyExc_LdbError, Py_BuildValue(discard_const_p(char, "(i,s)"),
55 ret, ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
58 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
59 struct ldb_message_element *el,
60 struct ldb_val *val)
62 const struct ldb_schema_attribute *a;
63 struct ldb_val new_val;
64 TALLOC_CTX *mem_ctx = talloc_new(NULL);
65 PyObject *ret;
67 new_val = *val;
69 if (ldb_ctx != NULL) {
70 a = ldb_schema_attribute_by_name(ldb_ctx, el->name);
72 if (a != NULL) {
73 if (a->syntax->ldif_write_fn(ldb_ctx, mem_ctx, val, &new_val) != 0) {
74 talloc_free(mem_ctx);
75 return NULL;
80 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
82 talloc_free(mem_ctx);
84 return ret;
87 bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object,
88 struct ldb_context *ldb_ctx, struct ldb_dn **dn)
90 struct ldb_dn *odn;
92 if (ldb_ctx != NULL && PyString_Check(object)) {
93 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
94 *dn = odn;
95 return true;
98 if (PyLdbDn_Check(object)) {
99 *dn = PyLdbDn_AsDn(object);
100 return true;
103 PyErr_SetString(PyExc_TypeError, "Expected DN");
104 return false;
107 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
109 PyObject *ret;
110 int i;
111 if (result == NULL) {
112 Py_RETURN_NONE;
114 ret = PyList_New(result->count);
115 for (i = 0; i < result->count; i++) {
116 PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i])
119 return ret;
122 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx, PyObject *obj)
124 struct ldb_result *res;
125 int i;
127 if (obj == Py_None)
128 return NULL;
130 res = talloc_zero(mem_ctx, struct ldb_result);
131 res->count = PyList_Size(obj);
132 res->msgs = talloc_array(res, struct ldb_message *, res->count);
133 for (i = 0; i < res->count; i++) {
134 PyObject *item = PyList_GetItem(obj, i);
135 res->msgs[i] = PyLdbMessage_AsMessage(item);
137 return res;
140 static PyObject *py_ldb_dn_validate(PyLdbDnObject *self)
142 return PyBool_FromLong(ldb_dn_validate(self->dn));
145 static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self)
147 return PyBool_FromLong(ldb_dn_is_valid(self->dn));
150 static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self)
152 return PyBool_FromLong(ldb_dn_is_special(self->dn));
155 static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self)
157 return PyBool_FromLong(ldb_dn_is_null(self->dn));
160 static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self)
162 return PyString_FromString(ldb_dn_get_casefold(self->dn));
165 static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self)
167 return PyString_FromString(ldb_dn_get_linearized(self->dn));
170 static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self)
172 return PyString_FromString(ldb_dn_canonical_string(self->dn, self->dn));
175 static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self)
177 return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn));
180 static PyObject *py_ldb_dn_repr(PyLdbDnObject *self)
182 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn))));
185 static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args)
187 char *name;
189 if (!PyArg_ParseTuple(args, "s", &name))
190 return NULL;
192 return ldb_dn_check_special(self->dn, name)?Py_True:Py_False;
195 static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
197 return ldb_dn_compare(dn1->dn, dn2->dn);
200 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
202 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
203 return PyLdbDn_FromDn(ldb_dn_get_parent(NULL, dn));
206 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
208 static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
210 PyObject *py_other;
211 struct ldb_dn *dn, *other;
212 if (!PyArg_ParseTuple(args, "O", &py_other))
213 return NULL;
215 dn = PyLdbDn_AsDn((PyObject *)self);
217 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
218 return NULL;
220 return ldb_dn_add_child(dn, other)?Py_True:Py_False;
223 static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
225 PyObject *py_other;
226 struct ldb_dn *other, *dn;
227 if (!PyArg_ParseTuple(args, "O", &py_other))
228 return NULL;
230 dn = PyLdbDn_AsDn((PyObject *)self);
232 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
233 return NULL;
235 return ldb_dn_add_base(dn, other)?Py_True:Py_False;
238 static PyMethodDef py_ldb_dn_methods[] = {
239 { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS,
240 "S.validate() -> bool\n"
241 "Validate DN is correct." },
242 { "is_valid", (PyCFunction)py_ldb_dn_is_valid, METH_NOARGS,
243 "S.is_valid() -> bool\n" },
244 { "is_special", (PyCFunction)py_ldb_dn_is_special, METH_NOARGS,
245 "S.is_special() -> bool\n"
246 "Check whether this is a special LDB DN." },
247 { "is_null", (PyCFunction)py_ldb_dn_is_null, METH_NOARGS,
248 "Check whether this is a null DN." },
249 { "get_casefold", (PyCFunction)py_ldb_dn_get_casefold, METH_NOARGS,
250 NULL },
251 { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS,
252 NULL },
253 { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
254 "S.canonical_str() -> string\n"
255 "Canonical version of this DN (like a posix path)." },
256 { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
257 "S.canonical_ex_str() -> string\n"
258 "Canonical version of this DN (like a posix path, with terminating newline)." },
259 { "check_special", (PyCFunction)py_ldb_dn_is_special, METH_VARARGS,
260 NULL },
261 { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS,
262 "S.parent() -> dn\n"
263 "Get the parent for this DN." },
264 { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS,
265 "S.add_child(dn) -> None\n"
266 "Add a child DN to this DN." },
267 { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
268 "S.add_base(dn) -> None\n"
269 "Add a base DN to this DN." },
270 { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
271 NULL },
272 { NULL }
275 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
277 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
280 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
282 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self),
283 *other;
284 struct ldb_dn *ret = ldb_dn_copy(NULL, dn);
285 if (!PyObject_AsDn(NULL, py_other, NULL, &other))
286 return NULL;
287 ldb_dn_add_child(ret, other);
288 return PyLdbDn_FromDn(ret);
291 static PySequenceMethods py_ldb_dn_seq = {
292 .sq_length = (lenfunc)py_ldb_dn_len,
293 .sq_concat = (binaryfunc)py_ldb_dn_concat,
296 static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
298 struct ldb_dn *ret;
299 char *str;
300 PyObject *py_ldb;
301 struct ldb_context *ldb_ctx;
302 PyLdbDnObject *py_ret;
303 const char * const kwnames[] = { "ldb", "dn", NULL };
305 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os",
306 discard_const_p(char *, kwnames),
307 &py_ldb, &str))
308 return NULL;
310 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
312 ret = ldb_dn_new(ldb_ctx, ldb_ctx, str);
313 /* ldb_dn_new() doesn't accept NULL as memory context, so
314 we do it this way... */
315 talloc_steal(NULL, ret);
317 if (ret == NULL || !ldb_dn_validate(ret)) {
318 PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
319 return NULL;
322 py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0);
323 if (ret == NULL) {
324 PyErr_NoMemory();
325 return NULL;
327 py_ret->dn = ret;
328 return (PyObject *)py_ret;
331 PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
333 PyLdbDnObject *py_ret;
334 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
335 if (py_ret == NULL) {
336 PyErr_NoMemory();
337 return NULL;
339 py_ret->mem_ctx = talloc_new(NULL);
340 py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
341 return (PyObject *)py_ret;
344 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
346 talloc_free(self->mem_ctx);
347 self->ob_type->tp_free(self);
350 PyTypeObject PyLdbDn = {
351 .tp_name = "Dn",
352 .tp_methods = py_ldb_dn_methods,
353 .tp_str = (reprfunc)py_ldb_dn_get_linearized,
354 .tp_repr = (reprfunc)py_ldb_dn_repr,
355 .tp_compare = (cmpfunc)py_ldb_dn_compare,
356 .tp_as_sequence = &py_ldb_dn_seq,
357 .tp_doc = "A LDB distinguished name.",
358 .tp_new = py_ldb_dn_new,
359 .tp_dealloc = (destructor)py_ldb_dn_dealloc,
360 .tp_basicsize = sizeof(PyLdbObject),
361 .tp_flags = Py_TPFLAGS_DEFAULT,
364 /* Debug */
365 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
366 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
368 PyObject *fn = (PyObject *)context;
369 PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap));
372 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
374 PyObject *cb;
376 if (!PyArg_ParseTuple(args, "O", &cb))
377 return NULL;
379 Py_INCREF(cb);
380 /* FIXME: Where do we DECREF cb ? */
381 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
383 Py_RETURN_NONE;
386 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
388 unsigned int perms;
389 if (!PyArg_ParseTuple(args, "I", &perms))
390 return NULL;
392 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
394 Py_RETURN_NONE;
397 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
399 char *modules_dir;
400 if (!PyArg_ParseTuple(args, "s", &modules_dir))
401 return NULL;
403 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
405 Py_RETURN_NONE;
408 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
410 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
411 Py_RETURN_NONE;
414 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
416 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
417 Py_RETURN_NONE;
420 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
422 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
423 Py_RETURN_NONE;
426 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
428 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
429 Py_RETURN_NONE;
432 static PyObject *py_ldb_repr(PyLdbObject *self)
434 return PyString_FromFormat("<ldb connection>");
437 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
439 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
440 if (dn == NULL)
441 Py_RETURN_NONE;
442 return PyLdbDn_FromDn(dn);
446 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
448 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
449 if (dn == NULL)
450 Py_RETURN_NONE;
451 return PyLdbDn_FromDn(dn);
455 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
457 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
458 if (dn == NULL)
459 Py_RETURN_NONE;
460 return PyLdbDn_FromDn(dn);
464 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
466 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
467 if (dn == NULL)
468 Py_RETURN_NONE;
469 return PyLdbDn_FromDn(dn);
472 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list)
474 const char **ret;
475 int i;
476 if (!PyList_Check(list)) {
477 PyErr_SetString(PyExc_TypeError, "options is not a list");
478 return NULL;
480 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
481 for (i = 0; i < PyList_Size(list); i++) {
482 PyObject *item = PyList_GetItem(list, i);
483 if (!PyString_Check(item)) {
484 PyErr_SetString(PyExc_TypeError, "options should be strings");
485 return NULL;
487 ret[i] = PyString_AsString(item);
489 ret[i] = NULL;
490 return ret;
493 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
495 const char * const kwnames[] = { "url", "flags", "options", NULL };
496 char *url = NULL;
497 PyObject *py_options = Py_None;
498 const char **options;
499 int flags = 0;
500 int ret;
501 struct ldb_context *ldb;
503 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__",
504 discard_const_p(char *, kwnames),
505 &url, &flags, &py_options))
506 return -1;
508 ldb = PyLdb_AsLdbContext(self);
510 if (py_options == Py_None) {
511 options = NULL;
512 } else {
513 options = PyList_AsStringList(ldb, py_options);
514 if (options == NULL)
515 return -1;
518 if (url != NULL) {
519 ret = ldb_connect(ldb, url, flags, options);
520 if (ret != LDB_SUCCESS) {
521 PyErr_SetLdbError(ret, ldb);
522 return -1;
526 talloc_free(options);
527 return 0;
530 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
532 PyLdbObject *ret;
533 struct ldb_context *ldb;
534 ldb = ldb_init(NULL, NULL);
535 if (ldb == NULL) {
536 PyErr_NoMemory();
537 return NULL;
540 ret = (PyLdbObject *)type->tp_alloc(type, 0);
541 if (ret == NULL) {
542 PyErr_NoMemory();
543 return NULL;
545 ret->ldb_ctx = ldb;
546 return (PyObject *)ret;
549 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
551 char *url;
552 int flags = 0;
553 PyObject *py_options = Py_None;
554 int ret;
555 const char **options;
556 const char * const kwnames[] = { "url", "flags", "options", NULL };
558 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO",
559 discard_const_p(char *, kwnames),
560 &url, &flags, &py_options))
561 return NULL;
563 if (py_options == Py_None) {
564 options = NULL;
565 } else {
566 options = PyList_AsStringList(NULL, py_options);
567 if (options == NULL)
568 return NULL;
571 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
572 talloc_free(options);
574 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
576 Py_RETURN_NONE;
579 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
581 PyObject *py_msg;
582 int ret;
583 if (!PyArg_ParseTuple(args, "O", &py_msg))
584 return NULL;
586 if (!PyLdbMessage_Check(py_msg)) {
587 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
588 return NULL;
591 ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
592 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
594 Py_RETURN_NONE;
597 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
599 PyObject *py_msg;
600 int ret;
601 Py_ssize_t dict_pos, msg_pos;
602 struct ldb_message_element *msgel;
603 struct ldb_message *msg;
604 PyObject *key, *value;
606 if (!PyArg_ParseTuple(args, "O", &py_msg))
607 return NULL;
609 if (PyDict_Check(py_msg)) {
610 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
611 msg = ldb_msg_new(NULL);
612 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
613 msg_pos = dict_pos = 0;
614 if (dn_value) {
615 if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
616 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
617 return NULL;
619 if (msg->dn == NULL) {
620 PyErr_SetString(PyExc_TypeError, "dn set but not found");
621 return NULL;
625 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
626 char *key_str = PyString_AsString(key);
627 if (strcmp(key_str, "dn") != 0) {
628 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
629 if (msgel == NULL) {
630 PyErr_SetString(PyExc_TypeError, "unable to import element");
631 return NULL;
633 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
634 msg_pos++;
638 if (msg->dn == NULL) {
639 PyErr_SetString(PyExc_TypeError, "no dn set");
640 return NULL;
643 msg->num_elements = msg_pos;
644 } else {
645 msg = PyLdbMessage_AsMessage(py_msg);
648 ret = ldb_add(PyLdb_AsLdbContext(self), msg);
649 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
651 Py_RETURN_NONE;
656 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
658 PyObject *py_dn;
659 struct ldb_dn *dn;
660 int ret;
661 struct ldb_context *ldb;
662 if (!PyArg_ParseTuple(args, "O", &py_dn))
663 return NULL;
665 ldb = PyLdb_AsLdbContext(self);
667 if (!PyObject_AsDn(NULL, py_dn, ldb, &dn))
668 return NULL;
670 ret = ldb_delete(ldb, dn);
671 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
673 Py_RETURN_NONE;
676 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
678 PyObject *py_dn1, *py_dn2;
679 struct ldb_dn *dn1, *dn2;
680 int ret;
681 struct ldb_context *ldb;
682 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
683 return NULL;
685 ldb = PyLdb_AsLdbContext(self);
686 if (!PyObject_AsDn(NULL, py_dn1, ldb, &dn1))
687 return NULL;
689 if (!PyObject_AsDn(NULL, py_dn2, ldb, &dn2))
690 return NULL;
692 ret = ldb_rename(ldb, dn1, dn2);
693 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
695 Py_RETURN_NONE;
698 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
700 char *name;
701 if (!PyArg_ParseTuple(args, "s", &name))
702 return NULL;
704 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
706 Py_RETURN_NONE;
709 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
711 char *attribute, *syntax;
712 unsigned int flags;
713 int ret;
714 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
715 return NULL;
717 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
719 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
721 Py_RETURN_NONE;
724 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
726 if (ldif == NULL) {
727 Py_RETURN_NONE;
728 } else {
729 /* We don't want this attached to the 'ldb' any more */
730 talloc_steal(NULL, ldif);
731 return Py_BuildValue(discard_const_p(char, "(iO)"),
732 ldif->changetype,
733 PyLdbMessage_FromMessage(ldif->msg));
738 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
740 PyObject *list;
741 struct ldb_ldif *ldif;
742 const char *s;
744 if (!PyArg_ParseTuple(args, "s", &s))
745 return NULL;
747 list = PyList_New(0);
748 while ((ldif = ldb_ldif_read_string(self->ldb_ctx, &s)) != NULL) {
749 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
751 return PyObject_GetIter(list);
754 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
756 const struct ldb_schema_attribute *a;
757 struct ldb_val old_val;
758 struct ldb_val new_val;
759 TALLOC_CTX *mem_ctx;
760 PyObject *ret;
761 char *element_name;
762 PyObject *val;
764 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
765 return NULL;
767 mem_ctx = talloc_new(NULL);
769 old_val.data = (uint8_t *)PyString_AsString(val);
770 old_val.length = PyString_Size(val);
772 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
774 if (a == NULL) {
775 Py_RETURN_NONE;
778 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
779 talloc_free(mem_ctx);
780 Py_RETURN_NONE;
783 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
785 talloc_free(mem_ctx);
787 return ret;
790 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
792 PyObject *py_base = Py_None;
793 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
794 char *expr = NULL;
795 PyObject *py_attrs = Py_None;
796 PyObject *py_controls = Py_None;
797 const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
798 int ret;
799 struct ldb_result *res;
800 struct ldb_request *req;
801 const char **attrs;
802 struct ldb_context *ldb_ctx;
803 struct ldb_control **parsed_controls;
804 struct ldb_dn *base;
806 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
807 discard_const_p(char *, kwnames),
808 &py_base, &scope, &expr, &py_attrs, &py_controls))
809 return NULL;
811 ldb_ctx = PyLdb_AsLdbContext(self);
813 if (py_attrs == Py_None) {
814 attrs = NULL;
815 } else {
816 attrs = PyList_AsStringList(ldb_ctx, py_attrs);
817 if (attrs == NULL)
818 return NULL;
821 if (py_base == Py_None) {
822 base = ldb_get_default_basedn(ldb_ctx);
823 } else {
824 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base))
825 return NULL;
828 if (py_controls == Py_None) {
829 parsed_controls = NULL;
830 } else {
831 const char **controls = PyList_AsStringList(ldb_ctx, py_controls);
832 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
833 talloc_free(controls);
836 res = talloc_zero(ldb_ctx, struct ldb_result);
837 if (res == NULL) {
838 PyErr_NoMemory();
839 return NULL;
842 ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
843 base,
844 scope,
845 expr,
846 attrs,
847 parsed_controls,
848 res,
849 ldb_search_default_callback,
850 NULL);
852 if (ret != LDB_SUCCESS) {
853 talloc_free(res);
854 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb_ctx);
855 return NULL;
858 ret = ldb_request(ldb_ctx, req);
860 if (ret == LDB_SUCCESS) {
861 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
864 talloc_free(req);
866 if (ret != LDB_SUCCESS) {
867 talloc_free(res);
868 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb_ctx);
869 return NULL;
872 return PyLdbResult_FromResult(res);
875 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
877 char *name;
878 void *data;
880 if (!PyArg_ParseTuple(args, "s", &name))
881 return NULL;
883 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
885 if (data == NULL)
886 Py_RETURN_NONE;
888 /* FIXME: More interpretation */
890 return Py_True;
893 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
895 char *name;
896 PyObject *data;
898 if (!PyArg_ParseTuple(args, "sO", &name, &data))
899 return NULL;
901 /* FIXME: More interpretation */
903 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
905 Py_RETURN_NONE;
908 static PyObject *py_ldb_modules(PyLdbObject *self)
910 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
911 PyObject *ret = PyList_New(0);
912 struct ldb_module *mod;
914 for (mod = ldb->modules; mod; mod = mod->next) {
915 PyList_Append(ret, PyLdbModule_FromModule(mod));
918 return ret;
921 static PyMethodDef py_ldb_methods[] = {
922 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
923 "S.set_debug(callback) -> None\n"
924 "Set callback for LDB debug messages.\n"
925 "The callback should accept a debug level and debug text." },
926 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
927 "S.set_create_perms(mode) -> None\n"
928 "Set mode to use when creating new LDB files." },
929 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
930 "S.set_modules_dir(path) -> None\n"
931 "Set path LDB should search for modules" },
932 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
933 "S.transaction_start() -> None\n"
934 "Start a new transaction." },
935 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
936 "S.transaction_commit() -> None\n"
937 "commit a new transaction." },
938 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
939 "S.transaction_cancel() -> None\n"
940 "cancel a new transaction." },
941 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
942 NULL },
943 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
944 NULL },
945 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
946 NULL },
947 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
948 NULL },
949 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
950 NULL },
951 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
952 "S.connect(url, flags=0, options=None) -> None\n"
953 "Connect to a LDB URL." },
954 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
955 "S.modify(message) -> None\n"
956 "Modify an entry." },
957 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
958 "S.add(message) -> None\n"
959 "Add an entry." },
960 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
961 "S.delete(dn) -> None\n"
962 "Remove an entry." },
963 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
964 "S.rename(old_dn, new_dn) -> None\n"
965 "Rename an entry." },
966 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
967 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
968 "Search in a database.\n"
969 "\n"
970 ":param base: Optional base DN to search\n"
971 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
972 ":param expression: Optional search expression\n"
973 ":param attrs: Attributes to return (defaults to all)\n"
974 ":param controls: Optional list of controls\n"
975 ":return: Iterator over Message objects\n"
977 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
978 NULL },
979 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
980 NULL },
981 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
982 NULL },
983 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
984 "S.parse_ldif(ldif) -> iter(messages)\n"
985 "Parse a string formatted using LDIF." },
986 { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
987 "S.get_opaque(name) -> value\n"
988 "Get an opaque value set on this LDB connection. \n"
989 ":note: The returned value may not be useful in Python."
991 { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
992 "S.set_opaque(name, value) -> None\n"
993 "Set an opaque value on this LDB connection. \n"
994 ":note: Passing incorrect values may cause crashes." },
995 { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
996 "S.modules() -> list\n"
997 "Return the list of modules on this LDB connection " },
998 { NULL },
1001 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
1003 PyLdbModuleObject *ret;
1005 ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
1006 if (ret == NULL) {
1007 PyErr_NoMemory();
1008 return NULL;
1010 ret->mem_ctx = talloc_new(NULL);
1011 ret->mod = talloc_reference(ret->mem_ctx, mod);
1012 return (PyObject *)ret;
1015 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
1017 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
1020 static PyGetSetDef py_ldb_getset[] = {
1021 { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL },
1022 { NULL }
1025 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
1027 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1028 struct ldb_dn *dn;
1029 struct ldb_result *result;
1030 int ret;
1031 int count;
1033 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
1034 return -1;
1036 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
1037 if (ret != LDB_SUCCESS) {
1038 PyErr_SetLdbError(ret, ldb_ctx);
1039 return -1;
1042 count = result->count;
1044 talloc_free(result);
1046 return count;
1049 static PySequenceMethods py_ldb_seq = {
1050 .sq_contains = (objobjproc)py_ldb_contains,
1053 PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
1055 PyLdbObject *ret;
1057 ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1058 if (ret == NULL) {
1059 PyErr_NoMemory();
1060 return NULL;
1062 ret->mem_ctx = talloc_new(NULL);
1063 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1064 return (PyObject *)ret;
1067 static void py_ldb_dealloc(PyLdbObject *self)
1069 talloc_free(self->mem_ctx);
1070 self->ob_type->tp_free(self);
1073 PyTypeObject PyLdb = {
1074 .tp_name = "Ldb",
1075 .tp_methods = py_ldb_methods,
1076 .tp_repr = (reprfunc)py_ldb_repr,
1077 .tp_new = py_ldb_new,
1078 .tp_init = (initproc)py_ldb_init,
1079 .tp_dealloc = (destructor)py_ldb_dealloc,
1080 .tp_getset = py_ldb_getset,
1081 .tp_getattro = PyObject_GenericGetAttr,
1082 .tp_basicsize = sizeof(PyLdbObject),
1083 .tp_doc = "Connection to a LDB database.",
1084 .tp_as_sequence = &py_ldb_seq,
1085 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1088 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
1090 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1093 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
1095 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1098 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1100 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1101 Py_RETURN_NONE;
1104 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1106 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1107 Py_RETURN_NONE;
1110 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1112 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1113 Py_RETURN_NONE;
1116 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1118 PyObject *py_base, *py_tree, *py_attrs;
1119 int ret, scope;
1120 struct ldb_request *req;
1121 const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1122 struct ldb_module *mod;
1124 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
1125 discard_const_p(char *, kwnames),
1126 &py_base, &scope, &py_tree, &py_attrs))
1127 return NULL;
1129 mod = self->mod;
1131 ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base),
1132 scope, NULL /* expr */, py_attrs == Py_None?NULL:PyList_AsStringList(req, py_attrs),
1133 NULL /* controls */, NULL, NULL, NULL);
1134 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1136 ret = mod->ops->search(mod, req);
1137 talloc_free(req);
1139 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1141 return PyLdbResult_FromResult(req->op.search.res);
1145 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1147 struct ldb_request *req;
1148 PyObject *py_message;
1149 int ret;
1150 struct ldb_module *mod;
1152 if (!PyArg_ParseTuple(args, "O", &py_message))
1153 return NULL;
1155 req = talloc_zero(NULL, struct ldb_request);
1156 req->operation = LDB_ADD;
1157 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1159 mod = PyLdbModule_AsModule(self);
1160 ret = mod->ops->add(mod, req);
1162 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1164 Py_RETURN_NONE;
1167 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
1169 int ret;
1170 struct ldb_request *req;
1171 PyObject *py_message;
1172 struct ldb_module *mod;
1174 if (!PyArg_ParseTuple(args, "O", &py_message))
1175 return NULL;
1177 req = talloc_zero(NULL, struct ldb_request);
1178 req->operation = LDB_MODIFY;
1179 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1181 mod = PyLdbModule_AsModule(self);
1182 ret = mod->ops->modify(mod, req);
1184 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1186 Py_RETURN_NONE;
1189 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
1191 int ret;
1192 struct ldb_request *req;
1193 PyObject *py_dn;
1195 if (!PyArg_ParseTuple(args, "O", &py_dn))
1196 return NULL;
1198 req = talloc_zero(NULL, struct ldb_request);
1199 req->operation = LDB_DELETE;
1200 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1202 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1204 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1206 Py_RETURN_NONE;
1209 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1211 int ret;
1212 struct ldb_request *req;
1213 PyObject *py_dn1, *py_dn2;
1215 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1216 return NULL;
1218 req = talloc_zero(NULL, struct ldb_request);
1220 req->operation = LDB_RENAME;
1221 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1222 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1224 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1226 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1228 Py_RETURN_NONE;
1231 static PyMethodDef py_ldb_module_methods[] = {
1232 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1233 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1234 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1235 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1236 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1237 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1238 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1239 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1240 { NULL },
1243 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
1245 talloc_free(self->mem_ctx);
1246 self->ob_type->tp_free(self);
1249 PyTypeObject PyLdbModule = {
1250 .tp_name = "LdbModule",
1251 .tp_methods = py_ldb_module_methods,
1252 .tp_repr = (reprfunc)py_ldb_module_repr,
1253 .tp_str = (reprfunc)py_ldb_module_str,
1254 .tp_basicsize = sizeof(PyLdbModuleObject),
1255 .tp_dealloc = (destructor)py_ldb_module_dealloc,
1256 .tp_flags = Py_TPFLAGS_DEFAULT,
1259 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1260 PyObject *set_obj, int flags,
1261 const char *attr_name)
1263 struct ldb_message_element *me;
1265 if (PyLdbMessageElement_Check(set_obj))
1266 return PyLdbMessageElement_AsMessageElement(set_obj);
1268 me = talloc(mem_ctx, struct ldb_message_element);
1270 me->name = attr_name;
1271 me->flags = flags;
1272 if (PyString_Check(set_obj)) {
1273 me->num_values = 1;
1274 me->values = talloc_array(me, struct ldb_val, me->num_values);
1275 me->values[0].length = PyString_Size(set_obj);
1276 me->values[0].data = (uint8_t *)talloc_strndup(me->values,
1277 PyString_AsString(set_obj),
1278 me->values[0].length);
1279 } else if (PySequence_Check(set_obj)) {
1280 int i;
1281 me->num_values = PySequence_Size(set_obj);
1282 me->values = talloc_array(me, struct ldb_val, me->num_values);
1283 for (i = 0; i < me->num_values; i++) {
1284 PyObject *obj = PySequence_GetItem(set_obj, i);
1286 me->values[i].length = PyString_Size(obj);
1287 me->values[i].data = (uint8_t *)PyString_AsString(obj);
1289 } else {
1290 talloc_free(me);
1291 me = NULL;
1294 return me;
1298 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
1299 struct ldb_message_element *me)
1301 int i;
1302 PyObject *result;
1304 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1305 result = PyList_New(me->num_values);
1307 for (i = 0; i < me->num_values; i++) {
1308 PyList_SetItem(result, i,
1309 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1312 return result;
1315 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1317 int i;
1318 if (!PyArg_ParseTuple(args, "i", &i))
1319 return NULL;
1320 if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1321 Py_RETURN_NONE;
1323 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1324 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1327 static PyMethodDef py_ldb_msg_element_methods[] = {
1328 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1329 { NULL },
1332 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1334 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1337 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1339 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1340 if (idx < 0 || idx >= el->num_values) {
1341 PyErr_SetString(PyExc_IndexError, "Out of range");
1342 return NULL;
1344 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1347 static PySequenceMethods py_ldb_msg_element_seq = {
1348 .sq_length = (lenfunc)py_ldb_msg_element_len,
1349 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1352 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1354 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1355 PyLdbMessageElement_AsMessageElement(other));
1358 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1360 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1363 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
1365 PyLdbMessageElementObject *ret;
1366 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1367 if (ret == NULL) {
1368 PyErr_NoMemory();
1369 return NULL;
1371 ret->mem_ctx = talloc_new(NULL);
1372 if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1373 PyErr_NoMemory();
1374 return NULL;
1376 ret->el = el;
1377 return (PyObject *)ret;
1380 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1382 PyObject *py_elements = NULL;
1383 struct ldb_message_element *el;
1384 int flags = 0;
1385 char *name = NULL;
1386 const char * const kwnames[] = { "elements", "flags", "name", NULL };
1387 PyLdbMessageElementObject *ret;
1389 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois",
1390 discard_const_p(char *, kwnames),
1391 &py_elements, &flags, &name))
1392 return NULL;
1394 el = talloc_zero(NULL, struct ldb_message_element);
1396 if (py_elements != NULL) {
1397 int i;
1398 if (PyString_Check(py_elements)) {
1399 el->num_values = 1;
1400 el->values = talloc_array(el, struct ldb_val, 1);
1401 el->values[0].data = (uint8_t *)PyString_AsString(py_elements);
1402 el->values[0].length = PyString_Size(py_elements);
1403 } else if (PySequence_Check(py_elements)) {
1404 el->num_values = PySequence_Size(py_elements);
1405 el->values = talloc_array(el, struct ldb_val, el->num_values);
1406 for (i = 0; i < el->num_values; i++) {
1407 PyObject *item = PySequence_GetItem(py_elements, i);
1408 el->values[i].data = (uint8_t *)PyString_AsString(item);
1409 el->values[i].length = PyString_Size(item);
1411 } else {
1412 PyErr_SetString(PyExc_TypeError,
1413 "Expected string or list");
1414 talloc_free(el);
1415 return NULL;
1419 el->flags = flags;
1420 el->name = talloc_strdup(el, name);
1422 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1423 if (ret == NULL) {
1424 PyErr_NoMemory();
1425 talloc_free(el);
1426 return NULL;
1429 ret->mem_ctx = talloc_new(NULL);
1430 ret->el = talloc_reference(ret->mem_ctx, el);
1431 return (PyObject *)ret;
1434 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1436 char *element_str = NULL;
1437 int i;
1438 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1439 PyObject *ret;
1441 for (i = 0; i < el->num_values; i++) {
1442 PyObject *o = py_ldb_msg_element_find(self, i);
1443 if (element_str == NULL)
1444 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1445 else
1446 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1449 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1451 talloc_free(element_str);
1453 return ret;
1456 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
1458 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1460 if (el->num_values == 1)
1461 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1462 else
1463 Py_RETURN_NONE;
1466 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
1468 talloc_free(self->mem_ctx);
1469 self->ob_type->tp_free(self);
1472 PyTypeObject PyLdbMessageElement = {
1473 .tp_name = "MessageElement",
1474 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1475 .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1476 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1477 .tp_str = (reprfunc)py_ldb_msg_element_str,
1478 .tp_methods = py_ldb_msg_element_methods,
1479 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1480 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1481 .tp_as_sequence = &py_ldb_msg_element_seq,
1482 .tp_new = py_ldb_msg_element_new,
1483 .tp_flags = Py_TPFLAGS_DEFAULT,
1486 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
1488 char *name;
1489 if (!PyArg_ParseTuple(args, "s", &name))
1490 return NULL;
1492 ldb_msg_remove_attr(self->msg, name);
1494 Py_RETURN_NONE;
1497 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
1499 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1500 int i, j = 0;
1501 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1502 if (msg->dn != NULL) {
1503 PyList_SetItem(obj, j, PyString_FromString("dn"));
1504 j++;
1506 for (i = 0; i < msg->num_elements; i++) {
1507 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1508 j++;
1510 return obj;
1513 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
1515 struct ldb_message_element *el;
1516 char *name = PyString_AsString(py_name);
1517 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1518 if (!strcmp(name, "dn"))
1519 return PyLdbDn_FromDn(msg->dn);
1520 el = ldb_msg_find_element(msg, name);
1521 if (el == NULL) {
1522 return NULL;
1524 return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg);
1527 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
1529 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
1530 if (ret == NULL) {
1531 PyErr_SetString(PyExc_KeyError, "No such element");
1532 return NULL;
1534 return ret;
1537 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
1539 PyObject *name, *ret;
1540 if (!PyArg_ParseTuple(args, "O", &name))
1541 return NULL;
1543 ret = py_ldb_msg_getitem_helper(self, name);
1544 if (ret == NULL)
1545 Py_RETURN_NONE;
1546 return ret;
1549 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
1551 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1552 int i, j;
1553 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
1554 j = 0;
1555 if (msg->dn != NULL) {
1556 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
1557 j++;
1559 for (i = 0; i < msg->num_elements; i++, j++) {
1560 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], self->msg)));
1562 return l;
1565 static PyMethodDef py_ldb_msg_methods[] = {
1566 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
1567 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
1568 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
1569 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
1570 { NULL },
1573 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
1575 PyObject *list, *iter;
1577 list = py_ldb_msg_keys(self);
1578 iter = PyObject_GetIter(list);
1579 Py_DECREF(list);
1580 return iter;
1583 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
1585 char *attr_name = PyString_AsString(name);
1586 if (value == NULL) {
1587 ldb_msg_remove_attr(self->msg, attr_name);
1588 } else {
1589 struct ldb_message_element *el = PyObject_AsMessageElement(NULL,
1590 value, 0, attr_name);
1591 if (el == NULL)
1592 return -1;
1593 talloc_steal(self->msg, el);
1594 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
1595 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
1597 return 0;
1600 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
1602 return PyLdbMessage_AsMessage(self)->num_elements;
1605 static PyMappingMethods py_ldb_msg_mapping = {
1606 .mp_length = (lenfunc)py_ldb_msg_length,
1607 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
1608 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
1611 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1613 const char * const kwnames[] = { "dn", NULL };
1614 struct ldb_message *ret;
1615 PyObject *pydn = NULL;
1616 PyLdbMessageObject *py_ret;
1618 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
1619 discard_const_p(char *, kwnames),
1620 &pydn))
1621 return NULL;
1623 ret = ldb_msg_new(NULL);
1624 if (ret == NULL) {
1625 PyErr_NoMemory();
1626 return NULL;
1629 if (pydn != NULL)
1630 if (!PyObject_AsDn(NULL, pydn, NULL, &ret->dn))
1631 return NULL;
1633 py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
1634 if (py_ret == NULL) {
1635 PyErr_NoMemory();
1636 return NULL;
1639 py_ret->mem_ctx = talloc_new(NULL);
1640 py_ret->msg = talloc_reference(py_ret->mem_ctx, ret);
1641 return (PyObject *)py_ret;
1644 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
1646 PyLdbMessageObject *ret;
1648 ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
1649 if (ret == NULL) {
1650 PyErr_NoMemory();
1651 return NULL;
1653 ret->mem_ctx = talloc_new(NULL);
1654 ret->msg = talloc_reference(ret->mem_ctx, msg);
1655 return (PyObject *)ret;
1658 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
1660 return PyLdbDn_FromDn(PyLdbMessage_AsMessage(self)->dn);
1663 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
1665 PyLdbMessage_AsMessage(self)->dn = PyLdbDn_AsDn(value);
1666 return 0;
1669 static PyGetSetDef py_ldb_msg_getset[] = {
1670 { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
1671 { NULL }
1674 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
1676 PyObject *dict = PyDict_New(), *ret;
1677 if (PyDict_Update(dict, (PyObject *)self) != 0)
1678 return NULL;
1679 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
1680 Py_DECREF(dict);
1681 return ret;
1684 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
1686 talloc_free(self->mem_ctx);
1687 self->ob_type->tp_free(self);
1690 PyTypeObject PyLdbMessage = {
1691 .tp_name = "Message",
1692 .tp_methods = py_ldb_msg_methods,
1693 .tp_getset = py_ldb_msg_getset,
1694 .tp_as_mapping = &py_ldb_msg_mapping,
1695 .tp_basicsize = sizeof(PyLdbMessageObject),
1696 .tp_dealloc = (destructor)py_ldb_msg_dealloc,
1697 .tp_new = py_ldb_msg_new,
1698 .tp_repr = (reprfunc)py_ldb_msg_repr,
1699 .tp_flags = Py_TPFLAGS_DEFAULT,
1700 .tp_iter = (getiterfunc)py_ldb_msg_iter,
1703 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
1705 PyLdbTreeObject *ret;
1707 ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
1708 if (ret == NULL) {
1709 PyErr_NoMemory();
1710 return NULL;
1713 ret->mem_ctx = talloc_new(NULL);
1714 ret->tree = talloc_reference(ret->mem_ctx, tree);
1715 return (PyObject *)ret;
1718 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
1720 talloc_free(self->mem_ctx);
1721 self->ob_type->tp_free(self);
1724 PyTypeObject PyLdbTree = {
1725 .tp_name = "Tree",
1726 .tp_basicsize = sizeof(PyLdbTreeObject),
1727 .tp_dealloc = (destructor)py_ldb_tree_dealloc,
1728 .tp_flags = Py_TPFLAGS_DEFAULT,
1731 /* Ldb_module */
1732 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
1734 PyObject *py_ldb = (PyObject *)mod->private_data;
1735 PyObject *py_result, *py_base, *py_attrs, *py_tree;
1737 py_base = PyLdbDn_FromDn(req->op.search.base);
1739 if (py_base == NULL)
1740 return LDB_ERR_OPERATIONS_ERROR;
1742 py_tree = PyLdbTree_FromTree(req->op.search.tree);
1744 if (py_tree == NULL)
1745 return LDB_ERR_OPERATIONS_ERROR;
1747 if (req->op.search.attrs == NULL) {
1748 py_attrs = Py_None;
1749 } else {
1750 int i, len;
1751 for (len = 0; req->op.search.attrs[len]; len++);
1752 py_attrs = PyList_New(len);
1753 for (i = 0; i < len; i++)
1754 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1757 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
1758 discard_const_p(char, "OiOO"),
1759 py_base, req->op.search.scope, py_tree, py_attrs);
1761 Py_DECREF(py_attrs);
1762 Py_DECREF(py_tree);
1763 Py_DECREF(py_base);
1765 if (py_result == NULL) {
1766 return LDB_ERR_PYTHON_EXCEPTION;
1769 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
1770 if (req->op.search.res == NULL) {
1771 return LDB_ERR_PYTHON_EXCEPTION;
1774 Py_DECREF(py_result);
1776 return LDB_SUCCESS;
1779 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1781 PyObject *py_ldb = (PyObject *)mod->private_data;
1782 PyObject *py_result, *py_msg;
1784 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
1786 if (py_msg == NULL) {
1787 return LDB_ERR_OPERATIONS_ERROR;
1790 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
1791 discard_const_p(char, "O"),
1792 py_msg);
1794 Py_DECREF(py_msg);
1796 if (py_result == NULL) {
1797 return LDB_ERR_PYTHON_EXCEPTION;
1800 Py_DECREF(py_result);
1802 return LDB_SUCCESS;
1805 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1807 PyObject *py_ldb = (PyObject *)mod->private_data;
1808 PyObject *py_result, *py_msg;
1810 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
1812 if (py_msg == NULL) {
1813 return LDB_ERR_OPERATIONS_ERROR;
1816 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
1817 discard_const_p(char, "O"),
1818 py_msg);
1820 Py_DECREF(py_msg);
1822 if (py_result == NULL) {
1823 return LDB_ERR_PYTHON_EXCEPTION;
1826 Py_DECREF(py_result);
1828 return LDB_SUCCESS;
1831 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
1833 PyObject *py_ldb = (PyObject *)mod->private_data;
1834 PyObject *py_result, *py_dn;
1836 py_dn = PyLdbDn_FromDn(req->op.del.dn);
1838 if (py_dn == NULL)
1839 return LDB_ERR_OPERATIONS_ERROR;
1841 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
1842 discard_const_p(char, "O"),
1843 py_dn);
1845 if (py_result == NULL) {
1846 return LDB_ERR_PYTHON_EXCEPTION;
1849 Py_DECREF(py_result);
1851 return LDB_SUCCESS;
1854 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
1856 PyObject *py_ldb = (PyObject *)mod->private_data;
1857 PyObject *py_result, *py_olddn, *py_newdn;
1859 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
1861 if (py_olddn == NULL)
1862 return LDB_ERR_OPERATIONS_ERROR;
1864 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
1866 if (py_newdn == NULL)
1867 return LDB_ERR_OPERATIONS_ERROR;
1869 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
1870 discard_const_p(char, "OO"),
1871 py_olddn, py_newdn);
1873 Py_DECREF(py_olddn);
1874 Py_DECREF(py_newdn);
1876 if (py_result == NULL) {
1877 return LDB_ERR_PYTHON_EXCEPTION;
1880 Py_DECREF(py_result);
1882 return LDB_SUCCESS;
1885 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
1887 PyObject *py_ldb = (PyObject *)mod->private_data;
1888 PyObject *py_result;
1890 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
1891 discard_const_p(char, ""));
1893 return LDB_ERR_OPERATIONS_ERROR;
1896 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
1898 PyObject *py_ldb = (PyObject *)mod->private_data;
1899 PyObject *py_result;
1901 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
1902 discard_const_p(char, ""));
1904 return LDB_ERR_OPERATIONS_ERROR;
1907 static int py_module_start_transaction(struct ldb_module *mod)
1909 PyObject *py_ldb = (PyObject *)mod->private_data;
1910 PyObject *py_result;
1912 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
1913 discard_const_p(char, ""));
1915 if (py_result == NULL) {
1916 return LDB_ERR_PYTHON_EXCEPTION;
1919 Py_DECREF(py_result);
1921 return LDB_SUCCESS;
1924 static int py_module_end_transaction(struct ldb_module *mod)
1926 PyObject *py_ldb = (PyObject *)mod->private_data;
1927 PyObject *py_result;
1929 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
1930 discard_const_p(char, ""));
1932 if (py_result == NULL) {
1933 return LDB_ERR_PYTHON_EXCEPTION;
1936 Py_DECREF(py_result);
1938 return LDB_SUCCESS;
1941 static int py_module_del_transaction(struct ldb_module *mod)
1943 PyObject *py_ldb = (PyObject *)mod->private_data;
1944 PyObject *py_result;
1946 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
1947 discard_const_p(char, ""));
1949 if (py_result == NULL) {
1950 return LDB_ERR_PYTHON_EXCEPTION;
1953 Py_DECREF(py_result);
1955 return LDB_SUCCESS;
1958 static int py_module_destructor(struct ldb_module *mod)
1960 Py_DECREF((PyObject *)mod->private_data);
1961 return 0;
1964 static int py_module_init(struct ldb_module *mod)
1966 PyObject *py_class = (PyObject *)mod->ops->private_data;
1967 PyObject *py_result, *py_next, *py_ldb;
1969 py_ldb = PyLdb_FromLdbContext(mod->ldb);
1971 if (py_ldb == NULL)
1972 return LDB_ERR_OPERATIONS_ERROR;
1974 py_next = PyLdbModule_FromModule(mod->next);
1976 if (py_next == NULL)
1977 return LDB_ERR_OPERATIONS_ERROR;
1979 py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
1980 py_ldb, py_next);
1982 if (py_result == NULL) {
1983 return LDB_ERR_PYTHON_EXCEPTION;
1986 mod->private_data = py_result;
1988 talloc_set_destructor(mod, py_module_destructor);
1990 return ldb_next_init(mod);
1993 static PyObject *py_register_module(PyObject *module, PyObject *args)
1995 int ret;
1996 struct ldb_module_ops *ops;
1997 PyObject *input;
1999 if (!PyArg_ParseTuple(args, "O", &input))
2000 return NULL;
2002 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
2003 if (ops == NULL) {
2004 PyErr_NoMemory();
2005 return NULL;
2008 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name"))));
2010 Py_INCREF(input);
2011 ops->private_data = input;
2012 ops->init_context = py_module_init;
2013 ops->search = py_module_search;
2014 ops->add = py_module_add;
2015 ops->modify = py_module_modify;
2016 ops->del = py_module_del;
2017 ops->rename = py_module_rename;
2018 ops->request = py_module_request;
2019 ops->extended = py_module_extended;
2020 ops->start_transaction = py_module_start_transaction;
2021 ops->end_transaction = py_module_end_transaction;
2022 ops->del_transaction = py_module_del_transaction;
2024 ret = ldb_register_module(ops);
2026 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
2028 Py_RETURN_NONE;
2031 static PyObject *py_timestring(PyObject *module, PyObject *args)
2033 time_t t;
2034 char *tresult;
2035 PyObject *ret;
2036 if (!PyArg_ParseTuple(args, "L", &t))
2037 return NULL;
2038 tresult = ldb_timestring(NULL, t);
2039 ret = PyString_FromString(tresult);
2040 talloc_free(tresult);
2041 return ret;
2044 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
2046 char *str;
2047 if (!PyArg_ParseTuple(args, "s", &str))
2048 return NULL;
2050 return PyInt_FromLong(ldb_string_to_time(str));
2053 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
2055 char *name;
2056 if (!PyArg_ParseTuple(args, "s", &name))
2057 return NULL;
2058 return PyBool_FromLong(ldb_valid_attr_name(name));
2061 static PyMethodDef py_ldb_global_methods[] = {
2062 { "register_module", py_register_module, METH_VARARGS,
2063 "S.register_module(module) -> None\n"
2064 "Register a LDB module."},
2065 { "timestring", py_timestring, METH_VARARGS,
2066 "S.timestring(int) -> string\n"
2067 "Generate a LDAP time string from a UNIX timestamp" },
2068 { "string_to_time", py_string_to_time, METH_VARARGS,
2069 "S.string_to_time(string) -> int\n"
2070 "Parse a LDAP time string into a UNIX timestamp." },
2071 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2072 "S.valid_attr_name(name) -> bool\n"
2073 "Check whether the supplied name is a valid attribute name." },
2074 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2075 NULL },
2076 { NULL }
2079 void initldb(void)
2081 PyObject *m;
2083 if (PyType_Ready(&PyLdbDn) < 0)
2084 return;
2086 if (PyType_Ready(&PyLdbMessage) < 0)
2087 return;
2089 if (PyType_Ready(&PyLdbMessageElement) < 0)
2090 return;
2092 if (PyType_Ready(&PyLdb) < 0)
2093 return;
2095 if (PyType_Ready(&PyLdbModule) < 0)
2096 return;
2098 if (PyType_Ready(&PyLdbTree) < 0)
2099 return;
2101 m = Py_InitModule3("ldb", py_ldb_global_methods,
2102 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2103 if (m == NULL)
2104 return;
2106 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2107 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2108 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2109 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2111 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2112 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2113 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2114 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2116 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2117 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2118 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2119 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2120 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2121 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2122 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2123 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2124 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2125 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2126 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2127 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2128 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2129 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2130 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2131 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2132 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2133 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2134 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2135 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2136 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2137 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2138 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2139 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2140 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2141 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2142 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2143 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2144 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2145 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2146 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2147 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2148 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2149 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2150 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2151 PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2152 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2153 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2155 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2157 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2159 PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL);
2160 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2162 Py_INCREF(&PyLdb);
2163 Py_INCREF(&PyLdbDn);
2164 Py_INCREF(&PyLdbModule);
2165 Py_INCREF(&PyLdbMessage);
2166 Py_INCREF(&PyLdbMessageElement);
2167 Py_INCREF(&PyLdbTree);
2169 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2170 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2171 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2172 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2173 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2174 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);