2563b85570b4ec50b9f0753dc3c3deb80a159230
[samba.git] / source4 / auth / pyauth.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
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/param.h"
22 #include "pyauth.h"
23 #include "auth/system_session_proto.h"
24 #include "param/pyparam.h"
25 #include "libcli/security/security.h"
26
27
28 PyTypeObject PyAuthSession = {
29         .tp_name = "AuthSession",
30         .tp_basicsize = sizeof(py_talloc_Object),
31         .tp_dealloc = py_talloc_dealloc,
32         .tp_flags = Py_TPFLAGS_DEFAULT,
33         .tp_repr = py_talloc_default_repr,
34 };
35
36 PyObject *PyAuthSession_FromSession(struct auth_session_info *session)
37 {
38         return py_talloc_reference(&PyAuthSession, session);
39 }
40
41 static PyObject *py_system_session(PyObject *module, PyObject *args)
42 {
43         PyObject *py_lp_ctx = Py_None;
44         struct loadparm_context *lp_ctx = NULL;
45         struct auth_session_info *session;
46         if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
47                 return NULL;
48
49         lp_ctx = lp_from_py_object(NULL, py_lp_ctx); /* FIXME: Leaks memory */
50         if (lp_ctx == NULL)
51                 return NULL;
52
53         session = system_session(lp_ctx);
54
55         return PyAuthSession_FromSession(session);
56 }
57
58
59 static PyObject *py_system_session_anon(PyObject *module, PyObject *args)
60 {
61         PyObject *py_lp_ctx = Py_None;
62         struct loadparm_context *lp_ctx;
63         struct auth_session_info *session;
64
65         if (!PyArg_ParseTuple(args, "|O", &py_lp_ctx))
66                 return NULL;
67
68         lp_ctx = lp_from_py_object(NULL, py_lp_ctx); /* FIXME: leaks memory */
69         if (lp_ctx == NULL)
70                 return NULL;
71
72         session = system_session_anon(NULL, lp_ctx);
73
74         return PyAuthSession_FromSession(session);
75 }
76
77 static PyObject *py_admin_session(PyObject *module, PyObject *args)
78 {
79         PyObject *py_lp_ctx;
80         PyObject *py_sid;
81         struct loadparm_context *lp_ctx = NULL;
82         struct auth_session_info *session;
83         struct dom_sid *domain_sid = NULL;
84         if (!PyArg_ParseTuple(args, "OO", &py_lp_ctx, &py_sid))
85                 return NULL;
86
87         lp_ctx = lp_from_py_object(NULL, py_lp_ctx); /* FIXME: leaky */
88         if (lp_ctx == NULL)
89                 return NULL;
90
91         domain_sid = dom_sid_parse_talloc(NULL, PyString_AsString(py_sid));
92         session = admin_session(NULL, lp_ctx, domain_sid);
93
94         return PyAuthSession_FromSession(session);
95 }
96
97 static PyMethodDef py_auth_methods[] = {
98         { "system_session", (PyCFunction)py_system_session, METH_VARARGS, NULL },
99         { "system_session_anonymous", (PyCFunction)py_system_session_anon, METH_VARARGS, NULL },
100         { "admin_session", (PyCFunction)py_admin_session, METH_VARARGS, NULL },
101         { NULL },
102 };
103
104 void initauth(void)
105 {
106         PyObject *m;
107
108         if (PyType_Ready(&PyAuthSession) < 0)
109                 return;
110
111         m = Py_InitModule3("auth", py_auth_methods,
112                                            "Authentication and authorization support.");
113         if (m == NULL)
114                 return;
115
116         Py_INCREF(&PyAuthSession);
117         PyModule_AddObject(m, "AuthSession", (PyObject *)&PyAuthSession);
118 }