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 "libcli/security/security.h"
21 static void PyType_AddMethods(PyTypeObject *type, PyMethodDef *methods)
25 if (type->tp_dict == NULL)
26 type->tp_dict = PyDict_New();
28 for (i = 0; methods[i].ml_name; i++) {
29 PyObject *descr = PyDescr_NewMethod(type, &methods[i]);
30 PyDict_SetItemString(dict, methods[i].ml_name,
35 static int py_dom_sid_cmp(PyObject *self, PyObject *py_other)
37 struct dom_sid *this = py_talloc_get_ptr(self), *other;
38 other = py_talloc_get_ptr(py_other);
42 return dom_sid_compare(this, other);
45 static PyObject *py_dom_sid_str(PyObject *self)
47 struct dom_sid *this = py_talloc_get_ptr(self);
48 char *str = dom_sid_string(NULL, this);
49 PyObject *ret = PyString_FromString(str);
54 static PyObject *py_dom_sid_repr(PyObject *self)
56 struct dom_sid *this = py_talloc_get_ptr(self);
57 char *str = dom_sid_string(NULL, this);
58 PyObject *ret = PyString_FromFormat("dom_sid('%s')", str);
63 static int py_dom_sid_init(PyObject *self, PyObject *args, PyObject *kwargs)
66 struct dom_sid *sid = py_talloc_get_ptr(self);
67 const char *kwnames[] = { "str", NULL };
69 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &str))
72 if (str != NULL && !dom_sid_parse(str, sid)) {
73 PyErr_SetString(PyExc_TypeError, "Unable to parse string");
80 static void py_dom_sid_patch(PyTypeObject *type)
82 type->tp_init = py_dom_sid_init;
83 type->tp_str = py_dom_sid_str;
84 type->tp_repr = py_dom_sid_repr;
85 type->tp_compare = py_dom_sid_cmp;
88 #define PY_DOM_SID_PATCH py_dom_sid_patch
90 static PyObject *py_descriptor_sacl_add(PyObject *self, PyObject *args)
92 struct security_descriptor *desc = py_talloc_get_ptr(self);
94 struct security_ace *ace;
97 if (!PyArg_ParseTuple(args, "O", &py_ace))
100 ace = py_talloc_get_ptr(py_ace);
101 status = security_descriptor_sacl_add(desc, ace);
102 PyErr_NTSTATUS_IS_ERR_RAISE(status);
106 static PyObject *py_descriptor_dacl_add(PyObject *self, PyObject *args)
108 struct security_descriptor *desc = py_talloc_get_ptr(self);
110 struct security_ace *ace;
113 if (!PyArg_ParseTuple(args, "O", &py_ace))
116 ace = py_talloc_get_ptr(py_ace);
118 status = security_descriptor_dacl_add(desc, ace);
119 PyErr_NTSTATUS_IS_ERR_RAISE(status);
123 static PyObject *py_descriptor_dacl_del(PyObject *self, PyObject *args)
125 struct security_descriptor *desc = py_talloc_get_ptr(self);
130 if (!PyArg_ParseTuple(args, "O", &py_sid))
133 sid = py_talloc_get_ptr(py_sid);
134 status = security_descriptor_dacl_del(desc, sid);
135 PyErr_NTSTATUS_IS_ERR_RAISE(status);
139 static PyObject *py_descriptor_sacl_del(PyObject *self, PyObject *args)
141 struct security_descriptor *desc = py_talloc_get_ptr(self);
146 if (!PyArg_ParseTuple(args, "O", &py_sid))
149 sid = py_talloc_get_ptr(py_sid);
150 status = security_descriptor_sacl_del(desc, sid);
151 PyErr_NTSTATUS_IS_ERR_RAISE(status);
155 static PyObject *py_descriptor_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
157 return py_talloc_import(self, security_descriptor_initialise(NULL));
160 static PyMethodDef py_descriptor_extra_methods[] = {
161 { "sacl_add", (PyCFunction)py_descriptor_sacl_add, METH_VARARGS,
162 "S.sacl_add(ace) -> None\n"
163 "Add a security ace to this security descriptor" },
164 { "dacl_add", (PyCFunction)py_descriptor_dacl_add, METH_VARARGS,
166 { "dacl_del", (PyCFunction)py_descriptor_dacl_del, METH_VARARGS,
168 { "sacl_del", (PyCFunction)py_descriptor_sacl_del, METH_VARARGS,
173 static void py_descriptor_patch(PyTypeObject *type)
175 type->tp_new = py_descriptor_new;
176 PyType_AddMethods(type, py_descriptor_extra_methods);
179 #define PY_DESCRIPTOR_PATCH py_descriptor_patch
181 static PyObject *py_token_is_sid(PyObject *self, PyObject *args)
185 struct security_token *token = py_talloc_get_ptr(self);
186 if (!PyArg_ParseTuple(args, "O", &py_sid))
189 sid = py_talloc_get_ptr(py_sid);
191 return PyBool_FromLong(security_token_is_sid(token, sid));
194 static PyObject *py_token_has_sid(PyObject *self, PyObject *args)
198 struct security_token *token = py_talloc_get_ptr(self);
199 if (!PyArg_ParseTuple(args, "O", &py_sid))
202 sid = py_talloc_get_ptr(py_sid);
204 return PyBool_FromLong(security_token_has_sid(token, sid));
207 static PyObject *py_token_is_anonymous(PyObject *self)
209 struct security_token *token = py_talloc_get_ptr(self);
211 return PyBool_FromLong(security_token_is_anonymous(token));
214 static PyObject *py_token_is_system(PyObject *self)
216 struct security_token *token = py_talloc_get_ptr(self);
218 return PyBool_FromLong(security_token_is_system(token));
221 static PyObject *py_token_has_builtin_administrators(PyObject *self)
223 struct security_token *token = py_talloc_get_ptr(self);
225 return PyBool_FromLong(security_token_has_builtin_administrators(token));
228 static PyObject *py_token_has_nt_authenticated_users(PyObject *self)
230 struct security_token *token = py_talloc_get_ptr(self);
232 return PyBool_FromLong(security_token_has_nt_authenticated_users(token));
235 static PyObject *py_token_has_privilege(PyObject *self, PyObject *args)
238 struct security_token *token = py_talloc_get_ptr(self);
240 if (!PyArg_ParseTuple(args, "i", &priv))
243 return PyBool_FromLong(security_token_has_privilege(token, priv));
246 static PyObject *py_token_set_privilege(PyObject *self, PyObject *args)
249 struct security_token *token = py_talloc_get_ptr(self);
251 if (!PyArg_ParseTuple(args, "i", &priv))
254 security_token_set_privilege(token, priv);
258 static PyObject *py_token_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
260 return py_talloc_import(self, security_token_initialise(NULL));
263 static PyMethodDef py_token_extra_methods[] = {
264 { "is_sid", (PyCFunction)py_token_is_sid, METH_VARARGS,
265 "S.is_sid(sid) -> bool\n"
266 "Check whether this token is of the specified SID." },
267 { "has_sid", (PyCFunction)py_token_has_sid, METH_VARARGS,
269 { "is_anonymous", (PyCFunction)py_token_is_anonymous, METH_NOARGS,
270 "S.is_anonymus() -> bool\n"
271 "Check whether this is an anonymous token." },
272 { "is_system", (PyCFunction)py_token_is_system, METH_NOARGS,
274 { "has_builtin_administrators", (PyCFunction)py_token_has_builtin_administrators, METH_NOARGS,
276 { "has_nt_authenticated_users", (PyCFunction)py_token_has_nt_authenticated_users, METH_NOARGS,
278 { "has_privilege", (PyCFunction)py_token_has_privilege, METH_VARARGS,
280 { "set_privilege", (PyCFunction)py_token_set_privilege, METH_VARARGS,
285 #define PY_TOKEN_PATCH py_token_patch
286 static void py_token_patch(PyTypeObject *type)
288 type->tp_new = py_token_new;
289 PyType_AddMethods(type, py_token_extra_methods);
292 static PyObject *py_privilege_name(PyObject *self, PyObject *args)
295 if (!PyArg_ParseTuple(args, "i", &priv))
298 return PyString_FromString(sec_privilege_name(priv));
301 static PyObject *py_privilege_id(PyObject *self, PyObject *args)
305 if (!PyArg_ParseTuple(args, "s", &name))
308 return PyInt_FromLong(sec_privilege_id(name));
311 static PyObject *py_random_sid(PyObject *self)
315 char *str = talloc_asprintf(NULL, "S-1-5-21-%u-%u-%u",
316 (unsigned)generate_random(),
317 (unsigned)generate_random(),
318 (unsigned)generate_random());
320 sid = dom_sid_parse_talloc(NULL, str);
322 ret = py_talloc_import(&dom_sid_Type, sid);
327 static PyMethodDef py_mod_security_extra_methods[] = {
328 { "random_sid", (PyCFunction)py_random_sid, METH_NOARGS, NULL },
329 { "privilege_id", (PyCFunction)py_privilege_id, METH_VARARGS, NULL },
330 { "privilege_name", (PyCFunction)py_privilege_name, METH_VARARGS, NULL },
334 static void py_mod_security_patch(PyObject *m)
337 for (i = 0; py_mod_security_extra_methods[i].ml_name; i++) {
338 PyObject *descr = PyCFunction_New(&py_mod_security_extra_methods[i], NULL);
339 PyModule_AddObject(m, py_mod_security_extra_methods[i].ml_name,
344 #define PY_MOD_SECURITY_PATCH py_mod_security_patch