s4: ran minimal_includes.pl on source4/auth/gensec
[ira/wip.git] / source4 / auth / gensec / pygensec.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009
4    
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.
9    
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.
14    
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/>.
17 */
18
19 #include "includes.h"
20 #include <Python.h>
21 #include "param/pyparam.h"
22 #include "auth/gensec/gensec.h"
23 #include "libcli/util/pyerrors.h"
24 #include "scripting/python/modules.h"
25 #include "pytalloc.h"
26 #include <tevent.h>
27
28 #ifndef Py_RETURN_NONE
29 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
30 #endif
31
32 static PyObject *py_get_name_by_authtype(PyObject *self, PyObject *args)
33 {
34         int type;
35         const char *name;
36         struct gensec_security *security;
37
38         if (!PyArg_ParseTuple(args, "i", &type))
39                 return NULL;
40
41         security = (struct gensec_security *)py_talloc_get_ptr(self);
42
43         name = gensec_get_name_by_authtype(security, type);
44         if (name == NULL)
45                 Py_RETURN_NONE;
46
47         return PyString_FromString(name);
48 }
49
50 static struct gensec_settings *settings_from_object(TALLOC_CTX *mem_ctx, PyObject *object)
51 {
52         struct gensec_settings *s;
53         PyObject *py_hostname, *py_lp_ctx;
54
55         if (!PyDict_Check(object)) {
56                 PyErr_SetString(PyExc_ValueError, "settings should be a dictionary");
57                 return NULL;
58         }
59
60         s = talloc_zero(mem_ctx, struct gensec_settings);
61         if (!s) return NULL;
62
63         py_hostname = PyDict_GetItemString(object, "target_hostname");
64         if (!py_hostname) {
65                 PyErr_SetString(PyExc_ValueError, "settings.target_hostname not found");
66                 return NULL;
67         }
68
69         py_lp_ctx = PyDict_GetItemString(object, "lp_ctx");
70         if (!py_lp_ctx) {
71                 PyErr_SetString(PyExc_ValueError, "settings.lp_ctx not found");
72                 return NULL;
73         }
74         
75         s->target_hostname = PyString_AsString(py_hostname);
76         s->lp_ctx = lp_from_py_object(py_lp_ctx);
77         s->iconv_convenience = py_iconv_convenience(s);
78         return s;
79 }
80
81 static PyObject *py_gensec_start_client(PyTypeObject *type, PyObject *args, PyObject *kwargs)
82 {
83         NTSTATUS status;
84         py_talloc_Object *self;
85         struct gensec_settings *settings;
86         const char *kwnames[] = { "settings", NULL };
87         PyObject *py_settings;
88         struct tevent_context *ev;
89
90         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", discard_const_p(char *, kwnames), &py_settings))
91                 return NULL;
92
93         self = (py_talloc_Object*)type->tp_alloc(type, 0);
94         if (self == NULL) {
95                 PyErr_NoMemory();
96                 return NULL;
97         }
98         self->talloc_ctx = talloc_new(NULL);
99         if (self->talloc_ctx == NULL) {
100                 PyErr_NoMemory();
101                 return NULL;
102         }
103
104         settings = settings_from_object(self->talloc_ctx, py_settings);
105         if (settings == NULL) {
106                 PyObject_DEL(self);
107                 return NULL;
108         }
109         
110         ev = tevent_context_init(self->talloc_ctx);
111         if (ev == NULL) {
112                 PyErr_NoMemory();
113                 PyObject_Del(self);
114                 return NULL;
115         }
116
117         status = gensec_init(settings->lp_ctx);
118         if (!NT_STATUS_IS_OK(status)) {
119                 PyErr_SetNTSTATUS(status);
120                 PyObject_DEL(self);
121                 return NULL;
122         }
123
124         status = gensec_client_start(self->talloc_ctx, 
125                 (struct gensec_security **)&self->ptr, ev, settings);
126         if (!NT_STATUS_IS_OK(status)) {
127                 PyErr_SetNTSTATUS(status);
128                 PyObject_DEL(self);
129                 return NULL;
130         }
131         return (PyObject *)self;
132 }
133
134 static PyObject *py_gensec_session_info(PyObject *self)
135 {
136         NTSTATUS status;
137         struct gensec_security *security = (struct gensec_security *)py_talloc_get_ptr(self);
138         struct auth_session_info *info;
139         if (security->ops == NULL) {
140                 PyErr_SetString(PyExc_ValueError, "gensec not fully initialised - ask Andrew");
141                 return NULL;
142         }
143         status = gensec_session_info(security, &info);
144         if (NT_STATUS_IS_ERR(status)) {
145                 PyErr_SetNTSTATUS(status);
146                 return NULL;
147         }
148
149         /* FIXME */
150         Py_RETURN_NONE;
151 }
152
153 static PyMethodDef py_gensec_security_methods[] = {
154         { "start_client", (PyCFunction)py_gensec_start_client, METH_VARARGS|METH_KEYWORDS|METH_CLASS, 
155                 "S.start_client(settings) -> gensec" },
156 /*      { "start_server", (PyCFunction)py_gensec_start_server, METH_VARARGS|METH_KEYWORDS|METH_CLASS, 
157                 "S.start_server(auth_ctx, settings) -> gensec" },*/
158         { "session_info", (PyCFunction)py_gensec_session_info, METH_NOARGS,
159                 "S.session_info() -> info" },
160         { "get_name_by_authtype", (PyCFunction)py_get_name_by_authtype, METH_VARARGS,
161                 "S.get_name_by_authtype(authtype) -> name\nLookup an auth type." },
162         { NULL }
163 };
164
165 static PyTypeObject Py_Security = {
166         .tp_name = "Security",
167         .tp_flags = Py_TPFLAGS_DEFAULT,
168         .tp_methods = py_gensec_security_methods,
169         .tp_basicsize = sizeof(py_talloc_Object),
170         .tp_dealloc = py_talloc_dealloc,
171 };
172
173 void initgensec(void)
174 {
175         PyObject *m;
176
177         if (PyType_Ready(&Py_Security) < 0)
178                 return;
179
180         m = Py_InitModule3("gensec", NULL, "Generic Security Interface.");
181         if (m == NULL)
182                 return;
183
184         Py_INCREF(&Py_Security);
185         PyModule_AddObject(m, "Security", (PyObject *)&Py_Security);
186 }