all that is left to do is to actually perform the
[Samba.git] / source / python / py_ntsec.c
blobf216d96aa8ff84d46f0b54f3a42293f1445bc6ac
1 /*
2 Python wrappers for DCERPC/SMB client routines.
4 Copyright (C) Tim Potter, 2002
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
22 #include "Python.h"
24 #include "python/py_common_proto.h"
26 /* Convert a SID to a Python dict */
28 BOOL py_from_SID(PyObject **obj, DOM_SID *sid)
30 fstring sidstr;
32 if (!sid) {
33 Py_INCREF(Py_None);
34 *obj = Py_None;
35 return True;
38 if (!sid_to_string(sidstr, sid))
39 return False;
41 *obj = PyString_FromString(sidstr);
43 return True;
46 BOOL py_to_SID(DOM_SID *sid, PyObject *obj)
48 if (!PyString_Check(obj))
49 return False;
51 return string_to_sid(sid, PyString_AsString(obj));
54 BOOL py_from_ACE(PyObject **dict, SEC_ACE *ace)
56 PyObject *obj;
58 if (!ace) {
59 Py_INCREF(Py_None);
60 *dict = Py_None;
61 return True;
64 *dict = PyDict_New();
66 PyDict_SetItemString(*dict, "type", PyInt_FromLong(ace->type));
67 PyDict_SetItemString(*dict, "flags", PyInt_FromLong(ace->flags));
68 PyDict_SetItemString(*dict, "mask", PyInt_FromLong(ace->info.mask));
70 if (py_from_SID(&obj, &ace->trustee))
71 PyDict_SetItemString(*dict, "trustee", obj);
73 return True;
76 BOOL py_to_ACE(SEC_ACE *ace, PyObject *dict)
78 PyObject *obj;
79 uint8 ace_type, ace_flags;
80 DOM_SID trustee;
81 SEC_ACCESS sec_access;
83 if (!PyDict_Check(dict))
84 return False;
86 if (!(obj = PyDict_GetItemString(dict, "type")) ||
87 !PyInt_Check(obj))
88 return False;
90 ace_type = PyInt_AsLong(obj);
92 if (!(obj = PyDict_GetItemString(dict, "flags")) ||
93 !PyInt_Check(obj))
94 return False;
96 ace_flags = PyInt_AsLong(obj);
98 if (!(obj = PyDict_GetItemString(dict, "trustee")) ||
99 !PyString_Check(obj))
100 return False;
102 if (!py_to_SID(&trustee, obj))
103 return False;
105 if (!(obj = PyDict_GetItemString(dict, "mask")) ||
106 !PyInt_Check(obj))
107 return False;
109 sec_access.mask = PyInt_AsLong(obj);
111 init_sec_ace(ace, &trustee, ace_type, sec_access, ace_flags);
113 /* Fill in size field */
115 ace->size = SEC_ACE_HEADER_SIZE + sid_size(&trustee);
117 return True;
120 BOOL py_from_ACL(PyObject **dict, SEC_ACL *acl)
122 PyObject *ace_list;
123 int i;
125 if (!acl) {
126 Py_INCREF(Py_None);
127 *dict = Py_None;
128 return True;
131 *dict = PyDict_New();
133 PyDict_SetItemString(*dict, "revision", PyInt_FromLong(acl->revision));
135 ace_list = PyList_New(acl->num_aces);
137 for (i = 0; i < acl->num_aces; i++) {
138 PyObject *obj;
140 if (py_from_ACE(&obj, &acl->ace[i]))
141 PyList_SetItem(ace_list, i, obj);
144 PyDict_SetItemString(*dict, "ace_list", ace_list);
146 return True;
149 BOOL py_to_ACL(SEC_ACL *acl, PyObject *dict, TALLOC_CTX *mem_ctx)
151 PyObject *obj;
152 uint32 i;
154 if (!(obj = PyDict_GetItemString(dict, "revision")) ||
155 !PyInt_Check(obj))
156 return False;
158 acl->revision = PyInt_AsLong(obj);
160 if (!(obj = PyDict_GetItemString(dict, "ace_list")) ||
161 !PyList_Check(obj))
162 return False;
164 acl->num_aces = PyList_Size(obj);
166 acl->ace = talloc(mem_ctx, acl->num_aces * sizeof(SEC_ACE));
167 acl->size = SEC_ACL_HEADER_SIZE;
169 for (i = 0; i < acl->num_aces; i++) {
170 PyObject *py_ace = PyList_GetItem(obj, i);
172 if (!py_to_ACE(&acl->ace[i], py_ace))
173 return False;
175 acl->size += acl->ace[i].size;
178 return True;
181 BOOL py_from_SECDESC(PyObject **dict, SEC_DESC *sd)
183 PyObject *obj;
185 *dict = PyDict_New();
187 PyDict_SetItemString(*dict, "revision", PyInt_FromLong(sd->revision));
189 if (py_from_SID(&obj, sd->owner_sid))
190 PyDict_SetItemString(*dict, "owner_sid", obj);
192 if (py_from_SID(&obj, sd->grp_sid))
193 PyDict_SetItemString(*dict, "group_sid", obj);
195 if (py_from_ACL(&obj, sd->dacl))
196 PyDict_SetItemString(*dict, "dacl", obj);
198 if (py_from_ACL(&obj, sd->sacl))
199 PyDict_SetItemString(*dict, "sacl", obj);
201 return True;
204 BOOL py_to_SECDESC(SEC_DESC **sd, PyObject *dict, TALLOC_CTX *mem_ctx)
206 PyObject *obj;
207 uint16 revision;
208 DOM_SID owner_sid, group_sid;
209 SEC_ACL sacl, dacl;
210 BOOL got_dacl = False, got_sacl = False;
211 BOOL got_owner_sid = False, got_group_sid = False;
213 ZERO_STRUCT(dacl); ZERO_STRUCT(sacl);
214 ZERO_STRUCT(owner_sid); ZERO_STRUCT(group_sid);
216 if (!(obj = PyDict_GetItemString(dict, "revision")))
217 return False;
219 revision = PyInt_AsLong(obj);
221 if ((obj = PyDict_GetItemString(dict, "owner_sid"))) {
223 if (obj != Py_None) {
225 if (!py_to_SID(&owner_sid, obj))
226 return False;
228 got_owner_sid = True;
232 if ((obj = PyDict_GetItemString(dict, "group_sid"))) {
234 if (obj != Py_None) {
236 if (!py_to_SID(&group_sid, obj))
237 return False;
239 got_group_sid = True;
243 if ((obj = PyDict_GetItemString(dict, "dacl"))) {
245 if (obj != Py_None) {
247 if (!py_to_ACL(&dacl, obj, mem_ctx))
248 return False;
250 got_dacl = True;
254 if ((obj = PyDict_GetItemString(dict, "sacl"))) {
256 if (obj != Py_None) {
258 if (!py_to_ACL(&sacl, obj, mem_ctx))
259 return False;
261 got_sacl = True;
265 #if 0 /* For new secdesc code */
266 *sd = make_sec_desc(mem_ctx, revision,
267 got_owner_sid ? &owner_sid : NULL,
268 got_group_sid ? &group_sid : NULL,
269 got_sacl ? &sacl : NULL,
270 got_dacl ? &dacl : NULL);
271 #else
273 size_t sd_size;
275 *sd = make_sec_desc(mem_ctx, revision,
276 got_owner_sid ? &owner_sid : NULL,
277 got_group_sid ? &group_sid : NULL,
278 got_sacl ? &sacl : NULL,
279 got_dacl ? &dacl : NULL, &sd_size);
281 #endif
283 return True;