pygensec: Add initial work on a gensec Python module.
[Samba/eduardoll.git] / source4 / auth / gensec / pygensec.c
blobefa97e0184a914c094137c4580673b1b99a82315
1 /*
2 Unix SMB/CIFS implementation.
3 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "includes.h"
20 #include <Python.h>
21 #include "param/param.h"
22 #include "auth/gensec/gensec.h"
23 #include "libcli/util/pyerrors.h"
24 #include "pytalloc.h"
25 #include <tevent.h>
27 #ifndef Py_RETURN_NONE
28 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
29 #endif
31 static PyObject *py_get_name_by_authtype(PyObject *self, PyObject *args)
33 int type;
34 const char *name;
35 struct gensec_security *security;
37 if (!PyArg_ParseTuple(args, "i", &type))
38 return NULL;
40 security = (struct gensec_security *)py_talloc_get_ptr(self);
42 name = gensec_get_name_by_authtype(security, type);
43 if (name == NULL)
44 Py_RETURN_NONE;
46 return PyString_FromString(name);
49 static struct gensec_settings *settings_from_object(PyObject *object)
51 return NULL; /* FIXME */
54 static PyObject *py_gensec_start_client(PyTypeObject *type, PyObject *args, PyObject *kwargs)
56 NTSTATUS status;
57 py_talloc_Object *self;
58 struct gensec_settings *settings;
59 const char *kwnames[] = { "settings", NULL };
60 PyObject *py_settings;
61 struct tevent_context *ev;
63 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwnames, &py_settings))
64 return NULL;
66 settings = settings_from_object(py_settings);
67 if (settings == NULL)
68 return NULL;
70 self = (py_talloc_Object*)type->tp_alloc(type, 0);
71 if (self == NULL) {
72 PyErr_NoMemory();
73 return NULL;
75 self->talloc_ctx = talloc_new(NULL);
76 if (self->talloc_ctx == NULL) {
77 PyErr_NoMemory();
78 return NULL;
80 ev = tevent_context_init(self->talloc_ctx);
81 if (ev == NULL) {
82 PyErr_NoMemory();
83 PyObject_Del(self);
84 return NULL;
86 status = gensec_client_start(self->talloc_ctx,
87 (struct gensec_security **)&self->ptr, ev, settings);
88 if (!NT_STATUS_IS_OK(status)) {
89 PyErr_SetNTSTATUS(status);
90 PyObject_DEL(self);
91 return NULL;
93 return (PyObject *)self;
96 static PyObject *py_gensec_session_info(PyObject *self)
98 NTSTATUS status;
99 struct gensec_security *security = (struct gensec_security *)py_talloc_get_ptr(self);
100 struct auth_session_info *info;
101 status = gensec_session_info(security, &info);
102 if (NT_STATUS_IS_ERR(status)) {
103 PyErr_SetNTSTATUS(status);
104 return NULL;
107 /* FIXME */
108 Py_RETURN_NONE;
111 static PyMethodDef py_gensec_security_methods[] = {
112 { "start_client", (PyCFunction)py_gensec_start_client, METH_VARARGS|METH_KEYWORDS|METH_CLASS,
113 "S.start_client(settings) -> gensec" },
114 /* { "start_server", (PyCFunction)py_gensec_start_server, METH_VARARGS|METH_KEYWORDS|METH_CLASS,
115 "S.start_server(auth_ctx, settings) -> gensec" },*/
116 { "session_info", (PyCFunction)py_gensec_session_info, METH_NOARGS,
117 "S.session_info() -> info" },
118 { "get_name_by_authtype", (PyCFunction)py_get_name_by_authtype, METH_VARARGS,
119 "S.get_name_by_authtype(authtype) -> name\nLookup an auth type." },
120 { NULL }
123 static PyTypeObject Py_Security = {
124 .tp_name = "Security",
125 .tp_flags = Py_TPFLAGS_DEFAULT,
126 .tp_methods = py_gensec_security_methods,
127 .tp_basicsize = sizeof(py_talloc_Object),
128 .tp_dealloc = py_talloc_dealloc,
131 void initgensec(void)
133 PyObject *m;
135 if (PyType_Ready(&Py_Security) < 0)
136 return;
138 m = Py_InitModule3("gensec", NULL, "Generic Security Interface.");
139 if (m == NULL)
140 return;
142 Py_INCREF(&Py_Security);
143 PyModule_AddObject(m, "Security", (PyObject *)&Py_Security);