pyldb: Raise proper exception when attempting to assign a string to a dn
[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] = PyString_AsString(item);
542         }
543         ret[i] = NULL;
544         return ret;
545 }
546
547 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
548 {
549         const char * const kwnames[] = { "url", "flags", "options", NULL };
550         char *url = NULL;
551         PyObject *py_options = Py_None;
552         const char **options;
553         int flags = 0;
554         int ret;
555         struct ldb_context *ldb;
556
557         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__",
558                                          discard_const_p(char *, kwnames),
559                                          &url, &flags, &py_options))
560                 return -1;
561
562         ldb = PyLdb_AsLdbContext(self);
563
564         if (py_options == Py_None) {
565                 options = NULL;
566         } else {
567                 options = PyList_AsStringList(ldb, py_options, "options");
568                 if (options == NULL)
569                         return -1;
570         }
571
572         if (url != NULL) {
573                 ret = ldb_connect(ldb, url, flags, options);
574                 if (ret != LDB_SUCCESS) {
575                         PyErr_SetLdbError(PyExc_LdbError, ret, ldb);
576                         return -1;
577                 }
578         }
579
580         talloc_free(options);
581         return 0;
582 }
583
584 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
585 {
586         PyLdbObject *ret;
587         struct ldb_context *ldb;
588         ret = (PyLdbObject *)type->tp_alloc(type, 0);
589         if (ret == NULL) {
590                 PyErr_NoMemory();
591                 return NULL;
592         }
593         ret->mem_ctx = talloc_new(NULL);
594         ldb = ldb_init(ret->mem_ctx, NULL);
595
596         if (ldb == NULL) {
597                 PyErr_NoMemory();
598                 return NULL;
599         }
600
601         ret->ldb_ctx = ldb;
602         return (PyObject *)ret;
603 }
604
605 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
606 {
607         char *url;
608         int flags = 0;
609         PyObject *py_options = Py_None;
610         int ret;
611         const char **options;
612         const char * const kwnames[] = { "url", "flags", "options", NULL };
613
614         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO",
615                                          discard_const_p(char *, kwnames),
616                                          &url, &flags, &py_options))
617                 return NULL;
618
619         if (py_options == Py_None) {
620                 options = NULL;
621         } else {
622                 options = PyList_AsStringList(NULL, py_options, "options");
623                 if (options == NULL)
624                         return NULL;
625         }
626
627         ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
628         talloc_free(options);
629
630         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
631
632         Py_RETURN_NONE;
633 }
634
635 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
636 {
637         PyObject *py_msg;
638         int ret;
639         if (!PyArg_ParseTuple(args, "O", &py_msg))
640                 return NULL;
641
642         if (!PyLdbMessage_Check(py_msg)) {
643                 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
644                 return NULL;
645         }
646
647         ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
648         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
649
650         Py_RETURN_NONE;
651 }
652
653 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
654 {
655         PyObject *py_msg;
656         int ret;
657         Py_ssize_t dict_pos, msg_pos;
658         struct ldb_message_element *msgel;
659         struct ldb_message *msg;
660         PyObject *key, *value;
661         TALLOC_CTX *mem_ctx;
662
663         if (!PyArg_ParseTuple(args, "O", &py_msg))
664                 return NULL;
665
666         mem_ctx = talloc_new(NULL);
667
668         if (PyDict_Check(py_msg)) {
669                 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
670                 msg = ldb_msg_new(mem_ctx);
671                 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
672                 msg_pos = dict_pos = 0;
673                 if (dn_value) {
674                         if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
675                                 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
676                                 talloc_free(mem_ctx);
677                                 return NULL;
678                         }
679                         if (msg->dn == NULL) {
680                                 PyErr_SetString(PyExc_TypeError, "dn set but not found");
681                                 talloc_free(mem_ctx);
682                                 return NULL;
683                         }
684                 }
685
686                 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
687                         char *key_str = PyString_AsString(key);
688                         if (strcmp(key_str, "dn") != 0) {
689                                 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
690                                 if (msgel == NULL) {
691                                         PyErr_SetString(PyExc_TypeError, "unable to import element");
692                                         talloc_free(mem_ctx);
693                                         return NULL;
694                                 }
695                                 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
696                                 msg_pos++;
697                         }
698                 }
699
700                 if (msg->dn == NULL) {
701                         PyErr_SetString(PyExc_TypeError, "no dn set");
702                         talloc_free(mem_ctx);
703                         return NULL;
704                 }
705
706                 msg->num_elements = msg_pos;
707         } else {
708                 msg = PyLdbMessage_AsMessage(py_msg);
709         }
710
711         ret = ldb_add(PyLdb_AsLdbContext(self), msg);
712         talloc_free(mem_ctx);
713         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
714
715         Py_RETURN_NONE;
716 }
717
718 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
719 {
720         PyObject *py_dn;
721         struct ldb_dn *dn;
722         int ret;
723         struct ldb_context *ldb;
724         if (!PyArg_ParseTuple(args, "O", &py_dn))
725                 return NULL;
726
727         ldb = PyLdb_AsLdbContext(self);
728
729         if (!PyObject_AsDn(NULL, py_dn, ldb, &dn))
730                 return NULL;
731
732         ret = ldb_delete(ldb, dn);
733         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
734
735         Py_RETURN_NONE;
736 }
737
738 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
739 {
740         PyObject *py_dn1, *py_dn2;
741         struct ldb_dn *dn1, *dn2;
742         int ret;
743         struct ldb_context *ldb;
744         TALLOC_CTX *mem_ctx;
745         if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
746                 return NULL;
747
748         mem_ctx = talloc_new(NULL);
749         if (mem_ctx == NULL) {
750                 PyErr_NoMemory();
751                 return NULL;
752         }
753         ldb = PyLdb_AsLdbContext(self);
754         if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
755                 talloc_free(mem_ctx);
756                 return NULL;
757         }
758
759         if (!PyObject_AsDn(mem_ctx, py_dn2, ldb, &dn2)) {
760                 talloc_free(mem_ctx);
761                 return NULL;
762         }
763
764         ret = ldb_rename(ldb, dn1, dn2);
765         talloc_free(mem_ctx);
766         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
767
768         Py_RETURN_NONE;
769 }
770
771 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
772 {
773         char *name;
774         if (!PyArg_ParseTuple(args, "s", &name))
775                 return NULL;
776
777         ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
778
779         Py_RETURN_NONE;
780 }
781
782 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
783 {
784         char *attribute, *syntax;
785         unsigned int flags;
786         int ret;
787         if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
788                 return NULL;
789
790         ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
791
792         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
793
794         Py_RETURN_NONE;
795 }
796
797 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
798 {
799         if (ldif == NULL) {
800                 Py_RETURN_NONE;
801         } else {
802         /* We don't want this attached to the 'ldb' any more */
803                 return Py_BuildValue(discard_const_p(char, "(iO)"),
804                                      ldif->changetype,
805                                      PyLdbMessage_FromMessage(ldif->msg));
806         }
807 }
808
809
810 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
811 {
812         PyObject *list;
813         struct ldb_ldif *ldif;
814         const char *s;
815
816         TALLOC_CTX *mem_ctx;
817
818         if (!PyArg_ParseTuple(args, "s", &s))
819                 return NULL;
820
821         mem_ctx = talloc_new(NULL);
822         if (!mem_ctx) {
823                 Py_RETURN_NONE;
824         }
825
826         list = PyList_New(0);
827         while (s && *s != '\0') {
828                 ldif = ldb_ldif_read_string(self->ldb_ctx, &s);
829                 talloc_steal(mem_ctx, ldif);
830                 if (ldif) {
831                         PyList_Append(list, ldb_ldif_to_pyobject(ldif));
832                 } else {
833                         PyErr_SetString(PyExc_ValueError, "unable to parse ldif string");
834                         talloc_free(mem_ctx);
835                         return NULL;
836                 }
837         }
838         talloc_free(mem_ctx); /* The pyobject already has a reference to the things it needs */
839         return PyObject_GetIter(list);
840 }
841
842 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
843 {
844         const struct ldb_schema_attribute *a;
845         struct ldb_val old_val;
846         struct ldb_val new_val;
847         TALLOC_CTX *mem_ctx;
848         PyObject *ret;
849         char *element_name;
850         PyObject *val;
851
852         if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
853                 return NULL;
854
855         mem_ctx = talloc_new(NULL);
856
857         old_val.data = (uint8_t *)PyString_AsString(val);
858         old_val.length = PyString_Size(val);
859
860         a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
861
862         if (a == NULL) {
863                 Py_RETURN_NONE;
864         }
865
866         if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
867                 talloc_free(mem_ctx);
868                 Py_RETURN_NONE;
869         }
870
871         ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
872
873         talloc_free(mem_ctx);
874
875         return ret;
876 }
877
878 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
879 {
880         PyObject *py_base = Py_None;
881         enum ldb_scope scope = LDB_SCOPE_DEFAULT;
882         char *expr = NULL;
883         PyObject *py_attrs = Py_None;
884         PyObject *py_controls = Py_None;
885         const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
886         int ret;
887         struct ldb_result *res;
888         struct ldb_request *req;
889         const char **attrs;
890         struct ldb_context *ldb_ctx;
891         struct ldb_control **parsed_controls;
892         struct ldb_dn *base;
893         PyObject *py_ret;
894
895         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
896                                          discard_const_p(char *, kwnames),
897                                          &py_base, &scope, &expr, &py_attrs, &py_controls))
898                 return NULL;
899
900         ldb_ctx = PyLdb_AsLdbContext(self);
901
902         if (py_attrs == Py_None) {
903                 attrs = NULL;
904         } else {
905                 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
906                 if (attrs == NULL)
907                         return NULL;
908         }
909
910         if (py_base == Py_None) {
911                 base = ldb_get_default_basedn(ldb_ctx);
912         } else {
913                 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
914                         talloc_free(attrs);
915                         return NULL;
916                 }
917         }
918
919         if (py_controls == Py_None) {
920                 parsed_controls = NULL;
921         } else {
922                 const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
923                 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
924                 talloc_free(controls);
925         }
926
927         res = talloc_zero(ldb_ctx, struct ldb_result);
928         if (res == NULL) {
929                 PyErr_NoMemory();
930                 talloc_free(attrs);
931                 return NULL;
932         }
933
934         ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
935                                    base,
936                                    scope,
937                                    expr,
938                                    attrs,
939                                    parsed_controls,
940                                    res,
941                                    ldb_search_default_callback,
942                                    NULL);
943
944         talloc_steal(req, attrs);
945
946         if (ret != LDB_SUCCESS) {
947                 talloc_free(res);
948                 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
949                 return NULL;
950         }
951
952         ret = ldb_request(ldb_ctx, req);
953
954         if (ret == LDB_SUCCESS) {
955                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
956         }
957
958         talloc_free(req);
959
960         if (ret != LDB_SUCCESS) {
961                 talloc_free(res);
962                 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
963                 return NULL;
964         }
965
966         py_ret = PyLdbResult_FromResult(res);
967
968         talloc_free(res);
969
970         return py_ret;
971 }
972
973 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
974 {
975         char *name;
976         void *data;
977
978         if (!PyArg_ParseTuple(args, "s", &name))
979                 return NULL;
980
981         data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
982
983         if (data == NULL)
984                 Py_RETURN_NONE;
985
986         /* FIXME: More interpretation */
987
988         return Py_True;
989 }
990
991 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
992 {
993         char *name;
994         PyObject *data;
995
996         if (!PyArg_ParseTuple(args, "sO", &name, &data))
997                 return NULL;
998
999         /* FIXME: More interpretation */
1000
1001         ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
1002
1003         Py_RETURN_NONE;
1004 }
1005
1006 static PyObject *py_ldb_modules(PyLdbObject *self)
1007 {
1008         struct ldb_context *ldb = PyLdb_AsLdbContext(self);
1009         PyObject *ret = PyList_New(0);
1010         struct ldb_module *mod;
1011
1012         for (mod = ldb->modules; mod; mod = mod->next) {
1013                 PyList_Append(ret, PyLdbModule_FromModule(mod));
1014         }
1015
1016         return ret;
1017 }
1018
1019 static PyMethodDef py_ldb_methods[] = {
1020         { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS, 
1021                 "S.set_debug(callback) -> None\n"
1022                 "Set callback for LDB debug messages.\n"
1023                 "The callback should accept a debug level and debug text." },
1024         { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS, 
1025                 "S.set_create_perms(mode) -> None\n"
1026                 "Set mode to use when creating new LDB files." },
1027         { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
1028                 "S.set_modules_dir(path) -> None\n"
1029                 "Set path LDB should search for modules" },
1030         { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS, 
1031                 "S.transaction_start() -> None\n"
1032                 "Start a new transaction." },
1033         { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS, 
1034                 "S.transaction_commit() -> None\n"
1035                 "commit a new transaction." },
1036         { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS, 
1037                 "S.transaction_cancel() -> None\n"
1038                 "cancel a new transaction." },
1039         { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS, 
1040                 NULL },
1041         { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
1042                 NULL },
1043         { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
1044                 NULL },
1045         { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
1046                 NULL },
1047         { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
1048                 NULL },
1049         { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS, 
1050                 "S.connect(url, flags=0, options=None) -> None\n"
1051                 "Connect to a LDB URL." },
1052         { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS, 
1053                 "S.modify(message) -> None\n"
1054                 "Modify an entry." },
1055         { "add", (PyCFunction)py_ldb_add, METH_VARARGS, 
1056                 "S.add(message) -> None\n"
1057                 "Add an entry." },
1058         { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
1059                 "S.delete(dn) -> None\n"
1060                 "Remove an entry." },
1061         { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
1062                 "S.rename(old_dn, new_dn) -> None\n"
1063                 "Rename an entry." },
1064         { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
1065                 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1066                 "Search in a database.\n"
1067                 "\n"
1068                 ":param base: Optional base DN to search\n"
1069                 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1070                 ":param expression: Optional search expression\n"
1071                 ":param attrs: Attributes to return (defaults to all)\n"
1072                 ":param controls: Optional list of controls\n"
1073                 ":return: Iterator over Message objects\n"
1074         },
1075         { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
1076                 NULL },
1077         { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
1078                 NULL },
1079         { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
1080                 NULL },
1081         { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
1082                 "S.parse_ldif(ldif) -> iter(messages)\n"
1083                 "Parse a string formatted using LDIF." },
1084         { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
1085                 "S.get_opaque(name) -> value\n"
1086                 "Get an opaque value set on this LDB connection. \n"
1087                 ":note: The returned value may not be useful in Python."
1088         },
1089         { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
1090                 "S.set_opaque(name, value) -> None\n"
1091                 "Set an opaque value on this LDB connection. \n"
1092                 ":note: Passing incorrect values may cause crashes." },
1093         { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
1094                 "S.modules() -> list\n"
1095                 "Return the list of modules on this LDB connection " },
1096         { NULL },
1097 };
1098
1099 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
1100 {
1101         PyLdbModuleObject *ret;
1102
1103         ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
1104         if (ret == NULL) {
1105                 PyErr_NoMemory();
1106                 return NULL;
1107         }
1108         ret->mem_ctx = talloc_new(NULL);
1109         ret->mod = talloc_reference(ret->mem_ctx, mod);
1110         return (PyObject *)ret;
1111 }
1112
1113 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
1114 {
1115         return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
1116 }
1117
1118 static PyGetSetDef py_ldb_getset[] = {
1119         { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL },
1120         { NULL }
1121 };
1122
1123 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
1124 {
1125         struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1126         struct ldb_dn *dn;
1127         struct ldb_result *result;
1128         int ret;
1129         int count;
1130
1131         if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
1132                 return -1;
1133
1134         ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
1135         if (ret != LDB_SUCCESS) {
1136                 PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
1137                 return -1;
1138         }
1139
1140         count = result->count;
1141
1142         talloc_free(result);
1143
1144         return count;
1145 }
1146
1147 static PySequenceMethods py_ldb_seq = {
1148         .sq_contains = (objobjproc)py_ldb_contains,
1149 };
1150
1151 PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
1152 {
1153         PyLdbObject *ret;
1154
1155         ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1156         if (ret == NULL) {
1157                 PyErr_NoMemory();
1158                 return NULL;
1159         }
1160         ret->mem_ctx = talloc_new(NULL);
1161         ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1162         return (PyObject *)ret;
1163 }
1164
1165 static void py_ldb_dealloc(PyLdbObject *self)
1166 {
1167         talloc_free(self->mem_ctx);
1168         self->ob_type->tp_free(self);
1169 }
1170
1171 PyTypeObject PyLdb = {
1172         .tp_name = "Ldb",
1173         .tp_methods = py_ldb_methods,
1174         .tp_repr = (reprfunc)py_ldb_repr,
1175         .tp_new = py_ldb_new,
1176         .tp_init = (initproc)py_ldb_init,
1177         .tp_dealloc = (destructor)py_ldb_dealloc,
1178         .tp_getset = py_ldb_getset,
1179         .tp_getattro = PyObject_GenericGetAttr,
1180         .tp_basicsize = sizeof(PyLdbObject),
1181         .tp_doc = "Connection to a LDB database.",
1182         .tp_as_sequence = &py_ldb_seq,
1183         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1184 };
1185
1186 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
1187 {
1188         return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1189 }
1190
1191 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
1192 {
1193         return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1194 }
1195
1196 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
1197 {
1198         PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1199         Py_RETURN_NONE;
1200 }
1201
1202 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
1203 {
1204         PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1205         Py_RETURN_NONE;
1206 }
1207
1208 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
1209 {
1210         PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1211         Py_RETURN_NONE;
1212 }
1213
1214 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
1215 {
1216         PyObject *py_base, *py_tree, *py_attrs, *py_ret;
1217         int ret, scope;
1218         struct ldb_request *req;
1219         const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1220         struct ldb_module *mod;
1221         const char * const*attrs;
1222
1223         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
1224                                          discard_const_p(char *, kwnames),
1225                                          &py_base, &scope, &py_tree, &py_attrs))
1226                 return NULL;
1227
1228         mod = self->mod;
1229
1230         if (py_attrs == Py_None) {
1231                 attrs = NULL;
1232         } else {
1233                 attrs = PyList_AsStringList(NULL, py_attrs, "attrs");
1234                 if (attrs == NULL)
1235                         return NULL;
1236         }
1237
1238         ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base), 
1239                              scope, NULL /* expr */, attrs,
1240                              NULL /* controls */, NULL, NULL, NULL);
1241
1242         talloc_steal(req, attrs);
1243
1244         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1245
1246         req->op.search.res = NULL;
1247
1248         ret = mod->ops->search(mod, req);
1249
1250         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1251
1252         py_ret = PyLdbResult_FromResult(req->op.search.res);
1253
1254         talloc_free(req);
1255
1256         return py_ret;  
1257 }
1258
1259
1260 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
1261 {
1262         struct ldb_request *req;
1263         PyObject *py_message;
1264         int ret;
1265         struct ldb_module *mod;
1266
1267         if (!PyArg_ParseTuple(args, "O", &py_message))
1268                 return NULL;
1269
1270         req = talloc_zero(NULL, struct ldb_request);
1271         req->operation = LDB_ADD;
1272         req->op.add.message = PyLdbMessage_AsMessage(py_message);
1273
1274         mod = PyLdbModule_AsModule(self);
1275         ret = mod->ops->add(mod, req);
1276
1277         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1278
1279         Py_RETURN_NONE;
1280 }
1281
1282 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args) 
1283 {
1284         int ret;
1285         struct ldb_request *req;
1286         PyObject *py_message;
1287         struct ldb_module *mod;
1288
1289         if (!PyArg_ParseTuple(args, "O", &py_message))
1290                 return NULL;
1291
1292         req = talloc_zero(NULL, struct ldb_request);
1293         req->operation = LDB_MODIFY;
1294         req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1295
1296         mod = PyLdbModule_AsModule(self);
1297         ret = mod->ops->modify(mod, req);
1298
1299         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
1300
1301         Py_RETURN_NONE;
1302 }
1303
1304 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args) 
1305 {
1306         int ret;
1307         struct ldb_request *req;
1308         PyObject *py_dn;
1309
1310         if (!PyArg_ParseTuple(args, "O", &py_dn))
1311                 return NULL;
1312
1313         req = talloc_zero(NULL, struct ldb_request);
1314         req->operation = LDB_DELETE;
1315         req->op.del.dn = PyLdbDn_AsDn(py_dn);
1316
1317         ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1318
1319         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1320
1321         Py_RETURN_NONE;
1322 }
1323
1324 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
1325 {
1326         int ret;
1327         struct ldb_request *req;
1328         PyObject *py_dn1, *py_dn2;
1329
1330         if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1331                 return NULL;
1332
1333         req = talloc_zero(NULL, struct ldb_request);
1334
1335         req->operation = LDB_RENAME;
1336         req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1337         req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1338
1339         ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1340
1341         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
1342
1343         Py_RETURN_NONE;
1344 }
1345
1346 static PyMethodDef py_ldb_module_methods[] = {
1347         { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1348         { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1349         { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1350         { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1351         { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1352         { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1353         { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1354         { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1355         { NULL },
1356 };
1357
1358 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
1359 {
1360         talloc_free(self->mem_ctx);
1361         self->ob_type->tp_free(self);
1362 }
1363
1364 PyTypeObject PyLdbModule = {
1365         .tp_name = "LdbModule",
1366         .tp_methods = py_ldb_module_methods,
1367         .tp_repr = (reprfunc)py_ldb_module_repr,
1368         .tp_str = (reprfunc)py_ldb_module_str,
1369         .tp_basicsize = sizeof(PyLdbModuleObject),
1370         .tp_dealloc = (destructor)py_ldb_module_dealloc,
1371         .tp_flags = Py_TPFLAGS_DEFAULT,
1372 };
1373
1374
1375 /**
1376  * Create a ldb_message_element from a Python object.
1377  *
1378  * This will accept any sequence objects that contains strings, or 
1379  * a string object.
1380  *
1381  * A reference to set_obj will be borrowed. 
1382  *
1383  * @param mem_ctx Memory context
1384  * @param set_obj Python object to convert
1385  * @param flags ldb_message_element flags to set
1386  * @param attr_name Name of the attribute
1387  * @return New ldb_message_element, allocated as child of mem_ctx
1388  */
1389 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
1390                                                                                            PyObject *set_obj, int flags,
1391                                                                                            const char *attr_name)
1392 {
1393         struct ldb_message_element *me;
1394
1395         if (PyLdbMessageElement_Check(set_obj))
1396                 return PyLdbMessageElement_AsMessageElement(set_obj);
1397
1398         me = talloc(mem_ctx, struct ldb_message_element);
1399
1400         me->name = attr_name;
1401         me->flags = flags;
1402         if (PyString_Check(set_obj)) {
1403                 me->num_values = 1;
1404                 me->values = talloc_array(me, struct ldb_val, me->num_values);
1405                 me->values[0].length = PyString_Size(set_obj);
1406                 me->values[0].data = (uint8_t *)PyString_AsString(set_obj);
1407         } else if (PySequence_Check(set_obj)) {
1408                 int i;
1409                 me->num_values = PySequence_Size(set_obj);
1410                 me->values = talloc_array(me, struct ldb_val, me->num_values);
1411                 for (i = 0; i < me->num_values; i++) {
1412                         PyObject *obj = PySequence_GetItem(set_obj, i);
1413
1414                         me->values[i].length = PyString_Size(obj);
1415                         me->values[i].data = (uint8_t *)PyString_AsString(obj);
1416                 }
1417         } else {
1418                 talloc_free(me);
1419                 me = NULL;
1420         }
1421
1422         return me;
1423 }
1424
1425
1426 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx, 
1427                                                                  struct ldb_message_element *me)
1428 {
1429         int i;
1430         PyObject *result;
1431
1432         /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1433         result = PyList_New(me->num_values);
1434
1435         for (i = 0; i < me->num_values; i++) {
1436                 PyList_SetItem(result, i,
1437                         PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1438         }
1439
1440         return result;
1441 }
1442
1443 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
1444 {
1445         int i;
1446         if (!PyArg_ParseTuple(args, "i", &i))
1447                 return NULL;
1448         if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1449                 Py_RETURN_NONE;
1450
1451         return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self), 
1452                                                                  &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1453 }
1454
1455 static PyMethodDef py_ldb_msg_element_methods[] = {
1456         { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1457         { NULL },
1458 };
1459
1460 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
1461 {
1462         return PyLdbMessageElement_AsMessageElement(self)->num_values;
1463 }
1464
1465 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
1466 {
1467         struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1468         if (idx < 0 || idx >= el->num_values) {
1469                 PyErr_SetString(PyExc_IndexError, "Out of range");
1470                 return NULL;
1471         }
1472         return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1473 }
1474
1475 static PySequenceMethods py_ldb_msg_element_seq = {
1476         .sq_length = (lenfunc)py_ldb_msg_element_len,
1477         .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1478 };
1479
1480 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
1481 {
1482         return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self), 
1483                                                                    PyLdbMessageElement_AsMessageElement(other));
1484 }
1485
1486 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
1487 {
1488         return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1489 }
1490
1491 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
1492 {
1493         PyLdbMessageElementObject *ret;
1494         ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1495         if (ret == NULL) {
1496                 PyErr_NoMemory();
1497                 return NULL;
1498         }
1499         ret->mem_ctx = talloc_new(NULL);
1500         if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1501                 PyErr_NoMemory();
1502                 return NULL;
1503         }
1504         ret->el = el;
1505         return (PyObject *)ret;
1506 }
1507
1508 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1509 {
1510         PyObject *py_elements = NULL;
1511         struct ldb_message_element *el;
1512         int flags = 0;
1513         char *name = NULL;
1514         const char * const kwnames[] = { "elements", "flags", "name", NULL };
1515         PyLdbMessageElementObject *ret;
1516         TALLOC_CTX *mem_ctx;
1517
1518         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois",
1519                                          discard_const_p(char *, kwnames),
1520                                          &py_elements, &flags, &name))
1521                 return NULL;
1522
1523         mem_ctx = talloc_new(NULL);
1524         if (mem_ctx == NULL) {
1525                 PyErr_NoMemory();
1526                 return NULL;
1527         }
1528
1529         el = talloc_zero(mem_ctx, struct ldb_message_element);
1530
1531         if (py_elements != NULL) {
1532                 int i;
1533                 if (PyString_Check(py_elements)) {
1534                         el->num_values = 1;
1535                         el->values = talloc_array(el, struct ldb_val, 1);
1536                         el->values[0].data = (uint8_t *)PyString_AsString(py_elements);
1537                         el->values[0].length = PyString_Size(py_elements);
1538                 } else if (PySequence_Check(py_elements)) {
1539                         el->num_values = PySequence_Size(py_elements);
1540                         el->values = talloc_array(el, struct ldb_val, el->num_values);
1541                         for (i = 0; i < el->num_values; i++) {
1542                                 PyObject *item = PySequence_GetItem(py_elements, i);
1543                                 el->values[i].data = (uint8_t *)PyString_AsString(item);
1544                                 el->values[i].length = PyString_Size(item);
1545                         }
1546                 } else {
1547                         PyErr_SetString(PyExc_TypeError, 
1548                                         "Expected string or list");
1549                         talloc_free(mem_ctx);
1550                         return NULL;
1551                 }
1552         }
1553
1554         el->flags = flags;
1555         el->name = talloc_strdup(el, name);
1556
1557         ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1558         if (ret == NULL) {
1559                 PyErr_NoMemory();
1560                 talloc_free(mem_ctx);
1561                 return NULL;
1562         }
1563
1564         ret->mem_ctx = mem_ctx;
1565         ret->el = el;
1566         return (PyObject *)ret;
1567 }
1568
1569 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
1570 {
1571         char *element_str = NULL;
1572         int i;
1573         struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1574         PyObject *ret;
1575
1576         for (i = 0; i < el->num_values; i++) {
1577                 PyObject *o = py_ldb_msg_element_find(self, i);
1578                 if (element_str == NULL)
1579                         element_str = talloc_strdup(NULL, PyObject_REPR(o));
1580                 else
1581                         element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1582         }
1583
1584         ret = PyString_FromFormat("MessageElement([%s])", element_str);
1585
1586         talloc_free(element_str);
1587
1588         return ret;
1589 }
1590
1591 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
1592 {
1593         struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1594
1595         if (el->num_values == 1)
1596                 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1597         else 
1598                 Py_RETURN_NONE;
1599 }
1600
1601 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
1602 {
1603         talloc_free(self->mem_ctx);
1604         self->ob_type->tp_free(self);
1605 }
1606
1607 PyTypeObject PyLdbMessageElement = {
1608         .tp_name = "MessageElement",
1609         .tp_basicsize = sizeof(PyLdbMessageElementObject),
1610         .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1611         .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1612         .tp_str = (reprfunc)py_ldb_msg_element_str,
1613         .tp_methods = py_ldb_msg_element_methods,
1614         .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1615         .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1616         .tp_as_sequence = &py_ldb_msg_element_seq,
1617         .tp_new = py_ldb_msg_element_new,
1618         .tp_flags = Py_TPFLAGS_DEFAULT,
1619 };
1620
1621 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
1622 {
1623         char *name;
1624         if (!PyArg_ParseTuple(args, "s", &name))
1625                 return NULL;
1626
1627         ldb_msg_remove_attr(self->msg, name);
1628
1629         Py_RETURN_NONE;
1630 }
1631
1632 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
1633 {
1634         struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1635         int i, j = 0;
1636         PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1637         if (msg->dn != NULL) {
1638                 PyList_SetItem(obj, j, PyString_FromString("dn"));
1639                 j++;
1640         }
1641         for (i = 0; i < msg->num_elements; i++) {
1642                 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1643                 j++;
1644         }
1645         return obj;
1646 }
1647
1648 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
1649 {
1650         struct ldb_message_element *el;
1651         char *name = PyString_AsString(py_name);
1652         struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1653         if (!strcmp(name, "dn"))
1654                 return PyLdbDn_FromDn(msg->dn);
1655         el = ldb_msg_find_element(msg, name);
1656         if (el == NULL) {
1657                 return NULL;
1658         }
1659         return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg);
1660 }
1661
1662 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
1663 {
1664         PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
1665         if (ret == NULL) {
1666                 PyErr_SetString(PyExc_KeyError, "No such element");
1667                 return NULL;
1668         }
1669         return ret;
1670 }
1671
1672 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
1673 {
1674         PyObject *name, *ret;
1675         if (!PyArg_ParseTuple(args, "O", &name))
1676                 return NULL;
1677
1678         ret = py_ldb_msg_getitem_helper(self, name);
1679         if (ret == NULL)
1680                 Py_RETURN_NONE;
1681         return ret;
1682 }
1683
1684 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
1685 {
1686         struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1687         int i, j;
1688         PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
1689         j = 0;
1690         if (msg->dn != NULL) {
1691                 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
1692                 j++;
1693         }
1694         for (i = 0; i < msg->num_elements; i++, j++) {
1695                 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], self->msg)));
1696         }
1697         return l;
1698 }
1699
1700 static PyMethodDef py_ldb_msg_methods[] = { 
1701         { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
1702         { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
1703         { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
1704         { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
1705         { NULL },
1706 };
1707
1708 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
1709 {
1710         PyObject *list, *iter;
1711
1712         list = py_ldb_msg_keys(self);
1713         iter = PyObject_GetIter(list);
1714         Py_DECREF(list);
1715         return iter;
1716 }
1717
1718 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
1719 {
1720         char *attr_name = PyString_AsString(name);
1721         if (value == NULL) {
1722                 ldb_msg_remove_attr(self->msg, attr_name);
1723         } else {
1724                 struct ldb_message_element *el = PyObject_AsMessageElement(NULL,
1725                                                                                         value, 0, attr_name);
1726                 if (el == NULL)
1727                         return -1;
1728                 talloc_steal(self->msg, el);
1729                 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
1730                 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
1731         }
1732         return 0;
1733 }
1734
1735 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
1736 {
1737         return PyLdbMessage_AsMessage(self)->num_elements;
1738 }
1739
1740 static PyMappingMethods py_ldb_msg_mapping = {
1741         .mp_length = (lenfunc)py_ldb_msg_length,
1742         .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
1743         .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
1744 };
1745
1746 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1747 {
1748         const char * const kwnames[] = { "dn", NULL };
1749         struct ldb_message *ret;
1750         TALLOC_CTX *mem_ctx;
1751         PyObject *pydn = NULL;
1752         PyLdbMessageObject *py_ret;
1753
1754         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
1755                                          discard_const_p(char *, kwnames),
1756                                          &pydn))
1757                 return NULL;
1758
1759         mem_ctx = talloc_new(NULL);
1760         if (mem_ctx == NULL) {
1761                 PyErr_NoMemory();
1762                 return NULL;
1763         }
1764
1765         ret = ldb_msg_new(mem_ctx);
1766         if (ret == NULL) {
1767                 talloc_free(mem_ctx);
1768                 PyErr_NoMemory();
1769                 return NULL;
1770         }
1771
1772         if (pydn != NULL) {
1773                 struct ldb_dn *dn;
1774                 if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) {
1775                         talloc_free(mem_ctx);
1776                         return NULL;
1777                 }
1778                 ret->dn = talloc_reference(ret, dn);
1779         }
1780
1781         py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
1782         if (py_ret == NULL) {
1783                 PyErr_NoMemory();
1784                 talloc_free(mem_ctx);
1785                 return NULL;
1786         }
1787
1788         py_ret->mem_ctx = mem_ctx;
1789         py_ret->msg = ret;
1790         return (PyObject *)py_ret;
1791 }
1792
1793 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
1794 {
1795         PyLdbMessageObject *ret;
1796
1797         ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
1798         if (ret == NULL) {
1799                 PyErr_NoMemory();
1800                 return NULL;
1801         }
1802         ret->mem_ctx = talloc_new(NULL);
1803         ret->msg = talloc_reference(ret->mem_ctx, msg);
1804         return (PyObject *)ret;
1805 }
1806
1807 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
1808 {
1809         struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1810         return PyLdbDn_FromDn(msg->dn);
1811 }
1812
1813 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
1814 {
1815         struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1816         if (!PyLdbDn_Check(value)) {
1817                 PyErr_SetNone(PyExc_TypeError);
1818                 return -1;
1819         }
1820
1821         msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value));
1822         return 0;
1823 }
1824
1825 static PyGetSetDef py_ldb_msg_getset[] = {
1826         { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
1827         { NULL }
1828 };
1829
1830 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
1831 {
1832         PyObject *dict = PyDict_New(), *ret;
1833         if (PyDict_Update(dict, (PyObject *)self) != 0)
1834                 return NULL;
1835         ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
1836         Py_DECREF(dict);
1837         return ret;
1838 }
1839
1840 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
1841 {
1842         talloc_free(self->mem_ctx);
1843         self->ob_type->tp_free(self);
1844 }
1845
1846 PyTypeObject PyLdbMessage = {
1847         .tp_name = "Message",
1848         .tp_methods = py_ldb_msg_methods,
1849         .tp_getset = py_ldb_msg_getset,
1850         .tp_as_mapping = &py_ldb_msg_mapping,
1851         .tp_basicsize = sizeof(PyLdbMessageObject),
1852         .tp_dealloc = (destructor)py_ldb_msg_dealloc,
1853         .tp_new = py_ldb_msg_new,
1854         .tp_repr = (reprfunc)py_ldb_msg_repr,
1855         .tp_flags = Py_TPFLAGS_DEFAULT,
1856         .tp_iter = (getiterfunc)py_ldb_msg_iter,
1857 };
1858
1859 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
1860 {
1861         PyLdbTreeObject *ret;
1862
1863         ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
1864         if (ret == NULL) {
1865                 PyErr_NoMemory();
1866                 return NULL;
1867         }
1868
1869         ret->mem_ctx = talloc_new(NULL);
1870         ret->tree = talloc_reference(ret->mem_ctx, tree);
1871         return (PyObject *)ret;
1872 }
1873
1874 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
1875 {
1876         talloc_free(self->mem_ctx);
1877         self->ob_type->tp_free(self);
1878 }
1879
1880 PyTypeObject PyLdbTree = {
1881         .tp_name = "Tree",
1882         .tp_basicsize = sizeof(PyLdbTreeObject),
1883         .tp_dealloc = (destructor)py_ldb_tree_dealloc,
1884         .tp_flags = Py_TPFLAGS_DEFAULT,
1885 };
1886
1887 /* Ldb_module */
1888 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
1889 {
1890         PyObject *py_ldb = (PyObject *)mod->private_data;
1891         PyObject *py_result, *py_base, *py_attrs, *py_tree;
1892
1893         py_base = PyLdbDn_FromDn(req->op.search.base);
1894
1895         if (py_base == NULL)
1896                 return LDB_ERR_OPERATIONS_ERROR;
1897
1898         py_tree = PyLdbTree_FromTree(req->op.search.tree);
1899
1900         if (py_tree == NULL)
1901                 return LDB_ERR_OPERATIONS_ERROR;
1902
1903         if (req->op.search.attrs == NULL) {
1904                 py_attrs = Py_None;
1905         } else {
1906                 int i, len;
1907                 for (len = 0; req->op.search.attrs[len]; len++);
1908                 py_attrs = PyList_New(len);
1909                 for (i = 0; i < len; i++)
1910                         PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1911         }
1912
1913         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
1914                                         discard_const_p(char, "OiOO"),
1915                                         py_base, req->op.search.scope, py_tree, py_attrs);
1916
1917         Py_DECREF(py_attrs);
1918         Py_DECREF(py_tree);
1919         Py_DECREF(py_base);
1920
1921         if (py_result == NULL) {
1922                 return LDB_ERR_PYTHON_EXCEPTION;
1923         }
1924
1925         req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
1926         if (req->op.search.res == NULL) {
1927                 return LDB_ERR_PYTHON_EXCEPTION;
1928         }
1929
1930         Py_DECREF(py_result);
1931
1932         return LDB_SUCCESS;
1933 }
1934
1935 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1936 {
1937         PyObject *py_ldb = (PyObject *)mod->private_data;
1938         PyObject *py_result, *py_msg;
1939
1940         py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
1941
1942         if (py_msg == NULL) {
1943                 return LDB_ERR_OPERATIONS_ERROR;
1944         }
1945
1946         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
1947                                         discard_const_p(char, "O"),
1948                                         py_msg);
1949
1950         Py_DECREF(py_msg);
1951
1952         if (py_result == NULL) {
1953                 return LDB_ERR_PYTHON_EXCEPTION;
1954         }
1955
1956         Py_DECREF(py_result);
1957
1958         return LDB_SUCCESS;
1959 }
1960
1961 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1962 {
1963         PyObject *py_ldb = (PyObject *)mod->private_data;
1964         PyObject *py_result, *py_msg;
1965
1966         py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
1967
1968         if (py_msg == NULL) {
1969                 return LDB_ERR_OPERATIONS_ERROR;
1970         }
1971
1972         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
1973                                         discard_const_p(char, "O"),
1974                                         py_msg);
1975
1976         Py_DECREF(py_msg);
1977
1978         if (py_result == NULL) {
1979                 return LDB_ERR_PYTHON_EXCEPTION;
1980         }
1981
1982         Py_DECREF(py_result);
1983
1984         return LDB_SUCCESS;
1985 }
1986
1987 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
1988 {
1989         PyObject *py_ldb = (PyObject *)mod->private_data;
1990         PyObject *py_result, *py_dn;
1991
1992         py_dn = PyLdbDn_FromDn(req->op.del.dn);
1993
1994         if (py_dn == NULL)
1995                 return LDB_ERR_OPERATIONS_ERROR;
1996
1997         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
1998                                         discard_const_p(char, "O"),
1999                                         py_dn);
2000
2001         if (py_result == NULL) {
2002                 return LDB_ERR_PYTHON_EXCEPTION;
2003         }
2004
2005         Py_DECREF(py_result);
2006
2007         return LDB_SUCCESS;
2008 }
2009
2010 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
2011 {
2012         PyObject *py_ldb = (PyObject *)mod->private_data;
2013         PyObject *py_result, *py_olddn, *py_newdn;
2014
2015         py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
2016
2017         if (py_olddn == NULL)
2018                 return LDB_ERR_OPERATIONS_ERROR;
2019
2020         py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
2021
2022         if (py_newdn == NULL)
2023                 return LDB_ERR_OPERATIONS_ERROR;
2024
2025         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
2026                                         discard_const_p(char, "OO"),
2027                                         py_olddn, py_newdn);
2028
2029         Py_DECREF(py_olddn);
2030         Py_DECREF(py_newdn);
2031
2032         if (py_result == NULL) {
2033                 return LDB_ERR_PYTHON_EXCEPTION;
2034         }
2035
2036         Py_DECREF(py_result);
2037
2038         return LDB_SUCCESS;
2039 }
2040
2041 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
2042 {
2043         PyObject *py_ldb = (PyObject *)mod->private_data;
2044         PyObject *py_result;
2045
2046         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
2047                                         discard_const_p(char, ""));
2048
2049         return LDB_ERR_OPERATIONS_ERROR;
2050 }
2051
2052 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
2053 {
2054         PyObject *py_ldb = (PyObject *)mod->private_data;
2055         PyObject *py_result;
2056
2057         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
2058                                         discard_const_p(char, ""));
2059
2060         return LDB_ERR_OPERATIONS_ERROR;
2061 }
2062
2063 static int py_module_start_transaction(struct ldb_module *mod)
2064 {
2065         PyObject *py_ldb = (PyObject *)mod->private_data;
2066         PyObject *py_result;
2067
2068         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
2069                                         discard_const_p(char, ""));
2070
2071         if (py_result == NULL) {
2072                 return LDB_ERR_PYTHON_EXCEPTION;
2073         }
2074
2075         Py_DECREF(py_result);
2076
2077         return LDB_SUCCESS;
2078 }
2079
2080 static int py_module_end_transaction(struct ldb_module *mod)
2081 {
2082         PyObject *py_ldb = (PyObject *)mod->private_data;
2083         PyObject *py_result;
2084
2085         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
2086                                         discard_const_p(char, ""));
2087
2088         if (py_result == NULL) {
2089                 return LDB_ERR_PYTHON_EXCEPTION;
2090         }
2091
2092         Py_DECREF(py_result);
2093
2094         return LDB_SUCCESS;
2095 }
2096
2097 static int py_module_del_transaction(struct ldb_module *mod)
2098 {
2099         PyObject *py_ldb = (PyObject *)mod->private_data;
2100         PyObject *py_result;
2101
2102         py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
2103                                         discard_const_p(char, ""));
2104
2105         if (py_result == NULL) {
2106                 return LDB_ERR_PYTHON_EXCEPTION;
2107         }
2108
2109         Py_DECREF(py_result);
2110
2111         return LDB_SUCCESS;
2112 }
2113
2114 static int py_module_destructor(struct ldb_module *mod)
2115 {
2116         Py_DECREF((PyObject *)mod->private_data);
2117         return 0;
2118 }
2119
2120 static int py_module_init(struct ldb_module *mod)
2121 {
2122         PyObject *py_class = (PyObject *)mod->ops->private_data;
2123         PyObject *py_result, *py_next, *py_ldb;
2124
2125         py_ldb = PyLdb_FromLdbContext(mod->ldb);
2126
2127         if (py_ldb == NULL)
2128                 return LDB_ERR_OPERATIONS_ERROR;
2129
2130         py_next = PyLdbModule_FromModule(mod->next);
2131
2132         if (py_next == NULL)
2133                 return LDB_ERR_OPERATIONS_ERROR;
2134
2135         py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
2136                                           py_ldb, py_next);
2137
2138         if (py_result == NULL) {
2139                 return LDB_ERR_PYTHON_EXCEPTION;
2140         }
2141
2142         mod->private_data = py_result;
2143
2144         talloc_set_destructor(mod, py_module_destructor);
2145
2146         return ldb_next_init(mod);
2147 }
2148
2149 static PyObject *py_register_module(PyObject *module, PyObject *args)
2150 {
2151         int ret;
2152         struct ldb_module_ops *ops;
2153         PyObject *input;
2154
2155         if (!PyArg_ParseTuple(args, "O", &input))
2156                 return NULL;
2157
2158         ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
2159         if (ops == NULL) {
2160                 PyErr_NoMemory();
2161                 return NULL;
2162         }
2163
2164         ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name"))));
2165
2166         Py_INCREF(input);
2167         ops->private_data = input;
2168         ops->init_context = py_module_init;
2169         ops->search = py_module_search;
2170         ops->add = py_module_add;
2171         ops->modify = py_module_modify;
2172         ops->del = py_module_del;
2173         ops->rename = py_module_rename;
2174         ops->request = py_module_request;
2175         ops->extended = py_module_extended;
2176         ops->start_transaction = py_module_start_transaction;
2177         ops->end_transaction = py_module_end_transaction;
2178         ops->del_transaction = py_module_del_transaction;
2179
2180         ret = ldb_register_module(ops);
2181
2182         PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
2183
2184         Py_RETURN_NONE;
2185 }
2186
2187 static PyObject *py_timestring(PyObject *module, PyObject *args)
2188 {
2189         time_t t;
2190         char *tresult;
2191         PyObject *ret;
2192         if (!PyArg_ParseTuple(args, "L", &t))
2193                 return NULL;
2194         tresult = ldb_timestring(NULL, t);
2195         ret = PyString_FromString(tresult);
2196         talloc_free(tresult);
2197         return ret;
2198 }
2199
2200 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
2201 {
2202         char *str;
2203         if (!PyArg_ParseTuple(args, "s", &str))
2204                 return NULL;
2205
2206         return PyInt_FromLong(ldb_string_to_time(str));
2207 }
2208
2209 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
2210 {
2211         char *name;
2212         if (!PyArg_ParseTuple(args, "s", &name))
2213                 return NULL;
2214         return PyBool_FromLong(ldb_valid_attr_name(name));
2215 }
2216
2217 static PyMethodDef py_ldb_global_methods[] = {
2218         { "register_module", py_register_module, METH_VARARGS, 
2219                 "S.register_module(module) -> None\n"
2220                 "Register a LDB module."},
2221         { "timestring", py_timestring, METH_VARARGS, 
2222                 "S.timestring(int) -> string\n"
2223                 "Generate a LDAP time string from a UNIX timestamp" },
2224         { "string_to_time", py_string_to_time, METH_VARARGS,
2225                 "S.string_to_time(string) -> int\n"
2226                 "Parse a LDAP time string into a UNIX timestamp." },
2227         { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2228                 "S.valid_attr_name(name) -> bool\n"
2229                 "Check whether the supplied name is a valid attribute name." },
2230         { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2231                 NULL },
2232         { NULL }
2233 };
2234
2235 void initldb(void)
2236 {
2237         PyObject *m;
2238
2239         if (PyType_Ready(&PyLdbDn) < 0)
2240                 return;
2241
2242         if (PyType_Ready(&PyLdbMessage) < 0)
2243                 return;
2244
2245         if (PyType_Ready(&PyLdbMessageElement) < 0)
2246                 return;
2247
2248         if (PyType_Ready(&PyLdb) < 0)
2249                 return;
2250
2251         if (PyType_Ready(&PyLdbModule) < 0)
2252                 return;
2253
2254         if (PyType_Ready(&PyLdbTree) < 0)
2255                 return;
2256
2257         m = Py_InitModule3("ldb", py_ldb_global_methods, 
2258                 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2259         if (m == NULL)
2260                 return;
2261
2262         PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2263         PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2264         PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2265         PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2266
2267         PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2268         PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2269         PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2270         PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2271
2272         PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2273         PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2274         PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2275         PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2276         PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2277         PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2278         PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2279         PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2280         PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2281         PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2282         PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2283         PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2284         PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2285         PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2286         PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2287         PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2288         PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2289         PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2290         PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2291         PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2292         PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2293         PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2294         PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2295         PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2296         PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2297         PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2298         PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2299         PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2300         PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2301         PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2302         PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2303         PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2304         PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2305         PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2306         PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2307         PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2308         PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2309         PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2310
2311         PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2312
2313         PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2314
2315         PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL);
2316         PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2317
2318         Py_INCREF(&PyLdb);
2319         Py_INCREF(&PyLdbDn);
2320         Py_INCREF(&PyLdbModule);
2321         Py_INCREF(&PyLdbMessage);
2322         Py_INCREF(&PyLdbMessageElement);
2323         Py_INCREF(&PyLdbTree);
2324
2325         PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2326         PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2327         PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2328         PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2329         PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2330         PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);
2331 }