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))) {
55 fprintf(stderr, "could not initialise cli state\n");
59 if (!(mem_ctx = talloc_init())) {
60 fprintf(stderr, "unable to initialise talloc context\n");
64 werror = cli_spoolss_open_printer_ex(
65 cli, mem_ctx, full_name, "", desired_access, computer_name,
68 if (!W_ERROR_IS_OK(werror)) {
71 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
75 result = new_policy_hnd_object(cli, mem_ctx, &hnd);
78 SAFE_FREE(computer_name);
85 PyObject *spoolss_closeprinter(PyObject *self, PyObject *args)
88 spoolss_policy_hnd_object *hnd;
91 /* Parse parameters */
93 if (!PyArg_ParseTuple(args, "O!", &spoolss_policy_hnd_type, &po))
96 hnd = (spoolss_policy_hnd_object *)po;
98 /* Call rpc function */
100 result = cli_spoolss_close_printer(hnd->cli, hnd->mem_ctx, &hnd->pol);
102 /* Cleanup samba stuf */
104 cli_shutdown(hnd->cli);
105 talloc_destroy(hnd->mem_ctx);
113 /* Fetch printer information */
115 PyObject *spoolss_getprinter(PyObject *self, PyObject *args, PyObject *kw)
117 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
119 PyObject *result = NULL;
120 PRINTER_INFO_CTR ctr;
123 static char *kwlist[] = {"level", NULL};
125 /* Parse parameters */
127 if (!PyArg_ParseTupleAndKeywords(args, kw, "|i", kwlist, &level))
130 /* Call rpc function */
132 werror = cli_spoolss_getprinter(
133 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level, &ctr);
135 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
136 werror = cli_spoolss_getprinter(
137 hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
142 if (!W_ERROR_IS_OK(werror)) {
143 PyErr_SetObject(spoolss_werror,
144 PyInt_FromLong(W_ERROR_V(werror)));
153 py_from_PRINTER_INFO_0(&result, ctr.printers_0);
157 py_from_PRINTER_INFO_1(&result, ctr.printers_1);
161 py_from_PRINTER_INFO_2(&result, ctr.printers_2);
165 py_from_PRINTER_INFO_3(&result, ctr.printers_3);
169 PyDict_SetItemString(result, "level", PyInt_FromLong(level));
175 /* Set printer information */
177 PyObject *spoolss_setprinter(PyObject *self, PyObject *args, PyObject *kw)
179 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
181 PyObject *info, *level_obj;
182 PRINTER_INFO_CTR ctr;
184 static char *kwlist[] = {"dict", NULL};
186 PRINTER_INFO_0 printers_0;
187 PRINTER_INFO_1 printers_1;
188 PRINTER_INFO_2 printers_2;
189 PRINTER_INFO_3 printers_3;
190 PRINTER_INFO_4 printers_4;
191 PRINTER_INFO_5 printers_5;
194 /* Parse parameters */
196 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist,
197 &PyDict_Type, &info))
200 /* Check dictionary contains a level */
202 if ((level_obj = PyDict_GetItemString(info, "level"))) {
204 if (!PyInt_Check(level_obj))
207 level = PyInt_AsLong(level_obj);
211 PyErr_SetString(spoolss_error, "invalid info");
215 /* Fill in printer info */
221 PyObject *devmode_obj;
223 ctr.printers_2 = &pinfo.printers_2;
225 if (!py_to_PRINTER_INFO_2(&pinfo.printers_2, info))
229 devmode_obj = PyDict_GetItemString(info, "device_mode");
231 pinfo.printers_2.devmode = talloc(
232 hnd->mem_ctx, sizeof(DEVICEMODE));
234 PyDEVICEMODE_AsDEVICEMODE(pinfo.printers_2.devmode,
239 /* FIXME: can we actually set the security descriptor using
240 a setprinter level 2? */
242 pinfo.printers_2.secdesc = NULL;
243 pinfo.printers_2.secdesc = NULL;
249 PyErr_SetString(spoolss_error, "unsupported info level");
253 /* Call rpc function */
255 werror = cli_spoolss_setprinter(hnd->cli, hnd->mem_ctx, &hnd->pol,
260 if (!W_ERROR_IS_OK(werror)) {
261 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
269 /* Enumerate printers */
271 PyObject *spoolss_enumprinters(PyObject *self, PyObject *args, PyObject *kw)
274 PyObject *result, *creds = NULL;
275 PRINTER_INFO_CTR ctr;
276 int level = 1, flags = PRINTER_ENUM_LOCAL, i;
277 uint32 needed, num_printers;
278 static char *kwlist[] = {"server", "name", "level", "flags",
280 TALLOC_CTX *mem_ctx = NULL;
281 struct cli_state *cli = NULL;
282 char *server, *name = NULL;
284 /* Parse parameters */
286 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|siiO!", kwlist,
287 &server, &name, &level, &flags,
288 &PyDict_Type, &creds))
291 if (server[0] == '\\' && server[1] == '\\')
294 mem_ctx = talloc_init();
295 cli = open_pipe_creds(server, creds, cli_spoolss_initialise, NULL);
297 /* Call rpc function */
299 werror = cli_spoolss_enum_printers(
300 cli, mem_ctx, 0, &needed, flags, level,
301 &num_printers, &ctr);
303 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
304 werror = cli_spoolss_enum_printers(
305 cli, mem_ctx, needed, NULL, flags, level,
306 &num_printers, &ctr);
310 if (!W_ERROR_IS_OK(werror)) {
311 PyErr_SetObject(spoolss_werror,
312 PyInt_FromLong(W_ERROR_V(werror)));
316 result = PyList_New(num_printers);
320 for (i = 0; i < num_printers; i++) {
323 py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
325 PyList_SetItem(result, i, value);
330 for(i = 0; i < num_printers; i++) {
333 py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
335 PyList_SetItem(result, i, value);
340 for(i = 0; i < num_printers; i++) {
343 py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
345 PyList_SetItem(result, i, value);
350 for(i = 0; i < num_printers; i++) {
353 py_from_PRINTER_INFO_3(&value, &ctr.printers_3[i]);
355 PyList_SetItem(result, i, value);