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 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 "python/py_common.h"
22 /* Convert a SID to a Python dict */
24 BOOL py_from_SID(PyObject **obj, DOM_SID *sid)
34 if (!sid_to_string(sidstr, sid))
37 *obj = PyString_FromString(sidstr);
42 BOOL py_to_SID(DOM_SID *sid, PyObject *obj)
44 if (!PyString_Check(obj))
47 return string_to_sid(sid, PyString_AsString(obj));
50 BOOL py_from_ACE(PyObject **dict, SEC_ACE *ace)
60 *dict = Py_BuildValue("{sisisi}", "type", ace->type,
62 "mask", ace->access_mask);
64 if (py_from_SID(&obj, &ace->trustee)) {
65 PyDict_SetItemString(*dict, "trustee", obj);
72 BOOL py_to_ACE(SEC_ACE *ace, PyObject *dict)
75 uint8 ace_type, ace_flags;
77 SEC_ACCESS sec_access;
79 if (!PyDict_Check(dict))
82 if (!(obj = PyDict_GetItemString(dict, "type")) ||
86 ace_type = PyInt_AsLong(obj);
88 if (!(obj = PyDict_GetItemString(dict, "flags")) ||
92 ace_flags = PyInt_AsLong(obj);
94 if (!(obj = PyDict_GetItemString(dict, "trustee")) ||
98 if (!py_to_SID(&trustee, obj))
101 if (!(obj = PyDict_GetItemString(dict, "mask")) ||
105 sec_access = PyInt_AsLong(obj);
107 init_sec_ace(ace, &trustee, ace_type, sec_access, ace_flags);
109 /* Fill in size field */
111 ace->size = SEC_ACE_HEADER_SIZE + sid_size(&trustee);
116 BOOL py_from_ACL(PyObject **dict, SEC_ACL *acl)
127 ace_list = PyList_New(acl->num_aces);
129 for (i = 0; i < acl->num_aces; i++) {
132 if (py_from_ACE(&obj, &acl->aces[i]))
133 PyList_SetItem(ace_list, i, obj);
136 *dict = Py_BuildValue("{sisN}", "revision", acl->revision,
137 "ace_list", ace_list);
142 BOOL py_to_ACL(SEC_ACL *acl, PyObject *dict, TALLOC_CTX *mem_ctx)
147 if (!(obj = PyDict_GetItemString(dict, "revision")) ||
151 acl->revision = PyInt_AsLong(obj);
153 if (!(obj = PyDict_GetItemString(dict, "ace_list")) ||
157 acl->num_aces = PyList_Size(obj);
159 acl->aces = TALLOC_ARRAY(mem_ctx, struct security_ace, acl->num_aces);
160 acl->size = SEC_ACL_HEADER_SIZE;
162 for (i = 0; i < acl->num_aces; i++) {
163 PyObject *py_ace = PyList_GetItem(obj, i);
165 if (!py_to_ACE(&acl->aces[i], py_ace))
168 acl->size += acl->aces[i].size;
174 BOOL py_from_SECDESC(PyObject **dict, SEC_DESC *sd)
178 *dict = PyDict_New();
180 obj = PyInt_FromLong(sd->revision);
181 PyDict_SetItemString(*dict, "revision", obj);
184 obj = PyInt_FromLong(sd->type);
185 PyDict_SetItemString(*dict, "type", obj);
188 if (py_from_SID(&obj, sd->owner_sid)) {
189 PyDict_SetItemString(*dict, "owner_sid", obj);
193 if (py_from_SID(&obj, sd->group_sid)) {
194 PyDict_SetItemString(*dict, "group_sid", obj);
198 if (py_from_ACL(&obj, sd->dacl)) {
199 PyDict_SetItemString(*dict, "dacl", obj);
203 if (py_from_ACL(&obj, sd->sacl)) {
204 PyDict_SetItemString(*dict, "sacl", obj);
211 BOOL py_to_SECDESC(SEC_DESC **sd, PyObject *dict, TALLOC_CTX *mem_ctx)
215 uint16 type = SEC_DESC_SELF_RELATIVE;
216 DOM_SID owner_sid, group_sid;
218 BOOL got_dacl = False, got_sacl = False;
219 BOOL got_owner_sid = False, got_group_sid = False;
221 ZERO_STRUCT(dacl); ZERO_STRUCT(sacl);
222 ZERO_STRUCT(owner_sid); ZERO_STRUCT(group_sid);
224 if (!(obj = PyDict_GetItemString(dict, "revision")))
227 revision = PyInt_AsLong(obj);
229 if ((obj = PyDict_GetItemString(dict, "type"))) {
230 if (obj != Py_None) {
231 type = PyInt_AsLong(obj);
235 if ((obj = PyDict_GetItemString(dict, "owner_sid"))) {
237 if (obj != Py_None) {
239 if (!py_to_SID(&owner_sid, obj))
242 got_owner_sid = True;
246 if ((obj = PyDict_GetItemString(dict, "group_sid"))) {
248 if (obj != Py_None) {
250 if (!py_to_SID(&group_sid, obj))
253 got_group_sid = True;
257 if ((obj = PyDict_GetItemString(dict, "dacl"))) {
259 if (obj != Py_None) {
261 if (!py_to_ACL(&dacl, obj, mem_ctx))
268 if ((obj = PyDict_GetItemString(dict, "sacl"))) {
270 if (obj != Py_None) {
272 if (!py_to_ACL(&sacl, obj, mem_ctx))
279 #if 0 /* For new secdesc code */
280 *sd = make_sec_desc(mem_ctx, revision,
281 got_owner_sid ? &owner_sid : NULL,
282 got_group_sid ? &group_sid : NULL,
283 got_sacl ? &sacl : NULL,
284 got_dacl ? &dacl : NULL);
289 *sd = make_sec_desc(mem_ctx, revision, type,
290 got_owner_sid ? &owner_sid : NULL,
291 got_group_sid ? &group_sid : NULL,
292 got_sacl ? &sacl : NULL,
293 got_dacl ? &dacl : NULL, &sd_size);