Started working on setprinter code.
[jerry/samba.git] / source / 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.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         BOOL result;
49
50         if (!PyString_Check(obj))
51                 return False;
52
53         result = string_to_sid(sid, PyString_AsString(obj));
54
55         if (result)
56                 DEBUG(0, ("py: got sid %s\n", PyString_AsString(obj)));
57
58         return result;
59 }
60
61 BOOL py_from_ACE(PyObject **dict, SEC_ACE *ace)
62 {
63         PyObject *obj;
64
65         if (!ace) {
66                 Py_INCREF(Py_None);
67                 *dict = Py_None;
68                 return True;
69         }
70
71         *dict = PyDict_New();
72
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));
76
77         if (py_from_SID(&obj, &ace->trustee))
78                 PyDict_SetItemString(*dict, "trustee", obj);
79
80         return True;
81 }
82
83 BOOL py_to_ACE(SEC_ACE *ace, PyObject *dict)
84 {
85         PyObject *obj;
86         uint8 ace_type, ace_flags;
87         DOM_SID trustee;
88         SEC_ACCESS sec_access;
89
90         if (!PyDict_Check(dict))
91                 return False;
92
93         if (!(obj = PyDict_GetItemString(dict, "type")) ||
94             !PyInt_Check(obj))
95                 return False;
96
97         ace_type = PyInt_AsLong(obj);
98
99         DEBUG(0, ("py: got ace_type %d\n", ace_type));
100
101         if (!(obj = PyDict_GetItemString(dict, "flags")) ||
102             !PyInt_Check(obj))
103                 return False;
104
105         ace_flags = PyInt_AsLong(obj);
106
107         DEBUG(0, ("py: got ace_flags %d\n", ace_flags));
108
109         if (!(obj = PyDict_GetItemString(dict, "trustee")) ||
110             !PyString_Check(obj))
111                 return False;
112
113         if (!py_to_SID(&trustee, obj))
114                 return False;
115
116         DEBUG(0, ("py: got trustee\n"));
117
118         if (!(obj = PyDict_GetItemString(dict, "mask")) ||
119             !PyInt_Check(obj))
120                 return False;
121
122         sec_access.mask = PyInt_AsLong(obj);
123
124         DEBUG(0, ("py: got mask 0x%08x\n", sec_access.mask));
125
126         init_sec_ace(ace, &trustee, ace_type, sec_access, ace_flags);
127
128         return True;
129 }
130
131 BOOL py_from_ACL(PyObject **dict, SEC_ACL *acl)
132 {
133         PyObject *ace_list;
134         int i;
135
136         if (!acl) {
137                 Py_INCREF(Py_None);
138                 *dict = Py_None;
139                 return True;
140         }
141
142         *dict = PyDict_New();
143
144         PyDict_SetItemString(*dict, "revision", PyInt_FromLong(acl->revision));
145
146         ace_list = PyList_New(acl->num_aces);
147
148         for (i = 0; i < acl->num_aces; i++) {
149                 PyObject *obj;
150
151                 if (py_from_ACE(&obj, &acl->ace[i]))
152                         PyList_SetItem(ace_list, i, obj);
153         }
154
155         PyDict_SetItemString(*dict, "ace_list", ace_list);
156
157         return True;
158 }
159
160 BOOL py_to_ACL(SEC_ACL *acl, PyObject *dict, TALLOC_CTX *mem_ctx)
161 {
162         PyObject *obj;
163         uint32 i;
164
165         if (!(obj = PyDict_GetItemString(dict, "revision")) ||
166             !PyInt_Check(obj))
167                 return False;
168
169         acl->revision = PyInt_AsLong(obj);
170
171         DEBUG(0, ("py: got revision %d\n", acl->revision));
172
173         if (!(obj = PyDict_GetItemString(dict, "ace_list")) ||
174             !PyList_Check(obj)) 
175                 return False;
176         
177         acl->num_aces = PyList_Size(obj);
178
179         DEBUG(0, ("py: got num_aces %d\n", acl->num_aces));
180
181         acl->ace = talloc(mem_ctx, acl->num_aces * sizeof(SEC_ACE));
182
183         for (i = 0; i < acl->num_aces; i++) {
184                 PyObject *py_ace = PyList_GetItem(obj, i);
185
186                 if (!py_to_ACE(acl->ace, py_ace))
187                         return False;
188
189                 DEBUG(0, ("py: got ace %d\n", i));
190         }
191
192         return True;
193 }
194
195 BOOL py_from_SECDESC(PyObject **dict, SEC_DESC *sd)
196 {
197         PyObject *obj;
198
199         *dict = PyDict_New();
200
201         PyDict_SetItemString(*dict, "revision", PyInt_FromLong(sd->revision));
202
203         if (py_from_SID(&obj, sd->owner_sid))
204                 PyDict_SetItemString(*dict, "owner_sid", obj);
205
206         if (py_from_SID(&obj, sd->grp_sid))
207                 PyDict_SetItemString(*dict, "group_sid", obj);
208
209         if (py_from_ACL(&obj, sd->dacl))
210                 PyDict_SetItemString(*dict, "dacl", obj);
211
212         if (py_from_ACL(&obj, sd->sacl))
213                 PyDict_SetItemString(*dict, "sacl", obj);
214
215         return True;
216 }
217
218 BOOL py_to_SECDESC(SEC_DESC **sd, PyObject *dict, TALLOC_CTX *mem_ctx)
219 {
220         PyObject *obj;
221         uint16 revision;
222         DOM_SID owner_sid, group_sid;
223         SEC_ACL sacl, dacl;
224         size_t sd_size;
225         BOOL got_dacl = False, got_sacl = False;
226
227         ZERO_STRUCT(dacl); ZERO_STRUCT(sacl);
228         ZERO_STRUCT(owner_sid); ZERO_STRUCT(group_sid);
229
230         if (!(obj = PyDict_GetItemString(dict, "revision")))
231                 return False;
232
233         revision = PyInt_AsLong(obj);
234
235         if (!(obj = PyDict_GetItemString(dict, "owner_sid")))
236                 return False;
237
238         if (!py_to_SID(&owner_sid, obj))
239                 return False;
240
241         if (!(obj = PyDict_GetItemString(dict, "group_sid")))
242                 return False;
243
244         if (!py_to_SID(&group_sid, obj))
245                 return False;
246
247         if ((obj = PyDict_GetItemString(dict, "dacl"))) {
248
249                 if (!py_to_ACL(&dacl, obj, mem_ctx))
250                         return False;
251
252                 got_dacl = True;
253         }
254
255         DEBUG(0, ("py: got dacl\n"));
256
257         if ((obj = PyDict_GetItemString(dict, "sacl"))) {
258                 if (obj != Py_None) {
259
260                         if (!py_to_ACL(&sacl, obj, mem_ctx))
261                                 return False;
262
263                         got_sacl = True;
264                 }
265         }
266
267         DEBUG(0, ("py: got sacl\n"));
268
269         *sd = make_sec_desc(mem_ctx, revision, &owner_sid, &group_sid,
270                             got_sacl ? &sacl : NULL, 
271                             got_dacl ? &dacl : NULL, &sd_size);
272         
273         return True;
274 }