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