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"
25 PyObject *spoolss_openprinter(PyObject *self, PyObject *args, PyObject *kw)
27 char *full_name, *computer_name = NULL;
31 PyObject *result = NULL, *creds = NULL;
32 static char *kwlist[] = { "printername", "creds", "access", NULL };
33 uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
34 struct cli_state *cli;
36 if (!PyArg_ParseTupleAndKeywords(
37 args, kw, "s|O!i", kwlist, &full_name, &PyDict_Type, &creds,
43 /* FIXME: Return name format exception for names without a UNC
46 computer_name = strdup(full_name + 2);
48 if (strchr(computer_name, '\\')) {
49 char *c = strchr(computer_name, '\\');
53 if (!(cli = open_pipe_creds(computer_name, creds,
54 cli_spoolss_initialise, NULL))) {
56 /* Error state set in open_pipe_creds() */
61 if (!(mem_ctx = talloc_init())) {
62 PyErr_SetString(spoolss_error,
63 "unable to initialise talloc context\n");
67 werror = cli_spoolss_open_printer_ex(
68 cli, mem_ctx, full_name, "", desired_access, computer_name,
71 if (!W_ERROR_IS_OK(werror)) {
74 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
78 result = new_policy_hnd_object(cli, mem_ctx, &hnd);
81 SAFE_FREE(computer_name);
88 PyObject *spoolss_closeprinter(PyObject *self, PyObject *args)
91 spoolss_policy_hnd_object *hnd;
94 /* Parse parameters */
96 if (!PyArg_ParseTuple(args, "O!", &spoolss_policy_hnd_type, &po))
99 hnd = (spoolss_policy_hnd_object *)po;
101 /* Call rpc function */
103 result = cli_spoolss_close_printer(hnd->cli, hnd->mem_ctx, &hnd->pol);
111 /* Fetch printer information */
113 PyObject *spoolss_getprinter(PyObject *self, PyObject *args, PyObject *kw)
115 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
117 PyObject *result = NULL;
118 PRINTER_INFO_CTR ctr;
121 static char *kwlist[] = {"level", NULL};
123 /* Parse parameters */
125 if (!PyArg_ParseTupleAndKeywords(args, kw, "|i", kwlist, &level))
128 /* Call rpc function */
130 werror = cli_spoolss_getprinter(
131 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level, &ctr);
133 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
134 werror = cli_spoolss_getprinter(
135 hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
140 if (!W_ERROR_IS_OK(werror)) {
141 PyErr_SetObject(spoolss_werror,
142 PyInt_FromLong(W_ERROR_V(werror)));
151 py_from_PRINTER_INFO_0(&result, ctr.printers_0);
155 py_from_PRINTER_INFO_1(&result, ctr.printers_1);
159 py_from_PRINTER_INFO_2(&result, ctr.printers_2);
163 py_from_PRINTER_INFO_3(&result, ctr.printers_3);
167 PyDict_SetItemString(result, "level", PyInt_FromLong(level));
173 /* Set printer information */
175 PyObject *spoolss_setprinter(PyObject *self, PyObject *args, PyObject *kw)
177 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
179 PyObject *info, *level_obj;
180 PRINTER_INFO_CTR ctr;
182 static char *kwlist[] = {"dict", NULL};
184 PRINTER_INFO_0 printers_0;
185 PRINTER_INFO_1 printers_1;
186 PRINTER_INFO_2 printers_2;
187 PRINTER_INFO_3 printers_3;
188 PRINTER_INFO_4 printers_4;
189 PRINTER_INFO_5 printers_5;
192 /* Parse parameters */
194 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist,
195 &PyDict_Type, &info))
198 /* Check dictionary contains a level */
200 if ((level_obj = PyDict_GetItemString(info, "level"))) {
202 if (!PyInt_Check(level_obj))
205 level = PyInt_AsLong(level_obj);
209 PyErr_SetString(spoolss_error, "invalid info");
213 /* Fill in printer info */
219 PyObject *devmode_obj;
221 ctr.printers_2 = &pinfo.printers_2;
223 if (!py_to_PRINTER_INFO_2(&pinfo.printers_2, info))
227 devmode_obj = PyDict_GetItemString(info, "device_mode");
229 pinfo.printers_2.devmode = talloc(
230 hnd->mem_ctx, sizeof(DEVICEMODE));
232 PyDEVICEMODE_AsDEVICEMODE(pinfo.printers_2.devmode,
237 /* FIXME: can we actually set the security descriptor using
238 a setprinter level 2? */
240 pinfo.printers_2.secdesc = NULL;
241 pinfo.printers_2.secdesc = NULL;
247 PyErr_SetString(spoolss_error, "unsupported info level");
251 /* Call rpc function */
253 werror = cli_spoolss_setprinter(hnd->cli, hnd->mem_ctx, &hnd->pol,
258 if (!W_ERROR_IS_OK(werror)) {
259 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
267 /* Enumerate printers */
269 PyObject *spoolss_enumprinters(PyObject *self, PyObject *args, PyObject *kw)
272 PyObject *result, *creds = NULL;
273 PRINTER_INFO_CTR ctr;
274 int level = 1, flags = PRINTER_ENUM_LOCAL, i;
275 uint32 needed, num_printers;
276 static char *kwlist[] = {"server", "name", "level", "flags",
278 TALLOC_CTX *mem_ctx = NULL;
279 struct cli_state *cli = NULL;
280 char *server, *name = NULL;
282 /* Parse parameters */
284 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|siiO!", kwlist,
285 &server, &name, &level, &flags,
286 &PyDict_Type, &creds))
289 if (server[0] == '\\' && server[1] == '\\')
292 mem_ctx = talloc_init();
293 cli = open_pipe_creds(server, creds, cli_spoolss_initialise, NULL);
295 /* Call rpc function */
297 werror = cli_spoolss_enum_printers(
298 cli, mem_ctx, 0, &needed, flags, level,
299 &num_printers, &ctr);
301 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
302 werror = cli_spoolss_enum_printers(
303 cli, mem_ctx, needed, NULL, flags, level,
304 &num_printers, &ctr);
308 if (!W_ERROR_IS_OK(werror)) {
309 PyErr_SetObject(spoolss_werror,
310 PyInt_FromLong(W_ERROR_V(werror)));
314 result = PyList_New(num_printers);
318 for (i = 0; i < num_printers; i++) {
321 py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
323 PyList_SetItem(result, i, value);
328 for(i = 0; i < num_printers; i++) {
331 py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
333 PyList_SetItem(result, i, value);
338 for(i = 0; i < num_printers; i++) {
341 py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
343 PyList_SetItem(result, i, value);
348 for(i = 0; i < num_printers; i++) {
351 py_from_PRINTER_INFO_3(&value, &ctr.printers_3[i]);
353 PyList_SetItem(result, i, value);