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.
24 #include "python/py_common.h"
25 #include "python/py_conv.h"
26 #include "python/py_spoolss.h"
28 /* Exceptions this module can raise */
30 PyObject *spoolss_error, *spoolss_werror;
33 * Routines to convert from python hashes to Samba structures
36 /* Return a cli_state struct opened on the SPOOLSS pipe. If credentials
37 are passed use them. */
39 typedef struct cli_state *(cli_pipe_fn)(
40 struct cli_state *cli, char *system_name,
41 struct ntuser_creds *creds);
43 struct cli_state *open_pipe_creds(char *system_name, PyObject *creds,
44 cli_pipe_fn *connect_fn,
45 struct cli_state *cli)
47 struct ntuser_creds nt_creds;
50 cli = (struct cli_state *)malloc(sizeof(struct cli_state));
57 /* Extract credentials from the python dictionary and initialise
58 the ntuser_creds struct from them. */
60 ZERO_STRUCT(nt_creds);
61 nt_creds.pwd.null_pwd = True;
64 char *username, *password, *domain;
65 PyObject *username_obj, *password_obj, *domain_obj;
67 /* Check credentials passed are valid. This means the
68 username, domain and password keys must exist and be
71 username_obj = PyDict_GetItemString(creds, "username");
72 domain_obj = PyDict_GetItemString(creds, "domain");
73 password_obj = PyDict_GetItemString(creds, "password");
75 if (!username_obj || !domain_obj || !password_obj) {
77 PyErr_SetString(spoolss_error, "invalid credentials");
81 if (!PyString_Check(username_obj) ||
82 !PyString_Check(domain_obj) ||
83 !PyString_Check(password_obj))
86 username = PyString_AsString(username_obj);
87 domain = PyString_AsString(domain_obj);
88 password = PyString_AsString(password_obj);
90 if (!username || !domain || !password)
93 /* Initialise nt_creds structure with passed creds */
95 fstrcpy(nt_creds.user_name, username);
96 fstrcpy(nt_creds.domain, domain);
98 if (lp_encrypted_passwords())
99 pwd_make_lm_nt_16(&nt_creds.pwd, password);
101 pwd_set_cleartext(&nt_creds.pwd, password);
103 nt_creds.pwd.null_pwd = False;
106 /* Now try to connect */
108 connect_fn(cli, system_name, &nt_creds);
113 static PyObject *new_policy_hnd_object(struct cli_state *cli,
114 TALLOC_CTX *mem_ctx, POLICY_HND *pol)
116 spoolss_policy_hnd_object *o;
118 o = PyObject_New(spoolss_policy_hnd_object, &spoolss_policy_hnd_type);
121 o->mem_ctx = mem_ctx;
122 memcpy(&o->pol, pol, sizeof(POLICY_HND));
128 * Method dispatch table
131 #include "py_spoolss_printers.c"
132 #include "py_spoolss_drivers.c"
133 #include "py_spoolss_ports.c"
134 #include "py_spoolss_forms.c"
136 static PyMethodDef spoolss_methods[] = {
138 /* Open/close printer handles */
140 { "openprinter", spoolss_openprinter, METH_VARARGS | METH_KEYWORDS,
143 { "closeprinter", spoolss_closeprinter, METH_VARARGS,
146 /* Server enumeratation functions */
148 { "enumprinters", spoolss_enumprinters, METH_VARARGS | METH_KEYWORDS,
149 "Enumerate printers" },
151 { "enumports", spoolss_enumports, METH_VARARGS | METH_KEYWORDS,
154 { "enumprinterdrivers", spoolss_enumprinterdrivers, METH_VARARGS |
155 METH_KEYWORDS, "Enumerate printer drivers" },
157 /* Miscellaneous other commands */
159 { "getprinterdriverdir", spoolss_getprinterdriverdir, METH_VARARGS |
160 METH_KEYWORDS, "Get printer driver directory" },
165 /* Methods attached to a spoolss handle object */
167 static PyMethodDef spoolss_hnd_methods[] = {
171 { "getprinter", spoolss_getprinter, METH_VARARGS | METH_KEYWORDS,
172 "Fetch printer information" },
174 { "setprinter", spoolss_setprinter, METH_VARARGS | METH_KEYWORDS,
175 "Set printer information" },
177 /* Printer drivers */
179 { "getprinterdriver", spoolss_getprinterdriver,
180 METH_VARARGS | METH_KEYWORDS, "Fetch printer driver" },
184 { "enumforms", spoolss_enumforms, METH_VARARGS | METH_KEYWORDS,
187 { "setform", spoolss_setform, METH_VARARGS | METH_KEYWORDS,
188 "Modify properties of a form" },
190 { "addform", spoolss_addform, METH_VARARGS | METH_KEYWORDS,
193 { "getform", spoolss_getform, METH_VARARGS | METH_KEYWORDS,
194 "Fetch form properties" },
196 { "deleteform", spoolss_deleteform, METH_VARARGS | METH_KEYWORDS,
203 static void py_policy_hnd_dealloc(PyObject* self)
208 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
210 return Py_FindMethod(spoolss_hnd_methods, self, attrname);
213 PyTypeObject spoolss_policy_hnd_type = {
214 PyObject_HEAD_INIT(NULL)
216 "Spoolss Policy Handle",
217 sizeof(spoolss_policy_hnd_object),
219 py_policy_hnd_dealloc, /*tp_dealloc*/
221 py_policy_hnd_getattr, /*tp_getattr*/
226 0, /*tp_as_sequence*/
231 /* Initialise constants */
233 struct spoolss_const {
236 } spoolss_const_vals[] = {
238 /* Access permissions */
240 { "MAXIMUM_ALLOWED_ACCESS", MAXIMUM_ALLOWED_ACCESS },
241 { "SERVER_ALL_ACCESS", SERVER_ALL_ACCESS },
242 { "PRINTER_ALL_ACCESS", PRINTER_ALL_ACCESS },
244 /* Printer enumeration flags */
246 { "PRINTER_ENUM_DEFAULT", PRINTER_ENUM_DEFAULT },
247 { "PRINTER_ENUM_LOCAL", PRINTER_ENUM_LOCAL },
248 { "PRINTER_ENUM_CONNECTIONS", PRINTER_ENUM_CONNECTIONS },
249 { "PRINTER_ENUM_FAVORITE", PRINTER_ENUM_FAVORITE },
250 { "PRINTER_ENUM_NAME", PRINTER_ENUM_NAME },
251 { "PRINTER_ENUM_REMOTE", PRINTER_ENUM_REMOTE },
252 { "PRINTER_ENUM_SHARED", PRINTER_ENUM_SHARED },
253 { "PRINTER_ENUM_NETWORK", PRINTER_ENUM_NETWORK },
258 static void const_init(PyObject *dict)
260 struct spoolss_const *tmp;
263 for (tmp = spoolss_const_vals; tmp->name; tmp++) {
264 obj = PyInt_FromLong(tmp->value);
265 PyDict_SetItemString(dict, tmp->name, obj);
270 /* Module initialisation */
272 void initspoolss(void)
274 PyObject *module, *dict;
276 /* Initialise module */
278 module = Py_InitModule("spoolss", spoolss_methods);
279 dict = PyModule_GetDict(module);
281 /* Make spools_error global an exception we can raise when an error
284 spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
285 PyDict_SetItemString(dict, "error", spoolss_error);
287 spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
288 PyDict_SetItemString(dict, "werror", spoolss_werror);
290 /* Initialise policy handle object */
292 spoolss_policy_hnd_type.ob_type = &PyType_Type;
294 /* Initialise constants */
298 /* Do samba initialisation */