s4-pygensec: a bit closer to working
authorAndrew Tridgell <tridge@samba.org>
Fri, 2 Oct 2009 08:50:59 +0000 (18:50 +1000)
committerAndrew Tridgell <tridge@samba.org>
Fri, 2 Oct 2009 08:50:59 +0000 (18:50 +1000)
I'll need help from Andrew on how to get gensec to initialise it's ops
element

source4/auth/gensec/config.mk
source4/auth/gensec/pygensec.c
source4/auth/gensec/tests/bindings.py

index 65f5208e1ab13dc334f6614162ea9d7ed858e45a..aa52b184fca3e82dfed7bc2f2a0284c6dd71784e 100644 (file)
@@ -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
index efa97e0184a914c094137c4580673b1b99a82315..d6d1f63ef1da5e88de74d56bc2f347032f3ecf32 100644 (file)
 #include "includes.h"
 #include <Python.h>
 #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 <tevent.h>
 
@@ -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);
index 95d7833e4a07cfcd47db2cd037feed3fd7d069e2..f88fa82ae1b84099d9b2606af6f28bada73811a6 100644 (file)
@@ -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())