From 64bc64ce647164e3544bfdd231c3f2d1239d2ca3 Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Tue, 17 Jan 2017 13:20:38 +0100 Subject: [PATCH] python: samba.gensec: Port module to Python 3 compatible form Port samba.gensec and samba.tests.gensec modules to Python 3 compatible form, enable execution of tests with Python 3 and remove unused import of samba.gensec from samba.tests module __init__.py file. Signed-off-by: Lumir Balhar Reviewed-by: Andrew Bartlett Reviewed-by: Douglas Bagnall --- python/samba/tests/__init__.py | 1 - python/samba/tests/gensec.py | 36 +++++++++---------- source4/auth/gensec/pygensec.c | 57 ++++++++++++++++++------------- source4/auth/gensec/wscript_build | 14 +++++--- 4 files changed, 60 insertions(+), 48 deletions(-) diff --git a/python/samba/tests/__init__.py b/python/samba/tests/__init__.py index 21a55c24735..d6c8d863d2e 100644 --- a/python/samba/tests/__init__.py +++ b/python/samba/tests/__init__.py @@ -39,7 +39,6 @@ if not PY3: import samba.ndr import samba.dcerpc.dcerpc import samba.dcerpc.epmapper - from samba import gensec try: from unittest import SkipTest diff --git a/python/samba/tests/gensec.py b/python/samba/tests/gensec.py index e270c418ea6..368d406b6e3 100644 --- a/python/samba/tests/gensec.py +++ b/python/samba/tests/gensec.py @@ -66,26 +66,26 @@ class GensecTests(samba.tests.TestCase): client_finished = False server_finished = False - server_to_client = "" + server_to_client = b"" """Run the actual call loop""" while not client_finished and not server_finished: if not client_finished: - print "running client gensec_update" + print("running client gensec_update") (client_finished, client_to_server) = self.gensec_client.update(server_to_client) if not server_finished: - print "running server gensec_update" + print("running server gensec_update") (server_finished, server_to_client) = self.gensec_server.update(client_to_server) session_info = self.gensec_server.session_info() - test_string = "Hello Server" - test_wrapped = self.gensec_client.wrap(test_string) + test_bytes = b"Hello Server" + test_wrapped = self.gensec_client.wrap(test_bytes) test_unwrapped = self.gensec_server.unwrap(test_wrapped) - self.assertEqual(test_string, test_unwrapped) - test_string = "Hello Client" - test_wrapped = self.gensec_server.wrap(test_string) + self.assertEqual(test_bytes, test_unwrapped) + test_bytes = b"Hello Client" + test_wrapped = self.gensec_server.wrap(test_bytes) test_unwrapped = self.gensec_client.unwrap(test_wrapped) - self.assertEqual(test_string, test_unwrapped) + self.assertEqual(test_bytes, test_unwrapped) client_session_key = self.gensec_client.session_key() server_session_key = self.gensec_server.session_key() @@ -114,17 +114,17 @@ class GensecTests(samba.tests.TestCase): client_finished = False server_finished = False - server_to_client = "" + server_to_client = b"" """Run the actual call loop""" i = 0 while not client_finished or not server_finished: i += 1 if not client_finished: - print "running client gensec_update: %d: %r" % (len(server_to_client), server_to_client) + print("running client gensec_update: %d: %r" % (len(server_to_client), server_to_client)) (client_finished, client_to_server) = self.gensec_client.update(server_to_client) if not server_finished: - print "running server gensec_update: %d: %r" % (len(client_to_server), client_to_server) + print("running server gensec_update: %d: %r" % (len(client_to_server), client_to_server)) (server_finished, server_to_client) = self.gensec_server.update(client_to_server) """Here we expect a lot more than the typical 1 or 2 roundtrips""" @@ -132,14 +132,14 @@ class GensecTests(samba.tests.TestCase): session_info = self.gensec_server.session_info() - test_string = "Hello Server" - test_wrapped = self.gensec_client.wrap(test_string) + test_bytes = b"Hello Server" + test_wrapped = self.gensec_client.wrap(test_bytes) test_unwrapped = self.gensec_server.unwrap(test_wrapped) - self.assertEqual(test_string, test_unwrapped) - test_string = "Hello Client" - test_wrapped = self.gensec_server.wrap(test_string) + self.assertEqual(test_bytes, test_unwrapped) + test_bytes = b"Hello Client" + test_wrapped = self.gensec_server.wrap(test_bytes) test_unwrapped = self.gensec_client.unwrap(test_wrapped) - self.assertEqual(test_string, test_unwrapped) + self.assertEqual(test_bytes, test_unwrapped) client_session_key = self.gensec_client.session_key() server_session_key = self.gensec_server.session_key() diff --git a/source4/auth/gensec/pygensec.c b/source4/auth/gensec/pygensec.c index 56016af3127..946a0827eb8 100644 --- a/source4/auth/gensec/pygensec.c +++ b/source4/auth/gensec/pygensec.c @@ -17,6 +17,7 @@ */ #include +#include "python/py3compat.h" #include "includes.h" #include "param/pyparam.h" #include "auth/gensec/gensec.h" @@ -43,7 +44,7 @@ static PyObject *py_get_name_by_authtype(PyObject *self, PyObject *args) if (name == NULL) Py_RETURN_NONE; - return PyString_FromString(name); + return PyStr_FromString(name); } static struct gensec_settings *settings_from_object(TALLOC_CTX *mem_ctx, PyObject *object) @@ -71,7 +72,7 @@ static struct gensec_settings *settings_from_object(TALLOC_CTX *mem_ctx, PyObjec return NULL; } - s->target_hostname = PyString_AsString(py_hostname); + s->target_hostname = PyStr_AsString(py_hostname); s->lp_ctx = lpcfg_from_py_object(s, py_lp_ctx); return s; } @@ -313,7 +314,7 @@ static PyObject *py_gensec_session_key(PyObject *self) return NULL; } - session_key_obj = PyString_FromStringAndSize((const char *)session_key.data, + session_key_obj = PyBytes_FromStringAndSize((const char *)session_key.data, session_key.length); talloc_free(mem_ctx); return session_key_obj; @@ -433,14 +434,13 @@ static PyObject *py_gensec_update(PyObject *self, PyObject *args) return NULL; mem_ctx = talloc_new(NULL); - - if (!PyString_Check(py_in)) { - PyErr_Format(PyExc_TypeError, "expected a string"); + if (!PyBytes_Check(py_in)) { + PyErr_Format(PyExc_TypeError, "bytes expected"); return NULL; } - in.data = (uint8_t *)PyString_AsString(py_in); - in.length = PyString_Size(py_in); + in.data = (uint8_t *)PyBytes_AsString(py_in); + in.length = PyBytes_Size(py_in); status = gensec_update(security, mem_ctx, in, &out); @@ -450,7 +450,7 @@ static PyObject *py_gensec_update(PyObject *self, PyObject *args) talloc_free(mem_ctx); return NULL; } - ret = PyString_FromStringAndSize((const char *)out.data, out.length); + ret = PyBytes_FromStringAndSize((const char *)out.data, out.length); talloc_free(mem_ctx); if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) { @@ -476,12 +476,12 @@ static PyObject *py_gensec_wrap(PyObject *self, PyObject *args) mem_ctx = talloc_new(NULL); - if (!PyString_Check(py_in)) { - PyErr_Format(PyExc_TypeError, "expected a string"); + if (!PyBytes_Check(py_in)) { + PyErr_Format(PyExc_TypeError, "bytes expected"); return NULL; } - in.data = (uint8_t *)PyString_AsString(py_in); - in.length = PyString_Size(py_in); + in.data = (uint8_t *)PyBytes_AsString(py_in); + in.length = PyBytes_Size(py_in); status = gensec_wrap(security, mem_ctx, &in, &out); @@ -491,11 +491,12 @@ static PyObject *py_gensec_wrap(PyObject *self, PyObject *args) return NULL; } - ret = PyString_FromStringAndSize((const char *)out.data, out.length); + ret = PyBytes_FromStringAndSize((const char *)out.data, out.length); talloc_free(mem_ctx); return ret; } + static PyObject *py_gensec_unwrap(PyObject *self, PyObject *args) { NTSTATUS status; @@ -510,13 +511,13 @@ static PyObject *py_gensec_unwrap(PyObject *self, PyObject *args) mem_ctx = talloc_new(NULL); - if (!PyString_Check(py_in)) { - PyErr_Format(PyExc_TypeError, "expected a string"); + if (!PyBytes_Check(py_in)) { + PyErr_Format(PyExc_TypeError, "bytes expected"); return NULL; } - in.data = (uint8_t *)PyString_AsString(py_in); - in.length = PyString_Size(py_in); + in.data = (uint8_t *)PyBytes_AsString(py_in); + in.length = PyBytes_Size(py_in); status = gensec_unwrap(security, mem_ctx, &in, &out); @@ -526,7 +527,7 @@ static PyObject *py_gensec_unwrap(PyObject *self, PyObject *args) return NULL; } - ret = PyString_FromStringAndSize((const char *)out.data, out.length); + ret = PyBytes_FromStringAndSize((const char *)out.data, out.length); talloc_free(mem_ctx); return ret; } @@ -654,23 +655,29 @@ static PyMethodDef py_gensec_security_methods[] = { { NULL } }; +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + .m_name = "gensec", + .m_doc = "Generic Security Interface.", + .m_size = -1, +}; + static PyTypeObject Py_Security = { .tp_name = "gensec.Security", .tp_flags = Py_TPFLAGS_DEFAULT, .tp_methods = py_gensec_security_methods, }; -void initgensec(void); -void initgensec(void) +MODULE_INIT_FUNC(gensec) { PyObject *m; if (pytalloc_BaseObject_PyType_Ready(&Py_Security) < 0) - return; + return NULL; - m = Py_InitModule3("gensec", NULL, "Generic Security Interface."); + m = PyModule_Create(&moduledef); if (m == NULL) - return; + return NULL; PyModule_AddObject(m, "FEATURE_SESSION_KEY", PyInt_FromLong(GENSEC_FEATURE_SESSION_KEY)); PyModule_AddObject(m, "FEATURE_SIGN", PyInt_FromLong(GENSEC_FEATURE_SIGN)); @@ -683,4 +690,6 @@ void initgensec(void) Py_INCREF(&Py_Security); PyModule_AddObject(m, "Security", (PyObject *)&Py_Security); + + return m; } diff --git a/source4/auth/gensec/wscript_build b/source4/auth/gensec/wscript_build index 098826af876..f9889d278a3 100644 --- a/source4/auth/gensec/wscript_build +++ b/source4/auth/gensec/wscript_build @@ -26,9 +26,13 @@ bld.SAMBA_MODULE('gensec_gssapi', deps='gssapi samba-credentials authkrb5 com_err' ) -bld.SAMBA_PYTHON('pygensec', - source='pygensec.c', - deps='gensec pytalloc-util pyparam_util', - realname='samba/gensec.so' - ) +for env in bld.gen_python_environments(): + pytalloc_util = bld.pyembed_libname('pytalloc-util') + pyparam_util = bld.pyembed_libname('pyparam_util') + + bld.SAMBA_PYTHON('pygensec', + source='pygensec.c', + deps='gensec %s %s' % (pytalloc_util, pyparam_util), + realname='samba/gensec.so' + ) -- 2.47.3