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 struct cli_state *open_pipe_creds(char *system_name, PyObject *creds,
32 cli_pipe_fn *connect_fn,
33 struct cli_state *cli)
35 struct ntuser_creds nt_creds;
38 cli = (struct cli_state *)malloc(sizeof(struct cli_state));
45 /* Extract credentials from the python dictionary and initialise
46 the ntuser_creds struct from them. */
48 ZERO_STRUCT(nt_creds);
49 nt_creds.pwd.null_pwd = True;
51 if (creds && PyDict_Size(creds) > 0) {
52 char *username, *password, *domain;
53 PyObject *username_obj, *password_obj, *domain_obj;
55 /* Check credentials passed are valid. This means the
56 username, domain and password keys must exist and be
59 username_obj = PyDict_GetItemString(creds, "username");
60 domain_obj = PyDict_GetItemString(creds, "domain");
61 password_obj = PyDict_GetItemString(creds, "password");
63 if (!username_obj || !domain_obj || !password_obj) {
65 PyErr_SetString(spoolss_error, "invalid credentials");
69 if (!PyString_Check(username_obj) ||
70 !PyString_Check(domain_obj) ||
71 !PyString_Check(password_obj))
74 username = PyString_AsString(username_obj);
75 domain = PyString_AsString(domain_obj);
76 password = PyString_AsString(password_obj);
78 if (!username || !domain || !password)
81 /* Initialise nt_creds structure with passed creds */
83 fstrcpy(nt_creds.user_name, username);
84 fstrcpy(nt_creds.domain, domain);
86 if (lp_encrypted_passwords())
87 pwd_make_lm_nt_16(&nt_creds.pwd, password);
89 pwd_set_cleartext(&nt_creds.pwd, password);
91 nt_creds.pwd.null_pwd = False;
94 /* Now try to connect */
96 connect_fn(cli, system_name, &nt_creds);
101 PyObject *new_policy_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
104 spoolss_policy_hnd_object *o;
106 o = PyObject_New(spoolss_policy_hnd_object, &spoolss_policy_hnd_type);
109 o->mem_ctx = mem_ctx;
110 memcpy(&o->pol, pol, sizeof(POLICY_HND));
116 * Method dispatch table
119 static PyMethodDef spoolss_methods[] = {
121 /* Open/close printer handles */
123 { "openprinter", spoolss_openprinter, METH_VARARGS | METH_KEYWORDS,
124 "openprinter(printername, [creds, access]) -> <spoolss hnd object>
126 Open a printer given by printername in UNC format. Optionally a
127 dictionary of (username, password) may be given in which case they are
128 used when opening the RPC pipe. An access mask may also be given which
129 defaults to MAXIMUM_ALLOWED_ACCESS.
133 >>> hnd = spoolss.openprinter(\"\\\\\\\\NPSD-PDC2\\\\meanie\")
136 { "closeprinter", spoolss_closeprinter, METH_VARARGS,
139 Close a printer handle opened with openprinter or addprinter.
143 >>> spoolss.closeprinter(hnd)
146 /* Server enumeratation functions */
148 { "enumprinters", spoolss_enumprinters, METH_VARARGS | METH_KEYWORDS,
149 "enumprinters(server, [creds, level, flags]) -> list
151 Return a list of printers on a print server. The credentials, info level
152 and flags may be specified as keyword arguments.
156 >>> print spoolss.enumprinters(\"\\\\\\\\npsd-pdc2\")
157 [{'comment': 'i am a comment', 'printer_name': 'meanie', 'flags': 8388608,
158 'description': 'meanie,Generic / Text Only,i am a location'},
159 {'comment': '', 'printer_name': 'fileprint', 'flags': 8388608,
160 'description': 'fileprint,Generic / Text Only,'}]
163 { "enumports", spoolss_enumports, METH_VARARGS | METH_KEYWORDS,
164 "enumports(server, [creds, level]) -> list
166 Return a list of ports on a print server.
170 >>> print spoolss.enumports(\"\\\\\\\\npsd-pdc2\")
171 [{'name': 'LPT1:'}, {'name': 'LPT2:'}, {'name': 'COM1:'}, {'name': 'COM2:'},
172 {'name': 'FILE:'}, {'name': '\\\\nautilus1\\zpekt3r'}]
175 { "enumprinterdrivers", spoolss_enumprinterdrivers, METH_VARARGS |
177 "enumprinterdrivers(server, [level, arch, creds]) -> list
179 Return a list of printer drivers.
181 /* Miscellaneous other commands */
183 { "getprinterdriverdir", spoolss_getprinterdriverdir, METH_VARARGS |
184 METH_KEYWORDS, "getprinterdriverdir(server, [creds]) -> string
186 Return the printer driver directory for a given architecture. The
187 architecture defaults to \"Windows NT x86\".
190 /* Other stuff - this should really go into a samba config module
191 but for the moment let's leave it here. */
193 { "setup_logging", py_setup_logging, METH_VARARGS | METH_KEYWORDS,
196 { "get_debuglevel", get_debuglevel, METH_VARARGS, "" },
197 { "set_debuglevel", set_debuglevel, METH_VARARGS, "" },
202 /* Methods attached to a spoolss handle object */
204 static PyMethodDef spoolss_hnd_methods[] = {
208 { "getprinter", spoolss_getprinter, METH_VARARGS | METH_KEYWORDS,
209 "getprinter([level]) -> dict
211 Return a dictionary of print information. The info level defaults to 1.
216 {'comment': 'i am a comment', 'printer_name': '\\\\NPSD-PDC2\\meanie',
217 'description': '\\\\NPSD-PDC2\\meanie,Generic / Text Only,i am a location',
221 { "setprinter", spoolss_setprinter, METH_VARARGS | METH_KEYWORDS,
222 "setprinter(dict) -> None
224 Set printer information.
227 /* Printer drivers */
229 { "getprinterdriver", spoolss_getprinterdriver,
230 METH_VARARGS | METH_KEYWORDS,
231 "getprinterdriver([level = 1, arch = \"Windows NT x86\"] -> dict
233 Return a dictionary of printer driver information.
238 { "enumforms", spoolss_enumforms, METH_VARARGS | METH_KEYWORDS,
239 "enumforms([level = 1]) -> list
241 Return a list of forms supported by a printer.
244 { "setform", spoolss_setform, METH_VARARGS | METH_KEYWORDS,
245 "setform(dict) -> None
247 Set the form given by the dictionary argument.
250 { "addform", spoolss_addform, METH_VARARGS | METH_KEYWORDS,
253 { "getform", spoolss_getform, METH_VARARGS | METH_KEYWORDS,
254 "Fetch form properties" },
256 { "deleteform", spoolss_deleteform, METH_VARARGS | METH_KEYWORDS,
263 static void py_policy_hnd_dealloc(PyObject* self)
268 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
270 return Py_FindMethod(spoolss_hnd_methods, self, attrname);
273 static char spoolss_type_doc[] =
274 "Python wrapper for Windows NT SPOOLSS rpc pipe.";
276 PyTypeObject spoolss_policy_hnd_type = {
277 PyObject_HEAD_INIT(NULL)
280 sizeof(spoolss_policy_hnd_object),
282 py_policy_hnd_dealloc, /* tp_dealloc*/
284 py_policy_hnd_getattr, /* tp_getattr*/
289 0, /* tp_as_sequence*/
290 0, /* tp_as_mapping*/
297 Py_TPFLAGS_DEFAULT, /* tp_flags */
298 spoolss_type_doc, /* tp_doc */
301 /* Initialise constants */
303 struct spoolss_const {
306 } spoolss_const_vals[] = {
308 /* Access permissions */
310 { "MAXIMUM_ALLOWED_ACCESS", MAXIMUM_ALLOWED_ACCESS },
311 { "SERVER_ALL_ACCESS", SERVER_ALL_ACCESS },
312 { "SERVER_READ", SERVER_READ },
313 { "SERVER_WRITE", SERVER_WRITE },
314 { "SERVER_EXECUTE", SERVER_EXECUTE },
315 { "SERVER_ACCESS_ADMINISTER", SERVER_ACCESS_ADMINISTER },
316 { "SERVER_ACCESS_ENUMERATE", SERVER_ACCESS_ENUMERATE },
317 { "PRINTER_ALL_ACCESS", PRINTER_ALL_ACCESS },
318 { "PRINTER_READ", PRINTER_READ },
319 { "PRINTER_WRITE", PRINTER_WRITE },
320 { "PRINTER_EXECUTE", PRINTER_EXECUTE },
321 { "PRINTER_ACCESS_ADMINISTER", PRINTER_ACCESS_ADMINISTER },
322 { "PRINTER_ACCESS_USE", PRINTER_ACCESS_USE },
323 { "JOB_ACCESS_ADMINISTER", JOB_ACCESS_ADMINISTER },
324 { "JOB_ALL_ACCESS", JOB_ALL_ACCESS },
325 { "JOB_READ", JOB_READ },
326 { "JOB_WRITE", JOB_WRITE },
327 { "JOB_EXECUTE", JOB_EXECUTE },
328 { "STANDARD_RIGHTS_ALL_ACCESS", STANDARD_RIGHTS_ALL_ACCESS },
329 { "STANDARD_RIGHTS_EXECUTE_ACCESS", STANDARD_RIGHTS_EXECUTE_ACCESS },
330 { "STANDARD_RIGHTS_READ_ACCESS", STANDARD_RIGHTS_READ_ACCESS },
331 { "STANDARD_RIGHTS_REQUIRED_ACCESS", STANDARD_RIGHTS_REQUIRED_ACCESS },
332 { "STANDARD_RIGHTS_WRITE_ACCESS", STANDARD_RIGHTS_WRITE_ACCESS },
334 /* Printer enumeration flags */
336 { "PRINTER_ENUM_DEFAULT", PRINTER_ENUM_DEFAULT },
337 { "PRINTER_ENUM_LOCAL", PRINTER_ENUM_LOCAL },
338 { "PRINTER_ENUM_CONNECTIONS", PRINTER_ENUM_CONNECTIONS },
339 { "PRINTER_ENUM_FAVORITE", PRINTER_ENUM_FAVORITE },
340 { "PRINTER_ENUM_NAME", PRINTER_ENUM_NAME },
341 { "PRINTER_ENUM_REMOTE", PRINTER_ENUM_REMOTE },
342 { "PRINTER_ENUM_SHARED", PRINTER_ENUM_SHARED },
343 { "PRINTER_ENUM_NETWORK", PRINTER_ENUM_NETWORK },
347 { "FORM_USER", FORM_USER },
348 { "FORM_BUILTIN", FORM_BUILTIN },
349 { "FORM_PRINTER", FORM_PRINTER },
354 { "WERR_BADFILE", 2 },
355 { "WERR_ACCESS_DENIED", 5 },
356 { "WERR_BADFID", 6 },
357 { "WERR_BADFUNC", 1 },
358 { "WERR_INSUFFICIENT_BUFFER", 122 },
359 { "WERR_NO_SUCH_SHARE", 67 },
360 { "WERR_ALREADY_EXISTS", 80 },
361 { "WERR_INVALID_PARAM", 87 },
362 { "WERR_NOT_SUPPORTED", 50 },
363 { "WERR_BAD_PASSWORD", 86 },
365 { "WERR_INVALID_NAME", 123 },
366 { "WERR_UNKNOWN_LEVEL", 124 },
367 { "WERR_OBJECT_PATH_INVALID", 161 },
368 { "WERR_NO_MORE_ITEMS", 259 },
369 { "WERR_MORE_DATA", 234 },
370 { "WERR_UNKNOWN_PRINTER_DRIVER", 1797 },
371 { "WERR_INVALID_PRINTER_NAME", 1801 },
372 { "WERR_PRINTER_ALREADY_EXISTS", 1802 },
373 { "WERR_INVALID_DATATYPE", 1804 },
374 { "WERR_INVALID_ENVIRONMENT", 1805 },
375 { "WERR_INVALID_FORM_NAME", 1902 },
376 { "WERR_INVALID_FORM_SIZE", 1903 },
377 { "WERR_BUF_TOO_SMALL", 2123 },
378 { "WERR_JOB_NOT_FOUND", 2151 },
379 { "WERR_DEST_NOT_FOUND", 2152 },
380 { "WERR_NOT_LOCAL_DOMAIN", 2320 },
381 { "WERR_PRINTER_DRIVER_IN_USE", 3001 },
382 { "WERR_STATUS_MORE_ENTRIES ", 0x0105 },
387 static void const_init(PyObject *dict)
389 struct spoolss_const *tmp;
392 for (tmp = spoolss_const_vals; tmp->name; tmp++) {
393 obj = PyInt_FromLong(tmp->value);
394 PyDict_SetItemString(dict, tmp->name, obj);
399 /* Module initialisation */
401 void initspoolss(void)
403 PyObject *module, *dict;
405 /* Initialise module */
407 module = Py_InitModule("spoolss", spoolss_methods);
408 dict = PyModule_GetDict(module);
410 /* Make spools_error global an exception we can raise when an error
413 spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
414 PyDict_SetItemString(dict, "error", spoolss_error);
416 spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
417 PyDict_SetItemString(dict, "werror", spoolss_werror);
419 /* Initialise policy handle object */
421 spoolss_policy_hnd_type.ob_type = &PyType_Type;
423 PyDict_SetItemString(dict, "spoolss.hnd",
424 (PyObject *)&spoolss_policy_hnd_type);
426 /* Initialise constants */
430 /* Do samba initialisation */