2 Unix SMB/CIFS implementation.
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
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/>.
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."
32 %module(docstring=DOCSTRING) ldb
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;
57 %include "exception.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 * {
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;
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* {
88 SWIG_exception(SWIG_ValueError,
89 "ldb context must be non-NULL");
92 %typemap(check,noblock=1) ldb_msg * {
94 SWIG_exception(SWIG_ValueError,
95 "Message can not be None");
102 %typemap(in,noblock=1) struct ldb_val *INPUT (struct ldb_val temp) {
104 if (!PyString_Check($input)) {
105 PyErr_SetString(PyExc_TypeError, "string arg expected");
108 $1->length = PyString_Size($input);
109 $1->data = PyString_AsString($input);
113 PyObject *ldb_val_to_py_object(struct ldb_context *ldb_ctx,
114 struct ldb_message_element *el,
117 const struct ldb_schema_attribute *a;
118 struct ldb_val new_val;
119 TALLOC_CTX *mem_ctx = talloc_new(NULL);
124 if (ldb_ctx != NULL) {
125 a = ldb_schema_attribute_by_name(ldb_ctx, el->name);
128 if (a->syntax->ldif_write_fn(ldb_ctx, mem_ctx, val, &new_val) != 0) {
129 talloc_free(mem_ctx);
135 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
137 talloc_free(mem_ctx);
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 **OUT (struct ldb_result *temp_ldb_result) {
157 $1 = &temp_ldb_result;
161 %typemap(argout,noblock=1) struct ldb_result ** (int i) {
162 $result = PyList_New((*$1)->count);
163 for (i = 0; i < (*$1)->count; i++) {
164 PyList_SetItem($result, i,
165 SWIG_NewPointerObj((*$1)->msgs[i], SWIGTYPE_p_ldb_message, 0)
170 %typemap(in,noblock=1,numinputs=1) const char * const *NULL_STR_LIST {
171 if ($input == Py_None) {
173 } else if (PySequence_Check($input)) {
175 $1 = talloc_array(NULL, char *, PySequence_Size($input)+1);
176 for(i = 0; i < PySequence_Size($input); i++)
177 $1[i] = PyString_AsString(PySequence_GetItem($input, i));
180 SWIG_exception(SWIG_TypeError, "expected sequence");
184 %typemap(freearg,noblock=1) const char * const *NULL_STR_LIST {
188 %apply const char * const *NULL_STR_LIST { const char * const *attrs }
189 %apply const char * const *NULL_STR_LIST { const char * const *control_strings }
193 %types(struct ldb_result *, struct ldb_parse_tree *);
199 %rename(__str__) ldb_dn::get_linearized;
200 %rename(__cmp__) ldb_dn::compare;
201 %rename(__len__) ldb_dn::get_comp_num;
203 %feature("docstring") ldb_dn "A LDB distinguished name.";
204 typedef struct ldb_dn {
206 %feature("docstring") ldb_dn "S.__init__(ldb, string)\n" \
208 ldb_dn(ldb *ldb_ctx, const char *str)
210 ldb_dn *ret = ldb_dn_new(ldb_ctx, ldb_ctx, str);
211 /* ldb_dn_new() doesn't accept NULL as memory context, so
212 we do it this way... */
213 talloc_steal(NULL, ret);
216 SWIG_exception(SWIG_ValueError,
217 "unable to parse dn string");
221 ~ldb_dn() { talloc_free($self); }
222 %feature("docstring") validate "S.validate() -> bool\n" \
223 "Validate DN is correct.";
225 const char *get_casefold();
226 const char *get_linearized();
227 %feature("docstring") parent "S.parent() -> dn\n" \
228 "Get the parent for this DN.";
229 ldb_dn *parent() { return ldb_dn_get_parent(NULL, $self); }
230 int compare(ldb_dn *other);
232 %feature("docstring") is_special "S.is_special() -> bool\n" \
233 "Check whether this is a special LDB DN.";
235 %feature("docstring") is_null "S.is_null() -> bool\n" \
236 "Check whether this is a null DN.";
238 bool check_special(const char *name);
240 %feature("docstring") add_child "S.add_child(dn) -> None\n" \
241 "Add a child DN to this DN.";
242 bool add_child(ldb_dn *child);
243 %feature("docstring") add_base "S.add_base(dn) -> None\n" \
244 "Add a base DN to this DN.";
245 bool add_base(ldb_dn *base);
246 %feature("docstring") canonical_str "S.canonical_str() -> string\n" \
247 "Canonical version of this DN (like a posix path).";
248 const char *canonical_str() {
249 return ldb_dn_canonical_string($self, $self);
251 %feature("docstring") canonical_ex_str "S.canonical_ex_str() -> string\n" \
252 "Canonical version of this DN (like a posix path, with terminating newline).";
253 const char *canonical_ex_str() {
254 return ldb_dn_canonical_ex_string($self, $self);
259 char *dn = ldb_dn_get_linearized($self), *ret;
260 asprintf(&ret, "Dn('%s')", dn);
265 ldb_dn *__add__(ldb_dn *other)
267 ldb_dn *ret = ldb_dn_copy(NULL, $self);
268 ldb_dn_add_child(ret, other);
272 /* FIXME: implement __getslice__ */
275 def __eq__(self, other):
276 if isinstance(other, self.__class__):
277 return self.__cmp__(other) == 0
278 if isinstance(other, str):
279 return str(self) == other
287 struct ldb_context *ldb_context_from_py_object(PyObject *py_obj)
289 struct ldb_context *ldb_ctx;
290 if (SWIG_ConvertPtr(py_obj, (void *)&ldb_ctx, SWIGTYPE_p_ldb_context, 0 | 0 ) < 0)
295 int ldb_dn_from_pyobject(TALLOC_CTX *mem_ctx, PyObject *object,
296 struct ldb_context *ldb_ctx, ldb_dn **dn)
300 if (ldb_ctx != NULL && PyString_Check(object)) {
301 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
308 ret = SWIG_ConvertPtr(object, (void **)&odn, SWIGTYPE_p_ldb_dn,
309 SWIG_POINTER_EXCEPTION);
310 *dn = ldb_dn_copy(mem_ctx, odn);
317 ldb_message_element *ldb_msg_element_from_pyobject(TALLOC_CTX *mem_ctx,
318 PyObject *set_obj, int flags,
319 const char *attr_name)
321 struct ldb_message_element *me = talloc(mem_ctx, struct ldb_message_element);
322 me->name = attr_name;
324 if (PyString_Check(set_obj)) {
326 me->values = talloc_array(me, struct ldb_val, me->num_values);
327 me->values[0].length = PyString_Size(set_obj);
328 me->values[0].data = (uint8_t *)talloc_strdup(me->values,
329 PyString_AsString(set_obj));
330 } else if (PySequence_Check(set_obj)) {
332 me->num_values = PySequence_Size(set_obj);
333 me->values = talloc_array(me, struct ldb_val, me->num_values);
334 for (i = 0; i < me->num_values; i++) {
335 PyObject *obj = PySequence_GetItem(set_obj, i);
336 me->values[i].length = PyString_Size(obj);
337 me->values[i].data = (uint8_t *)PyString_AsString(obj);
347 PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
348 ldb_message_element *me)
353 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
354 result = PyList_New(me->num_values);
356 for (i = 0; i < me->num_values; i++) {
357 PyList_SetItem(result, i,
358 ldb_val_to_py_object(ldb_ctx, me, &me->values[i]));
367 /* ldb_message_element */
368 %rename(MessageElement) ldb_message_element;
369 %feature("docstring") ldb_message_element "Message element.";
370 typedef struct ldb_message_element {
373 int __cmp__(ldb_message_element *other)
375 return ldb_msg_element_compare($self, other);
378 PyObject *__iter__(void)
380 return PyObject_GetIter(ldb_msg_element_to_set(NULL, $self));
383 PyObject *__set__(void)
385 return ldb_msg_element_to_set(NULL, $self);
388 ldb_message_element(PyObject *set_obj, int flags=0, const char *name = NULL)
390 return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name);
395 return $self->num_values;
401 if (i < 0 || i >= $self->num_values)
404 return ldb_val_to_py_object(NULL, $self, &$self->values[i]);
407 ~ldb_message_element() { talloc_free($self); }
410 def __getitem__(self, i):
413 raise KeyError("no such value")
417 return "MessageElement([%s])" % (",".join(repr(x) for x in self.__set__()))
419 def __eq__(self, other):
420 if (len(self) == 1 and self.get(0) == other):
422 if isinstance(other, self.__class__):
423 return self.__cmp__(other) == 0
425 for i in range(len(self)):
426 if self.get(i) != o.next():
430 } ldb_message_element;
434 %feature("docstring") ldb_message "Message.";
435 %rename(Message) ldb_message;
437 %rename(__delitem__) ldb_message::remove_attr;
438 %typemap(out) ldb_message_element * {
440 PyErr_SetString(PyExc_KeyError, "no such element");
442 $result = SWIG_NewPointerObj($1, SWIGTYPE_p_ldb_message_element, 0);
446 PyObject *ldb_msg_list_elements(ldb_msg *msg)
449 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
450 if (msg->dn != NULL) {
451 PyList_SetItem(obj, j, PyString_FromString("dn"));
454 for (i = 0; i < msg->num_elements; i++) {
455 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
464 typedef struct ldb_message {
468 ldb_msg(ldb_dn *dn = NULL) {
469 ldb_msg *ret = ldb_msg_new(NULL);
470 ret->dn = talloc_reference(ret, dn);
473 ~ldb_msg() { talloc_free($self); }
474 ldb_message_element *find_element(const char *name);
477 void __setitem__(const char *attr_name, ldb_message_element *val)
479 struct ldb_message_element *el;
481 ldb_msg_remove_attr($self, attr_name);
483 el = talloc($self, struct ldb_message_element);
484 el->name = talloc_strdup(el, attr_name);
485 el->num_values = val->num_values;
486 el->values = talloc_reference(el, val->values);
488 ldb_msg_add($self, el, val->flags);
491 void __setitem__(const char *attr_name, PyObject *val)
493 struct ldb_message_element *el = ldb_msg_element_from_pyobject(NULL,
495 talloc_steal($self, el);
496 ldb_msg_remove_attr($self, attr_name);
497 ldb_msg_add($self, el, el->flags);
500 unsigned int __len__() { return $self->num_elements; }
504 return ldb_msg_list_elements($self);
507 PyObject *__iter__(void)
509 return PyObject_GetIter(ldb_msg_list_elements($self));
512 void remove_attr(const char *name);
514 def get(self, key, default=None):
517 return self.find_element(key)
519 def __getitem__(self, key):
520 ret = self.get(key, None)
522 raise KeyError("No such element")
526 for k in self.keys():
530 return list(self.iteritems())
533 return "Message(%s)" % repr(dict(self.iteritems()))
538 /* FIXME: Convert ldb_result to 3-tuple:
539 (msgs, refs, controls)
542 typedef struct ldb_ldif ldb_ldif;
546 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
548 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
551 PyObject *fn = context;
553 vasprintf(&text, fmt, ap);
554 PyObject_CallFunction(fn, (char *)"(i,s)", level, text);
559 %typemap(in,numinputs=1,noblock=1) (void (*debug)(void *context, enum ldb_debug_level level, const char *fmt, va_list ap), void *context) {
561 /* FIXME: Should be decreased somewhere as well. Perhaps register a
562 destructor and tie it to the ldb context ? */
569 static PyObject *ldb_ldif_to_pyobject(ldb_ldif *ldif)
574 return Py_BuildValue((char *)"(iO)", ldif->changetype,
575 SWIG_NewPointerObj(ldif->msg, SWIGTYPE_p_ldb_message, 0));
585 PyObject *PyExc_LdbError;
589 LdbError = _ldb.LdbError
593 PyExc_LdbError = PyErr_NewException((char *)"_ldb.LdbError", NULL, NULL);
594 PyDict_SetItemString(d, "LdbError", PyExc_LdbError);
597 %ignore _LDB_ERRORS_H_;
599 %include "include/ldb_errors.h"
606 %typemap(out,noblock=1) ldb_error {
607 if ($1 != LDB_SUCCESS) {
608 PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", $1, ldb_errstring(arg1)));
614 %typemap(out,noblock=1) ldb_int_error {
615 if ($1 != LDB_SUCCESS) {
616 PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", $1, ldb_strerror($1)));
622 %typemap(out,noblock=1) struct ldb_control ** {
624 PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(s)", ldb_errstring(arg1)));
627 $result = SWIG_NewPointerObj($1, $1_descriptor, 0);
630 %rename(Ldb) ldb_context;
631 %feature("docstring") ldb_context "Connection to a LDB database.";
633 %typemap(in,noblock=1) struct ldb_dn * {
634 if (ldb_dn_from_pyobject(NULL, $input, arg1, &$1) != 0) {
639 %typemap(freearg,noblock=1) struct ldb_dn * {
643 %typemap(in,numinputs=1) ldb_msg *add_msg {
644 Py_ssize_t dict_pos, msg_pos;
645 ldb_message_element *msgel;
646 PyObject *key, *value;
648 if (PyDict_Check($input)) {
649 PyObject *dn_value = PyDict_GetItemString($input, "dn");
650 $1 = ldb_msg_new(NULL);
651 $1->elements = talloc_zero_array($1, struct ldb_message_element, PyDict_Size($input));
652 msg_pos = dict_pos = 0;
654 /* using argp1 (magic SWIG value) here is a hack */
655 if (ldb_dn_from_pyobject($1, dn_value, argp1, &$1->dn) != 0) {
656 SWIG_exception(SWIG_TypeError, "unable to import dn object");
658 if ($1->dn == NULL) {
659 SWIG_exception(SWIG_TypeError, "dn set but not found");
663 while (PyDict_Next($input, &dict_pos, &key, &value)) {
664 char *key_str = PyString_AsString(key);
665 if (strcmp(key_str, "dn") != 0) {
666 msgel = ldb_msg_element_from_pyobject($1->elements, value, 0, key_str);
668 SWIG_exception(SWIG_TypeError, "unable to import element");
670 memcpy(&$1->elements[msg_pos], msgel, sizeof(*msgel));
675 if ($1->dn == NULL) {
676 SWIG_exception(SWIG_TypeError, "no dn set");
679 $1->num_elements = msg_pos;
681 if (SWIG_ConvertPtr($input, (void **)&$1, SWIGTYPE_p_ldb_message, 0) != 0) {
682 SWIG_exception(SWIG_TypeError, "unable to convert ldb message");
687 /* Top-level ldb operations */
688 typedef struct ldb_context {
689 %rename(firstmodule) modules;
690 struct ldb_module *modules;
693 def itermodules(self):
700 return list(self.itermodules())
705 return ldb_init(NULL, event_context_init(NULL));
708 %feature("docstring") connect "S.connect(url,flags=0,options=None) -> None\n" \
709 "Connect to a LDB URL.";
710 ldb_error connect(const char *url, unsigned int flags = 0,
711 const char *options[] = NULL);
713 ~ldb() { talloc_free($self); }
714 ldb_error search_ex(TALLOC_CTX *mem_ctx,
716 enum ldb_scope scope = LDB_SCOPE_DEFAULT,
717 const char *expression = NULL,
718 const char *const *attrs = NULL,
719 struct ldb_control **controls = NULL,
720 struct ldb_result **OUT) {
722 struct ldb_result *res;
723 struct ldb_request *req;
724 res = talloc_zero(mem_ctx, struct ldb_result);
726 return LDB_ERR_OPERATIONS_ERROR;
729 ret = ldb_build_search_req(&req, $self, mem_ctx,
730 base?base:ldb_get_default_basedn($self),
736 ldb_search_default_callback);
738 if (ret != LDB_SUCCESS) {
743 ldb_set_timeout($self, req, 0); /* use default timeout */
745 ret = ldb_request($self, req);
747 if (ret == LDB_SUCCESS) {
748 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
757 %feature("docstring") delete "S.delete(dn) -> None\n" \
759 ldb_error delete(ldb_dn *dn);
760 %feature("docstring") rename "S.rename(old_dn, new_dn) -> None\n" \
762 ldb_error rename(ldb_dn *olddn, ldb_dn *newdn);
763 struct ldb_control **parse_control_strings(TALLOC_CTX *mem_ctx,
764 const char * const*control_strings);
765 %feature("docstring") add "S.add(message) -> None\n" \
767 ldb_error add(ldb_msg *add_msg);
768 %feature("docstring") modify "S.modify(message) -> None\n" \
770 ldb_error modify(ldb_msg *message);
771 ldb_dn *get_config_basedn();
772 ldb_dn *get_root_basedn();
773 ldb_dn *get_schema_basedn();
774 ldb_dn *get_default_basedn();
775 PyObject *schema_format_value(const char *element_name, PyObject *val)
777 const struct ldb_schema_attribute *a;
778 struct ldb_val old_val;
779 struct ldb_val new_val;
780 TALLOC_CTX *mem_ctx = talloc_new(NULL);
783 old_val.data = PyString_AsString(val);
784 old_val.length = PyString_Size(val);
786 a = ldb_schema_attribute_by_name($self, element_name);
792 if (a->syntax->ldif_write_fn($self, mem_ctx, &old_val, &new_val) != 0) {
793 talloc_free(mem_ctx);
797 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
799 talloc_free(mem_ctx);
804 const char *errstring();
805 %feature("docstring") set_create_perms "S.set_create_perms(mode) -> None\n" \
806 "Set mode to use when creating new LDB files.";
807 void set_create_perms(unsigned int perms);
808 %feature("docstring") set_modules_dir "S.set_modules_dir(path) -> None\n" \
809 "Set path LDB should search for modules";
810 void set_modules_dir(const char *path);
811 %feature("docstring") set_debug "S.set_debug(callback) -> None\n" \
812 "Set callback for LDB debug messages.\n" \
813 "The callback should accept a debug level and debug text.";
814 ldb_error set_debug(void (*debug)(void *context, enum ldb_debug_level level,
815 const char *fmt, va_list ap),
817 %feature("docstring") set_opaque "S.set_opaque(name, value) -> None\n" \
818 "Set an opaque value on this LDB connection. \n"
819 ":note: Passing incorrect values may cause crashes.";
820 ldb_error set_opaque(const char *name, void *value);
821 %feature("docstring") get_opaque "S.get_opaque(name) -> value\n" \
822 "Get an opaque value set on this LDB connection. \n"
823 ":note: The returned value may not be useful in Python.";
824 void *get_opaque(const char *name);
825 %feature("docstring") transaction_start "S.transaction_start() -> None\n" \
826 "Start a new transaction.";
827 ldb_error transaction_start();
828 %feature("docstring") transaction_commit "S.transaction_commit() -> None\n" \
829 "Commit currently active transaction.";
830 ldb_error transaction_commit();
831 %feature("docstring") transaction_cancel "S.transaction_cancel() -> None\n" \
832 "Cancel currently active transaction.";
833 ldb_error transaction_cancel();
834 void schema_attribute_remove(const char *name);
835 ldb_error schema_attribute_add(const char *attribute, unsigned flags, const char *syntax);
836 ldb_error setup_wellknown_attributes(void);
839 %typemap(in,numinputs=0,noblock=1) struct ldb_result **result_as_bool (struct ldb_result *tmp) { $1 = &tmp; }
840 %typemap(argout,noblock=1) struct ldb_result **result_as_bool { $result = ((*$1)->count > 0)?Py_True:Py_False; }
841 %typemap(freearg,noblock=1) struct ldb_result **result_as_bool { talloc_free(*$1); }
842 ldb_error __contains__(ldb_dn *dn, struct ldb_result **result_as_bool)
844 return ldb_search($self, dn, LDB_SCOPE_BASE, NULL, NULL,
848 %feature("docstring") parse_ldif "S.parse_ldif(ldif) -> iter(messages)\n" \
849 "Parse a string formatted using LDIF.";
851 PyObject *parse_ldif(const char *s)
853 PyObject *list = PyList_New(0);
854 struct ldb_ldif *ldif;
855 while ((ldif = ldb_ldif_read_string($self, &s)) != NULL) {
856 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
858 return PyObject_GetIter(list);
864 asprintf(&ret, "<ldb connection at 0x%x>", ret);
870 def __init__(self, url=None, flags=0, options=None):
871 """Create a new LDB object.
873 Will also connect to the specified URL if one was given.
875 _ldb.Ldb_swiginit(self,_ldb.new_Ldb())
877 self.connect(url, flags, options)
879 def search(self, base=None, scope=SCOPE_DEFAULT, expression=None,
880 attrs=None, controls=None):
881 """Search in a database.
883 :param base: Optional base DN to search
884 :param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)
885 :param expression: Optional search expression
886 :param attrs: Attributes to return (defaults to all)
887 :param controls: Optional list of controls
888 :return: Iterator over Message objects
890 if not (attrs is None or isinstance(attrs, list)):
891 raise TypeError("attributes not a list")
892 parsed_controls = None
893 if controls is not None:
894 parsed_controls = self.parse_control_strings(controls)
895 return self.search_ex(base, scope, expression, attrs,
901 %typemap(in,noblock=1) struct ldb_dn *;
902 %typemap(freearg,noblock=1) struct ldb_dn *;
904 %nodefault ldb_message;
905 %nodefault ldb_context;
908 %rename(valid_attr_name) ldb_valid_attr_name;
909 %feature("docstring") ldb_valid_attr_name "S.valid_attr_name(name) -> bool\n"
910 "Check whether the supplied name is a valid attribute name.";
911 int ldb_valid_attr_name(const char *s);
913 typedef unsigned long time_t;
915 %feature("docstring") timestring "S.timestring(int) -> string\n"
916 "Generate a LDAP time string from a UNIX timestamp";
919 static char *timestring(time_t t)
921 char *tresult = ldb_timestring(NULL, t);
922 char *result = strdup(tresult);
923 talloc_free(tresult);
928 %rename(string_to_time) ldb_string_to_time;
929 %feature("docstring") ldb_string_to_time "S.string_to_time(string) -> int\n"
930 "Parse a LDAP time string into a UNIX timestamp.";
931 time_t ldb_string_to_time(const char *s);
933 typedef struct ldb_module {
934 struct ldb_module *prev, *next;
938 const char *__str__() {
939 return $self->ops->name;
943 asprintf(&ret, "<ldb module '%s'>", $self->ops->name);
947 int search(struct ldb_request *req) {
948 return $self->ops->search($self, req);
950 ldb_error add(struct ldb_request *req) {
951 return $self->ops->add($self, req);
953 ldb_error modify(struct ldb_request *req) {
954 return $self->ops->modify($self, req);
956 ldb_error delete(struct ldb_request *req) {
957 return $self->ops->del($self, req);
959 ldb_error rename(struct ldb_request *req) {
960 return $self->ops->rename($self, req);
962 ldb_error start_transaction() {
963 return $self->ops->start_transaction($self);
965 ldb_error end_transaction() {
966 return $self->ops->end_transaction($self);
968 ldb_error del_transaction() {
969 return $self->ops->del_transaction($self);
975 int py_module_search(struct ldb_module *mod, struct ldb_request *req)
977 PyObject *py_ldb = mod->private_data;
978 PyObject *py_result, *py_base, *py_attrs, *py_tree;
980 py_base = SWIG_NewPointerObj(req->op.search.base, SWIGTYPE_p_ldb_dn, 0);
983 return LDB_ERR_OPERATIONS_ERROR;
985 py_tree = SWIG_NewPointerObj(req->op.search.tree, SWIGTYPE_p_ldb_parse_tree, 0);
988 return LDB_ERR_OPERATIONS_ERROR;
990 if (req->op.search.attrs == NULL) {
994 for (len = 0; req->op.search.attrs[len]; len++);
995 py_attrs = PyList_New(len);
996 for (i = 0; i < len; i++)
997 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1000 py_result = PyObject_CallMethod(py_ldb, "search", "OiOO", py_base, req->op.search.scope, py_tree, py_attrs);
1002 Py_DECREF(py_attrs);
1006 if (py_result == NULL) {
1007 return LDB_ERR_OPERATIONS_ERROR;
1010 if (SWIG_ConvertPtr(py_result, &req->op.search.res, SWIGTYPE_p_ldb_result, 0) != 0) {
1011 return LDB_ERR_OPERATIONS_ERROR;
1014 Py_DECREF(py_result);
1019 int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1021 PyObject *py_ldb = mod->private_data;
1022 PyObject *py_result, *py_msg;
1024 py_msg = SWIG_NewPointerObj(req->op.add.message, SWIGTYPE_p_ldb_message, 0);
1026 if (py_msg == NULL) {
1027 return LDB_ERR_OPERATIONS_ERROR;
1030 py_result = PyObject_CallMethod(py_ldb, "add", "O", py_msg);
1034 if (py_result == NULL) {
1035 return LDB_ERR_OPERATIONS_ERROR;
1038 Py_DECREF(py_result);
1043 int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1045 PyObject *py_ldb = mod->private_data;
1046 PyObject *py_result, *py_msg;
1048 py_msg = SWIG_NewPointerObj(req->op.mod.message, SWIGTYPE_p_ldb_message, 0);
1050 if (py_msg == NULL) {
1051 return LDB_ERR_OPERATIONS_ERROR;
1054 py_result = PyObject_CallMethod(py_ldb, "modify", "O", py_msg);
1058 if (py_result == NULL) {
1059 return LDB_ERR_OPERATIONS_ERROR;
1062 Py_DECREF(py_result);
1067 int py_module_del(struct ldb_module *mod, struct ldb_request *req)
1069 PyObject *py_ldb = mod->private_data;
1070 PyObject *py_result, *py_dn;
1072 py_dn = SWIG_NewPointerObj(req->op.del.dn, SWIGTYPE_p_ldb_dn, 0);
1075 return LDB_ERR_OPERATIONS_ERROR;
1077 py_result = PyObject_CallMethod(py_ldb, "delete", "O", py_dn);
1079 if (py_result == NULL) {
1080 return LDB_ERR_OPERATIONS_ERROR;
1083 Py_DECREF(py_result);
1088 int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
1090 PyObject *py_ldb = mod->private_data;
1091 PyObject *py_result, *py_olddn, *py_newdn;
1093 py_olddn = SWIG_NewPointerObj(req->op.rename.olddn, SWIGTYPE_p_ldb_dn, 0);
1095 if (py_olddn == NULL)
1096 return LDB_ERR_OPERATIONS_ERROR;
1098 py_newdn = SWIG_NewPointerObj(req->op.rename.newdn, SWIGTYPE_p_ldb_dn, 0);
1100 if (py_newdn == NULL)
1101 return LDB_ERR_OPERATIONS_ERROR;
1103 py_result = PyObject_CallMethod(py_ldb, "rename", "OO", py_olddn, py_newdn);
1105 Py_DECREF(py_olddn);
1106 Py_DECREF(py_newdn);
1108 if (py_result == NULL) {
1109 return LDB_ERR_OPERATIONS_ERROR;
1112 Py_DECREF(py_result);
1117 int py_module_request(struct ldb_module *mod, struct ldb_request *req)
1119 PyObject *py_ldb = mod->private_data;
1120 PyObject *py_result;
1122 py_result = PyObject_CallMethod(py_ldb, "request", "");
1124 return LDB_ERR_OPERATIONS_ERROR;
1127 int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
1129 PyObject *py_ldb = mod->private_data;
1130 PyObject *py_result;
1132 py_result = PyObject_CallMethod(py_ldb, "extended", "");
1134 return LDB_ERR_OPERATIONS_ERROR;
1137 int py_module_start_transaction(struct ldb_module *mod)
1139 PyObject *py_ldb = mod->private_data;
1140 PyObject *py_result;
1142 py_result = PyObject_CallMethod(py_ldb, "start_transaction", "");
1144 if (py_result == NULL) {
1145 return LDB_ERR_OPERATIONS_ERROR;
1148 Py_DECREF(py_result);
1153 int py_module_end_transaction(struct ldb_module *mod)
1155 PyObject *py_ldb = mod->private_data;
1156 PyObject *py_result;
1158 py_result = PyObject_CallMethod(py_ldb, "end_transaction", "");
1160 if (py_result == NULL) {
1161 return LDB_ERR_OPERATIONS_ERROR;
1164 Py_DECREF(py_result);
1169 int py_module_del_transaction(struct ldb_module *mod)
1171 PyObject *py_ldb = mod->private_data;
1172 PyObject *py_result;
1174 py_result = PyObject_CallMethod(py_ldb, "del_transaction", "");
1176 if (py_result == NULL) {
1177 return LDB_ERR_OPERATIONS_ERROR;
1180 Py_DECREF(py_result);
1185 int py_module_wait(struct ldb_handle *mod, enum ldb_wait_type wait_type)
1187 PyObject *py_ldb = mod->private_data;
1188 PyObject *py_result;
1190 py_result = PyObject_CallMethod(py_ldb, "wait", "i", wait_type);
1192 if (py_result == NULL) {
1193 return LDB_ERR_OPERATIONS_ERROR;
1196 Py_DECREF(py_result);
1201 int py_module_sequence_number(struct ldb_module *mod, struct ldb_request *req)
1203 PyObject *py_ldb = mod->private_data;
1204 PyObject *py_result;
1207 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);
1209 if (py_result == NULL) {
1210 return LDB_ERR_OPERATIONS_ERROR;
1213 ret = PyInt_AsLong(py_result);
1215 Py_DECREF(py_result);
1220 static int py_module_destructor(void *_mod)
1222 struct ldb_module *mod = _mod;
1223 Py_DECREF((PyObject *)mod->private_data);
1227 int py_module_init (struct ldb_module *mod)
1229 PyObject *py_class = mod->ops->private_data;
1230 PyObject *py_result, *py_next, *py_ldb;
1232 py_ldb = SWIG_NewPointerObj(mod->ldb, SWIGTYPE_p_ldb_context, 0);
1235 return LDB_ERR_OPERATIONS_ERROR;
1237 py_next = SWIG_NewPointerObj(mod->next, SWIGTYPE_p_ldb_module, 0);
1239 if (py_next == NULL)
1240 return LDB_ERR_OPERATIONS_ERROR;
1242 py_result = PyObject_CallFunction(py_class, "OO", py_ldb, py_next);
1244 if (py_result == NULL) {
1245 return LDB_ERR_OPERATIONS_ERROR;
1248 mod->private_data = py_result;
1250 talloc_set_destructor (mod, py_module_destructor);
1252 return ldb_next_init(mod);
1256 %typemap(in,noblock=1) const struct ldb_module_ops * {
1257 $1 = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
1259 $1->name = talloc_strdup($1, PyString_AsString(PyObject_GetAttrString($input, (char *)"name")));
1262 $1->private_data = $input;
1263 $1->init_context = py_module_init;
1264 $1->search = py_module_search;
1265 $1->add = py_module_add;
1266 $1->modify = py_module_modify;
1267 $1->del = py_module_del;
1268 $1->rename = py_module_rename;
1269 $1->request = py_module_request;
1270 $1->extended = py_module_extended;
1271 $1->start_transaction = py_module_start_transaction;
1272 $1->end_transaction = py_module_end_transaction;
1273 $1->del_transaction = py_module_del_transaction;
1274 $1->wait = py_module_wait;
1275 $1->sequence_number = py_module_sequence_number;
1278 %feature("docstring") ldb_register_module "S.register_module(module) -> None\n"
1279 "Register a LDB module.";
1280 %rename(register_module) ldb_register_module;
1281 ldb_int_error ldb_register_module(const struct ldb_module_ops *);
1284 __docformat__ = "restructuredText"