Always validate a DN when constructing from a string in python
[kai/samba.git] / source4 / lib / ldb / ldb.i
index da4c52f77831583ee794cd9f5a6ba4cbff413933..6ecbfbfa08acf08bfc4d1c0fda660b59b9aa7ef9 100644 (file)
    License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
-%module ldb
+%define DOCSTRING
+"An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server."
+%enddef
+
+%module(docstring=DOCSTRING) ldb
 
 %{
 
 #include <stdint.h>
 #include <stdbool.h>
 #include "talloc.h"
+#include "events.h"
 #include "ldb.h"
 #include "ldb_errors.h"
 #include "ldb_private.h"
@@ -40,8 +45,10 @@ typedef struct ldb_message ldb_msg;
 typedef struct ldb_context ldb;
 typedef struct ldb_dn ldb_dn;
 typedef struct ldb_ldif ldb_ldif;
-typedef struct ldb_message_element ldb_msg_element;
+typedef struct ldb_message_element ldb_message_element;
+typedef struct ldb_module ldb_module;
 typedef int ldb_error;
+typedef int ldb_int_error;
 
 %}
 
@@ -146,17 +153,21 @@ PyObject *ldb_val_to_py_object(struct ldb_context *ldb_ctx,
  * Wrap struct ldb_result
  */
 
-%typemap(in,noblock=1,numinputs=0) struct ldb_result **OUT (struct ldb_result *temp_ldb_result) {
+%typemap(in,noblock=1,numinputs=0) struct ldb_result ** (struct ldb_result *temp_ldb_result) {
        $1 = &temp_ldb_result;
 }
 
 #ifdef SWIGPYTHON
 %typemap(argout,noblock=1) struct ldb_result ** (int i) {
-       $result = PyList_New((*$1)->count);
-    for (i = 0; i < (*$1)->count; i++) {
-        PyList_SetItem($result, i, 
-            SWIG_NewPointerObj((*$1)->msgs[i], SWIGTYPE_p_ldb_message, 0)
-        );
+    if ($1 == NULL) {
+        $result = Py_None;
+    } else {
+        $result = PyList_New((*$1)->count);
+        for (i = 0; i < (*$1)->count; i++) {
+            PyList_SetItem($result, i, 
+                SWIG_NewPointerObj((*$1)->msgs[i], SWIGTYPE_p_ldb_message, 0)
+            );
+        }
     }
 }
 
@@ -183,7 +194,7 @@ PyObject *ldb_val_to_py_object(struct ldb_context *ldb_ctx,
 
 #endif
 
-%types(struct ldb_result *);
+%types(struct ldb_result *, struct ldb_parse_tree *);
 
 /*
  * Wrap struct ldb_dn
@@ -193,8 +204,11 @@ PyObject *ldb_val_to_py_object(struct ldb_context *ldb_ctx,
 %rename(__cmp__) ldb_dn::compare;
 %rename(__len__) ldb_dn::get_comp_num;
 %rename(Dn) ldb_dn;
+%feature("docstring") ldb_dn "A LDB distinguished name.";
 typedef struct ldb_dn {
     %extend {
+        %feature("docstring") ldb_dn "S.__init__(ldb, string)\n" \
+                 "Create a new DN.";
         ldb_dn(ldb *ldb_ctx, const char *str)
         {
             ldb_dn *ret = ldb_dn_new(ldb_ctx, ldb_ctx, str);
@@ -202,32 +216,55 @@ typedef struct ldb_dn {
                we do it this way... */
             talloc_steal(NULL, ret);
 
-            if (ret == NULL)
+            if (ret == NULL || !ldb_dn_validate(ret))
                 SWIG_exception(SWIG_ValueError, 
                                 "unable to parse dn string");
 fail:
             return ret;
         }
         ~ldb_dn() { talloc_free($self); }
+        %feature("docstring") validate "S.validate() -> bool\n" \
+                                       "Validate DN is correct.";
         bool validate();
         const char *get_casefold();
         const char *get_linearized();
+        %feature("docstring") parent "S.parent() -> dn\n" \
+                                     "Get the parent for this DN.";
         ldb_dn *parent() { return ldb_dn_get_parent(NULL, $self); }
         int compare(ldb_dn *other);
         bool is_valid();
+        %feature("docstring") is_special "S.is_special() -> bool\n" \
+                                         "Check whether this is a special LDB DN.";
         bool is_special();
+        %feature("docstring") is_null "S.is_null() -> bool\n" \
+                                         "Check whether this is a null DN.";
         bool is_null();
         bool check_special(const char *name);
         int get_comp_num();
+        %feature("docstring") add_child "S.add_child(dn) -> None\n" \
+                                         "Add a child DN to this DN.";
         bool add_child(ldb_dn *child);
+        %feature("docstring") add_base "S.add_base(dn) -> None\n" \
+                                         "Add a base DN to this DN.";
         bool add_base(ldb_dn *base);
+        %feature("docstring") canonical_str "S.canonical_str() -> string\n" \
+                                         "Canonical version of this DN (like a posix path).";
         const char *canonical_str() {
             return ldb_dn_canonical_string($self, $self);
         }
+        %feature("docstring") canonical_ex_str "S.canonical_ex_str() -> string\n" \
+                                               "Canonical version of this DN (like a posix path, with terminating newline).";
         const char *canonical_ex_str() {
             return ldb_dn_canonical_ex_string($self, $self);
         }
 #ifdef SWIGPYTHON
+        char *__repr__(void)
+        {
+            char *dn = ldb_dn_get_linearized($self), *ret;
+            asprintf(&ret, "Dn('%s')", dn);
+            return ret;
+        }
+
         ldb_dn *__add__(ldb_dn *other)
         {
             ldb_dn *ret = ldb_dn_copy(NULL, $self);
@@ -250,22 +287,37 @@ fail:
 
 #ifdef SWIGPYTHON
 %{
+struct ldb_context *ldb_context_from_py_object(PyObject *py_obj)
+{
+        struct ldb_context *ldb_ctx;
+    if (SWIG_ConvertPtr(py_obj, (void *)&ldb_ctx, SWIGTYPE_p_ldb_context, 0 |  0 ) < 0)
+        return NULL;
+    return ldb_ctx;
+}
+
 int ldb_dn_from_pyobject(TALLOC_CTX *mem_ctx, PyObject *object, 
                          struct ldb_context *ldb_ctx, ldb_dn **dn)
 {
     int ret;
     struct ldb_dn *odn;
     if (ldb_ctx != NULL && PyString_Check(object)) {
-        *dn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
+        odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
+       if (!odn) {
+               return SWIG_ERROR;
+       }
+       *dn = odn;
         return 0;
     }
     ret = SWIG_ConvertPtr(object, (void **)&odn, SWIGTYPE_p_ldb_dn, 
                            SWIG_POINTER_EXCEPTION);
     *dn = ldb_dn_copy(mem_ctx, odn);
+    if (odn && !*dn) {
+       return SWIG_ERROR;
+    }
     return ret;
 }
 
-ldb_msg_element *ldb_msg_element_from_pyobject(TALLOC_CTX *mem_ctx,
+ldb_message_element *ldb_msg_element_from_pyobject(TALLOC_CTX *mem_ctx,
                                                PyObject *set_obj, int flags,
                                                const char *attr_name)
 {
@@ -296,7 +348,7 @@ ldb_msg_element *ldb_msg_element_from_pyobject(TALLOC_CTX *mem_ctx,
 }
 
 PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx, 
-                                 ldb_msg_element *me)
+                                 ldb_message_element *me)
 {
     int i;
     PyObject *result;
@@ -316,11 +368,16 @@ PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
 #endif
 
 /* ldb_message_element */
-%rename(__cmp__) ldb_message_element::compare;
-%rename(MessageElement) ldb_msg_element;
+%rename(MessageElement) ldb_message_element;
+%feature("docstring") ldb_message_element "Message element.";
 typedef struct ldb_message_element {
     %extend {
 #ifdef SWIGPYTHON
+        int __cmp__(ldb_message_element *other)
+        {
+            return ldb_msg_element_compare($self, other);
+        }
+
         PyObject *__iter__(void)
         {
             return PyObject_GetIter(ldb_msg_element_to_set(NULL, $self));
@@ -331,7 +388,7 @@ typedef struct ldb_message_element {
             return ldb_msg_element_to_set(NULL, $self);
         }
 
-        ldb_msg_element(PyObject *set_obj, int flags=0, const char *name = NULL)
+        ldb_message_element(PyObject *set_obj, int flags=0, const char *name = NULL)
         {
             return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name);
         }
@@ -350,8 +407,7 @@ typedef struct ldb_message_element {
             return ldb_val_to_py_object(NULL, $self, &$self->values[i]);
         }
 
-        ~ldb_msg_element() { talloc_free($self); }
-        int compare(ldb_msg_element *);
+        ~ldb_message_element() { talloc_free($self); }
     }
     %pythoncode {
         def __getitem__(self, i):
@@ -360,6 +416,9 @@ typedef struct ldb_message_element {
                 raise KeyError("no such value")
             return ret
 
+        def __repr__(self):
+            return "MessageElement([%s])" % (",".join(repr(x) for x in self.__set__()))
+
         def __eq__(self, other):
             if (len(self) == 1 and self.get(0) == other):
                 return True
@@ -371,30 +430,34 @@ typedef struct ldb_message_element {
                     return False
             return True
     }
-} ldb_msg_element;
+} ldb_message_element;
 
 /* ldb_message */
 
+%feature("docstring") ldb_message "Message.";
 %rename(Message) ldb_message;
 #ifdef SWIGPYTHON
 %rename(__delitem__) ldb_message::remove_attr;
-%typemap(out) ldb_msg_element * {
+%typemap(out) ldb_message_element * {
        if ($1 == NULL)
                PyErr_SetString(PyExc_KeyError, "no such element");
     else
         $result = SWIG_NewPointerObj($1, SWIGTYPE_p_ldb_message_element, 0);
 }
-%rename(__getitem__) ldb_message::find_element;
-//%typemap(out) ldb_msg_element *;
-
 
 %inline {
     PyObject *ldb_msg_list_elements(ldb_msg *msg)
     {
-        int i;
-        PyObject *obj = PyList_New(msg->num_elements);
-        for (i = 0; i < msg->num_elements; i++)
-            PyList_SetItem(obj, i, PyString_FromString(msg->elements[i].name));
+        int i, j = 0;
+        PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
+        if (msg->dn != NULL) {
+            PyList_SetItem(obj, j, PyString_FromString("dn"));
+            j++;
+        }
+        for (i = 0; i < msg->num_elements; i++) {
+            PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
+            j++;
+        }
         return obj;
     }
 }
@@ -411,10 +474,10 @@ typedef struct ldb_message {
             return ret;
         }
         ~ldb_msg() { talloc_free($self); }
-        ldb_msg_element *find_element(const char *name);
+        ldb_message_element *find_element(const char *name);
         
 #ifdef SWIGPYTHON
-        void __setitem__(const char *attr_name, ldb_msg_element *val)
+        void __setitem__(const char *attr_name, ldb_message_element *val)
         {
             struct ldb_message_element *el;
             
@@ -450,6 +513,28 @@ typedef struct ldb_message {
         }
 #endif
         void remove_attr(const char *name);
+%pythoncode {
+    def get(self, key, default=None):
+        if key == "dn":
+            return self.dn
+        return self.find_element(key)
+
+    def __getitem__(self, key):
+        ret = self.get(key, None)
+        if ret is None:
+            raise KeyError("No such element")
+        return ret
+
+    def iteritems(self):
+        for k in self.keys():
+            yield k, self[k]
+    
+    def items(self):
+        return list(self.iteritems())
+
+    def __repr__(self):
+        return "Message(%s)" % repr(dict(self.iteritems()))
+}
     }
 } ldb_msg;
 
@@ -461,6 +546,8 @@ typedef struct ldb_ldif ldb_ldif;
 
 #ifdef SWIGPYTHON
 %{
+static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
+
 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
 {
     char *text;
@@ -520,6 +607,14 @@ PyObject *PyExc_LdbError;
 
 
 %typemap(out,noblock=1) ldb_error {
+    if ($1 != LDB_SUCCESS) {
+        PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", $1, ldb_errstring(arg1)));
+        SWIG_fail;
+    }
+    $result = Py_None;
+};
+
+%typemap(out,noblock=1) ldb_int_error {
     if ($1 != LDB_SUCCESS) {
         PyErr_SetObject(PyExc_LdbError, Py_BuildValue((char *)"(i,s)", $1, ldb_strerror($1)));
         SWIG_fail;
@@ -536,6 +631,7 @@ PyObject *PyExc_LdbError;
 }
 
 %rename(Ldb) ldb_context;
+%feature("docstring") ldb_context "Connection to a LDB database.";
 
 %typemap(in,noblock=1) struct ldb_dn * {
     if (ldb_dn_from_pyobject(NULL, $input, arg1, &$1) != 0) {
@@ -548,22 +644,29 @@ PyObject *PyExc_LdbError;
 };
 
 %typemap(in,numinputs=1) ldb_msg *add_msg {
-    int dict_pos, msg_pos;
+    Py_ssize_t dict_pos, msg_pos;
+    ldb_message_element *msgel;
     PyObject *key, *value;
-    ldb_msg_element *msgel;
 
     if (PyDict_Check($input)) {
+       PyObject *dn_value = PyDict_GetItemString($input, "dn");
         $1 = ldb_msg_new(NULL);
         $1->elements = talloc_zero_array($1, struct ldb_message_element, PyDict_Size($input));
         msg_pos = dict_pos = 0;
-        while (PyDict_Next($input, &dict_pos, &key, &value)) {
-            if (!strcmp(PyString_AsString(key), "dn")) {
-                /* using argp0 (magic SWIG value) here is a hack */
-                if (ldb_dn_from_pyobject($1, value, argp1, &$1->dn) != 0) {
+       if (dn_value) {
+                /* using argp1 (magic SWIG value) here is a hack */
+                if (ldb_dn_from_pyobject($1, dn_value, argp1, &$1->dn) != 0) {
                     SWIG_exception(SWIG_TypeError, "unable to import dn object");
                 }
-            } else {
-                msgel = ldb_msg_element_from_pyobject($1->elements, value, 0, PyString_AsString(key));
+               if ($1->dn == NULL) {
+                   SWIG_exception(SWIG_TypeError, "dn set but not found");
+               }
+       }
+
+       while (PyDict_Next($input, &dict_pos, &key, &value)) {
+           char *key_str = PyString_AsString(key);
+            if (strcmp(key_str, "dn") != 0) {
+                msgel = ldb_msg_element_from_pyobject($1->elements, value, 0, key_str);
                 if (msgel == NULL) {
                     SWIG_exception(SWIG_TypeError, "unable to import element");
                 }
@@ -586,9 +689,27 @@ PyObject *PyExc_LdbError;
 
 /* Top-level ldb operations */
 typedef struct ldb_context {
+    %rename(firstmodule) modules;
+    struct ldb_module *modules;
+
+    %pythoncode {
+        def itermodules(self):
+            m = self.firstmodule
+            while m is not None:
+                yield m
+                m = m.next
+
+        def modules(self):
+            return list(self.itermodules())
+    }
+
     %extend {
-        ldb(void) { return ldb_init(NULL); }
+        ldb(void) { 
+            return ldb_init(NULL, event_context_init(NULL)); 
+        }
 
+        %feature("docstring") connect "S.connect(url,flags=0,options=None) -> None\n" \
+                                      "Connect to a LDB URL.";
         ldb_error connect(const char *url, unsigned int flags = 0, 
             const char *options[] = NULL);
 
@@ -615,15 +736,14 @@ typedef struct ldb_context {
                            attrs,
                            controls,
                            res,
-                           ldb_search_default_callback);
+                           ldb_search_default_callback,
+                           NULL);
 
             if (ret != LDB_SUCCESS) {
                 talloc_free(res);
                 return ret;
             }
 
-            ldb_set_timeout($self, req, 0); /* use default timeout */
-                
             ret = ldb_request($self, req);
                 
             if (ret == LDB_SUCCESS) {
@@ -636,11 +756,19 @@ typedef struct ldb_context {
             return ret;
         }
 
+        %feature("docstring") delete "S.delete(dn) -> None\n" \
+                                     "Remove an entry.";
         ldb_error delete(ldb_dn *dn);
+        %feature("docstring") rename "S.rename(old_dn, new_dn) -> None\n" \
+                                     "Rename an entry.";
         ldb_error rename(ldb_dn *olddn, ldb_dn *newdn);
         struct ldb_control **parse_control_strings(TALLOC_CTX *mem_ctx, 
                                                    const char * const*control_strings);
+        %feature("docstring") add "S.add(message) -> None\n" \
+                                  "Add an entry.";
         ldb_error add(ldb_msg *add_msg);
+        %feature("docstring") modify "S.modify(message) -> None\n" \
+                                  "Modify an entry.";
         ldb_error modify(ldb_msg *message);
         ldb_dn *get_config_basedn();
         ldb_dn *get_root_basedn();
@@ -676,30 +804,51 @@ typedef struct ldb_context {
         }
 
         const char *errstring();
+        %feature("docstring") set_create_perms "S.set_create_perms(mode) -> None\n" \
+                                               "Set mode to use when creating new LDB files.";
         void set_create_perms(unsigned int perms);
+        %feature("docstring") set_modules_dir "S.set_modules_dir(path) -> None\n" \
+                                              "Set path LDB should search for modules";
         void set_modules_dir(const char *path);
+        %feature("docstring") set_debug "S.set_debug(callback) -> None\n" \
+                                        "Set callback for LDB debug messages.\n" \
+                                        "The callback should accept a debug level and debug text.";
         ldb_error set_debug(void (*debug)(void *context, enum ldb_debug_level level, 
                                           const char *fmt, va_list ap),
                             void *context);
+        %feature("docstring") set_opaque "S.set_opaque(name, value) -> None\n" \
+            "Set an opaque value on this LDB connection. \n"
+            ":note: Passing incorrect values may cause crashes.";
         ldb_error set_opaque(const char *name, void *value);
+        %feature("docstring") get_opaque "S.get_opaque(name) -> value\n" \
+            "Get an opaque value set on this LDB connection. \n"
+            ":note: The returned value may not be useful in Python.";
         void *get_opaque(const char *name);
+        %feature("docstring") transaction_start "S.transaction_start() -> None\n" \
+                                                "Start a new transaction.";
         ldb_error transaction_start();
+        %feature("docstring") transaction_commit "S.transaction_commit() -> None\n" \
+                                                 "Commit currently active transaction.";
         ldb_error transaction_commit();
+        %feature("docstring") transaction_cancel "S.transaction_cancel() -> None\n" \
+                                                 "Cancel currently active transaction.";
         ldb_error transaction_cancel();
         void schema_attribute_remove(const char *name);
         ldb_error schema_attribute_add(const char *attribute, unsigned flags, const char *syntax);
-       ldb_error setup_wellknown_attributes(void);
-       
+        ldb_error setup_wellknown_attributes(void);
 #ifdef SWIGPYTHON
         %typemap(in,numinputs=0,noblock=1) struct ldb_result **result_as_bool (struct ldb_result *tmp) { $1 = &tmp; }
         %typemap(argout,noblock=1) struct ldb_result **result_as_bool { $result = ((*$1)->count > 0)?Py_True:Py_False; }
         %typemap(freearg,noblock=1) struct ldb_result **result_as_bool { talloc_free(*$1); }
         ldb_error __contains__(ldb_dn *dn, struct ldb_result **result_as_bool)
         {
-            return ldb_search($self, dn, LDB_SCOPE_BASE, NULL, NULL, 
-                             result_as_bool);
+            return ldb_search($self, $self, result_as_bool, dn, LDB_SCOPE_BASE, NULL, NULL);
         }
 
+        %feature("docstring") parse_ldif "S.parse_ldif(ldif) -> iter(messages)\n" \
+            "Parse a string formatted using LDIF.";
+
         PyObject *parse_ldif(const char *s)
         {
             PyObject *list = PyList_New(0);
@@ -710,16 +859,37 @@ typedef struct ldb_context {
             return PyObject_GetIter(list);
         }
 
+        char *__repr__(void)
+        {
+            char *ret;
+            asprintf(&ret, "<ldb connection at 0x%x>", ret); 
+            return ret;
+        }
 #endif
     }
     %pythoncode {
         def __init__(self, url=None, flags=0, options=None):
+            """Create a new LDB object.
+
+            Will also connect to the specified URL if one was given.
+            """
             _ldb.Ldb_swiginit(self,_ldb.new_Ldb())
             if url is not None:
                 self.connect(url, flags, options)
 
         def search(self, base=None, scope=SCOPE_DEFAULT, expression=None, 
                    attrs=None, controls=None):
+            """Search in a database.
+
+            :param base: Optional base DN to search
+            :param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)
+            :param expression: Optional search expression
+            :param attrs: Attributes to return (defaults to all)
+            :param controls: Optional list of controls
+            :return: Iterator over Message objects
+            """
+            if not (attrs is None or isinstance(attrs, list)):
+                raise TypeError("attributes not a list")
             parsed_controls = None
             if controls is not None:
                 parsed_controls = self.parse_control_strings(controls)
@@ -737,10 +907,15 @@ typedef struct ldb_context {
 %nodefault Dn;
 
 %rename(valid_attr_name) ldb_valid_attr_name;
+%feature("docstring") ldb_valid_attr_name "S.valid_attr_name(name) -> bool\n"
+                                          "Check whether the supplied name is a valid attribute name.";
 int ldb_valid_attr_name(const char *s);
 
 typedef unsigned long time_t;
 
+%feature("docstring") timestring "S.timestring(int) -> string\n"
+                                 "Generate a LDAP time string from a UNIX timestamp";
+
 %inline %{
 static char *timestring(time_t t)
 {
@@ -752,13 +927,359 @@ static char *timestring(time_t t)
 %}
 
 %rename(string_to_time) ldb_string_to_time;
+%feature("docstring") ldb_string_to_time "S.string_to_time(string) -> int\n"
+                                     "Parse a LDAP time string into a UNIX timestamp.";
 time_t ldb_string_to_time(const char *s);
 
+typedef struct ldb_module {
+    struct ldb_module *prev, *next;
+
+    %extend {
+#ifdef SWIGPYTHON
+        const char *__str__() {
+            return $self->ops->name;
+        }
+        char *__repr__() {
+            char *ret;
+            asprintf(&ret, "<ldb module '%s'>", $self->ops->name);
+            return ret;
+        }
+#endif
+        int search(struct ldb_dn *base, enum ldb_scope scope, struct ldb_parse_tree *tree, const char * const * attrs, struct ldb_result **res) {
+            int ret;
+            struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
+
+            req->operation = LDB_SEARCH;
+            req->op.search.base = base;
+            req->op.search.scope = scope;
+            req->op.search.tree = tree;
+            req->op.search.attrs = attrs;
+
+            req->op.search.res = talloc_zero(NULL, struct ldb_result);
+
+            ret = $self->ops->search($self, req);
+
+            *res = req->op.search.res;
+
+            talloc_free(req);
+
+            return ret;
+        }
+        ldb_error add(struct ldb_message *message) {
+            struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
+            req->operation = LDB_ADD;
+            req->op.add.message = message;
+            
+            return $self->ops->add($self, &req);
+        }
+        ldb_error modify(struct ldb_message *message) {
+            struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
+            req->operation = LDB_MODIFY;
+            req->op.mod.message = message;
+            
+            return $self->ops->modify($self, &req);
+        }
+        ldb_error delete(struct ldb_dn *dn) {
+            struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
+            req->operation = LDB_DELETE;
+            req->op.del.dn = dn;
+            
+            return $self->ops->del($self, &req);
+
+        }
+        ldb_error rename(struct ldb_dn *olddn, struct ldb_dn *newdn) {
+            struct ldb_request *req = talloc_zero(NULL, struct ldb_request);
+            req->operation = LDB_RENAME;
+            req->op.rename.olddn = olddn;
+            req->op.rename.olddn = newdn;
+            
+            return $self->ops->rename($self, &req);
+        }
+        ldb_error start_transaction() {
+            return $self->ops->start_transaction($self);
+        }
+        ldb_error end_transaction() {
+            return $self->ops->end_transaction($self);
+        }
+        ldb_error del_transaction() {
+            return $self->ops->del_transaction($self);
+        }
+    }
+} ldb_module;
+
+%{
+int py_module_search(struct ldb_module *mod, struct ldb_request *req)
+{
+    PyObject *py_ldb = mod->private_data;
+    PyObject *py_result, *py_base, *py_attrs, *py_tree;
+
+    py_base = SWIG_NewPointerObj(req->op.search.base, SWIGTYPE_p_ldb_dn, 0);
+
+    if (py_base == NULL)
+        return LDB_ERR_OPERATIONS_ERROR;
+
+    py_tree = SWIG_NewPointerObj(req->op.search.tree, SWIGTYPE_p_ldb_parse_tree, 0);
+
+    if (py_tree == NULL)
+        return LDB_ERR_OPERATIONS_ERROR;
+
+    if (req->op.search.attrs == NULL) {
+        py_attrs = Py_None;
+    } else {
+        int i, len;
+        for (len = 0; req->op.search.attrs[len]; len++);
+        py_attrs = PyList_New(len);
+        for (i = 0; i < len; i++)
+            PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
+    }
+
+    py_result = PyObject_CallMethod(py_ldb, "search", "OiOO", py_base, req->op.search.scope, py_tree, py_attrs);
+
+    Py_DECREF(py_attrs);
+    Py_DECREF(py_tree);
+    Py_DECREF(py_base);
+
+    if (py_result == NULL) {
+        return LDB_ERR_OPERATIONS_ERROR;
+    }
+
+    if (SWIG_ConvertPtr(py_result, &req->op.search.res, SWIGTYPE_p_ldb_result, 0) != 0) {
+        return LDB_ERR_OPERATIONS_ERROR;
+    }
+
+    Py_DECREF(py_result);
+
+    return LDB_SUCCESS;
+}
+
+int py_module_add(struct ldb_module *mod, struct ldb_request *req)
+{
+    PyObject *py_ldb = mod->private_data;
+    PyObject *py_result, *py_msg;
+
+    py_msg = SWIG_NewPointerObj(req->op.add.message, SWIGTYPE_p_ldb_message, 0);
+
+    if (py_msg == NULL) {
+        return LDB_ERR_OPERATIONS_ERROR;
+    }
+
+    py_result = PyObject_CallMethod(py_ldb, "add", "O", py_msg);
+
+    Py_DECREF(py_msg);
+
+    if (py_result == NULL) {
+        return LDB_ERR_OPERATIONS_ERROR;
+    }
+
+    Py_DECREF(py_result);
+
+    return LDB_SUCCESS;
+}
+
+int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
+{
+    PyObject *py_ldb = mod->private_data;
+    PyObject *py_result, *py_msg;
+
+    py_msg = SWIG_NewPointerObj(req->op.mod.message, SWIGTYPE_p_ldb_message, 0);
+
+    if (py_msg == NULL) {
+        return LDB_ERR_OPERATIONS_ERROR;
+    }
+
+    py_result = PyObject_CallMethod(py_ldb, "modify", "O", py_msg);
+
+    Py_DECREF(py_msg);
+
+    if (py_result == NULL) {
+        return LDB_ERR_OPERATIONS_ERROR;
+    }
+
+    Py_DECREF(py_result);
+
+    return LDB_SUCCESS;
+}
+
+int py_module_del(struct ldb_module *mod, struct ldb_request *req)
+{
+    PyObject *py_ldb = mod->private_data;
+    PyObject *py_result, *py_dn;
+
+    py_dn = SWIG_NewPointerObj(req->op.del.dn, SWIGTYPE_p_ldb_dn, 0);
+
+    if (py_dn == NULL)
+        return LDB_ERR_OPERATIONS_ERROR;
+
+    py_result = PyObject_CallMethod(py_ldb, "delete", "O", py_dn);
+
+    if (py_result == NULL) {
+        return LDB_ERR_OPERATIONS_ERROR;
+    }
+
+    Py_DECREF(py_result);
+
+    return LDB_SUCCESS;
+}
+
+int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
+{
+    PyObject *py_ldb = mod->private_data;
+    PyObject *py_result, *py_olddn, *py_newdn;
+
+    py_olddn = SWIG_NewPointerObj(req->op.rename.olddn, SWIGTYPE_p_ldb_dn, 0);
+
+    if (py_olddn == NULL)
+        return LDB_ERR_OPERATIONS_ERROR;
+
+    py_newdn = SWIG_NewPointerObj(req->op.rename.newdn, SWIGTYPE_p_ldb_dn, 0);
+
+    if (py_newdn == NULL)
+        return LDB_ERR_OPERATIONS_ERROR;
+
+    py_result = PyObject_CallMethod(py_ldb, "rename", "OO", py_olddn, py_newdn);
+
+    Py_DECREF(py_olddn);
+    Py_DECREF(py_newdn);
+
+    if (py_result == NULL) {
+        return LDB_ERR_OPERATIONS_ERROR;
+    }
+
+    Py_DECREF(py_result);
+
+    return LDB_SUCCESS;
+}
+
+int py_module_request(struct ldb_module *mod, struct ldb_request *req)
+{
+    PyObject *py_ldb = mod->private_data;
+    PyObject *py_result;
+
+    py_result = PyObject_CallMethod(py_ldb, "request", "");
+
+    return LDB_ERR_OPERATIONS_ERROR;
+}
+
+int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
+{
+    PyObject *py_ldb = mod->private_data;
+    PyObject *py_result;
+
+    py_result = PyObject_CallMethod(py_ldb, "extended", "");
+
+    return LDB_ERR_OPERATIONS_ERROR;
+}
+
+int py_module_start_transaction(struct ldb_module *mod)
+{
+    PyObject *py_ldb = mod->private_data;
+    PyObject *py_result;
+
+    py_result = PyObject_CallMethod(py_ldb, "start_transaction", "");
+
+    if (py_result == NULL) {
+        return LDB_ERR_OPERATIONS_ERROR;
+    }
+
+    Py_DECREF(py_result);
+
+    return LDB_SUCCESS;
+}
+
+int py_module_end_transaction(struct ldb_module *mod)
+{
+    PyObject *py_ldb = mod->private_data;
+    PyObject *py_result;
+
+    py_result = PyObject_CallMethod(py_ldb, "end_transaction", "");
+
+    if (py_result == NULL) {
+        return LDB_ERR_OPERATIONS_ERROR;
+    }
+
+    Py_DECREF(py_result);
+
+    return LDB_SUCCESS;
+}
+
+int py_module_del_transaction(struct ldb_module *mod)
+{
+    PyObject *py_ldb = mod->private_data;
+    PyObject *py_result;
+
+    py_result = PyObject_CallMethod(py_ldb, "del_transaction", "");
+
+    if (py_result == NULL) {
+        return LDB_ERR_OPERATIONS_ERROR;
+    }
+
+    Py_DECREF(py_result);
+
+    return LDB_SUCCESS;
+}
+
+static int py_module_destructor(void *_mod)
+{
+    struct ldb_module *mod = _mod;
+    Py_DECREF((PyObject *)mod->private_data);
+    return 0;
+}
+
+int py_module_init (struct ldb_module *mod)
+{
+    PyObject *py_class = mod->ops->private_data;
+    PyObject *py_result, *py_next, *py_ldb;
+
+    py_ldb = SWIG_NewPointerObj(mod->ldb, SWIGTYPE_p_ldb_context, 0);
+
+    if (py_ldb == NULL)
+        return LDB_ERR_OPERATIONS_ERROR;
+
+    py_next = SWIG_NewPointerObj(mod->next, SWIGTYPE_p_ldb_module, 0);
+
+    if (py_next == NULL)
+        return LDB_ERR_OPERATIONS_ERROR;
+
+    py_result = PyObject_CallFunction(py_class, "OO", py_ldb, py_next);
+
+    if (py_result == NULL) {
+        return LDB_ERR_OPERATIONS_ERROR;
+    }
+
+    mod->private_data = py_result;
+
+    talloc_set_destructor (mod, py_module_destructor);
+
+    return ldb_next_init(mod);
+}
+%}
+
 %typemap(in,noblock=1) const struct ldb_module_ops * {
     $1 = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
 
-    $1->name = (char *)PyObject_GetAttrString($input, (char *)"name");
+    $1->name = talloc_strdup($1, PyString_AsString(PyObject_GetAttrString($input, (char *)"name")));
+
+    Py_INCREF($input);
+    $1->private_data = $input;
+    $1->init_context = py_module_init;
+    $1->search = py_module_search;
+    $1->add = py_module_add;
+    $1->modify = py_module_modify;
+    $1->del = py_module_del;
+    $1->rename = py_module_rename;
+    $1->request = py_module_request;
+    $1->extended = py_module_extended;
+    $1->start_transaction = py_module_start_transaction;
+    $1->end_transaction = py_module_end_transaction;
+    $1->del_transaction = py_module_del_transaction;
 }
 
+%feature("docstring") ldb_register_module "S.register_module(module) -> None\n"
+                                          "Register a LDB module.";
 %rename(register_module) ldb_register_module;
-ldb_error ldb_register_module(const struct ldb_module_ops *);
+ldb_int_error ldb_register_module(const struct ldb_module_ops *);
+
+%pythoncode {
+__docformat__ = "restructuredText"
+open = Ldb
+}