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." },
304 static void py_policy_hnd_dealloc(PyObject* self)
306 spoolss_policy_hnd_object *hnd;
308 /* Close down policy handle and free talloc context */
310 hnd = (spoolss_policy_hnd_object*)self;
312 cli_shutdown(hnd->cli);
313 talloc_destroy(hnd->mem_ctx);
318 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
320 return Py_FindMethod(spoolss_hnd_methods, self, attrname);
323 static char spoolss_type_doc[] =
324 "Python wrapper for Windows NT SPOOLSS rpc pipe.";
326 PyTypeObject spoolss_policy_hnd_type = {
327 PyObject_HEAD_INIT(NULL)
330 sizeof(spoolss_policy_hnd_object),
332 py_policy_hnd_dealloc, /* tp_dealloc*/
334 py_policy_hnd_getattr, /* tp_getattr*/
339 0, /* tp_as_sequence*/
340 0, /* tp_as_mapping*/
347 Py_TPFLAGS_DEFAULT, /* tp_flags */
348 spoolss_type_doc, /* tp_doc */
351 /* Initialise constants */
353 static struct const_vals {
356 } module_const_vals[] = {
358 /* Access permissions */
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 },
384 /* Printer enumeration flags */
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 },
397 { "FORM_USER", FORM_USER },
398 { "FORM_BUILTIN", FORM_BUILTIN },
399 { "FORM_PRINTER", FORM_PRINTER },
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 },
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 },
434 /* Job control constants */
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 },
445 static void const_init(PyObject *dict)
447 struct const_vals *tmp;
450 for (tmp = module_const_vals; tmp->name; tmp++) {
451 obj = PyInt_FromLong(tmp->value);
452 PyDict_SetItemString(dict, tmp->name, obj);
457 /* Module initialisation */
459 void initspoolss(void)
461 PyObject *module, *dict;
463 /* Initialise module */
465 module = Py_InitModule("spoolss", spoolss_methods);
466 dict = PyModule_GetDict(module);
468 /* Exceptions we can raise */
470 spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
471 PyDict_SetItemString(dict, "error", spoolss_error);
473 spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
474 PyDict_SetItemString(dict, "werror", spoolss_werror);
476 /* Initialise policy handle object */
478 spoolss_policy_hnd_type.ob_type = &PyType_Type;
480 PyDict_SetItemString(dict, "spoolss.hnd",
481 (PyObject *)&spoolss_policy_hnd_type);
483 /* Initialise constants */
487 /* Do samba initialisation */