2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008-2010
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "libcli/security/security.h"
23 #ifndef Py_RETURN_NONE
24 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
27 static void PyType_AddMethods(PyTypeObject *type, PyMethodDef *methods)
31 if (type->tp_dict == NULL)
32 type->tp_dict = PyDict_New();
34 for (i = 0; methods[i].ml_name; i++) {
36 if (methods[i].ml_flags & METH_CLASS)
37 descr = PyCFunction_New(&methods[i], (PyObject *)type);
39 descr = PyDescr_NewMethod(type, &methods[i]);
40 PyDict_SetItemString(dict, methods[i].ml_name,
45 static PyObject *py_dom_sid_split(PyObject *py_self, PyObject *args)
47 struct dom_sid *self = pytalloc_get_ptr(py_self);
48 struct dom_sid *domain_sid;
52 PyObject *py_domain_sid;
54 mem_ctx = talloc_new(NULL);
55 if (mem_ctx == NULL) {
60 status = dom_sid_split_rid(mem_ctx, self, &domain_sid, &rid);
61 if (!NT_STATUS_IS_OK(status)) {
62 PyErr_SetString(PyExc_RuntimeError, "dom_sid_split_rid failed");
67 py_domain_sid = pytalloc_steal(&dom_sid_Type, domain_sid);
69 return Py_BuildValue("(OI)", py_domain_sid, rid);
72 static int py_dom_sid_cmp(PyObject *py_self, PyObject *py_other)
74 struct dom_sid *self = pytalloc_get_ptr(py_self), *other;
75 other = pytalloc_get_ptr(py_other);
79 return dom_sid_compare(self, other);
82 static PyObject *py_dom_sid_str(PyObject *py_self)
84 struct dom_sid *self = pytalloc_get_ptr(py_self);
85 char *str = dom_sid_string(NULL, self);
86 PyObject *ret = PyString_FromString(str);
91 static PyObject *py_dom_sid_repr(PyObject *py_self)
93 struct dom_sid *self = pytalloc_get_ptr(py_self);
94 char *str = dom_sid_string(NULL, self);
95 PyObject *ret = PyString_FromFormat("dom_sid('%s')", str);
100 static int py_dom_sid_init(PyObject *self, PyObject *args, PyObject *kwargs)
103 struct dom_sid *sid = pytalloc_get_ptr(self);
104 const char *kwnames[] = { "str", NULL };
106 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &str))
109 if (str != NULL && !dom_sid_parse(str, sid)) {
110 PyErr_SetString(PyExc_TypeError, "Unable to parse string");
117 static PyMethodDef py_dom_sid_extra_methods[] = {
118 { "split", (PyCFunction)py_dom_sid_split, METH_NOARGS,
119 "S.split() -> (domain_sid, rid)\n"
120 "Split a domain sid" },
125 static void py_dom_sid_patch(PyTypeObject *type)
127 type->tp_init = py_dom_sid_init;
128 type->tp_str = py_dom_sid_str;
129 type->tp_repr = py_dom_sid_repr;
130 type->tp_compare = py_dom_sid_cmp;
131 PyType_AddMethods(type, py_dom_sid_extra_methods);
134 #define PY_DOM_SID_PATCH py_dom_sid_patch
136 static PyObject *py_descriptor_sacl_add(PyObject *self, PyObject *args)
138 struct security_descriptor *desc = pytalloc_get_ptr(self);
140 struct security_ace *ace;
143 if (!PyArg_ParseTuple(args, "O", &py_ace))
146 ace = pytalloc_get_ptr(py_ace);
147 status = security_descriptor_sacl_add(desc, ace);
148 PyErr_NTSTATUS_IS_ERR_RAISE(status);
152 static PyObject *py_descriptor_dacl_add(PyObject *self, PyObject *args)
154 struct security_descriptor *desc = pytalloc_get_ptr(self);
156 struct security_ace *ace;
159 if (!PyArg_ParseTuple(args, "O", &py_ace))
162 ace = pytalloc_get_ptr(py_ace);
164 status = security_descriptor_dacl_add(desc, ace);
165 PyErr_NTSTATUS_IS_ERR_RAISE(status);
169 static PyObject *py_descriptor_dacl_del(PyObject *self, PyObject *args)
171 struct security_descriptor *desc = pytalloc_get_ptr(self);
176 if (!PyArg_ParseTuple(args, "O", &py_sid))
179 sid = pytalloc_get_ptr(py_sid);
180 status = security_descriptor_dacl_del(desc, sid);
181 PyErr_NTSTATUS_IS_ERR_RAISE(status);
185 static PyObject *py_descriptor_sacl_del(PyObject *self, PyObject *args)
187 struct security_descriptor *desc = pytalloc_get_ptr(self);
192 if (!PyArg_ParseTuple(args, "O", &py_sid))
195 sid = pytalloc_get_ptr(py_sid);
196 status = security_descriptor_sacl_del(desc, sid);
197 PyErr_NTSTATUS_IS_ERR_RAISE(status);
201 static PyObject *py_descriptor_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
203 return pytalloc_steal(self, security_descriptor_initialise(NULL));
206 static PyObject *py_descriptor_from_sddl(PyObject *self, PyObject *args)
208 struct security_descriptor *secdesc;
213 if (!PyArg_ParseTuple(args, "sO!", &sddl, &dom_sid_Type, &py_sid))
216 sid = pytalloc_get_ptr(py_sid);
218 secdesc = sddl_decode(NULL, sddl, sid);
219 if (secdesc == NULL) {
220 PyErr_SetString(PyExc_TypeError, "Unable to parse SDDL");
224 return pytalloc_steal((PyTypeObject *)self, secdesc);
227 static PyObject *py_descriptor_as_sddl(PyObject *self, PyObject *args)
230 PyObject *py_sid = Py_None;
231 struct security_descriptor *desc = pytalloc_get_ptr(self);
235 if (!PyArg_ParseTuple(args, "|O!", &dom_sid_Type, &py_sid))
238 if (py_sid != Py_None)
239 sid = pytalloc_get_ptr(py_sid);
243 text = sddl_encode(NULL, desc, sid);
245 ret = PyString_FromString(text);
252 static PyMethodDef py_descriptor_extra_methods[] = {
253 { "sacl_add", (PyCFunction)py_descriptor_sacl_add, METH_VARARGS,
254 "S.sacl_add(ace) -> None\n"
255 "Add a security ace to this security descriptor" },
256 { "dacl_add", (PyCFunction)py_descriptor_dacl_add, METH_VARARGS,
258 { "dacl_del", (PyCFunction)py_descriptor_dacl_del, METH_VARARGS,
260 { "sacl_del", (PyCFunction)py_descriptor_sacl_del, METH_VARARGS,
262 { "from_sddl", (PyCFunction)py_descriptor_from_sddl, METH_VARARGS|METH_CLASS,
264 { "as_sddl", (PyCFunction)py_descriptor_as_sddl, METH_VARARGS,
269 static void py_descriptor_patch(PyTypeObject *type)
271 type->tp_new = py_descriptor_new;
272 PyType_AddMethods(type, py_descriptor_extra_methods);
275 #define PY_DESCRIPTOR_PATCH py_descriptor_patch
277 static PyObject *py_token_is_sid(PyObject *self, PyObject *args)
281 struct security_token *token = pytalloc_get_ptr(self);
282 if (!PyArg_ParseTuple(args, "O", &py_sid))
285 sid = pytalloc_get_ptr(py_sid);
287 return PyBool_FromLong(security_token_is_sid(token, sid));
290 static PyObject *py_token_has_sid(PyObject *self, PyObject *args)
294 struct security_token *token = pytalloc_get_ptr(self);
295 if (!PyArg_ParseTuple(args, "O", &py_sid))
298 sid = pytalloc_get_ptr(py_sid);
300 return PyBool_FromLong(security_token_has_sid(token, sid));
303 static PyObject *py_token_is_anonymous(PyObject *self)
305 struct security_token *token = pytalloc_get_ptr(self);
307 return PyBool_FromLong(security_token_is_anonymous(token));
310 static PyObject *py_token_is_system(PyObject *self)
312 struct security_token *token = pytalloc_get_ptr(self);
314 return PyBool_FromLong(security_token_is_system(token));
317 static PyObject *py_token_has_builtin_administrators(PyObject *self)
319 struct security_token *token = pytalloc_get_ptr(self);
321 return PyBool_FromLong(security_token_has_builtin_administrators(token));
324 static PyObject *py_token_has_nt_authenticated_users(PyObject *self)
326 struct security_token *token = pytalloc_get_ptr(self);
328 return PyBool_FromLong(security_token_has_nt_authenticated_users(token));
331 static PyObject *py_token_has_privilege(PyObject *self, PyObject *args)
334 struct security_token *token = pytalloc_get_ptr(self);
336 if (!PyArg_ParseTuple(args, "i", &priv))
339 return PyBool_FromLong(security_token_has_privilege(token, priv));
342 static PyObject *py_token_set_privilege(PyObject *self, PyObject *args)
345 struct security_token *token = pytalloc_get_ptr(self);
347 if (!PyArg_ParseTuple(args, "i", &priv))
350 security_token_set_privilege(token, priv);
354 static PyObject *py_token_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
356 return pytalloc_steal(self, security_token_initialise(NULL));
359 static PyMethodDef py_token_extra_methods[] = {
360 { "is_sid", (PyCFunction)py_token_is_sid, METH_VARARGS,
361 "S.is_sid(sid) -> bool\n"
362 "Check whether this token is of the specified SID." },
363 { "has_sid", (PyCFunction)py_token_has_sid, METH_VARARGS,
365 { "is_anonymous", (PyCFunction)py_token_is_anonymous, METH_NOARGS,
366 "S.is_anonymus() -> bool\n"
367 "Check whether this is an anonymous token." },
368 { "is_system", (PyCFunction)py_token_is_system, METH_NOARGS,
370 { "has_builtin_administrators", (PyCFunction)py_token_has_builtin_administrators, METH_NOARGS,
372 { "has_nt_authenticated_users", (PyCFunction)py_token_has_nt_authenticated_users, METH_NOARGS,
374 { "has_privilege", (PyCFunction)py_token_has_privilege, METH_VARARGS,
376 { "set_privilege", (PyCFunction)py_token_set_privilege, METH_VARARGS,
381 #define PY_TOKEN_PATCH py_token_patch
382 static void py_token_patch(PyTypeObject *type)
384 type->tp_new = py_token_new;
385 PyType_AddMethods(type, py_token_extra_methods);
388 static PyObject *py_privilege_name(PyObject *self, PyObject *args)
391 if (!PyArg_ParseTuple(args, "i", &priv))
394 return PyString_FromString(sec_privilege_name(priv));
397 static PyObject *py_privilege_id(PyObject *self, PyObject *args)
401 if (!PyArg_ParseTuple(args, "s", &name))
404 return PyInt_FromLong(sec_privilege_id(name));
407 static PyObject *py_random_sid(PyObject *self)
411 char *str = talloc_asprintf(NULL, "S-1-5-21-%u-%u-%u",
412 (unsigned)generate_random(),
413 (unsigned)generate_random(),
414 (unsigned)generate_random());
416 sid = dom_sid_parse_talloc(NULL, str);
418 ret = pytalloc_steal(&dom_sid_Type, sid);
422 static PyMethodDef py_mod_security_extra_methods[] = {
423 { "random_sid", (PyCFunction)py_random_sid, METH_NOARGS, NULL },
424 { "privilege_id", (PyCFunction)py_privilege_id, METH_VARARGS, NULL },
425 { "privilege_name", (PyCFunction)py_privilege_name, METH_VARARGS, NULL },
429 static void py_mod_security_patch(PyObject *m)
432 for (i = 0; py_mod_security_extra_methods[i].ml_name; i++) {
433 PyObject *descr = PyCFunction_New(&py_mod_security_extra_methods[i], NULL);
434 PyModule_AddObject(m, py_mod_security_extra_methods[i].ml_name,
439 #define PY_MOD_SECURITY_PATCH py_mod_security_patch