From: Lumir Balhar Date: Tue, 20 Dec 2016 09:57:13 +0000 (+0100) Subject: python: samba.auth: Port samba.auth to Python 3 compatible form X-Git-Tag: tdb-1.3.13~562 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1ac5bf21a1efdc74d377feff74f15021c69e009e;p=thirdparty%2Fsamba.git python: samba.auth: Port samba.auth to Python 3 compatible form Port samba.auth Python module to Python 3 compatible form and enable tests execution with Python 3. Signed-off-by: Lumir Balhar Reviewed-by: Andrew Bartlett Reviewed-by: Douglas Bagnall --- diff --git a/selftest/tests.py b/selftest/tests.py index 150948325b3..b3027c41a24 100644 --- a/selftest/tests.py +++ b/selftest/tests.py @@ -54,7 +54,7 @@ planpythontestsuite("none", "samba.tests.blackbox.ndrdump") planpythontestsuite("none", "api", name="ldb.python", extra_path=['lib/ldb/tests/python']) planpythontestsuite("none", "samba.tests.credentials", py3_compatible=True) planpythontestsuite("none", "samba.tests.registry") -planpythontestsuite("none", "samba.tests.auth") +planpythontestsuite("none", "samba.tests.auth", py3_compatible=True) planpythontestsuite("none", "samba.tests.get_opt") planpythontestsuite("none", "samba.tests.security") planpythontestsuite("none", "samba.tests.dcerpc.misc", py3_compatible=True) diff --git a/source4/auth/pyauth.c b/source4/auth/pyauth.c index 61db907b902..2d827604262 100644 --- a/source4/auth/pyauth.c +++ b/source4/auth/pyauth.c @@ -18,6 +18,7 @@ */ #include +#include "python/py3compat.h" #include "includes.h" #include "libcli/util/pyerrors.h" #include "param/param.h" @@ -32,9 +33,7 @@ #include "librpc/rpc/pyrpc_util.h" #include "lib/events/events.h" -void initauth(void); - -staticforward PyTypeObject PyAuthContext; +static PyTypeObject PyAuthContext; static PyObject *PyAuthSession_FromSession(struct auth_session_info *session) { @@ -73,13 +72,13 @@ static PyObject *py_system_session(PyObject *module, PyObject *args) static PyObject *py_admin_session(PyObject *module, PyObject *args) { PyObject *py_lp_ctx; - PyObject *py_sid; + const char *sid; struct loadparm_context *lp_ctx = NULL; struct auth_session_info *session; struct dom_sid *domain_sid = NULL; TALLOC_CTX *mem_ctx; - if (!PyArg_ParseTuple(args, "OO", &py_lp_ctx, &py_sid)) + if (!PyArg_ParseTuple(args, "Os", &py_lp_ctx, &sid)) return NULL; mem_ctx = talloc_new(NULL); @@ -94,10 +93,9 @@ static PyObject *py_admin_session(PyObject *module, PyObject *args) return NULL; } - domain_sid = dom_sid_parse_talloc(mem_ctx, PyString_AsString(py_sid)); + domain_sid = dom_sid_parse_talloc(mem_ctx, sid); if (domain_sid == NULL) { - PyErr_Format(PyExc_RuntimeError, "Unable to parse sid %s", - PyString_AsString(py_sid)); + PyErr_Format(PyExc_RuntimeError, "Unable to parse sid %s", sid); talloc_free(mem_ctx); return NULL; } @@ -183,13 +181,19 @@ static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, } for (i = 0; i < PyList_Size(list); i++) { + const char *value; + Py_ssize_t size; PyObject *item = PyList_GetItem(list, i); - if (!PyString_Check(item)) { + if (!PyStr_Check(item)) { PyErr_Format(PyExc_TypeError, "%s should be strings", paramname); return NULL; } - ret[i] = talloc_strndup(ret, PyString_AsString(item), - PyString_Size(item)); + value = PyStr_AsUTF8AndSize(item, &size); + if (value == NULL) { + talloc_free(ret); + return NULL; + } + ret[i] = talloc_strndup(ret, value, size); } ret[i] = NULL; return ret; @@ -303,24 +307,32 @@ static PyMethodDef py_auth_methods[] = { { NULL }, }; -void initauth(void) +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + .m_name = "auth", + .m_doc = "Authentication and authorization support.", + .m_size = -1, + .m_methods = py_auth_methods, +}; + +MODULE_INIT_FUNC(auth) { PyObject *m; if (pytalloc_BaseObject_PyType_Ready(&PyAuthContext) < 0) - return; + return NULL; - m = Py_InitModule3("auth", py_auth_methods, - "Authentication and authorization support."); + m = PyModule_Create(&moduledef); if (m == NULL) - return; + return NULL; Py_INCREF(&PyAuthContext); PyModule_AddObject(m, "AuthContext", (PyObject *)&PyAuthContext); -#define ADD_FLAG(val) PyModule_AddObject(m, #val, PyInt_FromLong(val)) +#define ADD_FLAG(val) PyModule_AddIntConstant(m, #val, val) ADD_FLAG(AUTH_SESSION_INFO_DEFAULT_GROUPS); ADD_FLAG(AUTH_SESSION_INFO_AUTHENTICATED); ADD_FLAG(AUTH_SESSION_INFO_SIMPLE_PRIVILEGES); + return m; } diff --git a/source4/auth/wscript_build b/source4/auth/wscript_build index 6742537294f..5786fa5a11a 100644 --- a/source4/auth/wscript_build +++ b/source4/auth/wscript_build @@ -43,10 +43,14 @@ bld.SAMBA_SUBSYSTEM('auth4_sam', ) -bld.SAMBA_PYTHON('pyauth', - source='pyauth.c', - public_deps='auth_system_session', - deps='samdb pytalloc-util pyparam_util pyldb-util pycredentials auth4', - realname='samba/auth.so' - ) +for env in bld.gen_python_environments(): + pytalloc_util = bld.pyembed_libname('pytalloc-util') + pyparam_util = bld.pyembed_libname('pyparam_util') + pyldb_util = bld.pyembed_libname('pyldb-util') + bld.SAMBA_PYTHON('pyauth', + source='pyauth.c', + public_deps='auth_system_session', + deps='samdb %s %s %s pycredentials auth4' % (pytalloc_util, pyparam_util, pyldb_util), + realname='samba/auth.so' + )