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