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