2 Python wrappers for DCERPC/SMB client routines.
4 Copyright (C) Tim Potter, 2002
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.
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.
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.
21 #include "python/py_spoolss.h"
23 /* Exceptions this module can raise */
25 PyObject *spoolss_error, *spoolss_werror;
28 * Routines to convert from python hashes to Samba structures
31 PyObject *new_spoolss_policy_hnd_object(struct cli_state *cli,
32 TALLOC_CTX *mem_ctx, POLICY_HND *pol)
34 spoolss_policy_hnd_object *o;
36 o = PyObject_New(spoolss_policy_hnd_object, &spoolss_policy_hnd_type);
40 memcpy(&o->pol, pol, sizeof(POLICY_HND));
46 * Method dispatch table
49 static PyMethodDef spoolss_methods[] = {
51 /* Open/close printer handles */
53 { "openprinter", (PyCFunction)spoolss_openprinter, METH_VARARGS | METH_KEYWORDS,
54 "Open a printer by name in UNC format.
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.
62 >>> hnd = spoolss.openprinter(\"\\\\\\\\NPSD-PDC2\\\\meanie\")"},
64 { "closeprinter", spoolss_closeprinter, METH_VARARGS,
65 "Close a printer handle opened with openprinter or addprinter.
69 >>> spoolss.closeprinter(hnd)"},
71 { "addprinterex", (PyCFunction)spoolss_addprinterex, METH_VARARGS,
74 /* Server enumeratation functions */
76 { "enumprinters", (PyCFunction)spoolss_enumprinters,
77 METH_VARARGS | METH_KEYWORDS,
78 "Enumerate printers on a print server.
80 Return a list of printers on a print server. The credentials, info level
81 and flags may be specified as keyword arguments.
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,'}]"},
91 { "enumports", (PyCFunction)spoolss_enumports,
92 METH_VARARGS | METH_KEYWORDS,
93 "Enumerate ports on a print server.
95 Return a list of ports on a print server.
99 >>> print spoolss.enumports(\"\\\\\\\\npsd-pdc2\")
100 [{'name': 'LPT1:'}, {'name': 'LPT2:'}, {'name': 'COM1:'}, {'name': 'COM2:'},
101 {'name': 'FILE:'}, {'name': '\\\\nautilus1\\zpekt3r'}]"},
103 { "enumprinterdrivers", (PyCFunction)spoolss_enumprinterdrivers,
104 METH_VARARGS | METH_KEYWORDS,
105 "Enumerate printer drivers on a print server.
107 Return a list of printer drivers."},
108 /* Miscellaneous other commands */
110 { "getprinterdriverdir", (PyCFunction)spoolss_getprinterdriverdir,
111 METH_VARARGS | METH_KEYWORDS,
112 "Return printer driver directory.
114 Return the printer driver directory for a given architecture. The
115 architecture defaults to \"Windows NT x86\"."},
117 /* Other stuff - this should really go into a samba config module
118 but for the moment let's leave it here. */
120 { "setup_logging", (PyCFunction)py_setup_logging,
121 METH_VARARGS | METH_KEYWORDS,
122 "Set up debug logging.
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
130 >>> spoolss.setup_logging(interactive = 1)" },
132 { "get_debuglevel", (PyCFunction)get_debuglevel,
134 "Set the current debug level.
138 >>> spoolss.get_debuglevel()
141 { "set_debuglevel", (PyCFunction)set_debuglevel,
143 "Get the current debug level.
147 >>> spoolss.set_debuglevel(10)" },
149 /* Printer driver routines */
151 { "addprinterdriver", (PyCFunction)spoolss_addprinterdriver,
152 METH_VARARGS | METH_KEYWORDS,
153 "Add a printer driver." },
155 { "addprinterdriverex", (PyCFunction)spoolss_addprinterdriverex,
156 METH_VARARGS | METH_KEYWORDS,
157 "Add a printer driver." },
159 { "deleteprinterdriver", (PyCFunction)spoolss_deleteprinterdriver,
160 METH_VARARGS | METH_KEYWORDS,
161 "Delete a printer driver." },
163 { "deleteprinterdriverex", (PyCFunction)spoolss_deleteprinterdriverex,
164 METH_VARARGS | METH_KEYWORDS,
165 "Delete a printer driver." },
170 /* Methods attached to a spoolss handle object */
172 static PyMethodDef spoolss_hnd_methods[] = {
176 { "getprinter", (PyCFunction)spoolss_hnd_getprinter,
177 METH_VARARGS | METH_KEYWORDS,
178 "Get printer information.
180 Return a dictionary of print information. The info level defaults to 1.
185 {'comment': 'i am a comment', 'printer_name': '\\\\NPSD-PDC2\\meanie',
186 'description': '\\\\NPSD-PDC2\\meanie,Generic / Text Only,i am a location',
189 { "setprinter", (PyCFunction)spoolss_hnd_setprinter,
190 METH_VARARGS | METH_KEYWORDS,
191 "Set printer information."},
193 /* Printer drivers */
195 { "getprinterdriver", (PyCFunction)spoolss_hnd_getprinterdriver,
196 METH_VARARGS | METH_KEYWORDS,
197 "Return printer driver information.
199 Return a dictionary of printer driver information for the printer driver
200 bound to this printer."},
204 { "enumforms", (PyCFunction)spoolss_hnd_enumforms,
205 METH_VARARGS | METH_KEYWORDS,
206 "Enumerate supported forms.
208 Return a list of forms supported by this printer or print server."},
210 { "setform", (PyCFunction)spoolss_hnd_setform,
211 METH_VARARGS | METH_KEYWORDS,
214 Set the form given by the dictionary argument."},
216 { "addform", (PyCFunction)spoolss_hnd_addform,
217 METH_VARARGS | METH_KEYWORDS,
220 { "getform", (PyCFunction)spoolss_hnd_getform,
221 METH_VARARGS | METH_KEYWORDS,
222 "Get form properties." },
224 { "deleteform", (PyCFunction)spoolss_hnd_deleteform,
225 METH_VARARGS | METH_KEYWORDS,
228 /* Job related methods */
230 { "enumjobs", (PyCFunction)spoolss_hnd_enumjobs,
231 METH_VARARGS | METH_KEYWORDS,
234 { "setjob", (PyCFunction)spoolss_hnd_setjob,
235 METH_VARARGS | METH_KEYWORDS,
236 "Set job information." },
238 { "getjob", (PyCFunction)spoolss_hnd_getjob,
239 METH_VARARGS | METH_KEYWORDS,
240 "Get job information." },
242 { "startpageprinter", (PyCFunction)spoolss_hnd_startpageprinter,
243 METH_VARARGS | METH_KEYWORDS,
244 "Notify spooler that a page is about to be printed." },
246 { "endpageprinter", (PyCFunction)spoolss_hnd_endpageprinter,
247 METH_VARARGS | METH_KEYWORDS,
248 "Notify spooler that a page is about to be printed." },
250 { "startdocprinter", (PyCFunction)spoolss_hnd_startdocprinter,
251 METH_VARARGS | METH_KEYWORDS,
252 "Notify spooler that a document is about to be printed." },
254 { "enddocprinter", (PyCFunction)spoolss_hnd_enddocprinter,
255 METH_VARARGS | METH_KEYWORDS,
256 "Notify spooler that a document is about to be printed." },
258 { "writeprinter", (PyCFunction)spoolss_hnd_writeprinter,
259 METH_VARARGS | METH_KEYWORDS,
260 "Write job data to a printer." },
262 { "addjob", (PyCFunction)spoolss_hnd_addjob,
263 METH_VARARGS | METH_KEYWORDS,
264 "Add a job to the list of print jobs." },
268 { "getprinterdata", (PyCFunction)spoolss_hnd_getprinterdata,
269 METH_VARARGS | METH_KEYWORDS,
270 "Get printer data." },
272 { "setprinterdata", (PyCFunction)spoolss_hnd_setprinterdata,
273 METH_VARARGS | METH_KEYWORDS,
274 "Set printer data." },
276 { "enumprinterdata", (PyCFunction)spoolss_hnd_enumprinterdata,
277 METH_VARARGS | METH_KEYWORDS,
278 "Enumerate printer data." },
280 { "deleteprinterdata", (PyCFunction)spoolss_hnd_deleteprinterdata,
281 METH_VARARGS | METH_KEYWORDS,
282 "Delete printer data." },
284 { "getprinterdataex", (PyCFunction)spoolss_hnd_getprinterdataex,
285 METH_VARARGS | METH_KEYWORDS,
286 "Get printer data." },
288 { "setprinterdataex", (PyCFunction)spoolss_hnd_setprinterdataex,
289 METH_VARARGS | METH_KEYWORDS,
290 "Set printer data." },
292 { "enumprinterdataex", (PyCFunction)spoolss_hnd_enumprinterdataex,
293 METH_VARARGS | METH_KEYWORDS,
294 "Enumerate printer data." },
296 { "deleteprinterdataex", (PyCFunction)spoolss_hnd_deleteprinterdataex,
297 METH_VARARGS | METH_KEYWORDS,
298 "Delete printer data." },
300 { "enumprinterkey", (PyCFunction)spoolss_hnd_enumprinterkey,
301 METH_VARARGS | METH_KEYWORDS,
302 "Enumerate printer key." },
305 /* Not implemented */
307 { "deleteprinterkey", (PyCFunction)spoolss_hnd_deleteprinterkey,
308 METH_VARARGS | METH_KEYWORDS,
309 "Delete printer key." },
316 static void py_policy_hnd_dealloc(PyObject* self)
318 spoolss_policy_hnd_object *hnd;
320 /* Close down policy handle and free talloc context */
322 hnd = (spoolss_policy_hnd_object*)self;
324 cli_shutdown(hnd->cli);
325 talloc_destroy(hnd->mem_ctx);
330 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
332 return Py_FindMethod(spoolss_hnd_methods, self, attrname);
335 static char spoolss_type_doc[] =
336 "Python wrapper for Windows NT SPOOLSS rpc pipe.";
338 PyTypeObject spoolss_policy_hnd_type = {
339 PyObject_HEAD_INIT(NULL)
342 sizeof(spoolss_policy_hnd_object),
344 py_policy_hnd_dealloc, /* tp_dealloc*/
346 py_policy_hnd_getattr, /* tp_getattr*/
351 0, /* tp_as_sequence*/
352 0, /* tp_as_mapping*/
359 Py_TPFLAGS_DEFAULT, /* tp_flags */
360 spoolss_type_doc, /* tp_doc */
363 /* Initialise constants */
365 static struct const_vals {
368 } module_const_vals[] = {
370 /* Access permissions */
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 },
396 /* Printer enumeration flags */
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 },
409 { "FORM_USER", FORM_USER },
410 { "FORM_BUILTIN", FORM_BUILTIN },
411 { "FORM_PRINTER", FORM_PRINTER },
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 },
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 },
446 /* Job control constants */
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 },
457 static void const_init(PyObject *dict)
459 struct const_vals *tmp;
462 for (tmp = module_const_vals; tmp->name; tmp++) {
463 obj = PyInt_FromLong(tmp->value);
464 PyDict_SetItemString(dict, tmp->name, obj);
469 /* Module initialisation */
471 void initspoolss(void)
473 PyObject *module, *dict;
475 /* Initialise module */
477 module = Py_InitModule("spoolss", spoolss_methods);
478 dict = PyModule_GetDict(module);
480 /* Exceptions we can raise */
482 spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
483 PyDict_SetItemString(dict, "error", spoolss_error);
485 spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
486 PyDict_SetItemString(dict, "werror", spoolss_werror);
488 /* Initialise policy handle object */
490 spoolss_policy_hnd_type.ob_type = &PyType_Type;
492 PyDict_SetItemString(dict, "spoolss.hnd",
493 (PyObject *)&spoolss_policy_hnd_type);
495 /* Initialise constants */
499 /* Do samba initialisation */