From 2e7a6cb6bf32a49682ccadc07244d3a6ae4058d3 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 21 Dec 2008 03:08:14 +0100 Subject: py: Fix initialisation of subtypes, fix segfaults. --- lib/talloc/pytalloc.c | 4 +- source4/lib/ldb/pyldb.c | 8 +- source4/lib/registry/pyregistry.c | 3 + source4/param/param.i | 7 +- source4/param/param.py | 2 +- source4/param/param_wrap.c | 93 +++++----- source4/scripting/python/config.mk | 10 +- source4/scripting/python/pyglue.c | 273 ++++++++++++++++++++++++++++ source4/scripting/python/pymisc.c | 273 ---------------------------- source4/scripting/python/samba/__init__.py | 16 +- source4/scripting/python/samba/idmap.py | 4 +- source4/scripting/python/samba/provision.py | 12 +- source4/scripting/python/samba/samdb.py | 14 +- 13 files changed, 367 insertions(+), 352 deletions(-) create mode 100644 source4/scripting/python/pyglue.c delete mode 100644 source4/scripting/python/pymisc.c diff --git a/lib/talloc/pytalloc.c b/lib/talloc/pytalloc.c index f4b7d10e97..8bc85eead6 100644 --- a/lib/talloc/pytalloc.c +++ b/lib/talloc/pytalloc.c @@ -25,13 +25,13 @@ void py_talloc_dealloc(PyObject* self) py_talloc_Object *obj = (py_talloc_Object *)self; talloc_free(obj->talloc_ctx); obj->talloc_ctx = NULL; - PyObject_Del(self); + self->ob_type->tp_free(self); } PyObject *py_talloc_import_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr) { - py_talloc_Object *ret = PyObject_New(py_talloc_Object, py_type); + py_talloc_Object *ret = (py_talloc_Object *)py_type->tp_alloc(py_type, 0); ret->talloc_ctx = talloc_new(NULL); if (ret->talloc_ctx == NULL) { return NULL; diff --git a/source4/lib/ldb/pyldb.c b/source4/lib/ldb/pyldb.c index 53b4fcef3d..340e02cafa 100644 --- a/source4/lib/ldb/pyldb.c +++ b/source4/lib/ldb/pyldb.c @@ -320,6 +320,7 @@ PyTypeObject PyLdbDn = { .tp_new = py_ldb_dn_new, .tp_dealloc = py_talloc_dealloc, .tp_basicsize = sizeof(PyLdbObject), + .tp_flags = Py_TPFLAGS_DEFAULT, }; /* Debug */ @@ -971,7 +972,6 @@ PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx) } PyTypeObject PyLdb = { - PyObject_HEAD_INIT(NULL) .tp_name = "Ldb", .tp_methods = py_ldb_methods, .tp_repr = (reprfunc)py_ldb_repr, @@ -1151,6 +1151,7 @@ PyTypeObject PyLdbModule = { .tp_str = (reprfunc)py_ldb_module_str, .tp_basicsize = sizeof(py_talloc_Object), .tp_dealloc = py_talloc_dealloc, + .tp_flags = Py_TPFLAGS_DEFAULT, }; struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx, @@ -1328,6 +1329,7 @@ PyTypeObject PyLdbMessageElement = { .tp_iter = (getiterfunc)py_ldb_msg_element_iter, .tp_as_sequence = &py_ldb_msg_element_seq, .tp_new = py_ldb_msg_element_new, + .tp_flags = Py_TPFLAGS_DEFAULT, }; static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args) @@ -1462,7 +1464,7 @@ static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kw if (!PyObject_AsDn(NULL, pydn, NULL, &ret->dn)) return NULL; - return py_talloc_import(&PyLdbMessage, ret); + return py_talloc_import(type, ret); } PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg) @@ -1505,6 +1507,7 @@ PyTypeObject PyLdbMessage = { .tp_dealloc = py_talloc_dealloc, .tp_new = py_ldb_msg_new, .tp_repr = (reprfunc)py_ldb_msg_repr, + .tp_flags = Py_TPFLAGS_DEFAULT, }; PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree) @@ -1516,6 +1519,7 @@ PyTypeObject PyLdbTree = { .tp_name = "Tree", .tp_basicsize = sizeof(PyLdbTreeObject), .tp_dealloc = py_talloc_dealloc, + .tp_flags = Py_TPFLAGS_DEFAULT, }; /* Ldb_module */ diff --git a/source4/lib/registry/pyregistry.c b/source4/lib/registry/pyregistry.c index e49fdad8a2..b93071f703 100644 --- a/source4/lib/registry/pyregistry.c +++ b/source4/lib/registry/pyregistry.c @@ -156,6 +156,7 @@ PyTypeObject PyRegistry = { .tp_new = registry_new, .tp_basicsize = sizeof(py_talloc_Object), .tp_dealloc = py_talloc_dealloc, + .tp_flags = Py_TPFLAGS_DEFAULT, }; static PyObject *py_hive_key_del(PyObject *self, PyObject *args) @@ -246,12 +247,14 @@ PyTypeObject PyHiveKey = { .tp_new = hive_open, .tp_basicsize = sizeof(py_talloc_Object), .tp_dealloc = py_talloc_dealloc, + .tp_flags = Py_TPFLAGS_DEFAULT, }; PyTypeObject PyRegistryKey = { .tp_name = "RegistryKey", .tp_basicsize = sizeof(py_talloc_Object), .tp_dealloc = py_talloc_dealloc, + .tp_flags = Py_TPFLAGS_DEFAULT, }; static PyObject *py_open_samba(PyObject *self, PyObject *args, PyObject *kwargs) diff --git a/source4/param/param.i b/source4/param/param.i index 06579f9b26..c04c9279f4 100644 --- a/source4/param/param.i +++ b/source4/param/param.i @@ -78,6 +78,10 @@ typedef struct loadparm_context { return lp_set_cmdline($self, parm_name, parm_value); } + char *private_path(const char *name, TALLOC_CTX *mem_ctx) { + return private_path(mem_ctx, $self, name); + } + %feature("docstring") set "S.get(name, service_name) -> value\n" \ "Find specified parameter."; PyObject *get(const char *param_name, const char *service_name) @@ -354,6 +358,3 @@ struct loadparm_context *py_default_loadparm_context(TALLOC_CTX *mem_ctx) } %} - -char *private_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx, - const char *name); diff --git a/source4/param/param.py b/source4/param/param.py index 5c455671f9..3662c99a9c 100644 --- a/source4/param/param.py +++ b/source4/param/param.py @@ -119,6 +119,7 @@ LoadParm.is_mydomain = new_instancemethod(_param.LoadParm_is_mydomain,None,LoadP LoadParm.is_myname = new_instancemethod(_param.LoadParm_is_myname,None,LoadParm) LoadParm.use = new_instancemethod(_param.LoadParm_use,None,LoadParm) LoadParm.set = new_instancemethod(_param.LoadParm_set,None,LoadParm) +LoadParm.private_path = new_instancemethod(_param.LoadParm_private_path,None,LoadParm) LoadParm.get = new_instancemethod(_param.LoadParm_get,None,LoadParm) LoadParm_swigregister = _param.LoadParm_swigregister LoadParm_swigregister(LoadParm) @@ -262,6 +263,5 @@ param_section.next_parameter = new_instancemethod(_param.param_section_next_para param_section_swigregister = _param.param_section_swigregister param_section_swigregister(param_section) -private_path = _param.private_path diff --git a/source4/param/param_wrap.c b/source4/param/param_wrap.c index 2849950f7a..6ce17957c1 100644 --- a/source4/param/param_wrap.c +++ b/source4/param/param_wrap.c @@ -2673,6 +2673,9 @@ SWIGINTERN bool loadparm_context_set(loadparm_context *self,char const *parm_nam return false; return lp_set_cmdline(self, parm_name, parm_value); } +SWIGINTERN char *loadparm_context_private_path(loadparm_context *self,char const *name,TALLOC_CTX *mem_ctx){ + return private_path(mem_ctx, self, name); + } SWIGINTERN PyObject *loadparm_context_get(loadparm_context *self,char const *param_name,char const *service_name){ struct parm_struct *parm = NULL; void *parm_ptr = NULL; @@ -3217,6 +3220,50 @@ fail: } +SWIGINTERN PyObject *_wrap_LoadParm_private_path(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *kwargs) { + PyObject *resultobj = 0; + loadparm_context *arg1 = (loadparm_context *) 0 ; + char *arg2 = (char *) 0 ; + TALLOC_CTX *arg3 = (TALLOC_CTX *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + char * kwnames[] = { + (char *) "self",(char *) "name", NULL + }; + char *result = 0 ; + + arg1 = loadparm_init(NULL); + arg3 = NULL; + if (!PyArg_ParseTupleAndKeywords(args,kwargs,(char *)"|OO:LoadParm_private_path",kwnames,&obj0,&obj1)) SWIG_fail; + if (obj0) { + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_loadparm_context, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "LoadParm_private_path" "', argument " "1"" of type '" "loadparm_context *""'"); + } + arg1 = (loadparm_context *)(argp1); + } + if (obj1) { + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "LoadParm_private_path" "', argument " "2"" of type '" "char const *""'"); + } + arg2 = (char *)(buf2); + } + result = (char *)loadparm_context_private_path(arg1,(char const *)arg2,arg3); + resultobj = SWIG_FromCharPtr((const char *)result); + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + return resultobj; +fail: + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + return NULL; +} + + SWIGINTERN PyObject *_wrap_LoadParm_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *kwargs) { PyObject *resultobj = 0; loadparm_context *arg1 = (loadparm_context *) 0 ; @@ -4153,50 +4200,6 @@ SWIGINTERN PyObject *param_section_swiginit(PyObject *SWIGUNUSEDPARM(self), PyOb return SWIG_Python_InitShadowInstance(args); } -SWIGINTERN PyObject *_wrap_private_path(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *kwargs) { - PyObject *resultobj = 0; - TALLOC_CTX *arg1 = (TALLOC_CTX *) 0 ; - struct loadparm_context *arg2 = (struct loadparm_context *) 0 ; - char *arg3 = (char *) 0 ; - void *argp2 = 0 ; - int res2 = 0 ; - int res3 ; - char *buf3 = 0 ; - int alloc3 = 0 ; - PyObject * obj0 = 0 ; - PyObject * obj1 = 0 ; - char * kwnames[] = { - (char *) "lp_ctx",(char *) "name", NULL - }; - char *result = 0 ; - - arg2 = loadparm_init(NULL); - arg1 = NULL; - if (!PyArg_ParseTupleAndKeywords(args,kwargs,(char *)"|OO:private_path",kwnames,&obj0,&obj1)) SWIG_fail; - if (obj0) { - res2 = SWIG_ConvertPtr(obj0, &argp2,SWIGTYPE_p_loadparm_context, 0 | 0 ); - if (!SWIG_IsOK(res2)) { - SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "private_path" "', argument " "2"" of type '" "struct loadparm_context *""'"); - } - arg2 = (struct loadparm_context *)(argp2); - } - if (obj1) { - res3 = SWIG_AsCharPtrAndSize(obj1, &buf3, NULL, &alloc3); - if (!SWIG_IsOK(res3)) { - SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "private_path" "', argument " "3"" of type '" "char const *""'"); - } - arg3 = (char *)(buf3); - } - result = (char *)private_path(arg1,arg2,(char const *)arg3); - resultobj = SWIG_FromCharPtr((const char *)result); - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return resultobj; -fail: - if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); - return NULL; -} - - static PyMethodDef SwigMethods[] = { { (char *)"new_LoadParm", (PyCFunction)_wrap_new_LoadParm, METH_NOARGS, NULL}, { (char *)"LoadParm_default_service", (PyCFunction) _wrap_LoadParm_default_service, METH_VARARGS | METH_KEYWORDS, NULL}, @@ -4227,6 +4230,7 @@ static PyMethodDef SwigMethods[] = { "S.set(name, value) -> bool\n" "Change a parameter.\n" ""}, + { (char *)"LoadParm_private_path", (PyCFunction) _wrap_LoadParm_private_path, METH_VARARGS | METH_KEYWORDS, NULL}, { (char *)"LoadParm_get", (PyCFunction) _wrap_LoadParm_get, METH_VARARGS | METH_KEYWORDS, NULL}, { (char *)"delete_LoadParm", (PyCFunction) _wrap_delete_LoadParm, METH_VARARGS | METH_KEYWORDS, NULL}, { (char *)"LoadParm_swigregister", LoadParm_swigregister, METH_VARARGS, NULL}, @@ -4274,7 +4278,6 @@ static PyMethodDef SwigMethods[] = { { (char *)"delete_param_section", (PyCFunction)_wrap_delete_param_section, METH_O, NULL}, { (char *)"param_section_swigregister", param_section_swigregister, METH_VARARGS, NULL}, { (char *)"param_section_swiginit", param_section_swiginit, METH_VARARGS, NULL}, - { (char *)"private_path", (PyCFunction) _wrap_private_path, METH_VARARGS | METH_KEYWORDS, NULL}, { NULL, NULL, 0, NULL } }; diff --git a/source4/scripting/python/config.mk b/source4/scripting/python/config.mk index 8eb0aab528..1f57294c59 100644 --- a/source4/scripting/python/config.mk +++ b/source4/scripting/python/config.mk @@ -15,13 +15,13 @@ PRIVATE_DEPENDENCIES = LIBNDR python_uuid_OBJ_FILES = $(pyscriptsrcdir)/uuidmodule.o -[PYTHON::python_misc] -LIBRARY_REALNAME = samba/misc.$(SHLIBEXT) -PRIVATE_DEPENDENCIES = LIBNDR LIBLDB SAMDB CREDENTIALS swig_ldb +[PYTHON::python_glue] +LIBRARY_REALNAME = samba/glue.$(SHLIBEXT) +PRIVATE_DEPENDENCIES = LIBNDR LIBLDB SAMDB CREDENTIALS swig_ldb python_dcerpc_misc python_dcerpc_security -python_misc_OBJ_FILES = $(pyscriptsrcdir)/pymisc.o +python_glue_OBJ_FILES = $(pyscriptsrcdir)/pyglue.o -$(python_misc_OBJ_FILES): CFLAGS+=$(CFLAG_NO_CAST_QUAL) -I$(ldbsrcdir) +$(python_glue_OBJ_FILES): CFLAGS+=$(CFLAG_NO_CAST_QUAL) -I$(ldbsrcdir) _PY_FILES = $(shell find $(pyscriptsrcdir)/samba ../lib/subunit/python -name "*.py") diff --git a/source4/scripting/python/pyglue.c b/source4/scripting/python/pyglue.c new file mode 100644 index 0000000000..381792b735 --- /dev/null +++ b/source4/scripting/python/pyglue.c @@ -0,0 +1,273 @@ +/* + Unix SMB/CIFS implementation. + Copyright (C) Jelmer Vernooij 2007 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "includes.h" +#include "ldb.h" +#include "param/param.h" +#include "auth/credentials/credentials.h" +#include "dsdb/samdb/samdb.h" +#include "lib/ldb-samba/ldif_handlers.h" +#include "librpc/ndr/libndr.h" +#include "version.h" +#include +#include "pyldb.h" +#include "libcli/util/pyerrors.h" +#include "librpc/gen_ndr/py_misc.h" +#include "librpc/gen_ndr/py_security.h" + +/* FIXME: These should be in a header file somewhere, once we finish moving + * away from SWIG .. */ +extern struct loadparm_context *lp_from_py_object(PyObject *py_obj); +extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj); + +#define PyErr_LDB_OR_RAISE(py_ldb, ldb) \ + if (!PyLdb_Check(py_ldb)) { \ + /*PyErr_SetString(PyExc_TypeError, "Ldb connection object required"); \ + return NULL; \ */ \ + } \ + ldb = PyLdb_AsLdbContext(py_ldb); + + +static PyObject *py_generate_random_str(PyObject *self, PyObject *args) +{ + int len; + PyObject *ret; + char *retstr; + if (!PyArg_ParseTuple(args, "i", &len)) + return NULL; + + retstr = generate_random_str(NULL, len); + ret = PyString_FromString(retstr); + talloc_free(retstr); + return ret; +} + +static PyObject *py_unix2nttime(PyObject *self, PyObject *args) +{ + time_t t; + NTTIME nt; + if (!PyArg_ParseTuple(args, "I", &t)) + return NULL; + + unix_to_nt_time(&nt, t); + + return PyInt_FromLong((uint64_t)nt); +} + +static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args) +{ + PyObject *py_creds, *py_ldb; + struct cli_credentials *creds; + struct ldb_context *ldb; + if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_creds)) + return NULL; + + PyErr_LDB_OR_RAISE(py_ldb, ldb); + + creds = cli_credentials_from_py_object(py_creds); + if (creds == NULL) { + PyErr_SetString(PyExc_TypeError, "Expected credentials object"); + return NULL; + } + + ldb_set_opaque(ldb, "credentials", creds); + + return Py_None; +} + +static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args) +{ + PyObject *py_lp_ctx, *py_ldb; + struct loadparm_context *lp_ctx; + struct ldb_context *ldb; + if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_lp_ctx)) + return NULL; + + PyErr_LDB_OR_RAISE(py_ldb, ldb); + + lp_ctx = lp_from_py_object(py_lp_ctx); + if (lp_ctx == NULL) { + PyErr_SetString(PyExc_TypeError, "Expected loadparm object"); + return NULL; + } + + ldb_set_opaque(ldb, "loadparm", lp_ctx); + + return Py_None; +} + + +static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args) +{ + PyObject *py_session_info, *py_ldb; + struct auth_session_info *info; + struct ldb_context *ldb; + if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_session_info)) + return NULL; + + PyErr_LDB_OR_RAISE(py_ldb, ldb); + /* FIXME: Magic py_session_info -> info */ + + ldb_set_opaque(ldb, "sessionInfo", info); + + return Py_None; +} + +static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args) +{ + PyObject *py_ldb, *py_sid; + struct ldb_context *ldb; + struct dom_sid *sid; + bool ret; + + if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid)) + return NULL; + + PyErr_LDB_OR_RAISE(py_ldb, ldb); + + if (!dom_sid_Check(py_sid)) { + PyErr_SetString(PyExc_TypeError, "expected SID"); + return NULL; + } + + sid = py_talloc_get_ptr(py_sid); + + ret = samdb_set_domain_sid(ldb, sid); + if (!ret) { + PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed"); + return NULL; + } + return Py_None; +} + +static PyObject *py_ldb_register_samba_handlers(PyObject *self, PyObject *args) +{ + PyObject *py_ldb; + struct ldb_context *ldb; + int ret; + + if (!PyArg_ParseTuple(args, "O", &py_ldb)) + return NULL; + + PyErr_LDB_OR_RAISE(py_ldb, ldb); + ret = ldb_register_samba_handlers(ldb); + + PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb); + return Py_None; +} + +static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args) +{ + PyObject *py_ldb, *py_guid; + bool ret; + struct GUID *guid; + struct ldb_context *ldb; + if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid)) + return NULL; + + PyErr_LDB_OR_RAISE(py_ldb, ldb); + if (!GUID_Check(py_guid)) { + PyErr_SetString(PyExc_TypeError, "Expected GUID"); + return NULL; + } + guid = py_talloc_get_ptr(py_guid); + + ret = samdb_set_ntds_invocation_id(ldb, guid); + if (!ret) { + PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed"); + return NULL; + } + return Py_None; +} + +static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args) +{ + PyObject *py_ldb; + struct ldb_context *ldb; + int ret; + if (!PyArg_ParseTuple(args, "O", &py_ldb)) + return NULL; + + PyErr_LDB_OR_RAISE(py_ldb, ldb); + + ret = dsdb_set_global_schema(ldb); + PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb); + + return Py_None; +} + +static PyObject *py_dsdb_attach_schema_from_ldif_file(PyObject *self, PyObject *args) +{ + WERROR result; + char *pf, *df; + PyObject *py_ldb; + struct ldb_context *ldb; + + if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df)) + return NULL; + + PyErr_LDB_OR_RAISE(py_ldb, ldb); + + result = dsdb_attach_schema_from_ldif_file(ldb, pf, df); + PyErr_WERROR_IS_ERR_RAISE(result); + + return Py_None; +} + +static PyMethodDef py_misc_methods[] = { + { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS, + "random_password(len) -> string\n" + "Generate random password with specified length." }, + { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS, + "unix2nttime(timestamp) -> nttime" }, + { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS, + "ldb_set_credentials(ldb, credentials) -> None\n" + "Set credentials to use when connecting." }, + { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS, + "ldb_set_session_info(ldb, session_info)\n" + "Set session info to use when connecting." }, + { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS, + "ldb_set_loadparm(ldb, session_info)\n" + "Set loadparm context to use when connecting." }, + { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS, + "samdb_set_domain_sid(samdb, sid)\n" + "Set SID of domain to use." }, + { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS, + "ldb_register_samba_handlers(ldb)\n" + "Register Samba-specific LDB modules and schemas." }, + { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS, + NULL }, + { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS, + NULL }, + { "dsdb_attach_schema_from_ldif_file", (PyCFunction)py_dsdb_attach_schema_from_ldif_file, METH_VARARGS, + NULL }, + { NULL } +}; + +void initglue(void) +{ + PyObject *m; + + m = Py_InitModule3("glue", py_misc_methods, + "Python bindings for miscellaneous Samba functions."); + if (m == NULL) + return; + + PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING)); +} + diff --git a/source4/scripting/python/pymisc.c b/source4/scripting/python/pymisc.c deleted file mode 100644 index 2ba682a6d2..0000000000 --- a/source4/scripting/python/pymisc.c +++ /dev/null @@ -1,273 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Copyright (C) Jelmer Vernooij 2007 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "includes.h" -#include "ldb.h" -#include "param/param.h" -#include "auth/credentials/credentials.h" -#include "dsdb/samdb/samdb.h" -#include "lib/ldb-samba/ldif_handlers.h" -#include "librpc/ndr/libndr.h" -#include "version.h" -#include -#include "pyldb.h" -#include "libcli/util/pyerrors.h" -#include "librpc/gen_ndr/py_misc.h" -#include "librpc/gen_ndr/py_security.h" - -/* FIXME: These should be in a header file somewhere, once we finish moving - * away from SWIG .. */ -extern struct loadparm_context *lp_from_py_object(PyObject *py_obj); -extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj); - -#define PyErr_LDB_OR_RAISE(py_ldb, ldb) \ - if (!PyLdb_Check(py_ldb)) { \ - PyErr_SetString(PyExc_TypeError, "Ldb connection object required"); \ - return NULL; \ - } \ - ldb = PyLdb_AsLdbContext(py_ldb); - - -static PyObject *py_generate_random_str(PyObject *self, PyObject *args) -{ - int len; - PyObject *ret; - char *retstr; - if (!PyArg_ParseTuple(args, "i", &len)) - return NULL; - - retstr = generate_random_str(NULL, len); - ret = PyString_FromString(retstr); - talloc_free(retstr); - return ret; -} - -static PyObject *py_unix2nttime(PyObject *self, PyObject *args) -{ - time_t t; - NTTIME nt; - if (!PyArg_ParseTuple(args, "I", &t)) - return NULL; - - unix_to_nt_time(&nt, t); - - return PyInt_FromLong((uint64_t)nt); -} - -static PyObject *py_ldb_set_credentials(PyObject *self, PyObject *args) -{ - PyObject *py_creds, *py_ldb; - struct cli_credentials *creds; - struct ldb_context *ldb; - if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_creds)) - return NULL; - - PyErr_LDB_OR_RAISE(py_ldb, ldb); - - creds = cli_credentials_from_py_object(py_creds); - if (creds == NULL) { - PyErr_SetString(PyExc_TypeError, "Expected credentials object"); - return NULL; - } - - ldb_set_opaque(ldb, "credentials", creds); - - return Py_None; -} - -static PyObject *py_ldb_set_loadparm(PyObject *self, PyObject *args) -{ - PyObject *py_lp_ctx, *py_ldb; - struct loadparm_context *lp_ctx; - struct ldb_context *ldb; - if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_lp_ctx)) - return NULL; - - PyErr_LDB_OR_RAISE(py_ldb, ldb); - - lp_ctx = lp_from_py_object(py_lp_ctx); - if (lp_ctx == NULL) { - PyErr_SetString(PyExc_TypeError, "Expected loadparm object"); - return NULL; - } - - ldb_set_opaque(ldb, "loadparm", lp_ctx); - - return Py_None; -} - - -static PyObject *py_ldb_set_session_info(PyObject *self, PyObject *args) -{ - PyObject *py_session_info, *py_ldb; - struct auth_session_info *info; - struct ldb_context *ldb; - if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_session_info)) - return NULL; - - PyErr_LDB_OR_RAISE(py_ldb, ldb); - /* FIXME: Magic py_session_info -> info */ - - ldb_set_opaque(ldb, "sessionInfo", info); - - return Py_None; -} - -static PyObject *py_samdb_set_domain_sid(PyLdbObject *self, PyObject *args) -{ - PyObject *py_ldb, *py_sid; - struct ldb_context *ldb; - struct dom_sid *sid; - bool ret; - - if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_sid)) - return NULL; - - PyErr_LDB_OR_RAISE(py_ldb, ldb); - - if (!dom_sid_Check(py_sid)) { - PyErr_SetString(PyExc_TypeError, "expected SID"); - return NULL; - } - - sid = py_talloc_get_ptr(py_sid); - - ret = samdb_set_domain_sid(ldb, sid); - if (!ret) { - PyErr_SetString(PyExc_RuntimeError, "set_domain_sid failed"); - return NULL; - } - return Py_None; -} - -static PyObject *py_ldb_register_samba_handlers(PyObject *self, PyObject *args) -{ - PyObject *py_ldb; - struct ldb_context *ldb; - int ret; - - if (!PyArg_ParseTuple(args, "O", &py_ldb)) - return NULL; - - PyErr_LDB_OR_RAISE(py_ldb, ldb); - ret = ldb_register_samba_handlers(ldb); - - PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb); - return Py_None; -} - -static PyObject *py_dsdb_set_ntds_invocation_id(PyObject *self, PyObject *args) -{ - PyObject *py_ldb, *py_guid; - bool ret; - struct GUID *guid; - struct ldb_context *ldb; - if (!PyArg_ParseTuple(args, "OO", &py_ldb, &py_guid)) - return NULL; - - PyErr_LDB_OR_RAISE(py_ldb, ldb); - if (!GUID_Check(py_guid)) { - PyErr_SetString(PyExc_TypeError, "Expected GUID"); - return NULL; - } - guid = py_talloc_get_ptr(py_guid); - - ret = samdb_set_ntds_invocation_id(ldb, guid); - if (!ret) { - PyErr_SetString(PyExc_RuntimeError, "set_ntds_invocation_id failed"); - return NULL; - } - return Py_None; -} - -static PyObject *py_dsdb_set_global_schema(PyObject *self, PyObject *args) -{ - PyObject *py_ldb; - struct ldb_context *ldb; - int ret; - if (!PyArg_ParseTuple(args, "O", &py_ldb)) - return NULL; - - PyErr_LDB_OR_RAISE(py_ldb, ldb); - - ret = dsdb_set_global_schema(ldb); - PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb); - - return Py_None; -} - -static PyObject *py_dsdb_attach_schema_from_ldif_file(PyObject *self, PyObject *args) -{ - WERROR result; - char *pf, *df; - PyObject *py_ldb; - struct ldb_context *ldb; - - if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &pf, &df)) - return NULL; - - PyErr_LDB_OR_RAISE(py_ldb, ldb); - - result = dsdb_attach_schema_from_ldif_file(ldb, pf, df); - PyErr_WERROR_IS_ERR_RAISE(result); - - return Py_None; -} - -static PyMethodDef py_misc_methods[] = { - { "generate_random_str", (PyCFunction)py_generate_random_str, METH_VARARGS, - "random_password(len) -> string\n" - "Generate random password with specified length." }, - { "unix2nttime", (PyCFunction)py_unix2nttime, METH_VARARGS, - "unix2nttime(timestamp) -> nttime" }, - { "ldb_set_credentials", (PyCFunction)py_ldb_set_credentials, METH_VARARGS, - "ldb_set_credentials(ldb, credentials) -> None\n" - "Set credentials to use when connecting." }, - { "ldb_set_session_info", (PyCFunction)py_ldb_set_session_info, METH_VARARGS, - "ldb_set_session_info(ldb, session_info)\n" - "Set session info to use when connecting." }, - { "ldb_set_loadparm", (PyCFunction)py_ldb_set_loadparm, METH_VARARGS, - "ldb_set_loadparm(ldb, session_info)\n" - "Set loadparm context to use when connecting." }, - { "samdb_set_domain_sid", (PyCFunction)py_samdb_set_domain_sid, METH_VARARGS, - "samdb_set_domain_sid(samdb, sid)\n" - "Set SID of domain to use." }, - { "ldb_register_samba_handlers", (PyCFunction)py_ldb_register_samba_handlers, METH_VARARGS, - "ldb_register_samba_handlers(ldb)\n" - "Register Samba-specific LDB modules and schemas." }, - { "dsdb_set_ntds_invocation_id", (PyCFunction)py_dsdb_set_ntds_invocation_id, METH_VARARGS, - NULL }, - { "dsdb_set_global_schema", (PyCFunction)py_dsdb_set_global_schema, METH_VARARGS, - NULL }, - { "dsdb_attach_schema_from_ldif_file", (PyCFunction)py_dsdb_attach_schema_from_ldif_file, METH_VARARGS, - NULL }, - { NULL } -}; - -void initmisc(void) -{ - PyObject *m; - - m = Py_InitModule3("misc", py_misc_methods, - "Python bindings for miscellaneous Samba functions."); - if (m == NULL) - return; - - PyModule_AddObject(m, "version", PyString_FromString(SAMBA_VERSION_STRING)); -} - diff --git a/source4/scripting/python/samba/__init__.py b/source4/scripting/python/samba/__init__.py index e191227108..e9fc26af20 100644 --- a/source4/scripting/python/samba/__init__.py +++ b/source4/scripting/python/samba/__init__.py @@ -43,7 +43,7 @@ else: import ldb import credentials -import misc +import glue class Ldb(ldb.Ldb): """Simple Samba-specific LDB subclass that takes care @@ -80,7 +80,7 @@ class Ldb(ldb.Ldb): if session_info is not None: self.set_session_info(session_info) - assert misc.ldb_register_samba_handlers(self) == 0 + glue.ldb_register_samba_handlers(self) if lp is not None: self.set_loadparm(lp) @@ -92,10 +92,14 @@ class Ldb(ldb.Ldb): if url is not None: self.connect(url) + def set_credentials(self, credentials): + glue.ldb_set_credentials(self, credentials) - set_credentials = misc.ldb_set_credentials - set_session_info = misc.ldb_set_session_info - set_loadparm = misc.ldb_set_loadparm + def set_session_info(self, session_info): + glue.ldb_set_session_info(self, session_info) + + def set_loadparm(self, lp_ctx): + glue.ldb_set_loadparm(self, lp_ctx) def searchone(self, attribute, basedn=None, expression=None, scope=ldb.SCOPE_BASE): @@ -235,4 +239,4 @@ def valid_netbios_name(name): return False return True -version = misc.version +version = glue.version diff --git a/source4/scripting/python/samba/idmap.py b/source4/scripting/python/samba/idmap.py index 755ec52c7b..f8eeb18925 100644 --- a/source4/scripting/python/samba/idmap.py +++ b/source4/scripting/python/samba/idmap.py @@ -23,7 +23,7 @@ __docformat__ = "restructuredText" import samba -import misc +import glue import ldb class IDmapDB(samba.Ldb): @@ -50,7 +50,7 @@ class IDmapDB(samba.Ldb): self.connect(lp.get("idmap database")) def connect(self, url): - super(IDmapDB, self).connect(misc.private_path(self.lp, url)) + super(IDmapDB, self).connect(self.lp.private_path(url)) def setup_name_mapping(self, sid, type, unixid): """Setup a mapping between a sam name and a unix name. diff --git a/source4/scripting/python/samba/provision.py b/source4/scripting/python/samba/provision.py index 568092926f..0819a0c8bf 100644 --- a/source4/scripting/python/samba/provision.py +++ b/source4/scripting/python/samba/provision.py @@ -29,7 +29,7 @@ import os import pwd import grp import time -import uuid, misc +import uuid, glue import socket import param import registry @@ -926,13 +926,13 @@ def provision(setup_dir, message, session_info, if policyguid is None: policyguid = str(uuid.uuid4()) if adminpass is None: - adminpass = misc.random_password(12) + adminpass = glue.generate_random_str(12) if krbtgtpass is None: - krbtgtpass = misc.random_password(12) + krbtgtpass = glue.generate_random_str(12) if machinepass is None: - machinepass = misc.random_password(12) + machinepass = glue.generate_random_str(12) if dnspass is None: - dnspass = misc.random_password(12) + dnspass = glue.generate_random_str(12) root_uid = findnss_uid([root or "root"]) nobody_uid = findnss_uid([nobody or "nobody"]) users_gid = findnss_gid([users or "users"]) @@ -1172,7 +1172,7 @@ def provision_backend(setup_dir=None, message=None, root = findnss(pwd.getpwnam, ["root"])[0] if adminpass is None: - adminpass = misc.random_password(12) + adminpass = glue.generate_random_str(12) if targetdir is not None: if (not os.path.exists(os.path.join(targetdir, "etc"))): diff --git a/source4/scripting/python/samba/samdb.py b/source4/scripting/python/samba/samdb.py index 4a64c2f76d..92b0bd7b89 100644 --- a/source4/scripting/python/samba/samdb.py +++ b/source4/scripting/python/samba/samdb.py @@ -23,7 +23,7 @@ """Convenience functions for using the SAM.""" import samba -import misc +import glue import ldb from samba.idmap import IDmapDB import pwd @@ -43,14 +43,14 @@ class SamDB(samba.Ldb): self.lp = lp super(SamDB, self).__init__(session_info=session_info, credentials=credentials, modules_dir=modules_dir, lp=lp) - assert misc.dsdb_set_global_schema(self) == 0 + glue.dsdb_set_global_schema(self) if url: self.connect(url) else: self.connect(lp.get("sam database")) def connect(self, url): - super(SamDB, self).connect(misc.private_path(self.lp, url)) + super(SamDB, self).connect(self.lp.private_path(url)) def add_foreign(self, domaindn, sid, desc): """Add a foreign security principle.""" @@ -182,17 +182,17 @@ userPassword: %s :param sid: The new domain sid to use. """ - misc.samdb_set_domain_sid(self, sid) + glue.samdb_set_domain_sid(self, sid) def attach_schema_from_ldif(self, pf, df): - misc.dsdb_attach_schema_from_ldif_file(self, pf, df) + glue.dsdb_attach_schema_from_ldif_file(self, pf, df) def set_invocation_id(self, invocation_id): """Set the invocation id for this SamDB handle. :param invocation_id: GUID of the invocation id. """ - misc.dsdb_set_ntds_invocation_id(self, invocation_id) + glue.dsdb_set_ntds_invocation_id(self, invocation_id) def setexpiry(self, user, expiry_seconds, noexpiry): """Set the password expiry for a user @@ -212,7 +212,7 @@ userPassword: %s accountExpires = 0 else: userAccountControl = userAccountControl & ~0x10000 - accountExpires = misc.unix2nttime(expiry_seconds + int(time.time())) + accountExpires = glue.unix2nttime(expiry_seconds + int(time.time())) mod = """ dn: %s -- cgit