static PyTypeObject PyLdbSearchIterator;
static PyTypeObject PyLdbMessage;
#define PyLdbMessage_Check(ob) PyObject_TypeCheck(ob, &PyLdbMessage)
-static PyTypeObject PyLdbModule;
static PyTypeObject PyLdbDn;
#define pyldb_Dn_Check(ob) PyObject_TypeCheck(ob, &PyLdbDn)
static PyTypeObject PyLdb;
#define pyldb_MessageElement_Check(ob) PyObject_TypeCheck(ob, &PyLdbMessageElement)
static PyTypeObject PyLdbTree;
-static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx);
-static PyObject *PyLdbModule_FromModule(struct ldb_module *mod);
static struct ldb_message_element *PyObject_AsMessageElement(
TALLOC_CTX *mem_ctx,
PyObject *set_obj,
return (PyObject *)ret;
}
-/**
- * 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
- */
-static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx,
- PyObject *obj)
-{
- struct ldb_result *res;
- Py_ssize_t i;
-
- if (obj == Py_None)
- return NULL;
-
- if (!PyList_Check(obj)) {
- PyErr_SetString(PyExc_ValueError, "Expected list of LDB results");
- return NULL;
- }
-
- res = talloc_zero(mem_ctx, struct ldb_result);
- if (res == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
- res->count = PyList_Size(obj);
- res->msgs = talloc_array(res, struct ldb_message *, res->count);
- if (res->msgs == NULL) {
- talloc_free(res);
- PyErr_NoMemory();
- return NULL;
- }
- for (i = 0; i < res->count; i++) {
- PyObject *item = PyList_GetItem(obj, i);
- if (item == NULL) {
- talloc_free(res);
- return NULL;
- }
- res->msgs[i] = pyldb_Message_AsMessage(item);
- }
- return res;
-}
-
static PyObject *py_ldb_dn_validate(PyLdbDnObject *self,
PyObject *Py_UNUSED(ignored))
{
Py_RETURN_NONE;
}
-static PyObject *py_ldb_modules(PyLdbObject *self,
- PyObject *Py_UNUSED(ignored))
-{
- struct ldb_context *ldb = pyldb_Ldb_AS_LDBCONTEXT(self);
- PyObject *ret = PyList_New(0);
- struct ldb_module *mod;
-
- if (ret == NULL) {
- return PyErr_NoMemory();
- }
- for (mod = ldb->modules; mod; mod = mod->next) {
- PyObject *item = PyLdbModule_FromModule(mod);
- int res = 0;
- if (item == NULL) {
- PyErr_SetString(PyExc_RuntimeError,
- "Failed to load LdbModule");
- Py_CLEAR(ret);
- return NULL;
- }
- res = PyList_Append(ret, item);
- Py_CLEAR(item);
- if (res == -1) {
- Py_CLEAR(ret);
- return NULL;
- }
- }
-
- return ret;
-}
-
static PyObject *py_ldb_sequence_number(PyLdbObject *self, PyObject *args)
{
struct ldb_context *ldb = pyldb_Ldb_AS_LDBCONTEXT(self);
"S.set_opaque(name, value) -> None\n"
"Set an opaque value on this LDB connection. \n"
":note: Passing incorrect values may cause crashes." },
- { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
- "S.modules() -> list\n"
- "Return the list of modules on this LDB connection " },
{ "sequence_number", (PyCFunction)py_ldb_sequence_number, METH_VARARGS,
"S.sequence_number(type) -> value\n"
"Return the value of the sequence according to the requested type" },
{0},
};
-static PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
-{
- TALLOC_CTX *mem_ctx = NULL;
- struct ldb_module *mod_ref = NULL;
- PyLdbModuleObject *ret;
-
- mem_ctx = talloc_new(NULL);
- if (mem_ctx == NULL) {
- return PyErr_NoMemory();
- }
-
- mod_ref = talloc_reference(mem_ctx, mod);
- if (mod_ref == NULL) {
- talloc_free(mem_ctx);
- return PyErr_NoMemory();
- }
-
- ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
- if (ret == NULL) {
- talloc_free(mem_ctx);
- PyErr_NoMemory();
- return NULL;
- }
- ret->mem_ctx = mem_ctx;
- ret->mod = mod_ref;
- return (PyObject *)ret;
-}
-
-static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
-{
- struct ldb_module *mod = pyldb_Ldb_AS_LDBCONTEXT(self)->modules;
- if (mod == NULL) {
- Py_RETURN_NONE;
- }
- return PyLdbModule_FromModule(mod);
-}
-
-static PyGetSetDef py_ldb_getset[] = {
- {
- .name = discard_const_p(char, "firstmodule"),
- .get = (getter)py_ldb_get_firstmodule,
- },
- { .name = NULL },
-};
-
static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
{
struct ldb_context *ldb_ctx = pyldb_Ldb_AS_LDBCONTEXT(self);
.sq_contains = (objobjproc)py_ldb_contains,
};
-static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
-{
- TALLOC_CTX *mem_ctx = NULL;
- struct ldb_context *ldb_ctx_ref = NULL;
- PyLdbObject *ret;
-
- mem_ctx = talloc_new(NULL);
- if (mem_ctx == NULL) {
- return PyErr_NoMemory();
- }
-
- ldb_ctx_ref = talloc_reference(mem_ctx, ldb_ctx);
- if (ldb_ctx_ref == NULL) {
- talloc_free(mem_ctx);
- return PyErr_NoMemory();
- }
-
- ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
- if (ret == NULL) {
- talloc_free(mem_ctx);
- PyErr_NoMemory();
- return NULL;
- }
- ret->mem_ctx = mem_ctx;
- ret->ldb_ctx = ldb_ctx_ref;
- return (PyObject *)ret;
-}
-
static void py_ldb_dealloc(PyLdbObject *self)
{
talloc_free(self->mem_ctx);
.tp_new = py_ldb_new,
.tp_init = (initproc)py_ldb_init,
.tp_dealloc = (destructor)py_ldb_dealloc,
- .tp_getset = py_ldb_getset,
.tp_getattro = PyObject_GenericGetAttr,
.tp_basicsize = sizeof(PyLdbObject),
.tp_doc = "Connection to a LDB database.",
.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
};
-static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
-{
- return PyUnicode_FromFormat("<ldb module '%s'>",
- pyldb_Module_AsModule(self)->ops->name);
-}
-
-static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
-{
- return PyUnicode_FromString(pyldb_Module_AsModule(self)->ops->name);
-}
-
-static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self,
- PyObject *Py_UNUSED(ignored))
-{
- pyldb_Module_AsModule(self)->ops->start_transaction(pyldb_Module_AsModule(self));
- Py_RETURN_NONE;
-}
-
-static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self,
- PyObject *Py_UNUSED(ignored))
-{
- pyldb_Module_AsModule(self)->ops->end_transaction(pyldb_Module_AsModule(self));
- Py_RETURN_NONE;
-}
-
-static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self,
- PyObject *Py_UNUSED(ignored))
-{
- pyldb_Module_AsModule(self)->ops->del_transaction(pyldb_Module_AsModule(self));
- Py_RETURN_NONE;
-}
-
-static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
-{
- PyObject *py_base, *py_tree, *py_attrs, *py_ret;
- int ret, scope;
- struct ldb_request *req;
- const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
- struct ldb_module *mod;
- const char * const*attrs;
-
- /* type "int" rather than "enum" for "scope" is intentional */
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!iOO",
- discard_const_p(char *, kwnames),
- &PyLdbDn, &py_base, &scope, &py_tree, &py_attrs))
- return NULL;
-
- mod = self->mod;
-
- if (py_attrs == Py_None) {
- attrs = NULL;
- } else {
- attrs = PyList_AsStrList(NULL, py_attrs, "attrs");
- if (attrs == NULL)
- return NULL;
- }
-
- ret = ldb_build_search_req(&req, mod->ldb, NULL, pyldb_Dn_AS_DN(py_base),
- scope, NULL /* expr */, attrs,
- NULL /* controls */, NULL, NULL, NULL);
-
- talloc_steal(req, attrs);
-
- PyErr_LDB_ERROR_IS_ERR_RAISE_FREE(PyExc_LdbError, ret, mod->ldb, req);
-
- req->op.search.res = NULL;
-
- ret = mod->ops->search(mod, req);
-
- PyErr_LDB_ERROR_IS_ERR_RAISE_FREE(PyExc_LdbError, ret, mod->ldb, req);
-
- py_ret = PyLdbResult_FromResult(req->op.search.res);
-
- talloc_free(req);
-
- return py_ret;
-}
-
-
-static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
-{
- struct ldb_request *req;
- PyObject *py_message;
- int ret;
- struct ldb_module *mod;
-
- if (!PyArg_ParseTuple(args, "O!", &PyLdbMessage, &py_message))
- return NULL;
-
- req = talloc_zero(NULL, struct ldb_request);
- if (req == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
- req->operation = LDB_ADD;
- req->op.add.message = pyldb_Message_AsMessage(py_message);
-
- mod = pyldb_Module_AsModule(self);
- ret = mod->ops->add(mod, req);
-
- PyErr_LDB_ERROR_IS_ERR_RAISE_FREE(PyExc_LdbError, ret, mod->ldb, req);
-
- TALLOC_FREE(req);
- Py_RETURN_NONE;
-}
-
-static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
-{
- int ret;
- struct ldb_request *req;
- PyObject *py_message;
- struct ldb_module *mod;
-
- if (!PyArg_ParseTuple(args, "O!", &PyLdbMessage, &py_message))
- return NULL;
-
- req = talloc_zero(NULL, struct ldb_request);
- if (req == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
-
- req->operation = LDB_MODIFY;
- req->op.mod.message = pyldb_Message_AsMessage(py_message);
-
- mod = pyldb_Module_AsModule(self);
- ret = mod->ops->modify(mod, req);
-
- PyErr_LDB_ERROR_IS_ERR_RAISE_FREE(PyExc_LdbError, ret, mod->ldb, req);
-
- TALLOC_FREE(req);
- Py_RETURN_NONE;
-}
-
-static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
-{
- int ret;
- struct ldb_request *req;
- PyObject *py_dn;
-
- if (!PyArg_ParseTuple(args, "O!", &PyLdbDn, &py_dn))
- return NULL;
-
- req = talloc_zero(NULL, struct ldb_request);
- if (req == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
- req->operation = LDB_DELETE;
- req->op.del.dn = pyldb_Dn_AS_DN(py_dn);
-
- ret = pyldb_Module_AsModule(self)->ops->del(pyldb_Module_AsModule(self), req);
-
- PyErr_LDB_ERROR_IS_ERR_RAISE_FREE(PyExc_LdbError, ret, NULL, req);
-
- TALLOC_FREE(req);
-
- Py_RETURN_NONE;
-}
-
-static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
-{
- int ret;
- struct ldb_request *req;
- PyObject *py_dn1, *py_dn2;
-
- if (!PyArg_ParseTuple(args, "O!O!", &PyLdbDn, &py_dn1, &PyLdbDn, &py_dn2))
- return NULL;
-
- req = talloc_zero(NULL, struct ldb_request);
- if (req == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
-
- req->operation = LDB_RENAME;
- req->op.rename.olddn = pyldb_Dn_AS_DN(py_dn1);
- req->op.rename.newdn = pyldb_Dn_AS_DN(py_dn2);
-
- ret = pyldb_Module_AsModule(self)->ops->rename(pyldb_Module_AsModule(self), req);
-
- PyErr_LDB_ERROR_IS_ERR_RAISE_FREE(PyExc_LdbError, ret, NULL, req);
-
- TALLOC_FREE(req);
-
- Py_RETURN_NONE;
-}
-
-static PyMethodDef py_ldb_module_methods[] = {
- { "search", PY_DISCARD_FUNC_SIG(PyCFunction, py_ldb_module_search),
- METH_VARARGS|METH_KEYWORDS, NULL },
- { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
- { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
- { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
- { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
- { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
- { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
- { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
- {0},
-};
-
-static void py_ldb_module_dealloc(PyLdbModuleObject *self)
-{
- talloc_free(self->mem_ctx);
- PyObject_Del(self);
-}
-
-static PyTypeObject PyLdbModule = {
- .tp_name = "ldb.LdbModule",
- .tp_methods = py_ldb_module_methods,
- .tp_repr = (reprfunc)py_ldb_module_repr,
- .tp_str = (reprfunc)py_ldb_module_str,
- .tp_basicsize = sizeof(PyLdbModuleObject),
- .tp_dealloc = (destructor)py_ldb_module_dealloc,
- .tp_flags = Py_TPFLAGS_DEFAULT,
- .tp_doc = "LDB module (extension)",
-};
-
-
/**
* Create a ldb_message_element from a Python object.
*
.tp_doc = "A LDB Message",
};
-static PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
-{
- TALLOC_CTX *mem_ctx = NULL;
- struct ldb_parse_tree *tree_ref = NULL;
- PyLdbTreeObject *ret;
-
- mem_ctx = talloc_new(NULL);
- if (mem_ctx == NULL) {
- return PyErr_NoMemory();
- }
-
- tree_ref = talloc_reference(mem_ctx, tree);
- if (tree_ref == NULL) {
- talloc_free(mem_ctx);
- return PyErr_NoMemory();
- }
-
- ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
- if (ret == NULL) {
- talloc_free(mem_ctx);
- PyErr_NoMemory();
- return NULL;
- }
-
- ret->mem_ctx = mem_ctx;
- ret->tree = tree_ref;
- return (PyObject *)ret;
-}
-
static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
{
talloc_free(self->mem_ctx);
.tp_doc = "A search tree",
};
-/* Ldb_module */
-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 = pyldb_Dn_FromDn(req->op.search.base);
-
- if (py_base == NULL)
- return LDB_ERR_OPERATIONS_ERROR;
-
- py_tree = PyLdbTree_FromTree(req->op.search.tree);
-
- if (py_tree == NULL) {
- Py_DECREF(py_base);
- 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);
- if (py_attrs == NULL) {
- Py_DECREF(py_tree);
- Py_DECREF(py_base);
- return LDB_ERR_OPERATIONS_ERROR;
- }
- for (i = 0; i < len; i++) {
- PyObject *py_attr = NULL;
- int ret;
-
- py_attr = PyUnicode_FromString(req->op.search.attrs[i]);
- if (py_attr == NULL) {
- Py_DECREF(py_tree);
- Py_DECREF(py_base);
- Py_DECREF(py_attrs);
- return LDB_ERR_OPERATIONS_ERROR;
- }
-
- ret = PyList_SetItem(py_attrs, i, py_attr);
- if (ret) {
- Py_DECREF(py_attr);
- Py_DECREF(py_tree);
- Py_DECREF(py_base);
- Py_DECREF(py_attrs);
- return LDB_ERR_OPERATIONS_ERROR;
- }
- }
- }
-
- py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
- discard_const_p(char, "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_PYTHON_EXCEPTION;
- }
-
- req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
- if (req->op.search.res == NULL) {
- Py_DECREF(py_result);
- return LDB_ERR_PYTHON_EXCEPTION;
- }
-
- Py_DECREF(py_result);
-
- return LDB_SUCCESS;
-}
-
-static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
-{
- PyObject *py_ldb = (PyObject *)mod->private_data;
- PyObject *py_result, *py_msg;
-
- py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
-
- if (py_msg == NULL) {
- return LDB_ERR_OPERATIONS_ERROR;
- }
-
- py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
- discard_const_p(char, "O"),
- py_msg);
-
- Py_DECREF(py_msg);
-
- if (py_result == NULL) {
- return LDB_ERR_PYTHON_EXCEPTION;
- }
-
- Py_DECREF(py_result);
-
- return LDB_SUCCESS;
-}
-
-static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
-{
- PyObject *py_ldb = (PyObject *)mod->private_data;
- PyObject *py_result, *py_msg;
-
- py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
-
- if (py_msg == NULL) {
- return LDB_ERR_OPERATIONS_ERROR;
- }
-
- py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
- discard_const_p(char, "O"),
- py_msg);
-
- Py_DECREF(py_msg);
-
- if (py_result == NULL) {
- return LDB_ERR_PYTHON_EXCEPTION;
- }
-
- Py_DECREF(py_result);
-
- return LDB_SUCCESS;
-}
-
-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 = pyldb_Dn_FromDn(req->op.del.dn);
-
- if (py_dn == NULL)
- return LDB_ERR_OPERATIONS_ERROR;
-
- py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
- discard_const_p(char, "O"),
- py_dn);
- Py_DECREF(py_dn);
-
- if (py_result == NULL) {
- return LDB_ERR_PYTHON_EXCEPTION;
- }
-
- Py_DECREF(py_result);
-
- return LDB_SUCCESS;
-}
-
-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 = pyldb_Dn_FromDn(req->op.rename.olddn);
-
- if (py_olddn == NULL)
- return LDB_ERR_OPERATIONS_ERROR;
-
- py_newdn = pyldb_Dn_FromDn(req->op.rename.newdn);
-
- if (py_newdn == NULL) {
- Py_DECREF(py_olddn);
- return LDB_ERR_OPERATIONS_ERROR;
- }
-
- py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
- discard_const_p(char, "OO"),
- py_olddn, py_newdn);
-
- Py_DECREF(py_olddn);
- Py_DECREF(py_newdn);
-
- if (py_result == NULL) {
- return LDB_ERR_PYTHON_EXCEPTION;
- }
-
- Py_DECREF(py_result);
-
- return LDB_SUCCESS;
-}
-
-static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
-{
- PyObject *py_ldb = (PyObject *)mod->private_data;
- PyObject *py_result;
-
- py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
- discard_const_p(char, ""));
-
- Py_XDECREF(py_result);
-
- return LDB_ERR_OPERATIONS_ERROR;
-}
-
-static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
-{
- PyObject *py_ldb = (PyObject *)mod->private_data;
- PyObject *py_result;
-
- py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
- discard_const_p(char, ""));
-
- Py_XDECREF(py_result);
-
- return LDB_ERR_OPERATIONS_ERROR;
-}
-
-static int py_module_start_transaction(struct ldb_module *mod)
-{
- PyObject *py_ldb = (PyObject *)mod->private_data;
- PyObject *py_result;
-
- py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
- discard_const_p(char, ""));
-
- if (py_result == NULL) {
- return LDB_ERR_PYTHON_EXCEPTION;
- }
-
- Py_DECREF(py_result);
-
- return LDB_SUCCESS;
-}
-
-static int py_module_end_transaction(struct ldb_module *mod)
-{
- PyObject *py_ldb = (PyObject *)mod->private_data;
- PyObject *py_result;
-
- py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
- discard_const_p(char, ""));
-
- if (py_result == NULL) {
- return LDB_ERR_PYTHON_EXCEPTION;
- }
-
- Py_DECREF(py_result);
-
- return LDB_SUCCESS;
-}
-
-static int py_module_del_transaction(struct ldb_module *mod)
-{
- PyObject *py_ldb = (PyObject *)mod->private_data;
- PyObject *py_result;
-
- py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
- discard_const_p(char, ""));
-
- if (py_result == NULL) {
- return LDB_ERR_PYTHON_EXCEPTION;
- }
-
- Py_DECREF(py_result);
-
- return LDB_SUCCESS;
-}
-
-static int py_module_destructor(struct ldb_module *mod)
-{
- Py_CLEAR(mod->private_data);
- return 0;
-}
-
-static int py_module_init(struct ldb_module *mod)
-{
- PyObject *py_class = (PyObject *)mod->ops->private_data;
- PyObject *py_result, *py_next, *py_ldb;
-
- py_ldb = PyLdb_FromLdbContext(mod->ldb);
-
- if (py_ldb == NULL)
- return LDB_ERR_OPERATIONS_ERROR;
-
- py_next = PyLdbModule_FromModule(mod->next);
-
- if (py_next == NULL) {
- Py_DECREF(py_ldb);
- return LDB_ERR_OPERATIONS_ERROR;
- }
-
- py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
- py_ldb, py_next);
-
- Py_DECREF(py_next);
- Py_DECREF(py_ldb);
-
- if (py_result == NULL) {
- return LDB_ERR_PYTHON_EXCEPTION;
- }
-
- mod->private_data = py_result;
-
- talloc_set_destructor(mod, py_module_destructor);
-
- return ldb_next_init(mod);
-}
-
-static PyObject *py_register_module(PyObject *module, PyObject *args)
-{
- int ret;
- struct ldb_module_ops *ops;
- PyObject *input;
- PyObject *tmp = NULL;
- const char *name = NULL;
-
- if (!PyArg_ParseTuple(args, "O", &input))
- return NULL;
-
- ops = talloc_zero(NULL, struct ldb_module_ops);
- if (ops == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
-
- tmp = PyObject_GetAttrString(input, discard_const_p(char, "name"));
- if (tmp == NULL) {
- TALLOC_FREE(ops);
- return NULL;
- }
- name = PyUnicode_AsUTF8(tmp);
- if (name == NULL) {
- Py_DECREF(tmp);
- TALLOC_FREE(ops);
- return NULL;
- }
-
- ops->name = talloc_strdup(ops, name);
- Py_XDECREF(tmp);
- if (ops->name == NULL) {
- TALLOC_FREE(ops);
- return PyErr_NoMemory();
- }
- Py_INCREF(input);
- ops->private_data = input;
- ops->init_context = py_module_init;
- ops->search = py_module_search;
- ops->add = py_module_add;
- ops->modify = py_module_modify;
- ops->del = py_module_del;
- ops->rename = py_module_rename;
- ops->request = py_module_request;
- ops->extended = py_module_extended;
- ops->start_transaction = py_module_start_transaction;
- ops->end_transaction = py_module_end_transaction;
- ops->del_transaction = py_module_del_transaction;
-
- ret = ldb_register_module(ops);
- if (ret != LDB_SUCCESS) {
- Py_DECREF(input);
- TALLOC_FREE(ops);
- }
-
- PyErr_LDB_ERROR_IS_ERR_RAISE_FREE(PyExc_LdbError, ret, NULL, ops);
-
- Py_RETURN_NONE;
-}
-
static PyObject *py_timestring(PyObject *module, PyObject *args)
{
/* most times "time_t" is a signed integer type with 32 or 64 bit:
}
static PyMethodDef py_ldb_global_methods[] = {
- { "register_module", py_register_module, METH_VARARGS,
- "S.register_module(module) -> None\n\n"
- "Register a LDB module."},
{ "timestring", py_timestring, METH_VARARGS,
"S.timestring(int) -> string\n\n"
"Generate a LDAP time string from a UNIX timestamp" },
if (PyType_Ready(&PyLdb) < 0)
return NULL;
- if (PyType_Ready(&PyLdbModule) < 0)
- return NULL;
-
if (PyType_Ready(&PyLdbTree) < 0)
return NULL;
Py_INCREF(&PyLdb);
Py_INCREF(&PyLdbDn);
- Py_INCREF(&PyLdbModule);
Py_INCREF(&PyLdbMessage);
Py_INCREF(&PyLdbMessageElement);
Py_INCREF(&PyLdbTree);
PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
- PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);
PyModule_AddObject(m, "Result", (PyObject *)&PyLdbResult);
PyModule_AddObject(m, "Control", (PyObject *)&PyLdbControl);