From 5a5c30cb38c7e08fbe1abbc98496142ac022e7f8 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 2 Oct 2009 18:50:59 +1000 Subject: s4-pygensec: a bit closer to working I'll need help from Andrew on how to get gensec to initialise it's ops element --- source4/auth/gensec/config.mk | 2 +- source4/auth/gensec/pygensec.c | 57 ++++++++++++++++++++++++++++++----- source4/auth/gensec/tests/bindings.py | 6 +++- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/source4/auth/gensec/config.mk b/source4/auth/gensec/config.mk index 65f5208e1a..aa52b184fc 100644 --- a/source4/auth/gensec/config.mk +++ b/source4/auth/gensec/config.mk @@ -86,7 +86,7 @@ SCHANNELDB_OBJ_FILES = $(addprefix $(gensecsrcdir)/, schannel_state.o) $(eval $(call proto_header_template,$(gensecsrcdir)/schannel_state.h,$(SCHANNELDB_OBJ_FILES:.o=.c))) [PYTHON::pygensec] -PRIVATE_DEPENDENCIES = gensec PYTALLOC +PRIVATE_DEPENDENCIES = gensec PYTALLOC pyparam_util LIBRARY_REALNAME = samba/gensec.$(SHLIBEXT) pygensec_OBJ_FILES = $(gensecsrcdir)/pygensec.o diff --git a/source4/auth/gensec/pygensec.c b/source4/auth/gensec/pygensec.c index efa97e0184..d6d1f63ef1 100644 --- a/source4/auth/gensec/pygensec.c +++ b/source4/auth/gensec/pygensec.c @@ -19,8 +19,10 @@ #include "includes.h" #include #include "param/param.h" +#include "param/pyparam.h" #include "auth/gensec/gensec.h" #include "libcli/util/pyerrors.h" +#include "scripting/python/modules.h" #include "pytalloc.h" #include @@ -46,9 +48,35 @@ static PyObject *py_get_name_by_authtype(PyObject *self, PyObject *args) return PyString_FromString(name); } -static struct gensec_settings *settings_from_object(PyObject *object) +static struct gensec_settings *settings_from_object(TALLOC_CTX *mem_ctx, PyObject *object) { - return NULL; /* FIXME */ + struct gensec_settings *s; + PyObject *py_hostname, *py_lp_ctx; + + if (!PyDict_Check(object)) { + PyErr_SetString(PyExc_ValueError, "settings should be a dictionary"); + return NULL; + } + + s = talloc_zero(mem_ctx, struct gensec_settings); + if (!s) return NULL; + + py_hostname = PyDict_GetItemString(object, "target_hostname"); + if (!py_hostname) { + PyErr_SetString(PyExc_ValueError, "settings.target_hostname not found"); + return NULL; + } + + py_lp_ctx = PyDict_GetItemString(object, "lp_ctx"); + if (!py_lp_ctx) { + PyErr_SetString(PyExc_ValueError, "settings.lp_ctx not found"); + return NULL; + } + + s->target_hostname = PyString_AsString(py_hostname); + s->lp_ctx = lp_from_py_object(py_lp_ctx); + s->iconv_convenience = py_iconv_convenience(s); + return s; } static PyObject *py_gensec_start_client(PyTypeObject *type, PyObject *args, PyObject *kwargs) @@ -60,13 +88,9 @@ static PyObject *py_gensec_start_client(PyTypeObject *type, PyObject *args, PyOb PyObject *py_settings; struct tevent_context *ev; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwnames, &py_settings)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", discard_const_p(char *, kwnames), &py_settings)) return NULL; - settings = settings_from_object(py_settings); - if (settings == NULL) - return NULL; - self = (py_talloc_Object*)type->tp_alloc(type, 0); if (self == NULL) { PyErr_NoMemory(); @@ -77,12 +101,27 @@ static PyObject *py_gensec_start_client(PyTypeObject *type, PyObject *args, PyOb PyErr_NoMemory(); return NULL; } + + settings = settings_from_object(self->talloc_ctx, py_settings); + if (settings == NULL) { + PyObject_DEL(self); + return NULL; + } + ev = tevent_context_init(self->talloc_ctx); if (ev == NULL) { PyErr_NoMemory(); PyObject_Del(self); return NULL; } + + status = gensec_init(settings->lp_ctx); + if (!NT_STATUS_IS_OK(status)) { + PyErr_SetNTSTATUS(status); + PyObject_DEL(self); + return NULL; + } + status = gensec_client_start(self->talloc_ctx, (struct gensec_security **)&self->ptr, ev, settings); if (!NT_STATUS_IS_OK(status)) { @@ -98,6 +137,10 @@ static PyObject *py_gensec_session_info(PyObject *self) NTSTATUS status; struct gensec_security *security = (struct gensec_security *)py_talloc_get_ptr(self); struct auth_session_info *info; + if (security->ops == NULL) { + PyErr_SetString(PyExc_ValueError, "gensec not fully initialised - ask Andrew"); + return NULL; + } status = gensec_session_info(security, &info); if (NT_STATUS_IS_ERR(status)) { PyErr_SetNTSTATUS(status); diff --git a/source4/auth/gensec/tests/bindings.py b/source4/auth/gensec/tests/bindings.py index 95d7833e4a..f88fa82ae1 100644 --- a/source4/auth/gensec/tests/bindings.py +++ b/source4/auth/gensec/tests/bindings.py @@ -25,11 +25,15 @@ the functionality, that's already done in other tests. import unittest from samba import gensec +from samba.tests import cmdline_loadparm class CredentialsTests(unittest.TestCase): def setUp(self): - self.gensec = gensec.Security.start_client() + settings = {} + settings["target_hostname"] = "localhost" + settings["lp_ctx"] = cmdline_loadparm + self.gensec = gensec.Security.start_client(settings) def test_info(self): self.assertEquals(None, self.gensec.session_info()) -- cgit