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 ** (struct ldb_result *temp_ldb_result) {
157 $1 = &temp_ldb_result;
161 %typemap(argout,noblock=1) struct ldb_result ** (int i) {
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) {
177 } else if (PySequence_Check($input)) {
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));
184 SWIG_exception(SWIG_TypeError, "expected sequence");
188 %typemap(freearg,noblock=1) const char * const *NULL_STR_LIST {
192 %apply const char * const *NULL_STR_LIST { const char * const *attrs }
193 %apply const char * const *NULL_STR_LIST { const char * const *options }
194 %apply const char * const *NULL_STR_LIST { const char * const *control_strings }
198 %types(struct ldb_result *, struct ldb_parse_tree *);
204 %rename(__str__) ldb_dn::get_linearized;
205 %rename(__cmp__) ldb_dn::compare;
206 %rename(__len__) ldb_dn::get_comp_num;
208 %feature("docstring") ldb_dn "A LDB distinguished name.";
209 typedef struct ldb_dn {
211 %feature("docstring") ldb_dn "S.__init__(ldb, string)\n" \
213 ldb_dn(ldb *ldb_ctx, const char *str)
215 ldb_dn *ret = ldb_dn_new(ldb_ctx, ldb_ctx, str);
216 /* ldb_dn_new() doesn't accept NULL as memory context, so
217 we do it this way... */
218 talloc_steal(NULL, ret);
221 SWIG_exception(SWIG_ValueError,
222 "unable to parse dn string");
226 ~ldb_dn() { talloc_free($self); }
227 %feature("docstring") validate "S.validate() -> bool\n" \
228 "Validate DN is correct.";
230 const char *get_casefold();
231 const char *get_linearized();
232 %feature("docstring") parent "S.parent() -> dn\n" \
233 "Get the parent for this DN.";
234 ldb_dn *parent() { return ldb_dn_get_parent(NULL, $self); }
235 int compare(ldb_dn *other);
237 %feature("docstring") is_special "S.is_special() -> bool\n" \
238 "Check whether this is a special LDB DN.";
240 %feature("docstring") is_null "S.is_null() -> bool\n" \
241 "Check whether this is a null DN.";
243 bool check_special(const char *name);
245 %feature("docstring") add_child "S.add_child(dn) -> None\n" \
246 "Add a child DN to this DN.";
247 bool add_child(ldb_dn *child);
248 %feature("docstring") add_base "S.add_base(dn) -> None\n" \
249 "Add a base DN to this DN.";
250 bool add_base(ldb_dn *base);
251 %feature("docstring") canonical_str "S.canonical_str() -> string\n" \
252 "Canonical version of this DN (like a posix path).";
253 const char *canonical_str() {
254 return ldb_dn_canonical_string($self, $self);
256 %feature("docstring") canonical_ex_str "S.canonical_ex_str() -> string\n" \
257 "Canonical version of this DN (like a posix path, with terminating newline).";
258 const char *canonical_ex_str() {
259 return ldb_dn_canonical_ex_string($self, $self);
264 char *dn = ldb_dn_get_linearized($self), *ret;
265 asprintf(&ret, "Dn('%s')", dn);
270 ldb_dn *__add__(ldb_dn *other)
272 ldb_dn *ret = ldb_dn_copy(NULL, $self);
273 ldb_dn_add_child(ret, other);
277 /* FIXME: implement __getslice__ */
280 def __eq__(self, other):
281 if isinstance(other, self.__class__):
282 return self.__cmp__(other) == 0
283 if isinstance(other, str):
284 return str(self) == other
292 struct ldb_context *ldb_context_from_py_object(PyObject *py_obj)
294 struct ldb_context *ldb_ctx;
295 if (SWIG_ConvertPtr(py_obj, (void *)&ldb_ctx, SWIGTYPE_p_ldb_context, 0 | 0 ) < 0)
300 int ldb_dn_from_pyobject(TALLOC_CTX *mem_ctx, PyObject *object,
301 struct ldb_context *ldb_ctx, ldb_dn **dn)
305 if (ldb_ctx != NULL && PyString_Check(object)) {
306 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
313 ret = SWIG_ConvertPtr(object, (void **)&odn, SWIGTYPE_p_ldb_dn,
314 SWIG_POINTER_EXCEPTION);
315 *dn = ldb_dn_copy(mem_ctx, odn);
322 ldb_message_element *ldb_msg_element_from_pyobject(TALLOC_CTX *mem_ctx,
323 PyObject *set_obj, int flags,
324 const char *attr_name)
326 struct ldb_message_element *me = talloc(mem_ctx, struct ldb_message_element);
327 me->name = attr_name;
329 if (PyString_Check(set_obj)) {
331 me->values = talloc_array(me, struct ldb_val, me->num_values);
332 me->values[0].length = PyString_Size(set_obj);
333 me->values[0].data = (uint8_t *)talloc_strdup(me->values,
334 PyString_AsString(set_obj));
335 } else if (PySequence_Check(set_obj)) {
337 me->num_values = PySequence_Size(set_obj);
338 me->values = talloc_array(me, struct ldb_val, me->num_values);
339 for (i = 0; i < me->num_values; i++) {
340 PyObject *obj = PySequence_GetItem(set_obj, i);
341 me->values[i].length = PyString_Size(obj);
342 me->values[i].data = (uint8_t *)PyString_AsString(obj);
352 PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
353 ldb_message_element *me)
358 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
359 result = PyList_New(me->num_values);
361 for (i = 0; i < me->num_values; i++) {
362 PyList_SetItem(result, i,
363 ldb_val_to_py_object(ldb_ctx, me, &me->values[i]));
372 /* ldb_message_element */
373 %rename(MessageElement) ldb_message_element;
374 %feature("docstring") ldb_message_element "Message element.";
375 typedef struct ldb_message_element {
378 int __cmp__(ldb_message_element *other)
380 return ldb_msg_element_compare($self, other);
383 PyObject *__iter__(void)
385 return PyObject_GetIter(ldb_msg_element_to_set(NULL, $self));
388 PyObject *__set__(void)
390 return ldb_msg_element_to_set(NULL, $self);
393 ldb_message_element(PyObject *set_obj, int flags=0, const char *name = NULL)
395 return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name);
400 return $self->num_values;
406 if (i < 0 || i >= $self->num_values)
409 return ldb_val_to_py_object(NULL, $self, &$self->values[i]);
412 ~ldb_message_element() { talloc_free($self); }
415 def __getitem__(self, i):
418 raise KeyError("no such value")
422 return "MessageElement([%s])" % (",".join(repr(x) for x in self.__set__()))
424 def __eq__(self, other):
425 if (len(self) == 1 and self.get(0) == other):
427 if isinstance(other, self.__class__):
428 return self.__cmp__(other) == 0
430 for i in range(len(self)):
431 if self.get(i) != o.next():
435 } ldb_message_element;
439 %feature("docstring") ldb_message "Message.";
440 %rename(Message) ldb_message;
442 %rename(__delitem__) ldb_message::remove_attr;
443 %typemap(out) ldb_message_element * {
445 PyErr_SetString(PyExc_KeyError, "no such element");
447 $result = SWIG_NewPointerObj($1, SWIGTYPE_p_ldb_message_element, 0);
451 PyObject *ldb_msg_list_elements(ldb_msg *msg)
454 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
455 if (msg->dn != NULL) {
456 PyList_SetItem(obj, j, PyString_FromString("dn"));
459 for (i = 0; i < msg->num_elements; i++) {
460 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
469 typedef struct ldb_message {
473 ldb_msg(ldb_dn *dn = NULL) {
474 ldb_msg *ret = ldb_msg_new(NULL);
475 ret->dn = talloc_reference(ret, dn);
478 ~ldb_msg() { talloc_free($self); }
479 ldb_message_element *find_element(const char *name);
482 void __setitem__(const char *attr_name, ldb_message_element *val)
484 struct ldb_message_element *el;
486 ldb_msg_remove_attr($self, attr_name);
488 el = talloc($self, struct ldb_message_element);
489 el->name = talloc_strdup(el, attr_name);
490 el->num_values = val->num_values;
491 el->values = talloc_reference(el, val->values);
493 ldb_msg_add($self, el, val->flags);
496 void __setitem__(const char *attr_name, PyObject *val)
498 struct ldb_message_element *el = ldb_msg_element_from_pyobject(NULL,
500 talloc_steal($self, el);
501 ldb_msg_remove_attr($self, attr_name);
502 ldb_msg_add($self, el, el->flags);
505 unsigned int __len__() { return $self->num_elements; }
509 return ldb_msg_list_elements($self);
512 PyObject *__iter__(void)
514 return PyObject_GetIter(ldb_msg_list_elements($self));
517 void remove_attr(const char *name);
519 def get(self, key, default=None):
522 return self.find_element(key)
524 def __getitem__(self, key):
525 ret = self.get(key, None)
527 raise KeyError("No such element")
531 for k in self.keys():
535 return list(self.iteritems())
538 return "Message(%s)" % repr(dict(self.iteritems()))
543 /* FIXME: Convert ldb_result to 3-tuple:
544 (msgs, refs, controls)
547 typedef struct ldb_ldif ldb_ldif;
551 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
553 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
556 PyObject *fn = context;
558 vasprintf(&text, fmt, ap);
559 PyObject_CallFunction(fn, (char *)"(i,s)", level, text);
564 %typemap(in,numinputs=1,noblock=1) (void (*debug)(void *context, enum ldb_debug_level level, const char *fmt, va_list ap), void *context) {
566 /* FIXME: Should be decreased somewhere as well. Perhaps register a
567 destructor and tie it to the ldb context ? */
574 static PyObject *ldb_ldif_to_pyobject(ldb_ldif *ldif)
579 return Py_BuildValue((char *)"(iO)", ldif->changetype,
580 SWIG_NewPointerObj(ldif->msg, SWIGTYPE_p_ldb_message, 0));
590 PyObject *PyExc_LdbError;
594 LdbError = _ldb.LdbError
598 PyExc_LdbError = PyErr_NewException((char *)"_ldb.LdbError", NULL, NULL);
599 PyDict_SetItemString(d, "LdbError", PyExc_LdbError);
602 %ignore _LDB_ERRORS_H_;
604 %include "include/ldb_errors.h"
611 %typemap(out,noblock=1) ldb_error {
612 if ($1 != LDB_SUCCESS) {
613 PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", $1, ldb_errstring(arg1)));
619 %typemap(out,noblock=1) ldb_int_error {
620 if ($1 != LDB_SUCCESS) {
621 PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", $1, ldb_strerror($1)));
627 %typemap(out,noblock=1) struct ldb_control ** {
629 PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(s)", ldb_errstring(arg1)));
632 $result = SWIG_NewPointerObj($1, $1_descriptor, 0);
635 %rename(Ldb) ldb_context;
636 %feature("docstring") ldb_context "Connection to a LDB database.";
638 %typemap(in,noblock=1) struct ldb_dn * {
639 if (ldb_dn_from_pyobject(NULL, $input, arg1, &$1) != 0) {
644 %typemap(freearg,noblock=1) struct ldb_dn * {
648 %typemap(in,numinputs=1) ldb_msg *add_msg {
649 Py_ssize_t dict_pos, msg_pos;
650 ldb_message_element *msgel;
651 PyObject *key, *value;
653 if (PyDict_Check($input)) {
654 PyObject *dn_value = PyDict_GetItemString($input, "dn");
655 $1 = ldb_msg_new(NULL);
656 $1->elements = talloc_zero_array($1, struct ldb_message_element, PyDict_Size($input));
657 msg_pos = dict_pos = 0;
659 /* using argp1 (magic SWIG value) here is a hack */
660 if (ldb_dn_from_pyobject($1, dn_value, argp1, &$1->dn) != 0) {
661 SWIG_exception(SWIG_TypeError, "unable to import dn object");
663 if ($1->dn == NULL) {
664 SWIG_exception(SWIG_TypeError, "dn set but not found");
668 while (PyDict_Next($input, &dict_pos, &key, &value)) {
669 char *key_str = PyString_AsString(key);
670 if (strcmp(key_str, "dn") != 0) {
671 msgel = ldb_msg_element_from_pyobject($1->elements, value, 0, key_str);
673 SWIG_exception(SWIG_TypeError, "unable to import element");
675 memcpy(&$1->elements[msg_pos], msgel, sizeof(*msgel));
680 if ($1->dn == NULL) {
681 SWIG_exception(SWIG_TypeError, "no dn set");
684 $1->num_elements = msg_pos;
686 if (SWIG_ConvertPtr($input, (void **)&$1, SWIGTYPE_p_ldb_message, 0) != 0) {
687 SWIG_exception(SWIG_TypeError, "unable to convert ldb message");
692 /* Top-level ldb operations */
693 typedef struct ldb_context {
694 %rename(firstmodule) modules;
695 struct ldb_module *modules;
698 def itermodules(self):
705 return list(self.itermodules())
710 return ldb_init(NULL, event_context_init(NULL));
713 %feature("docstring") connect "S.connect(url,flags=0,options=None) -> None\n" \
714 "Connect to a LDB URL.";
715 ldb_error connect(const char *url, unsigned int flags = 0,
716 const char *const *options = NULL);
718 ~ldb() { talloc_free($self); }
720 ldb_error search_ex(TALLOC_CTX *mem_ctx,
722 enum ldb_scope scope = LDB_SCOPE_DEFAULT,
723 const char *expression = NULL,
724 const char *const *attrs = NULL,
725 struct ldb_control **controls = NULL,
726 struct ldb_result **OUT) {
728 struct ldb_result *res;
729 struct ldb_request *req;
730 res = talloc_zero(mem_ctx, struct ldb_result);
732 return LDB_ERR_OPERATIONS_ERROR;
735 ret = ldb_build_search_req(&req, $self, mem_ctx,
736 base?base:ldb_get_default_basedn($self),
742 ldb_search_default_callback,
745 if (ret != LDB_SUCCESS) {
750 ret = ldb_request($self, req);
752 if (ret == LDB_SUCCESS) {
753 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
762 %feature("docstring") delete "S.delete(dn) -> None\n" \
764 ldb_error delete(ldb_dn *dn);
765 %feature("docstring") rename "S.rename(old_dn, new_dn) -> None\n" \
767 ldb_error rename(ldb_dn *olddn, ldb_dn *newdn);
768 struct ldb_control **parse_control_strings(TALLOC_CTX *mem_ctx,
769 const char * const*control_strings);
770 %feature("docstring") add "S.add(message) -> None\n" \
772 ldb_error add(ldb_msg *add_msg);
773 %feature("docstring") modify "S.modify(message) -> None\n" \
775 ldb_error modify(ldb_msg *message);
776 ldb_dn *get_config_basedn();
777 ldb_dn *get_root_basedn();
778 ldb_dn *get_schema_basedn();
779 ldb_dn *get_default_basedn();
780 PyObject *schema_format_value(const char *element_name, PyObject *val)
782 const struct ldb_schema_attribute *a;
783 struct ldb_val old_val;
784 struct ldb_val new_val;
785 TALLOC_CTX *mem_ctx = talloc_new(NULL);
788 old_val.data = PyString_AsString(val);
789 old_val.length = PyString_Size(val);
791 a = ldb_schema_attribute_by_name($self, element_name);
797 if (a->syntax->ldif_write_fn($self, mem_ctx, &old_val, &new_val) != 0) {
798 talloc_free(mem_ctx);
802 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
804 talloc_free(mem_ctx);
809 const char *errstring();
810 %feature("docstring") set_create_perms "S.set_create_perms(mode) -> None\n" \
811 "Set mode to use when creating new LDB files.";
812 void set_create_perms(unsigned int perms);
813 %feature("docstring") set_modules_dir "S.set_modules_dir(path) -> None\n" \
814 "Set path LDB should search for modules";
815 void set_modules_dir(const char *path);
816 %feature("docstring") set_debug "S.set_debug(callback) -> None\n" \
817 "Set callback for LDB debug messages.\n" \
818 "The callback should accept a debug level and debug text.";
819 ldb_error set_debug(void (*debug)(void *context, enum ldb_debug_level level,
820 const char *fmt, va_list ap),
822 %feature("docstring") set_opaque "S.set_opaque(name, value) -> None\n" \
823 "Set an opaque value on this LDB connection. \n"
824 ":note: Passing incorrect values may cause crashes.";
825 ldb_error set_opaque(const char *name, void *value);
826 %feature("docstring") get_opaque "S.get_opaque(name) -> value\n" \
827 "Get an opaque value set on this LDB connection. \n"
828 ":note: The returned value may not be useful in Python.";
829 void *get_opaque(const char *name);
830 %feature("docstring") transaction_start "S.transaction_start() -> None\n" \
831 "Start a new transaction.";
832 ldb_error transaction_start();
833 %feature("docstring") transaction_commit "S.transaction_commit() -> None\n" \
834 "Commit currently active transaction.";
835 ldb_error transaction_commit();
836 %feature("docstring") transaction_cancel "S.transaction_cancel() -> None\n" \
837 "Cancel currently active transaction.";
838 ldb_error transaction_cancel();
839 void schema_attribute_remove(const char *name);
840 ldb_error schema_attribute_add(const char *attribute, unsigned flags, const char *syntax);
841 ldb_error setup_wellknown_attributes(void);
844 %typemap(in,numinputs=0,noblock=1) struct ldb_result **result_as_bool (struct ldb_result *tmp) { $1 = &tmp; }
845 %typemap(argout,noblock=1) struct ldb_result **result_as_bool { $result = ((*$1)->count > 0)?Py_True:Py_False; }
846 %typemap(freearg,noblock=1) struct ldb_result **result_as_bool { talloc_free(*$1); }
847 ldb_error __contains__(ldb_dn *dn, struct ldb_result **result_as_bool)
849 return ldb_search($self, $self, result_as_bool, dn, LDB_SCOPE_BASE, NULL, NULL);
852 %feature("docstring") parse_ldif "S.parse_ldif(ldif) -> iter(messages)\n" \
853 "Parse a string formatted using LDIF.";
855 PyObject *parse_ldif(const char *s)
857 PyObject *list = PyList_New(0);
858 struct ldb_ldif *ldif;
859 while ((ldif = ldb_ldif_read_string($self, &s)) != NULL) {
860 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
862 return PyObject_GetIter(list);
868 asprintf(&ret, "<ldb connection at 0x%x>", ret);
874 def __init__(self, url=None, flags=0, options=None):
875 """Create a new LDB object.
877 Will also connect to the specified URL if one was given.
879 _ldb.Ldb_swiginit(self,_ldb.new_Ldb())
881 self.connect(url, flags, options)
883 def search(self, base=None, scope=SCOPE_DEFAULT, expression=None,
884 attrs=None, controls=None):
885 """Search in a database.
887 :param base: Optional base DN to search
888 :param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)
889 :param expression: Optional search expression
890 :param attrs: Attributes to return (defaults to all)
891 :param controls: Optional list of controls
892 :return: Iterator over Message objects
894 if not (attrs is None or isinstance(attrs, list)):
895 raise TypeError("attributes not a list")
896 parsed_controls = None
897 if controls is not None:
898 parsed_controls = self.parse_control_strings(controls)
899 return self.search_ex(base, scope, expression, attrs,
905 %typemap(in,noblock=1) struct ldb_dn *;
906 %typemap(freearg,noblock=1) struct ldb_dn *;
908 %nodefault ldb_message;
909 %nodefault ldb_context;
912 %rename(valid_attr_name) ldb_valid_attr_name;
913 %feature("docstring") ldb_valid_attr_name "S.valid_attr_name(name) -> bool\n"
914 "Check whether the supplied name is a valid attribute name.";
915 int ldb_valid_attr_name(const char *s);
917 typedef unsigned long time_t;
919 %feature("docstring") timestring "S.timestring(int) -> string\n"
920 "Generate a LDAP time string from a UNIX timestamp";
923 static char *timestring(time_t t)
925 char *tresult = ldb_timestring(NULL, t);
926 char *result = strdup(tresult);
927 talloc_free(tresult);
932 %rename(string_to_time) ldb_string_to_time;
933 %feature("docstring") ldb_string_to_time "S.string_to_time(string) -> int\n"
934 "Parse a LDAP time string into a UNIX timestamp.";
935 time_t ldb_string_to_time(const char *s);
937 typedef struct ldb_module {
938 struct ldb_module *prev, *next;
942 const char *__str__() {
943 return $self->ops->name;
947 asprintf(&ret, "<ldb module '%s'>", $self->ops->name);
951 int search(struct ldb_dn *base, enum ldb_scope scope, struct ldb_parse_tree *tree, const char * const * attrs, struct ldb_result **res) {
953 struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
955 req->operation = LDB_SEARCH;
956 req->op.search.base = base;
957 req->op.search.scope = scope;
958 req->op.search.tree = tree;
959 req->op.search.attrs = attrs;
961 req->op.search.res = talloc_zero(NULL, struct ldb_result);
963 ret = $self->ops->search($self, req);
965 *res = req->op.search.res;
971 ldb_error add(struct ldb_message *message) {
972 struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
973 req->operation = LDB_ADD;
974 req->op.add.message = message;
976 return $self->ops->add($self, &req);
978 ldb_error modify(struct ldb_message *message) {
979 struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
980 req->operation = LDB_MODIFY;
981 req->op.mod.message = message;
983 return $self->ops->modify($self, &req);
985 ldb_error delete(struct ldb_dn *dn) {
986 struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
987 req->operation = LDB_DELETE;
990 return $self->ops->del($self, &req);
993 ldb_error rename(struct ldb_dn *olddn, struct ldb_dn *newdn) {
994 struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
995 req->operation = LDB_RENAME;
996 req->op.rename.olddn = olddn;
997 req->op.rename.olddn = newdn;
999 return $self->ops->rename($self, &req);
1001 ldb_error start_transaction() {
1002 return $self->ops->start_transaction($self);
1004 ldb_error end_transaction() {
1005 return $self->ops->end_transaction($self);
1007 ldb_error del_transaction() {
1008 return $self->ops->del_transaction($self);
1014 int py_module_search(struct ldb_module *mod, struct ldb_request *req)
1016 PyObject *py_ldb = mod->private_data;
1017 PyObject *py_result, *py_base, *py_attrs, *py_tree;
1019 py_base = SWIG_NewPointerObj(req->op.search.base, SWIGTYPE_p_ldb_dn, 0);
1021 if (py_base == NULL)
1022 return LDB_ERR_OPERATIONS_ERROR;
1024 py_tree = SWIG_NewPointerObj(req->op.search.tree, SWIGTYPE_p_ldb_parse_tree, 0);
1026 if (py_tree == NULL)
1027 return LDB_ERR_OPERATIONS_ERROR;
1029 if (req->op.search.attrs == NULL) {
1033 for (len = 0; req->op.search.attrs[len]; len++);
1034 py_attrs = PyList_New(len);
1035 for (i = 0; i < len; i++)
1036 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1039 py_result = PyObject_CallMethod(py_ldb, "search", "OiOO", py_base, req->op.search.scope, py_tree, py_attrs);
1041 Py_DECREF(py_attrs);
1045 if (py_result == NULL) {
1046 return LDB_ERR_OPERATIONS_ERROR;
1049 if (SWIG_ConvertPtr(py_result, &req->op.search.res, SWIGTYPE_p_ldb_result, 0) != 0) {
1050 return LDB_ERR_OPERATIONS_ERROR;
1053 Py_DECREF(py_result);
1058 int py_module_add(struct ldb_module *mod, struct ldb_request *req)
1060 PyObject *py_ldb = mod->private_data;
1061 PyObject *py_result, *py_msg;
1063 py_msg = SWIG_NewPointerObj(req->op.add.message, SWIGTYPE_p_ldb_message, 0);
1065 if (py_msg == NULL) {
1066 return LDB_ERR_OPERATIONS_ERROR;
1069 py_result = PyObject_CallMethod(py_ldb, "add", "O", py_msg);
1073 if (py_result == NULL) {
1074 return LDB_ERR_OPERATIONS_ERROR;
1077 Py_DECREF(py_result);
1082 int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
1084 PyObject *py_ldb = mod->private_data;
1085 PyObject *py_result, *py_msg;
1087 py_msg = SWIG_NewPointerObj(req->op.mod.message, SWIGTYPE_p_ldb_message, 0);
1089 if (py_msg == NULL) {
1090 return LDB_ERR_OPERATIONS_ERROR;
1093 py_result = PyObject_CallMethod(py_ldb, "modify", "O", py_msg);
1097 if (py_result == NULL) {
1098 return LDB_ERR_OPERATIONS_ERROR;
1101 Py_DECREF(py_result);
1106 int py_module_del(struct ldb_module *mod, struct ldb_request *req)
1108 PyObject *py_ldb = mod->private_data;
1109 PyObject *py_result, *py_dn;
1111 py_dn = SWIG_NewPointerObj(req->op.del.dn, SWIGTYPE_p_ldb_dn, 0);
1114 return LDB_ERR_OPERATIONS_ERROR;
1116 py_result = PyObject_CallMethod(py_ldb, "delete", "O", py_dn);
1118 if (py_result == NULL) {
1119 return LDB_ERR_OPERATIONS_ERROR;
1122 Py_DECREF(py_result);
1127 int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
1129 PyObject *py_ldb = mod->private_data;
1130 PyObject *py_result, *py_olddn, *py_newdn;
1132 py_olddn = SWIG_NewPointerObj(req->op.rename.olddn, SWIGTYPE_p_ldb_dn, 0);
1134 if (py_olddn == NULL)
1135 return LDB_ERR_OPERATIONS_ERROR;
1137 py_newdn = SWIG_NewPointerObj(req->op.rename.newdn, SWIGTYPE_p_ldb_dn, 0);
1139 if (py_newdn == NULL)
1140 return LDB_ERR_OPERATIONS_ERROR;
1142 py_result = PyObject_CallMethod(py_ldb, "rename", "OO", py_olddn, py_newdn);
1144 Py_DECREF(py_olddn);
1145 Py_DECREF(py_newdn);
1147 if (py_result == NULL) {
1148 return LDB_ERR_OPERATIONS_ERROR;
1151 Py_DECREF(py_result);
1156 int py_module_request(struct ldb_module *mod, struct ldb_request *req)
1158 PyObject *py_ldb = mod->private_data;
1159 PyObject *py_result;
1161 py_result = PyObject_CallMethod(py_ldb, "request", "");
1163 return LDB_ERR_OPERATIONS_ERROR;
1166 int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
1168 PyObject *py_ldb = mod->private_data;
1169 PyObject *py_result;
1171 py_result = PyObject_CallMethod(py_ldb, "extended", "");
1173 return LDB_ERR_OPERATIONS_ERROR;
1176 int py_module_start_transaction(struct ldb_module *mod)
1178 PyObject *py_ldb = mod->private_data;
1179 PyObject *py_result;
1181 py_result = PyObject_CallMethod(py_ldb, "start_transaction", "");
1183 if (py_result == NULL) {
1184 return LDB_ERR_OPERATIONS_ERROR;
1187 Py_DECREF(py_result);
1192 int py_module_end_transaction(struct ldb_module *mod)
1194 PyObject *py_ldb = mod->private_data;
1195 PyObject *py_result;
1197 py_result = PyObject_CallMethod(py_ldb, "end_transaction", "");
1199 if (py_result == NULL) {
1200 return LDB_ERR_OPERATIONS_ERROR;
1203 Py_DECREF(py_result);
1208 int py_module_del_transaction(struct ldb_module *mod)
1210 PyObject *py_ldb = mod->private_data;
1211 PyObject *py_result;
1213 py_result = PyObject_CallMethod(py_ldb, "del_transaction", "");
1215 if (py_result == NULL) {
1216 return LDB_ERR_OPERATIONS_ERROR;
1219 Py_DECREF(py_result);
1224 static int py_module_destructor(void *_mod)
1226 struct ldb_module *mod = _mod;
1227 Py_DECREF((PyObject *)mod->private_data);
1231 int py_module_init (struct ldb_module *mod)
1233 PyObject *py_class = mod->ops->private_data;
1234 PyObject *py_result, *py_next, *py_ldb;
1236 py_ldb = SWIG_NewPointerObj(mod->ldb, SWIGTYPE_p_ldb_context, 0);
1239 return LDB_ERR_OPERATIONS_ERROR;
1241 py_next = SWIG_NewPointerObj(mod->next, SWIGTYPE_p_ldb_module, 0);
1243 if (py_next == NULL)
1244 return LDB_ERR_OPERATIONS_ERROR;
1246 py_result = PyObject_CallFunction(py_class, "OO", py_ldb, py_next);
1248 if (py_result == NULL) {
1249 return LDB_ERR_OPERATIONS_ERROR;
1252 mod->private_data = py_result;
1254 talloc_set_destructor (mod, py_module_destructor);
1256 return ldb_next_init(mod);
1260 %typemap(in,noblock=1) const struct ldb_module_ops * {
1261 $1 = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
1263 $1->name = talloc_strdup($1, PyString_AsString(PyObject_GetAttrString($input, (char *)"name")));
1266 $1->private_data = $input;
1267 $1->init_context = py_module_init;
1268 $1->search = py_module_search;
1269 $1->add = py_module_add;
1270 $1->modify = py_module_modify;
1271 $1->del = py_module_del;
1272 $1->rename = py_module_rename;
1273 $1->request = py_module_request;
1274 $1->extended = py_module_extended;
1275 $1->start_transaction = py_module_start_transaction;
1276 $1->end_transaction = py_module_end_transaction;
1277 $1->del_transaction = py_module_del_transaction;
1280 %feature("docstring") ldb_register_module "S.register_module(module) -> None\n"
1281 "Register a LDB module.";
1282 %rename(register_module) ldb_register_module;
1283 ldb_int_error ldb_register_module(const struct ldb_module_ops *);
1286 __docformat__ = "restructuredText"