Call PyErr_SetString instead of fprintf to stderr.
[samba.git] / source3 / 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 *full_name, *computer_name = NULL;
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, &full_name, &PyDict_Type, &creds,
38                 &desired_access)) {
39
40                 goto done;
41         }
42
43         /* FIXME: Return name format exception for names without a UNC
44            prefix */ 
45
46         computer_name = strdup(full_name + 2);
47
48         if (strchr(computer_name, '\\')) {
49                 char *c = strchr(computer_name, '\\');
50                 *c = 0;
51         }
52
53         if (!(cli = open_pipe_creds(computer_name, creds, 
54                                     cli_spoolss_initialise, NULL))) {
55
56                 /* Error state set in open_pipe_creds() */
57
58                 goto done;
59         }
60
61         if (!(mem_ctx = talloc_init())) {
62                 PyErr_SetString(spoolss_error, 
63                                 "unable to initialise talloc context\n");
64                 goto done;
65         }
66
67         werror = cli_spoolss_open_printer_ex(
68                 cli, mem_ctx, full_name, "", desired_access, computer_name, 
69                 "", &hnd);
70
71         if (!W_ERROR_IS_OK(werror)) {
72                 cli_shutdown(cli);
73                 SAFE_FREE(cli);
74                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
75                 goto done;
76         }
77
78         result = new_policy_hnd_object(cli, mem_ctx, &hnd);
79
80  done:
81         SAFE_FREE(computer_name);
82
83         return result;
84 }
85
86 /* Close a printer */
87
88 PyObject *spoolss_closeprinter(PyObject *self, PyObject *args)
89 {
90         PyObject *po;
91         spoolss_policy_hnd_object *hnd;
92         WERROR result;
93
94         /* Parse parameters */
95
96         if (!PyArg_ParseTuple(args, "O!", &spoolss_policy_hnd_type, &po))
97                 return NULL;
98
99         hnd = (spoolss_policy_hnd_object *)po;
100
101         /* Call rpc function */
102
103         result = cli_spoolss_close_printer(hnd->cli, hnd->mem_ctx, &hnd->pol);
104
105         /* Return value */
106
107         Py_INCREF(Py_None);
108         return Py_None; 
109 }
110
111 /* Fetch printer information */
112
113 PyObject *spoolss_getprinter(PyObject *self, PyObject *args, PyObject *kw)
114 {
115         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
116         WERROR werror;
117         PyObject *result = NULL;
118         PRINTER_INFO_CTR ctr;
119         int level = 1;
120         uint32 needed;
121         static char *kwlist[] = {"level", NULL};
122
123         /* Parse parameters */
124
125         if (!PyArg_ParseTupleAndKeywords(args, kw, "|i", kwlist, &level))
126                 return NULL;
127         
128         /* Call rpc function */
129         
130         werror = cli_spoolss_getprinter(
131                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level, &ctr);
132
133         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
134                 werror = cli_spoolss_getprinter(
135                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
136                         level, &ctr);
137
138         /* Return value */
139
140         if (!W_ERROR_IS_OK(werror)) {
141                 PyErr_SetObject(spoolss_werror,
142                                 PyInt_FromLong(W_ERROR_V(werror)));
143                 return NULL;
144         }
145
146         result = Py_None;
147
148         switch (level) {
149                 
150         case 0:
151                 py_from_PRINTER_INFO_0(&result, ctr.printers_0);
152                 break;
153
154         case 1:
155                 py_from_PRINTER_INFO_1(&result, ctr.printers_1);
156                 break;
157
158         case 2:
159                 py_from_PRINTER_INFO_2(&result, ctr.printers_2);
160                 break;
161
162         case 3:
163                 py_from_PRINTER_INFO_3(&result, ctr.printers_3);
164                 break;
165         }
166
167         PyDict_SetItemString(result, "level", PyInt_FromLong(level));
168
169         Py_INCREF(result);
170         return result;
171 }
172
173 /* Set printer information */
174
175 PyObject *spoolss_setprinter(PyObject *self, PyObject *args, PyObject *kw)
176 {
177         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
178         WERROR werror;
179         PyObject *info, *level_obj;
180         PRINTER_INFO_CTR ctr;
181         uint32 level;
182         static char *kwlist[] = {"dict", NULL};
183         union {
184                 PRINTER_INFO_0 printers_0;
185                 PRINTER_INFO_1 printers_1;
186                 PRINTER_INFO_2 printers_2;
187                 PRINTER_INFO_3 printers_3;
188                 PRINTER_INFO_4 printers_4;
189                 PRINTER_INFO_5 printers_5;
190         } pinfo;
191
192         /* Parse parameters */
193
194         if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist, 
195                                          &PyDict_Type, &info))
196                 return NULL;
197         
198         /* Check dictionary contains a level */
199
200         if ((level_obj = PyDict_GetItemString(info, "level"))) {
201
202                 if (!PyInt_Check(level_obj))
203                         goto error;
204
205                 level = PyInt_AsLong(level_obj);
206
207         } else {
208         error:
209                 PyErr_SetString(spoolss_error, "invalid info");
210                 return NULL;
211         }
212
213         /* Fill in printer info */
214
215         ZERO_STRUCT(ctr);
216
217         switch (level) {
218         case 2: {
219                 PyObject *devmode_obj;
220
221                 ctr.printers_2 = &pinfo.printers_2;
222
223                 if (!py_to_PRINTER_INFO_2(&pinfo.printers_2, info))
224                         goto error;
225
226 #if 0
227                 devmode_obj = PyDict_GetItemString(info, "device_mode");
228
229                 pinfo.printers_2.devmode = talloc(
230                         hnd->mem_ctx, sizeof(DEVICEMODE));
231
232                 PyDEVICEMODE_AsDEVICEMODE(pinfo.printers_2.devmode, 
233                                           devmode_obj);
234
235 #else
236
237                 /* FIXME: can we actually set the security descriptor using
238                    a setprinter level 2? */
239
240                 pinfo.printers_2.secdesc = NULL;
241                 pinfo.printers_2.secdesc = NULL;
242
243 #endif
244                 break;
245         }
246         default:
247                 PyErr_SetString(spoolss_error, "unsupported info level");
248                 return NULL;
249         }
250
251         /* Call rpc function */
252         
253         werror = cli_spoolss_setprinter(hnd->cli, hnd->mem_ctx, &hnd->pol,
254                                         level, &ctr, 0);
255
256         /* Return value */
257
258         if (!W_ERROR_IS_OK(werror)) {
259                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
260                 return NULL;
261         }
262
263         Py_INCREF(Py_None);
264         return Py_None;
265 }
266
267 /* Enumerate printers */
268
269 PyObject *spoolss_enumprinters(PyObject *self, PyObject *args, PyObject *kw)
270 {
271         WERROR werror;
272         PyObject *result, *creds = NULL;
273         PRINTER_INFO_CTR ctr;
274         int level = 1, flags = PRINTER_ENUM_LOCAL, i;
275         uint32 needed, num_printers;
276         static char *kwlist[] = {"server", "name", "level", "flags", 
277                                  "creds", NULL};
278         TALLOC_CTX *mem_ctx = NULL;
279         struct cli_state *cli = NULL;
280         char *server, *name = NULL;
281
282         /* Parse parameters */
283
284         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|siiO!", kwlist, 
285                                          &server, &name, &level, &flags, 
286                                          &PyDict_Type, &creds))
287                 return NULL;
288         
289         if (server[0] == '\\' && server[1] == '\\')
290                 server += 2;
291
292         mem_ctx = talloc_init();
293         cli = open_pipe_creds(server, creds, cli_spoolss_initialise, NULL);
294
295         /* Call rpc function */
296         
297         werror = cli_spoolss_enum_printers(
298                 cli, mem_ctx, 0, &needed, flags, level,
299                 &num_printers, &ctr);
300
301         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
302                 werror = cli_spoolss_enum_printers(
303                         cli, mem_ctx, needed, NULL, flags, level,
304                         &num_printers, &ctr);
305
306         /* Return value */
307         
308         if (!W_ERROR_IS_OK(werror)) {
309                 PyErr_SetObject(spoolss_werror,
310                                 PyInt_FromLong(W_ERROR_V(werror)));
311                 return NULL;
312         }
313
314         result = PyList_New(num_printers);
315
316         switch (level) {
317         case 0: 
318                 for (i = 0; i < num_printers; i++) {
319                         PyObject *value;
320
321                         py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
322
323                         PyList_SetItem(result, i, value);
324                 }
325
326                 break;
327         case 1:
328                 for(i = 0; i < num_printers; i++) {
329                         PyObject *value;
330
331                         py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
332
333                         PyList_SetItem(result, i, value);
334                 }
335                 
336                 break;
337         case 2:
338                 for(i = 0; i < num_printers; i++) {
339                         PyObject *value;
340
341                         py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
342
343                         PyList_SetItem(result, i, value);
344                 }
345                 
346                 break;
347         case 3:
348                 for(i = 0; i < num_printers; i++) {
349                         PyObject *value;
350
351                         py_from_PRINTER_INFO_3(&value, &ctr.printers_3[i]);
352
353                         PyList_SetItem(result, i, value);
354                 }
355                 
356                 break;
357         }
358
359         Py_INCREF(result);
360         return result;
361 }