Initial version.
[samba.git] / source3 / python / py_spoolss.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 "includes.h"
22 #include "Python.h"
23 #include "python/py_common.h"
24 #include "python/py_spoolss.h"
25
26 /* Exceptions this module can raise */
27
28 PyObject *spoolss_error, *spoolss_werror;
29
30 static void py_policy_hnd_dealloc(PyObject* self)
31 {
32         PyObject_Del(self);
33 }
34
35 static PyObject *new_policy_hnd_object(struct cli_state *cli, 
36                                        TALLOC_CTX *mem_ctx, POLICY_HND *pol)
37 {
38         spoolss_policy_hnd_object *o;
39
40         o = PyObject_New(spoolss_policy_hnd_object, &spoolss_policy_hnd_type);
41
42         o->cli = cli;
43         o->mem_ctx = mem_ctx;
44         memcpy(&o->pol, pol, sizeof(POLICY_HND));
45
46         return (PyObject*)o;
47 }
48      
49 PyTypeObject spoolss_policy_hnd_type = {
50         PyObject_HEAD_INIT(NULL)
51         0,
52         "Policy Handle",
53         sizeof(spoolss_policy_hnd_object),
54         0,
55         py_policy_hnd_dealloc, /*tp_dealloc*/
56         0,          /*tp_print*/
57         0,          /*tp_getattr*/
58         0,          /*tp_setattr*/
59         0,          /*tp_compare*/
60         0,          /*tp_repr*/
61         0,          /*tp_as_number*/
62         0,          /*tp_as_sequence*/
63         0,          /*tp_as_mapping*/
64         0,          /*tp_hash */
65 };
66
67 /*
68  * Routines to convert from python hashes to Samba structures
69  */
70
71 struct pyconv py_PRINTER_INFO_0[] = {
72         { "printer_name", PY_UNISTR, offsetof(PRINTER_INFO_0, printername) },
73         { "server_name", PY_UNISTR, offsetof(PRINTER_INFO_0, servername) },
74
75         { "cjobs", PY_UINT32, offsetof(PRINTER_INFO_0, cjobs) },
76         { "total_jobs", PY_UINT32, offsetof(PRINTER_INFO_0, total_jobs) },
77         { "total_bytes", PY_UINT32, offsetof(PRINTER_INFO_0, total_bytes) },
78
79         { "year", PY_UINT16, offsetof(PRINTER_INFO_0, year) },
80         { "month", PY_UINT16, offsetof(PRINTER_INFO_0, month) },
81         { "day_of_week", PY_UINT16, offsetof(PRINTER_INFO_0, dayofweek) },
82         { "day", PY_UINT16, offsetof(PRINTER_INFO_0, day) },
83         { "hour", PY_UINT16, offsetof(PRINTER_INFO_0, hour) },
84         { "minute", PY_UINT16, offsetof(PRINTER_INFO_0, minute) },
85         { "second", PY_UINT16, offsetof(PRINTER_INFO_0, second) },
86         { "milliseconds", PY_UINT16, offsetof(PRINTER_INFO_0, milliseconds) },
87
88         { "global_counter", PY_UINT32, offsetof(PRINTER_INFO_0, global_counter) },
89         { "total_pages", PY_UINT32, offsetof(PRINTER_INFO_0, total_pages) },
90
91         { "major_version", PY_UINT16, offsetof(PRINTER_INFO_0, major_version) },
92         { "build_version", PY_UINT16, offsetof(PRINTER_INFO_0, build_version) },
93
94         { "unknown7", PY_UINT32, offsetof(PRINTER_INFO_0, unknown7) },
95         { "unknown8", PY_UINT32, offsetof(PRINTER_INFO_0, unknown8) },
96         { "unknown9", PY_UINT32, offsetof(PRINTER_INFO_0, unknown9) },
97         { "session_counter", PY_UINT32, offsetof(PRINTER_INFO_0, session_counter)},
98         { "unknown11", PY_UINT32, offsetof(PRINTER_INFO_0, unknown11) },
99         { "printer_errors", PY_UINT32, offsetof(PRINTER_INFO_0, printer_errors) },
100         { "unknown13", PY_UINT32, offsetof(PRINTER_INFO_0, unknown13) },
101         { "unknown14", PY_UINT32, offsetof(PRINTER_INFO_0, unknown14) },
102         { "unknown15", PY_UINT32, offsetof(PRINTER_INFO_0, unknown15) },
103         { "unknown16", PY_UINT32, offsetof(PRINTER_INFO_0, unknown16) },
104         { "change_id", PY_UINT32, offsetof(PRINTER_INFO_0, change_id) },
105         { "unknown18", PY_UINT32, offsetof(PRINTER_INFO_0, unknown18) },
106         { "status", PY_UINT32, offsetof(PRINTER_INFO_0, status) },
107         { "unknown20", PY_UINT32, offsetof(PRINTER_INFO_0, unknown20) },
108         { "c_setprinter", PY_UINT32, offsetof(PRINTER_INFO_0, c_setprinter) },
109         { "unknown22", PY_UINT32, offsetof(PRINTER_INFO_0, unknown22) },
110         { "unknown23", PY_UINT32, offsetof(PRINTER_INFO_0, unknown23) },
111         { "unknown24", PY_UINT32, offsetof(PRINTER_INFO_0, unknown24) },
112         { "unknown25", PY_UINT32, offsetof(PRINTER_INFO_0, unknown25) },
113         { "unknown26", PY_UINT32, offsetof(PRINTER_INFO_0, unknown26) },
114         { "unknown27", PY_UINT32, offsetof(PRINTER_INFO_0, unknown27) },
115         { "unknown28", PY_UINT32, offsetof(PRINTER_INFO_0, unknown28) },
116         { "unknown29", PY_UINT32, offsetof(PRINTER_INFO_0, unknown29) },
117
118         { NULL }
119 };      
120
121 struct pyconv py_PRINTER_INFO_1[] = {
122         { "printer_name", PY_UNISTR, offsetof(PRINTER_INFO_1, name) },
123         { "description", PY_UNISTR, offsetof(PRINTER_INFO_1, description) },
124         { "comment", PY_UNISTR, offsetof(PRINTER_INFO_1, comment) },
125         { "flags", PY_UINT32, offsetof(PRINTER_INFO_1, flags) },
126         { NULL }
127 };      
128
129 struct pyconv py_PRINTER_INFO_2[] = {
130         { "server_name", PY_UNISTR, offsetof(PRINTER_INFO_2, servername) },
131         { "printer_name", PY_UNISTR, offsetof(PRINTER_INFO_2, printername) },
132         { "share_name", PY_UNISTR, offsetof(PRINTER_INFO_2, sharename) },
133         { "port_name", PY_UNISTR, offsetof(PRINTER_INFO_2, portname) },
134         { "driver_name", PY_UNISTR, offsetof(PRINTER_INFO_2, drivername) },
135         { "comment", PY_UNISTR, offsetof(PRINTER_INFO_2, comment) },
136         { "location", PY_UNISTR, offsetof(PRINTER_INFO_2, location) },
137         { "datatype", PY_UNISTR, offsetof(PRINTER_INFO_2, datatype) },
138         { "sepfile", PY_UNISTR, offsetof(PRINTER_INFO_2, sepfile) },
139         { "print_processor", PY_UNISTR, offsetof(PRINTER_INFO_2, printprocessor) },
140         { "parameters", PY_UNISTR, offsetof(PRINTER_INFO_2, parameters) },
141         { "attributes", PY_UINT32, offsetof(PRINTER_INFO_2, attributes) },
142         { "default_priority", PY_UINT32, offsetof(PRINTER_INFO_2, defaultpriority) },
143         { "priority", PY_UINT32, offsetof(PRINTER_INFO_2, priority) },
144         { "start_time", PY_UINT32, offsetof(PRINTER_INFO_2, starttime) },
145         { "until_time", PY_UINT32, offsetof(PRINTER_INFO_2, untiltime) },
146         { "status", PY_UINT32, offsetof(PRINTER_INFO_2, status) },
147         { "cjobs", PY_UINT32, offsetof(PRINTER_INFO_2, cjobs) },
148         { "average_ppm", PY_UINT32, offsetof(PRINTER_INFO_2, averageppm) },
149         { NULL }
150 };      
151
152 struct pyconv py_PRINTER_INFO_3[] = {
153         { "flags", PY_UINT32, offsetof(PRINTER_INFO_3, flags) },
154         { NULL }
155 };      
156
157 struct pyconv py_DEVICEMODE[] = {
158         { "device_name", PY_UNISTR, offsetof(DEVICEMODE, devicename) },
159         { "spec_version", PY_UINT16, offsetof(DEVICEMODE, specversion) },
160         { "driver_version", PY_UINT16, offsetof(DEVICEMODE, driverversion) },
161         { "size", PY_UINT16, offsetof(DEVICEMODE, size) },
162         { "fields", PY_UINT16, offsetof(DEVICEMODE, fields) },
163         { "orientation", PY_UINT16, offsetof(DEVICEMODE, orientation) },
164         { "paper_size", PY_UINT16, offsetof(DEVICEMODE, papersize) },
165         { "paper_width", PY_UINT16, offsetof(DEVICEMODE, paperwidth) },
166         { "paper_length", PY_UINT16, offsetof(DEVICEMODE, paperlength) },
167         { "scale", PY_UINT16, offsetof(DEVICEMODE, scale) },
168         { "copies", PY_UINT16, offsetof(DEVICEMODE, copies) },
169         { "default_source", PY_UINT16, offsetof(DEVICEMODE, defaultsource) },
170         { "print_quality", PY_UINT16, offsetof(DEVICEMODE, printquality) },
171         { "color", PY_UINT16, offsetof(DEVICEMODE, color) },
172         { "duplex", PY_UINT16, offsetof(DEVICEMODE, duplex) },
173         { "y_resolution", PY_UINT16, offsetof(DEVICEMODE, yresolution) },
174         { "tt_option", PY_UINT16, offsetof(DEVICEMODE, ttoption) },
175         { "collate", PY_UINT16, offsetof(DEVICEMODE, collate) },
176         { "form_name", PY_UNISTR, offsetof(DEVICEMODE, formname) },
177         { "log_pixels", PY_UINT16, offsetof(DEVICEMODE, logpixels) },
178         { "bits_per_pel", PY_UINT32, offsetof(DEVICEMODE, bitsperpel) },
179         { "pels_width", PY_UINT32, offsetof(DEVICEMODE, pelswidth) },
180         { "pels_height", PY_UINT32, offsetof(DEVICEMODE, pelsheight) },
181         { "display_flags", PY_UINT32, offsetof(DEVICEMODE, displayflags) },
182         { "display_frequency", PY_UINT32, offsetof(DEVICEMODE, displayfrequency) },
183         { "icm_method", PY_UINT32, offsetof(DEVICEMODE, icmmethod) },
184         { "icm_intent", PY_UINT32, offsetof(DEVICEMODE, icmintent) },
185         { "media_type", PY_UINT32, offsetof(DEVICEMODE, mediatype) },
186         { "dither_type", PY_UINT32, offsetof(DEVICEMODE, dithertype) },
187         { "reserved1", PY_UINT32, offsetof(DEVICEMODE, reserved1) },
188         { "reserved2", PY_UINT32, offsetof(DEVICEMODE, reserved2) },
189         { "panning_width", PY_UINT32, offsetof(DEVICEMODE, panningwidth) },
190         { "panning_height", PY_UINT32, offsetof(DEVICEMODE, panningheight) },
191         { NULL }
192 };
193
194 struct pyconv py_DRIVER_INFO_1[] = {
195         { "name", PY_UNISTR, offsetof(DRIVER_INFO_1, name) },
196         { NULL }
197 };
198
199 struct pyconv py_DRIVER_INFO_2[] = {
200         { "version", PY_UINT32, offsetof(DRIVER_INFO_2, version) },
201         { "name", PY_UNISTR, offsetof(DRIVER_INFO_2, name) },
202         { "architecture", PY_UNISTR, offsetof(DRIVER_INFO_2, architecture) },
203         { "driver_path", PY_UNISTR, offsetof(DRIVER_INFO_2, driverpath) },
204         { "data_file", PY_UNISTR, offsetof(DRIVER_INFO_2, datafile) },
205         { "config_file", PY_UNISTR, offsetof(DRIVER_INFO_2, configfile) },
206         { NULL }
207 };
208
209 struct pyconv py_DRIVER_INFO_3[] = {
210         { "version", PY_UINT32, offsetof(DRIVER_INFO_3, version) },
211         { "name", PY_UNISTR, offsetof(DRIVER_INFO_3, name) },
212         { "architecture", PY_UNISTR, offsetof(DRIVER_INFO_3, architecture) },
213         { "driver_path", PY_UNISTR, offsetof(DRIVER_INFO_3, driverpath) },
214         { "data_file", PY_UNISTR, offsetof(DRIVER_INFO_3, datafile) },
215         { "config_file", PY_UNISTR, offsetof(DRIVER_INFO_3, configfile) },
216         { "help_file", PY_UNISTR, offsetof(DRIVER_INFO_3, helpfile) },
217         /* dependentfiles */
218         { "monitor_name", PY_UNISTR, offsetof(DRIVER_INFO_3, monitorname) },
219         { "default_datatype", PY_UNISTR, offsetof(DRIVER_INFO_3, defaultdatatype) },
220         { NULL }
221 };
222
223 struct pyconv py_DRIVER_INFO_6[] = {
224         { "version", PY_UINT32, offsetof(DRIVER_INFO_6, version) },
225         { "name", PY_UNISTR, offsetof(DRIVER_INFO_6, name) },
226         { "architecture", PY_UNISTR, offsetof(DRIVER_INFO_6, architecture) },
227         { "driver_path", PY_UNISTR, offsetof(DRIVER_INFO_6, driverpath) },
228         { "data_file", PY_UNISTR, offsetof(DRIVER_INFO_6, datafile) },
229         { "config_file", PY_UNISTR, offsetof(DRIVER_INFO_6, configfile) },
230         { "help_file", PY_UNISTR, offsetof(DRIVER_INFO_6, helpfile) },
231         /* dependentfiles */
232         { "monitor_name", PY_UNISTR, offsetof(DRIVER_INFO_6, monitorname) },
233         { "default_datatype", PY_UNISTR, offsetof(DRIVER_INFO_6, defaultdatatype) },
234         /* driver_date */
235
236         { "padding", PY_UINT32, offsetof(DRIVER_INFO_6, padding) },
237         { "driver_version_low", PY_UINT32, offsetof(DRIVER_INFO_6, driver_version_low) },
238         { "driver_version_high", PY_UINT32, offsetof(DRIVER_INFO_6, driver_version_high) },
239         { "mfg_name", PY_UNISTR, offsetof(DRIVER_INFO_6, mfgname) },
240         { "oem_url", PY_UNISTR, offsetof(DRIVER_INFO_6, oem_url) },
241         { "hardware_id", PY_UNISTR, offsetof(DRIVER_INFO_6, hardware_id) },
242         { "provider", PY_UNISTR, offsetof(DRIVER_INFO_6, provider) },
243         
244         { NULL }
245 };
246
247 static void fstr_pull(fstring str, UNISTR *uni)
248 {
249         rpcstr_pull(str, uni->buffer, sizeof(fstring), 0, STR_TERMINATE);
250 }
251
252 /* Convert a structure to a Python dict */
253
254 PyObject *from_struct(void *s, struct pyconv *conv)
255 {
256         PyObject *obj, *item;
257         int i;
258
259         obj = PyDict_New();
260
261         for (i = 0; conv[i].name; i++) {
262                 switch (conv[i].type) {
263                 case PY_UNISTR: {
264                         UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
265                         fstring s = "";
266
267                         if (u->buffer)
268                                 fstr_pull(s, u);
269
270                         item = PyString_FromString(s);
271                         PyDict_SetItemString(obj, conv[i].name, item);
272
273                         break;
274                 }
275                 case PY_UINT32: {
276                         uint32 *u = (uint32 *)((char *)s + conv[i].offset);
277
278                         item = PyInt_FromLong(*u);
279                         PyDict_SetItemString(obj, conv[i].name, item);
280                         
281                         break;
282                 }
283                 case PY_UINT16: {
284                         uint16 *u = (uint16 *)((char *)s + conv[i].offset);
285
286                         item = PyInt_FromLong(*u);
287                         PyDict_SetItemString(obj, conv[i].name, item);
288
289                         break;
290                 }
291                 default:
292                         break;
293                 }
294         }
295
296         return obj;
297 }
298
299 /* Convert a Python dict to a structure */
300
301 void to_struct(void *s, PyObject *dict, struct pyconv *conv)
302 {
303         int i;
304
305         for (i = 0; conv[i].name; i++) {
306                 PyObject *obj;
307                 
308                 obj = PyDict_GetItemString(dict, conv[i].name);
309                 
310                 switch (conv[i].type) {
311                 case PY_UNISTR: {
312                         UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
313                         char *s = "";
314
315                         if (obj && PyString_Check(obj))
316                                 s = PyString_AsString(obj);
317
318                         init_unistr(u, s);
319                         
320                         break;
321                 }
322                 case PY_UINT32: {
323                         uint32 *u = (uint32 *)((char *)s + conv[i].offset);
324
325                         if (obj && PyInt_Check(obj)) 
326                                 *u = PyInt_AsLong(obj);
327                         else
328                                 *u = 0;
329
330                         break;
331                 }
332                 case PY_UINT16: {
333                         uint16 *u = (uint16 *)((char *)s + conv[i].offset);
334
335                         if (obj && PyInt_Check(obj)) 
336                                 *u = PyInt_AsLong(obj);
337                         else
338                                 *u = 0;
339
340                         break;
341                 }
342                 default:
343                         break;
344                 }
345         }
346 }
347
348 /* Return a cli_state struct opened on the SPOOLSS pipe.  If credentials
349    are passed use them. */
350
351 typedef struct cli_state *(cli_pipe_fn)(
352         struct cli_state *cli, char *system_name,
353         struct ntuser_creds *creds);
354
355 static struct cli_state *open_pipe_creds(char *system_name, PyObject *creds, 
356                                          cli_pipe_fn *connect_fn,
357                                          struct cli_state *cli)
358 {
359         struct ntuser_creds nt_creds;
360
361         if (!cli) {
362                 cli = (struct cli_state *)malloc(sizeof(struct cli_state));
363                 if (!cli)
364                         return NULL;
365         }
366
367         ZERO_STRUCTP(cli);
368
369         /* Extract credentials from the python dictionary and initialise
370            the ntuser_creds struct from them. */
371
372         ZERO_STRUCT(nt_creds);
373         nt_creds.pwd.null_pwd = True;
374
375         if (creds) {
376                 char *username, *password, *domain;
377                 PyObject *username_obj, *password_obj, *domain_obj;
378
379                 /* Check credentials passed are valid.  This means the
380                    username, domain and password keys must exist and be
381                    string objects. */
382
383                 username_obj = PyDict_GetItemString(creds, "username");
384                 domain_obj = PyDict_GetItemString(creds, "domain");
385                 password_obj = PyDict_GetItemString(creds, "password");
386
387                 if (!username_obj || !domain_obj || !password_obj) {
388                 error:
389                         PyErr_SetString(spoolss_error, "invalid credentials");
390                         return NULL;
391                 }
392
393                 if (!PyString_Check(username_obj) || 
394                     !PyString_Check(domain_obj) || 
395                     !PyString_Check(password_obj))
396                         goto error;
397
398                 username = PyString_AsString(username_obj);
399                 domain = PyString_AsString(domain_obj);
400                 password = PyString_AsString(password_obj);
401
402                 if (!username || !domain || !password)
403                         goto error;
404
405                 /* Initialise nt_creds structure with passed creds */
406
407                 fstrcpy(nt_creds.user_name, username);
408                 fstrcpy(nt_creds.domain, domain);
409
410                 if (lp_encrypted_passwords())
411                         pwd_make_lm_nt_16(&nt_creds.pwd, password);
412                 else
413                         pwd_set_cleartext(&nt_creds.pwd, password);
414
415                 nt_creds.pwd.null_pwd = False;
416         }
417
418         /* Now try to connect */
419
420         connect_fn(cli, system_name, &nt_creds);
421
422         return cli;
423 }
424
425 /* Open a printer */
426
427 static PyObject *spoolss_openprinter(PyObject *self, PyObject *args,
428                                      PyObject *kw)
429 {
430         char *full_name, *computer_name = NULL;
431         TALLOC_CTX *mem_ctx;
432         POLICY_HND hnd;
433         WERROR werror;
434         PyObject *result = NULL, *creds = NULL;
435         static char *kwlist[] = { "printername", "creds", "access", NULL };
436         uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
437         struct cli_state *cli;
438
439         if (!PyArg_ParseTupleAndKeywords(
440                 args, kw, "s|O!i", kwlist, &full_name, &PyDict_Type, &creds,
441                 &desired_access)) {
442
443                 goto done;
444         }
445
446         /* FIXME: Return name format exception for names without a UNC
447            prefix */ 
448
449         computer_name = strdup(full_name + 2);
450
451         if (strchr(computer_name, '\\')) {
452                 char *c = strchr(computer_name, '\\');
453                 *c = 0;
454         }
455
456         if (!(cli = open_pipe_creds(computer_name, creds, 
457                                     cli_spoolss_initialise, NULL))) {
458                 fprintf(stderr, "could not initialise cli state\n");
459                 goto done;
460         }
461
462         if (!(mem_ctx = talloc_init())) {
463                 fprintf(stderr, "unable to initialise talloc context\n");
464                 goto done;
465         }
466
467         werror = cli_spoolss_open_printer_ex(
468                 cli, mem_ctx, full_name, "", desired_access, computer_name, 
469                 "", &hnd);
470
471         if (!W_ERROR_IS_OK(werror)) {
472                 cli_shutdown(cli);
473                 SAFE_FREE(cli);
474                 PyErr_SetObject(spoolss_werror,
475                                 PyInt_FromLong(W_ERROR_V(werror)));
476                 goto done;
477         }
478
479         result = new_policy_hnd_object(cli, mem_ctx, &hnd);
480
481  done:
482         SAFE_FREE(computer_name);
483
484         return result;
485 }
486
487 /* Close a printer */
488
489 static PyObject *spoolss_closeprinter(PyObject *self, PyObject *args)
490 {
491         PyObject *po;
492         spoolss_policy_hnd_object *hnd;
493         WERROR result;
494
495         /* Parse parameters */
496
497         if (!PyArg_ParseTuple(args, "O!", &spoolss_policy_hnd_type, &po))
498                 return NULL;
499
500         hnd = (spoolss_policy_hnd_object *)po;
501
502         /* Call rpc function */
503
504         result = cli_spoolss_close_printer(hnd->cli, hnd->mem_ctx, &hnd->pol);
505
506         /* Cleanup samba stuf */
507
508         cli_shutdown(hnd->cli);
509         talloc_destroy(hnd->mem_ctx);
510
511         /* Return value */
512
513         Py_INCREF(Py_None);
514         return Py_None; 
515 }
516
517 /* Fetch printer driver */
518
519 static PyObject *spoolss_getprinterdriver(PyObject *self, PyObject *args,
520                                           PyObject *kw)
521 {
522         PyObject *po;
523         spoolss_policy_hnd_object *hnd;
524         WERROR werror;
525         PyObject *result;
526         PRINTER_DRIVER_CTR ctr;
527         int level = 1;
528         uint32 needed;
529         char *arch = "Windows NT x86";
530         static char *kwlist[] = {"hnd", "level", "arch", NULL};
531
532         /* Parse parameters */
533
534         if (!PyArg_ParseTupleAndKeywords(args, kw, "O!|is", kwlist, 
535                                          &spoolss_policy_hnd_type, &po, &level, &arch))
536                 return NULL;
537
538         hnd = (spoolss_policy_hnd_object *)po;
539
540         /* Call rpc function */
541
542         werror = cli_spoolss_getprinterdriver(
543                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level,
544                 arch, &ctr);
545
546         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
547                 werror = cli_spoolss_getprinterdriver(
548                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
549                         level, arch, &ctr);
550
551         /* Return value */
552         
553         result = Py_None;
554
555         if (W_ERROR_IS_OK(werror)) {
556                 switch (level) {
557                 case 1:
558                         result = from_struct(&ctr.info1, py_DRIVER_INFO_1);
559                         break;
560                 case 2: 
561                         result = from_struct(&ctr.info2, py_DRIVER_INFO_2);
562                         break;
563                 case 6:
564                         result = from_struct(&ctr.info6, py_DRIVER_INFO_6);
565                         break;
566                 default:
567                         result = Py_None;
568                         break;
569                 }
570         }
571         
572         Py_INCREF(result);
573         return result;
574 }
575
576 /* Enumerate printer drivers */
577
578 static PyObject *spoolss_enumprinterdrivers(PyObject *self, PyObject *args,
579                                             PyObject *kw)
580 {
581         PyObject *po;
582         spoolss_policy_hnd_object *hnd;
583         WERROR werror;
584         PyObject *result;
585         PRINTER_DRIVER_CTR ctr;
586         int level = 1, i;
587         uint32 needed, num_drivers;
588         char *arch = "Windows NT x86";
589         static char *kwlist[] = {"hnd", "level", "arch", NULL};
590
591         /* Parse parameters */
592
593         if (!PyArg_ParseTupleAndKeywords(args, kw, "O!|is", kwlist, 
594                                          &spoolss_policy_hnd_type, &po, &level, &arch))
595                 return NULL;
596         
597         hnd = (spoolss_policy_hnd_object *)po;
598         
599         /* Call rpc function */
600         
601         werror = cli_spoolss_enumprinterdrivers(
602                 hnd->cli, hnd->mem_ctx, 0, &needed, level, arch,
603                 &num_drivers, &ctr);
604
605         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
606                 werror = cli_spoolss_enumprinterdrivers(
607                         hnd->cli, hnd->mem_ctx, needed, NULL, level, arch, 
608                         &num_drivers, &ctr);
609
610         /* Return value */
611         
612         result = Py_None;
613
614         if (!W_ERROR_IS_OK(werror))
615                 goto done;
616
617         switch (level) {
618         case 1:
619                 result = PyList_New(num_drivers);
620                 
621                 for (i = 0; i < num_drivers; i++) {
622                         PyObject *value;
623                         
624                         value = from_struct(&ctr.info1, py_DRIVER_INFO_1);
625                         PyList_SetItem(result, i, value);
626                 }
627                 
628                 break;
629         case 2: 
630                 result = PyList_New(num_drivers);
631
632                 for(i = 0; i < num_drivers; i++) {
633                         PyObject *value;
634
635                         value = from_struct(&ctr.info2, py_DRIVER_INFO_2);
636                         PyList_SetItem(result, i, value);
637                 }
638
639                 break;
640         case 6: 
641                 result = PyList_New(num_drivers);
642
643                 for(i = 0; i < num_drivers; i++) {
644                         PyObject *value;
645
646                         value = from_struct(&ctr.info2, py_DRIVER_INFO_6);
647                         PyList_SetItem(result, i, value);
648                 }
649
650                 break;
651         default:
652                 result = Py_None;
653                 break;
654         }
655         
656  done:
657         Py_INCREF(result);
658         return result;
659 }
660
661 /* Convert a security descriptor to a Python dict */
662
663 static PyObject *PySID_FromSID(DOM_SID *sid)
664 {
665         fstring sidstr;
666
667         if (!sid) {
668                 Py_INCREF(Py_None);
669                 return Py_None;
670         }
671
672         if (sid_to_string(sidstr, sid))
673                 return PyString_FromString(sidstr);
674
675         Py_INCREF(Py_None);
676         return Py_None; 
677 }
678
679 static PyObject *PyACE_FromACE(SEC_ACE *ace)
680 {
681         PyObject *obj;
682
683         if (!ace) {
684                 Py_INCREF(Py_None);
685                 return Py_None;
686         }
687
688         obj = PyDict_New();
689
690         PyDict_SetItemString(obj, "type", PyInt_FromLong(ace->type));
691         PyDict_SetItemString(obj, "flags", PyInt_FromLong(ace->flags));
692         PyDict_SetItemString(obj, "mask", PyInt_FromLong(ace->info.mask));
693
694         PyDict_SetItemString(obj, "trustee", PySID_FromSID(&ace->trustee));
695
696         return obj;
697 }
698
699 static PyObject *PyACL_FromACL(SEC_ACL *acl)
700 {
701         PyObject *obj, *ace_list;
702         int i;
703
704         if (!acl) {
705                 Py_INCREF(Py_None);
706                 return Py_None;
707         }
708
709         obj = PyDict_New();
710
711         PyDict_SetItemString(obj, "revision", PyInt_FromLong(acl->revision));
712
713         ace_list = PyList_New(acl->num_aces);
714
715         for (i = 0; i < acl->num_aces; i++)
716                 PyList_SetItem(ace_list, i, PyACE_FromACE(&acl->ace[i]));
717
718         PyDict_SetItemString(obj, "ace_list", ace_list);
719
720         return obj;
721 }
722
723 static PyObject *PySECDESC_FromSECDESC(SEC_DESC *sd)
724 {
725         PyObject *obj = PyDict_New();
726
727         PyDict_SetItemString(obj, "revision", PyInt_FromLong(sd->revision));
728         PyDict_SetItemString(obj, "type", PyInt_FromLong(sd->type));
729
730         PyDict_SetItemString(obj, "owner_sid", PySID_FromSID(sd->owner_sid));
731         PyDict_SetItemString(obj, "group_sid", PySID_FromSID(sd->grp_sid));
732
733         PyDict_SetItemString(obj, "dacl", PyACL_FromACL(sd->dacl));
734         PyDict_SetItemString(obj, "sacl", PyACL_FromACL(sd->sacl));
735
736         return obj;
737 }
738
739 PyObject *PyDEVICEMODE_FromDEVICEMODE(DEVICEMODE *devmode)
740 {
741         PyObject *obj;
742
743         obj = from_struct(devmode, py_DEVICEMODE);
744
745         PyDict_SetItemString(obj, "private",
746                              PyString_FromStringAndSize(devmode->private, 
747                                                         devmode->driverextra));
748
749         return obj;
750 }
751
752 /* Fetch printer information */
753
754 static PyObject *spoolss_getprinter(PyObject *self, PyObject *args,
755                                     PyObject *kw)
756 {
757         PyObject *po;
758         spoolss_policy_hnd_object *hnd;
759         WERROR werror;
760         PyObject *result;
761         PRINTER_INFO_CTR ctr;
762         int level = 1;
763         uint32 needed;
764         static char *kwlist[] = {"hnd", "level", NULL};
765
766         /* Parse parameters */
767
768         if (!PyArg_ParseTupleAndKeywords(args, kw, "O!|i", kwlist, 
769                                          &spoolss_policy_hnd_type, &po, &level))
770                 return NULL;
771         
772         hnd = (spoolss_policy_hnd_object *)po;
773         
774         /* Call rpc function */
775         
776         werror = cli_spoolss_getprinter(
777                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level, &ctr);
778
779         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
780                 werror = cli_spoolss_getprinter(
781                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
782                         level, &ctr);
783
784         /* Return value */
785
786         result = Py_None;
787
788         if (!W_ERROR_IS_OK(werror))
789                 goto done;
790
791         switch (level) {
792
793         case 0:
794                 result = from_struct(ctr.printers_0, py_PRINTER_INFO_0);
795
796                 break;
797
798         case 1:
799                 result = from_struct(ctr.printers_1, py_PRINTER_INFO_1);
800
801                 break;
802
803         case 2:
804                 result = from_struct(ctr.printers_2, py_PRINTER_INFO_2);
805
806                 PyDict_SetItemString(result, "security_descriptor", 
807                                      PySECDESC_FromSECDESC(
808                                              ctr.printers_2->secdesc));
809                 
810                 PyDict_SetItemString(result, "device_mode",
811                                      PyDEVICEMODE_FromDEVICEMODE(
812                                              ctr.printers_2->devmode));
813
814                 break;
815
816         case 3:
817                 result = from_struct(ctr.printers_3, py_PRINTER_INFO_3);
818
819                 PyDict_SetItemString(result, "security_descriptor",
820                                      PySECDESC_FromSECDESC(
821                                              ctr.printers_3->secdesc));
822                 break;
823
824         default:
825                 result = Py_None;
826                 break;
827         }
828  done:
829         Py_INCREF(result);
830         return result;
831 }
832
833 /* Set printer information */
834
835 static PyObject *spoolss_setprinter(PyObject *self, PyObject *args,
836                                     PyObject *kw)
837 {
838         PyObject *po;
839         spoolss_policy_hnd_object *hnd;
840         WERROR werror;
841         PyObject *result, *info;
842         PRINTER_INFO_CTR ctr;
843         int level = 1;
844         static char *kwlist[] = { "hnd", "dict", "level", NULL };
845         union {
846                 PRINTER_INFO_0 printers_0;
847                 PRINTER_INFO_1 printers_1;
848                 PRINTER_INFO_2 printers_2;
849                 PRINTER_INFO_3 printers_3;
850                 PRINTER_INFO_4 printers_4;
851                 PRINTER_INFO_5 printers_5;
852         } pinfo;
853
854         /* Parse parameters */
855
856         if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|i", kwlist, 
857                                          &spoolss_policy_hnd_type, &po, 
858                                          &PyDict_Type, &info, &level))
859                 return NULL;
860         
861         hnd = (spoolss_policy_hnd_object *)po;
862         
863         /* Fill in printer info */
864
865         ZERO_STRUCT(ctr);
866
867         switch (level) {
868         case 1:
869                 ctr.printers_1 = &pinfo.printers_1;
870                 to_struct(&pinfo.printers_1, info, py_PRINTER_INFO_1);
871                 break;
872         default:
873         }
874
875         /* Call rpc function */
876         
877         werror = cli_spoolss_setprinter(hnd->cli, hnd->mem_ctx, &hnd->pol,
878                                         level, &ctr, 0);
879
880         /* Return value */
881
882         result = Py_None;
883
884         if (!W_ERROR_IS_OK(werror))
885                 goto done;
886
887  done:
888         Py_INCREF(result);
889         return result;
890 }
891
892 /* Enumerate printers */
893
894 static PyObject *spoolss_enumprinters(PyObject *self, PyObject *args,
895                                       PyObject *kw)
896 {
897         WERROR werror;
898         PyObject *result, *creds = NULL;
899         PRINTER_INFO_CTR ctr;
900         int level = 1, flags = PRINTER_ENUM_LOCAL, i;
901         uint32 needed, num_printers;
902         static char *kwlist[] = {"server", "name", "level", "flags", 
903                                  "creds", NULL};
904         TALLOC_CTX *mem_ctx = NULL;
905         struct cli_state *cli = NULL;
906         char *server, *name = NULL;
907
908         /* Parse parameters */
909
910         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|siiO!", kwlist, 
911                                          &server, &name, &level, &flags, 
912                                          &PyDict_Type, &creds))
913                 return NULL;
914         
915         if (server[0] == '\\' && server[1] == '\\')
916                 server += 2;
917
918         mem_ctx = talloc_init();
919         cli = open_pipe_creds(server, creds, cli_spoolss_initialise, NULL);
920
921         /* Call rpc function */
922         
923         werror = cli_spoolss_enum_printers(
924                 cli, mem_ctx, 0, &needed, flags, level,
925                 &num_printers, &ctr);
926
927         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
928                 werror = cli_spoolss_enum_printers(
929                         cli, mem_ctx, needed, NULL, flags, level,
930                         &num_printers, &ctr);
931
932         /* Return value */
933         
934         result = Py_None;
935
936         if (!W_ERROR_IS_OK(werror))
937                 goto done;
938
939         result = PyList_New(num_printers);
940
941         switch (level) {
942         case 0: 
943                 for (i = 0; i < num_printers; i++) {
944                         PyObject *value;
945
946                         value = from_struct (
947                                 &ctr.printers_0[i], py_PRINTER_INFO_0);
948
949                         PyList_SetItem(result, i, value);
950                 }
951
952                 break;
953         case 1:
954                 for(i = 0; i < num_printers; i++) {
955                         PyObject *value;
956
957                         value = from_struct(
958                                 &ctr.printers_1[i], py_PRINTER_INFO_1);
959
960                         PyList_SetItem(result, i, value);
961                 }
962                 
963                 break;
964         case 2:
965                 for(i = 0; i < num_printers; i++) {
966                         PyObject *value;
967
968                         value = from_struct(
969                                 &ctr.printers_2[i], py_PRINTER_INFO_2);
970
971                         PyList_SetItem(result, i, value);
972                 }
973                 
974                 break;
975         case 3:
976                 for(i = 0; i < num_printers; i++) {
977                         PyObject *value;
978
979                         value = from_struct(
980                                 &ctr.printers_3[i], py_PRINTER_INFO_3);
981
982                         PyList_SetItem(result, i, value);
983                 }
984                 
985                 break;
986         }
987
988  done:
989         Py_INCREF(result);
990         return result;
991 }
992
993 /* 
994  * Method dispatch table
995  */
996
997 #include "python/py_spoolss_forms.h"
998
999 static PyMethodDef spoolss_methods[] = {
1000
1001         /* Open/close printer handles */
1002         
1003         { "openprinter", spoolss_openprinter, METH_VARARGS | METH_KEYWORDS, 
1004           "Open printer" },
1005         
1006         { "closeprinter", spoolss_closeprinter, METH_VARARGS, 
1007           "Close printer" },
1008
1009         /* Printer drivers */
1010
1011         { "getprinterdriver", spoolss_getprinterdriver, 
1012           METH_VARARGS | METH_KEYWORDS, "Fetch printer driver" },
1013
1014         { "enumprinterdrivers", spoolss_enumprinterdrivers,
1015           METH_VARARGS | METH_KEYWORDS, "Enumerate printer drivers" },
1016
1017         { "getprinter", spoolss_getprinter, METH_VARARGS | METH_KEYWORDS,
1018           "Fetch printer information" },
1019
1020         { "setprinter", spoolss_setprinter, METH_VARARGS | METH_KEYWORDS,
1021           "Set printer information" },
1022
1023         { "enumprinters", spoolss_enumprinters, METH_VARARGS | METH_KEYWORDS,
1024           "Enumerate printers" },
1025
1026         /* Forms */
1027
1028         { "enumforms", spoolss_enumforms, METH_VARARGS | METH_KEYWORDS,
1029           "Enumerate forms" },
1030
1031         { "setform", spoolss_setform, METH_VARARGS | METH_KEYWORDS,
1032           "Modify properties of a form" },
1033
1034         { "addform", spoolss_addform, METH_VARARGS | METH_KEYWORDS,
1035           "Insert a form" },
1036
1037         { "getform", spoolss_getform, METH_VARARGS | METH_KEYWORDS,
1038           "Fetch form properties" },
1039
1040         { "deleteform", spoolss_deleteform, METH_VARARGS | METH_KEYWORDS,
1041           "Delete a form" },
1042
1043         { "enumforms", spoolss_enumforms, METH_VARARGS | METH_KEYWORDS,
1044           "Delete a form" },
1045
1046         { NULL }
1047 };
1048
1049 /* Initialise constants */
1050
1051 struct spoolss_const {
1052         char *name;
1053         uint32 value;
1054 } spoolss_const_vals[] = {
1055         
1056         /* Access permissions */
1057
1058         { "MAXIMUM_ALLOWED_ACCESS", MAXIMUM_ALLOWED_ACCESS },
1059         { "SERVER_ALL_ACCESS", SERVER_ALL_ACCESS },
1060         { "PRINTER_ALL_ACCESS", PRINTER_ALL_ACCESS },
1061
1062         /* Printer enumeration flags */
1063
1064         { "PRINTER_ENUM_DEFAULT", PRINTER_ENUM_DEFAULT },
1065         { "PRINTER_ENUM_LOCAL", PRINTER_ENUM_LOCAL },
1066         { "PRINTER_ENUM_CONNECTIONS", PRINTER_ENUM_CONNECTIONS },
1067         { "PRINTER_ENUM_FAVORITE", PRINTER_ENUM_FAVORITE },
1068         { "PRINTER_ENUM_NAME", PRINTER_ENUM_NAME },
1069         { "PRINTER_ENUM_REMOTE", PRINTER_ENUM_REMOTE },
1070         { "PRINTER_ENUM_SHARED", PRINTER_ENUM_SHARED },
1071         { "PRINTER_ENUM_NETWORK", PRINTER_ENUM_NETWORK },
1072
1073         { NULL },
1074 };
1075
1076 static void const_init(PyObject *dict)
1077 {
1078         struct spoolss_const *tmp;
1079         PyObject *obj;
1080
1081         for (tmp = spoolss_const_vals; tmp->name; tmp++) {
1082                 obj = PyInt_FromLong(tmp->value);
1083                 PyDict_SetItemString(dict, tmp->name, obj);
1084         }
1085 }
1086
1087 /* Module initialisation */
1088
1089 void initspoolss(void)
1090 {
1091         PyObject *module, *dict;
1092
1093         /* Initialise module */
1094
1095         module = Py_InitModule("spoolss", spoolss_methods);
1096         dict = PyModule_GetDict(module);
1097
1098         /* Make spools_error global an exception we can raise when an error
1099            occurs. */
1100
1101         spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
1102         PyDict_SetItemString(dict, "error", spoolss_error);
1103
1104         spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
1105         PyDict_SetItemString(dict, "werror", spoolss_werror);
1106
1107         /* Initialise policy handle object */
1108
1109         spoolss_policy_hnd_type.ob_type = &PyType_Type;
1110
1111         /* Initialise constants */
1112
1113         const_init(dict);
1114
1115         /* Do samba initialisation */
1116
1117         py_samba_init();
1118 }