summaryrefslogtreecommitdiff
path: root/lib/ldb/pyldb.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ldb/pyldb.c')
-rw-r--r--lib/ldb/pyldb.c124
1 files changed, 122 insertions, 2 deletions
diff --git a/lib/ldb/pyldb.c b/lib/ldb/pyldb.c
index 8c9d6b9599b..a45317a6434 100644
--- a/lib/ldb/pyldb.c
+++ b/lib/ldb/pyldb.c
@@ -193,7 +193,7 @@ static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_
ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
}
-static PyObject *PyObject_FromLdbValue(struct ldb_val *val)
+static PyObject *PyObject_FromLdbValue(const struct ldb_val *val)
{
return PyString_FromStringAndSize((const char *)val->data, val->length);
}
@@ -556,6 +556,103 @@ static PyObject *py_ldb_dn_is_child_of(PyLdbDnObject *self, PyObject *args)
return PyBool_FromLong(ldb_dn_compare_base(base, dn) == 0);
}
+static PyObject *py_ldb_dn_get_component_name(PyLdbDnObject *self, PyObject *args)
+{
+ struct ldb_dn *dn;
+ const char *name;
+ unsigned int num = 0;
+
+ if (!PyArg_ParseTuple(args, "I", &num))
+ return NULL;
+
+ dn = pyldb_Dn_AsDn((PyObject *)self);
+
+ name = ldb_dn_get_component_name(dn, num);
+ if (name == NULL) {
+ Py_RETURN_NONE;
+ }
+
+ return PyString_FromString(name);
+}
+
+static PyObject *py_ldb_dn_get_component_value(PyLdbDnObject *self, PyObject *args)
+{
+ struct ldb_dn *dn;
+ const struct ldb_val *val;
+ unsigned int num = 0;
+
+ if (!PyArg_ParseTuple(args, "I", &num))
+ return NULL;
+
+ dn = pyldb_Dn_AsDn((PyObject *)self);
+
+ val = ldb_dn_get_component_val(dn, num);
+ if (val == NULL) {
+ Py_RETURN_NONE;
+ }
+
+ return PyObject_FromLdbValue(val);
+}
+
+static PyObject *py_ldb_dn_set_component(PyLdbDnObject *self, PyObject *args)
+{
+ unsigned int num = 0;
+ char *name = NULL;
+ PyObject *value = Py_None;
+ struct ldb_val val = { NULL, };
+ int err;
+
+ if (!PyArg_ParseTuple(args, "IsO", &num, &name, &value))
+ return NULL;
+
+ if (value != Py_None) {
+ if (!PyString_Check(value)) {
+ PyErr_SetString(PyExc_TypeError, "Expected a string argument");
+ return NULL;
+ }
+ val.data = (uint8_t *)PyString_AsString(value);
+ val.length = PyString_Size(value);
+ }
+
+ err = ldb_dn_set_component(self->dn, num, name, val);
+ if (err != LDB_SUCCESS) {
+ PyErr_SetString(PyExc_TypeError, "Failed to set component");
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *py_ldb_dn_get_rdn_name(PyLdbDnObject *self)
+{
+ struct ldb_dn *dn;
+ const char *name;
+
+ dn = pyldb_Dn_AsDn((PyObject *)self);
+
+ name = ldb_dn_get_rdn_name(dn);
+ if (name == NULL) {
+ Py_RETURN_NONE;
+ }
+
+ return PyString_FromString(name);
+}
+
+static PyObject *py_ldb_dn_get_rdn_value(PyLdbDnObject *self)
+{
+ struct ldb_dn *dn;
+ const struct ldb_val *val;
+
+ dn = pyldb_Dn_AsDn((PyObject *)self);
+
+ val = ldb_dn_get_rdn_val(dn);
+ if (val == NULL) {
+ Py_RETURN_NONE;
+ }
+
+ return PyObject_FromLdbValue(val);
+}
+
static PyMethodDef py_ldb_dn_methods[] = {
{ "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS,
"S.validate() -> bool\n"
@@ -601,8 +698,23 @@ static PyMethodDef py_ldb_dn_methods[] = {
"S.get_extended_component(name) -> string\n\n"
"returns a DN extended component as a binary string"},
{ "set_extended_component", (PyCFunction)py_ldb_dn_set_extended_component, METH_VARARGS,
- "S.set_extended_component(name, value) -> string\n\n"
+ "S.set_extended_component(name, value) -> None\n\n"
"set a DN extended component as a binary string"},
+ { "get_component_name", (PyCFunction)py_ldb_dn_get_component_name, METH_VARARGS,
+ "S.get_component_name(num) -> string\n"
+ "get the attribute name of the specified component" },
+ { "get_component_value", (PyCFunction)py_ldb_dn_get_component_value, METH_VARARGS,
+ "S.get_component_value(num) -> string\n"
+ "get the attribute value of the specified component as a binary string" },
+ { "set_component", (PyCFunction)py_ldb_dn_set_component, METH_VARARGS,
+ "S.get_component_value(num, name, value) -> None\n"
+ "set the attribute name and value of the specified component" },
+ { "get_rdn_name", (PyCFunction)py_ldb_dn_get_rdn_name, METH_NOARGS,
+ "S.get_rdn_name() -> string\n"
+ "get the RDN attribute name" },
+ { "get_rdn_value", (PyCFunction)py_ldb_dn_get_rdn_value, METH_NOARGS,
+ "S.get_rdn_value() -> string\n"
+ "get the RDN attribute value as a binary string" },
{ NULL }
};
@@ -1075,6 +1187,10 @@ static struct ldb_message *PyDict_AsMessage(TALLOC_CTX *mem_ctx,
PyObject *dn_value = PyDict_GetItemString(py_obj, "dn");
msg = ldb_msg_new(mem_ctx);
+ if (msg == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_obj));
if (dn_value) {
@@ -3088,6 +3204,8 @@ static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
discard_const_p(char, ""));
+ Py_XDECREF(py_result);
+
return LDB_ERR_OPERATIONS_ERROR;
}
@@ -3099,6 +3217,8 @@ static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
discard_const_p(char, ""));
+ Py_XDECREF(py_result);
+
return LDB_ERR_OPERATIONS_ERROR;
}