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