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.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)
50 if (!PyString_Check(obj))
53 return string_to_sid(sid, PyString_AsString(obj));
56 BOOL py_from_ACE(PyObject **dict, SEC_ACE *ace)
68 PyDict_SetItemString(*dict, "type", PyInt_FromLong(ace->type));
69 PyDict_SetItemString(*dict, "flags", PyInt_FromLong(ace->flags));
70 PyDict_SetItemString(*dict, "mask", PyInt_FromLong(ace->info.mask));
72 if (py_from_SID(&obj, &ace->trustee))
73 PyDict_SetItemString(*dict, "trustee", obj);
78 BOOL py_to_ACE(SEC_ACE *ace, PyObject *dict)
81 uint8 ace_type, ace_flags;
83 SEC_ACCESS sec_access;
85 if (!PyDict_Check(dict))
88 if (!(obj = PyDict_GetItemString(dict, "type")) ||
92 ace_type = PyInt_AsLong(obj);
94 if (!(obj = PyDict_GetItemString(dict, "flags")) ||
98 ace_flags = PyInt_AsLong(obj);
100 if (!(obj = PyDict_GetItemString(dict, "trustee")) ||
101 !PyString_Check(obj))
104 if (!py_to_SID(&trustee, obj))
107 if (!(obj = PyDict_GetItemString(dict, "mask")) ||
111 sec_access.mask = PyInt_AsLong(obj);
113 init_sec_ace(ace, &trustee, ace_type, sec_access, ace_flags);
115 /* Fill in size field */
117 ace->size = SEC_ACE_HEADER_SIZE + sid_size(&trustee);
122 BOOL py_from_ACL(PyObject **dict, SEC_ACL *acl)
133 *dict = PyDict_New();
135 PyDict_SetItemString(*dict, "revision", PyInt_FromLong(acl->revision));
137 ace_list = PyList_New(acl->num_aces);
139 for (i = 0; i < acl->num_aces; i++) {
142 if (py_from_ACE(&obj, &acl->ace[i]))
143 PyList_SetItem(ace_list, i, obj);
146 PyDict_SetItemString(*dict, "ace_list", ace_list);
151 BOOL py_to_ACL(SEC_ACL *acl, PyObject *dict, TALLOC_CTX *mem_ctx)
156 if (!(obj = PyDict_GetItemString(dict, "revision")) ||
160 acl->revision = PyInt_AsLong(obj);
162 if (!(obj = PyDict_GetItemString(dict, "ace_list")) ||
166 acl->num_aces = PyList_Size(obj);
168 acl->ace = talloc(mem_ctx, acl->num_aces * sizeof(SEC_ACE));
169 acl->size = SEC_ACL_HEADER_SIZE;
171 for (i = 0; i < acl->num_aces; i++) {
172 PyObject *py_ace = PyList_GetItem(obj, i);
174 if (!py_to_ACE(&acl->ace[i], py_ace))
177 acl->size += acl->ace[i].size;
183 BOOL py_from_SECDESC(PyObject **dict, SEC_DESC *sd)
187 *dict = PyDict_New();
189 PyDict_SetItemString(*dict, "revision", PyInt_FromLong(sd->revision));
191 if (py_from_SID(&obj, sd->owner_sid))
192 PyDict_SetItemString(*dict, "owner_sid", obj);
194 if (py_from_SID(&obj, sd->grp_sid))
195 PyDict_SetItemString(*dict, "group_sid", obj);
197 if (py_from_ACL(&obj, sd->dacl))
198 PyDict_SetItemString(*dict, "dacl", obj);
200 if (py_from_ACL(&obj, sd->sacl))
201 PyDict_SetItemString(*dict, "sacl", obj);
206 BOOL py_to_SECDESC(SEC_DESC **sd, PyObject *dict, TALLOC_CTX *mem_ctx)
210 DOM_SID owner_sid, group_sid;
212 BOOL got_dacl = False, got_sacl = False;
213 BOOL got_owner_sid = False, got_group_sid = False;
215 ZERO_STRUCT(dacl); ZERO_STRUCT(sacl);
216 ZERO_STRUCT(owner_sid); ZERO_STRUCT(group_sid);
218 if (!(obj = PyDict_GetItemString(dict, "revision")))
221 revision = PyInt_AsLong(obj);
223 if ((obj = PyDict_GetItemString(dict, "owner_sid"))) {
225 if (obj != Py_None) {
227 if (!py_to_SID(&owner_sid, obj))
230 got_owner_sid = True;
234 if ((obj = PyDict_GetItemString(dict, "group_sid"))) {
236 if (obj != Py_None) {
238 if (!py_to_SID(&group_sid, obj))
241 got_group_sid = True;
245 if ((obj = PyDict_GetItemString(dict, "dacl"))) {
247 if (obj != Py_None) {
249 if (!py_to_ACL(&dacl, obj, mem_ctx))
256 if ((obj = PyDict_GetItemString(dict, "sacl"))) {
258 if (obj != Py_None) {
260 if (!py_to_ACL(&sacl, obj, mem_ctx))
267 *sd = make_sec_desc(mem_ctx, revision,
268 got_owner_sid ? &owner_sid : NULL,
269 got_group_sid ? &group_sid : NULL,
270 got_sacl ? &sacl : NULL,
271 got_dacl ? &dacl : NULL);