From 4618a7f3a188e938070e1aa70a9209cdc8045f81 Mon Sep 17 00:00:00 2001 From: Matthias Dieter Wallnöfer Date: Sun, 10 Apr 2011 19:48:07 +0200 Subject: ldb:pyldb.c - all flags should be unsigned Adapt it to the previous commits Reviewed-by: Jelmer + Metze --- source4/lib/ldb/pyldb.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'source4/lib/ldb/pyldb.c') diff --git a/source4/lib/ldb/pyldb.c b/source4/lib/ldb/pyldb.c index 5fcc5a64b69..68f909858de 100644 --- a/source4/lib/ldb/pyldb.c +++ b/source4/lib/ldb/pyldb.c @@ -48,7 +48,7 @@ static PyObject *PyLdbModule_FromModule(struct ldb_module *mod); static struct ldb_message_element *PyObject_AsMessageElement( TALLOC_CTX *mem_ctx, PyObject *set_obj, - int flags, + unsigned int flags, const char *attr_name); /* There's no Py_ssize_t in 2.4, apparently */ @@ -737,11 +737,11 @@ static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs) char *url = NULL; PyObject *py_options = Py_None; const char **options; - int flags = 0; + unsigned int flags = 0; int ret; struct ldb_context *ldb; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__", + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|zIO:Ldb.__init__", discard_const_p(char *, kwnames), &url, &flags, &py_options)) return -1; @@ -792,13 +792,13 @@ static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs) { char *url; - int flags = 0; + unsigned int flags = 0; PyObject *py_options = Py_None; int ret; const char **options; const char * const kwnames[] = { "url", "flags", "options", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO", + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|zIO", discard_const_p(char *, kwnames), &url, &flags, &py_options)) return NULL; @@ -2031,7 +2031,7 @@ static PyTypeObject PyLdbModule = { static struct ldb_message_element *PyObject_AsMessageElement( TALLOC_CTX *mem_ctx, PyObject *set_obj, - int flags, + unsigned int flags, const char *attr_name) { struct ldb_message_element *me; @@ -2122,9 +2122,9 @@ static PyObject *py_ldb_msg_element_flags(PyLdbMessageElementObject *self, PyObj static PyObject *py_ldb_msg_element_set_flags(PyLdbMessageElementObject *self, PyObject *args) { - int flags; + unsigned int flags; struct ldb_message_element *el; - if (!PyArg_ParseTuple(args, "i", &flags)) + if (!PyArg_ParseTuple(args, "I", &flags)) return NULL; el = PyLdbMessageElement_AsMessageElement(self); @@ -2192,13 +2192,13 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb { PyObject *py_elements = NULL; struct ldb_message_element *el; - int flags = 0; + unsigned int flags = 0; char *name = NULL; const char * const kwnames[] = { "elements", "flags", "name", NULL }; PyLdbMessageElementObject *ret; TALLOC_CTX *mem_ctx; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois", + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OIs", discard_const_p(char *, kwnames), &py_elements, &flags, &name)) return NULL; -- cgit From 8741f039955853c092c45cc7f2cedca2384b4c57 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 14 Jun 2011 16:39:49 +1000 Subject: pyldb: added validate option to ldb.modify() This allows validation of ldb messages in a ldb modify operation to be bypassed, by setting validate=False. This is useful in the dbcheck tool to allow for removing invalid empty attributes from the database --- source4/lib/ldb/pyldb.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'source4/lib/ldb/pyldb.c') diff --git a/source4/lib/ldb/pyldb.c b/source4/lib/ldb/pyldb.c index 68f909858de..58a63950b10 100644 --- a/source4/lib/ldb/pyldb.c +++ b/source4/lib/ldb/pyldb.c @@ -819,7 +819,7 @@ static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwa Py_RETURN_NONE; } -static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args) +static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args, PyObject *kwargs) { PyObject *py_msg; PyObject *py_controls = Py_None; @@ -829,8 +829,12 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args) struct ldb_message *msg; int ret; TALLOC_CTX *mem_ctx; + bool validate=true; + const char * const kwnames[] = { "message", "controls", "validate", NULL }; - if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Ob", + discard_const_p(char *, kwnames), + &py_msg, &py_controls, &validate)) return NULL; mem_ctx = talloc_new(NULL); @@ -855,11 +859,13 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args) } msg = PyLdbMessage_AsMessage(py_msg); - ret = ldb_msg_sanity_check(ldb_ctx, msg); - if (ret != LDB_SUCCESS) { - PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx); - talloc_free(mem_ctx); - return NULL; + 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); + talloc_free(mem_ctx); + return NULL; + } } ret = ldb_build_mod_req(&req, ldb_ctx, mem_ctx, msg, parsed_controls, @@ -1586,8 +1592,8 @@ static PyMethodDef py_ldb_methods[] = { { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS, "S.connect(url, flags=0, options=None) -> None\n" "Connect to a LDB URL." }, - { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS, - "S.modify(message) -> None\n" + { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS|METH_KEYWORDS, + "S.modify(message, controls=None, validate=False) -> None\n" "Modify an entry." }, { "add", (PyCFunction)py_ldb_add, METH_VARARGS, "S.add(message) -> None\n" -- cgit From 23202211050b3b3d41632d2bf2795249644d7e8a Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 14 Jun 2011 16:41:35 +1000 Subject: pyldb: make ldb operations more consistent This changes the controls option on ldb operations to be a keyword argument, which is more consistent with general python conventions. This also fixes the pydoc output to include the controls option. --- source4/lib/ldb/pyldb.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) (limited to 'source4/lib/ldb/pyldb.c') diff --git a/source4/lib/ldb/pyldb.c b/source4/lib/ldb/pyldb.c index 58a63950b10..61662f67633 100644 --- a/source4/lib/ldb/pyldb.c +++ b/source4/lib/ldb/pyldb.c @@ -964,7 +964,7 @@ static struct ldb_message *PyDict_AsMessage(TALLOC_CTX *mem_ctx, return msg; } -static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args) +static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args, PyObject *kwargs) { PyObject *py_obj; int ret; @@ -974,8 +974,11 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args) PyObject *py_controls = Py_None; TALLOC_CTX *mem_ctx; struct ldb_control **parsed_controls; + const char * const kwnames[] = { "message", "controls", NULL }; - if (!PyArg_ParseTuple(args, "O|O", &py_obj, &py_controls )) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", + discard_const_p(char *, kwnames), + &py_obj, &py_controls)) return NULL; mem_ctx = talloc_new(NULL); @@ -1053,7 +1056,7 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args) Py_RETURN_NONE; } -static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args) +static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args, PyObject *kwargs) { PyObject *py_dn; struct ldb_dn *dn; @@ -1063,8 +1066,11 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args) PyObject *py_controls = Py_None; TALLOC_CTX *mem_ctx; struct ldb_control **parsed_controls; + const char * const kwnames[] = { "dn", "controls", NULL }; - if (!PyArg_ParseTuple(args, "O|O", &py_dn, &py_controls)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", + discard_const_p(char *, kwnames), + &py_dn, &py_controls)) return NULL; mem_ctx = talloc_new(NULL); @@ -1125,7 +1131,7 @@ static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args) Py_RETURN_NONE; } -static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args) +static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args, PyObject *kwargs) { PyObject *py_dn1, *py_dn2; struct ldb_dn *dn1, *dn2; @@ -1136,10 +1142,13 @@ static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args) struct ldb_control **parsed_controls; struct ldb_context *ldb_ctx; struct ldb_request *req; + const char * const kwnames[] = { "dn1", "dn2", "controls", NULL }; ldb_ctx = PyLdb_AsLdbContext(self); - if (!PyArg_ParseTuple(args, "OO|O", &py_dn1, &py_dn2, &py_controls)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|O", + discard_const_p(char *, kwnames), + &py_dn1, &py_dn2, &py_controls)) return NULL; @@ -1595,14 +1604,14 @@ static PyMethodDef py_ldb_methods[] = { { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS|METH_KEYWORDS, "S.modify(message, controls=None, validate=False) -> None\n" "Modify an entry." }, - { "add", (PyCFunction)py_ldb_add, METH_VARARGS, - "S.add(message) -> None\n" + { "add", (PyCFunction)py_ldb_add, METH_VARARGS|METH_KEYWORDS, + "S.add(message, controls=None) -> None\n" "Add an entry." }, - { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS, - "S.delete(dn) -> None\n" + { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS|METH_KEYWORDS, + "S.delete(dn, controls=None) -> None\n" "Remove an entry." }, - { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS, - "S.rename(old_dn, new_dn) -> None\n" + { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS|METH_KEYWORDS, + "S.rename(old_dn, new_dn, controls=None) -> None\n" "Rename an entry." }, { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS, "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n" -- cgit From 02970f41a27bd4614d6aedf0fe337619b34310db Mon Sep 17 00:00:00 2001 From: Matthieu Patou Date: Sat, 11 Jun 2011 16:57:02 +0400 Subject: py-ldb: allow dictionnary like usage (ie. e.get("myattribute", defVal) --- source4/lib/ldb/pyldb.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'source4/lib/ldb/pyldb.c') diff --git a/source4/lib/ldb/pyldb.c b/source4/lib/ldb/pyldb.c index 61662f67633..b568bc2ccd1 100644 --- a/source4/lib/ldb/pyldb.c +++ b/source4/lib/ldb/pyldb.c @@ -2442,15 +2442,20 @@ static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name) static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args) { - PyObject *name, *ret; - if (!PyArg_ParseTuple(args, "O", &name)) + PyObject *name, *ret, *retobj; + retobj = NULL; + if (!PyArg_ParseTuple(args, "O|O", &name, &retobj)) return NULL; ret = py_ldb_msg_getitem_helper(self, name); if (ret == NULL) { if (PyErr_Occurred()) return NULL; - Py_RETURN_NONE; + if (retobj != NULL) { + return retobj; + } else { + Py_RETURN_NONE; + } } return ret; } -- cgit From dd5350b0a87c82be7d0b0d124885ecfd73bb1b5b Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 22 Jun 2011 12:34:32 +1000 Subject: ldb: expose syntax oids to python Pair-Programmed-With: Andrew Bartlett --- source4/lib/ldb/pyldb.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'source4/lib/ldb/pyldb.c') diff --git a/source4/lib/ldb/pyldb.c b/source4/lib/ldb/pyldb.c index b568bc2ccd1..1bcdaabe13e 100644 --- a/source4/lib/ldb/pyldb.c +++ b/source4/lib/ldb/pyldb.c @@ -3222,4 +3222,14 @@ void initldb(void) PyModule_AddObject(m, "Control", (PyObject *)&PyLdbControl); PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION)); + +#define ADD_LDB_STRING(val) PyModule_AddObject(m, #val, PyString_FromString(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); } -- cgit From 341884c835b9c5785794cba562c2a21939eb4bce Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 22 Jun 2011 13:49:37 +1000 Subject: ldb: added extended_str() method to pyldb this gives access to ldb_dn_get_extended_linearized() from python Pair-Programmed-With: Andrew Bartlett --- source4/lib/ldb/pyldb.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'source4/lib/ldb/pyldb.c') diff --git a/source4/lib/ldb/pyldb.c b/source4/lib/ldb/pyldb.c index 1bcdaabe13e..842da633bba 100644 --- a/source4/lib/ldb/pyldb.c +++ b/source4/lib/ldb/pyldb.c @@ -7,6 +7,8 @@ Copyright (C) 2006 Simo Sorce Copyright (C) 2007-2010 Jelmer Vernooij Copyright (C) 2009-2010 Matthias Dieter Wallnöfer + Copyright (C) 2009-2011 Andrew Tridgell + Copyright (C) 2009-2011 Andrew Bartlett ** NOTE! The following LGPL license applies to the ldb ** library. This does NOT imply that all of Samba is released @@ -384,6 +386,17 @@ static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self) return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn)); } +static PyObject *py_ldb_dn_extended_str(PyLdbDnObject *self, PyObject *args, PyObject *kwargs) +{ + const char * const kwnames[] = { "mode", NULL }; + int mode = 1; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", + discard_const_p(char *, kwnames), + &mode)) + return NULL; + return PyString_FromString(ldb_dn_get_extended_linearized(self->dn, self->dn, mode)); +} + static PyObject *py_ldb_dn_repr(PyLdbDnObject *self) { return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn)))); @@ -485,6 +498,9 @@ static PyMethodDef py_ldb_dn_methods[] = { { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS, "S.canonical_ex_str() -> string\n" "Canonical version of this DN (like a posix path, with terminating newline)." }, + { "extended_str", (PyCFunction)py_ldb_dn_extended_str, METH_VARARGS | METH_KEYWORDS, + "S.extended_str(mode=1) -> string\n" + "Extended version of this DN" }, { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS, "S.parent() -> dn\n" "Get the parent for this DN." }, -- cgit From 505dce2d3aa95d475e12c4e5e4e2b3f1907bdd84 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 22 Jun 2011 14:44:12 +1000 Subject: pyldb: added methods to get/set extended components on DNs this will be used by the dbcheck code Pair-Programmed-With: Andrew Bartlett --- source4/lib/ldb/pyldb.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'source4/lib/ldb/pyldb.c') diff --git a/source4/lib/ldb/pyldb.c b/source4/lib/ldb/pyldb.c index 842da633bba..e2a2e7180e1 100644 --- a/source4/lib/ldb/pyldb.c +++ b/source4/lib/ldb/pyldb.c @@ -397,6 +397,51 @@ static PyObject *py_ldb_dn_extended_str(PyLdbDnObject *self, PyObject *args, PyO return PyString_FromString(ldb_dn_get_extended_linearized(self->dn, self->dn, mode)); } +static PyObject *py_ldb_dn_get_extended_component(PyLdbDnObject *self, PyObject *args) +{ + char *name; + const struct ldb_val *val; + + if (!PyArg_ParseTuple(args, "s", &name)) + return NULL; + val = ldb_dn_get_extended_component(self->dn, name); + if (val == NULL) { + Py_RETURN_NONE; + } + + return PyString_FromStringAndSize((const char *)val->data, val->length); +} + +static PyObject *py_ldb_dn_set_extended_component(PyLdbDnObject *self, PyObject *args) +{ + char *name; + PyObject *value; + int err; + + if (!PyArg_ParseTuple(args, "sO", &name, &value)) + return NULL; + + if (value == Py_None) { + err = ldb_dn_set_extended_component(self->dn, name, NULL); + } else { + struct ldb_val val; + 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_extended_component(self->dn, name, &val); + } + + if (err != LDB_SUCCESS) { + PyErr_SetString(PyExc_TypeError, "Failed to set extended component"); + return NULL; + } + + Py_RETURN_NONE; +} + static PyObject *py_ldb_dn_repr(PyLdbDnObject *self) { return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn)))); @@ -513,6 +558,12 @@ static PyMethodDef py_ldb_dn_methods[] = { { "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"}, + { "get_extended_component", (PyCFunction)py_ldb_dn_get_extended_component, METH_VARARGS, + "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" + "set a DN extended component as a binary string"}, { NULL } }; -- cgit