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));
176 /* Set printer information */
178 PyObject *spoolss_setprinter(PyObject *self, PyObject *args, PyObject *kw)
180 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
182 PyObject *info, *level_obj;
183 PRINTER_INFO_CTR ctr;
185 static char *kwlist[] = {"dict", NULL};
187 PRINTER_INFO_0 printers_0;
188 PRINTER_INFO_1 printers_1;
189 PRINTER_INFO_2 printers_2;
190 PRINTER_INFO_3 printers_3;
191 PRINTER_INFO_4 printers_4;
192 PRINTER_INFO_5 printers_5;
195 /* Parse parameters */
197 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist,
198 &PyDict_Type, &info))
201 /* Check dictionary contains a level */
203 if ((level_obj = PyDict_GetItemString(info, "level"))) {
205 if (!PyInt_Check(level_obj))
208 level = PyInt_AsLong(level_obj);
212 PyErr_SetString(spoolss_error, "invalid info");
216 /* Fill in printer info */
222 PyObject *devmode_obj;
224 ctr.printers_2 = &pinfo.printers_2;
226 if (!py_to_PRINTER_INFO_2(&pinfo.printers_2, info))
230 devmode_obj = PyDict_GetItemString(info, "device_mode");
232 pinfo.printers_2.devmode = talloc(
233 hnd->mem_ctx, sizeof(DEVICEMODE));
235 PyDEVICEMODE_AsDEVICEMODE(pinfo.printers_2.devmode,
240 /* FIXME: can we actually set the security descriptor using
241 a setprinter level 2? */
243 pinfo.printers_2.secdesc = NULL;
244 pinfo.printers_2.secdesc = NULL;
250 PyErr_SetString(spoolss_error, "unsupported info level");
254 /* Call rpc function */
256 werror = cli_spoolss_setprinter(hnd->cli, hnd->mem_ctx, &hnd->pol,
261 if (!W_ERROR_IS_OK(werror)) {
262 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
270 /* Enumerate printers */
272 PyObject *spoolss_enumprinters(PyObject *self, PyObject *args, PyObject *kw)
275 PyObject *result, *creds = NULL;
276 PRINTER_INFO_CTR ctr;
277 int level = 1, flags = PRINTER_ENUM_LOCAL, i;
278 uint32 needed, num_printers;
279 static char *kwlist[] = {"server", "name", "level", "flags",
281 TALLOC_CTX *mem_ctx = NULL;
282 struct cli_state *cli = NULL;
283 char *server, *name = NULL;
285 /* Parse parameters */
287 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|siiO!", kwlist,
288 &server, &name, &level, &flags,
289 &PyDict_Type, &creds))
292 if (server[0] == '\\' && server[1] == '\\')
295 mem_ctx = talloc_init();
296 cli = open_pipe_creds(server, creds, cli_spoolss_initialise, NULL);
298 /* Call rpc function */
300 werror = cli_spoolss_enum_printers(
301 cli, mem_ctx, 0, &needed, flags, level,
302 &num_printers, &ctr);
304 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
305 werror = cli_spoolss_enum_printers(
306 cli, mem_ctx, needed, NULL, flags, level,
307 &num_printers, &ctr);
311 if (!W_ERROR_IS_OK(werror)) {
312 PyErr_SetObject(spoolss_werror,
313 PyInt_FromLong(W_ERROR_V(werror)));
317 result = PyList_New(num_printers);
321 for (i = 0; i < num_printers; i++) {
324 py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
326 PyList_SetItem(result, i, value);
331 for(i = 0; i < num_printers; i++) {
334 py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
336 PyList_SetItem(result, i, value);
341 for(i = 0; i < num_printers; i++) {
344 py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
346 PyList_SetItem(result, i, value);
351 for(i = 0; i < num_printers; i++) {
354 py_from_PRINTER_INFO_3(&value, &ctr.printers_3[i]);
356 PyList_SetItem(result, i, value);