r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[samba.git] / source / 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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "python/py_spoolss.h"
21 #include "python/py_conv.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         PyObject *result;
115         REGISTRY_VALUE value;
116
117         /* Parse parameters */
118
119         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &valuename))
120                 return NULL;
121
122         /* Call rpc function */
123
124         werror = rpccli_spoolss_getprinterdata(
125                 hnd->cli, hnd->mem_ctx, &hnd->pol, valuename,
126                 &value);
127
128         if (!W_ERROR_IS_OK(werror)) {
129                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
130                 return NULL;
131         }
132
133         py_from_printerdata(
134                 &result, NULL, valuename, value.type, value.data_p, 
135                 value.size);
136
137         return result;
138 }
139
140 PyObject *spoolss_hnd_setprinterdata(PyObject *self, PyObject *args, PyObject *kw)
141 {
142         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
143         static char *kwlist[] = { "data", NULL };
144         PyObject *py_data;
145         char *valuename;
146         WERROR werror;
147         REGISTRY_VALUE value;
148
149         if (!PyArg_ParseTupleAndKeywords(
150                     args, kw, "O!", kwlist, &PyDict_Type, &py_data))
151                 return NULL;
152         
153         if (!py_to_printerdata(
154                     NULL, &valuename, &value.type, &value.data_p, 
155                     &value.size, py_data))
156                 return NULL;
157
158         fstrcpy(value.valuename, valuename);
159         
160         /* Call rpc function */
161
162         werror = rpccli_spoolss_setprinterdata(
163                 hnd->cli, hnd->mem_ctx, &hnd->pol, &value);
164
165         if (!W_ERROR_IS_OK(werror)) {
166                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
167                 return NULL;
168         }
169
170         Py_INCREF(Py_None);
171         return Py_None;
172 }
173
174 PyObject *spoolss_hnd_enumprinterdata(PyObject *self, PyObject *args, PyObject *kw)
175 {
176         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
177         static char *kwlist[] = { NULL };
178         uint32 data_needed, value_needed, ndx = 0;
179         WERROR werror;
180         PyObject *result;
181         REGISTRY_VALUE value;
182
183         if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
184                 return NULL;
185
186         /* Get max buffer sizes for value and data */
187
188         werror = rpccli_spoolss_enumprinterdata(
189                 hnd->cli, hnd->mem_ctx, &hnd->pol, ndx, 0, 0,
190                 &value_needed, &data_needed, NULL);
191
192         if (!W_ERROR_IS_OK(werror)) {
193                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
194                 return NULL;
195         }
196
197         /* Iterate over all printerdata */
198
199         result = PyDict_New();
200
201         while (W_ERROR_IS_OK(werror)) {
202                 PyObject *obj;
203
204                 werror = rpccli_spoolss_enumprinterdata(
205                         hnd->cli, hnd->mem_ctx, &hnd->pol, ndx,
206                         value_needed, data_needed, NULL, NULL, &value);
207
208                 if (py_from_printerdata(
209                             &obj, NULL, value.valuename, value.type, 
210                             value.data_p, value.size))
211                         PyDict_SetItemString(result, value.valuename, obj);
212
213                 ndx++;
214         }
215
216         return result;
217 }
218
219 PyObject *spoolss_hnd_deleteprinterdata(PyObject *self, PyObject *args, PyObject *kw)
220 {
221         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
222         static char *kwlist[] = { "value", NULL };
223         char *value;
224         WERROR werror;
225
226         /* Parse parameters */
227
228         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &value))
229                 return NULL;
230
231         /* Call rpc function */
232
233         werror = rpccli_spoolss_deleteprinterdata(
234                 hnd->cli, hnd->mem_ctx, &hnd->pol, value);
235
236         if (!W_ERROR_IS_OK(werror)) {
237                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
238                 return NULL;
239         }
240
241         Py_INCREF(Py_None);
242         return Py_None;
243 }
244
245 PyObject *spoolss_hnd_getprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
246 {
247         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
248         static char *kwlist[] = { "key", "value", NULL };
249         char *key, *valuename;
250         WERROR werror;
251         PyObject *result;
252         REGISTRY_VALUE value;
253
254         /* Parse parameters */
255
256         if (!PyArg_ParseTupleAndKeywords(args, kw, "ss", kwlist, &key, &valuename))
257                 return NULL;
258
259         /* Call rpc function */
260
261         werror = rpccli_spoolss_getprinterdataex(
262                 hnd->cli, hnd->mem_ctx, &hnd->pol, key,
263                 valuename, &value);
264
265         if (!W_ERROR_IS_OK(werror)) {
266                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
267                 return NULL;
268         }
269
270         py_from_printerdata(
271                 &result, key, valuename, value.type, value.data_p, value.size);
272
273         return result;
274 }
275
276 PyObject *spoolss_hnd_setprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
277 {
278         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
279         static char *kwlist[] = { "data", NULL };
280         PyObject *py_data;
281         char *keyname, *valuename;
282         WERROR werror;
283         REGISTRY_VALUE value;
284
285         if (!PyArg_ParseTupleAndKeywords(
286                     args, kw, "O!", kwlist, &PyDict_Type, &py_data))
287                 return NULL;
288         
289         if (!py_to_printerdata(
290                     &keyname, &valuename, &value.type, &value.data_p, &value.size, py_data))
291                 return NULL;
292
293         fstrcpy(value.valuename,  valuename);
294
295         /* Call rpc function */
296
297         werror = rpccli_spoolss_setprinterdataex(
298                 hnd->cli, hnd->mem_ctx, &hnd->pol, keyname, &value);
299
300         if (!W_ERROR_IS_OK(werror)) {
301                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
302                 return NULL;
303         }
304
305         Py_INCREF(Py_None);
306         return Py_None;
307 }
308
309 PyObject *spoolss_hnd_enumprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
310 {
311         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
312         static char *kwlist[] = { "key", NULL };
313         uint32 i;
314         char *key;
315         WERROR werror;
316         PyObject *result;
317         REGVAL_CTR *ctr;
318
319         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &key))
320                 return NULL;
321
322         if (!(ctr = TALLOC_ZERO_P(hnd->mem_ctx, REGVAL_CTR))) {
323                 PyErr_SetString(spoolss_error, "talloc failed");
324                 return NULL;
325         }
326
327         /* Get max buffer sizes for value and data */
328
329         werror = rpccli_spoolss_enumprinterdataex(
330                 hnd->cli, hnd->mem_ctx, &hnd->pol, key, &ctr);
331
332         if (!W_ERROR_IS_OK(werror)) {
333                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
334                 return NULL;
335         }
336
337         /* Iterate over all printerdata */
338
339         result = PyDict_New();
340
341         for (i = 0; i < regval_ctr_numvals(&ctr); i++) {
342                 REGISTRY_VALUE *value;
343                 PyObject *item;
344
345                 item = PyDict_New();
346                 value = regval_ctr_specific_value(&ctr, i);
347
348                 if (py_from_printerdata(
349                             &item, key, value->valuename, value->type, 
350                             value->data_p, value->size))
351                         PyDict_SetItemString(result, value->valuename, item);
352         }
353         
354         return result;
355 }
356
357 PyObject *spoolss_hnd_deleteprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
358 {
359         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
360         static char *kwlist[] = { "key", "value", NULL };
361         char *key, *value;
362         WERROR werror;
363
364         /* Parse parameters */
365
366         if (!PyArg_ParseTupleAndKeywords(args, kw, "ss", kwlist, &key, &value))
367                 return NULL;
368
369         /* Call rpc function */
370
371         werror = rpccli_spoolss_deleteprinterdataex(
372                 hnd->cli, hnd->mem_ctx, &hnd->pol, key, value);
373
374         if (!W_ERROR_IS_OK(werror)) {
375                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
376                 return NULL;
377         }
378
379         Py_INCREF(Py_None);
380         return Py_None;
381 }
382
383 PyObject *spoolss_hnd_enumprinterkey(PyObject *self, PyObject *args,
384                                      PyObject *kw)
385 {
386         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
387         static char *kwlist[] = { "key", NULL };
388         char *keyname;
389         WERROR werror;
390         uint32 keylist_len;
391         uint16 *keylist;
392         PyObject *result;
393
394         /* Parse parameters */
395
396         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &keyname))
397                 return NULL;
398
399         /* Call rpc function */
400
401         werror = rpccli_spoolss_enumprinterkey(
402                 hnd->cli, hnd->mem_ctx, &hnd->pol, keyname, &keylist, 
403                 &keylist_len);
404
405         if (!W_ERROR_IS_OK(werror)) {
406                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
407                 return NULL;
408         }
409
410         result = from_unistr_list(keylist);
411
412         return result;
413 }
414
415 #if 0
416
417 PyObject *spoolss_hnd_deleteprinterkey(PyObject *self, PyObject *args,
418                                        PyObject *kw)
419 {
420         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
421         static char *kwlist[] = { "key", NULL };
422         char *keyname;
423         WERROR werror;
424
425         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &keyname))
426                 return NULL;
427
428         Py_INCREF(Py_None);
429         return Py_None;
430 }
431
432 #endif