Added enumprinterkey function.
[samba.git] / source / 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 "python/py_spoolss.h"
22
23 /* Exceptions this module can raise */
24
25 PyObject *spoolss_error, *spoolss_werror;
26
27 /*
28  * Routines to convert from python hashes to Samba structures
29  */
30
31 PyObject *new_spoolss_policy_hnd_object(struct cli_state *cli, 
32                                         TALLOC_CTX *mem_ctx, POLICY_HND *pol)
33 {
34         spoolss_policy_hnd_object *o;
35
36         o = PyObject_New(spoolss_policy_hnd_object, &spoolss_policy_hnd_type);
37
38         o->cli = cli;
39         o->mem_ctx = mem_ctx;
40         memcpy(&o->pol, pol, sizeof(POLICY_HND));
41
42         return (PyObject*)o;
43 }
44      
45 /* 
46  * Method dispatch table
47  */
48
49 static PyMethodDef spoolss_methods[] = {
50
51         /* Open/close printer handles */
52         
53         { "openprinter", (PyCFunction)spoolss_openprinter, METH_VARARGS | METH_KEYWORDS, 
54           "Open a printer by name in UNC format.
55
56 Optionally a dictionary of (domain, username, password) may be given in
57 which case they are used when opening the RPC pipe.  An access mask may
58 also be given which defaults to MAXIMUM_ALLOWED_ACCESS.
59
60 Example:
61
62 >>> hnd = spoolss.openprinter(\"\\\\\\\\NPSD-PDC2\\\\meanie\")"},
63         
64         { "closeprinter", spoolss_closeprinter, METH_VARARGS, 
65           "Close a printer handle opened with openprinter or addprinter.
66
67 Example:
68
69 >>> spoolss.closeprinter(hnd)"},
70
71         { "addprinterex", (PyCFunction)spoolss_addprinterex, METH_VARARGS, 
72           "addprinterex()"},
73
74         /* Server enumeratation functions */
75
76         { "enumprinters", (PyCFunction)spoolss_enumprinters, 
77           METH_VARARGS | METH_KEYWORDS,
78           "Enumerate printers on a print server.
79
80 Return a list of printers on a print server.  The credentials, info level
81 and flags may be specified as keyword arguments.
82
83 Example:
84
85 >>> print spoolss.enumprinters(\"\\\\\\\\npsd-pdc2\")
86 [{'comment': 'i am a comment', 'printer_name': 'meanie', 'flags': 8388608, 
87   'description': 'meanie,Generic / Text Only,i am a location'}, 
88  {'comment': '', 'printer_name': 'fileprint', 'flags': 8388608, 
89   'description': 'fileprint,Generic / Text Only,'}]"},
90
91         { "enumports", (PyCFunction)spoolss_enumports, 
92           METH_VARARGS | METH_KEYWORDS,
93           "Enumerate ports on a print server.
94
95 Return a list of ports on a print server.
96
97 Example:
98
99 >>> print spoolss.enumports(\"\\\\\\\\npsd-pdc2\")
100 [{'name': 'LPT1:'}, {'name': 'LPT2:'}, {'name': 'COM1:'}, {'name': 'COM2:'}, 
101  {'name': 'FILE:'}, {'name': '\\\\nautilus1\\zpekt3r'}]"},
102
103         { "enumprinterdrivers", (PyCFunction)spoolss_enumprinterdrivers, 
104           METH_VARARGS | METH_KEYWORDS, 
105           "Enumerate printer drivers on a print server.
106
107 Return a list of printer drivers."},
108         /* Miscellaneous other commands */
109
110         { "getprinterdriverdir", (PyCFunction)spoolss_getprinterdriverdir, 
111           METH_VARARGS | METH_KEYWORDS, 
112           "Return printer driver directory.
113
114 Return the printer driver directory for a given architecture.  The 
115 architecture defaults to \"Windows NT x86\"."},
116
117         /* Other stuff - this should really go into a samba config module
118            but for the moment let's leave it here. */
119
120         { "setup_logging", (PyCFunction)py_setup_logging, 
121           METH_VARARGS | METH_KEYWORDS, 
122           "Set up debug logging.
123
124 Initialises Samba's debug logging system.  One argument is expected which
125 is a boolean specifying whether debugging is interactive and sent to stdout
126 or logged to a file.
127
128 Example:
129
130 >>> spoolss.setup_logging(interactive = 1)" },
131
132         { "get_debuglevel", (PyCFunction)get_debuglevel, 
133           METH_VARARGS, 
134           "Set the current debug level.
135
136 Example:
137
138 >>> spoolss.get_debuglevel()
139 0" },
140
141         { "set_debuglevel", (PyCFunction)set_debuglevel, 
142           METH_VARARGS, 
143           "Get the current debug level.
144
145 Example:
146
147 >>> spoolss.set_debuglevel(10)" },
148
149         /* Printer driver routines */
150         
151         { "addprinterdriver", (PyCFunction)spoolss_addprinterdriver, 
152           METH_VARARGS | METH_KEYWORDS, 
153           "Add a printer driver." },
154
155         { "addprinterdriverex", (PyCFunction)spoolss_addprinterdriverex, 
156           METH_VARARGS | METH_KEYWORDS, 
157           "Add a printer driver." },
158
159         { "deleteprinterdriver", (PyCFunction)spoolss_deleteprinterdriver, 
160           METH_VARARGS | METH_KEYWORDS, 
161           "Delete a printer driver." },
162
163         { "deleteprinterdriverex", (PyCFunction)spoolss_deleteprinterdriverex, 
164           METH_VARARGS | METH_KEYWORDS, 
165           "Delete a printer driver." },
166
167         { NULL }
168 };
169
170 /* Methods attached to a spoolss handle object */
171
172 static PyMethodDef spoolss_hnd_methods[] = {
173
174         /* Printer info */
175
176         { "getprinter", (PyCFunction)spoolss_hnd_getprinter, 
177            METH_VARARGS | METH_KEYWORDS,
178           "Get printer information.
179
180 Return a dictionary of print information.  The info level defaults to 1.
181
182 Example:
183
184 >>> hnd.getprinter()
185 {'comment': 'i am a comment', 'printer_name': '\\\\NPSD-PDC2\\meanie', 
186  'description': '\\\\NPSD-PDC2\\meanie,Generic / Text Only,i am a location',
187  'flags': 8388608}"},
188
189         { "setprinter", (PyCFunction)spoolss_hnd_setprinter, 
190           METH_VARARGS | METH_KEYWORDS,
191           "Set printer information."},
192
193         /* Printer drivers */
194
195         { "getprinterdriver", (PyCFunction)spoolss_hnd_getprinterdriver, 
196           METH_VARARGS | METH_KEYWORDS, 
197           "Return printer driver information.
198
199 Return a dictionary of printer driver information for the printer driver
200 bound to this printer."},
201
202         /* Forms */
203
204         { "enumforms", (PyCFunction)spoolss_hnd_enumforms, 
205           METH_VARARGS | METH_KEYWORDS,
206           "Enumerate supported forms.
207
208 Return a list of forms supported by this printer or print server."},
209
210         { "setform", (PyCFunction)spoolss_hnd_setform, 
211           METH_VARARGS | METH_KEYWORDS,
212           "Set form data.
213
214 Set the form given by the dictionary argument."},
215
216         { "addform", (PyCFunction)spoolss_hnd_addform, 
217           METH_VARARGS | METH_KEYWORDS,
218           "Add a new form." },
219
220         { "getform", (PyCFunction)spoolss_hnd_getform, 
221           METH_VARARGS | METH_KEYWORDS,
222           "Get form properties." },
223
224         { "deleteform", (PyCFunction)spoolss_hnd_deleteform, 
225           METH_VARARGS | METH_KEYWORDS,
226           "Delete a form." },
227
228         /* Job related methods */
229
230         { "enumjobs", (PyCFunction)spoolss_hnd_enumjobs, 
231           METH_VARARGS | METH_KEYWORDS,
232           "Enumerate jobs." },
233
234         { "setjob", (PyCFunction)spoolss_hnd_setjob, 
235           METH_VARARGS | METH_KEYWORDS,
236           "Set job information." },
237
238         { "getjob", (PyCFunction)spoolss_hnd_getjob, 
239           METH_VARARGS | METH_KEYWORDS,
240           "Get job information." },
241
242         { "startpageprinter", (PyCFunction)spoolss_hnd_startpageprinter, 
243            METH_VARARGS | METH_KEYWORDS,
244           "Notify spooler that a page is about to be printed." },
245
246         { "endpageprinter", (PyCFunction)spoolss_hnd_endpageprinter, 
247            METH_VARARGS | METH_KEYWORDS,
248           "Notify spooler that a page is about to be printed." },
249
250         { "startdocprinter", (PyCFunction)spoolss_hnd_startdocprinter, 
251            METH_VARARGS | METH_KEYWORDS,
252           "Notify spooler that a document is about to be printed." },
253
254         { "enddocprinter", (PyCFunction)spoolss_hnd_enddocprinter, 
255            METH_VARARGS | METH_KEYWORDS,
256           "Notify spooler that a document is about to be printed." },
257
258         { "writeprinter", (PyCFunction)spoolss_hnd_writeprinter,
259           METH_VARARGS | METH_KEYWORDS,
260           "Write job data to a printer." },
261
262         { "addjob", (PyCFunction)spoolss_hnd_addjob,
263           METH_VARARGS | METH_KEYWORDS,
264           "Add a job to the list of print jobs." },
265
266         /* Printer data */
267
268         { "getprinterdata", (PyCFunction)spoolss_hnd_getprinterdata,
269            METH_VARARGS | METH_KEYWORDS,
270           "Get printer data." },
271
272         { "setprinterdata", (PyCFunction)spoolss_hnd_setprinterdata,
273            METH_VARARGS | METH_KEYWORDS,
274           "Set printer data." },
275
276         { "enumprinterdata", (PyCFunction)spoolss_hnd_enumprinterdata,
277            METH_VARARGS | METH_KEYWORDS,
278           "Enumerate printer data." },
279
280         { "deleteprinterdata", (PyCFunction)spoolss_hnd_deleteprinterdata,
281            METH_VARARGS | METH_KEYWORDS,
282           "Delete printer data." },
283
284         { "getprinterdataex", (PyCFunction)spoolss_hnd_getprinterdataex,
285            METH_VARARGS | METH_KEYWORDS,
286           "Get printer data." },
287
288         { "setprinterdataex", (PyCFunction)spoolss_hnd_setprinterdataex,
289            METH_VARARGS | METH_KEYWORDS,
290           "Set printer data." },
291
292         { "enumprinterdataex", (PyCFunction)spoolss_hnd_enumprinterdataex,
293            METH_VARARGS | METH_KEYWORDS,
294           "Enumerate printer data." },
295
296         { "deleteprinterdataex", (PyCFunction)spoolss_hnd_deleteprinterdataex,
297            METH_VARARGS | METH_KEYWORDS,
298           "Delete printer data." },
299
300         { "enumprinterkey", (PyCFunction)spoolss_hnd_enumprinterkey,
301            METH_VARARGS | METH_KEYWORDS,
302           "Enumerate printer key." },
303
304 #if 0
305         /* Not implemented */
306
307         { "deleteprinterkey", (PyCFunction)spoolss_hnd_deleteprinterkey,
308            METH_VARARGS | METH_KEYWORDS,
309           "Delete printer key." },
310 #endif
311
312         { NULL }
313
314 };
315
316 static void py_policy_hnd_dealloc(PyObject* self)
317 {
318         spoolss_policy_hnd_object *hnd;
319
320         /* Close down policy handle and free talloc context */
321
322         hnd = (spoolss_policy_hnd_object*)self;
323
324         cli_shutdown(hnd->cli);
325         talloc_destroy(hnd->mem_ctx);
326
327         PyObject_Del(self);
328 }
329
330 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
331 {
332         return Py_FindMethod(spoolss_hnd_methods, self, attrname);
333 }
334
335 static char spoolss_type_doc[] = 
336 "Python wrapper for Windows NT SPOOLSS rpc pipe.";
337
338 PyTypeObject spoolss_policy_hnd_type = {
339         PyObject_HEAD_INIT(NULL)
340         0,
341         "spoolss.hnd",
342         sizeof(spoolss_policy_hnd_object),
343         0,
344         py_policy_hnd_dealloc,  /* tp_dealloc*/
345         0,                      /* tp_print*/
346         py_policy_hnd_getattr,  /* tp_getattr*/
347         0,                      /* tp_setattr*/
348         0,                      /* tp_compare*/
349         0,                      /* tp_repr*/
350         0,                      /* tp_as_number*/
351         0,                      /* tp_as_sequence*/
352         0,                      /* tp_as_mapping*/
353         0,                      /* tp_hash */
354         0,                      /* tp_call */
355         0,                      /* tp_str */
356         0,                      /* tp_getattro */
357         0,                      /* tp_setattro */
358         0,                      /* tp_as_buffer*/
359         Py_TPFLAGS_DEFAULT,     /* tp_flags */
360         spoolss_type_doc,       /* tp_doc */
361 };
362
363 /* Initialise constants */
364
365 static struct const_vals {
366         char *name;
367         uint32 value;
368 } module_const_vals[] = {
369         
370         /* Access permissions */
371
372         { "MAXIMUM_ALLOWED_ACCESS", MAXIMUM_ALLOWED_ACCESS },
373         { "SERVER_ALL_ACCESS", SERVER_ALL_ACCESS },
374         { "SERVER_READ", SERVER_READ },
375         { "SERVER_WRITE", SERVER_WRITE },
376         { "SERVER_EXECUTE", SERVER_EXECUTE },
377         { "SERVER_ACCESS_ADMINISTER", SERVER_ACCESS_ADMINISTER },
378         { "SERVER_ACCESS_ENUMERATE", SERVER_ACCESS_ENUMERATE },
379         { "PRINTER_ALL_ACCESS", PRINTER_ALL_ACCESS },
380         { "PRINTER_READ", PRINTER_READ },
381         { "PRINTER_WRITE", PRINTER_WRITE },
382         { "PRINTER_EXECUTE", PRINTER_EXECUTE },
383         { "PRINTER_ACCESS_ADMINISTER", PRINTER_ACCESS_ADMINISTER },
384         { "PRINTER_ACCESS_USE", PRINTER_ACCESS_USE },
385         { "JOB_ACCESS_ADMINISTER", JOB_ACCESS_ADMINISTER },
386         { "JOB_ALL_ACCESS", JOB_ALL_ACCESS },
387         { "JOB_READ", JOB_READ },
388         { "JOB_WRITE", JOB_WRITE },
389         { "JOB_EXECUTE", JOB_EXECUTE },
390         { "STANDARD_RIGHTS_ALL_ACCESS", STANDARD_RIGHTS_ALL_ACCESS },
391         { "STANDARD_RIGHTS_EXECUTE_ACCESS", STANDARD_RIGHTS_EXECUTE_ACCESS },
392         { "STANDARD_RIGHTS_READ_ACCESS", STANDARD_RIGHTS_READ_ACCESS },
393         { "STANDARD_RIGHTS_REQUIRED_ACCESS", STANDARD_RIGHTS_REQUIRED_ACCESS },
394         { "STANDARD_RIGHTS_WRITE_ACCESS", STANDARD_RIGHTS_WRITE_ACCESS },
395
396         /* Printer enumeration flags */
397
398         { "PRINTER_ENUM_DEFAULT", PRINTER_ENUM_DEFAULT },
399         { "PRINTER_ENUM_LOCAL", PRINTER_ENUM_LOCAL },
400         { "PRINTER_ENUM_CONNECTIONS", PRINTER_ENUM_CONNECTIONS },
401         { "PRINTER_ENUM_FAVORITE", PRINTER_ENUM_FAVORITE },
402         { "PRINTER_ENUM_NAME", PRINTER_ENUM_NAME },
403         { "PRINTER_ENUM_REMOTE", PRINTER_ENUM_REMOTE },
404         { "PRINTER_ENUM_SHARED", PRINTER_ENUM_SHARED },
405         { "PRINTER_ENUM_NETWORK", PRINTER_ENUM_NETWORK },
406
407         /* Form types */
408
409         { "FORM_USER", FORM_USER },
410         { "FORM_BUILTIN", FORM_BUILTIN },
411         { "FORM_PRINTER", FORM_PRINTER },
412
413         /* WERRORs */
414
415         { "WERR_OK", 0 },
416         { "WERR_BADFILE", 2 },
417         { "WERR_ACCESS_DENIED", 5 },
418         { "WERR_BADFID", 6 },
419         { "WERR_BADFUNC", 1 },
420         { "WERR_INSUFFICIENT_BUFFER", 122 },
421         { "WERR_NO_SUCH_SHARE", 67 },
422         { "WERR_ALREADY_EXISTS", 80 },
423         { "WERR_INVALID_PARAM", 87 },
424         { "WERR_NOT_SUPPORTED", 50 },
425         { "WERR_BAD_PASSWORD", 86 },
426         { "WERR_NOMEM", 8 },
427         { "WERR_INVALID_NAME", 123 },
428         { "WERR_UNKNOWN_LEVEL", 124 },
429         { "WERR_OBJECT_PATH_INVALID", 161 },
430         { "WERR_NO_MORE_ITEMS", 259 },
431         { "WERR_MORE_DATA", 234 },
432         { "WERR_UNKNOWN_PRINTER_DRIVER", 1797 },
433         { "WERR_INVALID_PRINTER_NAME", 1801 },
434         { "WERR_PRINTER_ALREADY_EXISTS", 1802 },
435         { "WERR_INVALID_DATATYPE", 1804 },
436         { "WERR_INVALID_ENVIRONMENT", 1805 },
437         { "WERR_INVALID_FORM_NAME", 1902 },
438         { "WERR_INVALID_FORM_SIZE", 1903 },
439         { "WERR_BUF_TOO_SMALL", 2123 },
440         { "WERR_JOB_NOT_FOUND", 2151 },
441         { "WERR_DEST_NOT_FOUND", 2152 },
442         { "WERR_NOT_LOCAL_DOMAIN", 2320 },
443         { "WERR_PRINTER_DRIVER_IN_USE", 3001 },
444         { "WERR_STATUS_MORE_ENTRIES  ", 0x0105 },
445
446         /* Job control constants */
447
448         { "JOB_CONTROL_PAUSE", JOB_CONTROL_PAUSE },
449         { "JOB_CONTROL_RESUME", JOB_CONTROL_RESUME },
450         { "JOB_CONTROL_CANCEL", JOB_CONTROL_CANCEL },
451         { "JOB_CONTROL_RESTART", JOB_CONTROL_RESTART },
452         { "JOB_CONTROL_DELETE", JOB_CONTROL_DELETE },
453
454         { NULL },
455 };
456
457 static void const_init(PyObject *dict)
458 {
459         struct const_vals *tmp;
460         PyObject *obj;
461
462         for (tmp = module_const_vals; tmp->name; tmp++) {
463                 obj = PyInt_FromLong(tmp->value);
464                 PyDict_SetItemString(dict, tmp->name, obj);
465                 Py_DECREF(obj);
466         }
467 }
468
469 /* Module initialisation */
470
471 void initspoolss(void)
472 {
473         PyObject *module, *dict;
474
475         /* Initialise module */
476
477         module = Py_InitModule("spoolss", spoolss_methods);
478         dict = PyModule_GetDict(module);
479
480         /* Exceptions we can raise */
481
482         spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
483         PyDict_SetItemString(dict, "error", spoolss_error);
484
485         spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
486         PyDict_SetItemString(dict, "werror", spoolss_werror);
487
488         /* Initialise policy handle object */
489
490         spoolss_policy_hnd_type.ob_type = &PyType_Type;
491
492         PyDict_SetItemString(dict, "spoolss.hnd", 
493                              (PyObject *)&spoolss_policy_hnd_type);
494
495         /* Initialise constants */
496
497         const_init(dict);
498
499         /* Do samba initialisation */
500
501         py_samba_init();
502 }