This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[ira/wip.git] / source3 / python / py_ntsec.c
1 /* 
2    Python wrappers for DCERPC/SMB client routines.
3
4    Copyright (C) Tim Potter, 2002
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22 #include "Python.h"
23
24 #include "python/py_common_proto.h"
25
26 /* Convert a SID to a Python dict */
27
28 BOOL py_from_SID(PyObject **obj, DOM_SID *sid)
29 {
30         fstring sidstr;
31
32         if (!sid) {
33                 Py_INCREF(Py_None);
34                 *obj = Py_None;
35                 return True;
36         }
37
38         if (!sid_to_string(sidstr, sid))
39                 return False;
40
41         *obj = PyString_FromString(sidstr);
42
43         return True;
44 }
45
46 BOOL py_to_SID(DOM_SID *sid, PyObject *obj)
47 {
48         if (!PyString_Check(obj))
49                 return False;
50
51         return string_to_sid(sid, PyString_AsString(obj));
52 }
53
54 BOOL py_from_ACE(PyObject **dict, SEC_ACE *ace)
55 {
56         PyObject *obj;
57
58         if (!ace) {
59                 Py_INCREF(Py_None);
60                 *dict = Py_None;
61                 return True;
62         }
63
64         *dict = PyDict_New();
65
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));
69
70         if (py_from_SID(&obj, &ace->trustee))
71                 PyDict_SetItemString(*dict, "trustee", obj);
72
73         return True;
74 }
75
76 BOOL py_to_ACE(SEC_ACE *ace, PyObject *dict)
77 {
78         PyObject *obj;
79         uint8 ace_type, ace_flags;
80         DOM_SID trustee;
81         SEC_ACCESS sec_access;
82
83         if (!PyDict_Check(dict))
84                 return False;
85
86         if (!(obj = PyDict_GetItemString(dict, "type")) ||
87             !PyInt_Check(obj))
88                 return False;
89
90         ace_type = PyInt_AsLong(obj);
91
92         if (!(obj = PyDict_GetItemString(dict, "flags")) ||
93             !PyInt_Check(obj))
94                 return False;
95
96         ace_flags = PyInt_AsLong(obj);
97
98         if (!(obj = PyDict_GetItemString(dict, "trustee")) ||
99             !PyString_Check(obj))
100                 return False;
101
102         if (!py_to_SID(&trustee, obj))
103                 return False;
104
105         if (!(obj = PyDict_GetItemString(dict, "mask")) ||
106             !PyInt_Check(obj))
107                 return False;
108
109         sec_access.mask = PyInt_AsLong(obj);
110
111         init_sec_ace(ace, &trustee, ace_type, sec_access, ace_flags);
112
113         /* Fill in size field */
114
115         ace->size = SEC_ACE_HEADER_SIZE + sid_size(&trustee);
116
117         return True;
118 }
119
120 BOOL py_from_ACL(PyObject **dict, SEC_ACL *acl)
121 {
122         PyObject *ace_list;
123         int i;
124
125         if (!acl) {
126                 Py_INCREF(Py_None);
127                 *dict = Py_None;
128                 return True;
129         }
130
131         *dict = PyDict_New();
132
133         PyDict_SetItemString(*dict, "revision", PyInt_FromLong(acl->revision));
134
135         ace_list = PyList_New(acl->num_aces);
136
137         for (i = 0; i < acl->num_aces; i++) {
138                 PyObject *obj;
139
140                 if (py_from_ACE(&obj, &acl->ace[i]))
141                         PyList_SetItem(ace_list, i, obj);
142         }
143
144         PyDict_SetItemString(*dict, "ace_list", ace_list);
145
146         return True;
147 }
148
149 BOOL py_to_ACL(SEC_ACL *acl, PyObject *dict, TALLOC_CTX *mem_ctx)
150 {
151         PyObject *obj;
152         uint32 i;
153
154         if (!(obj = PyDict_GetItemString(dict, "revision")) ||
155             !PyInt_Check(obj))
156                 return False;
157
158         acl->revision = PyInt_AsLong(obj);
159
160         if (!(obj = PyDict_GetItemString(dict, "ace_list")) ||
161             !PyList_Check(obj)) 
162                 return False;
163         
164         acl->num_aces = PyList_Size(obj);
165
166         acl->ace = talloc(mem_ctx, acl->num_aces * sizeof(SEC_ACE));
167         acl->size = SEC_ACL_HEADER_SIZE;
168
169         for (i = 0; i < acl->num_aces; i++) {
170                 PyObject *py_ace = PyList_GetItem(obj, i);
171
172                 if (!py_to_ACE(&acl->ace[i], py_ace))
173                         return False;
174
175                 acl->size += acl->ace[i].size;
176         }
177
178         return True;
179 }
180
181 BOOL py_from_SECDESC(PyObject **dict, SEC_DESC *sd)
182 {
183         PyObject *obj;
184
185         *dict = PyDict_New();
186
187         PyDict_SetItemString(*dict, "revision", PyInt_FromLong(sd->revision));
188
189         if (py_from_SID(&obj, sd->owner_sid))
190                 PyDict_SetItemString(*dict, "owner_sid", obj);
191
192         if (py_from_SID(&obj, sd->grp_sid))
193                 PyDict_SetItemString(*dict, "group_sid", obj);
194
195         if (py_from_ACL(&obj, sd->dacl))
196                 PyDict_SetItemString(*dict, "dacl", obj);
197
198         if (py_from_ACL(&obj, sd->sacl))
199                 PyDict_SetItemString(*dict, "sacl", obj);
200
201         return True;
202 }
203
204 BOOL py_to_SECDESC(SEC_DESC **sd, PyObject *dict, TALLOC_CTX *mem_ctx)
205 {
206         PyObject *obj;
207         uint16 revision;
208         DOM_SID owner_sid, group_sid;
209         SEC_ACL sacl, dacl;
210         BOOL got_dacl = False, got_sacl = False;
211         BOOL got_owner_sid = False, got_group_sid = False;
212
213         ZERO_STRUCT(dacl); ZERO_STRUCT(sacl);
214         ZERO_STRUCT(owner_sid); ZERO_STRUCT(group_sid);
215
216         if (!(obj = PyDict_GetItemString(dict, "revision")))
217                 return False;
218
219         revision = PyInt_AsLong(obj);
220
221         if ((obj = PyDict_GetItemString(dict, "owner_sid"))) {
222
223                 if (obj != Py_None) {
224
225                         if (!py_to_SID(&owner_sid, obj))
226                                 return False;
227
228                         got_owner_sid = True;
229                 }
230         }
231
232         if ((obj = PyDict_GetItemString(dict, "group_sid"))) {
233
234                 if (obj != Py_None) {
235
236                         if (!py_to_SID(&group_sid, obj))
237                                 return False;
238                         
239                         got_group_sid = True;
240                 }
241         }
242
243         if ((obj = PyDict_GetItemString(dict, "dacl"))) {
244
245                 if (obj != Py_None) {
246
247                         if (!py_to_ACL(&dacl, obj, mem_ctx))
248                                 return False;
249                         
250                         got_dacl = True;
251                 }
252         }
253
254         if ((obj = PyDict_GetItemString(dict, "sacl"))) {
255
256                 if (obj != Py_None) {
257
258                         if (!py_to_ACL(&sacl, obj, mem_ctx))
259                                 return False;
260
261                         got_sacl = True;
262                 }
263         }
264
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);
271 #else
272         {
273                 size_t sd_size;
274
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);
280         }
281 #endif
282
283         return True;
284 }