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