More code to store ACEs and SIDs. I have almost enough to start testing
[Samba/gebeck_regimport.git] / source3 / python / py_spoolss_printerdata.c
blobf165475b0802ec7265bd47eef0cb214028cb7418
1 /*
2 Python wrappers for DCERPC/SMB client routines.
4 Copyright (C) Tim Potter, 2002
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.
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.
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.
21 #include "python/py_spoolss.h"
22 #include "python/py_conv.h"
24 static BOOL py_from_printerdata(PyObject **dict, char *key, char *value,
25 uint16 data_type, uint8 *data,
26 uint32 data_size)
28 *dict = PyDict_New();
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));
34 PyDict_SetItemString(*dict, "data",
35 Py_BuildValue("s#", data, data_size));
37 return True;
40 static BOOL py_to_printerdata(char **key, char **value, uint16 *data_type,
41 uint8 **data, uint32 *data_size,
42 PyObject *dict)
44 PyObject *obj;
46 if ((obj = PyDict_GetItemString(dict, "key"))) {
48 if (!PyString_Check(obj)) {
49 PyErr_SetString(spoolss_error,
50 "key not a string");
51 return False;
54 if (key) {
55 *key = PyString_AsString(obj);
57 if (!key[0])
58 *key = NULL;
60 } else
61 *key = NULL;
63 if ((obj = PyDict_GetItemString(dict, "value"))) {
65 if (!PyString_Check(obj)) {
66 PyErr_SetString(spoolss_error,
67 "value not a string");
68 return False;
71 *value = PyString_AsString(obj);
72 } else {
73 PyErr_SetString(spoolss_error, "no value present");
74 return False;
77 if ((obj = PyDict_GetItemString(dict, "type"))) {
79 if (!PyInt_Check(obj)) {
80 PyErr_SetString(spoolss_error,
81 "type not an integer");
82 return False;
85 *data_type = PyInt_AsLong(obj);
86 } else {
87 PyErr_SetString(spoolss_error, "no type present");
88 return False;
91 if ((obj = PyDict_GetItemString(dict, "data"))) {
93 if (!PyString_Check(obj)) {
94 PyErr_SetString(spoolss_error,
95 "data not a string");
96 return False;
99 *data = PyString_AsString(obj);
100 *data_size = PyString_Size(obj);
101 } else {
102 PyErr_SetString(spoolss_error, "no data present");
103 return False;
106 return True;
109 PyObject *spoolss_hnd_getprinterdata(PyObject *self, PyObject *args, PyObject *kw)
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;
119 /* Parse parameters */
121 if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &valuename))
122 return NULL;
124 /* Call rpc function */
126 werror = cli_spoolss_getprinterdata(
127 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, valuename,
128 &value);
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);
135 if (!W_ERROR_IS_OK(werror)) {
136 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
137 return NULL;
140 py_from_printerdata(
141 &result, NULL, valuename, value.type, value.data_p,
142 value.size);
144 return result;
147 PyObject *spoolss_hnd_setprinterdata(PyObject *self, PyObject *args, PyObject *kw)
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;
156 if (!PyArg_ParseTupleAndKeywords(
157 args, kw, "O!", kwlist, &PyDict_Type, &py_data))
158 return NULL;
160 if (!py_to_printerdata(
161 NULL, &valuename, &value.type, &value.data_p,
162 &value.size, py_data))
163 return NULL;
165 fstrcpy(value.valuename, valuename);
167 /* Call rpc function */
169 werror = cli_spoolss_setprinterdata(
170 hnd->cli, hnd->mem_ctx, &hnd->pol, &value);
172 if (!W_ERROR_IS_OK(werror)) {
173 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
174 return NULL;
177 Py_INCREF(Py_None);
178 return Py_None;
181 PyObject *spoolss_hnd_enumprinterdata(PyObject *self, PyObject *args, PyObject *kw)
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;
190 if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
191 return NULL;
193 /* Get max buffer sizes for value and data */
195 werror = cli_spoolss_enumprinterdata(
196 hnd->cli, hnd->mem_ctx, &hnd->pol, ndx, 0, 0,
197 &value_needed, &data_needed, NULL);
199 if (!W_ERROR_IS_OK(werror)) {
200 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
201 return NULL;
204 /* Iterate over all printerdata */
206 result = PyDict_New();
208 while (W_ERROR_IS_OK(werror)) {
209 PyObject *obj;
211 werror = cli_spoolss_enumprinterdata(
212 hnd->cli, hnd->mem_ctx, &hnd->pol, ndx,
213 value_needed, data_needed, NULL, NULL, &value);
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);
220 ndx++;
223 return result;
226 PyObject *spoolss_hnd_deleteprinterdata(PyObject *self, PyObject *args, PyObject *kw)
228 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
229 static char *kwlist[] = { "value", NULL };
230 char *value;
231 WERROR werror;
233 /* Parse parameters */
235 if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &value))
236 return NULL;
238 /* Call rpc function */
240 werror = cli_spoolss_deleteprinterdata(
241 hnd->cli, hnd->mem_ctx, &hnd->pol, value);
243 if (!W_ERROR_IS_OK(werror)) {
244 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
245 return NULL;
248 Py_INCREF(Py_None);
249 return Py_None;
252 PyObject *spoolss_hnd_getprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
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;
262 /* Parse parameters */
264 if (!PyArg_ParseTupleAndKeywords(args, kw, "ss", kwlist, &key, &valuename))
265 return NULL;
267 /* Call rpc function */
269 werror = cli_spoolss_getprinterdataex(
270 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, key,
271 valuename, &value);
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);
278 if (!W_ERROR_IS_OK(werror)) {
279 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
280 return NULL;
283 py_from_printerdata(
284 &result, key, valuename, value.type, value.data_p, value.size);
286 return result;
289 PyObject *spoolss_hnd_setprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
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;
298 if (!PyArg_ParseTupleAndKeywords(
299 args, kw, "O!", kwlist, &PyDict_Type, &py_data))
300 return NULL;
302 if (!py_to_printerdata(
303 &keyname, &valuename, &value.type, &value.data_p, &value.size, py_data))
304 return NULL;
306 fstrcpy(value.valuename, valuename);
308 /* Call rpc function */
310 werror = cli_spoolss_setprinterdataex(
311 hnd->cli, hnd->mem_ctx, &hnd->pol, keyname, &value);
313 if (!W_ERROR_IS_OK(werror)) {
314 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
315 return NULL;
318 Py_INCREF(Py_None);
319 return Py_None;
322 PyObject *spoolss_hnd_enumprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
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;
332 if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &key))
333 return NULL;
335 /* Get max buffer sizes for value and data */
337 werror = cli_spoolss_enumprinterdataex(
338 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, key, &ctr);
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);
345 if (!W_ERROR_IS_OK(werror)) {
346 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
347 return NULL;
350 /* Iterate over all printerdata */
352 result = PyDict_New();
354 for (i = 0; i < regval_ctr_numvals(&ctr); i++) {
355 REGISTRY_VALUE *value;
356 PyObject *item;
358 item = PyDict_New();
359 value = regval_ctr_specific_value(&ctr, i);
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);
367 return result;
370 PyObject *spoolss_hnd_deleteprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
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;
377 /* Parse parameters */
379 if (!PyArg_ParseTupleAndKeywords(args, kw, "ss", kwlist, &key, &value))
380 return NULL;
382 /* Call rpc function */
384 werror = cli_spoolss_deleteprinterdataex(
385 hnd->cli, hnd->mem_ctx, &hnd->pol, key, value);
387 if (!W_ERROR_IS_OK(werror)) {
388 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
389 return NULL;
392 Py_INCREF(Py_None);
393 return Py_None;
396 PyObject *spoolss_hnd_enumprinterkey(PyObject *self, PyObject *args,
397 PyObject *kw)
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;
407 /* Parse parameters */
409 if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &keyname))
410 return NULL;
412 /* Call rpc function */
414 werror = cli_spoolss_enumprinterkey(
415 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol,
416 keyname, &keylist, &keylist_len);
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);
423 if (!W_ERROR_IS_OK(werror)) {
424 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
425 return NULL;
428 result = from_unistr_list(keylist);
430 return result;
433 #if 0
435 PyObject *spoolss_hnd_deleteprinterkey(PyObject *self, PyObject *args,
436 PyObject *kw)
438 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
439 static char *kwlist[] = { "key", NULL };
440 char *keyname;
441 WERROR werror;
443 if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &keyname))
444 return NULL;
446 Py_INCREF(Py_None);
447 return Py_None;
450 #endif