Make sure uint32 unpacking is unsigned, and generates a Python long so
[samba.git] / source3 / python / py_spoolss_printerdata.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 "python/py_spoolss.h"
22
23 static BOOL py_from_printerdata(PyObject **dict, char *key, char *value,
24                                 uint16 data_type, uint8 *data, 
25                                 uint32 data_size) 
26 {
27         *dict = PyDict_New();
28
29         PyDict_SetItemString(*dict, "key", Py_BuildValue("s", key ? key : ""));
30         PyDict_SetItemString(*dict, "value", Py_BuildValue("s", value));
31         PyDict_SetItemString(*dict, "type", Py_BuildValue("i", data_type));
32
33         PyDict_SetItemString(*dict, "data", 
34                              Py_BuildValue("s#", data, data_size));
35
36         return True;
37 }
38
39 static BOOL py_to_printerdata(char **key, char **value, uint16 *data_type, 
40                               uint8 **data, uint32 *data_size, 
41                               PyObject *dict)
42 {
43         PyObject *obj;
44
45         if ((obj = PyDict_GetItemString(dict, "key"))) {
46
47                 if (!PyString_Check(obj)) {
48                         PyErr_SetString(spoolss_error,
49                                         "key not a string");
50                         return False;
51                 }
52
53                 if (key) {
54                         *key = PyString_AsString(obj);
55
56                         if (!key[0])
57                                 *key = NULL;
58                 }
59         } else
60                 *key = NULL;
61
62         if ((obj = PyDict_GetItemString(dict, "value"))) {
63
64                 if (!PyString_Check(obj)) {
65                         PyErr_SetString(spoolss_error,
66                                         "value not a string");
67                         return False;
68                 }
69
70                 *value = PyString_AsString(obj);
71         } else {
72                 PyErr_SetString(spoolss_error, "no value present");
73                 return False;
74         }
75
76         if ((obj = PyDict_GetItemString(dict, "type"))) {
77
78                 if (!PyInt_Check(obj)) {
79                         PyErr_SetString(spoolss_error,
80                                         "type not an integer");
81                         return False;
82                 }
83
84                 *data_type = PyInt_AsLong(obj);
85         } else {
86                 PyErr_SetString(spoolss_error, "no type present");
87                 return False;
88         }
89         
90         if ((obj = PyDict_GetItemString(dict, "data"))) {
91
92                 if (!PyString_Check(obj)) {
93                         PyErr_SetString(spoolss_error,
94                                         "data not a string");
95                         return False;
96                 }
97
98                 *data = PyString_AsString(obj);
99                 *data_size = PyString_Size(obj);
100         } else {
101                 PyErr_SetString(spoolss_error, "no data present");
102                 return False;
103         }
104
105         return True;
106 }
107
108 PyObject *spoolss_hnd_getprinterdata(PyObject *self, PyObject *args, PyObject *kw)
109 {
110         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
111         static char *kwlist[] = { "value", NULL };
112         char *valuename;
113         WERROR werror;
114         uint32 needed;
115         PyObject *result;
116         REGISTRY_VALUE value;
117
118         /* Parse parameters */
119
120         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &valuename))
121                 return NULL;
122
123         /* Call rpc function */
124
125         werror = cli_spoolss_getprinterdata(
126                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, valuename,
127                 &value);
128
129         if (W_ERROR_V(werror) == ERRmoredata) 
130                 werror = cli_spoolss_getprinterdata(
131                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, 
132                         valuename, &value);
133
134         if (!W_ERROR_IS_OK(werror)) {
135                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
136                 return NULL;
137         }
138
139         py_from_printerdata(
140                 &result, NULL, valuename, value.type, value.data_p, 
141                 value.size);
142
143         return result;
144 }
145
146 PyObject *spoolss_hnd_setprinterdata(PyObject *self, PyObject *args, PyObject *kw)
147 {
148         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
149         static char *kwlist[] = { "data", NULL };
150         PyObject *py_data;
151         char *valuename;
152         WERROR werror;
153         REGISTRY_VALUE value;
154
155         if (!PyArg_ParseTupleAndKeywords(
156                     args, kw, "O!", kwlist, &PyDict_Type, &py_data))
157                 return NULL;
158         
159         if (!py_to_printerdata(
160                     NULL, &valuename, &value.type, &value.data_p, 
161                     &value.size, py_data))
162                 return NULL;
163
164         fstrcpy(value.valuename, valuename);
165         
166         /* Call rpc function */
167
168         werror = cli_spoolss_setprinterdata(
169                 hnd->cli, hnd->mem_ctx, &hnd->pol, &value);
170
171         if (!W_ERROR_IS_OK(werror)) {
172                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
173                 return NULL;
174         }
175
176         Py_INCREF(Py_None);
177         return Py_None;
178 }
179
180 PyObject *spoolss_hnd_enumprinterdata(PyObject *self, PyObject *args, PyObject *kw)
181 {
182         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
183         static char *kwlist[] = { NULL };
184         uint32 data_needed, value_needed, ndx = 0;
185         WERROR werror;
186         PyObject *result;
187         REGISTRY_VALUE value;
188
189         if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
190                 return NULL;
191
192         /* Get max buffer sizes for value and data */
193
194         werror = cli_spoolss_enumprinterdata(
195                 hnd->cli, hnd->mem_ctx, &hnd->pol, ndx, 0, 0,
196                 &value_needed, &data_needed, NULL);
197
198         if (!W_ERROR_IS_OK(werror)) {
199                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
200                 return NULL;
201         }
202
203         /* Iterate over all printerdata */
204
205         result = PyDict_New();
206
207         while (W_ERROR_IS_OK(werror)) {
208                 PyObject *obj;
209
210                 werror = cli_spoolss_enumprinterdata(
211                         hnd->cli, hnd->mem_ctx, &hnd->pol, ndx,
212                         value_needed, data_needed, NULL, NULL, &value);
213
214                 if (py_from_printerdata(
215                             &obj, NULL, value.valuename, value.type, 
216                             value.data_p, value.size))
217                         PyDict_SetItemString(result, value.valuename, obj);
218
219                 ndx++;
220         }
221
222         return result;
223 }
224
225 PyObject *spoolss_hnd_deleteprinterdata(PyObject *self, PyObject *args, PyObject *kw)
226 {
227         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
228         static char *kwlist[] = { "value", NULL };
229         char *value;
230         WERROR werror;
231
232         /* Parse parameters */
233
234         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &value))
235                 return NULL;
236
237         /* Call rpc function */
238
239         werror = cli_spoolss_deleteprinterdata(
240                 hnd->cli, hnd->mem_ctx, &hnd->pol, value);
241
242         if (!W_ERROR_IS_OK(werror)) {
243                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
244                 return NULL;
245         }
246
247         Py_INCREF(Py_None);
248         return Py_None;
249 }
250
251 PyObject *spoolss_hnd_getprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
252 {
253         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
254         static char *kwlist[] = { "key", "value", NULL };
255         char *key, *valuename;
256         WERROR werror;
257         uint32 needed;
258         PyObject *result;
259         REGISTRY_VALUE value;
260
261         /* Parse parameters */
262
263         if (!PyArg_ParseTupleAndKeywords(args, kw, "ss", kwlist, &key, &valuename))
264                 return NULL;
265
266         /* Call rpc function */
267
268         werror = cli_spoolss_getprinterdataex(
269                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, key,
270                 valuename, &value);
271
272         if (W_ERROR_V(werror) == ERRmoredata) 
273                 werror = cli_spoolss_getprinterdataex(
274                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, key,
275                         valuename, &value);
276
277         if (!W_ERROR_IS_OK(werror)) {
278                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
279                 return NULL;
280         }
281
282         py_from_printerdata(
283                 &result, key, valuename, value.type, value.data_p, value.size);
284
285         return result;
286 }
287
288 PyObject *spoolss_hnd_setprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
289 {
290         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
291         static char *kwlist[] = { "data", NULL };
292         PyObject *py_data;
293         char *keyname, *valuename;
294         WERROR werror;
295         REGISTRY_VALUE value;
296
297         if (!PyArg_ParseTupleAndKeywords(
298                     args, kw, "O!", kwlist, &PyDict_Type, &py_data))
299                 return NULL;
300         
301         if (!py_to_printerdata(
302                     &keyname, &valuename, &value.type, &value.data_p, &value.size, py_data))
303                 return NULL;
304
305         fstrcpy(value.valuename,  valuename);
306
307         /* Call rpc function */
308
309         werror = cli_spoolss_setprinterdataex(
310                 hnd->cli, hnd->mem_ctx, &hnd->pol, keyname, &value);
311
312         if (!W_ERROR_IS_OK(werror)) {
313                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
314                 return NULL;
315         }
316
317         Py_INCREF(Py_None);
318         return Py_None;
319 }
320
321 PyObject *spoolss_hnd_enumprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
322 {
323         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
324         static char *kwlist[] = { "key", NULL };
325         uint32 needed, i;
326         char *key;
327         WERROR werror;
328         PyObject *result;
329         REGVAL_CTR ctr;
330
331         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &key))
332                 return NULL;
333
334         /* Get max buffer sizes for value and data */
335
336         werror = cli_spoolss_enumprinterdataex(
337                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, key, &ctr);
338
339         if (W_ERROR_V(werror) == ERRmoredata) 
340                 werror = cli_spoolss_enumprinterdataex(
341                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, key, 
342                         &ctr);
343
344         if (!W_ERROR_IS_OK(werror)) {
345                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
346                 return NULL;
347         }
348
349         /* Iterate over all printerdata */
350
351         result = PyDict_New();
352
353         for (i = 0; i < regval_ctr_numvals(&ctr); i++) {
354                 REGISTRY_VALUE *value;
355                 PyObject *item;
356
357                 item = PyDict_New();
358                 value = regval_ctr_specific_value(&ctr, i);
359
360                 if (py_from_printerdata(
361                             &item, key, value->valuename, value->type, 
362                             value->data_p, value->size))
363                         PyDict_SetItemString(result, value->valuename, item);
364         }
365         
366         return result;
367 }
368
369 PyObject *spoolss_hnd_deleteprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
370 {
371         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
372         static char *kwlist[] = { "key", "value", NULL };
373         char *key, *value;
374         WERROR werror;
375
376         /* Parse parameters */
377
378         if (!PyArg_ParseTupleAndKeywords(args, kw, "ss", kwlist, &key, &value))
379                 return NULL;
380
381         /* Call rpc function */
382
383         werror = cli_spoolss_deleteprinterdataex(
384                 hnd->cli, hnd->mem_ctx, &hnd->pol, key, value);
385
386         if (!W_ERROR_IS_OK(werror)) {
387                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
388                 return NULL;
389         }
390
391         Py_INCREF(Py_None);
392         return Py_None;
393 }