5311ce18f4e0b2b5e43332ecbbed4df58b363bbb
[jra/samba/.git] / source4 / librpc / ndr / py_security.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
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 #include "../lib/util/python_util.h"
20 #include "libcli/security/security.h"
21
22 static void PyType_AddMethods(PyTypeObject *type, PyMethodDef *methods)
23 {
24         PyObject *dict;
25         int i;
26         if (type->tp_dict == NULL)
27                 type->tp_dict = PyDict_New();
28         dict = type->tp_dict;
29         for (i = 0; methods[i].ml_name; i++) {
30                 PyObject *descr = PyDescr_NewMethod(type, &methods[i]);
31                 PyDict_SetItemString(dict, methods[i].ml_name, 
32                                      descr);
33         }
34 }
35
36 static int py_dom_sid_cmp(PyObject *self, PyObject *py_other)
37 {
38         struct dom_sid *this = py_talloc_get_ptr(self), *other;
39         other = py_talloc_get_ptr(py_other);
40         if (other == NULL)
41                 return -1;
42
43         return dom_sid_compare(this, other);
44 }
45
46 static PyObject *py_dom_sid_str(PyObject *self)
47 {
48         struct dom_sid *this = py_talloc_get_ptr(self);
49         char *str = dom_sid_string(NULL, this);
50         PyObject *ret = PyString_FromString(str);
51         talloc_free(str);
52         return ret;
53 }
54
55 static PyObject *py_dom_sid_repr(PyObject *self)
56 {
57         struct dom_sid *this = py_talloc_get_ptr(self);
58         char *str = dom_sid_string(NULL, this);
59         PyObject *ret = PyString_FromFormat("dom_sid('%s')", str);
60         talloc_free(str);
61         return ret;
62 }
63
64 static int py_dom_sid_init(PyObject *self, PyObject *args, PyObject *kwargs)
65 {
66         char *str = NULL;
67         struct dom_sid *sid = py_talloc_get_ptr(self);
68         const char *kwnames[] = { "str", NULL };
69
70         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &str))
71                 return -1;
72
73         if (str != NULL && !dom_sid_parse(str, sid)) {
74                 PyErr_SetString(PyExc_TypeError, "Unable to parse string");
75                 return -1;
76         }
77
78         return 0;
79 }
80
81 static void py_dom_sid_patch(PyTypeObject *type)
82 {
83         type->tp_init = py_dom_sid_init;
84         type->tp_str = py_dom_sid_str;
85         type->tp_repr = py_dom_sid_repr;
86         type->tp_compare = py_dom_sid_cmp;
87 }
88
89 #define PY_DOM_SID_PATCH py_dom_sid_patch
90
91 static PyObject *py_descriptor_sacl_add(PyObject *self, PyObject *args)
92 {
93         struct security_descriptor *desc = py_talloc_get_ptr(self);
94         NTSTATUS status;
95         struct security_ace *ace;
96         PyObject *py_ace;
97
98         if (!PyArg_ParseTuple(args, "O", &py_ace))
99                 return NULL;
100
101         ace = py_talloc_get_ptr(py_ace);
102         status = security_descriptor_sacl_add(desc, ace);
103         PyErr_NTSTATUS_IS_ERR_RAISE(status);
104         Py_RETURN_NONE;
105 }
106
107 static PyObject *py_descriptor_dacl_add(PyObject *self, PyObject *args)
108 {
109         struct security_descriptor *desc = py_talloc_get_ptr(self);
110         NTSTATUS status;
111         struct security_ace *ace;
112         PyObject *py_ace;
113
114         if (!PyArg_ParseTuple(args, "O", &py_ace))
115                 return NULL;
116
117         ace = py_talloc_get_ptr(py_ace);
118
119         status = security_descriptor_dacl_add(desc, ace);
120         PyErr_NTSTATUS_IS_ERR_RAISE(status);
121         Py_RETURN_NONE;
122 }
123
124 static PyObject *py_descriptor_dacl_del(PyObject *self, PyObject *args)
125 {
126         struct security_descriptor *desc = py_talloc_get_ptr(self);
127         NTSTATUS status;
128         struct dom_sid *sid;
129         PyObject *py_sid;
130
131         if (!PyArg_ParseTuple(args, "O", &py_sid))
132                 return NULL;
133
134         sid = py_talloc_get_ptr(py_sid);
135         status = security_descriptor_dacl_del(desc, sid);
136         PyErr_NTSTATUS_IS_ERR_RAISE(status);
137         Py_RETURN_NONE;
138 }
139
140 static PyObject *py_descriptor_sacl_del(PyObject *self, PyObject *args)
141 {
142         struct security_descriptor *desc = py_talloc_get_ptr(self);
143         NTSTATUS status;
144         struct dom_sid *sid;
145         PyObject *py_sid;
146
147         if (!PyArg_ParseTuple(args, "O", &py_sid))
148                 return NULL;
149
150         sid = py_talloc_get_ptr(py_sid);
151         status = security_descriptor_sacl_del(desc, sid);
152         PyErr_NTSTATUS_IS_ERR_RAISE(status);
153         Py_RETURN_NONE;
154 }
155
156 static PyObject *py_descriptor_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
157 {
158         return py_talloc_import(self, security_descriptor_initialise(NULL));
159 }       
160
161 static PyMethodDef py_descriptor_extra_methods[] = {
162         { "sacl_add", (PyCFunction)py_descriptor_sacl_add, METH_VARARGS,
163                 "S.sacl_add(ace) -> None\n"
164                 "Add a security ace to this security descriptor" },
165         { "dacl_add", (PyCFunction)py_descriptor_dacl_add, METH_VARARGS,
166                 NULL },
167         { "dacl_del", (PyCFunction)py_descriptor_dacl_del, METH_VARARGS,
168                 NULL },
169         { "sacl_del", (PyCFunction)py_descriptor_sacl_del, METH_VARARGS,
170                 NULL },
171         { NULL }
172 };
173
174 static void py_descriptor_patch(PyTypeObject *type)
175 {
176         type->tp_new = py_descriptor_new;
177         PyType_AddMethods(type, py_descriptor_extra_methods);
178 }
179
180 #define PY_DESCRIPTOR_PATCH py_descriptor_patch
181
182 static PyObject *py_token_is_sid(PyObject *self, PyObject *args)
183 {
184         PyObject *py_sid;
185         struct dom_sid *sid;
186         struct security_token *token = py_talloc_get_ptr(self);
187         if (!PyArg_ParseTuple(args, "O", &py_sid))
188                 return NULL;
189
190         sid = py_talloc_get_ptr(py_sid);
191
192         return PyBool_FromLong(security_token_is_sid(token, sid));
193 }
194
195 static PyObject *py_token_has_sid(PyObject *self, PyObject *args)
196 {
197         PyObject *py_sid;
198         struct dom_sid *sid;
199         struct security_token *token = py_talloc_get_ptr(self);
200         if (!PyArg_ParseTuple(args, "O", &py_sid))
201                 return NULL;
202
203         sid = py_talloc_get_ptr(py_sid);
204
205         return PyBool_FromLong(security_token_has_sid(token, sid));
206 }
207
208 static PyObject *py_token_is_anonymous(PyObject *self)
209 {
210         struct security_token *token = py_talloc_get_ptr(self);
211         
212         return PyBool_FromLong(security_token_is_anonymous(token));
213 }
214
215 static PyObject *py_token_is_system(PyObject *self)
216 {
217         struct security_token *token = py_talloc_get_ptr(self);
218         
219         return PyBool_FromLong(security_token_is_system(token));
220 }
221
222 static PyObject *py_token_has_builtin_administrators(PyObject *self)
223 {
224         struct security_token *token = py_talloc_get_ptr(self);
225         
226         return PyBool_FromLong(security_token_has_builtin_administrators(token));
227 }
228
229 static PyObject *py_token_has_nt_authenticated_users(PyObject *self)
230 {
231         struct security_token *token = py_talloc_get_ptr(self);
232         
233         return PyBool_FromLong(security_token_has_nt_authenticated_users(token));
234 }
235
236 static PyObject *py_token_has_privilege(PyObject *self, PyObject *args)
237 {
238         int priv;
239         struct security_token *token = py_talloc_get_ptr(self);
240
241         if (!PyArg_ParseTuple(args, "i", &priv))
242                 return NULL;
243
244         return PyBool_FromLong(security_token_has_privilege(token, priv));
245 }
246
247 static PyObject *py_token_set_privilege(PyObject *self, PyObject *args)
248 {
249         int priv;
250         struct security_token *token = py_talloc_get_ptr(self);
251
252         if (!PyArg_ParseTuple(args, "i", &priv))
253                 return NULL;
254
255         security_token_set_privilege(token, priv);
256         Py_RETURN_NONE;
257 }
258
259 static PyObject *py_token_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
260 {
261         return py_talloc_import(self, security_token_initialise(NULL));
262 }       
263
264 static PyMethodDef py_token_extra_methods[] = {
265         { "is_sid", (PyCFunction)py_token_is_sid, METH_VARARGS,
266                 "S.is_sid(sid) -> bool\n"
267                 "Check whether this token is of the specified SID." },
268         { "has_sid", (PyCFunction)py_token_has_sid, METH_VARARGS,
269                 NULL },
270         { "is_anonymous", (PyCFunction)py_token_is_anonymous, METH_NOARGS,
271                 "S.is_anonymus() -> bool\n"
272                 "Check whether this is an anonymous token." },
273         { "is_system", (PyCFunction)py_token_is_system, METH_NOARGS,
274                 NULL },
275         { "has_builtin_administrators", (PyCFunction)py_token_has_builtin_administrators, METH_NOARGS,
276                 NULL },
277         { "has_nt_authenticated_users", (PyCFunction)py_token_has_nt_authenticated_users, METH_NOARGS,
278                 NULL },
279         { "has_privilege", (PyCFunction)py_token_has_privilege, METH_VARARGS,
280                 NULL },
281         { "set_privilege", (PyCFunction)py_token_set_privilege, METH_VARARGS,
282                 NULL },
283         { NULL }
284 };
285
286 #define PY_TOKEN_PATCH py_token_patch
287 static void py_token_patch(PyTypeObject *type)
288 {
289         type->tp_new = py_token_new;
290         PyType_AddMethods(type, py_token_extra_methods);
291 }
292
293 static PyObject *py_privilege_name(PyObject *self, PyObject *args)
294 {
295         int priv;
296         if (!PyArg_ParseTuple(args, "i", &priv))
297                 return NULL;
298
299         return PyString_FromString(sec_privilege_name(priv));
300 }
301
302 static PyObject *py_privilege_id(PyObject *self, PyObject *args)
303 {
304         char *name;
305
306         if (!PyArg_ParseTuple(args, "s", &name))
307                 return NULL;
308
309         return PyInt_FromLong(sec_privilege_id(name));
310 }
311
312 static PyObject *py_random_sid(PyObject *self)
313 {
314         struct dom_sid *sid;
315         PyObject *ret;
316         char *str = talloc_asprintf(NULL, "S-1-5-21-%u-%u-%u", 
317                         (unsigned)generate_random(), 
318                         (unsigned)generate_random(), 
319                         (unsigned)generate_random());
320
321         sid = dom_sid_parse_talloc(NULL, str);
322         talloc_free(str);
323         ret = py_talloc_import(&dom_sid_Type, sid);
324         talloc_free(sid);
325         return ret;
326 }
327
328 static PyMethodDef py_mod_security_extra_methods[] = {
329         { "random_sid", (PyCFunction)py_random_sid, METH_NOARGS, NULL },
330         { "privilege_id", (PyCFunction)py_privilege_id, METH_VARARGS, NULL },
331         { "privilege_name", (PyCFunction)py_privilege_name, METH_VARARGS, NULL },
332         { NULL }
333 };
334
335 static void py_mod_security_patch(PyObject *m)
336 {
337         int i;
338         for (i = 0; py_mod_security_extra_methods[i].ml_name; i++) {
339                 PyObject *descr = PyCFunction_New(&py_mod_security_extra_methods[i], NULL);
340                 PyModule_AddObject(m, py_mod_security_extra_methods[i].ml_name,
341                                    descr);
342         }
343 }
344
345 #define PY_MOD_SECURITY_PATCH py_mod_security_patch