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", spoolss_openprinter, METH_VARARGS | METH_KEYWORDS,
54 "openprinter(printername, [creds, access]) -> <spoolss hnd object>
56 Open a printer given by printername in UNC format. Optionally a dictionary
57 of (domain, username, password) may be given in which case they are used
58 when opening the RPC pipe. An access mask may also be given which defaults
59 to MAXIMUM_ALLOWED_ACCESS.
63 >>> hnd = spoolss.openprinter(\"\\\\\\\\NPSD-PDC2\\\\meanie\")
66 { "closeprinter", spoolss_closeprinter, METH_VARARGS,
69 Close a printer handle opened with openprinter or addprinter.
73 >>> spoolss.closeprinter(hnd)
76 /* Server enumeratation functions */
78 { "enumprinters", spoolss_enumprinters, METH_VARARGS | METH_KEYWORDS,
79 "enumprinters(server, [creds, level, flags]) -> list
81 Return a list of printers on a print server. The credentials, info level
82 and flags may be specified as keyword arguments.
86 >>> print spoolss.enumprinters(\"\\\\\\\\npsd-pdc2\")
87 [{'comment': 'i am a comment', 'printer_name': 'meanie', 'flags': 8388608,
88 'description': 'meanie,Generic / Text Only,i am a location'},
89 {'comment': '', 'printer_name': 'fileprint', 'flags': 8388608,
90 'description': 'fileprint,Generic / Text Only,'}]
93 { "enumports", spoolss_enumports, METH_VARARGS | METH_KEYWORDS,
94 "enumports(server, [creds, level]) -> list
96 Return a list of ports on a print server.
100 >>> print spoolss.enumports(\"\\\\\\\\npsd-pdc2\")
101 [{'name': 'LPT1:'}, {'name': 'LPT2:'}, {'name': 'COM1:'}, {'name': 'COM2:'},
102 {'name': 'FILE:'}, {'name': '\\\\nautilus1\\zpekt3r'}]
105 { "enumprinterdrivers", spoolss_enumprinterdrivers, METH_VARARGS |
107 "enumprinterdrivers(server, [creds, level, arch]) -> list
109 Return a list of printer drivers.
111 /* Miscellaneous other commands */
113 { "getprinterdriverdir", spoolss_getprinterdriverdir, METH_VARARGS |
114 METH_KEYWORDS, "getprinterdriverdir(server, [creds]) -> string
116 Return the printer driver directory for a given architecture. The
117 architecture defaults to \"Windows NT x86\".
120 /* Other stuff - this should really go into a samba config module
121 but for the moment let's leave it here. */
123 { "setup_logging", py_setup_logging, METH_VARARGS | METH_KEYWORDS,
126 { "get_debuglevel", get_debuglevel, METH_VARARGS, "" },
127 { "set_debuglevel", set_debuglevel, METH_VARARGS, "" },
132 /* Methods attached to a spoolss handle object */
134 static PyMethodDef spoolss_hnd_methods[] = {
138 { "getprinter", spoolss_getprinter, METH_VARARGS | METH_KEYWORDS,
139 "getprinter([level]) -> dict
141 Return a dictionary of print information. The info level defaults to 1.
146 {'comment': 'i am a comment', 'printer_name': '\\\\NPSD-PDC2\\meanie',
147 'description': '\\\\NPSD-PDC2\\meanie,Generic / Text Only,i am a location',
151 { "setprinter", spoolss_setprinter, METH_VARARGS | METH_KEYWORDS,
152 "setprinter(dict) -> None
154 Set printer information.
157 /* Printer drivers */
159 { "getprinterdriver", spoolss_getprinterdriver,
160 METH_VARARGS | METH_KEYWORDS,
161 "getprinterdriver([level = 1, arch = \"Windows NT x86\"] -> dict
163 Return a dictionary of printer driver information.
168 { "enumforms", spoolss_enumforms, METH_VARARGS | METH_KEYWORDS,
169 "enumforms([level = 1]) -> list
171 Return a list of forms supported by a printer.
174 { "setform", spoolss_setform, METH_VARARGS | METH_KEYWORDS,
175 "setform(dict) -> None
177 Set the form given by the dictionary argument.
180 { "addform", spoolss_addform, METH_VARARGS | METH_KEYWORDS,
183 { "getform", spoolss_getform, METH_VARARGS | METH_KEYWORDS,
184 "Fetch form properties" },
186 { "deleteform", spoolss_deleteform, METH_VARARGS | METH_KEYWORDS,
189 /* Job related methods */
191 { "enumjobs", spoolss_enumjobs, METH_VARARGS | METH_KEYWORDS,
194 { "setjob", spoolss_setjob, METH_VARARGS | METH_KEYWORDS,
201 static void py_policy_hnd_dealloc(PyObject* self)
203 spoolss_policy_hnd_object *hnd;
205 /* Close down policy handle and free talloc context */
207 hnd = (spoolss_policy_hnd_object*)self;
209 cli_shutdown(hnd->cli);
210 talloc_destroy(hnd->mem_ctx);
215 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
217 return Py_FindMethod(spoolss_hnd_methods, self, attrname);
220 static char spoolss_type_doc[] =
221 "Python wrapper for Windows NT SPOOLSS rpc pipe.";
223 PyTypeObject spoolss_policy_hnd_type = {
224 PyObject_HEAD_INIT(NULL)
227 sizeof(spoolss_policy_hnd_object),
229 py_policy_hnd_dealloc, /* tp_dealloc*/
231 py_policy_hnd_getattr, /* tp_getattr*/
236 0, /* tp_as_sequence*/
237 0, /* tp_as_mapping*/
244 Py_TPFLAGS_DEFAULT, /* tp_flags */
245 spoolss_type_doc, /* tp_doc */
248 /* Initialise constants */
250 struct spoolss_const {
253 } spoolss_const_vals[] = {
255 /* Access permissions */
257 { "MAXIMUM_ALLOWED_ACCESS", MAXIMUM_ALLOWED_ACCESS },
258 { "SERVER_ALL_ACCESS", SERVER_ALL_ACCESS },
259 { "SERVER_READ", SERVER_READ },
260 { "SERVER_WRITE", SERVER_WRITE },
261 { "SERVER_EXECUTE", SERVER_EXECUTE },
262 { "SERVER_ACCESS_ADMINISTER", SERVER_ACCESS_ADMINISTER },
263 { "SERVER_ACCESS_ENUMERATE", SERVER_ACCESS_ENUMERATE },
264 { "PRINTER_ALL_ACCESS", PRINTER_ALL_ACCESS },
265 { "PRINTER_READ", PRINTER_READ },
266 { "PRINTER_WRITE", PRINTER_WRITE },
267 { "PRINTER_EXECUTE", PRINTER_EXECUTE },
268 { "PRINTER_ACCESS_ADMINISTER", PRINTER_ACCESS_ADMINISTER },
269 { "PRINTER_ACCESS_USE", PRINTER_ACCESS_USE },
270 { "JOB_ACCESS_ADMINISTER", JOB_ACCESS_ADMINISTER },
271 { "JOB_ALL_ACCESS", JOB_ALL_ACCESS },
272 { "JOB_READ", JOB_READ },
273 { "JOB_WRITE", JOB_WRITE },
274 { "JOB_EXECUTE", JOB_EXECUTE },
275 { "STANDARD_RIGHTS_ALL_ACCESS", STANDARD_RIGHTS_ALL_ACCESS },
276 { "STANDARD_RIGHTS_EXECUTE_ACCESS", STANDARD_RIGHTS_EXECUTE_ACCESS },
277 { "STANDARD_RIGHTS_READ_ACCESS", STANDARD_RIGHTS_READ_ACCESS },
278 { "STANDARD_RIGHTS_REQUIRED_ACCESS", STANDARD_RIGHTS_REQUIRED_ACCESS },
279 { "STANDARD_RIGHTS_WRITE_ACCESS", STANDARD_RIGHTS_WRITE_ACCESS },
281 /* Printer enumeration flags */
283 { "PRINTER_ENUM_DEFAULT", PRINTER_ENUM_DEFAULT },
284 { "PRINTER_ENUM_LOCAL", PRINTER_ENUM_LOCAL },
285 { "PRINTER_ENUM_CONNECTIONS", PRINTER_ENUM_CONNECTIONS },
286 { "PRINTER_ENUM_FAVORITE", PRINTER_ENUM_FAVORITE },
287 { "PRINTER_ENUM_NAME", PRINTER_ENUM_NAME },
288 { "PRINTER_ENUM_REMOTE", PRINTER_ENUM_REMOTE },
289 { "PRINTER_ENUM_SHARED", PRINTER_ENUM_SHARED },
290 { "PRINTER_ENUM_NETWORK", PRINTER_ENUM_NETWORK },
294 { "FORM_USER", FORM_USER },
295 { "FORM_BUILTIN", FORM_BUILTIN },
296 { "FORM_PRINTER", FORM_PRINTER },
301 { "WERR_BADFILE", 2 },
302 { "WERR_ACCESS_DENIED", 5 },
303 { "WERR_BADFID", 6 },
304 { "WERR_BADFUNC", 1 },
305 { "WERR_INSUFFICIENT_BUFFER", 122 },
306 { "WERR_NO_SUCH_SHARE", 67 },
307 { "WERR_ALREADY_EXISTS", 80 },
308 { "WERR_INVALID_PARAM", 87 },
309 { "WERR_NOT_SUPPORTED", 50 },
310 { "WERR_BAD_PASSWORD", 86 },
312 { "WERR_INVALID_NAME", 123 },
313 { "WERR_UNKNOWN_LEVEL", 124 },
314 { "WERR_OBJECT_PATH_INVALID", 161 },
315 { "WERR_NO_MORE_ITEMS", 259 },
316 { "WERR_MORE_DATA", 234 },
317 { "WERR_UNKNOWN_PRINTER_DRIVER", 1797 },
318 { "WERR_INVALID_PRINTER_NAME", 1801 },
319 { "WERR_PRINTER_ALREADY_EXISTS", 1802 },
320 { "WERR_INVALID_DATATYPE", 1804 },
321 { "WERR_INVALID_ENVIRONMENT", 1805 },
322 { "WERR_INVALID_FORM_NAME", 1902 },
323 { "WERR_INVALID_FORM_SIZE", 1903 },
324 { "WERR_BUF_TOO_SMALL", 2123 },
325 { "WERR_JOB_NOT_FOUND", 2151 },
326 { "WERR_DEST_NOT_FOUND", 2152 },
327 { "WERR_NOT_LOCAL_DOMAIN", 2320 },
328 { "WERR_PRINTER_DRIVER_IN_USE", 3001 },
329 { "WERR_STATUS_MORE_ENTRIES ", 0x0105 },
331 /* Job control constants */
333 { "JOB_CONTROL_PAUSE", JOB_CONTROL_PAUSE },
334 { "JOB_CONTROL_RESUME", JOB_CONTROL_RESUME },
335 { "JOB_CONTROL_CANCEL", JOB_CONTROL_CANCEL },
336 { "JOB_CONTROL_RESTART", JOB_CONTROL_RESTART },
337 { "JOB_CONTROL_DELETE", JOB_CONTROL_DELETE },
342 static void const_init(PyObject *dict)
344 struct spoolss_const *tmp;
347 for (tmp = spoolss_const_vals; tmp->name; tmp++) {
348 obj = PyInt_FromLong(tmp->value);
349 PyDict_SetItemString(dict, tmp->name, obj);
354 /* Module initialisation */
356 void initspoolss(void)
358 PyObject *module, *dict;
360 /* Initialise module */
362 module = Py_InitModule("spoolss", spoolss_methods);
363 dict = PyModule_GetDict(module);
365 /* Exceptions we can raise */
367 spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
368 PyDict_SetItemString(dict, "error", spoolss_error);
370 spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
371 PyDict_SetItemString(dict, "werror", spoolss_werror);
373 /* Initialise policy handle object */
375 spoolss_policy_hnd_type.ob_type = &PyType_Type;
377 PyDict_SetItemString(dict, "spoolss.hnd",
378 (PyObject *)&spoolss_policy_hnd_type);
380 /* Initialise constants */
384 /* Do samba initialisation */