2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
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/>.
19 #include "../lib/util/python_util.h"
20 #include "libcli/security/security.h"
22 static void PyType_AddMethods(PyTypeObject *type, PyMethodDef *methods)
26 if (type->tp_dict == NULL)
27 type->tp_dict = PyDict_New();
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,
36 static int py_dom_sid_cmp(PyObject *self, PyObject *py_other)
38 struct dom_sid *this = py_talloc_get_ptr(self), *other;
39 other = py_talloc_get_ptr(py_other);
43 return dom_sid_compare(this, other);
46 static PyObject *py_dom_sid_str(PyObject *self)
48 struct dom_sid *this = py_talloc_get_ptr(self);
49 char *str = dom_sid_string(NULL, this);
50 PyObject *ret = PyString_FromString(str);
55 static PyObject *py_dom_sid_repr(PyObject *self)
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);
64 static int py_dom_sid_init(PyObject *self, PyObject *args, PyObject *kwargs)
67 struct dom_sid *sid = py_talloc_get_ptr(self);
68 const char *kwnames[] = { "str", NULL };
70 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &str))
73 if (str != NULL && !dom_sid_parse(str, sid)) {
74 PyErr_SetString(PyExc_TypeError, "Unable to parse string");
81 static void py_dom_sid_patch(PyTypeObject *type)
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;
89 #define PY_DOM_SID_PATCH py_dom_sid_patch
91 static PyObject *py_descriptor_sacl_add(PyObject *self, PyObject *args)
93 struct security_descriptor *desc = py_talloc_get_ptr(self);
95 struct security_ace *ace;
98 if (!PyArg_ParseTuple(args, "O", &py_ace))
101 ace = py_talloc_get_ptr(py_ace);
102 status = security_descriptor_sacl_add(desc, ace);
103 PyErr_NTSTATUS_IS_ERR_RAISE(status);
107 static PyObject *py_descriptor_dacl_add(PyObject *self, PyObject *args)
109 struct security_descriptor *desc = py_talloc_get_ptr(self);
111 struct security_ace *ace;
114 if (!PyArg_ParseTuple(args, "O", &py_ace))
117 ace = py_talloc_get_ptr(py_ace);
119 status = security_descriptor_dacl_add(desc, ace);
120 PyErr_NTSTATUS_IS_ERR_RAISE(status);
124 static PyObject *py_descriptor_dacl_del(PyObject *self, PyObject *args)
126 struct security_descriptor *desc = py_talloc_get_ptr(self);
131 if (!PyArg_ParseTuple(args, "O", &py_sid))
134 sid = py_talloc_get_ptr(py_sid);
135 status = security_descriptor_dacl_del(desc, sid);
136 PyErr_NTSTATUS_IS_ERR_RAISE(status);
140 static PyObject *py_descriptor_sacl_del(PyObject *self, PyObject *args)
142 struct security_descriptor *desc = py_talloc_get_ptr(self);
147 if (!PyArg_ParseTuple(args, "O", &py_sid))
150 sid = py_talloc_get_ptr(py_sid);
151 status = security_descriptor_sacl_del(desc, sid);
152 PyErr_NTSTATUS_IS_ERR_RAISE(status);
156 static PyObject *py_descriptor_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
158 return py_talloc_import(self, security_descriptor_initialise(NULL));
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,
167 { "dacl_del", (PyCFunction)py_descriptor_dacl_del, METH_VARARGS,
169 { "sacl_del", (PyCFunction)py_descriptor_sacl_del, METH_VARARGS,
174 static void py_descriptor_patch(PyTypeObject *type)
176 type->tp_new = py_descriptor_new;
177 PyType_AddMethods(type, py_descriptor_extra_methods);
180 #define PY_DESCRIPTOR_PATCH py_descriptor_patch
182 static PyObject *py_token_is_sid(PyObject *self, PyObject *args)
186 struct security_token *token = py_talloc_get_ptr(self);
187 if (!PyArg_ParseTuple(args, "O", &py_sid))
190 sid = py_talloc_get_ptr(py_sid);
192 return PyBool_FromLong(security_token_is_sid(token, sid));
195 static PyObject *py_token_has_sid(PyObject *self, PyObject *args)
199 struct security_token *token = py_talloc_get_ptr(self);
200 if (!PyArg_ParseTuple(args, "O", &py_sid))
203 sid = py_talloc_get_ptr(py_sid);
205 return PyBool_FromLong(security_token_has_sid(token, sid));
208 static PyObject *py_token_is_anonymous(PyObject *self)
210 struct security_token *token = py_talloc_get_ptr(self);
212 return PyBool_FromLong(security_token_is_anonymous(token));
215 static PyObject *py_token_is_system(PyObject *self)
217 struct security_token *token = py_talloc_get_ptr(self);
219 return PyBool_FromLong(security_token_is_system(token));
222 static PyObject *py_token_has_builtin_administrators(PyObject *self)
224 struct security_token *token = py_talloc_get_ptr(self);
226 return PyBool_FromLong(security_token_has_builtin_administrators(token));
229 static PyObject *py_token_has_nt_authenticated_users(PyObject *self)
231 struct security_token *token = py_talloc_get_ptr(self);
233 return PyBool_FromLong(security_token_has_nt_authenticated_users(token));
236 static PyObject *py_token_has_privilege(PyObject *self, PyObject *args)
239 struct security_token *token = py_talloc_get_ptr(self);
241 if (!PyArg_ParseTuple(args, "i", &priv))
244 return PyBool_FromLong(security_token_has_privilege(token, priv));
247 static PyObject *py_token_set_privilege(PyObject *self, PyObject *args)
250 struct security_token *token = py_talloc_get_ptr(self);
252 if (!PyArg_ParseTuple(args, "i", &priv))
255 security_token_set_privilege(token, priv);
259 static PyObject *py_token_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
261 return py_talloc_import(self, security_token_initialise(NULL));
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,
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,
275 { "has_builtin_administrators", (PyCFunction)py_token_has_builtin_administrators, METH_NOARGS,
277 { "has_nt_authenticated_users", (PyCFunction)py_token_has_nt_authenticated_users, METH_NOARGS,
279 { "has_privilege", (PyCFunction)py_token_has_privilege, METH_VARARGS,
281 { "set_privilege", (PyCFunction)py_token_set_privilege, METH_VARARGS,
286 #define PY_TOKEN_PATCH py_token_patch
287 static void py_token_patch(PyTypeObject *type)
289 type->tp_new = py_token_new;
290 PyType_AddMethods(type, py_token_extra_methods);
293 static PyObject *py_privilege_name(PyObject *self, PyObject *args)
296 if (!PyArg_ParseTuple(args, "i", &priv))
299 return PyString_FromString(sec_privilege_name(priv));
302 static PyObject *py_privilege_id(PyObject *self, PyObject *args)
306 if (!PyArg_ParseTuple(args, "s", &name))
309 return PyInt_FromLong(sec_privilege_id(name));
312 static PyObject *py_random_sid(PyObject *self)
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());
321 sid = dom_sid_parse_talloc(NULL, str);
323 ret = py_talloc_import(&dom_sid_Type, sid);
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 },
335 static void py_mod_security_patch(PyObject *m)
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,
345 #define PY_MOD_SECURITY_PATCH py_mod_security_patch