* the printing code should now be back to the working state it was
[Samba.git] / source / python / py_spoolss.c
blob95be77de55b8d3ee23222b072cf074e7c0eabc64
1 /*
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);
38 o->cli = cli;
39 o->mem_ctx = mem_ctx;
40 memcpy(&o->pol, pol, sizeof(POLICY_HND));
42 return (PyObject*)o;
45 /*
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.
60 Example:
62 >>> hnd = spoolss.openprinter(\"\\\\\\\\NPSD-PDC2\\\\meanie\")"},
64 { "closeprinter", spoolss_closeprinter, METH_VARARGS,
65 "Close a printer handle opened with openprinter or addprinter.
67 Example:
69 >>> spoolss.closeprinter(hnd)"},
71 { "addprinterex", (PyCFunction)spoolss_addprinterex, METH_VARARGS,
72 "addprinterex()"},
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.
83 Example:
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.
97 Example:
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
126 or logged to a file.
128 Example:
130 >>> spoolss.setup_logging(interactive = 1)" },
132 { "get_debuglevel", (PyCFunction)get_debuglevel,
133 METH_VARARGS,
134 "Set the current debug level.
136 Example:
138 >>> spoolss.get_debuglevel()
139 0" },
141 { "set_debuglevel", (PyCFunction)set_debuglevel,
142 METH_VARARGS,
143 "Get the current debug level.
145 Example:
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." },
167 { NULL }
170 /* Methods attached to a spoolss handle object */
172 static PyMethodDef spoolss_hnd_methods[] = {
174 /* Printer info */
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.
182 Example:
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}"},
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."},
202 /* Forms */
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,
212 "Set form data.
214 Set the form given by the dictionary argument."},
216 { "addform", (PyCFunction)spoolss_hnd_addform,
217 METH_VARARGS | METH_KEYWORDS,
218 "Add a new form." },
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,
226 "Delete a form." },
228 /* Job related methods */
230 { "enumjobs", (PyCFunction)spoolss_hnd_enumjobs,
231 METH_VARARGS | METH_KEYWORDS,
232 "Enumerate jobs." },
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." },
266 /* Printer data */
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 { "deleteprinterdataex", (PyCFunction)spoolss_hnd_deleteprinterdataex,
285 METH_VARARGS | METH_KEYWORDS,
286 "Delete printer data." },
288 { NULL }
292 static void py_policy_hnd_dealloc(PyObject* self)
294 spoolss_policy_hnd_object *hnd;
296 /* Close down policy handle and free talloc context */
298 hnd = (spoolss_policy_hnd_object*)self;
300 cli_shutdown(hnd->cli);
301 talloc_destroy(hnd->mem_ctx);
303 PyObject_Del(self);
306 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
308 return Py_FindMethod(spoolss_hnd_methods, self, attrname);
311 static char spoolss_type_doc[] =
312 "Python wrapper for Windows NT SPOOLSS rpc pipe.";
314 PyTypeObject spoolss_policy_hnd_type = {
315 PyObject_HEAD_INIT(NULL)
317 "spoolss.hnd",
318 sizeof(spoolss_policy_hnd_object),
320 py_policy_hnd_dealloc, /* tp_dealloc*/
321 0, /* tp_print*/
322 py_policy_hnd_getattr, /* tp_getattr*/
323 0, /* tp_setattr*/
324 0, /* tp_compare*/
325 0, /* tp_repr*/
326 0, /* tp_as_number*/
327 0, /* tp_as_sequence*/
328 0, /* tp_as_mapping*/
329 0, /* tp_hash */
330 0, /* tp_call */
331 0, /* tp_str */
332 0, /* tp_getattro */
333 0, /* tp_setattro */
334 0, /* tp_as_buffer*/
335 Py_TPFLAGS_DEFAULT, /* tp_flags */
336 spoolss_type_doc, /* tp_doc */
339 /* Initialise constants */
341 static struct const_vals {
342 char *name;
343 uint32 value;
344 } module_const_vals[] = {
346 /* Access permissions */
348 { "MAXIMUM_ALLOWED_ACCESS", MAXIMUM_ALLOWED_ACCESS },
349 { "SERVER_ALL_ACCESS", SERVER_ALL_ACCESS },
350 { "SERVER_READ", SERVER_READ },
351 { "SERVER_WRITE", SERVER_WRITE },
352 { "SERVER_EXECUTE", SERVER_EXECUTE },
353 { "SERVER_ACCESS_ADMINISTER", SERVER_ACCESS_ADMINISTER },
354 { "SERVER_ACCESS_ENUMERATE", SERVER_ACCESS_ENUMERATE },
355 { "PRINTER_ALL_ACCESS", PRINTER_ALL_ACCESS },
356 { "PRINTER_READ", PRINTER_READ },
357 { "PRINTER_WRITE", PRINTER_WRITE },
358 { "PRINTER_EXECUTE", PRINTER_EXECUTE },
359 { "PRINTER_ACCESS_ADMINISTER", PRINTER_ACCESS_ADMINISTER },
360 { "PRINTER_ACCESS_USE", PRINTER_ACCESS_USE },
361 { "JOB_ACCESS_ADMINISTER", JOB_ACCESS_ADMINISTER },
362 { "JOB_ALL_ACCESS", JOB_ALL_ACCESS },
363 { "JOB_READ", JOB_READ },
364 { "JOB_WRITE", JOB_WRITE },
365 { "JOB_EXECUTE", JOB_EXECUTE },
366 { "STANDARD_RIGHTS_ALL_ACCESS", STANDARD_RIGHTS_ALL_ACCESS },
367 { "STANDARD_RIGHTS_EXECUTE_ACCESS", STANDARD_RIGHTS_EXECUTE_ACCESS },
368 { "STANDARD_RIGHTS_READ_ACCESS", STANDARD_RIGHTS_READ_ACCESS },
369 { "STANDARD_RIGHTS_REQUIRED_ACCESS", STANDARD_RIGHTS_REQUIRED_ACCESS },
370 { "STANDARD_RIGHTS_WRITE_ACCESS", STANDARD_RIGHTS_WRITE_ACCESS },
372 /* Printer enumeration flags */
374 { "PRINTER_ENUM_DEFAULT", PRINTER_ENUM_DEFAULT },
375 { "PRINTER_ENUM_LOCAL", PRINTER_ENUM_LOCAL },
376 { "PRINTER_ENUM_CONNECTIONS", PRINTER_ENUM_CONNECTIONS },
377 { "PRINTER_ENUM_FAVORITE", PRINTER_ENUM_FAVORITE },
378 { "PRINTER_ENUM_NAME", PRINTER_ENUM_NAME },
379 { "PRINTER_ENUM_REMOTE", PRINTER_ENUM_REMOTE },
380 { "PRINTER_ENUM_SHARED", PRINTER_ENUM_SHARED },
381 { "PRINTER_ENUM_NETWORK", PRINTER_ENUM_NETWORK },
383 /* Form types */
385 { "FORM_USER", FORM_USER },
386 { "FORM_BUILTIN", FORM_BUILTIN },
387 { "FORM_PRINTER", FORM_PRINTER },
389 /* WERRORs */
391 { "WERR_OK", 0 },
392 { "WERR_BADFILE", 2 },
393 { "WERR_ACCESS_DENIED", 5 },
394 { "WERR_BADFID", 6 },
395 { "WERR_BADFUNC", 1 },
396 { "WERR_INSUFFICIENT_BUFFER", 122 },
397 { "WERR_NO_SUCH_SHARE", 67 },
398 { "WERR_ALREADY_EXISTS", 80 },
399 { "WERR_INVALID_PARAM", 87 },
400 { "WERR_NOT_SUPPORTED", 50 },
401 { "WERR_BAD_PASSWORD", 86 },
402 { "WERR_NOMEM", 8 },
403 { "WERR_INVALID_NAME", 123 },
404 { "WERR_UNKNOWN_LEVEL", 124 },
405 { "WERR_OBJECT_PATH_INVALID", 161 },
406 { "WERR_NO_MORE_ITEMS", 259 },
407 { "WERR_MORE_DATA", 234 },
408 { "WERR_UNKNOWN_PRINTER_DRIVER", 1797 },
409 { "WERR_INVALID_PRINTER_NAME", 1801 },
410 { "WERR_PRINTER_ALREADY_EXISTS", 1802 },
411 { "WERR_INVALID_DATATYPE", 1804 },
412 { "WERR_INVALID_ENVIRONMENT", 1805 },
413 { "WERR_INVALID_FORM_NAME", 1902 },
414 { "WERR_INVALID_FORM_SIZE", 1903 },
415 { "WERR_BUF_TOO_SMALL", 2123 },
416 { "WERR_JOB_NOT_FOUND", 2151 },
417 { "WERR_DEST_NOT_FOUND", 2152 },
418 { "WERR_NOT_LOCAL_DOMAIN", 2320 },
419 { "WERR_PRINTER_DRIVER_IN_USE", 3001 },
420 { "WERR_STATUS_MORE_ENTRIES ", 0x0105 },
422 /* Job control constants */
424 { "JOB_CONTROL_PAUSE", JOB_CONTROL_PAUSE },
425 { "JOB_CONTROL_RESUME", JOB_CONTROL_RESUME },
426 { "JOB_CONTROL_CANCEL", JOB_CONTROL_CANCEL },
427 { "JOB_CONTROL_RESTART", JOB_CONTROL_RESTART },
428 { "JOB_CONTROL_DELETE", JOB_CONTROL_DELETE },
430 { NULL },
433 static void const_init(PyObject *dict)
435 struct const_vals *tmp;
436 PyObject *obj;
438 for (tmp = module_const_vals; tmp->name; tmp++) {
439 obj = PyInt_FromLong(tmp->value);
440 PyDict_SetItemString(dict, tmp->name, obj);
441 Py_DECREF(obj);
445 /* Module initialisation */
447 void initspoolss(void)
449 PyObject *module, *dict;
451 /* Initialise module */
453 module = Py_InitModule("spoolss", spoolss_methods);
454 dict = PyModule_GetDict(module);
456 /* Exceptions we can raise */
458 spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
459 PyDict_SetItemString(dict, "error", spoolss_error);
461 spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
462 PyDict_SetItemString(dict, "werror", spoolss_werror);
464 /* Initialise policy handle object */
466 spoolss_policy_hnd_type.ob_type = &PyType_Type;
468 PyDict_SetItemString(dict, "spoolss.hnd",
469 (PyObject *)&spoolss_policy_hnd_type);
471 /* Initialise constants */
473 const_init(dict);
475 /* Do samba initialisation */
477 py_samba_init();