pyldb: Prevent segfault when first module is NULL
[Samba.git] / source4 / auth / pyauth.c
blobd79d41782d9528457576b490ec90867b63c68731
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
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.
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.
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/>.
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"
35 void initauth(void);
37 staticforward PyTypeObject PyAuthContext;
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
46 static PyObject *PyAuthSession_FromSession(struct auth_session_info *session)
48 return py_return_ndr_struct("samba.dcerpc.auth", "session_info", session, session);
51 static PyObject *py_system_session(PyObject *module, PyObject *args)
53 PyObject *py_lp_ctx = Py_None;
54 struct loadparm_context *lp_ctx = NULL;
55 struct auth_session_info *session;
56 TALLOC_CTX *mem_ctx;
57 if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
58 return NULL;
60 mem_ctx = talloc_new(NULL);
61 if (mem_ctx == NULL) {
62 PyErr_NoMemory();
63 return NULL;
66 lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
67 if (lp_ctx == NULL) {
68 talloc_free(mem_ctx);
69 return NULL;
72 session = system_session(lp_ctx);
74 talloc_free(mem_ctx);
76 return PyAuthSession_FromSession(session);
80 static PyObject *py_admin_session(PyObject *module, PyObject *args)
82 PyObject *py_lp_ctx;
83 PyObject *py_sid;
84 struct loadparm_context *lp_ctx = NULL;
85 struct auth_session_info *session;
86 struct dom_sid *domain_sid = NULL;
87 TALLOC_CTX *mem_ctx;
89 if (!PyArg_ParseTuple(args, "OO", &py_lp_ctx, &py_sid))
90 return NULL;
92 mem_ctx = talloc_new(NULL);
93 if (mem_ctx == NULL) {
94 PyErr_NoMemory();
95 return NULL;
98 lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
99 if (lp_ctx == NULL) {
100 talloc_free(mem_ctx);
101 return NULL;
104 domain_sid = dom_sid_parse_talloc(mem_ctx, PyString_AsString(py_sid));
105 if (domain_sid == NULL) {
106 PyErr_Format(PyExc_RuntimeError, "Unable to parse sid %s",
107 PyString_AsString(py_sid));
108 talloc_free(mem_ctx);
109 return NULL;
111 session = admin_session(NULL, lp_ctx, domain_sid);
112 talloc_free(mem_ctx);
114 return PyAuthSession_FromSession(session);
117 static PyObject *py_user_session(PyObject *module, PyObject *args, PyObject *kwargs)
119 NTSTATUS nt_status;
120 struct auth_session_info *session;
121 TALLOC_CTX *mem_ctx;
122 const char * const kwnames[] = { "ldb", "lp_ctx", "principal", "dn", "session_info_flags", NULL };
123 struct ldb_context *ldb_ctx;
124 PyObject *py_ldb = Py_None;
125 PyObject *py_dn = Py_None;
126 PyObject *py_lp_ctx = Py_None;
127 struct loadparm_context *lp_ctx = NULL;
128 struct ldb_dn *user_dn;
129 char *principal = NULL;
130 int session_info_flags = 0; /* This is an int, because that's
131 * what we need for the python
132 * PyArg_ParseTupleAndKeywords */
134 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OzOi",
135 discard_const_p(char *, kwnames),
136 &py_ldb, &py_lp_ctx, &principal, &py_dn, &session_info_flags)) {
137 return NULL;
140 mem_ctx = talloc_new(NULL);
141 if (mem_ctx == NULL) {
142 PyErr_NoMemory();
143 return NULL;
146 ldb_ctx = pyldb_Ldb_AsLdbContext(py_ldb);
148 if (py_dn == Py_None) {
149 user_dn = NULL;
150 } else {
151 if (!pyldb_Object_AsDn(ldb_ctx, py_dn, ldb_ctx, &user_dn)) {
152 talloc_free(mem_ctx);
153 return NULL;
157 lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
158 if (lp_ctx == NULL) {
159 talloc_free(mem_ctx);
160 return NULL;
163 nt_status = authsam_get_session_info_principal(mem_ctx, lp_ctx, ldb_ctx, principal, user_dn,
164 session_info_flags, &session);
165 if (!NT_STATUS_IS_OK(nt_status)) {
166 talloc_free(mem_ctx);
167 PyErr_NTSTATUS_IS_ERR_RAISE(nt_status);
170 talloc_steal(NULL, session);
171 talloc_free(mem_ctx);
173 return PyAuthSession_FromSession(session);
177 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list,
178 const char *paramname)
180 const char **ret;
181 Py_ssize_t i;
182 if (!PyList_Check(list)) {
183 PyErr_Format(PyExc_TypeError, "%s is not a list", paramname);
184 return NULL;
186 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
187 if (ret == NULL) {
188 PyErr_NoMemory();
189 return NULL;
192 for (i = 0; i < PyList_Size(list); i++) {
193 PyObject *item = PyList_GetItem(list, i);
194 if (!PyString_Check(item)) {
195 PyErr_Format(PyExc_TypeError, "%s should be strings", paramname);
196 return NULL;
198 ret[i] = talloc_strndup(ret, PyString_AsString(item),
199 PyString_Size(item));
201 ret[i] = NULL;
202 return ret;
205 static PyObject *PyAuthContext_FromContext(struct auth4_context *auth_context)
207 return pytalloc_reference(&PyAuthContext, auth_context);
210 static PyObject *py_auth_context_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
212 PyObject *py_lp_ctx = Py_None;
213 PyObject *py_ldb = Py_None;
214 PyObject *py_imessaging_ctx = Py_None;
215 PyObject *py_auth_context = Py_None;
216 PyObject *py_methods = Py_None;
217 TALLOC_CTX *mem_ctx;
218 struct auth4_context *auth_context;
219 struct imessaging_context *imessaging_context = NULL;
220 struct loadparm_context *lp_ctx;
221 struct tevent_context *ev;
222 struct ldb_context *ldb = NULL;
223 NTSTATUS nt_status;
224 const char **methods;
226 const char * const kwnames[] = { "lp_ctx", "messaging_ctx", "ldb", "methods", NULL };
228 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOOO",
229 discard_const_p(char *, kwnames),
230 &py_lp_ctx, &py_imessaging_ctx, &py_ldb, &py_methods))
231 return NULL;
233 mem_ctx = talloc_new(NULL);
234 if (mem_ctx == NULL) {
235 PyErr_NoMemory();
236 return NULL;
239 if (py_ldb != Py_None) {
240 ldb = pyldb_Ldb_AsLdbContext(py_ldb);
243 lp_ctx = lpcfg_from_py_object(mem_ctx, py_lp_ctx);
244 if (lp_ctx == NULL) {
245 PyErr_NoMemory();
246 return NULL;
249 ev = s4_event_context_init(mem_ctx);
250 if (ev == NULL) {
251 PyErr_NoMemory();
252 return NULL;
255 if (py_imessaging_ctx != Py_None) {
256 imessaging_context = pytalloc_get_type(py_imessaging_ctx, struct imessaging_context);
259 if (py_methods == Py_None && py_ldb == Py_None) {
260 nt_status = auth_context_create(mem_ctx, ev, imessaging_context, lp_ctx, &auth_context);
261 } else {
262 if (py_methods != Py_None) {
263 methods = PyList_AsStringList(mem_ctx, py_methods, "methods");
264 if (methods == NULL) {
265 talloc_free(mem_ctx);
266 return NULL;
268 } else {
269 methods = auth_methods_from_lp(mem_ctx, lp_ctx);
271 nt_status = auth_context_create_methods(mem_ctx, methods, ev,
272 imessaging_context, lp_ctx,
273 ldb, &auth_context);
276 if (!NT_STATUS_IS_OK(nt_status)) {
277 talloc_free(mem_ctx);
278 PyErr_NTSTATUS_IS_ERR_RAISE(nt_status);
281 if (!talloc_reference(auth_context, lp_ctx)) {
282 talloc_free(mem_ctx);
283 PyErr_NoMemory();
284 return NULL;
287 if (!talloc_reference(auth_context, ev)) {
288 talloc_free(mem_ctx);
289 PyErr_NoMemory();
290 return NULL;
293 py_auth_context = PyAuthContext_FromContext(auth_context);
295 talloc_free(mem_ctx);
297 return py_auth_context;
300 static PyTypeObject PyAuthContext = {
301 .tp_name = "AuthContext",
302 .tp_basicsize = sizeof(pytalloc_Object),
303 .tp_flags = Py_TPFLAGS_DEFAULT,
304 .tp_new = py_auth_context_new,
307 static PyMethodDef py_auth_methods[] = {
308 { "system_session", (PyCFunction)py_system_session, METH_VARARGS, NULL },
309 { "admin_session", (PyCFunction)py_admin_session, METH_VARARGS, NULL },
310 { "user_session", (PyCFunction)py_user_session, METH_VARARGS|METH_KEYWORDS, NULL },
311 { NULL },
314 void initauth(void)
316 PyObject *m;
318 PyAuthContext.tp_base = pytalloc_GetObjectType();
319 if (PyAuthContext.tp_base == NULL)
320 return;
322 if (PyType_Ready(&PyAuthContext) < 0)
323 return;
325 m = Py_InitModule3("auth", py_auth_methods,
326 "Authentication and authorization support.");
327 if (m == NULL)
328 return;
330 Py_INCREF(&PyAuthContext);
331 PyModule_AddObject(m, "AuthContext", (PyObject *)&PyAuthContext);
333 #define ADD_FLAG(val) PyModule_AddObject(m, #val, PyInt_FromLong(val))
334 ADD_FLAG(AUTH_SESSION_INFO_DEFAULT_GROUPS);
335 ADD_FLAG(AUTH_SESSION_INFO_AUTHENTICATED);
336 ADD_FLAG(AUTH_SESSION_INFO_SIMPLE_PRIVILEGES);