Merge branch 'master' of ssh://git.samba.org/data/git/samba
[sfrench/samba-autobuild/.git] / source4 / lib / ldb / ldb.i
index 18e981f7bea008a2a10ff3b57c5121834960d63a..0f05c1fbab6bfaf66938bb5c0e734ea9d2da96cf 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,7 +45,8 @@ 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;
 
@@ -147,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)
+            );
+        }
     }
 }
 
@@ -180,11 +190,12 @@ PyObject *ldb_val_to_py_object(struct ldb_context *ldb_ctx,
 }
 
 %apply const char * const *NULL_STR_LIST { const char * const *attrs }
+%apply const char * const *NULL_STR_LIST { const char * const *options }
 %apply const char * const *NULL_STR_LIST { const char * const *control_strings }
 
 #endif
 
-%types(struct ldb_result *);
+%types(struct ldb_result *, struct ldb_parse_tree *);
 
 /*
  * Wrap struct ldb_dn
@@ -194,8 +205,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);
@@ -203,28 +217,44 @@ 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);
         }
@@ -233,7 +263,6 @@ fail:
         {
             char *dn = ldb_dn_get_linearized($self), *ret;
             asprintf(&ret, "Dn('%s')", dn);
-            talloc_free(dn);
             return ret;
         }
 
@@ -289,7 +318,7 @@ int ldb_dn_from_pyobject(TALLOC_CTX *mem_ctx, PyObject *object,
     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)
 {
@@ -320,7 +349,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;
@@ -340,11 +369,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));
@@ -355,7 +389,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);
         }
@@ -374,8 +408,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):
@@ -398,21 +431,20 @@ 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);
 }
-//%typemap(out) ldb_msg_element *;
-
 
 %inline {
     PyObject *ldb_msg_list_elements(ldb_msg *msg)
@@ -443,10 +475,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;
             
@@ -600,6 +632,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) {
@@ -613,7 +646,7 @@ PyObject *PyExc_LdbError;
 
 %typemap(in,numinputs=1) ldb_msg *add_msg {
     Py_ssize_t dict_pos, msg_pos;
-    ldb_msg_element *msgel;
+    ldb_message_element *msgel;
     PyObject *key, *value;
 
     if (PyDict_Check($input)) {
@@ -657,13 +690,32 @@ 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);
+            const char *const *options = NULL);
 
         ~ldb() { talloc_free($self); }
+
         ldb_error search_ex(TALLOC_CTX *mem_ctx,
                    ldb_dn *base = NULL, 
                    enum ldb_scope scope = LDB_SCOPE_DEFAULT, 
@@ -686,15 +738,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) {
@@ -707,11 +758,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();
@@ -747,30 +806,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);
@@ -791,12 +871,25 @@ typedef struct ldb_context {
     }
     %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
@@ -816,10 +909,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)
 {
@@ -831,13 +929,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_int_error ldb_register_module(const struct ldb_module_ops *);
+
+%pythoncode {
+__docformat__ = "restructuredText"
+open = Ldb
+}