Apply the changes that Derrell Lipman supplied ...
[Samba/gebeck_regimport.git] / source3 / python / py_ntsec.c
blob5ce5e8fc1bed18b816d1772486c947fdb81d4bd2
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 "python/py_common.h"
23 /* Convert a SID to a Python dict */
25 BOOL py_from_SID(PyObject **obj, DOM_SID *sid)
27 fstring sidstr;
29 if (!sid) {
30 Py_INCREF(Py_None);
31 *obj = Py_None;
32 return True;
35 if (!sid_to_string(sidstr, sid))
36 return False;
38 *obj = PyString_FromString(sidstr);
40 return True;
43 BOOL py_to_SID(DOM_SID *sid, PyObject *obj)
45 if (!PyString_Check(obj))
46 return False;
48 return string_to_sid(sid, PyString_AsString(obj));
51 BOOL py_from_ACE(PyObject **dict, SEC_ACE *ace)
53 PyObject *obj;
55 if (!ace) {
56 Py_INCREF(Py_None);
57 *dict = Py_None;
58 return True;
61 *dict = Py_BuildValue("{sisisi}", "type", ace->type,
62 "flags", ace->flags,
63 "mask", ace->info.mask);
65 if (py_from_SID(&obj, &ace->trustee)) {
66 PyDict_SetItemString(*dict, "trustee", obj);
67 Py_DECREF(obj);
70 return True;
73 BOOL py_to_ACE(SEC_ACE *ace, PyObject *dict)
75 PyObject *obj;
76 uint8 ace_type, ace_flags;
77 DOM_SID trustee;
78 SEC_ACCESS sec_access;
80 if (!PyDict_Check(dict))
81 return False;
83 if (!(obj = PyDict_GetItemString(dict, "type")) ||
84 !PyInt_Check(obj))
85 return False;
87 ace_type = PyInt_AsLong(obj);
89 if (!(obj = PyDict_GetItemString(dict, "flags")) ||
90 !PyInt_Check(obj))
91 return False;
93 ace_flags = PyInt_AsLong(obj);
95 if (!(obj = PyDict_GetItemString(dict, "trustee")) ||
96 !PyString_Check(obj))
97 return False;
99 if (!py_to_SID(&trustee, obj))
100 return False;
102 if (!(obj = PyDict_GetItemString(dict, "mask")) ||
103 !PyInt_Check(obj))
104 return False;
106 sec_access.mask = PyInt_AsLong(obj);
108 init_sec_ace(ace, &trustee, ace_type, sec_access, ace_flags);
110 /* Fill in size field */
112 ace->size = SEC_ACE_HEADER_SIZE + sid_size(&trustee);
114 return True;
117 BOOL py_from_ACL(PyObject **dict, SEC_ACL *acl)
119 PyObject *ace_list;
120 int i;
122 if (!acl) {
123 Py_INCREF(Py_None);
124 *dict = Py_None;
125 return True;
128 ace_list = PyList_New(acl->num_aces);
130 for (i = 0; i < acl->num_aces; i++) {
131 PyObject *obj;
133 if (py_from_ACE(&obj, &acl->ace[i]))
134 PyList_SetItem(ace_list, i, obj);
137 *dict = Py_BuildValue("{sisN}", "revision", acl->revision,
138 "ace_list", ace_list);
140 return True;
143 BOOL py_to_ACL(SEC_ACL *acl, PyObject *dict, TALLOC_CTX *mem_ctx)
145 PyObject *obj;
146 uint32 i;
148 if (!(obj = PyDict_GetItemString(dict, "revision")) ||
149 !PyInt_Check(obj))
150 return False;
152 acl->revision = PyInt_AsLong(obj);
154 if (!(obj = PyDict_GetItemString(dict, "ace_list")) ||
155 !PyList_Check(obj))
156 return False;
158 acl->num_aces = PyList_Size(obj);
160 acl->ace = talloc(mem_ctx, acl->num_aces * sizeof(SEC_ACE));
161 acl->size = SEC_ACL_HEADER_SIZE;
163 for (i = 0; i < acl->num_aces; i++) {
164 PyObject *py_ace = PyList_GetItem(obj, i);
166 if (!py_to_ACE(&acl->ace[i], py_ace))
167 return False;
169 acl->size += acl->ace[i].size;
172 return True;
175 BOOL py_from_SECDESC(PyObject **dict, SEC_DESC *sd)
177 PyObject *obj;
179 *dict = PyDict_New();
181 obj = PyInt_FromLong(sd->revision);
182 PyDict_SetItemString(*dict, "revision", obj);
183 Py_DECREF(obj);
185 if (py_from_SID(&obj, sd->owner_sid)) {
186 PyDict_SetItemString(*dict, "owner_sid", obj);
187 Py_DECREF(obj);
190 if (py_from_SID(&obj, sd->grp_sid)) {
191 PyDict_SetItemString(*dict, "group_sid", obj);
192 Py_DECREF(obj);
195 if (py_from_ACL(&obj, sd->dacl)) {
196 PyDict_SetItemString(*dict, "dacl", obj);
197 Py_DECREF(obj);
200 if (py_from_ACL(&obj, sd->sacl)) {
201 PyDict_SetItemString(*dict, "sacl", obj);
202 Py_DECREF(obj);
205 return True;
208 BOOL py_to_SECDESC(SEC_DESC **sd, PyObject *dict, TALLOC_CTX *mem_ctx)
210 PyObject *obj;
211 uint16 revision;
212 DOM_SID owner_sid, group_sid;
213 SEC_ACL sacl, dacl;
214 BOOL got_dacl = False, got_sacl = False;
215 BOOL got_owner_sid = False, got_group_sid = False;
217 ZERO_STRUCT(dacl); ZERO_STRUCT(sacl);
218 ZERO_STRUCT(owner_sid); ZERO_STRUCT(group_sid);
220 if (!(obj = PyDict_GetItemString(dict, "revision")))
221 return False;
223 revision = PyInt_AsLong(obj);
225 if ((obj = PyDict_GetItemString(dict, "owner_sid"))) {
227 if (obj != Py_None) {
229 if (!py_to_SID(&owner_sid, obj))
230 return False;
232 got_owner_sid = True;
236 if ((obj = PyDict_GetItemString(dict, "group_sid"))) {
238 if (obj != Py_None) {
240 if (!py_to_SID(&group_sid, obj))
241 return False;
243 got_group_sid = True;
247 if ((obj = PyDict_GetItemString(dict, "dacl"))) {
249 if (obj != Py_None) {
251 if (!py_to_ACL(&dacl, obj, mem_ctx))
252 return False;
254 got_dacl = True;
258 if ((obj = PyDict_GetItemString(dict, "sacl"))) {
260 if (obj != Py_None) {
262 if (!py_to_ACL(&sacl, obj, mem_ctx))
263 return False;
265 got_sacl = True;
269 #if 0 /* For new secdesc code */
270 *sd = make_sec_desc(mem_ctx, revision,
271 got_owner_sid ? &owner_sid : NULL,
272 got_group_sid ? &group_sid : NULL,
273 got_sacl ? &sacl : NULL,
274 got_dacl ? &dacl : NULL);
275 #else
277 size_t sd_size;
279 *sd = make_sec_desc(mem_ctx, revision, SEC_DESC_SELF_RELATIVE,
280 got_owner_sid ? &owner_sid : NULL,
281 got_group_sid ? &group_sid : NULL,
282 got_sacl ? &sacl : NULL,
283 got_dacl ? &dacl : NULL, &sd_size);
285 #endif
287 return True;