lib: Make async_sock includable on its own
[kai/samba.git] / lib / ldb / pyldb.c
index adec424aa1e215f7cfb39d3c3083b8799b8d8ae6..8c9d6b9599bacd510119fa6dbdd8d0d412f3a172 100644 (file)
@@ -29,7 +29,6 @@
 */
 
 #include <Python.h>
-#include <pytalloc.h>
 #include "ldb_private.h"
 #include "pyldb.h"
 
@@ -40,10 +39,15 @@ static PyObject *PyExc_LdbError;
 staticforward PyTypeObject PyLdbControl;
 staticforward PyTypeObject PyLdbResult;
 staticforward PyTypeObject PyLdbMessage;
+#define PyLdbMessage_Check(ob) PyObject_TypeCheck(ob, &PyLdbMessage)
 staticforward PyTypeObject PyLdbModule;
 staticforward PyTypeObject PyLdbDn;
+#define pyldb_Dn_Check(ob) PyObject_TypeCheck(ob, &PyLdbDn)
 staticforward PyTypeObject PyLdb;
+#define PyLdb_Check(ob) PyObject_TypeCheck(ob, &PyLdb)
 staticforward PyTypeObject PyLdbMessageElement;
+#define pyldb_MessageElement_Check(ob) PyObject_TypeCheck(ob, &PyLdbMessageElement)
+
 staticforward PyTypeObject PyLdbTree;
 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx);
 static PyObject *PyLdbModule_FromModule(struct ldb_module *mod);
@@ -132,7 +136,7 @@ static PyObject *py_ldb_control_new(PyTypeObject *type, PyObject *args, PyObject
                return NULL;
        }
 
-       ldb_ctx = PyLdb_AsLdbContext(py_ldb);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(py_ldb);
        parsed_controls = ldb_parse_control_from_string(ldb_ctx, mem_ctx, data);
 
        if (!parsed_controls) {
@@ -320,9 +324,12 @@ static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
 }
 
 /**
- * Create a LDB Result from a Python object. 
+ * Create a LDB Result from a Python object.
  * If conversion fails, NULL will be returned and a Python exception set.
  *
+ * Note: the result object only includes the messages at the moment; extended
+ * result, controls and referrals are ignored.
+ *
  * @param mem_ctx Memory context in which to allocate the LDB Result
  * @param obj Python object to convert
  * @return a ldb_result, or NULL if the conversion failed
@@ -341,7 +348,7 @@ static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx,
        res->msgs = talloc_array(res, struct ldb_message *, res->count);
        for (i = 0; i < res->count; i++) {
                PyObject *item = PyList_GetItem(obj, i);
-               res->msgs[i] = PyLdbMessage_AsMessage(item);
+               res->msgs[i] = pyldb_Message_AsMessage(item);
        }
        return res;
 }
@@ -468,7 +475,7 @@ static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
 
 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
 {
-       struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
+       struct ldb_dn *dn = pyldb_Dn_AsDn((PyObject *)self);
        struct ldb_dn *parent;
        PyLdbDnObject *py_ret;
        TALLOC_CTX *mem_ctx = talloc_new(NULL);
@@ -499,9 +506,9 @@ static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "O", &py_other))
                return NULL;
 
-       dn = PyLdbDn_AsDn((PyObject *)self);
+       dn = pyldb_Dn_AsDn((PyObject *)self);
 
-       if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
+       if (!pyldb_Object_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
                return NULL;
 
        return ldb_dn_add_child(dn, other)?Py_True:Py_False;
@@ -514,14 +521,26 @@ static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "O", &py_other))
                return NULL;
 
-       dn = PyLdbDn_AsDn((PyObject *)self);
+       dn = pyldb_Dn_AsDn((PyObject *)self);
 
-       if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
+       if (!pyldb_Object_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
                return NULL;
 
        return ldb_dn_add_base(dn, other)?Py_True:Py_False;
 }
 
+static PyObject *py_ldb_dn_remove_base_components(PyLdbDnObject *self, PyObject *args)
+{
+       struct ldb_dn *dn;
+       int i;
+       if (!PyArg_ParseTuple(args, "i", &i))
+               return NULL;
+
+       dn = pyldb_Dn_AsDn((PyObject *)self);
+
+       return ldb_dn_remove_base_components(dn, i)?Py_True:Py_False;
+}
+
 static PyObject *py_ldb_dn_is_child_of(PyLdbDnObject *self, PyObject *args)
 {
        PyObject *py_base;
@@ -529,9 +548,9 @@ static PyObject *py_ldb_dn_is_child_of(PyLdbDnObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "O", &py_base))
                return NULL;
 
-       dn = PyLdbDn_AsDn((PyObject *)self);
+       dn = pyldb_Dn_AsDn((PyObject *)self);
 
-       if (!PyObject_AsDn(NULL, py_base, dn_ldb_ctx(dn), &base))
+       if (!pyldb_Object_AsDn(NULL, py_base, dn_ldb_ctx(dn), &base))
                return NULL;
 
        return PyBool_FromLong(ldb_dn_compare_base(base, dn) == 0);
@@ -572,6 +591,9 @@ static PyMethodDef py_ldb_dn_methods[] = {
        { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
                "S.add_base(dn) -> None\n"
                "Add a base DN to this DN." },
+       { "remove_base_components", (PyCFunction)py_ldb_dn_remove_base_components, METH_VARARGS,
+               "S.remove_base_components(int) -> bool\n"
+               "Remove a number of DN components from the base of this DN." },
        { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
                "S.check_special(name) -> bool\n\n"
                "Check if name is a special DN name"},
@@ -586,16 +608,33 @@ static PyMethodDef py_ldb_dn_methods[] = {
 
 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
 {
-       return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
+       return ldb_dn_get_comp_num(pyldb_Dn_AsDn((PyObject *)self));
+}
+
+/*
+  copy a DN as a python object
+ */
+static PyObject *py_ldb_dn_copy(struct ldb_dn *dn)
+{
+       PyLdbDnObject *py_ret;
+
+       py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
+       if (py_ret == NULL) {
+               PyErr_NoMemory();
+               return NULL;
+       }
+       py_ret->mem_ctx = talloc_new(NULL);
+       py_ret->dn = ldb_dn_copy(py_ret->mem_ctx, dn);
+       return (PyObject *)py_ret;
 }
 
 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
 {
-       struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self), 
+       struct ldb_dn *dn = pyldb_Dn_AsDn((PyObject *)self), 
                                  *other;
        PyLdbDnObject *py_ret;
-       
-       if (!PyObject_AsDn(NULL, py_other, NULL, &other))
+
+       if (!pyldb_Object_AsDn(NULL, py_other, NULL, &other))
                return NULL;
 
        py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
@@ -605,7 +644,7 @@ static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
        }
        py_ret->mem_ctx = talloc_new(NULL);
        py_ret->dn = ldb_dn_copy(py_ret->mem_ctx, dn);
-       ldb_dn_add_child(py_ret->dn, other);
+       ldb_dn_add_base(py_ret->dn, other);
        return (PyObject *)py_ret;
 }
 
@@ -629,7 +668,12 @@ static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwa
                                         &py_ldb, &str))
                return NULL;
 
-       ldb_ctx = PyLdb_AsLdbContext(py_ldb);
+       if (!PyLdb_Check(py_ldb)) {
+               PyErr_SetString(PyExc_TypeError, "Expected Ldb");
+               return NULL;
+       }
+
+       ldb_ctx = pyldb_Ldb_AsLdbContext(py_ldb);
 
        mem_ctx = talloc_new(NULL);
        if (mem_ctx == NULL) {
@@ -683,16 +727,20 @@ static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *
        PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap));
 }
 
-static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
+static PyObject *py_ldb_set_debug(PyObject *self, PyObject *args)
 {
        PyObject *cb;
+       struct ldb_context *ldb_ctx;
 
        if (!PyArg_ParseTuple(args, "O", &cb))
                return NULL;
 
        Py_INCREF(cb);
        /* FIXME: Where do we DECREF cb ? */
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError,
+               ldb_set_debug(ldb_ctx, py_ldb_debug, cb),
+               ldb_ctx);
 
        Py_RETURN_NONE;
 }
@@ -703,7 +751,7 @@ static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "I", &perms))
                return NULL;
 
-       ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
+       ldb_set_create_perms(pyldb_Ldb_AsLdbContext(self), perms);
 
        Py_RETURN_NONE;
 }
@@ -714,38 +762,53 @@ static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "s", &modules_dir))
                return NULL;
 
-       ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
+       ldb_set_modules_dir(pyldb_Ldb_AsLdbContext(self), modules_dir);
 
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       struct ldb_context *ldb_ctx = pyldb_Ldb_AsLdbContext(self);
+       int ldb_err;
+       ldb_err = ldb_transaction_start(ldb_ctx);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_err, ldb_ctx);
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       struct ldb_context *ldb_ctx = pyldb_Ldb_AsLdbContext(self);
+       int ldb_err;
+       ldb_err = ldb_transaction_commit(ldb_ctx);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_err, ldb_ctx);
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_prepare_commit(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_prepare_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       struct ldb_context *ldb_ctx = pyldb_Ldb_AsLdbContext(self);
+       int ldb_err;
+       ldb_err = ldb_transaction_prepare_commit(ldb_ctx);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_err, ldb_ctx);
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       struct ldb_context *ldb_ctx = pyldb_Ldb_AsLdbContext(self);
+       int ldb_err;
+       ldb_err = ldb_transaction_cancel(ldb_ctx);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_err, ldb_ctx);
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
 {
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
+       struct ldb_context *ldb_ctx = pyldb_Ldb_AsLdbContext(self);
+       int ldb_err;
+       ldb_err = ldb_setup_wellknown_attributes(ldb_ctx);
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_err, ldb_ctx);
        Py_RETURN_NONE;
 }
 
@@ -756,35 +819,35 @@ static PyObject *py_ldb_repr(PyLdbObject *self)
 
 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
 {
-       struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
+       struct ldb_dn *dn = ldb_get_root_basedn(pyldb_Ldb_AsLdbContext(self));
        if (dn == NULL)
                Py_RETURN_NONE;
-       return PyLdbDn_FromDn(dn);
+       return py_ldb_dn_copy(dn);
 }
 
 
 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
 {
-       struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
+       struct ldb_dn *dn = ldb_get_schema_basedn(pyldb_Ldb_AsLdbContext(self));
        if (dn == NULL)
                Py_RETURN_NONE;
-       return PyLdbDn_FromDn(dn);
+       return py_ldb_dn_copy(dn);
 }
 
 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
 {
-       struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
+       struct ldb_dn *dn = ldb_get_config_basedn(pyldb_Ldb_AsLdbContext(self));
        if (dn == NULL)
                Py_RETURN_NONE;
-       return PyLdbDn_FromDn(dn);
+       return py_ldb_dn_copy(dn);
 }
 
 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
 {
-       struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
+       struct ldb_dn *dn = ldb_get_default_basedn(pyldb_Ldb_AsLdbContext(self));
        if (dn == NULL)
                Py_RETURN_NONE;
-       return PyLdbDn_FromDn(dn);
+       return py_ldb_dn_copy(dn);
 }
 
 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, 
@@ -830,7 +893,7 @@ static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
                                         &url, &flags, &py_options))
                return -1;
 
-       ldb = PyLdb_AsLdbContext(self);
+       ldb = pyldb_Ldb_AsLdbContext(self);
 
        if (py_options == Py_None) {
                options = NULL;
@@ -881,6 +944,7 @@ static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwa
        int ret;
        const char **options;
        const char * const kwnames[] = { "url", "flags", "options", NULL };
+       struct ldb_context *ldb_ctx;
 
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|zIO",
                                         discard_const_p(char *, kwnames),
@@ -895,10 +959,11 @@ static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwa
                        return NULL;
        }
 
-       ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
+       ret = ldb_connect(ldb_ctx, url, flags, options);
        talloc_free(options);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
 
        Py_RETURN_NONE;
 }
@@ -926,7 +991,7 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args, PyObject *kwar
                PyErr_NoMemory();
                return NULL;
        }
-       ldb_ctx = PyLdb_AsLdbContext(self);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
 
        if (py_controls == Py_None) {
                parsed_controls = NULL;
@@ -941,12 +1006,12 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args, PyObject *kwar
                talloc_free(mem_ctx);
                return NULL;
        }
-       msg = PyLdbMessage_AsMessage(py_msg);
+       msg = pyldb_Message_AsMessage(py_msg);
 
        if (validate) {
                ret = ldb_msg_sanity_check(ldb_ctx, msg);
                if (ret != LDB_SUCCESS) {
-                       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+                       PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
                        talloc_free(mem_ctx);
                        return NULL;
                }
@@ -966,22 +1031,19 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args, PyObject *kwar
        ret = ldb_transaction_start(ldb_ctx);
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
+               return NULL;
        }
 
        ret = ldb_request(ldb_ctx, req);
        if (ret == LDB_SUCCESS) {
-                       ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
        }
 
        if (ret == LDB_SUCCESS) {
                ret = ldb_transaction_commit(ldb_ctx);
        } else {
                ldb_transaction_cancel(ldb_ctx);
-               if (ldb_ctx->err_string == NULL) {
-                       /* no error string was setup by the backend */
-                       ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
-               }
        }
 
        talloc_free(mem_ctx);
@@ -1016,7 +1078,7 @@ static struct ldb_message *PyDict_AsMessage(TALLOC_CTX *mem_ctx,
        msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_obj));
 
        if (dn_value) {
-               if (!PyObject_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
+               if (!pyldb_Object_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
                        PyErr_SetString(PyExc_TypeError, "unable to import dn object");
                        return NULL;
                }
@@ -1031,7 +1093,7 @@ static struct ldb_message *PyDict_AsMessage(TALLOC_CTX *mem_ctx,
 
        while (PyDict_Next(py_obj, &dict_pos, &key, &value)) {
                char *key_str = PyString_AsString(key);
-               if (strcmp(key_str, "dn") != 0) {
+               if (ldb_attr_cmp(key_str, "dn") != 0) {
                        msg_el = PyObject_AsMessageElement(msg->elements, value,
                                                           mod_flags, key_str);
                        if (msg_el == NULL) {
@@ -1070,7 +1132,7 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args, PyObject *kwargs)
                PyErr_NoMemory();
                return NULL;
        }
-       ldb_ctx = PyLdb_AsLdbContext(self);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
 
        if (py_controls == Py_None) {
                parsed_controls = NULL;
@@ -1081,7 +1143,7 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args, PyObject *kwargs)
        }
 
        if (PyLdbMessage_Check(py_obj)) {
-               msg = PyLdbMessage_AsMessage(py_obj);
+               msg = pyldb_Message_AsMessage(py_obj);
        } else if (PyDict_Check(py_obj)) {
                msg = PyDict_AsMessage(mem_ctx, py_obj, ldb_ctx, LDB_FLAG_MOD_ADD);
        } else {
@@ -1097,7 +1159,7 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args, PyObject *kwargs)
 
        ret = ldb_msg_sanity_check(ldb_ctx, msg);
        if (ret != LDB_SUCCESS) {
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
                talloc_free(mem_ctx);
                return NULL;
        }
@@ -1116,22 +1178,19 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args, PyObject *kwargs)
        ret = ldb_transaction_start(ldb_ctx);
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
+               return NULL;
        }
 
        ret = ldb_request(ldb_ctx, req);
        if (ret == LDB_SUCCESS) {
-                       ret = ldb_wait(req->handle, LDB_WAIT_ALL);
-       } 
+               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+       }
 
        if (ret == LDB_SUCCESS) {
-                       ret = ldb_transaction_commit(ldb_ctx);
+               ret = ldb_transaction_commit(ldb_ctx);
        } else {
                ldb_transaction_cancel(ldb_ctx);
-               if (ldb_ctx->err_string == NULL) {
-                       /* no error string was setup by the backend */
-                       ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
-               }
        }
 
        talloc_free(mem_ctx);
@@ -1162,7 +1221,7 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args, PyObject *kwar
                PyErr_NoMemory();
                return NULL;
        }
-       ldb_ctx = PyLdb_AsLdbContext(self);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
 
        if (py_controls == Py_None) {
                parsed_controls = NULL;
@@ -1172,7 +1231,7 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args, PyObject *kwar
                talloc_free(controls);
        }
 
-       if (!PyObject_AsDn(mem_ctx, py_dn, ldb_ctx, &dn)) {
+       if (!pyldb_Object_AsDn(mem_ctx, py_dn, ldb_ctx, &dn)) {
                talloc_free(mem_ctx);
                return NULL;
        }
@@ -1191,7 +1250,8 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args, PyObject *kwar
        ret = ldb_transaction_start(ldb_ctx);
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
+               return NULL;
        }
 
        ret = ldb_request(ldb_ctx, req);
@@ -1203,10 +1263,6 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args, PyObject *kwar
                ret = ldb_transaction_commit(ldb_ctx);
        } else {
                ldb_transaction_cancel(ldb_ctx);
-               if (ldb_ctx->err_string == NULL) {
-                       /* no error string was setup by the backend */
-                       ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
-               }
        }
 
        talloc_free(mem_ctx);
@@ -1220,7 +1276,6 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwar
        PyObject *py_dn1, *py_dn2;
        struct ldb_dn *dn1, *dn2;
        int ret;
-       struct ldb_context *ldb;
        TALLOC_CTX *mem_ctx;
        PyObject *py_controls = Py_None;
        struct ldb_control **parsed_controls;
@@ -1228,7 +1283,7 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwar
        struct ldb_request *req;
        const char * const kwnames[] = { "dn1", "dn2", "controls", NULL };
 
-       ldb_ctx = PyLdb_AsLdbContext(self);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
 
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|O",
                                         discard_const_p(char *, kwnames),
@@ -1241,7 +1296,6 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwar
                PyErr_NoMemory();
                return NULL;
        }
-       ldb = PyLdb_AsLdbContext(self);
 
        if (py_controls == Py_None) {
                parsed_controls = NULL;
@@ -1252,12 +1306,12 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwar
        }
 
 
-       if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) {
+       if (!pyldb_Object_AsDn(mem_ctx, py_dn1, ldb_ctx, &dn1)) {
                talloc_free(mem_ctx);
                return NULL;
        }
 
-       if (!PyObject_AsDn(mem_ctx, py_dn2, ldb, &dn2)) {
+       if (!pyldb_Object_AsDn(mem_ctx, py_dn2, ldb_ctx, &dn2)) {
                talloc_free(mem_ctx);
                return NULL;
        }
@@ -1276,7 +1330,8 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwar
        ret = ldb_transaction_start(ldb_ctx);
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
+               return NULL;
        }
 
        ret = ldb_request(ldb_ctx, req);
@@ -1288,10 +1343,6 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwar
                ret = ldb_transaction_commit(ldb_ctx);
        } else {
                ldb_transaction_cancel(ldb_ctx);
-               if (ldb_ctx->err_string == NULL) {
-                       /* no error string was setup by the backend */
-                       ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
-               }
        }
 
        talloc_free(mem_ctx);
@@ -1306,7 +1357,7 @@ static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *arg
        if (!PyArg_ParseTuple(args, "s", &name))
                return NULL;
 
-       ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
+       ldb_schema_attribute_remove(pyldb_Ldb_AsLdbContext(self), name);
 
        Py_RETURN_NONE;
 }
@@ -1316,12 +1367,15 @@ static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
        char *attribute, *syntax;
        unsigned int flags;
        int ret;
+       struct ldb_context *ldb_ctx;
+
        if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
                return NULL;
 
-       ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
+       ret = ldb_schema_attribute_add(ldb_ctx, attribute, flags, syntax);
 
-       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
 
        Py_RETURN_NONE;
 }
@@ -1356,12 +1410,12 @@ static PyObject *py_ldb_write_ldif(PyLdbObject *self, PyObject *args)
                return NULL;
        }
 
-       ldif.msg = PyLdbMessage_AsMessage(py_msg);
+       ldif.msg = pyldb_Message_AsMessage(py_msg);
        ldif.changetype = changetype;
 
        mem_ctx = talloc_new(NULL);
 
-       string = ldb_ldif_write_string(PyLdb_AsLdbContext(self), mem_ctx, &ldif);
+       string = ldb_ldif_write_string(pyldb_Ldb_AsLdbContext(self), mem_ctx, &ldif);
        if (!string) {
                PyErr_SetString(PyExc_KeyError, "Failed to generate LDIF");
                return NULL;
@@ -1428,10 +1482,10 @@ static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args)
                return NULL;
        }
 
-       ldb = PyLdb_AsLdbContext(self);
+       ldb = pyldb_Ldb_AsLdbContext(self);
        ldb_ret = ldb_msg_difference(ldb, ldb,
-                                    PyLdbMessage_AsMessage(py_msg_old),
-                                    PyLdbMessage_AsMessage(py_msg_new),
+                                    pyldb_Message_AsMessage(py_msg_old),
+                                    pyldb_Message_AsMessage(py_msg_new),
                                     &diff);
        if (ldb_ret != LDB_SUCCESS) {
                PyErr_SetString(PyExc_RuntimeError, "Failed to generate the Ldb Message diff");
@@ -1463,13 +1517,13 @@ static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
        old_val.data = (uint8_t *)PyString_AsString(val);
        old_val.length = PyString_Size(val);
 
-       a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
+       a = ldb_schema_attribute_by_name(pyldb_Ldb_AsLdbContext(self), element_name);
 
        if (a == NULL) {
                Py_RETURN_NONE;
        }
 
-       if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
+       if (a->syntax->ldif_write_fn(pyldb_Ldb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
                talloc_free(mem_ctx);
                Py_RETURN_NONE;
        }
@@ -1511,7 +1565,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
                PyErr_NoMemory();
                return NULL;
        }
-       ldb_ctx = PyLdb_AsLdbContext(self);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(self);
 
        if (py_attrs == Py_None) {
                attrs = NULL;
@@ -1526,7 +1580,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
        if (py_base == Py_None) {
                base = ldb_get_default_basedn(ldb_ctx);
        } else {
-               if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
+               if (!pyldb_Object_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) {
                        talloc_free(attrs);
                        return NULL;
                }
@@ -1559,7 +1613,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
 
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
                return NULL;
        }
 
@@ -1573,7 +1627,7 @@ static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwar
 
        if (ret != LDB_SUCCESS) {
                talloc_free(mem_ctx);
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
+               PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx);
                return NULL;
        }
 
@@ -1592,7 +1646,7 @@ static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
        if (!PyArg_ParseTuple(args, "s", &name))
                return NULL;
 
-       data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
+       data = ldb_get_opaque(pyldb_Ldb_AsLdbContext(self), name);
 
        if (data == NULL)
                Py_RETURN_NONE;
@@ -1612,14 +1666,14 @@ static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
 
        /* FIXME: More interpretation */
 
-       ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
+       ldb_set_opaque(pyldb_Ldb_AsLdbContext(self), name, data);
 
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_modules(PyLdbObject *self)
 {
-       struct ldb_context *ldb = PyLdb_AsLdbContext(self);
+       struct ldb_context *ldb = pyldb_Ldb_AsLdbContext(self);
        PyObject *ret = PyList_New(0);
        struct ldb_module *mod;
 
@@ -1632,7 +1686,7 @@ static PyObject *py_ldb_modules(PyLdbObject *self)
 
 static PyObject *py_ldb_sequence_number(PyLdbObject *self, PyObject *args)
 {
-       struct ldb_context *ldb = PyLdb_AsLdbContext(self);
+       struct ldb_context *ldb = pyldb_Ldb_AsLdbContext(self);
        int type, ret;
        uint64_t value;
 
@@ -1643,10 +1697,8 @@ static PyObject *py_ldb_sequence_number(PyLdbObject *self, PyObject *args)
 
        ret = ldb_sequence_number(ldb, type, &value);
 
-       if (ret != LDB_SUCCESS) {
-               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
-               return NULL;
-       }
+       PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb);
+
        return PyLong_FromLongLong(value);
 }
 static PyMethodDef py_ldb_methods[] = {
@@ -1757,7 +1809,7 @@ static PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
 
 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
 {
-       return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
+       return PyLdbModule_FromModule(pyldb_Ldb_AsLdbContext(self)->modules);
 }
 
 static PyGetSetDef py_ldb_getset[] = {
@@ -1767,13 +1819,13 @@ static PyGetSetDef py_ldb_getset[] = {
 
 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
 {
-       struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
+       struct ldb_context *ldb_ctx = pyldb_Ldb_AsLdbContext(self);
        struct ldb_dn *dn;
        struct ldb_result *result;
        unsigned int count;
        int ret;
 
-       if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn)) {
+       if (!pyldb_Object_AsDn(ldb_ctx, obj, ldb_ctx, &dn)) {
                return -1;
        }
 
@@ -1925,29 +1977,30 @@ static PyTypeObject PyLdbResult = {
 
 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
 {
-       return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
+       return PyString_FromFormat("<ldb module '%s'>",
+               pyldb_Module_AsModule(self)->ops->name);
 }
 
 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
 {
-       return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
+       return PyString_FromString(pyldb_Module_AsModule(self)->ops->name);
 }
 
 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
 {
-       PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
+       pyldb_Module_AsModule(self)->ops->start_transaction(pyldb_Module_AsModule(self));
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
 {
-       PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
+       pyldb_Module_AsModule(self)->ops->end_transaction(pyldb_Module_AsModule(self));
        Py_RETURN_NONE;
 }
 
 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
 {
-       PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
+       pyldb_Module_AsModule(self)->ops->del_transaction(pyldb_Module_AsModule(self));
        Py_RETURN_NONE;
 }
 
@@ -1976,7 +2029,7 @@ static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, P
                        return NULL;
        }
 
-       ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base), 
+       ret = ldb_build_search_req(&req, mod->ldb, NULL, pyldb_Dn_AsDn(py_base), 
                             scope, NULL /* expr */, attrs,
                             NULL /* controls */, NULL, NULL, NULL);
 
@@ -1994,7 +2047,7 @@ static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, P
 
        talloc_free(req);
 
-       return py_ret;  
+       return py_ret;
 }
 
 
@@ -2010,9 +2063,9 @@ static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
 
        req = talloc_zero(NULL, struct ldb_request);
        req->operation = LDB_ADD;
-       req->op.add.message = PyLdbMessage_AsMessage(py_message);
+       req->op.add.message = pyldb_Message_AsMessage(py_message);
 
-       mod = PyLdbModule_AsModule(self);
+       mod = pyldb_Module_AsModule(self);
        ret = mod->ops->add(mod, req);
 
        PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
@@ -2032,9 +2085,9 @@ static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
 
        req = talloc_zero(NULL, struct ldb_request);
        req->operation = LDB_MODIFY;
-       req->op.mod.message = PyLdbMessage_AsMessage(py_message);
+       req->op.mod.message = pyldb_Message_AsMessage(py_message);
 
-       mod = PyLdbModule_AsModule(self);
+       mod = pyldb_Module_AsModule(self);
        ret = mod->ops->modify(mod, req);
 
        PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb);
@@ -2053,9 +2106,9 @@ static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
 
        req = talloc_zero(NULL, struct ldb_request);
        req->operation = LDB_DELETE;
-       req->op.del.dn = PyLdbDn_AsDn(py_dn);
+       req->op.del.dn = pyldb_Dn_AsDn(py_dn);
 
-       ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
+       ret = pyldb_Module_AsModule(self)->ops->del(pyldb_Module_AsModule(self), req);
 
        PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
 
@@ -2074,10 +2127,10 @@ static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
        req = talloc_zero(NULL, struct ldb_request);
 
        req->operation = LDB_RENAME;
-       req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
-       req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
+       req->op.rename.olddn = pyldb_Dn_AsDn(py_dn1);
+       req->op.rename.newdn = pyldb_Dn_AsDn(py_dn2);
 
-       ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
+       ret = pyldb_Module_AsModule(self)->ops->rename(pyldb_Module_AsModule(self), req);
 
        PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL);
 
@@ -2110,6 +2163,7 @@ static PyTypeObject PyLdbModule = {
        .tp_basicsize = sizeof(PyLdbModuleObject),
        .tp_dealloc = (destructor)py_ldb_module_dealloc,
        .tp_flags = Py_TPFLAGS_DEFAULT,
+       .tp_doc = "LDB module (extension)",
 };
 
 
@@ -2135,12 +2189,12 @@ static struct ldb_message_element *PyObject_AsMessageElement(
 {
        struct ldb_message_element *me;
 
-       if (PyLdbMessageElement_Check(set_obj)) {
+       if (pyldb_MessageElement_Check(set_obj)) {
                PyLdbMessageElementObject *set_obj_as_me = (PyLdbMessageElementObject *)set_obj;
                /* We have to talloc_reference() the memory context, not the pointer
                 * which may not actually be it's own context */
                if (talloc_reference(mem_ctx, set_obj_as_me->mem_ctx)) {
-                       return PyLdbMessageElement_AsMessageElement(set_obj);
+                       return pyldb_MessageElement_AsMessageElement(set_obj);
                }
                return NULL;
        }
@@ -2207,15 +2261,15 @@ static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObjec
        unsigned int i;
        if (!PyArg_ParseTuple(args, "I", &i))
                return NULL;
-       if (i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
+       if (i >= pyldb_MessageElement_AsMessageElement(self)->num_values)
                Py_RETURN_NONE;
 
-       return PyObject_FromLdbValue(&(PyLdbMessageElement_AsMessageElement(self)->values[i]));
+       return PyObject_FromLdbValue(&(pyldb_MessageElement_AsMessageElement(self)->values[i]));
 }
 
 static PyObject *py_ldb_msg_element_flags(PyLdbMessageElementObject *self, PyObject *args)
 {
-       struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
+       struct ldb_message_element *el = pyldb_MessageElement_AsMessageElement(self);
        return PyInt_FromLong(el->flags);
 }
 
@@ -2226,7 +2280,7 @@ static PyObject *py_ldb_msg_element_set_flags(PyLdbMessageElementObject *self, P
        if (!PyArg_ParseTuple(args, "I", &flags))
                return NULL;
 
-       el = PyLdbMessageElement_AsMessageElement(self);
+       el = pyldb_MessageElement_AsMessageElement(self);
        el->flags = flags;
        Py_RETURN_NONE;
 }
@@ -2240,12 +2294,12 @@ static PyMethodDef py_ldb_msg_element_methods[] = {
 
 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
 {
-       return PyLdbMessageElement_AsMessageElement(self)->num_values;
+       return pyldb_MessageElement_AsMessageElement(self)->num_values;
 }
 
 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
 {
-       struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
+       struct ldb_message_element *el = pyldb_MessageElement_AsMessageElement(self);
        if (idx < 0 || idx >= el->num_values) {
                PyErr_SetString(PyExc_IndexError, "Out of range");
                return NULL;
@@ -2260,14 +2314,16 @@ static PySequenceMethods py_ldb_msg_element_seq = {
 
 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
 {
-       int ret = ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
-                                                                         PyLdbMessageElement_AsMessageElement(other));
+       int ret = ldb_msg_element_compare(pyldb_MessageElement_AsMessageElement(self),
+                                                                         pyldb_MessageElement_AsMessageElement(other));
        return SIGN(ret);
 }
 
 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
 {
-       return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
+       PyObject *el = ldb_msg_element_to_set(NULL,
+                                             pyldb_MessageElement_AsMessageElement(self));
+       return PyObject_GetIter(el);
 }
 
 static PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
@@ -2378,7 +2434,7 @@ static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
 {
        char *element_str = NULL;
        Py_ssize_t i;
-       struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
+       struct ldb_message_element *el = pyldb_MessageElement_AsMessageElement(self);
        PyObject *ret;
 
        for (i = 0; i < el->num_values; i++) {
@@ -2401,7 +2457,7 @@ static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
 
 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
 {
-       struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
+       struct ldb_message_element *el = pyldb_MessageElement_AsMessageElement(self);
 
        if (el->num_values == 1)
                return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
@@ -2427,6 +2483,7 @@ static PyTypeObject PyLdbMessageElement = {
        .tp_as_sequence = &py_ldb_msg_element_seq,
        .tp_new = py_ldb_msg_element_new,
        .tp_flags = Py_TPFLAGS_DEFAULT,
+       .tp_doc = "An element of a Message",
 };
 
 
@@ -2445,6 +2502,11 @@ static PyObject *py_ldb_msg_from_dict(PyTypeObject *type, PyObject *args)
                return NULL;
        }
 
+       if (!PyLdb_Check(py_ldb)) {
+               PyErr_SetString(PyExc_TypeError, "Expected Ldb");
+               return NULL;
+       }
+
        /* mask only flags we are going to use */
        mod_flags = LDB_FLAG_MOD_TYPE(mod_flags);
        if (!mod_flags) {
@@ -2454,7 +2516,7 @@ static PyObject *py_ldb_msg_from_dict(PyTypeObject *type, PyObject *args)
                return NULL;
        }
 
-       ldb_ctx = PyLdb_AsLdbContext(py_ldb);
+       ldb_ctx = pyldb_Ldb_AsLdbContext(py_ldb);
 
        msg = PyDict_AsMessage(ldb_ctx, py_dict, ldb_ctx, mod_flags);
        if (!msg) {
@@ -2481,7 +2543,7 @@ static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args
 
 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
 {
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
        Py_ssize_t i, j = 0;
        PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
        if (msg->dn != NULL) {
@@ -2499,14 +2561,14 @@ static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *p
 {
        struct ldb_message_element *el;
        char *name;
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
        if (!PyString_Check(py_name)) {
                PyErr_SetNone(PyExc_TypeError);
                return NULL;
        }
        name = PyString_AsString(py_name);
-       if (!strcmp(name, "dn"))
-               return PyLdbDn_FromDn(msg->dn);
+       if (!ldb_attr_cmp(name, "dn"))
+               return pyldb_Dn_FromDn(msg->dn);
        el = ldb_msg_find_element(msg, name);
        if (el == NULL) {
                return NULL;
@@ -2524,33 +2586,47 @@ static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
        return ret;
 }
 
-static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
+static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args, PyObject *kwargs)
 {
-       PyObject *name, *ret, *retobj;
-       retobj = NULL;
-       if (!PyArg_ParseTuple(args, "O|O", &name, &retobj))
+       PyObject *def = NULL;
+       const char *kwnames[] = { "name", "default", "idx", NULL };
+       const char *name = NULL;
+       int idx = -1;
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
+       struct ldb_message_element *el;
+
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|Oi:msg",
+                                        discard_const_p(char *, kwnames), &name, &def, &idx)) {
                return NULL;
+       }
 
-       ret = py_ldb_msg_getitem_helper(self, name);
-       if (ret == NULL) {
-               if (PyErr_Occurred())
-                       return NULL;
-               if (retobj != NULL) {
-                       return retobj;
-               } else {
-                       Py_RETURN_NONE;
+       if (strcasecmp(name, "dn") == 0) {
+               return pyldb_Dn_FromDn(msg->dn);
+       }
+
+       el = ldb_msg_find_element(msg, name);
+
+       if (el == NULL || (idx != -1 && el->num_values <= idx)) {
+               if (def != NULL) {
+                       return def;
                }
+               Py_RETURN_NONE;
        }
-       return ret;
+
+       if (idx == -1) {
+               return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg->elements);
+       }
+
+       return PyObject_FromLdbValue(&el->values[idx]);
 }
 
 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
 {
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
        Py_ssize_t i, j = 0;
        PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
        if (msg->dn != NULL) {
-               PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
+               PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", pyldb_Dn_FromDn(msg->dn)));
                j++;
        }
        for (i = 0; i < msg->num_elements; i++, j++) {
@@ -2563,7 +2639,7 @@ static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
 
 static PyObject *py_ldb_msg_elements(PyLdbMessageObject *self)
 {
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
        Py_ssize_t i = 0;
        PyObject *l = PyList_New(msg->num_elements);
        for (i = 0; i < msg->num_elements; i++) {
@@ -2574,7 +2650,7 @@ static PyObject *py_ldb_msg_elements(PyLdbMessageObject *self)
 
 static PyObject *py_ldb_msg_add(PyLdbMessageObject *self, PyObject *args)
 {
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
        PyLdbMessageElementObject *py_element;
        int ret;
        struct ldb_message_element *el;
@@ -2605,7 +2681,12 @@ static PyMethodDef py_ldb_msg_methods[] = {
        { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, 
                "S.remove(name)\n\n"
                "Remove all entries for attributes with the specified name."},
-       { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
+       { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS | METH_KEYWORDS,
+         "msg.get(name,default=None,idx=None) -> string\n"
+         "idx is the index into the values array\n"
+         "if idx is None, then a list is returned\n"
+         "if idx is not None, then the element with that index is returned\n"
+         "if you pass the special name 'dn' then the DN object is returned\n"},
        { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
        { "elements", (PyCFunction)py_ldb_msg_elements, METH_NOARGS, NULL },
        { "add", (PyCFunction)py_ldb_msg_add, METH_VARARGS,
@@ -2638,19 +2719,24 @@ static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject
                /* delitem */
                ldb_msg_remove_attr(self->msg, attr_name);
        } else {
+               int ret;
                struct ldb_message_element *el = PyObject_AsMessageElement(self->msg,
                                                                           value, 0, attr_name);
                if (el == NULL)
                        return -1;
-               ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
-               ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
+               ldb_msg_remove_attr(pyldb_Message_AsMessage(self), attr_name);
+               ret = ldb_msg_add(pyldb_Message_AsMessage(self), el, el->flags);
+               if (ret != LDB_SUCCESS) {
+                       PyErr_SetLdbError(PyExc_LdbError, ret, NULL);
+                       return -1;
+               }
        }
        return 0;
 }
 
 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
 {
-       return PyLdbMessage_AsMessage(self)->num_elements;
+       return pyldb_Message_AsMessage(self)->num_elements;
 }
 
 static PyMappingMethods py_ldb_msg_mapping = {
@@ -2687,7 +2773,7 @@ static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kw
 
        if (pydn != NULL) {
                struct ldb_dn *dn;
-               if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) {
+               if (!pyldb_Object_AsDn(NULL, pydn, NULL, &dn)) {
                        talloc_free(mem_ctx);
                        return NULL;
                }
@@ -2722,19 +2808,19 @@ static PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
 
 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
 {
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
-       return PyLdbDn_FromDn(msg->dn);
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
+       return pyldb_Dn_FromDn(msg->dn);
 }
 
 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
 {
-       struct ldb_message *msg = PyLdbMessage_AsMessage(self);
-       if (!PyLdbDn_Check(value)) {
+       struct ldb_message *msg = pyldb_Message_AsMessage(self);
+       if (!pyldb_Dn_Check(value)) {
                PyErr_SetNone(PyExc_TypeError);
                return -1;
        }
 
-       msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value));
+       msg->dn = talloc_reference(msg, pyldb_Dn_AsDn(value));
        return 0;
 }
 
@@ -2762,8 +2848,8 @@ static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
 static int py_ldb_msg_compare(PyLdbMessageObject *py_msg1,
                              PyLdbMessageObject *py_msg2)
 {
-       struct ldb_message *msg1 = PyLdbMessage_AsMessage(py_msg1),
-                          *msg2 = PyLdbMessage_AsMessage(py_msg2);
+       struct ldb_message *msg1 = pyldb_Message_AsMessage(py_msg1),
+                          *msg2 = pyldb_Message_AsMessage(py_msg2);
        unsigned int i;
        int ret;
 
@@ -2808,6 +2894,7 @@ static PyTypeObject PyLdbMessage = {
        .tp_flags = Py_TPFLAGS_DEFAULT,
        .tp_iter = (getiterfunc)py_ldb_msg_iter,
        .tp_compare = (cmpfunc)py_ldb_msg_compare,
+       .tp_doc = "A LDB Message",
 };
 
 static PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
@@ -2836,6 +2923,7 @@ static PyTypeObject PyLdbTree = {
        .tp_basicsize = sizeof(PyLdbTreeObject),
        .tp_dealloc = (destructor)py_ldb_tree_dealloc,
        .tp_flags = Py_TPFLAGS_DEFAULT,
+       .tp_doc = "A search tree",
 };
 
 /* Ldb_module */
@@ -2844,7 +2932,7 @@ static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
        PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result, *py_base, *py_attrs, *py_tree;
 
-       py_base = PyLdbDn_FromDn(req->op.search.base);
+       py_base = pyldb_Dn_FromDn(req->op.search.base);
 
        if (py_base == NULL)
                return LDB_ERR_OPERATIONS_ERROR;
@@ -2943,7 +3031,7 @@ static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
        PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result, *py_dn;
 
-       py_dn = PyLdbDn_FromDn(req->op.del.dn);
+       py_dn = pyldb_Dn_FromDn(req->op.del.dn);
 
        if (py_dn == NULL)
                return LDB_ERR_OPERATIONS_ERROR;
@@ -2966,12 +3054,12 @@ static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
        PyObject *py_ldb = (PyObject *)mod->private_data;
        PyObject *py_result, *py_olddn, *py_newdn;
 
-       py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
+       py_olddn = pyldb_Dn_FromDn(req->op.rename.olddn);
 
        if (py_olddn == NULL)
                return LDB_ERR_OPERATIONS_ERROR;
 
-       py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
+       py_newdn = pyldb_Dn_FromDn(req->op.rename.newdn);
 
        if (py_newdn == NULL)
                return LDB_ERR_OPERATIONS_ERROR;
@@ -3170,21 +3258,75 @@ static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
        return PyBool_FromLong(ldb_valid_attr_name(name));
 }
 
+/*
+  encode a string using RFC2254 rules
+ */
+static PyObject *py_binary_encode(PyObject *self, PyObject *args)
+{
+       char *str, *encoded;
+       int size = 0;
+       struct ldb_val val;
+       PyObject *ret;
+
+       if (!PyArg_ParseTuple(args, "s#", &str, &size))
+               return NULL;
+       val.data = (uint8_t *)str;
+       val.length = size;
+
+       encoded = ldb_binary_encode(NULL, val);
+       if (encoded == NULL) {
+               PyErr_SetString(PyExc_TypeError, "unable to encode binary string");
+               return NULL;
+       }
+       ret = PyString_FromString(encoded);
+       talloc_free(encoded);
+       return ret;
+}
+
+/*
+  decode a string using RFC2254 rules
+ */
+static PyObject *py_binary_decode(PyObject *self, PyObject *args)
+{
+       char *str;
+       struct ldb_val val;
+       PyObject *ret;
+
+       if (!PyArg_ParseTuple(args, "s", &str))
+               return NULL;
+
+       val = ldb_binary_decode(NULL, str);
+       if (val.data == NULL) {
+               PyErr_SetString(PyExc_TypeError, "unable to decode binary string");
+               return NULL;
+       }
+       ret = Py_BuildValue("s#", val.data, val.length);
+       talloc_free(val.data);
+       return ret;
+}
+
 static PyMethodDef py_ldb_global_methods[] = {
        { "register_module", py_register_module, METH_VARARGS, 
-               "S.register_module(module) -> None\n"
+               "S.register_module(module) -> None\n\n"
                "Register a LDB module."},
        { "timestring", py_timestring, METH_VARARGS, 
-               "S.timestring(int) -> string\n"
+               "S.timestring(int) -> string\n\n"
                "Generate a LDAP time string from a UNIX timestamp" },
        { "string_to_time", py_string_to_time, METH_VARARGS,
-               "S.string_to_time(string) -> int\n"
+               "S.string_to_time(string) -> int\n\n"
                "Parse a LDAP time string into a UNIX timestamp." },
        { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
-               "S.valid_attr_name(name) -> bool\n"
+               "S.valid_attr_name(name) -> bool\n\nn"
                "Check whether the supplied name is a valid attribute name." },
        { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
-               NULL },
+               "S.open() -> Ldb\n\n"
+               "Open a new LDB context." },
+       { "binary_encode", py_binary_encode, METH_VARARGS,
+               "S.binary_encode(string) -> string\n\n"
+               "Perform a RFC2254 binary encoding on a string" },
+       { "binary_decode", py_binary_decode, METH_VARARGS,
+               "S.binary_decode(string) -> string\n\n"
+               "Perform a RFC2254 binary decode on a string" },
        { NULL }
 };
 
@@ -3307,13 +3449,14 @@ void initldb(void)
 
        PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));
 
-#define ADD_LDB_STRING(val)  PyModule_AddObject(m, #val, PyString_FromString(val))
+#define ADD_LDB_STRING(val)  PyModule_AddObject(m, #val, PyString_FromString(LDB_## val))
 
-       ADD_LDB_STRING(LDB_SYNTAX_DN);
-       ADD_LDB_STRING(LDB_SYNTAX_DN);
-       ADD_LDB_STRING(LDB_SYNTAX_DIRECTORY_STRING);
-       ADD_LDB_STRING(LDB_SYNTAX_INTEGER);
-       ADD_LDB_STRING(LDB_SYNTAX_BOOLEAN);
-       ADD_LDB_STRING(LDB_SYNTAX_OCTET_STRING);
-       ADD_LDB_STRING(LDB_SYNTAX_UTC_TIME);
+       ADD_LDB_STRING(SYNTAX_DN);
+       ADD_LDB_STRING(SYNTAX_DIRECTORY_STRING);
+       ADD_LDB_STRING(SYNTAX_INTEGER);
+       ADD_LDB_STRING(SYNTAX_BOOLEAN);
+       ADD_LDB_STRING(SYNTAX_OCTET_STRING);
+       ADD_LDB_STRING(SYNTAX_UTC_TIME);
+       ADD_LDB_STRING(OID_COMPARATOR_AND);
+       ADD_LDB_STRING(OID_COMPARATOR_OR);
 }