437e671bba280e7c0966533361481fb1574a76c7
[obnox/samba/samba-obnox.git] / source4 / auth / pyauth.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2011
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <Python.h>
21 #include "includes.h"
22 #include "libcli/util/pyerrors.h"
23 #include "param/param.h"
24 #include "pyauth.h"
25 #include "pyldb.h"
26 #include "auth/system_session_proto.h"
27 #include "auth/auth.h"
28 #include "param/pyparam.h"
29 #include "libcli/security/security.h"
30 #include "auth/credentials/pycredentials.h"
31 #include <tevent.h>
32 #include "librpc/rpc/pyrpc_util.h"
33 #include "lib/events/events.h"
34
35 void initauth(void);
36
37 staticforward PyTypeObject PyAuthContext;
38
39 /* There's no Py_ssize_t in 2.4, apparently */
40 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
41 typedef int Py_ssize_t;
42 typedef inquiry lenfunc;
43 typedef intargfunc ssizeargfunc;
44 #endif
45
46 #ifndef Py_RETURN_NONE
47 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
48 #endif
49
50 static PyObject *PyAuthSession_FromSession(struct auth_session_info *session)
51 {
52         return py_return_ndr_struct("samba.dcerpc.auth", "session_info", session, session);
53 }
54
55 static PyObject *py_system_session(PyObject *module, PyObject *args)
56 {
57         PyObject *py_lp_ctx = Py_None;
58         struct loadparm_context *lp_ctx = NULL;
59         struct auth_session_info *session;
60         TALLOC_CTX *mem_ctx;
61         if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
62                 return NULL;
63
64         mem_ctx = talloc_new(NULL);
65         if (mem_ctx == NULL) {
66                 PyErr_NoMemory();
67                 return NULL;
68         }
69
70         lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
71         if (lp_ctx == NULL) {
72                 talloc_free(mem_ctx);
73                 return NULL;
74         }
75
76         session = system_session(lp_ctx);
77
78         talloc_free(mem_ctx);
79
80         return PyAuthSession_FromSession(session);
81 }
82
83
84 static PyObject *py_admin_session(PyObject *module, PyObject *args)
85 {
86         PyObject *py_lp_ctx;
87         PyObject *py_sid;
88         struct loadparm_context *lp_ctx = NULL;
89         struct auth_session_info *session;
90         struct dom_sid *domain_sid = NULL;
91         TALLOC_CTX *mem_ctx;
92
93         if (!PyArg_ParseTuple(args, "OO", &py_lp_ctx, &py_sid))
94                 return NULL;
95
96         mem_ctx = talloc_new(NULL);
97         if (mem_ctx == NULL) {
98                 PyErr_NoMemory();
99                 return NULL;
100         }
101
102         lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
103         if (lp_ctx == NULL) {
104                 talloc_free(mem_ctx);
105                 return NULL;
106         }
107
108         domain_sid = dom_sid_parse_talloc(mem_ctx, PyString_AsString(py_sid));
109         if (domain_sid == NULL) {
110                 PyErr_Format(PyExc_RuntimeError, "Unable to parse sid %s", 
111                                          PyString_AsString(py_sid));
112                 talloc_free(mem_ctx);
113                 return NULL;
114         }
115         session = admin_session(NULL, lp_ctx, domain_sid);
116         talloc_free(mem_ctx);
117
118         return PyAuthSession_FromSession(session);
119 }
120
121 static PyObject *py_user_session(PyObject *module, PyObject *args, PyObject *kwargs)
122 {
123         NTSTATUS nt_status;
124         struct auth_session_info *session;
125         TALLOC_CTX *mem_ctx;
126         const char * const kwnames[] = { "ldb", "lp_ctx", "principal", "dn", "session_info_flags", NULL };
127         struct ldb_context *ldb_ctx;
128         PyObject *py_ldb = Py_None;
129         PyObject *py_dn = Py_None;
130         PyObject *py_lp_ctx = Py_None;
131         struct loadparm_context *lp_ctx = NULL;
132         struct ldb_dn *user_dn;
133         char *principal = NULL;
134         int session_info_flags = 0; /* This is an int, because that's
135                                  * what we need for the python
136                                  * PyArg_ParseTupleAndKeywords */
137
138         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OzOi",
139                                          discard_const_p(char *, kwnames),
140                                          &py_ldb, &py_lp_ctx, &principal, &py_dn, &session_info_flags)) {
141                 return NULL;
142         }
143
144         mem_ctx = talloc_new(NULL);
145         if (mem_ctx == NULL) {
146                 PyErr_NoMemory();
147                 return NULL;
148         }
149
150         ldb_ctx = pyldb_Ldb_AsLdbContext(py_ldb);
151
152         if (py_dn == Py_None) {
153                 user_dn = NULL;
154         } else {
155                 if (!pyldb_Object_AsDn(ldb_ctx, py_dn, ldb_ctx, &user_dn)) {
156                         talloc_free(mem_ctx);
157                         return NULL;
158                 }
159         }
160
161         lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
162         if (lp_ctx == NULL) {
163                 talloc_free(mem_ctx);
164                 return NULL;
165         }
166
167         nt_status = authsam_get_session_info_principal(mem_ctx, lp_ctx, ldb_ctx, principal, user_dn,
168                                                        session_info_flags, &session);
169         if (!NT_STATUS_IS_OK(nt_status)) {
170                 talloc_free(mem_ctx);
171                 PyErr_NTSTATUS_IS_ERR_RAISE(nt_status);
172         }
173
174         talloc_steal(NULL, session);
175         talloc_free(mem_ctx);
176
177         return PyAuthSession_FromSession(session);
178 }
179
180
181 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, 
182                                         const char *paramname)
183 {
184         const char **ret;
185         Py_ssize_t i;
186         if (!PyList_Check(list)) {
187                 PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
188                 return NULL;
189         }
190         ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
191         if (ret == NULL) {
192                 PyErr_NoMemory();
193                 return NULL;
194         }
195
196         for (i = 0; i < PyList_Size(list); i++) {
197                 PyObject *item = PyList_GetItem(list, i);
198                 if (!PyString_Check(item)) {
199                         PyErr_Format(PyExc_TypeError, "%s should be strings", paramname);
200                         return NULL;
201                 }
202                 ret[i] = talloc_strndup(ret, PyString_AsString(item),
203                                         PyString_Size(item));
204         }
205         ret[i] = NULL;
206         return ret;
207 }
208
209 static PyObject *PyAuthContext_FromContext(struct auth4_context *auth_context)
210 {
211         return pytalloc_reference(&PyAuthContext, auth_context);
212 }
213
214 static PyObject *py_auth_context_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
215 {
216         PyObject *py_lp_ctx = Py_None;
217         PyObject *py_ldb = Py_None;
218         PyObject *py_imessaging_ctx = Py_None;
219         PyObject *py_auth_context = Py_None;
220         PyObject *py_methods = Py_None;
221         TALLOC_CTX *mem_ctx;
222         struct auth4_context *auth_context;
223         struct imessaging_context *imessaging_context = NULL;
224         struct loadparm_context *lp_ctx;
225         struct tevent_context *ev;
226         struct ldb_context *ldb = NULL;
227         NTSTATUS nt_status;
228         const char **methods;
229
230         const char * const kwnames[] = { "lp_ctx", "messaging_ctx", "ldb", "methods", NULL };
231
232         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOOO",
233                                          discard_const_p(char *, kwnames),
234                                          &py_lp_ctx, &py_imessaging_ctx, &py_ldb, &py_methods))
235                 return NULL;
236
237         mem_ctx = talloc_new(NULL);
238         if (mem_ctx == NULL) {
239                 PyErr_NoMemory();
240                 return NULL;
241         }
242
243         if (py_ldb != Py_None) {
244                 ldb = pyldb_Ldb_AsLdbContext(py_ldb);
245         }
246
247         lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
248         if (lp_ctx == NULL) {
249                 PyErr_NoMemory();
250                 return NULL;
251         }
252
253         ev = s4_event_context_init(mem_ctx);
254         if (ev == NULL) {
255                 PyErr_NoMemory();
256                 return NULL;
257         }
258
259         if (py_imessaging_ctx != Py_None) {
260                 imessaging_context = pytalloc_get_type(py_imessaging_ctx, struct imessaging_context);
261         }
262
263         if (py_methods == Py_None && py_ldb == Py_None) {
264                 nt_status = auth_context_create(mem_ctx, ev, imessaging_context, lp_ctx, &auth_context);
265         } else {
266                 if (py_methods != Py_None) {
267                         methods = PyList_AsStringList(mem_ctx, py_methods, "methods");
268                         if (methods == NULL) {
269                                 talloc_free(mem_ctx);
270                                 return NULL;
271                         }
272                 } else {
273                         methods = auth_methods_from_lp(mem_ctx, lp_ctx);
274                 }
275                 nt_status = auth_context_create_methods(mem_ctx, methods, ev, 
276                                                         imessaging_context, lp_ctx,
277                                                         ldb, &auth_context);
278         }
279
280         if (!NT_STATUS_IS_OK(nt_status)) {
281                 talloc_free(mem_ctx);
282                 PyErr_NTSTATUS_IS_ERR_RAISE(nt_status);
283         }
284
285         if (!talloc_reference(auth_context, lp_ctx)) {
286                 talloc_free(mem_ctx);
287                 PyErr_NoMemory();
288                 return NULL;
289         }
290
291         if (!talloc_reference(auth_context, ev)) {
292                 talloc_free(mem_ctx);
293                 PyErr_NoMemory();
294                 return NULL;
295         }
296
297         py_auth_context = PyAuthContext_FromContext(auth_context);
298
299         talloc_free(mem_ctx);
300
301         return py_auth_context;
302 }
303
304 static PyTypeObject PyAuthContext = {
305         .tp_name = "AuthContext",
306         .tp_basicsize = sizeof(pytalloc_Object),
307         .tp_flags = Py_TPFLAGS_DEFAULT,
308         .tp_new = py_auth_context_new,
309 };
310
311 static PyMethodDef py_auth_methods[] = {
312         { "system_session", (PyCFunction)py_system_session, METH_VARARGS, NULL },
313         { "admin_session", (PyCFunction)py_admin_session, METH_VARARGS, NULL },
314         { "user_session", (PyCFunction)py_user_session, METH_VARARGS|METH_KEYWORDS, NULL },
315         { NULL },
316 };
317
318 void initauth(void)
319 {
320         PyObject *m;
321
322         PyAuthContext.tp_base = pytalloc_GetObjectType();
323         if (PyAuthContext.tp_base == NULL)
324                 return;
325
326         if (PyType_Ready(&PyAuthContext) < 0)
327                 return;
328
329         m = Py_InitModule3("auth", py_auth_methods,
330                                            "Authentication and authorization support.");
331         if (m == NULL)
332                 return;
333
334         Py_INCREF(&PyAuthContext);
335         PyModule_AddObject(m, "AuthContext", (PyObject *)&PyAuthContext);
336
337 #define ADD_FLAG(val)  PyModule_AddObject(m, #val, PyInt_FromLong(val))
338         ADD_FLAG(AUTH_SESSION_INFO_DEFAULT_GROUPS);
339         ADD_FLAG(AUTH_SESSION_INFO_AUTHENTICATED);
340         ADD_FLAG(AUTH_SESSION_INFO_SIMPLE_PRIVILEGES);
341
342 }