s4:ldb initialise e->values[i].length before use in python bindings
[metze/samba/wip.git] / source4 / lib / ldb / pyldb.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Python interface to ldb.
5
6    Copyright (C) 2005,2006 Tim Potter <tpot@samba.org>
7    Copyright (C) 2006 Simo Sorce <idra@samba.org>
8    Copyright (C) 2007-2009 Jelmer Vernooij <jelmer@samba.org>
9
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
13
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.
18
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.
23
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/>.
26 */
27
28 #include "replace.h"
29 #include "ldb_private.h"
30 #include <Python.h>
31 #include "pyldb.h"
32
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
39
40 #ifndef Py_RETURN_NONE
41 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
42 #endif
43
44 static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx)
45 {
46         if (ret == LDB_ERR_PYTHON_EXCEPTION)
47                 return; /* Python exception should already be set, just keep that */
48
49         PyErr_SetObject(error, 
50                                         Py_BuildValue(discard_const_p(char, "(i,s)"), ret, 
51                                   ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
52 }
53
54 static PyObject *PyExc_LdbError;
55
56 PyAPI_DATA(PyTypeObject) PyLdbMessage;
57 PyAPI_DATA(PyTypeObject) PyLdbModule;
58 PyAPI_DATA(PyTypeObject) PyLdbDn;
59 PyAPI_DATA(PyTypeObject) PyLdb;
60 PyAPI_DATA(PyTypeObject) PyLdbMessageElement;
61 PyAPI_DATA(PyTypeObject) PyLdbTree;
62
63 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx, 
64                                                            struct ldb_message_element *el, 
65                                                            struct ldb_val *val)
66 {
67         struct ldb_val new_val;
68         TALLOC_CTX *mem_ctx = talloc_new(NULL);
69         PyObject *ret;
70
71         new_val = *val;
72
73         ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
74
75         talloc_free(mem_ctx);
76
77         return ret;
78 }
79
80 /**
81  * Obtain a ldb DN from a Python object.
82  *
83  * @param mem_ctx Memory context
84  * @param object Python object
85  * @param ldb_ctx LDB context
86  * @return Whether or not the conversion succeeded
87  */
88 bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, 
89                    struct ldb_context *ldb_ctx, struct ldb_dn **dn)
90 {
91         struct ldb_dn *odn;
92
93         if (ldb_ctx != NULL && PyString_Check(object)) {
94                 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
95                 *dn = odn;
96                 return true;
97         }
98
99         if (PyLdbDn_Check(object)) {
100                 *dn = PyLdbDn_AsDn(object);
101                 return true;
102         }
103
104         PyErr_SetString(PyExc_TypeError, "Expected DN");
105         return false;
106 }
107
108 /**
109  * Create a Python object from a ldb_result.
110  *
111  * @param result LDB result to convert
112  * @return Python object with converted result (a list object)
113  */
114 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
115 {
116         PyObject *ret;
117         int i;
118         if (result == NULL) {
119                 Py_RETURN_NONE;
120         } 
121         ret = PyList_New(result->count);
122         for (i = 0; i < result->count; i++) {
123                 PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i])
124                 );
125         }
126         return ret;
127 }
128
129 /**
130  * Create a LDB Result from a Python object. 
131  * If conversion fails, NULL will be returned and a Python exception set.
132  *
133  * @param mem_ctx Memory context in which to allocate the LDB Result
134  * @param obj Python object to convert
135  * @return a ldb_result, or NULL if the conversion failed
136  */
137 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx, 
138                                                                                            PyObject *obj)
139 {
140         struct ldb_result *res;
141         int i;
142
143         if (obj == Py_None)
144                 return NULL;
145
146         res = talloc_zero(mem_ctx, struct ldb_result);
147         res->count = PyList_Size(obj);
148         res->msgs = talloc_array(res, struct ldb_message *, res->count);
149         for (i = 0; i < res->count; i++) {
150                 PyObject *item = PyList_GetItem(obj, i);
151                 res->msgs[i] = PyLdbMessage_AsMessage(item);
152         }
153         return res;
154 }
155
156 static PyObject *py_ldb_dn_validate(PyLdbDnObject *self)
157 {
158         return PyBool_FromLong(ldb_dn_validate(self->dn));
159 }
160
161 static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self)
162 {
163         return PyBool_FromLong(ldb_dn_is_valid(self->dn));
164 }
165
166 static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self)
167 {
168         return PyBool_FromLong(ldb_dn_is_special(self->dn));
169 }
170
171 static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self)
172 {
173         return PyBool_FromLong(ldb_dn_is_null(self->dn));
174 }
175  
176 static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self)
177 {
178         return PyString_FromString(ldb_dn_get_casefold(self->dn));
179 }
180
181 static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self)
182 {
183         return PyString_FromString(ldb_dn_get_linearized(self->dn));
184 }
185
186 static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self)
187 {
188         return PyString_FromString(ldb_dn_canonical_string(self->dn, self->dn));
189 }
190
191 static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self)
192 {
193         return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn));
194 }
195
196 static PyObject *py_ldb_dn_repr(PyLdbDnObject *self)
197 {
198         return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn))));
199 }
200
201 static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args)
202 {
203         char *name;
204
205         if (!PyArg_ParseTuple(args, "s", &name))
206                 return NULL;
207
208         return ldb_dn_check_special(self->dn, name)?Py_True:Py_False;
209 }
210
211 static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
212 {
213         return ldb_dn_compare(dn1->dn, dn2->dn);
214 }
215
216 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
217 {
218         struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
219         struct ldb_dn *parent;
220         PyLdbDnObject *py_ret;
221         TALLOC_CTX *mem_ctx = talloc_new(NULL);
222
223         parent = ldb_dn_get_parent(mem_ctx, dn);
224         if (parent == NULL) {
225                 talloc_free(mem_ctx);
226                 Py_RETURN_NONE;
227         }
228
229         py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
230         if (py_ret == NULL) {
231                 PyErr_NoMemory();
232                 talloc_free(mem_ctx);
233                 return NULL;
234         }
235         py_ret->mem_ctx = mem_ctx;
236         py_ret->dn = parent;
237         return (PyObject *)py_ret;
238 }
239
240 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
241
242 static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
243 {
244         PyObject *py_other;
245         struct ldb_dn *dn, *other;
246         if (!PyArg_ParseTuple(args, "O", &py_other))
247                 return NULL;
248
249         dn = PyLdbDn_AsDn((PyObject *)self);
250
251         if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
252                 return NULL;
253
254         return ldb_dn_add_child(dn, other)?Py_True:Py_False;
255 }
256
257 static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
258 {
259         PyObject *py_other;
260         struct ldb_dn *other, *dn;
261         if (!PyArg_ParseTuple(args, "O", &py_other))
262                 return NULL;
263
264         dn = PyLdbDn_AsDn((PyObject *)self);
265
266         if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
267                 return NULL;
268
269         return ldb_dn_add_base(dn, other)?Py_True:Py_False;
270 }
271
272 static PyMethodDef py_ldb_dn_methods[] = {
273         { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS, 
274                 "S.validate() -> bool\n"
275                 "Validate DN is correct." },
276         { "is_valid", (PyCFunction)py_ldb_dn_is_valid, METH_NOARGS,
277                 "S.is_valid() -> bool\n" },
278         { "is_special", (PyCFunction)py_ldb_dn_is_special, METH_NOARGS,
279                 "S.is_special() -> bool\n"
280                 "Check whether this is a special LDB DN." },
281         { "is_null", (PyCFunction)py_ldb_dn_is_null, METH_NOARGS,
282                 "Check whether this is a null DN." },
283         { "get_casefold", (PyCFunction)py_ldb_dn_get_casefold, METH_NOARGS,
284                 NULL },
285         { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS,
286                 NULL },
287         { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
288                 "S.canonical_str() -> string\n"
289                 "Canonical version of this DN (like a posix path)." },
290         { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
291                 "S.canonical_ex_str() -> string\n"
292                 "Canonical version of this DN (like a posix path, with terminating newline)." },
293         { "check_special", (PyCFunction)py_ldb_dn_is_special, METH_VARARGS, 
294                 NULL },
295         { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS,
296                 "S.parent() -> dn\n"
297                 "Get the parent for this DN." },
298         { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS, 
299                 "S.add_child(dn) -> None\n"
300                 "Add a child DN to this DN." },
301         { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
302                 "S.add_base(dn) -> None\n"
303                 "Add a base DN to this DN." },
304         { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
305                 NULL },
306         { NULL }
307 };
308
309 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
310 {
311         return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
312 }
313
314 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
315 {
316         struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self), 
317                                   *other;
318         PyLdbDnObject *py_ret;
319         
320         if (!PyObject_AsDn(NULL, py_other, NULL, &other))
321                 return NULL;
322
323         py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
324         if (py_ret == NULL) {
325                 PyErr_NoMemory();
326                 return NULL;
327         }
328         py_ret->mem_ctx = talloc_new(NULL);
329         py_ret->dn = ldb_dn_copy(py_ret->mem_ctx, dn);
330         ldb_dn_add_child(py_ret->dn, other);
331         return (PyObject *)py_ret;
332 }
333
334 static PySequenceMethods py_ldb_dn_seq = {
335         .sq_length = (lenfunc)py_ldb_dn_len,
336         .sq_concat = (binaryfunc)py_ldb_dn_concat,
337 };
338
339 static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
340 {
341         struct ldb_dn *ret;
342         char *str;
343         PyObject *py_ldb;
344         struct ldb_context *ldb_ctx;
345         TALLOC_CTX *mem_ctx;
346         PyLdbDnObject *py_ret;
347         const char * const kwnames[] = { "ldb", "dn", NULL };
348
349         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os",
350                                          discard_const_p(char *, kwnames),
351                                          &py_ldb, &str))
352                 return NULL;
353
354         ldb_ctx = PyLdb_AsLdbContext(py_ldb);
355
356         mem_ctx = talloc_new(NULL);
357         if (mem_ctx == NULL) {
358                 PyErr_NoMemory();
359                 return NULL;
360         }
361
362         ret = ldb_dn_new(mem_ctx, ldb_ctx, str);
363
364         if (ret == NULL || !ldb_dn_validate(ret)) {
365                 talloc_free(mem_ctx);
366                 PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
367                 return NULL;
368         }
369
370         py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0);
371         if (ret == NULL) {
372                 talloc_free(mem_ctx);
373                 PyErr_NoMemory();
374                 return NULL;
375         }
376         py_ret->mem_ctx = mem_ctx;
377         py_ret->dn = ret;
378         return (PyObject *)py_ret;
379 }
380
381 PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
382 {
383         PyLdbDnObject *py_ret;
384
385         if (dn == NULL) {
386                 Py_RETURN_NONE;
387         }
388
389         py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
390         if (py_ret == NULL) {
391                 PyErr_NoMemory();
392                 return NULL;
393         }
394         py_ret->mem_ctx = talloc_new(NULL);
395         py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
396         return (PyObject *)py_ret;
397 }
398
399 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
400 {
401         talloc_free(self->mem_ctx);
402         self->ob_type->tp_free(self);
403 }
404
405 PyTypeObject PyLdbDn = {
406         .tp_name = "Dn",
407         .tp_methods = py_ldb_dn_methods,
408         .tp_str = (reprfunc)py_ldb_dn_get_linearized,
409         .tp_repr = (reprfunc)py_ldb_dn_repr,
410         .tp_compare = (cmpfunc)py_ldb_dn_compare,
411         .tp_as_sequence = &py_ldb_dn_seq,
412         .tp_doc = "A LDB distinguished name.",
413         .tp_new = py_ldb_dn_new,
414         .tp_dealloc = (destructor)py_ldb_dn_dealloc,
415         .tp_basicsize = sizeof(PyLdbObject),
416         .tp_flags = Py_TPFLAGS_DEFAULT,
417 };
418
419 /* Debug */
420 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
421 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
422 {
423         PyObject *fn = (PyObject *)context;
424         PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap));
425 }
426
427 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
428 {
429         PyObject *cb;
430
431         if (!PyArg_ParseTuple(args, "O", &cb))
432                 return NULL;
433
434         Py_INCREF(cb);
435         /* FIXME: Where do we DECREF cb ? */
436         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
437
438         Py_RETURN_NONE;
439 }
440
441 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
442 {
443         unsigned int perms;
444         if (!PyArg_ParseTuple(args, "I", &perms))
445                 return NULL;
446
447         ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
448
449         Py_RETURN_NONE;
450 }
451
452 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
453 {
454         char *modules_dir;
455         if (!PyArg_ParseTuple(args, "s", &modules_dir))
456                 return NULL;
457
458         ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
459
460         Py_RETURN_NONE;
461 }
462
463 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
464 {
465         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
466         Py_RETURN_NONE;
467 }
468
469 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
470 {
471         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
472         Py_RETURN_NONE;
473 }
474
475 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
476 {
477         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
478         Py_RETURN_NONE;
479 }
480
481 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
482 {
483         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
484         Py_RETURN_NONE;
485 }
486
487 static PyObject *py_ldb_repr(PyLdbObject *self)
488 {
489         return PyString_FromFormat("<ldb connection>");
490 }
491
492 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
493 {
494         struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
495         if (dn == NULL)
496                 Py_RETURN_NONE;
497         return PyLdbDn_FromDn(dn);
498 }
499
500
501 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
502 {
503         struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
504         if (dn == NULL)
505                 Py_RETURN_NONE;
506         return PyLdbDn_FromDn(dn);
507 }
508
509 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
510 {
511         struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
512         if (dn == NULL)
513                 Py_RETURN_NONE;
514         return PyLdbDn_FromDn(dn);
515 }
516
517 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
518 {
519         struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
520         if (dn == NULL)
521                 Py_RETURN_NONE;
522         return PyLdbDn_FromDn(dn);
523 }
524
525 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, 
526                                                                                 const char *paramname)
527 {
528         const char **ret;
529         int i;
530         if (!PyList_Check(list)) {
531                 PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
532                 return NULL;
533         }
534         ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
535         for (i = 0; i < PyList_Size(list); i++) {
536                 PyObject *item = PyList_GetItem(list, i);
537                 if (!PyString_Check(item)) {
538                         PyErr_Format(PyExc_TypeError, "%s should be strings", paramname);
539                         return NULL;
540                 }
541                 ret[i] = talloc_strndup(ret, PyString_AsString(item),
542                                                            PyString_Size(item));
543         }
544         ret[i] = NULL;
545         return ret;
546 }
547
548 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
549 {
550         const char * const kwnames[] = { "url", "flags", "options", NULL };
551         char *url = NULL;
552         PyObject *py_options = Py_None;
553         const char **options;
554         int flags = 0;
555         int ret;
556         struct ldb_context *ldb;
557
558         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__",
559                                          discard_const_p(char *, kwnames),
560                                          &url, &flags, &py_options))
561                 return -1;
562
563         ldb = PyLdb_AsLdbContext(self);
564
565         if (py_options == Py_None) {
566                 options = NULL;
567         } else {
568                 options = PyList_AsStringList(ldb, py_options, "options");
569                 if (options == NULL)
570                         return -1;
571         }
572
573         if (url != NULL) {
574                 ret = ldb_connect(ldb, url, flags, options);
575                 if (ret != LDB_SUCCESS) {
576                         PyErr_SetLdbError(PyExc_LdbError, ret, ldb);
577                         return -1;
578                 }
579         }
580
581         talloc_free(options);
582         return 0;
583 }
584
585 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
586 {
587         PyLdbObject *ret;
588         struct ldb_context *ldb;
589         ret = (PyLdbObject *)type->tp_alloc(type, 0);
590         if (ret == NULL) {
591                 PyErr_NoMemory();
592                 return NULL;
593         }
594         ret->mem_ctx = talloc_new(NULL);
595         ldb = ldb_init(ret->mem_ctx, NULL);
596
597         if (ldb == NULL) {
598                 PyErr_NoMemory();
599                 return NULL;
600         }
601
602         ret->ldb_ctx = ldb;
603         return (PyObject *)ret;
604 }
605
606 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
607 {
608         char *url;
609         int flags = 0;
610         PyObject *py_options = Py_None;
611         int ret;
612         const char **options;
613         const char * const kwnames[] = { "url", "flags", "options", NULL };
614
615         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO",
616                                          discard_const_p(char *, kwnames),
617                                          &url, &flags, &py_options))
618                 return NULL;
619
620         if (py_options == Py_None) {
621                 options = NULL;
622         } else {
623                 options = PyList_AsStringList(NULL, py_options, "options");
624                 if (options == NULL)
625                         return NULL;
626         }
627
628         ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
629         talloc_free(options);
630
631         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
632
633         Py_RETURN_NONE;
634 }
635
636 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
637 {
638         PyObject *py_msg;
639         int ret;
640         if (!PyArg_ParseTuple(args, "O", &py_msg))
641                 return NULL;
642
643         if (!PyLdbMessage_Check(py_msg)) {
644                 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
645                 return NULL;
646         }
647
648         ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
649         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
650
651         Py_RETURN_NONE;
652 }
653
654 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
655 {
656         PyObject *py_msg;
657         int ret;
658         Py_ssize_t dict_pos, msg_pos;
659         struct ldb_message_element *msgel;
660         struct ldb_message *msg;
661         PyObject *key, *value;
662         TALLOC_CTX *mem_ctx;
663
664         if (!PyArg_ParseTuple(args, "O", &py_msg))
665                 return NULL;
666
667         mem_ctx = talloc_new(NULL);
668
669         if (PyDict_Check(py_msg)) {
670                 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
671                 msg = ldb_msg_new(mem_ctx);
672                 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
673                 msg_pos = dict_pos = 0;
674                 if (dn_value) {
675                         if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
676                                 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
677                                 talloc_free(mem_ctx);
678                                 return NULL;
679                         }
680                         if (msg->dn == NULL) {
681                                 PyErr_SetString(PyExc_TypeError, "dn set but not found");
682                                 talloc_free(mem_ctx);
683                                 return NULL;
684                         }
685                 }
686
687                 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
688                         char *key_str = PyString_AsString(key);
689                         if (strcmp(key_str, "dn") != 0) {
690                                 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
691                                 if (msgel == NULL) {
692                                         PyErr_SetString(PyExc_TypeError, "unable to import element");
693                                         talloc_free(mem_ctx);
694                                         return NULL;
695                                 }
696                                 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
697                                 msg_pos++;
698                         }
699                 }
700
701                 if (msg->dn == NULL) {
702                         PyErr_SetString(PyExc_TypeError, "no dn set");
703                         talloc_free(mem_ctx);
704                         return NULL;
705                 }
706
707                 msg->num_elements = msg_pos;
708         } else {
709                 msg = PyLdbMessage_AsMessage(py_msg);
710         }
711
712         ret = ldb_add(PyLdb_AsLdbContext(self), msg);
713         talloc_free(mem_ctx);
714         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
715
716         Py_RETURN_NONE;
717 }
718
719 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
720 {
721         PyObject *py_dn;
722         struct ldb_dn *dn;
723         int ret;
724         struct ldb_context *ldb;
725         if (!PyArg_ParseTuple(args, "O", &py_dn))
726                 return NULL;
727
728         ldb = PyLdb_AsLdbContext(self);
729
730         if (!PyObject_AsDn(NULL, py_dn, ldb, &dn))
731                 return NULL;
732
733         ret = ldb_delete(ldb, dn);
734         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
735
736         Py_RETURN_NONE;
737 }
738
739 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
740 {
741         PyObject *py_dn1, *py_dn2;
742         struct ldb_dn *dn1, *dn2;
743         int ret;
744         struct ldb_context *ldb;
745         TALLOC_CTX *mem_ctx;
746         if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
747                 return NULL;
748
749         mem_ctx = talloc_new(NULL);
750         if (mem_ctx == NULL) {
751                 PyErr_NoMemory();
752                 return NULL;
753         }
754         ldb = PyLdb_AsLdbContext(self);
755         if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
756                 talloc_free(mem_ctx);
757                 return NULL;
758         }
759
760         if (!PyObject_AsDn(mem_ctx, py_dn2, ldb, &dn2)) {
761                 talloc_free(mem_ctx);
762                 return NULL;
763         }
764
765         ret = ldb_rename(ldb, dn1, dn2);
766         talloc_free(mem_ctx);
767         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
768
769         Py_RETURN_NONE;
770 }
771
772 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
773 {
774         char *name;
775         if (!PyArg_ParseTuple(args, "s", &name))
776                 return NULL;
777
778         ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
779
780         Py_RETURN_NONE;
781 }
782
783 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
784 {
785         char *attribute, *syntax;
786         unsigned int flags;
787         int ret;
788         if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
789                 return NULL;
790
791         ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
792
793         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
794
795         Py_RETURN_NONE;
796 }
797
798 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
799 {
800         if (ldif == NULL) {
801                 Py_RETURN_NONE;
802         } else {
803         /* We don't want this attached to the 'ldb' any more */
804                 return Py_BuildValue(discard_const_p(char, "(iO)"),
805                                      ldif->changetype,
806                                      PyLdbMessage_FromMessage(ldif->msg));
807         }
808 }
809
810
811 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
812 {
813         PyObject *list;
814         struct ldb_ldif *ldif;
815         const char *s;
816
817         TALLOC_CTX *mem_ctx;
818
819         if (!PyArg_ParseTuple(args, "s", &s))
820                 return NULL;
821
822         mem_ctx = talloc_new(NULL);
823         if (!mem_ctx) {
824                 Py_RETURN_NONE;
825         }
826
827         list = PyList_New(0);
828         while (s && *s != '\0') {
829                 ldif = ldb_ldif_read_string(self->ldb_ctx, &s);
830                 talloc_steal(mem_ctx, ldif);
831                 if (ldif) {
832                         PyList_Append(list, ldb_ldif_to_pyobject(ldif));
833                 } else {
834                         PyErr_SetString(PyExc_ValueError, "unable to parse ldif string");
835                         talloc_free(mem_ctx);
836                         return NULL;
837                 }
838         }
839         talloc_free(mem_ctx); /* The pyobject already has a reference to the things it needs */
840         return PyObject_GetIter(list);
841 }
842
843 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
844 {
845         const struct ldb_schema_attribute *a;
846         struct ldb_val old_val;
847         struct ldb_val new_val;
848         TALLOC_CTX *mem_ctx;
849         PyObject *ret;
850         char *element_name;
851         PyObject *val;
852
853         if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
854                 return NULL;
855
856         mem_ctx = talloc_new(NULL);
857
858         old_val.data = (uint8_t *)PyString_AsString(val);
859         old_val.length = PyString_Size(val);
860
861         a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
862
863         if (a == NULL) {
864                 Py_RETURN_NONE;
865         }
866
867         if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
868                 talloc_free(mem_ctx);
869                 Py_RETURN_NONE;
870         }
871
872         ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
873
874         talloc_free(mem_ctx);
875
876         return ret;
877 }
878
879 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
880 {
881         PyObject *py_base = Py_None;
882         enum ldb_scope scope = LDB_SCOPE_DEFAULT;
883         char *expr = NULL;
884         PyObject *py_attrs = Py_None;
885         PyObject *py_controls = Py_None;
886         const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
887         int ret;
888         struct ldb_result *res;
889         struct ldb_request *req;
890         const char **attrs;
891         struct ldb_context *ldb_ctx;
892         struct ldb_control **parsed_controls;
893         struct ldb_dn *base;
894         PyObject *py_ret;
895
896         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
897                                          discard_const_p(char *, kwnames),
898                                          &py_base, &scope, &expr, &py_attrs, &py_controls))
899                 return NULL;
900
901         ldb_ctx = PyLdb_AsLdbContext(self);
902
903         if (py_attrs == Py_None) {
904                 attrs = NULL;
905         } else {
906                 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
907                 if (attrs == NULL)
908                         return NULL;
909         }
910
911         if (py_base == Py_None) {
912                 base = ldb_get_default_basedn(ldb_ctx);
913         } else {
914                 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
915                         talloc_free(attrs);
916                         return NULL;
917                 }
918         }
919
920         if (py_controls == Py_None) {
921                 parsed_controls = NULL;
922         } else {
923                 const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
924                 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
925                 talloc_free(controls);
926         }
927
928         res = talloc_zero(ldb_ctx, struct ldb_result);
929         if (res == NULL) {
930                 PyErr_NoMemory();
931                 talloc_free(attrs);
932                 return NULL;
933         }
934
935         ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
936                                    base,
937                                    scope,
938                                    expr,
939                                    attrs,
940                                    parsed_controls,
941                                    res,
942                                    ldb_search_default_callback,
943                                    NULL);
944
945         talloc_steal(req, attrs);
946
947         if (ret != LDB_SUCCESS) {
948                 talloc_free(res);
949                 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
950                 return NULL;
951         }
952
953         ret = ldb_request(ldb_ctx, req);
954
955         if (ret == LDB_SUCCESS) {
956                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
957         }
958
959         talloc_free(req);
960
961         if (ret != LDB_SUCCESS) {
962                 talloc_free(res);
963                 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
964                 return NULL;
965         }
966
967         py_ret = PyLdbResult_FromResult(res);
968
969         talloc_free(res);
970
971         return py_ret;
972 }
973
974 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
975 {
976         char *name;
977         void *data;
978
979         if (!PyArg_ParseTuple(args, "s", &name))
980                 return NULL;
981
982         data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
983
984         if (data == NULL)
985                 Py_RETURN_NONE;
986
987         /* FIXME: More interpretation */
988
989         return Py_True;
990 }
991
992 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
993 {
994         char *name;
995         PyObject *data;
996
997         if (!PyArg_ParseTuple(args, "sO", &name, &data))
998                 return NULL;
999
1000         /* FIXME: More interpretation */
1001
1002         ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
1003
1004         Py_RETURN_NONE;
1005 }
1006
1007 static PyObject *py_ldb_modules(PyLdbObject *self)
1008 {
1009         struct ldb_context *ldb = PyLdb_AsLdbContext(self);
1010         PyObject *ret = PyList_New(0);
1011         struct ldb_module *mod;
1012
1013         for (mod = ldb->modules; mod; mod = mod->next) {
1014                 PyList_Append(ret, PyLdbModule_FromModule(mod));
1015         }
1016
1017         return ret;
1018 }
1019
1020 static PyMethodDef py_ldb_methods[] = {
1021         { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS, 
1022                 "S.set_debug(callback) -> None\n"
1023                 "Set callback for LDB debug messages.\n"
1024                 "The callback should accept a debug level and debug text." },
1025         { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS, 
1026                 "S.set_create_perms(mode) -> None\n"
1027                 "Set mode to use when creating new LDB files." },
1028         { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
1029                 "S.set_modules_dir(path) -> None\n"
1030                 "Set path LDB should search for modules" },
1031         { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS, 
1032                 "S.transaction_start() -> None\n"
1033                 "Start a new transaction." },
1034         { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS, 
1035                 "S.transaction_commit() -> None\n"
1036                 "commit a new transaction." },
1037         { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS, 
1038                 "S.transaction_cancel() -> None\n"
1039                 "cancel a new transaction." },
1040         { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS, 
1041                 NULL },
1042         { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
1043                 NULL },
1044         { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
1045                 NULL },
1046         { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
1047                 NULL },
1048         { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
1049                 NULL },
1050         { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS, 
1051                 "S.connect(url, flags=0, options=None) -> None\n"
1052                 "Connect to a LDB URL." },
1053         { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS, 
1054                 "S.modify(message) -> None\n"
1055                 "Modify an entry." },
1056         { "add", (PyCFunction)py_ldb_add, METH_VARARGS, 
1057                 "S.add(message) -> None\n"
1058                 "Add an entry." },
1059         { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
1060                 "S.delete(dn) -> None\n"
1061                 "Remove an entry." },
1062         { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
1063                 "S.rename(old_dn, new_dn) -> None\n"
1064                 "Rename an entry." },
1065         { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
1066                 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1067                 "Search in a database.\n"
1068                 "\n"
1069                 ":param base: Optional base DN to search\n"
1070                 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1071                 ":param expression: Optional search expression\n"
1072                 ":param attrs: Attributes to return (defaults to all)\n"
1073                 ":param controls: Optional list of controls\n"
1074                 ":return: Iterator over Message objects\n"
1075         },
1076         { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
1077                 NULL },
1078         { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
1079                 NULL },
1080         { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
1081                 NULL },
1082         { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
1083                 "S.parse_ldif(ldif) -> iter(messages)\n"
1084                 "Parse a string formatted using LDIF." },
1085         { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
1086                 "S.get_opaque(name) -> value\n"
1087                 "Get an opaque value set on this LDB connection. \n"
1088                 ":note: The returned value may not be useful in Python."
1089         },
1090         { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
1091                 "S.set_opaque(name, value) -> None\n"
1092                 "Set an opaque value on this LDB connection. \n"
1093                 ":note: Passing incorrect values may cause crashes." },
1094         { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
1095                 "S.modules() -> list\n"
1096                 "Return the list of modules on this LDB connection " },
1097         { NULL },
1098 };
1099
1100 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
1101 {
1102         PyLdbModuleObject *ret;
1103
1104         ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
1105         if (ret == NULL) {
1106                 PyErr_NoMemory();
1107                 return NULL;
1108         }
1109         ret->mem_ctx = talloc_new(NULL);
1110         ret->mod = talloc_reference(ret->mem_ctx, mod);
1111         return (PyObject *)ret;
1112 }
1113
1114 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
1115 {
1116         return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
1117 }
1118
1119 static PyGetSetDef py_ldb_getset[] = {
1120         { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL },
1121         { NULL }
1122 };
1123
1124 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
1125 {
1126         struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1127         struct ldb_dn *dn;
1128         struct ldb_result *result;
1129         int ret;
1130         int count;
1131
1132         if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
1133                 return -1;
1134
1135         ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
1136         if (ret != LDB_SUCCESS) {
1137                 PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
1138                 return -1;
1139         }
1140
1141         count = result->count;
1142
1143         talloc_free(result);
1144
1145         return count;
1146 }
1147
1148 static PySequenceMethods py_ldb_seq = {
1149         .sq_contains = (objobjproc)py_ldb_contains,
1150 };
1151
1152 PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
1153 {
1154         PyLdbObject *ret;
1155
1156         ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1157         if (ret == NULL) {
1158                 PyErr_NoMemory();
1159                 return NULL;
1160         }
1161         ret->mem_ctx = talloc_new(NULL);
1162         ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1163         return (PyObject *)ret;
1164 }
1165
1166 static void py_ldb_dealloc(PyLdbObject *self)
1167 {
1168         talloc_free(self->mem_ctx);
1169         self->ob_type->tp_free(self);
1170 }
1171
1172 PyTypeObject PyLdb = {
1173         .tp_name = "Ldb",
1174         .tp_methods = py_ldb_methods,
1175         .tp_repr = (reprfunc)py_ldb_repr,
1176         .tp_new = py_ldb_new,
1177         .tp_init = (initproc)py_ldb_init,
1178         .tp_dealloc = (destructor)py_ldb_dealloc,
1179         .tp_getset = py_ldb_getset,
1180         .tp_getattro = PyObject_GenericGetAttr,
1181         .tp_basicsize = sizeof(PyLdbObject),
1182         .tp_doc = "Connection to a LDB database.",
1183         .tp_as_sequence = &py_ldb_seq,
1184         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1185 };
1186
1187 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
1188 {
1189         return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1190 }
1191
1192 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
1193 {
1194         return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1195 }
1196
1197 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1198 {
1199         PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1200         Py_RETURN_NONE;
1201 }
1202
1203 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1204 {
1205         PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1206         Py_RETURN_NONE;
1207 }
1208
1209 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1210 {
1211         PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1212         Py_RETURN_NONE;
1213 }
1214
1215 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1216 {
1217         PyObject *py_base, *py_tree, *py_attrs, *py_ret;
1218         int ret, scope;
1219         struct ldb_request *req;
1220         const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1221         struct ldb_module *mod;
1222         const char * const*attrs;
1223
1224         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
1225                                          discard_const_p(char *, kwnames),
1226                                          &py_base, &scope, &py_tree, &py_attrs))
1227                 return NULL;
1228
1229         mod = self->mod;
1230
1231         if (py_attrs == Py_None) {
1232                 attrs = NULL;
1233         } else {
1234                 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
1235                 if (attrs == NULL)
1236                         return NULL;
1237         }
1238
1239         ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base), 
1240                              scope, NULL /* expr */, attrs,
1241                              NULL /* controls */, NULL, NULL, NULL);
1242
1243         talloc_steal(req, attrs);
1244
1245         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1246
1247         req->op.search.res = NULL;
1248
1249         ret = mod->ops->search(mod, req);
1250
1251         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1252
1253         py_ret = PyLdbResult_FromResult(req->op.search.res);
1254
1255         talloc_free(req);
1256
1257         return py_ret;  
1258 }
1259
1260
1261 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1262 {
1263         struct ldb_request *req;
1264         PyObject *py_message;
1265         int ret;
1266         struct ldb_module *mod;
1267
1268         if (!PyArg_ParseTuple(args, "O", &py_message))
1269                 return NULL;
1270
1271         req = talloc_zero(NULL, struct ldb_request);
1272         req->operation = LDB_ADD;
1273         req->op.add.message = PyLdbMessage_AsMessage(py_message);
1274
1275         mod = PyLdbModule_AsModule(self);
1276         ret = mod->ops->add(mod, req);
1277
1278         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1279
1280         Py_RETURN_NONE;
1281 }
1282
1283 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args) 
1284 {
1285         int ret;
1286         struct ldb_request *req;
1287         PyObject *py_message;
1288         struct ldb_module *mod;
1289
1290         if (!PyArg_ParseTuple(args, "O", &py_message))
1291                 return NULL;
1292
1293         req = talloc_zero(NULL, struct ldb_request);
1294         req->operation = LDB_MODIFY;
1295         req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1296
1297         mod = PyLdbModule_AsModule(self);
1298         ret = mod->ops->modify(mod, req);
1299
1300         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1301
1302         Py_RETURN_NONE;
1303 }
1304
1305 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args) 
1306 {
1307         int ret;
1308         struct ldb_request *req;
1309         PyObject *py_dn;
1310
1311         if (!PyArg_ParseTuple(args, "O", &py_dn))
1312                 return NULL;
1313
1314         req = talloc_zero(NULL, struct ldb_request);
1315         req->operation = LDB_DELETE;
1316         req->op.del.dn = PyLdbDn_AsDn(py_dn);
1317
1318         ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1319
1320         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1321
1322         Py_RETURN_NONE;
1323 }
1324
1325 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1326 {
1327         int ret;
1328         struct ldb_request *req;
1329         PyObject *py_dn1, *py_dn2;
1330
1331         if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1332                 return NULL;
1333
1334         req = talloc_zero(NULL, struct ldb_request);
1335
1336         req->operation = LDB_RENAME;
1337         req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1338         req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1339
1340         ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1341
1342         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1343
1344         Py_RETURN_NONE;
1345 }
1346
1347 static PyMethodDef py_ldb_module_methods[] = {
1348         { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1349         { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1350         { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1351         { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1352         { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1353         { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1354         { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1355         { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1356         { NULL },
1357 };
1358
1359 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
1360 {
1361         talloc_free(self->mem_ctx);
1362         self->ob_type->tp_free(self);
1363 }
1364
1365 PyTypeObject PyLdbModule = {
1366         .tp_name = "LdbModule",
1367         .tp_methods = py_ldb_module_methods,
1368         .tp_repr = (reprfunc)py_ldb_module_repr,
1369         .tp_str = (reprfunc)py_ldb_module_str,
1370         .tp_basicsize = sizeof(PyLdbModuleObject),
1371         .tp_dealloc = (destructor)py_ldb_module_dealloc,
1372         .tp_flags = Py_TPFLAGS_DEFAULT,
1373 };
1374
1375
1376 /**
1377  * Create a ldb_message_element from a Python object.
1378  *
1379  * This will accept any sequence objects that contains strings, or 
1380  * a string object.
1381  *
1382  * A reference to set_obj will be borrowed. 
1383  *
1384  * @param mem_ctx Memory context
1385  * @param set_obj Python object to convert
1386  * @param flags ldb_message_element flags to set
1387  * @param attr_name Name of the attribute
1388  * @return New ldb_message_element, allocated as child of mem_ctx
1389  */
1390 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1391                                                                                            PyObject *set_obj, int flags,
1392                                                                                            const char *attr_name)
1393 {
1394         struct ldb_message_element *me;
1395
1396         if (PyLdbMessageElement_Check(set_obj))
1397                 return PyLdbMessageElement_AsMessageElement(set_obj);
1398
1399         me = talloc(mem_ctx, struct ldb_message_element);
1400
1401         me->name = talloc_strdup(me, attr_name);
1402         me->flags = flags;
1403         if (PyString_Check(set_obj)) {
1404                 me->num_values = 1;
1405                 me->values = talloc_array(me, struct ldb_val, me->num_values);
1406                 me->values[0].length = PyString_Size(set_obj);
1407                 me->values[0].data = talloc_memdup(me, 
1408                         (uint8_t *)PyString_AsString(set_obj), me->values[0].length);
1409         } else if (PySequence_Check(set_obj)) {
1410                 int i;
1411                 me->num_values = PySequence_Size(set_obj);
1412                 me->values = talloc_array(me, struct ldb_val, me->num_values);
1413                 for (i = 0; i < me->num_values; i++) {
1414                         PyObject *obj = PySequence_GetItem(set_obj, i);
1415
1416                         me->values[i].length = PyString_Size(obj);
1417                         me->values[i].data = talloc_memdup(me, 
1418                                 (uint8_t *)PyString_AsString(obj), me->values[i].length);
1419                 }
1420         } else {
1421                 talloc_free(me);
1422                 me = NULL;
1423         }
1424
1425         return me;
1426 }
1427
1428
1429 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx, 
1430                                                                  struct ldb_message_element *me)
1431 {
1432         int i;
1433         PyObject *result;
1434
1435         /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1436         result = PyList_New(me->num_values);
1437
1438         for (i = 0; i < me->num_values; i++) {
1439                 PyList_SetItem(result, i,
1440                         PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1441         }
1442
1443         return result;
1444 }
1445
1446 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1447 {
1448         int i;
1449         if (!PyArg_ParseTuple(args, "i", &i))
1450                 return NULL;
1451         if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1452                 Py_RETURN_NONE;
1453
1454         return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self), 
1455                                                                  &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1456 }
1457
1458 static PyMethodDef py_ldb_msg_element_methods[] = {
1459         { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1460         { NULL },
1461 };
1462
1463 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1464 {
1465         return PyLdbMessageElement_AsMessageElement(self)->num_values;
1466 }
1467
1468 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1469 {
1470         struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1471         if (idx < 0 || idx >= el->num_values) {
1472                 PyErr_SetString(PyExc_IndexError, "Out of range");
1473                 return NULL;
1474         }
1475         return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1476 }
1477
1478 static PySequenceMethods py_ldb_msg_element_seq = {
1479         .sq_length = (lenfunc)py_ldb_msg_element_len,
1480         .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1481 };
1482
1483 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1484 {
1485         return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self), 
1486                                                                    PyLdbMessageElement_AsMessageElement(other));
1487 }
1488
1489 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1490 {
1491         return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1492 }
1493
1494 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
1495 {
1496         PyLdbMessageElementObject *ret;
1497         ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1498         if (ret == NULL) {
1499                 PyErr_NoMemory();
1500                 return NULL;
1501         }
1502         ret->mem_ctx = talloc_new(NULL);
1503         if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1504                 PyErr_NoMemory();
1505                 return NULL;
1506         }
1507         ret->el = el;
1508         return (PyObject *)ret;
1509 }
1510
1511 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1512 {
1513         PyObject *py_elements = NULL;
1514         struct ldb_message_element *el;
1515         int flags = 0;
1516         char *name = NULL;
1517         const char * const kwnames[] = { "elements", "flags", "name", NULL };
1518         PyLdbMessageElementObject *ret;
1519         TALLOC_CTX *mem_ctx;
1520
1521         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois",
1522                                          discard_const_p(char *, kwnames),
1523                                          &py_elements, &flags, &name))
1524                 return NULL;
1525
1526         mem_ctx = talloc_new(NULL);
1527         if (mem_ctx == NULL) {
1528                 PyErr_NoMemory();
1529                 return NULL;
1530         }
1531
1532         el = talloc_zero(mem_ctx, struct ldb_message_element);
1533
1534         if (py_elements != NULL) {
1535                 int i;
1536                 if (PyString_Check(py_elements)) {
1537                         el->num_values = 1;
1538                         el->values = talloc_array(el, struct ldb_val, 1);
1539                         el->values[0].length = PyString_Size(py_elements);
1540                         el->values[0].data = talloc_memdup(el, 
1541                                 (uint8_t *)PyString_AsString(py_elements), el->values[0].length);
1542                 } else if (PySequence_Check(py_elements)) {
1543                         el->num_values = PySequence_Size(py_elements);
1544                         el->values = talloc_array(el, struct ldb_val, el->num_values);
1545                         for (i = 0; i < el->num_values; i++) {
1546                                 PyObject *item = PySequence_GetItem(py_elements, i);
1547                                 el->values[i].length = PyString_Size(item);
1548                                 el->values[i].data = talloc_memdup(el, 
1549                                         (uint8_t *)PyString_AsString(item), el->values[i].length);
1550                         }
1551                 } else {
1552                         PyErr_SetString(PyExc_TypeError, 
1553                                         "Expected string or list");
1554                         talloc_free(mem_ctx);
1555                         return NULL;
1556                 }
1557         }
1558
1559         el->flags = flags;
1560         el->name = talloc_strdup(el, name);
1561
1562         ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1563         if (ret == NULL) {
1564                 PyErr_NoMemory();
1565                 talloc_free(mem_ctx);
1566                 return NULL;
1567         }
1568
1569         ret->mem_ctx = mem_ctx;
1570         ret->el = el;
1571         return (PyObject *)ret;
1572 }
1573
1574 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1575 {
1576         char *element_str = NULL;
1577         int i;
1578         struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1579         PyObject *ret;
1580
1581         for (i = 0; i < el->num_values; i++) {
1582                 PyObject *o = py_ldb_msg_element_find(self, i);
1583                 if (element_str == NULL)
1584                         element_str = talloc_strdup(NULL, PyObject_REPR(o));
1585                 else
1586                         element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1587         }
1588
1589         ret = PyString_FromFormat("MessageElement([%s])", element_str);
1590
1591         talloc_free(element_str);
1592
1593         return ret;
1594 }
1595
1596 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
1597 {
1598         struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1599
1600         if (el->num_values == 1)
1601                 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1602         else 
1603                 Py_RETURN_NONE;
1604 }
1605
1606 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
1607 {
1608         talloc_free(self->mem_ctx);
1609         self->ob_type->tp_free(self);
1610 }
1611
1612 PyTypeObject PyLdbMessageElement = {
1613         .tp_name = "MessageElement",
1614         .tp_basicsize = sizeof(PyLdbMessageElementObject),
1615         .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1616         .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1617         .tp_str = (reprfunc)py_ldb_msg_element_str,
1618         .tp_methods = py_ldb_msg_element_methods,
1619         .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1620         .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1621         .tp_as_sequence = &py_ldb_msg_element_seq,
1622         .tp_new = py_ldb_msg_element_new,
1623         .tp_flags = Py_TPFLAGS_DEFAULT,
1624 };
1625
1626 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
1627 {
1628         char *name;
1629         if (!PyArg_ParseTuple(args, "s", &name))
1630                 return NULL;
1631
1632         ldb_msg_remove_attr(self->msg, name);
1633
1634         Py_RETURN_NONE;
1635 }
1636
1637 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
1638 {
1639         struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1640         int i, j = 0;
1641         PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1642         if (msg->dn != NULL) {
1643                 PyList_SetItem(obj, j, PyString_FromString("dn"));
1644                 j++;
1645         }
1646         for (i = 0; i < msg->num_elements; i++) {
1647                 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1648                 j++;
1649         }
1650         return obj;
1651 }
1652
1653 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
1654 {
1655         struct ldb_message_element *el;
1656         char *name = PyString_AsString(py_name);
1657         struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1658         if (!strcmp(name, "dn"))
1659                 return PyLdbDn_FromDn(msg->dn);
1660         el = ldb_msg_find_element(msg, name);
1661         if (el == NULL) {
1662                 return NULL;
1663         }
1664         return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg);
1665 }
1666
1667 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
1668 {
1669         PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
1670         if (ret == NULL) {
1671                 PyErr_SetString(PyExc_KeyError, "No such element");
1672                 return NULL;
1673         }
1674         return ret;
1675 }
1676
1677 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
1678 {
1679         PyObject *name, *ret;
1680         if (!PyArg_ParseTuple(args, "O", &name))
1681                 return NULL;
1682
1683         ret = py_ldb_msg_getitem_helper(self, name);
1684         if (ret == NULL)
1685                 Py_RETURN_NONE;
1686         return ret;
1687 }
1688
1689 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
1690 {
1691         struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1692         int i, j;
1693         PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
1694         j = 0;
1695         if (msg->dn != NULL) {
1696                 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
1697                 j++;
1698         }
1699         for (i = 0; i < msg->num_elements; i++, j++) {
1700                 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], self->msg)));
1701         }
1702         return l;
1703 }
1704
1705 static PyMethodDef py_ldb_msg_methods[] = { 
1706         { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
1707         { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
1708         { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
1709         { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
1710         { NULL },
1711 };
1712
1713 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
1714 {
1715         PyObject *list, *iter;
1716
1717         list = py_ldb_msg_keys(self);
1718         iter = PyObject_GetIter(list);
1719         Py_DECREF(list);
1720         return iter;
1721 }
1722
1723 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
1724 {
1725         char *attr_name;
1726
1727         if (!PyString_Check(name)) {
1728                 PyErr_SetNone(PyExc_TypeError);
1729                 return -1;
1730         }
1731         
1732         attr_name = PyString_AsString(name);
1733         if (value == NULL) {
1734                 /* delitem */
1735                 ldb_msg_remove_attr(self->msg, attr_name);
1736         } else {
1737                 struct ldb_message_element *el = PyObject_AsMessageElement(self->msg,
1738                                                                                         value, 0, attr_name);
1739                 if (el == NULL)
1740                         return -1;
1741                 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
1742                 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
1743         }
1744         return 0;
1745 }
1746
1747 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
1748 {
1749         return PyLdbMessage_AsMessage(self)->num_elements;
1750 }
1751
1752 static PyMappingMethods py_ldb_msg_mapping = {
1753         .mp_length = (lenfunc)py_ldb_msg_length,
1754         .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
1755         .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
1756 };
1757
1758 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1759 {
1760         const char * const kwnames[] = { "dn", NULL };
1761         struct ldb_message *ret;
1762         TALLOC_CTX *mem_ctx;
1763         PyObject *pydn = NULL;
1764         PyLdbMessageObject *py_ret;
1765
1766         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
1767                                          discard_const_p(char *, kwnames),
1768                                          &pydn))
1769                 return NULL;
1770
1771         mem_ctx = talloc_new(NULL);
1772         if (mem_ctx == NULL) {
1773                 PyErr_NoMemory();
1774                 return NULL;
1775         }
1776
1777         ret = ldb_msg_new(mem_ctx);
1778         if (ret == NULL) {
1779                 talloc_free(mem_ctx);
1780                 PyErr_NoMemory();
1781                 return NULL;
1782         }
1783
1784         if (pydn != NULL) {
1785                 struct ldb_dn *dn;
1786                 if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) {
1787                         talloc_free(mem_ctx);
1788                         return NULL;
1789                 }
1790                 ret->dn = talloc_reference(ret, dn);
1791         }
1792
1793         py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
1794         if (py_ret == NULL) {
1795                 PyErr_NoMemory();
1796                 talloc_free(mem_ctx);
1797                 return NULL;
1798         }
1799
1800         py_ret->mem_ctx = mem_ctx;
1801         py_ret->msg = ret;
1802         return (PyObject *)py_ret;
1803 }
1804
1805 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
1806 {
1807         PyLdbMessageObject *ret;
1808
1809         ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
1810         if (ret == NULL) {
1811                 PyErr_NoMemory();
1812                 return NULL;
1813         }
1814         ret->mem_ctx = talloc_new(NULL);
1815         ret->msg = talloc_reference(ret->mem_ctx, msg);
1816         return (PyObject *)ret;
1817 }
1818
1819 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
1820 {
1821         struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1822         return PyLdbDn_FromDn(msg->dn);
1823 }
1824
1825 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
1826 {
1827         struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1828         if (!PyLdbDn_Check(value)) {
1829                 PyErr_SetNone(PyExc_TypeError);
1830                 return -1;
1831         }
1832
1833         msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value));
1834         return 0;
1835 }
1836
1837 static PyGetSetDef py_ldb_msg_getset[] = {
1838         { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
1839         { NULL }
1840 };
1841
1842 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
1843 {
1844         PyObject *dict = PyDict_New(), *ret;
1845         if (PyDict_Update(dict, (PyObject *)self) != 0)
1846                 return NULL;
1847         ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
1848         Py_DECREF(dict);
1849         return ret;
1850 }
1851
1852 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
1853 {
1854         talloc_free(self->mem_ctx);
1855         self->ob_type->tp_free(self);
1856 }
1857
1858 PyTypeObject PyLdbMessage = {
1859         .tp_name = "Message",
1860         .tp_methods = py_ldb_msg_methods,
1861         .tp_getset = py_ldb_msg_getset,
1862         .tp_as_mapping = &py_ldb_msg_mapping,
1863         .tp_basicsize = sizeof(PyLdbMessageObject),
1864         .tp_dealloc = (destructor)py_ldb_msg_dealloc,
1865         .tp_new = py_ldb_msg_new,
1866         .tp_repr = (reprfunc)py_ldb_msg_repr,
1867         .tp_flags = Py_TPFLAGS_DEFAULT,
1868         .tp_iter = (getiterfunc)py_ldb_msg_iter,
1869 };
1870
1871 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
1872 {
1873         PyLdbTreeObject *ret;
1874
1875         ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
1876         if (ret == NULL) {
1877                 PyErr_NoMemory();
1878                 return NULL;
1879         }
1880
1881         ret->mem_ctx = talloc_new(NULL);
1882         ret->tree = talloc_reference(ret->mem_ctx, tree);
1883         return (PyObject *)ret;
1884 }
1885
1886 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
1887 {
1888         talloc_free(self->mem_ctx);
1889         self->ob_type->tp_free(self);
1890 }
1891
1892 PyTypeObject PyLdbTree = {
1893         .tp_name = "Tree",
1894         .tp_basicsize = sizeof(PyLdbTreeObject),
1895         .tp_dealloc = (destructor)py_ldb_tree_dealloc,
1896         .tp_flags = Py_TPFLAGS_DEFAULT,
1897 };
1898
1899 /* Ldb_module */
1900 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
1901 {
1902         PyObject *py_ldb = (PyObject *)mod->private_data;
1903         PyObject *py_result, *py_base, *py_attrs, *py_tree;
1904
1905         py_base = PyLdbDn_FromDn(req->op.search.base);
1906
1907         if (py_base == NULL)
1908                 return LDB_ERR_OPERATIONS_ERROR;
1909
1910         py_tree = PyLdbTree_FromTree(req->op.search.tree);
1911
1912         if (py_tree == NULL)
1913                 return LDB_ERR_OPERATIONS_ERROR;
1914
1915         if (req->op.search.attrs == NULL) {
1916                 py_attrs = Py_None;
1917         } else {
1918                 int i, len;
1919                 for (len = 0; req->op.search.attrs[len]; len++);
1920                 py_attrs = PyList_New(len);
1921                 for (i = 0; i < len; i++)
1922                         PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1923         }
1924
1925         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
1926                                         discard_const_p(char, "OiOO"),
1927                                         py_base, req->op.search.scope, py_tree, py_attrs);
1928
1929         Py_DECREF(py_attrs);
1930         Py_DECREF(py_tree);
1931         Py_DECREF(py_base);
1932
1933         if (py_result == NULL) {
1934                 return LDB_ERR_PYTHON_EXCEPTION;
1935         }
1936
1937         req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
1938         if (req->op.search.res == NULL) {
1939                 return LDB_ERR_PYTHON_EXCEPTION;
1940         }
1941
1942         Py_DECREF(py_result);
1943
1944         return LDB_SUCCESS;
1945 }
1946
1947 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1948 {
1949         PyObject *py_ldb = (PyObject *)mod->private_data;
1950         PyObject *py_result, *py_msg;
1951
1952         py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
1953
1954         if (py_msg == NULL) {
1955                 return LDB_ERR_OPERATIONS_ERROR;
1956         }
1957
1958         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
1959                                         discard_const_p(char, "O"),
1960                                         py_msg);
1961
1962         Py_DECREF(py_msg);
1963
1964         if (py_result == NULL) {
1965                 return LDB_ERR_PYTHON_EXCEPTION;
1966         }
1967
1968         Py_DECREF(py_result);
1969
1970         return LDB_SUCCESS;
1971 }
1972
1973 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1974 {
1975         PyObject *py_ldb = (PyObject *)mod->private_data;
1976         PyObject *py_result, *py_msg;
1977
1978         py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
1979
1980         if (py_msg == NULL) {
1981                 return LDB_ERR_OPERATIONS_ERROR;
1982         }
1983
1984         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
1985                                         discard_const_p(char, "O"),
1986                                         py_msg);
1987
1988         Py_DECREF(py_msg);
1989
1990         if (py_result == NULL) {
1991                 return LDB_ERR_PYTHON_EXCEPTION;
1992         }
1993
1994         Py_DECREF(py_result);
1995
1996         return LDB_SUCCESS;
1997 }
1998
1999 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
2000 {
2001         PyObject *py_ldb = (PyObject *)mod->private_data;
2002         PyObject *py_result, *py_dn;
2003
2004         py_dn = PyLdbDn_FromDn(req->op.del.dn);
2005
2006         if (py_dn == NULL)
2007                 return LDB_ERR_OPERATIONS_ERROR;
2008
2009         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
2010                                         discard_const_p(char, "O"),
2011                                         py_dn);
2012
2013         if (py_result == NULL) {
2014                 return LDB_ERR_PYTHON_EXCEPTION;
2015         }
2016
2017         Py_DECREF(py_result);
2018
2019         return LDB_SUCCESS;
2020 }
2021
2022 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
2023 {
2024         PyObject *py_ldb = (PyObject *)mod->private_data;
2025         PyObject *py_result, *py_olddn, *py_newdn;
2026
2027         py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
2028
2029         if (py_olddn == NULL)
2030                 return LDB_ERR_OPERATIONS_ERROR;
2031
2032         py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
2033
2034         if (py_newdn == NULL)
2035                 return LDB_ERR_OPERATIONS_ERROR;
2036
2037         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
2038                                         discard_const_p(char, "OO"),
2039                                         py_olddn, py_newdn);
2040
2041         Py_DECREF(py_olddn);
2042         Py_DECREF(py_newdn);
2043
2044         if (py_result == NULL) {
2045                 return LDB_ERR_PYTHON_EXCEPTION;
2046         }
2047
2048         Py_DECREF(py_result);
2049
2050         return LDB_SUCCESS;
2051 }
2052
2053 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
2054 {
2055         PyObject *py_ldb = (PyObject *)mod->private_data;
2056         PyObject *py_result;
2057
2058         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
2059                                         discard_const_p(char, ""));
2060
2061         return LDB_ERR_OPERATIONS_ERROR;
2062 }
2063
2064 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
2065 {
2066         PyObject *py_ldb = (PyObject *)mod->private_data;
2067         PyObject *py_result;
2068
2069         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
2070                                         discard_const_p(char, ""));
2071
2072         return LDB_ERR_OPERATIONS_ERROR;
2073 }
2074
2075 static int py_module_start_transaction(struct ldb_module *mod)
2076 {
2077         PyObject *py_ldb = (PyObject *)mod->private_data;
2078         PyObject *py_result;
2079
2080         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
2081                                         discard_const_p(char, ""));
2082
2083         if (py_result == NULL) {
2084                 return LDB_ERR_PYTHON_EXCEPTION;
2085         }
2086
2087         Py_DECREF(py_result);
2088
2089         return LDB_SUCCESS;
2090 }
2091
2092 static int py_module_end_transaction(struct ldb_module *mod)
2093 {
2094         PyObject *py_ldb = (PyObject *)mod->private_data;
2095         PyObject *py_result;
2096
2097         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
2098                                         discard_const_p(char, ""));
2099
2100         if (py_result == NULL) {
2101                 return LDB_ERR_PYTHON_EXCEPTION;
2102         }
2103
2104         Py_DECREF(py_result);
2105
2106         return LDB_SUCCESS;
2107 }
2108
2109 static int py_module_del_transaction(struct ldb_module *mod)
2110 {
2111         PyObject *py_ldb = (PyObject *)mod->private_data;
2112         PyObject *py_result;
2113
2114         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
2115                                         discard_const_p(char, ""));
2116
2117         if (py_result == NULL) {
2118                 return LDB_ERR_PYTHON_EXCEPTION;
2119         }
2120
2121         Py_DECREF(py_result);
2122
2123         return LDB_SUCCESS;
2124 }
2125
2126 static int py_module_destructor(struct ldb_module *mod)
2127 {
2128         Py_DECREF((PyObject *)mod->private_data);
2129         return 0;
2130 }
2131
2132 static int py_module_init(struct ldb_module *mod)
2133 {
2134         PyObject *py_class = (PyObject *)mod->ops->private_data;
2135         PyObject *py_result, *py_next, *py_ldb;
2136
2137         py_ldb = PyLdb_FromLdbContext(mod->ldb);
2138
2139         if (py_ldb == NULL)
2140                 return LDB_ERR_OPERATIONS_ERROR;
2141
2142         py_next = PyLdbModule_FromModule(mod->next);
2143
2144         if (py_next == NULL)
2145                 return LDB_ERR_OPERATIONS_ERROR;
2146
2147         py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
2148                                           py_ldb, py_next);
2149
2150         if (py_result == NULL) {
2151                 return LDB_ERR_PYTHON_EXCEPTION;
2152         }
2153
2154         mod->private_data = py_result;
2155
2156         talloc_set_destructor(mod, py_module_destructor);
2157
2158         return ldb_next_init(mod);
2159 }
2160
2161 static PyObject *py_register_module(PyObject *module, PyObject *args)
2162 {
2163         int ret;
2164         struct ldb_module_ops *ops;
2165         PyObject *input;
2166
2167         if (!PyArg_ParseTuple(args, "O", &input))
2168                 return NULL;
2169
2170         ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
2171         if (ops == NULL) {
2172                 PyErr_NoMemory();
2173                 return NULL;
2174         }
2175
2176         ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name"))));
2177
2178         Py_INCREF(input);
2179         ops->private_data = input;
2180         ops->init_context = py_module_init;
2181         ops->search = py_module_search;
2182         ops->add = py_module_add;
2183         ops->modify = py_module_modify;
2184         ops->del = py_module_del;
2185         ops->rename = py_module_rename;
2186         ops->request = py_module_request;
2187         ops->extended = py_module_extended;
2188         ops->start_transaction = py_module_start_transaction;
2189         ops->end_transaction = py_module_end_transaction;
2190         ops->del_transaction = py_module_del_transaction;
2191
2192         ret = ldb_register_module(ops);
2193
2194         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
2195
2196         Py_RETURN_NONE;
2197 }
2198
2199 static PyObject *py_timestring(PyObject *module, PyObject *args)
2200 {
2201         time_t t;
2202         char *tresult;
2203         PyObject *ret;
2204         if (!PyArg_ParseTuple(args, "L", &t))
2205                 return NULL;
2206         tresult = ldb_timestring(NULL, t);
2207         ret = PyString_FromString(tresult);
2208         talloc_free(tresult);
2209         return ret;
2210 }
2211
2212 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
2213 {
2214         char *str;
2215         if (!PyArg_ParseTuple(args, "s", &str))
2216                 return NULL;
2217
2218         return PyInt_FromLong(ldb_string_to_time(str));
2219 }
2220
2221 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
2222 {
2223         char *name;
2224         if (!PyArg_ParseTuple(args, "s", &name))
2225                 return NULL;
2226         return PyBool_FromLong(ldb_valid_attr_name(name));
2227 }
2228
2229 static PyMethodDef py_ldb_global_methods[] = {
2230         { "register_module", py_register_module, METH_VARARGS, 
2231                 "S.register_module(module) -> None\n"
2232                 "Register a LDB module."},
2233         { "timestring", py_timestring, METH_VARARGS, 
2234                 "S.timestring(int) -> string\n"
2235                 "Generate a LDAP time string from a UNIX timestamp" },
2236         { "string_to_time", py_string_to_time, METH_VARARGS,
2237                 "S.string_to_time(string) -> int\n"
2238                 "Parse a LDAP time string into a UNIX timestamp." },
2239         { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2240                 "S.valid_attr_name(name) -> bool\n"
2241                 "Check whether the supplied name is a valid attribute name." },
2242         { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2243                 NULL },
2244         { NULL }
2245 };
2246
2247 void initldb(void)
2248 {
2249         PyObject *m;
2250
2251         if (PyType_Ready(&PyLdbDn) < 0)
2252                 return;
2253
2254         if (PyType_Ready(&PyLdbMessage) < 0)
2255                 return;
2256
2257         if (PyType_Ready(&PyLdbMessageElement) < 0)
2258                 return;
2259
2260         if (PyType_Ready(&PyLdb) < 0)
2261                 return;
2262
2263         if (PyType_Ready(&PyLdbModule) < 0)
2264                 return;
2265
2266         if (PyType_Ready(&PyLdbTree) < 0)
2267                 return;
2268
2269         m = Py_InitModule3("ldb", py_ldb_global_methods, 
2270                 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2271         if (m == NULL)
2272                 return;
2273
2274         PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2275         PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2276         PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2277         PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2278
2279         PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2280         PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2281         PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2282         PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2283
2284         PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2285         PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2286         PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2287         PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2288         PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2289         PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2290         PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2291         PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2292         PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2293         PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2294         PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2295         PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2296         PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2297         PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2298         PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2299         PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2300         PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2301         PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2302         PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2303         PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2304         PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2305         PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2306         PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2307         PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2308         PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2309         PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2310         PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2311         PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2312         PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2313         PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2314         PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2315         PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2316         PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2317         PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2318         PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2319         PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2320         PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2321         PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2322
2323         PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2324
2325         PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2326
2327         PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL);
2328         PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2329
2330         Py_INCREF(&PyLdb);
2331         Py_INCREF(&PyLdbDn);
2332         Py_INCREF(&PyLdbModule);
2333         Py_INCREF(&PyLdbMessage);
2334         Py_INCREF(&PyLdbMessageElement);
2335         Py_INCREF(&PyLdbTree);
2336
2337         PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2338         PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2339         PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2340         PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2341         PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2342         PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);
2343 }