Use get_level_value() utility fn.
[samba.git] / source / python / py_spoolss_printers.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 /* Open a printer */
24
25 PyObject *spoolss_openprinter(PyObject *self, PyObject *args, PyObject *kw)
26 {
27         char *unc_name, *server = NULL, *errstr;
28         TALLOC_CTX *mem_ctx;
29         POLICY_HND hnd;
30         WERROR werror;
31         PyObject *result = NULL, *creds = NULL;
32         static char *kwlist[] = { "printername", "creds", "access", NULL };
33         uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
34         struct cli_state *cli;
35
36         if (!PyArg_ParseTupleAndKeywords(
37                     args, kw, "s|O!i", kwlist, &unc_name, &PyDict_Type, &creds,
38                     &desired_access))
39                 goto done;
40
41         /* FIXME: Return name format exception for names without a UNC
42            prefix */ 
43
44         server = strdup(unc_name + 2);
45
46         if (strchr(server, '\\')) {
47                 char *c = strchr(server, '\\');
48                 *c = 0;
49         }
50
51         if (!(cli = open_pipe_creds(
52                       server, creds, cli_spoolss_initialise, &errstr))) {
53                 PyErr_SetString(spoolss_error, errstr);
54                 free(errstr);
55                 goto done;
56         }
57
58         if (!(mem_ctx = talloc_init())) {
59                 PyErr_SetString(spoolss_error, 
60                                 "unable to init talloc context\n");
61                 goto done;
62         }
63
64         werror = cli_spoolss_open_printer_ex(
65                 cli, mem_ctx, unc_name, "", desired_access, server, 
66                 "", &hnd);
67
68         if (!W_ERROR_IS_OK(werror)) {
69                 cli_shutdown(cli);
70                 SAFE_FREE(cli);
71                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
72                 goto done;
73         }
74
75         result = new_spoolss_policy_hnd_object(cli, mem_ctx, &hnd);
76
77  done:
78         SAFE_FREE(server);
79
80         return result;
81 }
82
83 /* Close a printer */
84
85 PyObject *spoolss_closeprinter(PyObject *self, PyObject *args)
86 {
87         PyObject *po;
88         spoolss_policy_hnd_object *hnd;
89         WERROR result;
90
91         /* Parse parameters */
92
93         if (!PyArg_ParseTuple(args, "O!", &spoolss_policy_hnd_type, &po))
94                 return NULL;
95
96         hnd = (spoolss_policy_hnd_object *)po;
97
98         /* Call rpc function */
99
100         result = cli_spoolss_close_printer(hnd->cli, hnd->mem_ctx, &hnd->pol);
101
102         /* Return value */
103
104         Py_INCREF(Py_None);
105         return Py_None; 
106 }
107
108 /* Fetch printer information */
109
110 PyObject *spoolss_hnd_getprinter(PyObject *self, PyObject *args, PyObject *kw)
111 {
112         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
113         WERROR werror;
114         PyObject *result = NULL;
115         PRINTER_INFO_CTR ctr;
116         int level = 1;
117         uint32 needed;
118         static char *kwlist[] = {"level", NULL};
119         
120         /* Parse parameters */
121
122         if (!PyArg_ParseTupleAndKeywords(args, kw, "|i", kwlist, &level))
123                 return NULL;
124         
125         if (level < 0 || level > 3) {
126                 PyErr_SetString(spoolss_error, "Invalid info level");
127                 return NULL;
128         }
129
130         ZERO_STRUCT(ctr);
131
132         /* Call rpc function */
133         
134         werror = cli_spoolss_getprinter(
135                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level, &ctr);
136
137         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
138                 werror = cli_spoolss_getprinter(
139                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
140                         level, &ctr);
141
142         /* Return value */
143
144         if (!W_ERROR_IS_OK(werror)) {
145                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
146                 return NULL;
147         }
148
149         result = Py_None;
150
151         switch (level) {
152                 
153         case 0:
154                 py_from_PRINTER_INFO_0(&result, ctr.printers_0);
155                 break;
156
157         case 1:
158                 py_from_PRINTER_INFO_1(&result, ctr.printers_1);
159                 break;
160
161         case 2:
162                 py_from_PRINTER_INFO_2(&result, ctr.printers_2);
163                 break;
164
165         case 3:
166                 py_from_PRINTER_INFO_3(&result, ctr.printers_3);
167                 break;
168         }
169
170         Py_INCREF(result);
171         return result;
172 }
173
174 /* Set printer information */
175
176 PyObject *spoolss_hnd_setprinter(PyObject *self, PyObject *args, PyObject *kw)
177 {
178         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
179         WERROR werror;
180         PyObject *info;
181         PRINTER_INFO_CTR ctr;
182         uint32 level;
183         static char *kwlist[] = {"dict", NULL};
184         union {
185                 PRINTER_INFO_2 printers_2;
186                 PRINTER_INFO_3 printers_3;
187         } pinfo;
188
189         /* Parse parameters */
190
191         if (!PyArg_ParseTupleAndKeywords(
192                     args, kw, "O!", kwlist, &PyDict_Type, &info))
193                 return NULL;
194         
195         if (!get_level_value(info, &level)) {
196                 PyErr_SetString(spoolss_error, "invalid info level");
197                 return NULL;
198         }
199
200         if (level != 2 && level != 3) {
201                 PyErr_SetString(spoolss_error, "unsupported info level");
202                 return NULL;
203         }
204
205         /* Fill in printer info */
206
207         ZERO_STRUCT(ctr);
208
209         switch (level) {
210         case 2:
211                 ctr.printers_2 = &pinfo.printers_2;
212
213                 if (!py_to_PRINTER_INFO_2(&pinfo.printers_2, info,
214                                           hnd->mem_ctx)){
215                         PyErr_SetString(spoolss_error, 
216                                         "error converting printer to info 2");
217                         return NULL;
218                 }
219
220                 break;
221         case 3:
222                 ctr.printers_3 = &pinfo.printers_3;
223
224                 if (!py_to_PRINTER_INFO_3(&pinfo.printers_3, info,
225                                           hnd->mem_ctx)) {
226                         PyErr_SetString(spoolss_error,
227                                         "error converting to printer info 3");
228                         return NULL;
229                 }
230
231                 break;
232         default:
233                 PyErr_SetString(spoolss_error, "unsupported info level");
234                 return NULL;
235         }
236
237         /* Call rpc function */
238         
239         werror = cli_spoolss_setprinter(hnd->cli, hnd->mem_ctx, &hnd->pol,
240                                         level, &ctr, 0);
241
242         /* Return value */
243
244         if (!W_ERROR_IS_OK(werror)) {
245                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
246                 return NULL;
247         }
248
249         Py_INCREF(Py_None);
250         return Py_None;
251 }
252
253 /* Enumerate printers */
254
255 PyObject *spoolss_enumprinters(PyObject *self, PyObject *args, PyObject *kw)
256 {
257         WERROR werror;
258         PyObject *result = NULL, *creds = NULL;
259         PRINTER_INFO_CTR ctr;
260         int level = 1, flags = PRINTER_ENUM_LOCAL, i;
261         uint32 needed, num_printers;
262         static char *kwlist[] = {"server", "level", "flags", "creds", NULL};
263         TALLOC_CTX *mem_ctx = NULL;
264         struct cli_state *cli = NULL;
265         char *server, *errstr;
266
267         /* Parse parameters */
268
269         if (!PyArg_ParseTupleAndKeywords(
270                     args, kw, "s|iiO!", kwlist, &server, &level, &flags, 
271                     &PyDict_Type, &creds))
272                 return NULL;
273         
274         if (server[0] == '\\' && server[1] == '\\')
275                 server += 2;
276
277         if (!(cli = open_pipe_creds(
278                       server, creds, cli_spoolss_initialise, &errstr))) {
279                 PyErr_SetString(spoolss_error, errstr);
280                 free(errstr);
281                 goto done;
282         }
283
284         if (!(mem_ctx = talloc_init())) {
285                 PyErr_SetString(
286                         spoolss_error, "unable to init talloc context\n");
287                 goto done;
288         }
289
290         /* Call rpc function */
291         
292         werror = cli_spoolss_enum_printers(
293                 cli, mem_ctx, 0, &needed, flags, level,
294                 &num_printers, &ctr);
295
296         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
297                 werror = cli_spoolss_enum_printers(
298                         cli, mem_ctx, needed, NULL, flags, level,
299                         &num_printers, &ctr);
300
301         if (!W_ERROR_IS_OK(werror)) {
302                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
303                 goto done;
304         }
305
306         /* Return value */
307         
308         switch (level) {
309         case 0: 
310                 result = PyDict_New();
311
312                 for (i = 0; i < num_printers; i++) {
313                         PyObject *value;
314                         fstring name;
315
316                         rpcstr_pull(name, ctr.printers_0[i].printername.buffer,
317                                     sizeof(fstring), -1, STR_TERMINATE);
318
319                         py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
320
321                         PyDict_SetItemString(
322                                 value, "level", PyInt_FromLong(0));
323
324                         PyDict_SetItemString(result, name, value);
325                 }
326
327                 break;
328         case 1:
329                 result = PyDict_New();
330
331                 for(i = 0; i < num_printers; i++) {
332                         PyObject *value;
333                         fstring name;
334
335                         rpcstr_pull(name, ctr.printers_1[i].name.buffer,
336                                     sizeof(fstring), -1, STR_TERMINATE);
337
338                         py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
339
340                         PyDict_SetItemString(
341                                 value, "level", PyInt_FromLong(1));
342
343                         PyDict_SetItemString(result, name, value);
344                 }
345                 
346                 break;
347         case 2:
348                 result = PyDict_New();
349
350                 for(i = 0; i < num_printers; i++) {
351                         PyObject *value;
352                         fstring name;
353
354                         rpcstr_pull(name, ctr.printers_2[i].printername.buffer,
355                                     sizeof(fstring), -1, STR_TERMINATE);
356
357                         py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
358
359                         PyDict_SetItemString(
360                                 value, "level", PyInt_FromLong(2));
361
362                         PyDict_SetItemString(result, name, value);
363                 }
364                 
365                 break;
366         default:
367                 PyErr_SetString(spoolss_error, "unknown info level");
368                 goto done;
369         }
370
371 done:
372         if (cli)
373                 cli_shutdown(cli);
374
375         if (mem_ctx)
376                 talloc_destroy(mem_ctx);
377
378         return result;
379 }
380
381 /* Add a printer */
382
383 PyObject *spoolss_addprinterex(PyObject *self, PyObject *args, PyObject *kw)
384 {
385         static char *kwlist[] = { "server", "printername", "info", "creds", 
386                                   NULL};
387         char *printername, *server, *errstr;
388         PyObject *info, *result = NULL, *creds = NULL;
389         struct cli_state *cli = NULL;
390         TALLOC_CTX *mem_ctx = NULL;
391         PRINTER_INFO_CTR ctr;
392         PRINTER_INFO_2 info2;
393         WERROR werror;
394
395         if (!PyArg_ParseTupleAndKeywords(
396                     args, kw, "ssO!|O!", kwlist, &server, &printername,
397                     &PyDict_Type, &info, &PyDict_Type, &creds))
398                 return NULL;
399
400         if (!(cli = open_pipe_creds(
401                       server, creds, cli_spoolss_initialise, &errstr))) {
402                 PyErr_SetString(spoolss_error, errstr);
403                 free(errstr);
404                 goto done;
405         }
406
407         if (!(mem_ctx = talloc_init())) {
408                 PyErr_SetString(
409                         spoolss_error, "unable to init talloc context\n");
410                 return NULL;
411         }
412
413         if (!py_to_PRINTER_INFO_2(&info2, info, mem_ctx)) {
414                 PyErr_SetString(spoolss_error,
415                                 "error converting to printer info 2");
416                 goto done;
417         }
418
419         ctr.printers_2 = &info2;
420
421         werror = cli_spoolss_addprinterex(cli, mem_ctx, 2, &ctr);
422
423         Py_INCREF(Py_None);
424         result = Py_None;
425
426 done:
427         cli_shutdown(cli);
428         talloc_destroy(mem_ctx);
429
430         return result;
431 }