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