r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[Samba/gebeck_regimport.git] / source3 / python / py_spoolss.c
blob2543324b318262d4815dc31286f872f88f9b7e2f
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include "python/py_spoolss.h"
22 /* Exceptions this module can raise */
24 PyObject *spoolss_error, *spoolss_werror;
26 /*
27 * Method dispatch table
30 static PyMethodDef spoolss_methods[] = {
32 /* Open/close printer handles */
34 { "openprinter", (PyCFunction)spoolss_openprinter, METH_VARARGS | METH_KEYWORDS,
35 "Open a printer by name in UNC format.\n"
36 "\n"
37 "Optionally a dictionary of (domain, username, password) may be given in\n"
38 "which case they are used when opening the RPC pipe. An access mask may\n"
39 "also be given which defaults to MAXIMUM_ALLOWED_ACCESS.\n"
40 "\n"
41 "Example:\n"
42 "\n"
43 ">>> hnd = spoolss.openprinter(\"\\\\\\\\NPSD-PDC2\\\\meanie\")"},
45 { "closeprinter", spoolss_closeprinter, METH_VARARGS,
46 "Close a printer handle opened with openprinter or addprinter.\n"
47 "\n"
48 "Example:\n"
49 "\n"
50 ">>> spoolss.closeprinter(hnd)"},
52 { "addprinterex", (PyCFunction)spoolss_addprinterex, METH_VARARGS,
53 "addprinterex()"},
55 /* Server enumeratation functions */
57 { "enumprinters", (PyCFunction)spoolss_enumprinters,
58 METH_VARARGS | METH_KEYWORDS,
59 "Enumerate printers on a print server.\n"
60 "\n"
61 "Return a list of printers on a print server. The credentials, info level\n"
62 "and flags may be specified as keyword arguments.\n"
63 "\n"
64 "Example:\n"
65 "\n"
66 ">>> print spoolss.enumprinters(\"\\\\\\\\npsd-pdc2\")\n"
67 "[{'comment': 'i am a comment', 'printer_name': 'meanie', 'flags': 8388608, \n"
68 " 'description': 'meanie,Generic / Text Only,i am a location'}, \n"
69 " {'comment': '', 'printer_name': 'fileprint', 'flags': 8388608, \n"
70 " 'description': 'fileprint,Generic / Text Only,'}]"},
72 { "enumports", (PyCFunction)spoolss_enumports,
73 METH_VARARGS | METH_KEYWORDS,
74 "Enumerate ports on a print server.\n"
75 "\n"
76 "Return a list of ports on a print server.\n"
77 "\n"
78 "Example:\n"
79 "\n"
80 ">>> print spoolss.enumports(\"\\\\\\\\npsd-pdc2\")\n"
81 "[{'name': 'LPT1:'}, {'name': 'LPT2:'}, {'name': 'COM1:'}, \n"
82 "{'name': 'COM2:'}, {'name': 'FILE:'}, {'name': '\\\\nautilus1\\zpekt3r'}]"},
84 { "enumprinterdrivers", (PyCFunction)spoolss_enumprinterdrivers,
85 METH_VARARGS | METH_KEYWORDS,
86 "Enumerate printer drivers on a print server.\n"
87 "\n"
88 "Return a list of printer drivers."},
90 /* Miscellaneous other commands */
92 { "getprinterdriverdir", (PyCFunction)spoolss_getprinterdriverdir,
93 METH_VARARGS | METH_KEYWORDS,
94 "Return printer driver directory.\n"
95 "\n"
96 "Return the printer driver directory for a given architecture. The\n"
97 "architecture defaults to \"Windows NT x86\"."},
99 /* Other stuff - this should really go into a samba config module
100 but for the moment let's leave it here. */
102 { "setup_logging", (PyCFunction)py_setup_logging,
103 METH_VARARGS | METH_KEYWORDS,
104 "Set up debug logging.\n"
105 "\n"
106 "Initialises Samba's debug logging system. One argument is expected which\n"
107 "is a boolean specifying whether debugging is interactive and sent to stdout\n"
108 "or logged to a file.\n"
109 "\n"
110 "Example:\n"
111 "\n"
112 ">>> spoolss.setup_logging(interactive = 1)" },
114 { "get_debuglevel", (PyCFunction)get_debuglevel,
115 METH_VARARGS,
116 "Set the current debug level.\n"
117 "\n"
118 "Example:\n"
119 "\n"
120 ">>> spoolss.get_debuglevel()\n"
121 "0" },
123 { "set_debuglevel", (PyCFunction)set_debuglevel,
124 METH_VARARGS,
125 "Get the current debug level.\n"
126 "\n"
127 "Example:\n"
128 "\n"
129 ">>> spoolss.set_debuglevel(10)" },
131 /* Printer driver routines */
133 { "addprinterdriver", (PyCFunction)spoolss_addprinterdriver,
134 METH_VARARGS | METH_KEYWORDS,
135 "Add a printer driver." },
137 { "addprinterdriverex", (PyCFunction)spoolss_addprinterdriverex,
138 METH_VARARGS | METH_KEYWORDS,
139 "Add a printer driver." },
141 { "deleteprinterdriver", (PyCFunction)spoolss_deleteprinterdriver,
142 METH_VARARGS | METH_KEYWORDS,
143 "Delete a printer driver." },
145 { "deleteprinterdriverex", (PyCFunction)spoolss_deleteprinterdriverex,
146 METH_VARARGS | METH_KEYWORDS,
147 "Delete a printer driver." },
149 { NULL }
152 /* Methods attached to a spoolss handle object */
154 static PyMethodDef spoolss_hnd_methods[] = {
156 /* Printer info */
158 { "getprinter", (PyCFunction)spoolss_hnd_getprinter,
159 METH_VARARGS | METH_KEYWORDS,
160 "Get printer information.\n"
161 "\n"
162 "Return a dictionary of print information. The info level defaults to 1.\n"
163 "\n"
164 "Example:\n"
165 "\n"
166 ">>> hnd.getprinter()\n"
167 "{'comment': 'i am a comment', 'printer_name': '\\\\NPSD-PDC2\\meanie',\n"
168 " 'description': '\\\\NPSD-PDC2\\meanie,Generic / Text Only,i am a location',\n"
169 " 'flags': 8388608}"},
171 { "setprinter", (PyCFunction)spoolss_hnd_setprinter,
172 METH_VARARGS | METH_KEYWORDS,
173 "Set printer information."},
175 /* Printer drivers */
177 { "getprinterdriver", (PyCFunction)spoolss_hnd_getprinterdriver,
178 METH_VARARGS | METH_KEYWORDS,
179 "Return printer driver information.\n"
180 "\n"
181 "Return a dictionary of printer driver information for the printer driver\n"
182 "bound to this printer."},
184 /* Forms */
186 { "enumforms", (PyCFunction)spoolss_hnd_enumforms,
187 METH_VARARGS | METH_KEYWORDS,
188 "Enumerate supported forms.\n"
189 "\n"
190 "Return a list of forms supported by this printer or print server."},
192 { "setform", (PyCFunction)spoolss_hnd_setform,
193 METH_VARARGS | METH_KEYWORDS,
194 "Set form data.\n"
195 "\n"
196 "Set the form given by the dictionary argument."},
198 { "addform", (PyCFunction)spoolss_hnd_addform,
199 METH_VARARGS | METH_KEYWORDS,
200 "Add a new form." },
202 { "getform", (PyCFunction)spoolss_hnd_getform,
203 METH_VARARGS | METH_KEYWORDS,
204 "Get form properties." },
206 { "deleteform", (PyCFunction)spoolss_hnd_deleteform,
207 METH_VARARGS | METH_KEYWORDS,
208 "Delete a form." },
210 /* Job related methods */
212 { "enumjobs", (PyCFunction)spoolss_hnd_enumjobs,
213 METH_VARARGS | METH_KEYWORDS,
214 "Enumerate jobs." },
216 { "setjob", (PyCFunction)spoolss_hnd_setjob,
217 METH_VARARGS | METH_KEYWORDS,
218 "Set job information." },
220 { "getjob", (PyCFunction)spoolss_hnd_getjob,
221 METH_VARARGS | METH_KEYWORDS,
222 "Get job information." },
224 { "startpageprinter", (PyCFunction)spoolss_hnd_startpageprinter,
225 METH_VARARGS | METH_KEYWORDS,
226 "Notify spooler that a page is about to be printed." },
228 { "endpageprinter", (PyCFunction)spoolss_hnd_endpageprinter,
229 METH_VARARGS | METH_KEYWORDS,
230 "Notify spooler that a page is about to be printed." },
232 { "startdocprinter", (PyCFunction)spoolss_hnd_startdocprinter,
233 METH_VARARGS | METH_KEYWORDS,
234 "Notify spooler that a document is about to be printed." },
236 { "enddocprinter", (PyCFunction)spoolss_hnd_enddocprinter,
237 METH_VARARGS | METH_KEYWORDS,
238 "Notify spooler that a document is about to be printed." },
240 { "writeprinter", (PyCFunction)spoolss_hnd_writeprinter,
241 METH_VARARGS | METH_KEYWORDS,
242 "Write job data to a printer." },
244 { "addjob", (PyCFunction)spoolss_hnd_addjob,
245 METH_VARARGS | METH_KEYWORDS,
246 "Add a job to the list of print jobs." },
248 /* Printer data */
250 { "getprinterdata", (PyCFunction)spoolss_hnd_getprinterdata,
251 METH_VARARGS | METH_KEYWORDS,
252 "Get printer data." },
254 { "setprinterdata", (PyCFunction)spoolss_hnd_setprinterdata,
255 METH_VARARGS | METH_KEYWORDS,
256 "Set printer data." },
258 { "enumprinterdata", (PyCFunction)spoolss_hnd_enumprinterdata,
259 METH_VARARGS | METH_KEYWORDS,
260 "Enumerate printer data." },
262 { "deleteprinterdata", (PyCFunction)spoolss_hnd_deleteprinterdata,
263 METH_VARARGS | METH_KEYWORDS,
264 "Delete printer data." },
266 { "getprinterdataex", (PyCFunction)spoolss_hnd_getprinterdataex,
267 METH_VARARGS | METH_KEYWORDS,
268 "Get printer data." },
270 { "setprinterdataex", (PyCFunction)spoolss_hnd_setprinterdataex,
271 METH_VARARGS | METH_KEYWORDS,
272 "Set printer data." },
274 { "enumprinterdataex", (PyCFunction)spoolss_hnd_enumprinterdataex,
275 METH_VARARGS | METH_KEYWORDS,
276 "Enumerate printer data." },
278 { "deleteprinterdataex", (PyCFunction)spoolss_hnd_deleteprinterdataex,
279 METH_VARARGS | METH_KEYWORDS,
280 "Delete printer data." },
282 { "enumprinterkey", (PyCFunction)spoolss_hnd_enumprinterkey,
283 METH_VARARGS | METH_KEYWORDS,
284 "Enumerate printer key." },
286 #if 0
287 /* Not implemented */
289 { "deleteprinterkey", (PyCFunction)spoolss_hnd_deleteprinterkey,
290 METH_VARARGS | METH_KEYWORDS,
291 "Delete printer key." },
292 #endif
294 { NULL }
298 static void py_policy_hnd_dealloc(PyObject* self)
300 spoolss_policy_hnd_object *hnd;
302 /* Close down policy handle and free talloc context */
304 hnd = (spoolss_policy_hnd_object*)self;
306 cli_shutdown(hnd->cli);
307 talloc_destroy(hnd->mem_ctx);
309 PyObject_Del(self);
312 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
314 return Py_FindMethod(spoolss_hnd_methods, self, attrname);
317 static char spoolss_type_doc[] =
318 "Python wrapper for Windows NT SPOOLSS rpc pipe.";
320 PyTypeObject spoolss_policy_hnd_type = {
321 PyObject_HEAD_INIT(NULL)
323 "spoolss.hnd",
324 sizeof(spoolss_policy_hnd_object),
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 */
345 /* Initialise constants */
347 static struct const_vals {
348 char *name;
349 uint32 value;
350 } module_const_vals[] = {
352 /* Access permissions */
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 },
378 /* Printer enumeration flags */
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 },
389 /* Form types */
391 { "FORM_USER", FORM_USER },
392 { "FORM_BUILTIN", FORM_BUILTIN },
393 { "FORM_PRINTER", FORM_PRINTER },
395 /* WERRORs */
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 },
428 /* Job control constants */
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 },
436 { NULL },
439 static void const_init(PyObject *dict)
441 struct const_vals *tmp;
442 PyObject *obj;
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);
451 /* Module initialisation */
453 void initspoolss(void)
455 PyObject *module, *dict;
457 /* Initialise module */
459 module = Py_InitModule("spoolss", spoolss_methods);
460 dict = PyModule_GetDict(module);
462 /* Exceptions we can raise */
464 spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
465 PyDict_SetItemString(dict, "error", spoolss_error);
467 spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
468 PyDict_SetItemString(dict, "werror", spoolss_werror);
470 /* Initialise policy handle object */
472 spoolss_policy_hnd_type.ob_type = &PyType_Type;
474 PyDict_SetItemString(dict, "spoolss.hnd",
475 (PyObject *)&spoolss_policy_hnd_type);
477 /* Initialise constants */
479 const_init(dict);
481 /* Do samba initialisation */
483 py_samba_init();