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/>.
20 #include "libcli/security/security.h"
22 #ifndef Py_RETURN_NONE
23 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
26 static void PyType_AddMethods(PyTypeObject *type, PyMethodDef *methods)
30 if (type->tp_dict == NULL)
31 type->tp_dict = PyDict_New();
33 for (i = 0; methods[i].ml_name; i++) {
34 PyObject *descr = PyDescr_NewMethod(type, &methods[i]);
35 PyDict_SetItemString(dict, methods[i].ml_name,
40 static int py_dom_sid_cmp(PyObject *self, PyObject *py_other)
42 struct dom_sid *this = py_talloc_get_ptr(self), *other;
43 other = py_talloc_get_ptr(py_other);
47 return dom_sid_compare(this, other);
50 static PyObject *py_dom_sid_str(PyObject *self)
52 struct dom_sid *this = py_talloc_get_ptr(self);
53 char *str = dom_sid_string(NULL, this);
54 PyObject *ret = PyString_FromString(str);
59 static PyObject *py_dom_sid_repr(PyObject *self)
61 struct dom_sid *this = py_talloc_get_ptr(self);
62 char *str = dom_sid_string(NULL, this);
63 PyObject *ret = PyString_FromFormat("dom_sid('%s')", str);
68 static int py_dom_sid_init(PyObject *self, PyObject *args, PyObject *kwargs)
71 struct dom_sid *sid = py_talloc_get_ptr(self);
72 const char *kwnames[] = { "str", NULL };
74 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &str))
77 if (str != NULL && !dom_sid_parse(str, sid)) {
78 PyErr_SetString(PyExc_TypeError, "Unable to parse string");
85 static void py_dom_sid_patch(PyTypeObject *type)
87 type->tp_init = py_dom_sid_init;
88 type->tp_str = py_dom_sid_str;
89 type->tp_repr = py_dom_sid_repr;
90 type->tp_compare = py_dom_sid_cmp;
93 #define PY_DOM_SID_PATCH py_dom_sid_patch
95 static PyObject *py_descriptor_sacl_add(PyObject *self, PyObject *args)
97 struct security_descriptor *desc = py_talloc_get_ptr(self);
99 struct security_ace *ace;
102 if (!PyArg_ParseTuple(args, "O", &py_ace))
105 ace = py_talloc_get_ptr(py_ace);
106 status = security_descriptor_sacl_add(desc, ace);
107 PyErr_NTSTATUS_IS_ERR_RAISE(status);
111 static PyObject *py_descriptor_dacl_add(PyObject *self, PyObject *args)
113 struct security_descriptor *desc = py_talloc_get_ptr(self);
115 struct security_ace *ace;
118 if (!PyArg_ParseTuple(args, "O", &py_ace))
121 ace = py_talloc_get_ptr(py_ace);
123 status = security_descriptor_dacl_add(desc, ace);
124 PyErr_NTSTATUS_IS_ERR_RAISE(status);
128 static PyObject *py_descriptor_dacl_del(PyObject *self, PyObject *args)
130 struct security_descriptor *desc = py_talloc_get_ptr(self);
135 if (!PyArg_ParseTuple(args, "O", &py_sid))
138 sid = py_talloc_get_ptr(py_sid);
139 status = security_descriptor_dacl_del(desc, sid);
140 PyErr_NTSTATUS_IS_ERR_RAISE(status);
144 static PyObject *py_descriptor_sacl_del(PyObject *self, PyObject *args)
146 struct security_descriptor *desc = py_talloc_get_ptr(self);
151 if (!PyArg_ParseTuple(args, "O", &py_sid))
154 sid = py_talloc_get_ptr(py_sid);
155 status = security_descriptor_sacl_del(desc, sid);
156 PyErr_NTSTATUS_IS_ERR_RAISE(status);
160 static PyObject *py_descriptor_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
162 return py_talloc_import(self, security_descriptor_initialise(NULL));
165 static PyMethodDef py_descriptor_extra_methods[] = {
166 { "sacl_add", (PyCFunction)py_descriptor_sacl_add, METH_VARARGS,
167 "S.sacl_add(ace) -> None\n"
168 "Add a security ace to this security descriptor" },
169 { "dacl_add", (PyCFunction)py_descriptor_dacl_add, METH_VARARGS,
171 { "dacl_del", (PyCFunction)py_descriptor_dacl_del, METH_VARARGS,
173 { "sacl_del", (PyCFunction)py_descriptor_sacl_del, METH_VARARGS,
178 static void py_descriptor_patch(PyTypeObject *type)
180 type->tp_new = py_descriptor_new;
181 PyType_AddMethods(type, py_descriptor_extra_methods);
184 #define PY_DESCRIPTOR_PATCH py_descriptor_patch
186 static PyObject *py_token_is_sid(PyObject *self, PyObject *args)
190 struct security_token *token = py_talloc_get_ptr(self);
191 if (!PyArg_ParseTuple(args, "O", &py_sid))
194 sid = py_talloc_get_ptr(py_sid);
196 return PyBool_FromLong(security_token_is_sid(token, sid));
199 static PyObject *py_token_has_sid(PyObject *self, PyObject *args)
203 struct security_token *token = py_talloc_get_ptr(self);
204 if (!PyArg_ParseTuple(args, "O", &py_sid))
207 sid = py_talloc_get_ptr(py_sid);
209 return PyBool_FromLong(security_token_has_sid(token, sid));
212 static PyObject *py_token_is_anonymous(PyObject *self)
214 struct security_token *token = py_talloc_get_ptr(self);
216 return PyBool_FromLong(security_token_is_anonymous(token));
219 static PyObject *py_token_is_system(PyObject *self)
221 struct security_token *token = py_talloc_get_ptr(self);
223 return PyBool_FromLong(security_token_is_system(token));
226 static PyObject *py_token_has_builtin_administrators(PyObject *self)
228 struct security_token *token = py_talloc_get_ptr(self);
230 return PyBool_FromLong(security_token_has_builtin_administrators(token));
233 static PyObject *py_token_has_nt_authenticated_users(PyObject *self)
235 struct security_token *token = py_talloc_get_ptr(self);
237 return PyBool_FromLong(security_token_has_nt_authenticated_users(token));
240 static PyObject *py_token_has_privilege(PyObject *self, PyObject *args)
243 struct security_token *token = py_talloc_get_ptr(self);
245 if (!PyArg_ParseTuple(args, "i", &priv))
248 return PyBool_FromLong(security_token_has_privilege(token, priv));
251 static PyObject *py_token_set_privilege(PyObject *self, PyObject *args)
254 struct security_token *token = py_talloc_get_ptr(self);
256 if (!PyArg_ParseTuple(args, "i", &priv))
259 security_token_set_privilege(token, priv);
263 static PyObject *py_token_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
265 return py_talloc_import(self, security_token_initialise(NULL));
268 static PyMethodDef py_token_extra_methods[] = {
269 { "is_sid", (PyCFunction)py_token_is_sid, METH_VARARGS,
270 "S.is_sid(sid) -> bool\n"
271 "Check whether this token is of the specified SID." },
272 { "has_sid", (PyCFunction)py_token_has_sid, METH_VARARGS,
274 { "is_anonymous", (PyCFunction)py_token_is_anonymous, METH_NOARGS,
275 "S.is_anonymus() -> bool\n"
276 "Check whether this is an anonymous token." },
277 { "is_system", (PyCFunction)py_token_is_system, METH_NOARGS,
279 { "has_builtin_administrators", (PyCFunction)py_token_has_builtin_administrators, METH_NOARGS,
281 { "has_nt_authenticated_users", (PyCFunction)py_token_has_nt_authenticated_users, METH_NOARGS,
283 { "has_privilege", (PyCFunction)py_token_has_privilege, METH_VARARGS,
285 { "set_privilege", (PyCFunction)py_token_set_privilege, METH_VARARGS,
290 #define PY_TOKEN_PATCH py_token_patch
291 static void py_token_patch(PyTypeObject *type)
293 type->tp_new = py_token_new;
294 PyType_AddMethods(type, py_token_extra_methods);
297 static PyObject *py_privilege_name(PyObject *self, PyObject *args)
300 if (!PyArg_ParseTuple(args, "i", &priv))
303 return PyString_FromString(sec_privilege_name(priv));
306 static PyObject *py_privilege_id(PyObject *self, PyObject *args)
310 if (!PyArg_ParseTuple(args, "s", &name))
313 return PyInt_FromLong(sec_privilege_id(name));
316 static PyObject *py_random_sid(PyObject *self)
320 char *str = talloc_asprintf(NULL, "S-1-5-21-%u-%u-%u",
321 (unsigned)generate_random(),
322 (unsigned)generate_random(),
323 (unsigned)generate_random());
325 sid = dom_sid_parse_talloc(NULL, str);
327 ret = py_talloc_import(&dom_sid_Type, sid);
332 static PyMethodDef py_mod_security_extra_methods[] = {
333 { "random_sid", (PyCFunction)py_random_sid, METH_NOARGS, NULL },
334 { "privilege_id", (PyCFunction)py_privilege_id, METH_VARARGS, NULL },
335 { "privilege_name", (PyCFunction)py_privilege_name, METH_VARARGS, NULL },
339 static void py_mod_security_patch(PyObject *m)
342 for (i = 0; py_mod_security_extra_methods[i].ml_name; i++) {
343 PyObject *descr = PyCFunction_New(&py_mod_security_extra_methods[i], NULL);
344 PyModule_AddObject(m, py_mod_security_extra_methods[i].ml_name,
349 #define PY_MOD_SECURITY_PATCH py_mod_security_patch