LDB ASYNC: Core files
[Samba/ekacnet.git] / source4 / lib / ldb / ldb.i
blob1e3a3169c891550f61d842d7612c14c1f210251c
1 /*
2 Unix SMB/CIFS implementation.
4 Swig interface to ldb.
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>
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
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.
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.
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/>.
28 %define DOCSTRING
29 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server."
30 %enddef
32 %module(docstring=DOCSTRING) ldb
36 #include <stdint.h>
37 #include <stdbool.h>
38 #include "talloc.h"
39 #include "events.h"
40 #include "ldb.h"
41 #include "ldb_errors.h"
42 #include "ldb_private.h"
44 typedef struct ldb_message ldb_msg;
45 typedef struct ldb_context ldb;
46 typedef struct ldb_dn ldb_dn;
47 typedef struct ldb_ldif ldb_ldif;
48 typedef struct ldb_message_element ldb_message_element;
49 typedef struct ldb_module ldb_module;
50 typedef int ldb_error;
51 typedef int ldb_int_error;
55 %import "carrays.i"
56 %import "typemaps.i"
57 %include "exception.i"
58 %import "stdint.i"
60 /* Don't expose talloc contexts in Python code. Python does reference
61 counting for us, so just create a new top-level talloc context.
63 %typemap(in, numinputs=0, noblock=1) TALLOC_CTX * {
64 $1 = NULL;
69 %constant int SCOPE_DEFAULT = LDB_SCOPE_DEFAULT;
70 %constant int SCOPE_BASE = LDB_SCOPE_BASE;
71 %constant int SCOPE_ONELEVEL = LDB_SCOPE_ONELEVEL;
72 %constant int SCOPE_SUBTREE = LDB_SCOPE_SUBTREE;
74 %constant int CHANGETYPE_NONE = LDB_CHANGETYPE_NONE;
75 %constant int CHANGETYPE_ADD = LDB_CHANGETYPE_ADD;
76 %constant int CHANGETYPE_DELETE = LDB_CHANGETYPE_DELETE;
77 %constant int CHANGETYPE_MODIFY = LDB_CHANGETYPE_MODIFY;
79 /*
80 * Wrap struct ldb_context
83 /* The ldb functions will crash if a NULL ldb context is passed so
84 catch this before it happens. */
86 %typemap(check,noblock=1) struct ldb_context* {
87 if ($1 == NULL)
88 SWIG_exception(SWIG_ValueError,
89 "ldb context must be non-NULL");
92 %typemap(check,noblock=1) ldb_msg * {
93 if ($1 == NULL)
94 SWIG_exception(SWIG_ValueError,
95 "Message can not be None");
99 * Wrap struct ldb_val
102 %typemap(in,noblock=1) struct ldb_val *INPUT (struct ldb_val temp) {
103 $1 = &temp;
104 if (!PyString_Check($input)) {
105 PyErr_SetString(PyExc_TypeError, "string arg expected");
106 return NULL;
108 $1->length = PyString_Size($input);
109 $1->data = PyString_AsString($input);
112 %inline %{
113 PyObject *ldb_val_to_py_object(struct ldb_context *ldb_ctx,
114 struct ldb_message_element *el,
115 struct ldb_val *val)
117 const struct ldb_schema_attribute *a;
118 struct ldb_val new_val;
119 TALLOC_CTX *mem_ctx = talloc_new(NULL);
120 PyObject *ret;
122 new_val = *val;
124 if (ldb_ctx != NULL) {
125 a = ldb_schema_attribute_by_name(ldb_ctx, el->name);
127 if (a != NULL) {
128 if (a->syntax->ldif_write_fn(ldb_ctx, mem_ctx, val, &new_val) != 0) {
129 talloc_free(mem_ctx);
130 return NULL;
135 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
137 talloc_free(mem_ctx);
139 return ret;
144 %typemap(out,noblock=1) struct ldb_val * {
145 $result = PyString_FromStringAndSize((const char *)$1->data, $1->length)
148 %typemap(out,noblock=1) struct ldb_val {
149 $result = PyString_FromStringAndSize((const char *)$1.data, $1.length)
153 * Wrap struct ldb_result
156 %typemap(in,noblock=1,numinputs=0) struct ldb_result ** (struct ldb_result *temp_ldb_result) {
157 $1 = &temp_ldb_result;
160 #ifdef SWIGPYTHON
161 %typemap(argout,noblock=1) struct ldb_result ** (int i) {
162 if ($1 == NULL) {
163 $result = Py_None;
164 } else {
165 $result = PyList_New((*$1)->count);
166 for (i = 0; i < (*$1)->count; i++) {
167 PyList_SetItem($result, i,
168 SWIG_NewPointerObj((*$1)->msgs[i], SWIGTYPE_p_ldb_message, 0)
174 %typemap(in,noblock=1,numinputs=1) const char * const *NULL_STR_LIST {
175 if ($input == Py_None) {
176 $1 = NULL;
177 } else if (PySequence_Check($input)) {
178 int i;
179 $1 = talloc_array(NULL, char *, PySequence_Size($input)+1);
180 for(i = 0; i < PySequence_Size($input); i++)
181 $1[i] = PyString_AsString(PySequence_GetItem($input, i));
182 $1[i] = NULL;
183 } else {
184 SWIG_exception(SWIG_TypeError, "expected sequence");
188 %typemap(freearg,noblock=1) const char * const *NULL_STR_LIST {
189 talloc_free($1);
192 %apply const char * const *NULL_STR_LIST { const char * const *attrs }
193 %apply const char * const *NULL_STR_LIST { const char * const *control_strings }
195 #endif
197 %types(struct ldb_result *, struct ldb_parse_tree *);
200 * Wrap struct ldb_dn
203 %rename(__str__) ldb_dn::get_linearized;
204 %rename(__cmp__) ldb_dn::compare;
205 %rename(__len__) ldb_dn::get_comp_num;
206 %rename(Dn) ldb_dn;
207 %feature("docstring") ldb_dn "A LDB distinguished name.";
208 typedef struct ldb_dn {
209 %extend {
210 %feature("docstring") ldb_dn "S.__init__(ldb, string)\n" \
211 "Create a new DN.";
212 ldb_dn(ldb *ldb_ctx, const char *str)
214 ldb_dn *ret = ldb_dn_new(ldb_ctx, ldb_ctx, str);
215 /* ldb_dn_new() doesn't accept NULL as memory context, so
216 we do it this way... */
217 talloc_steal(NULL, ret);
219 if (ret == NULL)
220 SWIG_exception(SWIG_ValueError,
221 "unable to parse dn string");
222 fail:
223 return ret;
225 ~ldb_dn() { talloc_free($self); }
226 %feature("docstring") validate "S.validate() -> bool\n" \
227 "Validate DN is correct.";
228 bool validate();
229 const char *get_casefold();
230 const char *get_linearized();
231 %feature("docstring") parent "S.parent() -> dn\n" \
232 "Get the parent for this DN.";
233 ldb_dn *parent() { return ldb_dn_get_parent(NULL, $self); }
234 int compare(ldb_dn *other);
235 bool is_valid();
236 %feature("docstring") is_special "S.is_special() -> bool\n" \
237 "Check whether this is a special LDB DN.";
238 bool is_special();
239 %feature("docstring") is_null "S.is_null() -> bool\n" \
240 "Check whether this is a null DN.";
241 bool is_null();
242 bool check_special(const char *name);
243 int get_comp_num();
244 %feature("docstring") add_child "S.add_child(dn) -> None\n" \
245 "Add a child DN to this DN.";
246 bool add_child(ldb_dn *child);
247 %feature("docstring") add_base "S.add_base(dn) -> None\n" \
248 "Add a base DN to this DN.";
249 bool add_base(ldb_dn *base);
250 %feature("docstring") canonical_str "S.canonical_str() -> string\n" \
251 "Canonical version of this DN (like a posix path).";
252 const char *canonical_str() {
253 return ldb_dn_canonical_string($self, $self);
255 %feature("docstring") canonical_ex_str "S.canonical_ex_str() -> string\n" \
256 "Canonical version of this DN (like a posix path, with terminating newline).";
257 const char *canonical_ex_str() {
258 return ldb_dn_canonical_ex_string($self, $self);
260 #ifdef SWIGPYTHON
261 char *__repr__(void)
263 char *dn = ldb_dn_get_linearized($self), *ret;
264 asprintf(&ret, "Dn('%s')", dn);
265 talloc_free(dn);
266 return ret;
269 ldb_dn *__add__(ldb_dn *other)
271 ldb_dn *ret = ldb_dn_copy(NULL, $self);
272 ldb_dn_add_child(ret, other);
273 return ret;
276 /* FIXME: implement __getslice__ */
277 #endif
278 %pythoncode {
279 def __eq__(self, other):
280 if isinstance(other, self.__class__):
281 return self.__cmp__(other) == 0
282 if isinstance(other, str):
283 return str(self) == other
284 return False
287 } ldb_dn;
289 #ifdef SWIGPYTHON
291 struct ldb_context *ldb_context_from_py_object(PyObject *py_obj)
293 struct ldb_context *ldb_ctx;
294 if (SWIG_ConvertPtr(py_obj, (void *)&ldb_ctx, SWIGTYPE_p_ldb_context, 0 | 0 ) < 0)
295 return NULL;
296 return ldb_ctx;
299 int ldb_dn_from_pyobject(TALLOC_CTX *mem_ctx, PyObject *object,
300 struct ldb_context *ldb_ctx, ldb_dn **dn)
302 int ret;
303 struct ldb_dn *odn;
304 if (ldb_ctx != NULL && PyString_Check(object)) {
305 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
306 if (!odn) {
307 return SWIG_ERROR;
309 *dn = odn;
310 return 0;
312 ret = SWIG_ConvertPtr(object, (void **)&odn, SWIGTYPE_p_ldb_dn,
313 SWIG_POINTER_EXCEPTION);
314 *dn = ldb_dn_copy(mem_ctx, odn);
315 if (odn && !*dn) {
316 return SWIG_ERROR;
318 return ret;
321 ldb_message_element *ldb_msg_element_from_pyobject(TALLOC_CTX *mem_ctx,
322 PyObject *set_obj, int flags,
323 const char *attr_name)
325 struct ldb_message_element *me = talloc(mem_ctx, struct ldb_message_element);
326 me->name = attr_name;
327 me->flags = flags;
328 if (PyString_Check(set_obj)) {
329 me->num_values = 1;
330 me->values = talloc_array(me, struct ldb_val, me->num_values);
331 me->values[0].length = PyString_Size(set_obj);
332 me->values[0].data = (uint8_t *)talloc_strdup(me->values,
333 PyString_AsString(set_obj));
334 } else if (PySequence_Check(set_obj)) {
335 int i;
336 me->num_values = PySequence_Size(set_obj);
337 me->values = talloc_array(me, struct ldb_val, me->num_values);
338 for (i = 0; i < me->num_values; i++) {
339 PyObject *obj = PySequence_GetItem(set_obj, i);
340 me->values[i].length = PyString_Size(obj);
341 me->values[i].data = (uint8_t *)PyString_AsString(obj);
343 } else {
344 talloc_free(me);
345 me = NULL;
348 return me;
351 PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
352 ldb_message_element *me)
354 int i;
355 PyObject *result;
357 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
358 result = PyList_New(me->num_values);
360 for (i = 0; i < me->num_values; i++) {
361 PyList_SetItem(result, i,
362 ldb_val_to_py_object(ldb_ctx, me, &me->values[i]));
365 return result;
369 #endif
371 /* ldb_message_element */
372 %rename(MessageElement) ldb_message_element;
373 %feature("docstring") ldb_message_element "Message element.";
374 typedef struct ldb_message_element {
375 %extend {
376 #ifdef SWIGPYTHON
377 int __cmp__(ldb_message_element *other)
379 return ldb_msg_element_compare($self, other);
382 PyObject *__iter__(void)
384 return PyObject_GetIter(ldb_msg_element_to_set(NULL, $self));
387 PyObject *__set__(void)
389 return ldb_msg_element_to_set(NULL, $self);
392 ldb_message_element(PyObject *set_obj, int flags=0, const char *name = NULL)
394 return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name);
397 int __len__()
399 return $self->num_values;
401 #endif
403 PyObject *get(int i)
405 if (i < 0 || i >= $self->num_values)
406 return Py_None;
408 return ldb_val_to_py_object(NULL, $self, &$self->values[i]);
411 ~ldb_message_element() { talloc_free($self); }
413 %pythoncode {
414 def __getitem__(self, i):
415 ret = self.get(i)
416 if ret is None:
417 raise KeyError("no such value")
418 return ret
420 def __repr__(self):
421 return "MessageElement([%s])" % (",".join(repr(x) for x in self.__set__()))
423 def __eq__(self, other):
424 if (len(self) == 1 and self.get(0) == other):
425 return True
426 if isinstance(other, self.__class__):
427 return self.__cmp__(other) == 0
428 o = iter(other)
429 for i in range(len(self)):
430 if self.get(i) != o.next():
431 return False
432 return True
434 } ldb_message_element;
436 /* ldb_message */
438 %feature("docstring") ldb_message "Message.";
439 %rename(Message) ldb_message;
440 #ifdef SWIGPYTHON
441 %rename(__delitem__) ldb_message::remove_attr;
442 %typemap(out) ldb_message_element * {
443 if ($1 == NULL)
444 PyErr_SetString(PyExc_KeyError, "no such element");
445 else
446 $result = SWIG_NewPointerObj($1, SWIGTYPE_p_ldb_message_element, 0);
449 %inline {
450 PyObject *ldb_msg_list_elements(ldb_msg *msg)
452 int i, j = 0;
453 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
454 if (msg->dn != NULL) {
455 PyList_SetItem(obj, j, PyString_FromString("dn"));
456 j++;
458 for (i = 0; i < msg->num_elements; i++) {
459 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
460 j++;
462 return obj;
466 #endif
468 typedef struct ldb_message {
469 ldb_dn *dn;
471 %extend {
472 ldb_msg(ldb_dn *dn = NULL) {
473 ldb_msg *ret = ldb_msg_new(NULL);
474 ret->dn = talloc_reference(ret, dn);
475 return ret;
477 ~ldb_msg() { talloc_free($self); }
478 ldb_message_element *find_element(const char *name);
480 #ifdef SWIGPYTHON
481 void __setitem__(const char *attr_name, ldb_message_element *val)
483 struct ldb_message_element *el;
485 ldb_msg_remove_attr($self, attr_name);
487 el = talloc($self, struct ldb_message_element);
488 el->name = talloc_strdup(el, attr_name);
489 el->num_values = val->num_values;
490 el->values = talloc_reference(el, val->values);
492 ldb_msg_add($self, el, val->flags);
495 void __setitem__(const char *attr_name, PyObject *val)
497 struct ldb_message_element *el = ldb_msg_element_from_pyobject(NULL,
498 val, 0, attr_name);
499 talloc_steal($self, el);
500 ldb_msg_remove_attr($self, attr_name);
501 ldb_msg_add($self, el, el->flags);
504 unsigned int __len__() { return $self->num_elements; }
506 PyObject *keys(void)
508 return ldb_msg_list_elements($self);
511 PyObject *__iter__(void)
513 return PyObject_GetIter(ldb_msg_list_elements($self));
515 #endif
516 void remove_attr(const char *name);
517 %pythoncode {
518 def get(self, key, default=None):
519 if key == "dn":
520 return self.dn
521 return self.find_element(key)
523 def __getitem__(self, key):
524 ret = self.get(key, None)
525 if ret is None:
526 raise KeyError("No such element")
527 return ret
529 def iteritems(self):
530 for k in self.keys():
531 yield k, self[k]
533 def items(self):
534 return list(self.iteritems())
536 def __repr__(self):
537 return "Message(%s)" % repr(dict(self.iteritems()))
540 } ldb_msg;
542 /* FIXME: Convert ldb_result to 3-tuple:
543 (msgs, refs, controls)
546 typedef struct ldb_ldif ldb_ldif;
548 #ifdef SWIGPYTHON
550 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
552 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
554 char *text;
555 PyObject *fn = context;
557 vasprintf(&text, fmt, ap);
558 PyObject_CallFunction(fn, (char *)"(i,s)", level, text);
559 free(text);
563 %typemap(in,numinputs=1,noblock=1) (void (*debug)(void *context, enum ldb_debug_level level, const char *fmt, va_list ap), void *context) {
564 $1 = py_ldb_debug;
565 /* FIXME: Should be decreased somewhere as well. Perhaps register a
566 destructor and tie it to the ldb context ? */
567 Py_INCREF($input);
568 $2 = $input;
570 #endif
572 %inline {
573 static PyObject *ldb_ldif_to_pyobject(ldb_ldif *ldif)
575 if (ldif == NULL) {
576 return Py_None;
577 } else {
578 return Py_BuildValue((char *)"(iO)", ldif->changetype,
579 SWIG_NewPointerObj(ldif->msg, SWIGTYPE_p_ldb_message, 0));
585 * Wrap ldb errors
589 PyObject *PyExc_LdbError;
592 %pythoncode %{
593 LdbError = _ldb.LdbError
596 %init %{
597 PyExc_LdbError = PyErr_NewException((char *)"_ldb.LdbError", NULL, NULL);
598 PyDict_SetItemString(d, "LdbError", PyExc_LdbError);
601 %ignore _LDB_ERRORS_H_;
602 %ignore LDB_SUCCESS;
603 %include "include/ldb_errors.h"
606 * Wrap ldb functions
610 %typemap(out,noblock=1) ldb_error {
611 if ($1 != LDB_SUCCESS) {
612 PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", $1, ldb_errstring(arg1)));
613 SWIG_fail;
615 $result = Py_None;
618 %typemap(out,noblock=1) ldb_int_error {
619 if ($1 != LDB_SUCCESS) {
620 PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", $1, ldb_strerror($1)));
621 SWIG_fail;
623 $result = Py_None;
626 %typemap(out,noblock=1) struct ldb_control ** {
627 if ($1 == NULL) {
628 PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(s)", ldb_errstring(arg1)));
629 SWIG_fail;
631 $result = SWIG_NewPointerObj($1, $1_descriptor, 0);
634 %rename(Ldb) ldb_context;
635 %feature("docstring") ldb_context "Connection to a LDB database.";
637 %typemap(in,noblock=1) struct ldb_dn * {
638 if (ldb_dn_from_pyobject(NULL, $input, arg1, &$1) != 0) {
639 SWIG_fail;
643 %typemap(freearg,noblock=1) struct ldb_dn * {
644 talloc_free($1);
647 %typemap(in,numinputs=1) ldb_msg *add_msg {
648 Py_ssize_t dict_pos, msg_pos;
649 ldb_message_element *msgel;
650 PyObject *key, *value;
652 if (PyDict_Check($input)) {
653 PyObject *dn_value = PyDict_GetItemString($input, "dn");
654 $1 = ldb_msg_new(NULL);
655 $1->elements = talloc_zero_array($1, struct ldb_message_element, PyDict_Size($input));
656 msg_pos = dict_pos = 0;
657 if (dn_value) {
658 /* using argp1 (magic SWIG value) here is a hack */
659 if (ldb_dn_from_pyobject($1, dn_value, argp1, &$1->dn) != 0) {
660 SWIG_exception(SWIG_TypeError, "unable to import dn object");
662 if ($1->dn == NULL) {
663 SWIG_exception(SWIG_TypeError, "dn set but not found");
667 while (PyDict_Next($input, &dict_pos, &key, &value)) {
668 char *key_str = PyString_AsString(key);
669 if (strcmp(key_str, "dn") != 0) {
670 msgel = ldb_msg_element_from_pyobject($1->elements, value, 0, key_str);
671 if (msgel == NULL) {
672 SWIG_exception(SWIG_TypeError, "unable to import element");
674 memcpy(&$1->elements[msg_pos], msgel, sizeof(*msgel));
675 msg_pos++;
679 if ($1->dn == NULL) {
680 SWIG_exception(SWIG_TypeError, "no dn set");
683 $1->num_elements = msg_pos;
684 } else {
685 if (SWIG_ConvertPtr($input, (void **)&$1, SWIGTYPE_p_ldb_message, 0) != 0) {
686 SWIG_exception(SWIG_TypeError, "unable to convert ldb message");
691 /* Top-level ldb operations */
692 typedef struct ldb_context {
693 %rename(firstmodule) modules;
694 struct ldb_module *modules;
696 %pythoncode {
697 def itermodules(self):
698 m = self.firstmodule
699 while m is not None:
700 yield m
701 m = m.next
703 def modules(self):
704 return list(self.itermodules())
707 %extend {
708 ldb(void) {
709 return ldb_init(NULL, event_context_init(NULL));
712 %feature("docstring") connect "S.connect(url,flags=0,options=None) -> None\n" \
713 "Connect to a LDB URL.";
714 ldb_error connect(const char *url, unsigned int flags = 0,
715 const char *options[] = NULL);
717 ~ldb() { talloc_free($self); }
718 ldb_error search_ex(TALLOC_CTX *mem_ctx,
719 ldb_dn *base = NULL,
720 enum ldb_scope scope = LDB_SCOPE_DEFAULT,
721 const char *expression = NULL,
722 const char *const *attrs = NULL,
723 struct ldb_control **controls = NULL,
724 struct ldb_result **OUT) {
725 int ret;
726 struct ldb_result *res;
727 struct ldb_request *req;
728 res = talloc_zero(mem_ctx, struct ldb_result);
729 if (!res) {
730 return LDB_ERR_OPERATIONS_ERROR;
733 ret = ldb_build_search_req(&req, $self, mem_ctx,
734 base?base:ldb_get_default_basedn($self),
735 scope,
736 expression,
737 attrs,
738 controls,
739 res,
740 ldb_search_default_callback,
741 NULL);
743 if (ret != LDB_SUCCESS) {
744 talloc_free(res);
745 return ret;
748 ret = ldb_request($self, req);
750 if (ret == LDB_SUCCESS) {
751 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
754 talloc_free(req);
756 *OUT = res;
757 return ret;
760 %feature("docstring") delete "S.delete(dn) -> None\n" \
761 "Remove an entry.";
762 ldb_error delete(ldb_dn *dn);
763 %feature("docstring") rename "S.rename(old_dn, new_dn) -> None\n" \
764 "Rename an entry.";
765 ldb_error rename(ldb_dn *olddn, ldb_dn *newdn);
766 struct ldb_control **parse_control_strings(TALLOC_CTX *mem_ctx,
767 const char * const*control_strings);
768 %feature("docstring") add "S.add(message) -> None\n" \
769 "Add an entry.";
770 ldb_error add(ldb_msg *add_msg);
771 %feature("docstring") modify "S.modify(message) -> None\n" \
772 "Modify an entry.";
773 ldb_error modify(ldb_msg *message);
774 ldb_dn *get_config_basedn();
775 ldb_dn *get_root_basedn();
776 ldb_dn *get_schema_basedn();
777 ldb_dn *get_default_basedn();
778 PyObject *schema_format_value(const char *element_name, PyObject *val)
780 const struct ldb_schema_attribute *a;
781 struct ldb_val old_val;
782 struct ldb_val new_val;
783 TALLOC_CTX *mem_ctx = talloc_new(NULL);
784 PyObject *ret;
786 old_val.data = PyString_AsString(val);
787 old_val.length = PyString_Size(val);
789 a = ldb_schema_attribute_by_name($self, element_name);
791 if (a == NULL) {
792 return Py_None;
795 if (a->syntax->ldif_write_fn($self, mem_ctx, &old_val, &new_val) != 0) {
796 talloc_free(mem_ctx);
797 return Py_None;
800 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
802 talloc_free(mem_ctx);
804 return ret;
807 const char *errstring();
808 %feature("docstring") set_create_perms "S.set_create_perms(mode) -> None\n" \
809 "Set mode to use when creating new LDB files.";
810 void set_create_perms(unsigned int perms);
811 %feature("docstring") set_modules_dir "S.set_modules_dir(path) -> None\n" \
812 "Set path LDB should search for modules";
813 void set_modules_dir(const char *path);
814 %feature("docstring") set_debug "S.set_debug(callback) -> None\n" \
815 "Set callback for LDB debug messages.\n" \
816 "The callback should accept a debug level and debug text.";
817 ldb_error set_debug(void (*debug)(void *context, enum ldb_debug_level level,
818 const char *fmt, va_list ap),
819 void *context);
820 %feature("docstring") set_opaque "S.set_opaque(name, value) -> None\n" \
821 "Set an opaque value on this LDB connection. \n"
822 ":note: Passing incorrect values may cause crashes.";
823 ldb_error set_opaque(const char *name, void *value);
824 %feature("docstring") get_opaque "S.get_opaque(name) -> value\n" \
825 "Get an opaque value set on this LDB connection. \n"
826 ":note: The returned value may not be useful in Python.";
827 void *get_opaque(const char *name);
828 %feature("docstring") transaction_start "S.transaction_start() -> None\n" \
829 "Start a new transaction.";
830 ldb_error transaction_start();
831 %feature("docstring") transaction_commit "S.transaction_commit() -> None\n" \
832 "Commit currently active transaction.";
833 ldb_error transaction_commit();
834 %feature("docstring") transaction_cancel "S.transaction_cancel() -> None\n" \
835 "Cancel currently active transaction.";
836 ldb_error transaction_cancel();
837 void schema_attribute_remove(const char *name);
838 ldb_error schema_attribute_add(const char *attribute, unsigned flags, const char *syntax);
839 ldb_error setup_wellknown_attributes(void);
841 #ifdef SWIGPYTHON
842 %typemap(in,numinputs=0,noblock=1) struct ldb_result **result_as_bool (struct ldb_result *tmp) { $1 = &tmp; }
843 %typemap(argout,noblock=1) struct ldb_result **result_as_bool { $result = ((*$1)->count > 0)?Py_True:Py_False; }
844 %typemap(freearg,noblock=1) struct ldb_result **result_as_bool { talloc_free(*$1); }
845 ldb_error __contains__(ldb_dn *dn, struct ldb_result **result_as_bool)
847 return ldb_search($self, $self, result_as_bool, dn, LDB_SCOPE_BASE, NULL, NULL);
850 %feature("docstring") parse_ldif "S.parse_ldif(ldif) -> iter(messages)\n" \
851 "Parse a string formatted using LDIF.";
853 PyObject *parse_ldif(const char *s)
855 PyObject *list = PyList_New(0);
856 struct ldb_ldif *ldif;
857 while ((ldif = ldb_ldif_read_string($self, &s)) != NULL) {
858 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
860 return PyObject_GetIter(list);
863 char *__repr__(void)
865 char *ret;
866 asprintf(&ret, "<ldb connection at 0x%x>", ret);
867 return ret;
869 #endif
871 %pythoncode {
872 def __init__(self, url=None, flags=0, options=None):
873 """Create a new LDB object.
875 Will also connect to the specified URL if one was given.
877 _ldb.Ldb_swiginit(self,_ldb.new_Ldb())
878 if url is not None:
879 self.connect(url, flags, options)
881 def search(self, base=None, scope=SCOPE_DEFAULT, expression=None,
882 attrs=None, controls=None):
883 """Search in a database.
885 :param base: Optional base DN to search
886 :param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)
887 :param expression: Optional search expression
888 :param attrs: Attributes to return (defaults to all)
889 :param controls: Optional list of controls
890 :return: Iterator over Message objects
892 if not (attrs is None or isinstance(attrs, list)):
893 raise TypeError("attributes not a list")
894 parsed_controls = None
895 if controls is not None:
896 parsed_controls = self.parse_control_strings(controls)
897 return self.search_ex(base, scope, expression, attrs,
898 parsed_controls)
901 } ldb;
903 %typemap(in,noblock=1) struct ldb_dn *;
904 %typemap(freearg,noblock=1) struct ldb_dn *;
906 %nodefault ldb_message;
907 %nodefault ldb_context;
908 %nodefault Dn;
910 %rename(valid_attr_name) ldb_valid_attr_name;
911 %feature("docstring") ldb_valid_attr_name "S.valid_attr_name(name) -> bool\n"
912 "Check whether the supplied name is a valid attribute name.";
913 int ldb_valid_attr_name(const char *s);
915 typedef unsigned long time_t;
917 %feature("docstring") timestring "S.timestring(int) -> string\n"
918 "Generate a LDAP time string from a UNIX timestamp";
920 %inline %{
921 static char *timestring(time_t t)
923 char *tresult = ldb_timestring(NULL, t);
924 char *result = strdup(tresult);
925 talloc_free(tresult);
926 return result;
930 %rename(string_to_time) ldb_string_to_time;
931 %feature("docstring") ldb_string_to_time "S.string_to_time(string) -> int\n"
932 "Parse a LDAP time string into a UNIX timestamp.";
933 time_t ldb_string_to_time(const char *s);
935 typedef struct ldb_module {
936 struct ldb_module *prev, *next;
938 %extend {
939 #ifdef SWIGPYTHON
940 const char *__str__() {
941 return $self->ops->name;
943 char *__repr__() {
944 char *ret;
945 asprintf(&ret, "<ldb module '%s'>", $self->ops->name);
946 return ret;
948 #endif
949 int search(struct ldb_dn *base, enum ldb_scope scope, struct ldb_parse_tree *tree, const char * const * attrs, struct ldb_result **res) {
950 int ret;
951 struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
953 req->operation = LDB_SEARCH;
954 req->op.search.base = base;
955 req->op.search.scope = scope;
956 req->op.search.tree = tree;
957 req->op.search.attrs = attrs;
959 req->op.search.res = talloc_zero(NULL, struct ldb_result);
961 ret = $self->ops->search($self, req);
963 *res = req->op.search.res;
965 talloc_free(req);
967 return ret;
969 ldb_error add(struct ldb_message *message) {
970 struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
971 req->operation = LDB_ADD;
972 req->op.add.message = message;
974 return $self->ops->add($self, &req);
976 ldb_error modify(struct ldb_message *message) {
977 struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
978 req->operation = LDB_MODIFY;
979 req->op.mod.message = message;
981 return $self->ops->modify($self, &req);
983 ldb_error delete(struct ldb_dn *dn) {
984 struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
985 req->operation = LDB_DELETE;
986 req->op.del.dn = dn;
988 return $self->ops->del($self, &req);
991 ldb_error rename(struct ldb_dn *olddn, struct ldb_dn *newdn) {
992 struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
993 req->operation = LDB_RENAME;
994 req->op.rename.olddn = olddn;
995 req->op.rename.olddn = newdn;
997 return $self->ops->rename($self, &req);
999 ldb_error start_transaction() {
1000 return $self->ops->start_transaction($self);
1002 ldb_error end_transaction() {
1003 return $self->ops->end_transaction($self);
1005 ldb_error del_transaction() {
1006 return $self->ops->del_transaction($self);
1009 } ldb_module;
1012 int py_module_search(struct ldb_module *mod, struct ldb_request *req)
1014 PyObject *py_ldb = mod->private_data;
1015 PyObject *py_result, *py_base, *py_attrs, *py_tree;
1017 py_base = SWIG_NewPointerObj(req->op.search.base, SWIGTYPE_p_ldb_dn, 0);
1019 if (py_base == NULL)
1020 return LDB_ERR_OPERATIONS_ERROR;
1022 py_tree = SWIG_NewPointerObj(req->op.search.tree, SWIGTYPE_p_ldb_parse_tree, 0);
1024 if (py_tree == NULL)
1025 return LDB_ERR_OPERATIONS_ERROR;
1027 if (req->op.search.attrs == NULL) {
1028 py_attrs = Py_None;
1029 } else {
1030 int i, len;
1031 for (len = 0; req->op.search.attrs[len]; len++);
1032 py_attrs = PyList_New(len);
1033 for (i = 0; i < len; i++)
1034 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1037 py_result = PyObject_CallMethod(py_ldb, "search", "OiOO", py_base, req->op.search.scope, py_tree, py_attrs);
1039 Py_DECREF(py_attrs);
1040 Py_DECREF(py_tree);
1041 Py_DECREF(py_base);
1043 if (py_result == NULL) {
1044 return LDB_ERR_OPERATIONS_ERROR;
1047 if (SWIG_ConvertPtr(py_result, &req->op.search.res, SWIGTYPE_p_ldb_result, 0) != 0) {
1048 return LDB_ERR_OPERATIONS_ERROR;
1051 Py_DECREF(py_result);
1053 return LDB_SUCCESS;
1056 int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1058 PyObject *py_ldb = mod->private_data;
1059 PyObject *py_result, *py_msg;
1061 py_msg = SWIG_NewPointerObj(req->op.add.message, SWIGTYPE_p_ldb_message, 0);
1063 if (py_msg == NULL) {
1064 return LDB_ERR_OPERATIONS_ERROR;
1067 py_result = PyObject_CallMethod(py_ldb, "add", "O", py_msg);
1069 Py_DECREF(py_msg);
1071 if (py_result == NULL) {
1072 return LDB_ERR_OPERATIONS_ERROR;
1075 Py_DECREF(py_result);
1077 return LDB_SUCCESS;
1080 int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1082 PyObject *py_ldb = mod->private_data;
1083 PyObject *py_result, *py_msg;
1085 py_msg = SWIG_NewPointerObj(req->op.mod.message, SWIGTYPE_p_ldb_message, 0);
1087 if (py_msg == NULL) {
1088 return LDB_ERR_OPERATIONS_ERROR;
1091 py_result = PyObject_CallMethod(py_ldb, "modify", "O", py_msg);
1093 Py_DECREF(py_msg);
1095 if (py_result == NULL) {
1096 return LDB_ERR_OPERATIONS_ERROR;
1099 Py_DECREF(py_result);
1101 return LDB_SUCCESS;
1104 int py_module_del(struct ldb_module *mod, struct ldb_request *req)
1106 PyObject *py_ldb = mod->private_data;
1107 PyObject *py_result, *py_dn;
1109 py_dn = SWIG_NewPointerObj(req->op.del.dn, SWIGTYPE_p_ldb_dn, 0);
1111 if (py_dn == NULL)
1112 return LDB_ERR_OPERATIONS_ERROR;
1114 py_result = PyObject_CallMethod(py_ldb, "delete", "O", py_dn);
1116 if (py_result == NULL) {
1117 return LDB_ERR_OPERATIONS_ERROR;
1120 Py_DECREF(py_result);
1122 return LDB_SUCCESS;
1125 int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
1127 PyObject *py_ldb = mod->private_data;
1128 PyObject *py_result, *py_olddn, *py_newdn;
1130 py_olddn = SWIG_NewPointerObj(req->op.rename.olddn, SWIGTYPE_p_ldb_dn, 0);
1132 if (py_olddn == NULL)
1133 return LDB_ERR_OPERATIONS_ERROR;
1135 py_newdn = SWIG_NewPointerObj(req->op.rename.newdn, SWIGTYPE_p_ldb_dn, 0);
1137 if (py_newdn == NULL)
1138 return LDB_ERR_OPERATIONS_ERROR;
1140 py_result = PyObject_CallMethod(py_ldb, "rename", "OO", py_olddn, py_newdn);
1142 Py_DECREF(py_olddn);
1143 Py_DECREF(py_newdn);
1145 if (py_result == NULL) {
1146 return LDB_ERR_OPERATIONS_ERROR;
1149 Py_DECREF(py_result);
1151 return LDB_SUCCESS;
1154 int py_module_request(struct ldb_module *mod, struct ldb_request *req)
1156 PyObject *py_ldb = mod->private_data;
1157 PyObject *py_result;
1159 py_result = PyObject_CallMethod(py_ldb, "request", "");
1161 return LDB_ERR_OPERATIONS_ERROR;
1164 int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
1166 PyObject *py_ldb = mod->private_data;
1167 PyObject *py_result;
1169 py_result = PyObject_CallMethod(py_ldb, "extended", "");
1171 return LDB_ERR_OPERATIONS_ERROR;
1174 int py_module_start_transaction(struct ldb_module *mod)
1176 PyObject *py_ldb = mod->private_data;
1177 PyObject *py_result;
1179 py_result = PyObject_CallMethod(py_ldb, "start_transaction", "");
1181 if (py_result == NULL) {
1182 return LDB_ERR_OPERATIONS_ERROR;
1185 Py_DECREF(py_result);
1187 return LDB_SUCCESS;
1190 int py_module_end_transaction(struct ldb_module *mod)
1192 PyObject *py_ldb = mod->private_data;
1193 PyObject *py_result;
1195 py_result = PyObject_CallMethod(py_ldb, "end_transaction", "");
1197 if (py_result == NULL) {
1198 return LDB_ERR_OPERATIONS_ERROR;
1201 Py_DECREF(py_result);
1203 return LDB_SUCCESS;
1206 int py_module_del_transaction(struct ldb_module *mod)
1208 PyObject *py_ldb = mod->private_data;
1209 PyObject *py_result;
1211 py_result = PyObject_CallMethod(py_ldb, "del_transaction", "");
1213 if (py_result == NULL) {
1214 return LDB_ERR_OPERATIONS_ERROR;
1217 Py_DECREF(py_result);
1219 return LDB_SUCCESS;
1222 int py_module_wait(struct ldb_handle *mod, enum ldb_wait_type wait_type)
1224 PyObject *py_ldb = mod->private_data;
1225 PyObject *py_result;
1227 py_result = PyObject_CallMethod(py_ldb, "wait", "i", wait_type);
1229 if (py_result == NULL) {
1230 return LDB_ERR_OPERATIONS_ERROR;
1233 Py_DECREF(py_result);
1235 return LDB_SUCCESS;
1238 int py_module_sequence_number(struct ldb_module *mod, struct ldb_request *req)
1240 PyObject *py_ldb = mod->private_data;
1241 PyObject *py_result;
1242 int ret;
1244 py_result = PyObject_CallMethod(py_ldb, "sequence_number", "ili", req->op.seq_num.type, req->op.seq_num.seq_num, req->op.seq_num.flags);
1246 if (py_result == NULL) {
1247 return LDB_ERR_OPERATIONS_ERROR;
1250 ret = PyInt_AsLong(py_result);
1252 Py_DECREF(py_result);
1254 return ret;
1257 static int py_module_destructor(void *_mod)
1259 struct ldb_module *mod = _mod;
1260 Py_DECREF((PyObject *)mod->private_data);
1261 return 0;
1264 int py_module_init (struct ldb_module *mod)
1266 PyObject *py_class = mod->ops->private_data;
1267 PyObject *py_result, *py_next, *py_ldb;
1269 py_ldb = SWIG_NewPointerObj(mod->ldb, SWIGTYPE_p_ldb_context, 0);
1271 if (py_ldb == NULL)
1272 return LDB_ERR_OPERATIONS_ERROR;
1274 py_next = SWIG_NewPointerObj(mod->next, SWIGTYPE_p_ldb_module, 0);
1276 if (py_next == NULL)
1277 return LDB_ERR_OPERATIONS_ERROR;
1279 py_result = PyObject_CallFunction(py_class, "OO", py_ldb, py_next);
1281 if (py_result == NULL) {
1282 return LDB_ERR_OPERATIONS_ERROR;
1285 mod->private_data = py_result;
1287 talloc_set_destructor (mod, py_module_destructor);
1289 return ldb_next_init(mod);
1293 %typemap(in,noblock=1) const struct ldb_module_ops * {
1294 $1 = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
1296 $1->name = talloc_strdup($1, PyString_AsString(PyObject_GetAttrString($input, (char *)"name")));
1298 Py_INCREF($input);
1299 $1->private_data = $input;
1300 $1->init_context = py_module_init;
1301 $1->search = py_module_search;
1302 $1->add = py_module_add;
1303 $1->modify = py_module_modify;
1304 $1->del = py_module_del;
1305 $1->rename = py_module_rename;
1306 $1->request = py_module_request;
1307 $1->extended = py_module_extended;
1308 $1->start_transaction = py_module_start_transaction;
1309 $1->end_transaction = py_module_end_transaction;
1310 $1->del_transaction = py_module_del_transaction;
1311 $1->wait = py_module_wait;
1312 $1->sequence_number = py_module_sequence_number;
1315 %feature("docstring") ldb_register_module "S.register_module(module) -> None\n"
1316 "Register a LDB module.";
1317 %rename(register_module) ldb_register_module;
1318 ldb_int_error ldb_register_module(const struct ldb_module_ops *);
1320 %pythoncode {
1321 __docformat__ = "restructuredText"
1322 open = Ldb