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 result = string_to_sid(sid, PyString_AsString(obj));
56 DEBUG(0, ("py: got sid %s\n", PyString_AsString(obj)));
61 BOOL py_from_ACE(PyObject **dict, SEC_ACE *ace)
73 PyDict_SetItemString(*dict, "type", PyInt_FromLong(ace->type));
74 PyDict_SetItemString(*dict, "flags", PyInt_FromLong(ace->flags));
75 PyDict_SetItemString(*dict, "mask", PyInt_FromLong(ace->info.mask));
77 if (py_from_SID(&obj, &ace->trustee))
78 PyDict_SetItemString(*dict, "trustee", obj);
83 BOOL py_to_ACE(SEC_ACE *ace, PyObject *dict)
86 uint8 ace_type, ace_flags;
88 SEC_ACCESS sec_access;
90 if (!PyDict_Check(dict))
93 if (!(obj = PyDict_GetItemString(dict, "type")) ||
97 ace_type = PyInt_AsLong(obj);
99 DEBUG(0, ("py: got ace_type %d\n", ace_type));
101 if (!(obj = PyDict_GetItemString(dict, "flags")) ||
105 ace_flags = PyInt_AsLong(obj);
107 DEBUG(0, ("py: got ace_flags %d\n", ace_flags));
109 if (!(obj = PyDict_GetItemString(dict, "trustee")) ||
110 !PyString_Check(obj))
113 if (!py_to_SID(&trustee, obj))
116 DEBUG(0, ("py: got trustee\n"));
118 if (!(obj = PyDict_GetItemString(dict, "mask")) ||
122 sec_access.mask = PyInt_AsLong(obj);
124 DEBUG(0, ("py: got mask 0x%08x\n", sec_access.mask));
126 init_sec_ace(ace, &trustee, ace_type, sec_access, ace_flags);
131 BOOL py_from_ACL(PyObject **dict, SEC_ACL *acl)
142 *dict = PyDict_New();
144 PyDict_SetItemString(*dict, "revision", PyInt_FromLong(acl->revision));
146 ace_list = PyList_New(acl->num_aces);
148 for (i = 0; i < acl->num_aces; i++) {
151 if (py_from_ACE(&obj, &acl->ace[i]))
152 PyList_SetItem(ace_list, i, obj);
155 PyDict_SetItemString(*dict, "ace_list", ace_list);
160 BOOL py_to_ACL(SEC_ACL *acl, PyObject *dict, TALLOC_CTX *mem_ctx)
165 if (!(obj = PyDict_GetItemString(dict, "revision")) ||
169 acl->revision = PyInt_AsLong(obj);
171 DEBUG(0, ("py: got revision %d\n", acl->revision));
173 if (!(obj = PyDict_GetItemString(dict, "ace_list")) ||
177 acl->num_aces = PyList_Size(obj);
179 DEBUG(0, ("py: got num_aces %d\n", acl->num_aces));
181 acl->ace = talloc(mem_ctx, acl->num_aces * sizeof(SEC_ACE));
183 for (i = 0; i < acl->num_aces; i++) {
184 PyObject *py_ace = PyList_GetItem(obj, i);
186 if (!py_to_ACE(acl->ace, py_ace))
189 DEBUG(0, ("py: got ace %d\n", i));
195 BOOL py_from_SECDESC(PyObject **dict, SEC_DESC *sd)
199 *dict = PyDict_New();
201 PyDict_SetItemString(*dict, "revision", PyInt_FromLong(sd->revision));
203 if (py_from_SID(&obj, sd->owner_sid))
204 PyDict_SetItemString(*dict, "owner_sid", obj);
206 if (py_from_SID(&obj, sd->grp_sid))
207 PyDict_SetItemString(*dict, "group_sid", obj);
209 if (py_from_ACL(&obj, sd->dacl))
210 PyDict_SetItemString(*dict, "dacl", obj);
212 if (py_from_ACL(&obj, sd->sacl))
213 PyDict_SetItemString(*dict, "sacl", obj);
218 BOOL py_to_SECDESC(SEC_DESC **sd, PyObject *dict, TALLOC_CTX *mem_ctx)
222 DOM_SID owner_sid, group_sid;
225 BOOL got_dacl = False, got_sacl = False;
227 ZERO_STRUCT(dacl); ZERO_STRUCT(sacl);
228 ZERO_STRUCT(owner_sid); ZERO_STRUCT(group_sid);
230 if (!(obj = PyDict_GetItemString(dict, "revision")))
233 revision = PyInt_AsLong(obj);
235 if (!(obj = PyDict_GetItemString(dict, "owner_sid")))
238 if (!py_to_SID(&owner_sid, obj))
241 if (!(obj = PyDict_GetItemString(dict, "group_sid")))
244 if (!py_to_SID(&group_sid, obj))
247 if ((obj = PyDict_GetItemString(dict, "dacl"))) {
249 if (!py_to_ACL(&dacl, obj, mem_ctx))
255 DEBUG(0, ("py: got dacl\n"));
257 if ((obj = PyDict_GetItemString(dict, "sacl"))) {
258 if (obj != Py_None) {
260 if (!py_to_ACL(&sacl, obj, mem_ctx))
267 DEBUG(0, ("py: got sacl\n"));
269 *sd = make_sec_desc(mem_ctx, revision, &owner_sid, &group_sid,
270 got_sacl ? &sacl : NULL,
271 got_dacl ? &dacl : NULL, &sd_size);