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
)
35 if (!sid_to_string(sidstr
, sid
))
38 *obj
= PyString_FromString(sidstr
);
43 BOOL
py_to_SID(DOM_SID
*sid
, PyObject
*obj
)
45 if (!PyString_Check(obj
))
48 return string_to_sid(sid
, PyString_AsString(obj
));
51 BOOL
py_from_ACE(PyObject
**dict
, SEC_ACE
*ace
)
63 PyDict_SetItemString(*dict
, "type", PyInt_FromLong(ace
->type
));
64 PyDict_SetItemString(*dict
, "flags", PyInt_FromLong(ace
->flags
));
65 PyDict_SetItemString(*dict
, "mask", PyInt_FromLong(ace
->info
.mask
));
67 if (py_from_SID(&obj
, &ace
->trustee
))
68 PyDict_SetItemString(*dict
, "trustee", obj
);
73 BOOL
py_to_ACE(SEC_ACE
*ace
, PyObject
*dict
)
76 uint8 ace_type
, ace_flags
;
78 SEC_ACCESS sec_access
;
80 if (!PyDict_Check(dict
))
83 if (!(obj
= PyDict_GetItemString(dict
, "type")) ||
87 ace_type
= PyInt_AsLong(obj
);
89 if (!(obj
= PyDict_GetItemString(dict
, "flags")) ||
93 ace_flags
= PyInt_AsLong(obj
);
95 if (!(obj
= PyDict_GetItemString(dict
, "trustee")) ||
99 if (!py_to_SID(&trustee
, obj
))
102 if (!(obj
= PyDict_GetItemString(dict
, "mask")) ||
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
);
117 BOOL
py_from_ACL(PyObject
**dict
, SEC_ACL
*acl
)
128 *dict
= PyDict_New();
130 PyDict_SetItemString(*dict
, "revision", PyInt_FromLong(acl
->revision
));
132 ace_list
= PyList_New(acl
->num_aces
);
134 for (i
= 0; i
< acl
->num_aces
; i
++) {
137 if (py_from_ACE(&obj
, &acl
->ace
[i
]))
138 PyList_SetItem(ace_list
, i
, obj
);
141 PyDict_SetItemString(*dict
, "ace_list", ace_list
);
146 BOOL
py_to_ACL(SEC_ACL
*acl
, PyObject
*dict
, TALLOC_CTX
*mem_ctx
)
151 if (!(obj
= PyDict_GetItemString(dict
, "revision")) ||
155 acl
->revision
= PyInt_AsLong(obj
);
157 if (!(obj
= PyDict_GetItemString(dict
, "ace_list")) ||
161 acl
->num_aces
= PyList_Size(obj
);
163 acl
->ace
= talloc(mem_ctx
, acl
->num_aces
* sizeof(SEC_ACE
));
164 acl
->size
= SEC_ACL_HEADER_SIZE
;
166 for (i
= 0; i
< acl
->num_aces
; i
++) {
167 PyObject
*py_ace
= PyList_GetItem(obj
, i
);
169 if (!py_to_ACE(&acl
->ace
[i
], py_ace
))
172 acl
->size
+= acl
->ace
[i
].size
;
178 BOOL
py_from_SECDESC(PyObject
**dict
, SEC_DESC
*sd
)
182 *dict
= PyDict_New();
184 PyDict_SetItemString(*dict
, "revision", PyInt_FromLong(sd
->revision
));
186 if (py_from_SID(&obj
, sd
->owner_sid
))
187 PyDict_SetItemString(*dict
, "owner_sid", obj
);
189 if (py_from_SID(&obj
, sd
->grp_sid
))
190 PyDict_SetItemString(*dict
, "group_sid", obj
);
192 if (py_from_ACL(&obj
, sd
->dacl
))
193 PyDict_SetItemString(*dict
, "dacl", obj
);
195 if (py_from_ACL(&obj
, sd
->sacl
))
196 PyDict_SetItemString(*dict
, "sacl", obj
);
201 BOOL
py_to_SECDESC(SEC_DESC
**sd
, PyObject
*dict
, TALLOC_CTX
*mem_ctx
)
205 DOM_SID owner_sid
, group_sid
;
207 BOOL got_dacl
= False
, got_sacl
= False
;
208 BOOL got_owner_sid
= False
, got_group_sid
= False
;
210 ZERO_STRUCT(dacl
); ZERO_STRUCT(sacl
);
211 ZERO_STRUCT(owner_sid
); ZERO_STRUCT(group_sid
);
213 if (!(obj
= PyDict_GetItemString(dict
, "revision")))
216 revision
= PyInt_AsLong(obj
);
218 if ((obj
= PyDict_GetItemString(dict
, "owner_sid"))) {
220 if (obj
!= Py_None
) {
222 if (!py_to_SID(&owner_sid
, obj
))
225 got_owner_sid
= True
;
229 if ((obj
= PyDict_GetItemString(dict
, "group_sid"))) {
231 if (obj
!= Py_None
) {
233 if (!py_to_SID(&group_sid
, obj
))
236 got_group_sid
= True
;
240 if ((obj
= PyDict_GetItemString(dict
, "dacl"))) {
242 if (obj
!= Py_None
) {
244 if (!py_to_ACL(&dacl
, obj
, mem_ctx
))
251 if ((obj
= PyDict_GetItemString(dict
, "sacl"))) {
253 if (obj
!= Py_None
) {
255 if (!py_to_ACL(&sacl
, obj
, mem_ctx
))
262 #if 0 /* For new secdesc code */
263 *sd
= make_sec_desc(mem_ctx
, revision
,
264 got_owner_sid
? &owner_sid
: NULL
,
265 got_group_sid
? &group_sid
: NULL
,
266 got_sacl
? &sacl
: NULL
,
267 got_dacl
? &dacl
: NULL
);
272 *sd
= make_sec_desc(mem_ctx
, revision
,
273 got_owner_sid
? &owner_sid
: NULL
,
274 got_group_sid
? &group_sid
: NULL
,
275 got_sacl
? &sacl
: NULL
,
276 got_dacl
? &dacl
: NULL
, &sd_size
);