Started working on setprinter code.
[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 *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_spoolss_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_2 printers_2;
185                 PRINTER_INFO_3 printers_3;
186         } pinfo;
187
188         /* Parse parameters */
189
190         if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist, 
191                                          &PyDict_Type, &info))
192                 return NULL;
193         
194         /* Check dictionary contains a level */
195
196         if ((level_obj = PyDict_GetItemString(info, "level"))) {
197
198                 if (!PyInt_Check(level_obj)) {
199                         DEBUG(0, ("** level not an integer\n"));
200                         goto error;
201                 }
202
203                 level = PyInt_AsLong(level_obj);
204
205                 /* Only level 2, 3 supported by NT */
206
207                 if (level != 2 && level != 3) {
208                         DEBUG(0, ("** unsupported info level\n"));
209                         goto error;
210                 }
211
212         } else {
213                 DEBUG(0, ("** no level info\n"));
214         error:
215                 PyErr_SetString(spoolss_error, "invalid info");
216                 return NULL;
217         }
218
219         /* Fill in printer info */
220
221         ZERO_STRUCT(ctr);
222
223         switch (level) {
224         case 2:
225                 ctr.printers_2 = &pinfo.printers_2;
226
227                 if (!py_to_PRINTER_INFO_2(&pinfo.printers_2, info,
228                                           hnd->mem_ctx))
229                         goto error;
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, *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", "name", "level", "flags", 
263                                  "creds", NULL};
264         TALLOC_CTX *mem_ctx = NULL;
265         struct cli_state *cli = NULL;
266         char *server, *name = NULL;
267
268         /* Parse parameters */
269
270         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|siiO!", kwlist, 
271                                          &server, &name, &level, &flags, 
272                                          &PyDict_Type, &creds))
273                 return NULL;
274         
275         if (server[0] == '\\' && server[1] == '\\')
276                 server += 2;
277
278         mem_ctx = talloc_init();
279         cli = open_pipe_creds(server, creds, cli_spoolss_initialise, NULL);
280
281         /* Call rpc function */
282         
283         werror = cli_spoolss_enum_printers(
284                 cli, mem_ctx, 0, &needed, flags, level,
285                 &num_printers, &ctr);
286
287         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
288                 werror = cli_spoolss_enum_printers(
289                         cli, mem_ctx, needed, NULL, flags, level,
290                         &num_printers, &ctr);
291
292         /* Return value */
293         
294         if (!W_ERROR_IS_OK(werror)) {
295                 PyErr_SetObject(spoolss_werror,
296                                 PyInt_FromLong(W_ERROR_V(werror)));
297                 return NULL;
298         }
299
300         result = PyList_New(num_printers);
301
302         switch (level) {
303         case 0: 
304                 for (i = 0; i < num_printers; i++) {
305                         PyObject *value;
306
307                         py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
308
309                         PyList_SetItem(result, i, value);
310                 }
311
312                 break;
313         case 1:
314                 for(i = 0; i < num_printers; i++) {
315                         PyObject *value;
316
317                         py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
318
319                         PyList_SetItem(result, i, value);
320                 }
321                 
322                 break;
323         case 2:
324                 for(i = 0; i < num_printers; i++) {
325                         PyObject *value;
326
327                         py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
328
329                         PyList_SetItem(result, i, value);
330                 }
331                 
332                 break;
333         case 3:
334                 for(i = 0; i < num_printers; i++) {
335                         PyObject *value;
336
337                         py_from_PRINTER_INFO_3(&value, &ctr.printers_3[i]);
338
339                         PyList_SetItem(result, i, value);
340                 }
341                 
342                 break;
343         }
344
345         Py_INCREF(result);
346         return result;
347 }