ldb/python: Add bindings for schema functions.
[sfrench/samba-autobuild/.git] / source4 / lib / ldb / ldb.i
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 %module ldb
29
30 %{
31
32 #include <stdint.h>
33 #include <stdbool.h>
34 #include "talloc.h"
35 #include "ldb.h"
36 #include "ldb_errors.h"
37 #include "ldb_private.h"
38
39 typedef struct ldb_message ldb_msg;
40 typedef struct ldb_context ldb;
41 typedef struct ldb_dn ldb_dn;
42 typedef struct ldb_ldif ldb_ldif;
43 typedef struct ldb_message_element ldb_msg_element;
44 typedef int ldb_error;
45
46 %}
47
48 %import "carrays.i"
49 %import "typemaps.i"
50 %include "exception.i"
51 %import "stdint.i"
52
53 /* Don't expose talloc contexts in Python code. Python does reference 
54    counting for us, so just create a new top-level talloc context.
55  */
56 %typemap(in, numinputs=0, noblock=1) TALLOC_CTX * {
57     $1 = NULL;
58 }
59
60
61
62 %constant int SCOPE_DEFAULT = LDB_SCOPE_DEFAULT;
63 %constant int SCOPE_BASE = LDB_SCOPE_BASE;
64 %constant int SCOPE_ONELEVEL = LDB_SCOPE_ONELEVEL;
65 %constant int SCOPE_SUBTREE = LDB_SCOPE_SUBTREE;
66
67 %constant int CHANGETYPE_NONE = LDB_CHANGETYPE_NONE;
68 %constant int CHANGETYPE_ADD = LDB_CHANGETYPE_ADD;
69 %constant int CHANGETYPE_DELETE = LDB_CHANGETYPE_DELETE;
70 %constant int CHANGETYPE_MODIFY = LDB_CHANGETYPE_MODIFY;
71
72 /* 
73  * Wrap struct ldb_context
74  */
75
76 /* The ldb functions will crash if a NULL ldb context is passed so
77    catch this before it happens. */
78
79 %typemap(check,noblock=1) struct ldb_context* {
80         if ($1 == NULL)
81                 SWIG_exception(SWIG_ValueError, 
82                         "ldb context must be non-NULL");
83 }
84
85 %typemap(check,noblock=1) ldb_msg * {
86         if ($1 == NULL)
87                 SWIG_exception(SWIG_ValueError, 
88                         "Message can not be None");
89 }
90
91 /*
92  * Wrap struct ldb_val
93  */
94
95 %typemap(in,noblock=1) struct ldb_val *INPUT (struct ldb_val temp) {
96         $1 = &temp;
97         if (!PyString_Check($input)) {
98                 PyErr_SetString(PyExc_TypeError, "string arg expected");
99                 return NULL;
100         }
101         $1->length = PyString_Size($input);
102         $1->data = PyString_AsString($input);
103 }
104
105 %inline %{
106 PyObject *ldb_val_to_py_object(struct ldb_context *ldb_ctx, 
107                                struct ldb_message_element *el, 
108                                struct ldb_val *val)
109 {
110         const struct ldb_schema_attribute *a;
111         struct ldb_val new_val;
112         TALLOC_CTX *mem_ctx = talloc_new(NULL);
113         PyObject *ret;
114         
115         new_val = *val;
116         
117         if (ldb_ctx != NULL) {        
118                 a = ldb_schema_attribute_by_name(ldb_ctx, el->name);
119         
120                 if (a != NULL) {
121                         if (a->syntax->ldif_write_fn(ldb_ctx, mem_ctx, val, &new_val) != 0) {
122                                 talloc_free(mem_ctx);
123                                 return NULL;
124                         }
125                 }
126         } 
127         
128         ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
129         
130         talloc_free(mem_ctx);
131         
132         return ret;
133 }
134
135 %}
136
137 %typemap(out,noblock=1) struct ldb_val * {
138         $result = PyString_FromStringAndSize((const char *)$1->data, $1->length)
139 }
140
141 %typemap(out,noblock=1) struct ldb_val {
142         $result = PyString_FromStringAndSize((const char *)$1.data, $1.length)
143 }
144
145 /*
146  * Wrap struct ldb_result
147  */
148
149 %typemap(in,noblock=1,numinputs=0) struct ldb_result **OUT (struct ldb_result *temp_ldb_result) {
150         $1 = &temp_ldb_result;
151 }
152
153 #ifdef SWIGPYTHON
154 %typemap(argout,noblock=1) struct ldb_result ** (int i) {
155         $result = PyList_New((*$1)->count);
156     for (i = 0; i < (*$1)->count; i++) {
157         PyList_SetItem($result, i, 
158             SWIG_NewPointerObj((*$1)->msgs[i], SWIGTYPE_p_ldb_message, 0)
159         );
160     }
161 }
162
163 %typemap(in,noblock=1,numinputs=1) const char * const *NULL_STR_LIST {
164     if ($input == Py_None) {
165         $1 = NULL;
166     } else if (PySequence_Check($input)) {
167         int i;
168         $1 = talloc_array(NULL, char *, PySequence_Size($input)+1);
169         for(i = 0; i < PySequence_Size($input); i++)
170             $1[i] = PyString_AsString(PySequence_GetItem($input, i));
171         $1[i] = NULL;
172     } else {
173         SWIG_exception(SWIG_TypeError, "expected sequence");
174     }
175 }
176
177 %typemap(freearg,noblock=1) const char * const *NULL_STR_LIST {
178     talloc_free($1);
179 }
180
181 %apply const char * const *NULL_STR_LIST { const char * const *attrs }
182 %apply const char * const *NULL_STR_LIST { const char * const *control_strings }
183
184 #endif
185
186 %types(struct ldb_result *);
187
188 /*
189  * Wrap struct ldb_dn
190  */
191
192 %rename(__str__) ldb_dn::get_linearized;
193 %rename(__cmp__) ldb_dn::compare;
194 %rename(__len__) ldb_dn::get_comp_num;
195 %rename(Dn) ldb_dn;
196 typedef struct ldb_dn {
197     %extend {
198         ldb_dn(ldb *ldb_ctx, const char *str)
199         {
200             ldb_dn *ret = ldb_dn_new(ldb_ctx, ldb_ctx, str);
201             /* ldb_dn_new() doesn't accept NULL as memory context, so 
202                we do it this way... */
203             talloc_steal(NULL, ret);
204
205             if (ret == NULL)
206                 SWIG_exception(SWIG_ValueError, 
207                                 "unable to parse dn string");
208 fail:
209             return ret;
210         }
211         ~ldb_dn() { talloc_free($self); }
212         bool validate();
213         const char *get_casefold();
214         const char *get_linearized();
215         ldb_dn *parent() { return ldb_dn_get_parent(NULL, $self); }
216         int compare(ldb_dn *other);
217         bool is_valid();
218         bool is_special();
219         bool is_null();
220         bool check_special(const char *name);
221         int get_comp_num();
222         bool add_child(ldb_dn *child);
223         bool add_base(ldb_dn *base);
224         const char *canonical_str() {
225             return ldb_dn_canonical_string($self, $self);
226         }
227         const char *canonical_ex_str() {
228             return ldb_dn_canonical_ex_string($self, $self);
229         }
230 #ifdef SWIGPYTHON
231         ldb_dn *__add__(ldb_dn *other)
232         {
233             ldb_dn *ret = ldb_dn_copy(NULL, $self);
234             ldb_dn_add_child(ret, other);
235             return ret;
236         }
237
238         /* FIXME: implement __getslice__ */
239 #endif
240     %pythoncode {
241         def __eq__(self, other):
242             if isinstance(other, self.__class__):
243                 return self.__cmp__(other) == 0
244             if isinstance(other, str):
245                 return str(self) == other
246             return False
247     }
248     }
249 } ldb_dn;
250
251 #ifdef SWIGPYTHON
252 %{
253 int ldb_dn_from_pyobject(TALLOC_CTX *mem_ctx, PyObject *object, 
254                          struct ldb_context *ldb_ctx, ldb_dn **dn)
255 {
256     int ret;
257     struct ldb_dn *odn;
258     if (ldb_ctx != NULL && PyString_Check(object)) {
259         *dn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
260         return 0;
261     }
262     ret = SWIG_ConvertPtr(object, (void **)&odn, SWIGTYPE_p_ldb_dn, 
263                            SWIG_POINTER_EXCEPTION);
264     *dn = ldb_dn_copy(mem_ctx, odn);
265     return ret;
266 }
267
268 ldb_msg_element *ldb_msg_element_from_pyobject(TALLOC_CTX *mem_ctx,
269                                                PyObject *set_obj, int flags,
270                                                const char *attr_name)
271 {
272     struct ldb_message_element *me = talloc(mem_ctx, struct ldb_message_element);
273     me->name = attr_name;
274     me->flags = flags;
275     if (PyString_Check(set_obj)) {
276         me->num_values = 1;
277         me->values = talloc_array(me, struct ldb_val, me->num_values);
278         me->values[0].length = PyString_Size(set_obj);
279         me->values[0].data = (uint8_t *)talloc_strdup(me->values, 
280                                            PyString_AsString(set_obj));
281     } else if (PySequence_Check(set_obj)) {
282         int i;
283         me->num_values = PySequence_Size(set_obj);
284         me->values = talloc_array(me, struct ldb_val, me->num_values);
285         for (i = 0; i < me->num_values; i++) {
286             PyObject *obj = PySequence_GetItem(set_obj, i);
287             me->values[i].length = PyString_Size(obj);
288             me->values[i].data = (uint8_t *)PyString_AsString(obj);
289         }
290     } else {
291         talloc_free(me);
292         me = NULL;
293     }
294
295     return me;
296 }
297
298 PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx, 
299                                  ldb_msg_element *me)
300 {
301     int i;
302     PyObject *result;
303
304     /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
305     result = PyList_New(me->num_values);
306
307     for (i = 0; i < me->num_values; i++) {
308         PyList_SetItem(result, i,
309             ldb_val_to_py_object(ldb_ctx, me, &me->values[i]));
310     }
311
312     return result;
313 }
314
315 %}
316 #endif
317
318 /* ldb_message_element */
319 %rename(__cmp__) ldb_message_element::compare;
320 %rename(MessageElement) ldb_msg_element;
321 typedef struct ldb_message_element {
322     %extend {
323 #ifdef SWIGPYTHON
324         PyObject *__iter__(void)
325         {
326             return PyObject_GetIter(ldb_msg_element_to_set(NULL, $self));
327         }
328
329         PyObject *__set__(void)
330         {
331             return ldb_msg_element_to_set(NULL, $self);
332         }
333
334         ldb_msg_element(PyObject *set_obj, int flags=0, const char *name = NULL)
335         {
336             return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name);
337         }
338
339         int __len__()
340         {
341             return $self->num_values;
342         }
343 #endif
344
345         PyObject *get(int i)
346         {
347             if (i < 0 || i >= $self->num_values)
348                 return Py_None;
349
350             return ldb_val_to_py_object(NULL, $self, &$self->values[i]);
351         }
352
353         ~ldb_msg_element() { talloc_free($self); }
354         int compare(ldb_msg_element *);
355     }
356     %pythoncode {
357         def __getitem__(self, i):
358             ret = self.get(i)
359             if ret is None:
360                 raise KeyError("no such value")
361             return ret
362
363         def __eq__(self, other):
364             if (len(self) == 1 and self.get(0) == other):
365                 return True
366             if isinstance(other, self.__class__):
367                 return self.__cmp__(other) == 0
368             o = iter(other)
369             for i in range(len(self)):
370                 if self.get(i) != o.next():
371                     return False
372             return True
373     }
374 } ldb_msg_element;
375
376 /* ldb_message */
377
378 %rename(Message) ldb_message;
379 #ifdef SWIGPYTHON
380 %rename(__delitem__) ldb_message::remove_attr;
381 %typemap(out) ldb_msg_element * {
382         if ($1 == NULL)
383                 PyErr_SetString(PyExc_KeyError, "no such element");
384     else
385         $result = SWIG_NewPointerObj($1, SWIGTYPE_p_ldb_message_element, 0);
386 }
387 %rename(__getitem__) ldb_message::find_element;
388 //%typemap(out) ldb_msg_element *;
389
390
391 %inline {
392     PyObject *ldb_msg_list_elements(ldb_msg *msg)
393     {
394         int i;
395         PyObject *obj = PyList_New(msg->num_elements);
396         for (i = 0; i < msg->num_elements; i++)
397             PyList_SetItem(obj, i, PyString_FromString(msg->elements[i].name));
398         return obj;
399     }
400 }
401
402 #endif
403
404 typedef struct ldb_message {
405         ldb_dn *dn;
406
407     %extend {
408         ldb_msg(ldb_dn *dn = NULL) { 
409             ldb_msg *ret = ldb_msg_new(NULL); 
410             ret->dn = talloc_reference(ret, dn);
411             return ret;
412         }
413         ~ldb_msg() { talloc_free($self); }
414         ldb_msg_element *find_element(const char *name);
415         
416 #ifdef SWIGPYTHON
417         void __setitem__(const char *attr_name, ldb_msg_element *val)
418         {
419             struct ldb_message_element *el;
420             
421             ldb_msg_remove_attr($self, attr_name);
422
423             el = talloc($self, struct ldb_message_element);
424             el->name = talloc_strdup(el, attr_name);
425             el->num_values = val->num_values;
426             el->values = talloc_reference(el, val->values);
427
428             ldb_msg_add($self, el, val->flags);
429         }
430
431         void __setitem__(const char *attr_name, PyObject *val)
432         {
433             struct ldb_message_element *el = ldb_msg_element_from_pyobject(NULL,
434                                                 val, 0, attr_name);
435             talloc_steal($self, el);
436             ldb_msg_remove_attr($self, attr_name);
437             ldb_msg_add($self, el, el->flags);
438         }
439
440         unsigned int __len__() { return $self->num_elements; }
441
442         PyObject *keys(void)
443         {
444             return ldb_msg_list_elements($self);
445         }
446
447         PyObject *__iter__(void)
448         {
449             return PyObject_GetIter(ldb_msg_list_elements($self));
450         }
451 #endif
452         void remove_attr(const char *name);
453     }
454 } ldb_msg;
455
456 /* FIXME: Convert ldb_result to 3-tuple:
457    (msgs, refs, controls)
458  */
459
460 typedef struct ldb_ldif ldb_ldif;
461
462 #ifdef SWIGPYTHON
463 %{
464 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
465 {
466     char *text;
467     PyObject *fn = context;
468
469     vasprintf(&text, fmt, ap);
470     PyObject_CallFunction(fn, (char *)"(i,s)", level, text);
471     free(text);
472 }
473 %}
474
475 %typemap(in,numinputs=1,noblock=1) (void (*debug)(void *context, enum ldb_debug_level level, const char *fmt, va_list ap), void *context) {
476     $1 = py_ldb_debug;
477     /* FIXME: Should be decreased somewhere as well. Perhaps register a 
478        destructor and tie it to the ldb context ? */
479     Py_INCREF($input);
480     $2 = $input;
481 }
482 #endif
483
484 %inline {
485     static PyObject *ldb_ldif_to_pyobject(ldb_ldif *ldif)
486     {
487         if (ldif == NULL) {
488             return Py_None;
489         } else {
490             return Py_BuildValue((char *)"(iO)", ldif->changetype, 
491                    SWIG_NewPointerObj(ldif->msg, SWIGTYPE_p_ldb_message, 0));
492         }
493     }
494 }
495
496 /*
497  * Wrap ldb errors
498  */
499
500 %{
501 PyObject *PyExc_LdbError;
502 %}
503
504 %pythoncode %{
505     LdbError = _ldb.LdbError
506 %}
507
508 %init %{
509     PyExc_LdbError = PyErr_NewException((char *)"_ldb.LdbError", NULL, NULL);
510     PyDict_SetItemString(d, "LdbError", PyExc_LdbError);
511 %}
512
513 %ignore _LDB_ERRORS_H_;
514 %ignore LDB_SUCCESS;
515 %include "include/ldb_errors.h"
516
517 /*
518  * Wrap ldb functions 
519  */
520
521
522 %typemap(out,noblock=1) ldb_error {
523     if ($1 != LDB_SUCCESS) {
524         PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", $1, ldb_strerror($1)));
525         SWIG_fail;
526     }
527     $result = Py_None;
528 };
529
530 %typemap(out,noblock=1) struct ldb_control ** {
531     if ($1 == NULL) {
532         PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(s)", ldb_errstring(arg1)));
533         SWIG_fail;
534     }
535     $result = SWIG_NewPointerObj($1, $1_descriptor, 0);
536 }
537
538 %rename(Ldb) ldb_context;
539
540 %typemap(in,noblock=1) struct ldb_dn * {
541     if (ldb_dn_from_pyobject(NULL, $input, arg1, &$1) != 0) {
542         SWIG_fail;
543     }
544 };
545
546 %typemap(freearg,noblock=1) struct ldb_dn * {
547     talloc_free($1);
548 };
549
550 /* Top-level ldb operations */
551 typedef struct ldb_context {
552     %extend {
553         ldb(void) { return ldb_init(NULL); }
554
555         ldb_error connect(const char *url, unsigned int flags = 0, 
556             const char *options[] = NULL);
557
558         ~ldb() { talloc_free($self); }
559         ldb_error search_ex(TALLOC_CTX *mem_ctx,
560                    ldb_dn *base = NULL, 
561                    enum ldb_scope scope = LDB_SCOPE_DEFAULT, 
562                    const char *expression = NULL, 
563                    const char *const *attrs = NULL, 
564                    struct ldb_control **controls = NULL,
565                    struct ldb_result **OUT) {
566             int ret;
567             struct ldb_result *res;
568             struct ldb_request *req;
569             res = talloc_zero(mem_ctx, struct ldb_result);
570             if (!res) {
571                 return LDB_ERR_OPERATIONS_ERROR;
572             }
573
574             ret = ldb_build_search_req(&req, $self, mem_ctx,
575                            base?base:ldb_get_default_basedn($self),
576                            scope,
577                            expression,
578                            attrs,
579                            controls,
580                            res,
581                            ldb_search_default_callback);
582
583             if (ret != LDB_SUCCESS) {
584                 talloc_free(res);
585                 return ret;
586             }
587
588             ldb_set_timeout($self, req, 0); /* use default timeout */
589                 
590             ret = ldb_request($self, req);
591                 
592             if (ret == LDB_SUCCESS) {
593                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
594             }
595
596             talloc_free(req);
597
598             *OUT = res;
599             return ret;
600         }
601
602         ldb_error delete(ldb_dn *dn);
603         ldb_error rename(ldb_dn *olddn, ldb_dn *newdn);
604         struct ldb_control **parse_control_strings(TALLOC_CTX *mem_ctx, 
605                                                    const char * const*control_strings);
606         ldb_error add(ldb_msg *add_msg);
607         ldb_error add(PyObject *py_msg) 
608         {
609             ldb_error ret;
610             int dict_pos, msg_pos;
611             PyObject *key, *value;
612             ldb_msg_element *msgel;
613             ldb_msg *msg = NULL;
614
615             if (PyDict_Check(py_msg)) {
616                 msg = ldb_msg_new(NULL);
617                 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
618                 msg_pos = dict_pos = 0;
619                 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
620                     if (!strcmp(PyString_AsString(key), "dn")) {
621                         if (ldb_dn_from_pyobject(msg, value, $self, &msg->dn) != 0) {
622                             return LDB_ERR_OTHER;
623                         }
624                     } else {
625                         msgel = ldb_msg_element_from_pyobject(msg->elements, value, 0, PyString_AsString(key));
626                         if (msgel == NULL) {
627                             SWIG_exception(SWIG_TypeError, "unable to import element");
628                             return LDB_ERR_OTHER;
629                         }
630                         memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
631                         msg_pos++;
632                     }
633                 }
634
635                 if (msg->dn == NULL) {
636                     SWIG_exception(SWIG_TypeError, "no dn set");
637                     return LDB_ERR_OTHER;
638                 }
639
640                 msg->num_elements = msg_pos;
641             } else {
642                 if (SWIG_ConvertPtr(py_msg, (void **)&msg, SWIGTYPE_p_ldb_message, 0) != 0)
643                     return LDB_ERR_OTHER;
644             }
645
646             ret = ldb_add($self, msg);
647
648             talloc_free(msg);
649             return ret;
650
651             fail:
652             return LDB_ERR_OTHER;
653         }
654         ldb_error modify(ldb_msg *message);
655         ldb_dn *get_config_basedn();
656         ldb_dn *get_root_basedn();
657         ldb_dn *get_schema_basedn();
658         ldb_dn *get_default_basedn();
659         PyObject *schema_format_value(const char *element_name, PyObject *val)
660         {
661                 const struct ldb_schema_attribute *a;
662                 struct ldb_val old_val;
663                 struct ldb_val new_val;
664                 TALLOC_CTX *mem_ctx = talloc_new(NULL);
665                 PyObject *ret;
666                 
667                 old_val.data = PyString_AsString(val);
668                 old_val.length = PyString_Size(val);
669                 
670                 a = ldb_schema_attribute_by_name($self, element_name);
671         
672                 if (a == NULL) {
673                         return Py_None;
674                 }
675                 
676                 if (a->syntax->ldif_write_fn($self, mem_ctx, &old_val, &new_val) != 0) {
677                         talloc_free(mem_ctx);
678                         return Py_None;
679                  }
680         
681                 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
682                 
683                 talloc_free(mem_ctx);
684                 
685                 return ret;
686         }
687
688         const char *errstring();
689         void set_create_perms(unsigned int perms);
690         void set_modules_dir(const char *path);
691         ldb_error set_debug(void (*debug)(void *context, enum ldb_debug_level level, 
692                                           const char *fmt, va_list ap),
693                             void *context);
694         ldb_error set_opaque(const char *name, void *value);
695         void *get_opaque(const char *name);
696         ldb_error transaction_start();
697         ldb_error transaction_commit();
698         ldb_error transaction_cancel();
699         void schema_attribute_remove(const char *name);
700         ldb_error schema_attribute_add(const char *attribute, unsigned flags, const char *syntax);
701         ldb_error setup_wellknown_attributes(void);
702         
703 #ifdef SWIGPYTHON
704         %typemap(in,numinputs=0,noblock=1) struct ldb_result **result_as_bool (struct ldb_result *tmp) { $1 = &tmp; }
705         %typemap(argout,noblock=1) struct ldb_result **result_as_bool { $result = ((*$1)->count > 0)?Py_True:Py_False; }
706         %typemap(freearg,noblock=1) struct ldb_result **result_as_bool { talloc_free(*$1); }
707         ldb_error __contains__(ldb_dn *dn, struct ldb_result **result_as_bool)
708         {
709             return ldb_search($self, dn, LDB_SCOPE_BASE, NULL, NULL, 
710                              result_as_bool);
711         }
712
713         PyObject *parse_ldif(const char *s)
714         {
715             PyObject *list = PyList_New(0);
716             struct ldb_ldif *ldif;
717             while ((ldif = ldb_ldif_read_string($self, &s)) != NULL) {
718                 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
719             }
720             return PyObject_GetIter(list);
721         }
722
723 #endif
724     }
725     %pythoncode {
726         def __init__(self, url=None, flags=0, options=None):
727             _ldb.Ldb_swiginit(self,_ldb.new_Ldb())
728             if url is not None:
729                 self.connect(url, flags, options)
730
731         def search(self, base=None, scope=SCOPE_DEFAULT, expression=None, 
732                    attrs=None, controls=None):
733             parsed_controls = None
734             if controls is not None:
735                 parsed_controls = self.parse_control_strings(controls)
736             return self.search_ex(base, scope, expression, attrs, 
737                                   parsed_controls)
738     }
739
740 } ldb;
741
742 %typemap(in,noblock=1) struct ldb_dn *;
743 %typemap(freearg,noblock=1) struct ldb_dn *;
744
745 %nodefault ldb_message;
746 %nodefault ldb_context;
747 %nodefault Dn;
748
749 %rename(valid_attr_name) ldb_valid_attr_name;
750 int ldb_valid_attr_name(const char *s);
751
752 typedef unsigned long time_t;
753
754 %inline %{
755 static char *timestring(time_t t)
756 {
757     char *tresult = ldb_timestring(NULL, t);
758     char *result = strdup(tresult);
759     talloc_free(tresult);
760     return result; 
761 }
762 %}
763
764 %rename(string_to_time) ldb_string_to_time;
765 time_t ldb_string_to_time(const char *s);
766
767 %typemap(in,noblock=1) const struct ldb_module_ops * {
768     $1 = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
769
770     $1->name = (char *)PyObject_GetAttrString($input, (char *)"name");
771 }
772
773 %rename(register_module) ldb_register_module;
774 ldb_error ldb_register_module(const struct ldb_module_ops *);