Fix memory leaks and return values in error returns for enumprinters().
[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, *level_obj;
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         /* Check dictionary contains a level */
196
197         if ((level_obj = PyDict_GetItemString(info, "level"))) {
198
199                 if (!PyInt_Check(level_obj)) {
200                         PyErr_SetString(spoolss_error, 
201                                         "level not an integer");
202                         return NULL;
203                 }
204
205                 level = PyInt_AsLong(level_obj);
206
207                 /* Only level 2, 3 supported by NT */
208
209                 if (level != 2 && level != 3) {
210                         PyErr_SetString(spoolss_error,
211                                         "unsupported info level");
212                         return NULL;
213                 }
214
215         } else {
216                 PyErr_SetString(spoolss_error, "no info level present");
217                 return NULL;
218         }
219
220         /* Fill in printer info */
221
222         ZERO_STRUCT(ctr);
223
224         switch (level) {
225         case 2:
226                 ctr.printers_2 = &pinfo.printers_2;
227
228                 if (!py_to_PRINTER_INFO_2(&pinfo.printers_2, info,
229                                           hnd->mem_ctx)){
230                         PyErr_SetString(spoolss_error, 
231                                         "error converting printer to info 2");
232                         return NULL;
233                 }
234
235                 break;
236         case 3:
237                 ctr.printers_3 = &pinfo.printers_3;
238
239                 if (!py_to_PRINTER_INFO_3(&pinfo.printers_3, info,
240                                           hnd->mem_ctx)) {
241                         PyErr_SetString(spoolss_error,
242                                         "error converting to printer info 3");
243                         return NULL;
244                 }
245
246                 break;
247         default:
248                 PyErr_SetString(spoolss_error, "unsupported info level");
249                 return NULL;
250         }
251
252         /* Call rpc function */
253         
254         werror = cli_spoolss_setprinter(hnd->cli, hnd->mem_ctx, &hnd->pol,
255                                         level, &ctr, 0);
256
257         /* Return value */
258
259         if (!W_ERROR_IS_OK(werror)) {
260                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
261                 return NULL;
262         }
263
264         Py_INCREF(Py_None);
265         return Py_None;
266 }
267
268 /* Enumerate printers */
269
270 PyObject *spoolss_enumprinters(PyObject *self, PyObject *args, PyObject *kw)
271 {
272         WERROR werror;
273         PyObject *result = NULL, *creds = NULL;
274         PRINTER_INFO_CTR ctr;
275         int level = 1, flags = PRINTER_ENUM_LOCAL, i;
276         uint32 needed, num_printers;
277         static char *kwlist[] = {"server", "level", "flags", "creds", NULL};
278         TALLOC_CTX *mem_ctx = NULL;
279         struct cli_state *cli = NULL;
280         char *server, *errstr;
281
282         /* Parse parameters */
283
284         if (!PyArg_ParseTupleAndKeywords(
285                     args, kw, "s|iiO!", kwlist, &server, &level, &flags, 
286                     &PyDict_Type, &creds))
287                 return NULL;
288         
289         if (server[0] == '\\' && server[1] == '\\')
290                 server += 2;
291
292         if (!(cli = open_pipe_creds(
293                       server, creds, cli_spoolss_initialise, &errstr))) {
294                 PyErr_SetString(spoolss_error, errstr);
295                 free(errstr);
296                 goto done;
297         }
298
299         if (!(mem_ctx = talloc_init())) {
300                 PyErr_SetString(
301                         spoolss_error, "unable to init talloc context\n");
302                 goto done;
303         }
304
305         /* Call rpc function */
306         
307         werror = cli_spoolss_enum_printers(
308                 cli, mem_ctx, 0, &needed, flags, level,
309                 &num_printers, &ctr);
310
311         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
312                 werror = cli_spoolss_enum_printers(
313                         cli, mem_ctx, needed, NULL, flags, level,
314                         &num_printers, &ctr);
315
316         if (!W_ERROR_IS_OK(werror)) {
317                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
318                 goto done;
319         }
320
321         /* Return value */
322         
323         switch (level) {
324         case 0: 
325                 result = PyDict_New();
326
327                 for (i = 0; i < num_printers; i++) {
328                         PyObject *value;
329                         fstring name;
330
331                         rpcstr_pull(name, ctr.printers_0[i].printername.buffer,
332                                     sizeof(fstring), -1, STR_TERMINATE);
333
334                         py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
335
336                         PyDict_SetItemString(
337                                 value, "level", PyInt_FromLong(0));
338
339                         PyDict_SetItemString(result, name, value);
340                 }
341
342                 break;
343         case 1:
344                 result = PyDict_New();
345
346                 for(i = 0; i < num_printers; i++) {
347                         PyObject *value;
348                         fstring name;
349
350                         rpcstr_pull(name, ctr.printers_1[i].name.buffer,
351                                     sizeof(fstring), -1, STR_TERMINATE);
352
353                         py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
354
355                         PyDict_SetItemString(
356                                 value, "level", PyInt_FromLong(1));
357
358                         PyDict_SetItemString(result, name, value);
359                 }
360                 
361                 break;
362         case 2:
363                 result = PyDict_New();
364
365                 for(i = 0; i < num_printers; i++) {
366                         PyObject *value;
367                         fstring name;
368
369                         rpcstr_pull(name, ctr.printers_2[i].printername.buffer,
370                                     sizeof(fstring), -1, STR_TERMINATE);
371
372                         py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
373
374                         PyDict_SetItemString(
375                                 value, "level", PyInt_FromLong(2));
376
377                         PyDict_SetItemString(result, name, value);
378                 }
379                 
380                 break;
381         default:
382                 PyErr_SetString(spoolss_error, "unknown info level");
383                 goto done;
384         }
385
386 done:
387         if (cli)
388                 cli_shutdown(cli);
389
390         if (mem_ctx)
391                 talloc_destroy(mem_ctx);
392
393         return result;
394 }
395
396 /* Add a printer */
397
398 PyObject *spoolss_addprinterex(PyObject *self, PyObject *args, PyObject *kw)
399 {
400         static char *kwlist[] = { "server", "printername", "info", "creds", 
401                                   NULL};
402         char *printername, *server, *errstr;
403         PyObject *info, *result = NULL, *creds = NULL;
404         struct cli_state *cli = NULL;
405         TALLOC_CTX *mem_ctx = NULL;
406         PRINTER_INFO_CTR ctr;
407         PRINTER_INFO_2 info2;
408         WERROR werror;
409
410         if (!PyArg_ParseTupleAndKeywords(
411                     args, kw, "ssO!|O!", kwlist, &server, &printername,
412                     &PyDict_Type, &info, &PyDict_Type, &creds))
413                 return NULL;
414
415         if (!(cli = open_pipe_creds(
416                       server, creds, cli_spoolss_initialise, &errstr))) {
417                 PyErr_SetString(spoolss_error, errstr);
418                 free(errstr);
419                 goto done;
420         }
421
422         if (!(mem_ctx = talloc_init())) {
423                 PyErr_SetString(
424                         spoolss_error, "unable to init talloc context\n");
425                 return NULL;
426         }
427
428         if (!py_to_PRINTER_INFO_2(&info2, info, mem_ctx)) {
429                 PyErr_SetString(spoolss_error,
430                                 "error converting to printer info 2");
431                 goto done;
432         }
433
434         ctr.printers_2 = &info2;
435
436         werror = cli_spoolss_addprinterex(cli, mem_ctx, 2, &ctr);
437
438         Py_INCREF(Py_None);
439         result = Py_None;
440
441 done:
442         cli_shutdown(cli);
443         talloc_destroy(mem_ctx);
444
445         return result;
446 }