1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-25 17:57:42 +03:00

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 <lbalhar@redhat.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
This commit is contained in:
Lumir Balhar 2017-01-17 13:20:38 +01:00 committed by Andrew Bartlett
parent 0672fc145a
commit 64bc64ce64
4 changed files with 60 additions and 48 deletions

View File

@ -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

View File

@ -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()

View File

@ -17,6 +17,7 @@
*/
#include <Python.h>
#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;
}

View File

@ -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'
)