python: samba.auth: Port samba.auth to Python 3 compatible form
authorLumir Balhar <lbalhar@redhat.com>
Tue, 20 Dec 2016 09:57:13 +0000 (10:57 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 10 Mar 2017 06:31:11 +0000 (07:31 +0100)
Port samba.auth Python module to Python 3 compatible form and
enable tests execution with Python 3.

Signed-off-by: Lumir Balhar <lbalhar@redhat.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
selftest/tests.py
source4/auth/pyauth.c
source4/auth/wscript_build

index 150948325b37f743aeeb3f3122cc152091902bf9..b3027c41a248bbca508fa5a78be5238369b4ea3f 100644 (file)
@@ -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)
index 61db907b90260917f05a26e91ad350d92134f2ed..2d827604262ddf57c5482de10dbde9482232eddb 100644 (file)
@@ -18,6 +18,7 @@
 */
 
 #include <Python.h>
+#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;
 }
index 6742537294f648452fee01717a8c159cf25479b4..5786fa5a11aef36e580b07f7649c74d47fd391c5 100644 (file)
@@ -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'
+               )