s4:python Add bindings to set GENSEC flags on credentials in python
[idra/samba.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 "pytalloc.h"
26 #include <tevent.h>
27
28 static PyObject *py_get_name_by_authtype(PyObject *self, PyObject *args)
29 {
30         int type;
31         const char *name;
32         struct gensec_security *security;
33
34         if (!PyArg_ParseTuple(args, "i", &type))
35                 return NULL;
36
37         security = (struct gensec_security *)py_talloc_get_ptr(self);
38
39         name = gensec_get_name_by_authtype(security, type);
40         if (name == NULL)
41                 Py_RETURN_NONE;
42
43         return PyString_FromString(name);
44 }
45
46 static struct gensec_settings *settings_from_object(TALLOC_CTX *mem_ctx, PyObject *object)
47 {
48         struct gensec_settings *s;
49         PyObject *py_hostname, *py_lp_ctx;
50
51         if (!PyDict_Check(object)) {
52                 PyErr_SetString(PyExc_ValueError, "settings should be a dictionary");
53                 return NULL;
54         }
55
56         s = talloc_zero(mem_ctx, struct gensec_settings);
57         if (!s) return NULL;
58
59         py_hostname = PyDict_GetItemString(object, "target_hostname");
60         if (!py_hostname) {
61                 PyErr_SetString(PyExc_ValueError, "settings.target_hostname not found");
62                 return NULL;
63         }
64
65         py_lp_ctx = PyDict_GetItemString(object, "lp_ctx");
66         if (!py_lp_ctx) {
67                 PyErr_SetString(PyExc_ValueError, "settings.lp_ctx not found");
68                 return NULL;
69         }
70         
71         s->target_hostname = PyString_AsString(py_hostname);
72         s->lp_ctx = lp_from_py_object(py_lp_ctx);
73         s->iconv_convenience = py_iconv_convenience(s);
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
86         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", discard_const_p(char *, kwnames), &py_settings))
87                 return NULL;
88
89         self = (py_talloc_Object*)type->tp_alloc(type, 0);
90         if (self == NULL) {
91                 PyErr_NoMemory();
92                 return NULL;
93         }
94         self->talloc_ctx = talloc_new(NULL);
95         if (self->talloc_ctx == NULL) {
96                 PyErr_NoMemory();
97                 return NULL;
98         }
99
100         settings = settings_from_object(self->talloc_ctx, py_settings);
101         if (settings == NULL) {
102                 PyObject_DEL(self);
103                 return NULL;
104         }
105         
106         ev = tevent_context_init(self->talloc_ctx);
107         if (ev == NULL) {
108                 PyErr_NoMemory();
109                 PyObject_Del(self);
110                 return NULL;
111         }
112
113         status = gensec_init(settings->lp_ctx);
114         if (!NT_STATUS_IS_OK(status)) {
115                 PyErr_SetNTSTATUS(status);
116                 PyObject_DEL(self);
117                 return NULL;
118         }
119
120         status = gensec_client_start(self->talloc_ctx, 
121                 (struct gensec_security **)&self->ptr, ev, settings);
122         if (!NT_STATUS_IS_OK(status)) {
123                 PyErr_SetNTSTATUS(status);
124                 PyObject_DEL(self);
125                 return NULL;
126         }
127         return (PyObject *)self;
128 }
129
130 static PyObject *py_gensec_session_info(PyObject *self)
131 {
132         NTSTATUS status;
133         struct gensec_security *security = (struct gensec_security *)py_talloc_get_ptr(self);
134         struct auth_session_info *info;
135         if (security->ops == NULL) {
136                 PyErr_SetString(PyExc_ValueError, "gensec not fully initialised - ask Andrew");
137                 return NULL;
138         }
139         status = gensec_session_info(security, &info);
140         if (NT_STATUS_IS_ERR(status)) {
141                 PyErr_SetNTSTATUS(status);
142                 return NULL;
143         }
144
145         /* FIXME */
146         Py_RETURN_NONE;
147 }
148
149 static PyMethodDef py_gensec_security_methods[] = {
150         { "start_client", (PyCFunction)py_gensec_start_client, METH_VARARGS|METH_KEYWORDS|METH_CLASS, 
151                 "S.start_client(settings) -> gensec" },
152 /*      { "start_server", (PyCFunction)py_gensec_start_server, METH_VARARGS|METH_KEYWORDS|METH_CLASS, 
153                 "S.start_server(auth_ctx, settings) -> gensec" },*/
154         { "session_info", (PyCFunction)py_gensec_session_info, METH_NOARGS,
155                 "S.session_info() -> info" },
156         { "get_name_by_authtype", (PyCFunction)py_get_name_by_authtype, METH_VARARGS,
157                 "S.get_name_by_authtype(authtype) -> name\nLookup an auth type." },
158         { NULL }
159 };
160
161 static PyTypeObject Py_Security = {
162         .tp_name = "Security",
163         .tp_flags = Py_TPFLAGS_DEFAULT,
164         .tp_methods = py_gensec_security_methods,
165         .tp_basicsize = sizeof(py_talloc_Object),
166         .tp_dealloc = py_talloc_dealloc,
167 };
168
169 void initgensec(void)
170 {
171         PyObject *m;
172
173         if (PyType_Ready(&Py_Security) < 0)
174                 return;
175
176         m = Py_InitModule3("gensec", NULL, "Generic Security Interface.");
177         if (m == NULL)
178                 return;
179
180         PyModule_AddObject(m, "FEATURE_SESSION_KEY",     PyInt_FromLong(GENSEC_FEATURE_SESSION_KEY));
181         PyModule_AddObject(m, "FEATURE_SIGN",            PyInt_FromLong(GENSEC_FEATURE_SIGN));
182         PyModule_AddObject(m, "FEATURE_SEAL",            PyInt_FromLong(GENSEC_FEATURE_SEAL));
183         PyModule_AddObject(m, "FEATURE_DCE_STYLE",       PyInt_FromLong(GENSEC_FEATURE_DCE_STYLE));
184         PyModule_AddObject(m, "FEATURE_ASYNC_REPLIES",   PyInt_FromLong(GENSEC_FEATURE_ASYNC_REPLIES));
185         PyModule_AddObject(m, "FEATURE_DATAGRAM_MODE",   PyInt_FromLong(GENSEC_FEATURE_DATAGRAM_MODE));
186         PyModule_AddObject(m, "FEATURE_SIGN_PKT_HEADER", PyInt_FromLong(GENSEC_FEATURE_SIGN_PKT_HEADER));
187         PyModule_AddObject(m, "FEATURE_NEW_SPNEGO",      PyInt_FromLong(GENSEC_FEATURE_NEW_SPNEGO));
188
189         Py_INCREF(&Py_Security);
190         PyModule_AddObject(m, "Security", (PyObject *)&Py_Security);
191 }