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