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