Check in examples for tdb packer
[kai/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         { NULL }
301
302 };
303
304 static void py_policy_hnd_dealloc(PyObject* self)
305 {
306         spoolss_policy_hnd_object *hnd;
307
308         /* Close down policy handle and free talloc context */
309
310         hnd = (spoolss_policy_hnd_object*)self;
311
312         cli_shutdown(hnd->cli);
313         talloc_destroy(hnd->mem_ctx);
314
315         PyObject_Del(self);
316 }
317
318 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
319 {
320         return Py_FindMethod(spoolss_hnd_methods, self, attrname);
321 }
322
323 static char spoolss_type_doc[] = 
324 "Python wrapper for Windows NT SPOOLSS rpc pipe.";
325
326 PyTypeObject spoolss_policy_hnd_type = {
327         PyObject_HEAD_INIT(NULL)
328         0,
329         "spoolss.hnd",
330         sizeof(spoolss_policy_hnd_object),
331         0,
332         py_policy_hnd_dealloc,  /* tp_dealloc*/
333         0,                      /* tp_print*/
334         py_policy_hnd_getattr,  /* tp_getattr*/
335         0,                      /* tp_setattr*/
336         0,                      /* tp_compare*/
337         0,                      /* tp_repr*/
338         0,                      /* tp_as_number*/
339         0,                      /* tp_as_sequence*/
340         0,                      /* tp_as_mapping*/
341         0,                      /* tp_hash */
342         0,                      /* tp_call */
343         0,                      /* tp_str */
344         0,                      /* tp_getattro */
345         0,                      /* tp_setattro */
346         0,                      /* tp_as_buffer*/
347         Py_TPFLAGS_DEFAULT,     /* tp_flags */
348         spoolss_type_doc,       /* tp_doc */
349 };
350
351 /* Initialise constants */
352
353 static struct const_vals {
354         char *name;
355         uint32 value;
356 } module_const_vals[] = {
357         
358         /* Access permissions */
359
360         { "MAXIMUM_ALLOWED_ACCESS", MAXIMUM_ALLOWED_ACCESS },
361         { "SERVER_ALL_ACCESS", SERVER_ALL_ACCESS },
362         { "SERVER_READ", SERVER_READ },
363         { "SERVER_WRITE", SERVER_WRITE },
364         { "SERVER_EXECUTE", SERVER_EXECUTE },
365         { "SERVER_ACCESS_ADMINISTER", SERVER_ACCESS_ADMINISTER },
366         { "SERVER_ACCESS_ENUMERATE", SERVER_ACCESS_ENUMERATE },
367         { "PRINTER_ALL_ACCESS", PRINTER_ALL_ACCESS },
368         { "PRINTER_READ", PRINTER_READ },
369         { "PRINTER_WRITE", PRINTER_WRITE },
370         { "PRINTER_EXECUTE", PRINTER_EXECUTE },
371         { "PRINTER_ACCESS_ADMINISTER", PRINTER_ACCESS_ADMINISTER },
372         { "PRINTER_ACCESS_USE", PRINTER_ACCESS_USE },
373         { "JOB_ACCESS_ADMINISTER", JOB_ACCESS_ADMINISTER },
374         { "JOB_ALL_ACCESS", JOB_ALL_ACCESS },
375         { "JOB_READ", JOB_READ },
376         { "JOB_WRITE", JOB_WRITE },
377         { "JOB_EXECUTE", JOB_EXECUTE },
378         { "STANDARD_RIGHTS_ALL_ACCESS", STANDARD_RIGHTS_ALL_ACCESS },
379         { "STANDARD_RIGHTS_EXECUTE_ACCESS", STANDARD_RIGHTS_EXECUTE_ACCESS },
380         { "STANDARD_RIGHTS_READ_ACCESS", STANDARD_RIGHTS_READ_ACCESS },
381         { "STANDARD_RIGHTS_REQUIRED_ACCESS", STANDARD_RIGHTS_REQUIRED_ACCESS },
382         { "STANDARD_RIGHTS_WRITE_ACCESS", STANDARD_RIGHTS_WRITE_ACCESS },
383
384         /* Printer enumeration flags */
385
386         { "PRINTER_ENUM_DEFAULT", PRINTER_ENUM_DEFAULT },
387         { "PRINTER_ENUM_LOCAL", PRINTER_ENUM_LOCAL },
388         { "PRINTER_ENUM_CONNECTIONS", PRINTER_ENUM_CONNECTIONS },
389         { "PRINTER_ENUM_FAVORITE", PRINTER_ENUM_FAVORITE },
390         { "PRINTER_ENUM_NAME", PRINTER_ENUM_NAME },
391         { "PRINTER_ENUM_REMOTE", PRINTER_ENUM_REMOTE },
392         { "PRINTER_ENUM_SHARED", PRINTER_ENUM_SHARED },
393         { "PRINTER_ENUM_NETWORK", PRINTER_ENUM_NETWORK },
394
395         /* Form types */
396
397         { "FORM_USER", FORM_USER },
398         { "FORM_BUILTIN", FORM_BUILTIN },
399         { "FORM_PRINTER", FORM_PRINTER },
400
401         /* WERRORs */
402
403         { "WERR_OK", 0 },
404         { "WERR_BADFILE", 2 },
405         { "WERR_ACCESS_DENIED", 5 },
406         { "WERR_BADFID", 6 },
407         { "WERR_BADFUNC", 1 },
408         { "WERR_INSUFFICIENT_BUFFER", 122 },
409         { "WERR_NO_SUCH_SHARE", 67 },
410         { "WERR_ALREADY_EXISTS", 80 },
411         { "WERR_INVALID_PARAM", 87 },
412         { "WERR_NOT_SUPPORTED", 50 },
413         { "WERR_BAD_PASSWORD", 86 },
414         { "WERR_NOMEM", 8 },
415         { "WERR_INVALID_NAME", 123 },
416         { "WERR_UNKNOWN_LEVEL", 124 },
417         { "WERR_OBJECT_PATH_INVALID", 161 },
418         { "WERR_NO_MORE_ITEMS", 259 },
419         { "WERR_MORE_DATA", 234 },
420         { "WERR_UNKNOWN_PRINTER_DRIVER", 1797 },
421         { "WERR_INVALID_PRINTER_NAME", 1801 },
422         { "WERR_PRINTER_ALREADY_EXISTS", 1802 },
423         { "WERR_INVALID_DATATYPE", 1804 },
424         { "WERR_INVALID_ENVIRONMENT", 1805 },
425         { "WERR_INVALID_FORM_NAME", 1902 },
426         { "WERR_INVALID_FORM_SIZE", 1903 },
427         { "WERR_BUF_TOO_SMALL", 2123 },
428         { "WERR_JOB_NOT_FOUND", 2151 },
429         { "WERR_DEST_NOT_FOUND", 2152 },
430         { "WERR_NOT_LOCAL_DOMAIN", 2320 },
431         { "WERR_PRINTER_DRIVER_IN_USE", 3001 },
432         { "WERR_STATUS_MORE_ENTRIES  ", 0x0105 },
433
434         /* Job control constants */
435
436         { "JOB_CONTROL_PAUSE", JOB_CONTROL_PAUSE },
437         { "JOB_CONTROL_RESUME", JOB_CONTROL_RESUME },
438         { "JOB_CONTROL_CANCEL", JOB_CONTROL_CANCEL },
439         { "JOB_CONTROL_RESTART", JOB_CONTROL_RESTART },
440         { "JOB_CONTROL_DELETE", JOB_CONTROL_DELETE },
441
442         { NULL },
443 };
444
445 static void const_init(PyObject *dict)
446 {
447         struct const_vals *tmp;
448         PyObject *obj;
449
450         for (tmp = module_const_vals; tmp->name; tmp++) {
451                 obj = PyInt_FromLong(tmp->value);
452                 PyDict_SetItemString(dict, tmp->name, obj);
453                 Py_DECREF(obj);
454         }
455 }
456
457 /* Module initialisation */
458
459 void initspoolss(void)
460 {
461         PyObject *module, *dict;
462
463         /* Initialise module */
464
465         module = Py_InitModule("spoolss", spoolss_methods);
466         dict = PyModule_GetDict(module);
467
468         /* Exceptions we can raise */
469
470         spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
471         PyDict_SetItemString(dict, "error", spoolss_error);
472
473         spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
474         PyDict_SetItemString(dict, "werror", spoolss_werror);
475
476         /* Initialise policy handle object */
477
478         spoolss_policy_hnd_type.ob_type = &PyType_Type;
479
480         PyDict_SetItemString(dict, "spoolss.hnd", 
481                              (PyObject *)&spoolss_policy_hnd_type);
482
483         /* Initialise constants */
484
485         const_init(dict);
486
487         /* Do samba initialisation */
488
489         py_samba_init();
490 }