483a50cfcd61f5f6b15a6f457682f6b97d2e84e6
[sfrench/samba-autobuild/.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 <Python.h>
20 #include "includes.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 "lib/talloc/pytalloc.h"
26 #include <tevent.h>
27 #include "librpc/rpc/pyrpc_util.h"
28
29 static PyObject *py_get_name_by_authtype(PyObject *self, PyObject *args)
30 {
31         int type;
32         const char *name;
33         struct gensec_security *security;
34
35         if (!PyArg_ParseTuple(args, "i", &type))
36                 return NULL;
37
38         security = py_talloc_get_type(self, struct gensec_security);
39
40         name = gensec_get_name_by_authtype(security, type);
41         if (name == NULL)
42                 Py_RETURN_NONE;
43
44         return PyString_FromString(name);
45 }
46
47 static struct gensec_settings *settings_from_object(TALLOC_CTX *mem_ctx, PyObject *object)
48 {
49         struct gensec_settings *s;
50         PyObject *py_hostname, *py_lp_ctx;
51
52         if (!PyDict_Check(object)) {
53                 PyErr_SetString(PyExc_ValueError, "settings should be a dictionary");
54                 return NULL;
55         }
56
57         s = talloc_zero(mem_ctx, struct gensec_settings);
58         if (!s) return NULL;
59
60         py_hostname = PyDict_GetItemString(object, "target_hostname");
61         if (!py_hostname) {
62                 PyErr_SetString(PyExc_ValueError, "settings.target_hostname not found");
63                 return NULL;
64         }
65
66         py_lp_ctx = PyDict_GetItemString(object, "lp_ctx");
67         if (!py_lp_ctx) {
68                 PyErr_SetString(PyExc_ValueError, "settings.lp_ctx not found");
69                 return NULL;
70         }
71
72         s->target_hostname = PyString_AsString(py_hostname);
73         s->lp_ctx = lpcfg_from_py_object(s, py_lp_ctx);
74         return s;
75 }
76
77 static PyObject *py_gensec_start_client(PyTypeObject *type, PyObject *args, PyObject *kwargs)
78 {
79         NTSTATUS status;
80         py_talloc_Object *self;
81         struct gensec_settings *settings;
82         const char *kwnames[] = { "settings", NULL };
83         PyObject *py_settings;
84         struct tevent_context *ev;
85         struct gensec_security *gensec;
86
87         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", discard_const_p(char *, kwnames), &py_settings))
88                 return NULL;
89
90         self = (py_talloc_Object*)type->tp_alloc(type, 0);
91         if (self == NULL) {
92                 PyErr_NoMemory();
93                 return NULL;
94         }
95         self->talloc_ctx = talloc_new(NULL);
96         if (self->talloc_ctx == NULL) {
97                 PyErr_NoMemory();
98                 return NULL;
99         }
100
101         settings = settings_from_object(self->talloc_ctx, py_settings);
102         if (settings == NULL) {
103                 PyObject_DEL(self);
104                 return NULL;
105         }
106
107         ev = tevent_context_init(self->talloc_ctx);
108         if (ev == NULL) {
109                 PyErr_NoMemory();
110                 PyObject_Del(self);
111                 return NULL;
112         }
113
114         status = gensec_init(settings->lp_ctx);
115         if (!NT_STATUS_IS_OK(status)) {
116                 PyErr_SetNTSTATUS(status);
117                 PyObject_DEL(self);
118                 return NULL;
119         }
120
121         status = gensec_client_start(self->talloc_ctx, &gensec, ev, settings);
122         if (!NT_STATUS_IS_OK(status)) {
123                 PyErr_SetNTSTATUS(status);
124                 PyObject_DEL(self);
125                 return NULL;
126         }
127
128         self->ptr = gensec;
129
130         return (PyObject *)self;
131 }
132
133 static PyObject *py_gensec_session_info(PyObject *self)
134 {
135         NTSTATUS status;
136         PyObject *py_session_info;
137         struct gensec_security *security = py_talloc_get_type(self, struct gensec_security);
138         struct auth_session_info *info;
139         if (security->ops == NULL) {
140                 PyErr_SetString(PyExc_RuntimeError, "no mechanism selected");
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         py_session_info = py_return_ndr_struct("samba.auth", "session_info",
150                                                  info, info);
151         return py_session_info;
152 }
153
154 static PyObject *py_gensec_start_mech_by_name(PyObject *self, PyObject *args)
155 {
156     char *name;
157     struct gensec_security *security = py_talloc_get_type(self, struct gensec_security);
158     NTSTATUS status;
159
160     if (!PyArg_ParseTuple(args, "s", &name))
161         return NULL;
162
163     status = gensec_start_mech_by_name(security, name);
164     if (!NT_STATUS_IS_OK(status)) {
165         PyErr_SetNTSTATUS(status);
166         return NULL;
167     }
168
169     Py_RETURN_NONE;
170 }
171
172 static PyObject *py_gensec_start_mech_by_authtype(PyObject *self, PyObject *args)
173 {
174         int authtype, level;
175         struct gensec_security *security = py_talloc_get_type(self, struct gensec_security);
176         NTSTATUS status;
177         if (!PyArg_ParseTuple(args, "ii", &authtype, &level))
178                 return NULL;
179
180         status = gensec_start_mech_by_authtype(security, authtype, level);
181         if (!NT_STATUS_IS_OK(status)) {
182                 PyErr_SetNTSTATUS(status);
183                 return NULL;
184         }
185
186         Py_RETURN_NONE;
187 }
188
189 static PyMethodDef py_gensec_security_methods[] = {
190         { "start_client", (PyCFunction)py_gensec_start_client, METH_VARARGS|METH_KEYWORDS|METH_CLASS, 
191                 "S.start_client(settings) -> gensec" },
192 /*      { "start_server", (PyCFunction)py_gensec_start_server, METH_VARARGS|METH_KEYWORDS|METH_CLASS, 
193                 "S.start_server(auth_ctx, settings) -> gensec" },*/
194         { "session_info", (PyCFunction)py_gensec_session_info, METH_NOARGS,
195                 "S.session_info() -> info" },
196     { "start_mech_by_name", (PyCFunction)py_gensec_start_mech_by_name, METH_VARARGS, 
197         "S.start_mech_by_name(name)" },
198         { "start_mech_by_authtype", (PyCFunction)py_gensec_start_mech_by_authtype, METH_VARARGS, "S.start_mech_by_authtype(authtype, level)" },
199         { "get_name_by_authtype", (PyCFunction)py_get_name_by_authtype, METH_VARARGS,
200                 "S.get_name_by_authtype(authtype) -> name\nLookup an auth type." },
201         { NULL }
202 };
203
204 static PyTypeObject Py_Security = {
205         .tp_name = "Security",
206         .tp_flags = Py_TPFLAGS_DEFAULT,
207         .tp_methods = py_gensec_security_methods,
208         .tp_basicsize = sizeof(py_talloc_Object),
209 };
210
211 void initgensec(void)
212 {
213         PyObject *m;
214
215         Py_Security.tp_base = PyTalloc_GetObjectType();
216         if (Py_Security.tp_base == NULL)
217                 return;
218
219         if (PyType_Ready(&Py_Security) < 0)
220                 return;
221
222         m = Py_InitModule3("gensec", NULL, "Generic Security Interface.");
223         if (m == NULL)
224                 return;
225
226         PyModule_AddObject(m, "FEATURE_SESSION_KEY",     PyInt_FromLong(GENSEC_FEATURE_SESSION_KEY));
227         PyModule_AddObject(m, "FEATURE_SIGN",            PyInt_FromLong(GENSEC_FEATURE_SIGN));
228         PyModule_AddObject(m, "FEATURE_SEAL",            PyInt_FromLong(GENSEC_FEATURE_SEAL));
229         PyModule_AddObject(m, "FEATURE_DCE_STYLE",       PyInt_FromLong(GENSEC_FEATURE_DCE_STYLE));
230         PyModule_AddObject(m, "FEATURE_ASYNC_REPLIES",   PyInt_FromLong(GENSEC_FEATURE_ASYNC_REPLIES));
231         PyModule_AddObject(m, "FEATURE_DATAGRAM_MODE",   PyInt_FromLong(GENSEC_FEATURE_DATAGRAM_MODE));
232         PyModule_AddObject(m, "FEATURE_SIGN_PKT_HEADER", PyInt_FromLong(GENSEC_FEATURE_SIGN_PKT_HEADER));
233         PyModule_AddObject(m, "FEATURE_NEW_SPNEGO",      PyInt_FromLong(GENSEC_FEATURE_NEW_SPNEGO));
234
235         Py_INCREF(&Py_Security);
236         PyModule_AddObject(m, "Security", (PyObject *)&Py_Security);
237 }