Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
4 : Copyright (C) Andrew Bartlett <abartlet@samba.org> 2011
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 :
20 : #include "lib/replace/system/python.h"
21 : #include "python/py3compat.h"
22 : #include "includes.h"
23 : #include "python/modules.h"
24 : #include "libcli/util/pyerrors.h"
25 : #include "libcli/security/security.h"
26 : #include "pytalloc.h"
27 :
28 407 : static PyObject *py_se_access_check(PyObject *module, PyObject *args, PyObject *kwargs)
29 : {
30 103 : NTSTATUS nt_status;
31 407 : const char * const kwnames[] = { "security_descriptor", "token", "access_desired", NULL };
32 407 : PyObject *py_sec_desc = NULL;
33 407 : PyObject *py_security_token = NULL;
34 103 : struct security_descriptor *security_descriptor;
35 103 : struct security_token *security_token;
36 103 : unsigned int access_desired; /* This is an unsigned int, not uint32_t,
37 : * because that's what we need for the
38 : * python PyArg_ParseTupleAndKeywords */
39 103 : uint32_t access_granted;
40 :
41 407 : if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OOI",
42 : discard_const_p(char *, kwnames),
43 : &py_sec_desc, &py_security_token, &access_desired)) {
44 0 : return NULL;
45 : }
46 :
47 407 : security_descriptor = pytalloc_get_type(py_sec_desc, struct security_descriptor);
48 407 : if (!security_descriptor) {
49 0 : PyErr_Format(PyExc_TypeError,
50 : "Expected dcerpc.security.descriptor for security_descriptor argument got %s",
51 : pytalloc_get_name(py_sec_desc));
52 0 : return NULL;
53 : }
54 :
55 407 : security_token = pytalloc_get_type(py_security_token, struct security_token);
56 407 : if (!security_token) {
57 0 : PyErr_Format(PyExc_TypeError,
58 : "Expected dcerpc.security.token for token argument, got %s",
59 : pytalloc_get_name(py_sec_desc));
60 0 : return NULL;
61 : }
62 :
63 407 : nt_status = se_access_check(security_descriptor, security_token, access_desired, &access_granted);
64 407 : if (!NT_STATUS_IS_OK(nt_status)) {
65 42 : PyErr_NTSTATUS_IS_ERR_RAISE(nt_status);
66 : }
67 :
68 365 : return PyLong_FromLong(access_granted);
69 : }
70 :
71 : static PyMethodDef py_security_methods[] = {
72 : { "access_check", PY_DISCARD_FUNC_SIG(PyCFunction,
73 : py_se_access_check),
74 : METH_VARARGS|METH_KEYWORDS,
75 : "access_check(security_descriptor, token, access_desired) -> access_granted. Raises NT_STATUS on error, including on access check failure, returns access granted bitmask"},
76 : {0},
77 : };
78 :
79 : static struct PyModuleDef moduledef = {
80 : PyModuleDef_HEAD_INIT,
81 : .m_name = "security",
82 : .m_doc = "Security support.",
83 : .m_size = -1,
84 : .m_methods = py_security_methods,
85 : };
86 :
87 340 : MODULE_INIT_FUNC(security)
88 : {
89 3 : PyObject *m;
90 :
91 340 : m = PyModule_Create(&moduledef);
92 340 : if (m == NULL)
93 0 : return NULL;
94 :
95 337 : return m;
96 : }
|