From 3fea9df85a7d489b81e21a7a63f9e115de712d4c Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Wed, 22 Sep 2010 15:35:36 -0700 Subject: s4-param: Check type when converting python object to lp_ctx, fix some memory leaks. --- source4/param/provision.c | 9 +++++++-- source4/param/pyparam_util.c | 24 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) (limited to 'source4/param') diff --git a/source4/param/provision.c b/source4/param/provision.c index b0387869b5..593f9ff168 100644 --- a/source4/param/provision.c +++ b/source4/param/provision.c @@ -85,7 +85,7 @@ NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct provision_result *result) { const char *configfile; - PyObject *provision_mod, *provision_dict, *provision_fn, *py_result, *parameters; + PyObject *provision_mod, *provision_dict, *provision_fn, *py_result, *parameters, *py_lp_ctx; DEBUG(0,("Provision for Become-DC test using python\n")); @@ -193,7 +193,12 @@ NTSTATUS provision_bare(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, result->domaindn = talloc_strdup(mem_ctx, PyString_AsString(PyObject_GetAttrString(py_result, "domaindn"))); /* FIXME paths */ - result->lp_ctx = lpcfg_from_py_object(result, PyObject_GetAttrString(py_result, "lp")); + py_lp_ctx = PyObject_GetAttrString(py_result, "lp"); + if (py_lp_ctx == NULL) { + DEBUG(0, ("Missing 'lp' attribute")); + return NT_STATUS_UNSUCCESSFUL; + } + result->lp_ctx = lpcfg_from_py_object(result, py_lp_ctx); result->samdb = PyLdb_AsLdbContext(PyObject_GetAttrString(py_result, "samdb")); return NT_STATUS_OK; diff --git a/source4/param/pyparam_util.c b/source4/param/pyparam_util.c index 8c98cbcbfe..474dd31504 100644 --- a/source4/param/pyparam_util.c +++ b/source4/param/pyparam_util.c @@ -28,6 +28,9 @@ _PUBLIC_ struct loadparm_context *lpcfg_from_py_object(TALLOC_CTX *mem_ctx, PyObject *py_obj) { struct loadparm_context *lp_ctx; + PyObject *param_mod; + PyTypeObject *lp_type; + bool is_lpobj; if (PyString_Check(py_obj)) { lp_ctx = loadparm_init(mem_ctx); @@ -47,7 +50,26 @@ _PUBLIC_ struct loadparm_context *lpcfg_from_py_object(TALLOC_CTX *mem_ctx, PyOb return lp_ctx; } - return PyLoadparmContext_AsLoadparmContext(py_obj); + param_mod = PyImport_ImportModule("samba.param"); + if (param_mod == NULL) { + return NULL; + } + + lp_type = (PyTypeObject *)PyObject_GetAttrString(param_mod, "LoadParm"); + Py_DECREF(param_mod); + if (lp_type == NULL) { + PyErr_SetString(PyExc_RuntimeError, "Unable to import LoadParm"); + return NULL; + } + + is_lpobj = PyObject_TypeCheck(py_obj, lp_type); + Py_DECREF(lp_type); + if (is_lpobj) { + return talloc_reference(mem_ctx, PyLoadparmContext_AsLoadparmContext(py_obj)); + } + + PyErr_SetNone(PyExc_TypeError); + return NULL; } struct loadparm_context *py_default_loadparm_context(TALLOC_CTX *mem_ctx) -- cgit