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.
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)
38 if (!sid_to_string(sidstr, sid))
41 *obj = PyString_FromString(sidstr);
46 BOOL py_to_SID(DOM_SID *sid, PyObject *obj)
48 if (!PyString_Check(obj))
51 return string_to_sid(sid, PyString_AsString(obj));
54 BOOL py_from_ACE(PyObject **dict, SEC_ACE *ace)
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);
76 BOOL py_to_ACE(SEC_ACE *ace, PyObject *dict)
79 uint8 ace_type, ace_flags;
81 SEC_ACCESS sec_access;
83 if (!PyDict_Check(dict))
86 if (!(obj = PyDict_GetItemString(dict, "type")) ||
90 ace_type = PyInt_AsLong(obj);
92 if (!(obj = PyDict_GetItemString(dict, "flags")) ||
96 ace_flags = PyInt_AsLong(obj);
98 if (!(obj = PyDict_GetItemString(dict, "trustee")) ||
102 if (!py_to_SID(&trustee, obj))
105 if (!(obj = PyDict_GetItemString(dict, "mask")) ||
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);
120 BOOL py_from_ACL(PyObject **dict, SEC_ACL *acl)
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++) {
140 if (py_from_ACE(&obj, &acl->ace[i]))
141 PyList_SetItem(ace_list, i, obj);
144 PyDict_SetItemString(*dict, "ace_list", ace_list);
149 BOOL py_to_ACL(SEC_ACL *acl, PyObject *dict, TALLOC_CTX *mem_ctx)
154 if (!(obj = PyDict_GetItemString(dict, "revision")) ||
158 acl->revision = PyInt_AsLong(obj);
160 if (!(obj = PyDict_GetItemString(dict, "ace_list")) ||
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))
175 acl->size += acl->ace[i].size;
181 BOOL py_from_SECDESC(PyObject **dict, SEC_DESC *sd)
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);
204 BOOL py_to_SECDESC(SEC_DESC **sd, PyObject *dict, TALLOC_CTX *mem_ctx)
208 DOM_SID owner_sid, group_sid;
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")))
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))
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))
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))
254 if ((obj = PyDict_GetItemString(dict, "sacl"))) {
256 if (obj != Py_None) {
258 if (!py_to_ACL(&sacl, obj, mem_ctx))
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);
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);