Remove duplicate Python bindings for dom_sid, security_descriptor and
[ira/wip.git] / source4 / librpc / ndr / py_security.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
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 #include "libcli/security/security.h"
20
21 static PyObject *py_dom_sid_eq(PyObject *self, PyObject *args)
22 {
23         struct dom_sid *this = py_talloc_get_ptr(self), *other;
24         PyObject *py_other;
25
26         if (!PyArg_ParseTuple(args, "O", &py_other)) 
27                 return NULL;
28
29         other = py_talloc_get_type(py_other, struct dom_sid);
30         if (other == NULL)
31                 return Py_False;
32
33         return dom_sid_equal(this, other)?Py_True:Py_False;
34 }
35
36 static PyObject *py_dom_sid_str(PyObject *self)
37 {
38         struct dom_sid *this = py_talloc_get_ptr(self);
39         char *str = dom_sid_string(NULL, this);
40         PyObject *ret = PyString_FromString(str);
41         talloc_free(str);
42         return ret;
43 }
44
45 static PyObject *py_dom_sid_repr(PyObject *self)
46 {
47         struct dom_sid *this = py_talloc_get_ptr(self);
48         char *str = dom_sid_string(NULL, this);
49         PyObject *ret = PyString_FromFormat("dom_sid('%s')", str);
50         talloc_free(str);
51         return ret;
52 }
53
54 #define PY_DOM_SID_REPR py_dom_sid_repr
55
56 static PyObject *py_dom_sid_init(PyObject *self, PyObject *args)
57 {
58         struct dom_sid *this = py_talloc_get_ptr(self);
59         char *str;
60         struct dom_sid *new_this;
61
62         if (!PyArg_ParseTuple(args, "|s", &str))
63                 return NULL;
64
65         new_this = dom_sid_parse_talloc(NULL, str);
66         memcpy(this, new_this, sizeof(*new_this));
67         talloc_free(new_this);
68         return Py_None;
69 }
70
71 #define PY_DOM_SID_EXTRA_METHODS \
72         { "__eq__", (PyCFunction)py_dom_sid_eq, METH_VARARGS, "S.__eq__(x) -> S == x" }, \
73         { "__str__", (PyCFunction)py_dom_sid_str, METH_NOARGS, "S.__str__() -> str(S)" }, \
74         { "__init__", (PyCFunction)py_dom_sid_init, METH_VARARGS, "S.__init__(str=None)" },
75
76 static PyObject *py_descriptor_sacl_add(PyObject *self, PyObject *args)
77 {
78         struct security_descriptor *desc = py_talloc_get_ptr(self);
79         NTSTATUS status;
80         struct security_ace *ace;
81         PyObject *py_ace;
82
83         if (!PyArg_ParseTuple(args, "O", &py_ace))
84                 return NULL;
85
86         ace = py_talloc_get_ptr(py_ace);
87         status = security_descriptor_sacl_add(desc, ace);
88         PyErr_NTSTATUS_IS_ERR_RAISE(status);
89         return Py_None;
90 }
91
92 static PyObject *py_descriptor_dacl_add(PyObject *self, PyObject *args)
93 {
94         struct security_descriptor *desc = py_talloc_get_ptr(self);
95         NTSTATUS status;
96         struct security_ace *ace;
97         PyObject *py_ace;
98
99         if (!PyArg_ParseTuple(args, "O", &py_ace))
100                 return NULL;
101
102         ace = py_talloc_get_ptr(py_ace);
103
104         status = security_descriptor_dacl_add(desc, ace);
105         PyErr_NTSTATUS_IS_ERR_RAISE(status);
106         return Py_None;
107 }
108
109 static PyObject *py_descriptor_dacl_del(PyObject *self, PyObject *args)
110 {
111         struct security_descriptor *desc = py_talloc_get_ptr(self);
112         NTSTATUS status;
113         struct dom_sid *sid;
114         PyObject *py_sid;
115
116         if (!PyArg_ParseTuple(args, "O", &py_sid))
117                 return NULL;
118
119         sid = py_talloc_get_ptr(py_sid);
120         status = security_descriptor_dacl_del(desc, sid);
121         PyErr_NTSTATUS_IS_ERR_RAISE(status);
122         return Py_None;
123 }
124
125 static PyObject *py_descriptor_sacl_del(PyObject *self, PyObject *args)
126 {
127         struct security_descriptor *desc = py_talloc_get_ptr(self);
128         NTSTATUS status;
129         struct dom_sid *sid;
130         PyObject *py_sid;
131
132         if (!PyArg_ParseTuple(args, "O", &py_sid))
133                 return NULL;
134
135         sid = py_talloc_get_ptr(py_sid);
136         status = security_descriptor_sacl_del(desc, sid);
137         PyErr_NTSTATUS_IS_ERR_RAISE(status);
138         return Py_None;
139 }
140
141 static PyObject *py_descriptor_eq(PyObject *self, PyObject *args)
142 {
143         struct security_descriptor *desc1 = py_talloc_get_ptr(self), *desc2;
144         PyObject *py_other;
145
146         if (!PyArg_ParseTuple(args, "O", &py_other))
147                 return NULL;
148
149         desc2 = py_talloc_get_ptr(py_other);
150
151         return PyBool_FromLong(security_descriptor_equal(desc1, desc2));
152 }
153
154 static PyObject *py_descriptor_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
155 {
156         return py_talloc_import(self, security_descriptor_initialise(NULL));
157 }       
158
159 #define PY_SECURITY_DESCRIPTOR_EXTRA_METHODS \
160         { "sacl_add", (PyCFunction)py_descriptor_sacl_add, METH_VARARGS, \
161                 "S.sacl_add(ace) -> None\n" \
162                 "Add a security ace to this security descriptor" },\
163         { "dacl_add", (PyCFunction)py_descriptor_dacl_add, METH_VARARGS, \
164                 NULL }, \
165         { "dacl_del", (PyCFunction)py_descriptor_dacl_del, METH_VARARGS, \
166                 NULL }, \
167         { "sacl_del", (PyCFunction)py_descriptor_sacl_del, METH_VARARGS, \
168                 NULL }, \
169         { "__eq__", (PyCFunction)py_descriptor_eq, METH_VARARGS, \
170                 NULL },
171
172 static PyObject *py_token_is_sid(PyObject *self, PyObject *args)
173 {
174         PyObject *py_sid;
175         struct dom_sid *sid;
176         struct security_token *token = py_talloc_get_ptr(self);
177         if (!PyArg_ParseTuple(args, "O", &py_sid))
178                 return NULL;
179
180         sid = py_talloc_get_ptr(py_sid);
181
182         return PyBool_FromLong(security_token_is_sid(token, sid));
183 }
184
185 static PyObject *py_token_has_sid(PyObject *self, PyObject *args)
186 {
187         PyObject *py_sid;
188         struct dom_sid *sid;
189         struct security_token *token = py_talloc_get_ptr(self);
190         if (!PyArg_ParseTuple(args, "O", &py_sid))
191                 return NULL;
192
193         sid = py_talloc_get_ptr(py_sid);
194
195         return PyBool_FromLong(security_token_has_sid(token, sid));
196 }
197
198 static PyObject *py_token_is_anonymous(PyObject *self)
199 {
200         struct security_token *token = py_talloc_get_ptr(self);
201         
202         return PyBool_FromLong(security_token_is_anonymous(token));
203 }
204
205 static PyObject *py_token_is_system(PyObject *self)
206 {
207         struct security_token *token = py_talloc_get_ptr(self);
208         
209         return PyBool_FromLong(security_token_is_system(token));
210 }
211
212 static PyObject *py_token_has_builtin_administrators(PyObject *self)
213 {
214         struct security_token *token = py_talloc_get_ptr(self);
215         
216         return PyBool_FromLong(security_token_has_builtin_administrators(token));
217 }
218
219 static PyObject *py_token_has_nt_authenticated_users(PyObject *self)
220 {
221         struct security_token *token = py_talloc_get_ptr(self);
222         
223         return PyBool_FromLong(security_token_has_nt_authenticated_users(token));
224 }
225
226 static PyObject *py_token_has_privilege(PyObject *self, PyObject *args)
227 {
228         int priv;
229         struct security_token *token = py_talloc_get_ptr(self);
230
231         if (!PyArg_ParseTuple(args, "i", &priv))
232                 return NULL;
233
234         return PyBool_FromLong(security_token_has_privilege(token, priv));
235 }
236
237 static PyObject *py_token_set_privilege(PyObject *self, PyObject *args)
238 {
239         int priv;
240         struct security_token *token = py_talloc_get_ptr(self);
241
242         if (!PyArg_ParseTuple(args, "i", &priv))
243                 return NULL;
244
245         security_token_set_privilege(token, priv);
246         return Py_None;
247 }
248
249 static PyObject *py_token_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
250 {
251         return py_talloc_import(self, security_token_initialise(NULL));
252 }       
253
254 #define PY_SECURITY_TOKEN_EXTRA_METHODS \
255         { "is_sid", (PyCFunction)py_token_is_sid, METH_VARARGS, \
256                 "S.is_sid(sid) -> bool\n" \
257                 "Check whether this token is of the specified SID." }, \
258         { "has_sid", (PyCFunction)py_token_has_sid, METH_VARARGS, \
259                 NULL }, \
260         { "is_anonymous", (PyCFunction)py_token_is_anonymous, METH_NOARGS, \
261                 "S.is_anonymus() -> bool\n" \
262                 "Check whether this is an anonymous token." }, \
263         { "is_system", (PyCFunction)py_token_is_system, METH_NOARGS, \
264                 NULL }, \
265         { "has_builtin_administrators", (PyCFunction)py_token_has_builtin_administrators, METH_NOARGS, \
266                 NULL }, \
267         { "has_nt_authenticated_users", (PyCFunction)py_token_has_nt_authenticated_users, METH_NOARGS, \
268                 NULL }, \
269         { "has_privilege", (PyCFunction)py_token_has_privilege, METH_VARARGS, \
270                 NULL }, \
271         { "set_privilege", (PyCFunction)py_token_set_privilege, METH_VARARGS, \
272                 NULL },