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 *unc_name, *server = NULL, *errstr;
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, &unc_name, &PyDict_Type, &creds,
41 /* FIXME: Return name format exception for names without a UNC
44 server = strdup(unc_name + 2);
46 if (strchr(server, '\\')) {
47 char *c = strchr(server, '\\');
51 if (!(cli = open_pipe_creds(
52 server, creds, cli_spoolss_initialise, &errstr))) {
53 PyErr_SetString(spoolss_error, errstr);
58 if (!(mem_ctx = talloc_init())) {
59 PyErr_SetString(spoolss_error,
60 "unable to init talloc context\n");
64 werror = cli_spoolss_open_printer_ex(
65 cli, mem_ctx, unc_name, "", desired_access, server,
68 if (!W_ERROR_IS_OK(werror)) {
71 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
75 result = new_spoolss_policy_hnd_object(cli, mem_ctx, &hnd);
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);
108 /* Fetch printer information */
110 PyObject *spoolss_hnd_getprinter(PyObject *self, PyObject *args, PyObject *kw)
112 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
114 PyObject *result = NULL;
115 PRINTER_INFO_CTR ctr;
118 static char *kwlist[] = {"level", NULL};
120 /* Parse parameters */
122 if (!PyArg_ParseTupleAndKeywords(args, kw, "|i", kwlist, &level))
125 if (level < 0 || level > 3) {
126 PyErr_SetString(spoolss_error, "Invalid info level");
132 /* Call rpc function */
134 werror = cli_spoolss_getprinter(
135 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level, &ctr);
137 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
138 werror = cli_spoolss_getprinter(
139 hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
144 if (!W_ERROR_IS_OK(werror)) {
145 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
154 py_from_PRINTER_INFO_0(&result, ctr.printers_0);
158 py_from_PRINTER_INFO_1(&result, ctr.printers_1);
162 py_from_PRINTER_INFO_2(&result, ctr.printers_2);
166 py_from_PRINTER_INFO_3(&result, ctr.printers_3);
174 /* Set printer information */
176 PyObject *spoolss_hnd_setprinter(PyObject *self, PyObject *args, PyObject *kw)
178 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
181 PRINTER_INFO_CTR ctr;
183 static char *kwlist[] = {"dict", NULL};
185 PRINTER_INFO_2 printers_2;
186 PRINTER_INFO_3 printers_3;
189 /* Parse parameters */
191 if (!PyArg_ParseTupleAndKeywords(
192 args, kw, "O!", kwlist, &PyDict_Type, &info))
195 if (!get_level_value(info, &level)) {
196 PyErr_SetString(spoolss_error, "invalid info level");
200 if (level != 2 && level != 3) {
201 PyErr_SetString(spoolss_error, "unsupported info level");
205 /* Fill in printer info */
211 ctr.printers_2 = &pinfo.printers_2;
213 if (!py_to_PRINTER_INFO_2(&pinfo.printers_2, info,
215 PyErr_SetString(spoolss_error,
216 "error converting printer to info 2");
222 ctr.printers_3 = &pinfo.printers_3;
224 if (!py_to_PRINTER_INFO_3(&pinfo.printers_3, info,
226 PyErr_SetString(spoolss_error,
227 "error converting to printer info 3");
233 PyErr_SetString(spoolss_error, "unsupported info level");
237 /* Call rpc function */
239 werror = cli_spoolss_setprinter(hnd->cli, hnd->mem_ctx, &hnd->pol,
244 if (!W_ERROR_IS_OK(werror)) {
245 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
253 /* Enumerate printers */
255 PyObject *spoolss_enumprinters(PyObject *self, PyObject *args, PyObject *kw)
258 PyObject *result = NULL, *creds = NULL;
259 PRINTER_INFO_CTR ctr;
260 int level = 1, flags = PRINTER_ENUM_LOCAL, i;
261 uint32 needed, num_printers;
262 static char *kwlist[] = {"server", "level", "flags", "creds", NULL};
263 TALLOC_CTX *mem_ctx = NULL;
264 struct cli_state *cli = NULL;
265 char *server, *errstr;
267 /* Parse parameters */
269 if (!PyArg_ParseTupleAndKeywords(
270 args, kw, "s|iiO!", kwlist, &server, &level, &flags,
271 &PyDict_Type, &creds))
274 if (server[0] == '\\' && server[1] == '\\')
277 if (!(cli = open_pipe_creds(
278 server, creds, cli_spoolss_initialise, &errstr))) {
279 PyErr_SetString(spoolss_error, errstr);
284 if (!(mem_ctx = talloc_init())) {
286 spoolss_error, "unable to init talloc context\n");
290 /* Call rpc function */
292 werror = cli_spoolss_enum_printers(
293 cli, mem_ctx, 0, &needed, flags, level,
294 &num_printers, &ctr);
296 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
297 werror = cli_spoolss_enum_printers(
298 cli, mem_ctx, needed, NULL, flags, level,
299 &num_printers, &ctr);
301 if (!W_ERROR_IS_OK(werror)) {
302 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
310 result = PyDict_New();
312 for (i = 0; i < num_printers; i++) {
316 rpcstr_pull(name, ctr.printers_0[i].printername.buffer,
317 sizeof(fstring), -1, STR_TERMINATE);
319 py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
321 PyDict_SetItemString(
322 value, "level", PyInt_FromLong(0));
324 PyDict_SetItemString(result, name, value);
329 result = PyDict_New();
331 for(i = 0; i < num_printers; i++) {
335 rpcstr_pull(name, ctr.printers_1[i].name.buffer,
336 sizeof(fstring), -1, STR_TERMINATE);
338 py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
340 PyDict_SetItemString(
341 value, "level", PyInt_FromLong(1));
343 PyDict_SetItemString(result, name, value);
348 result = PyDict_New();
350 for(i = 0; i < num_printers; i++) {
354 rpcstr_pull(name, ctr.printers_2[i].printername.buffer,
355 sizeof(fstring), -1, STR_TERMINATE);
357 py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
359 PyDict_SetItemString(
360 value, "level", PyInt_FromLong(2));
362 PyDict_SetItemString(result, name, value);
367 PyErr_SetString(spoolss_error, "unknown info level");
376 talloc_destroy(mem_ctx);
383 PyObject *spoolss_addprinterex(PyObject *self, PyObject *args, PyObject *kw)
385 static char *kwlist[] = { "server", "printername", "info", "creds",
387 char *printername, *server, *errstr;
388 PyObject *info, *result = NULL, *creds = NULL;
389 struct cli_state *cli = NULL;
390 TALLOC_CTX *mem_ctx = NULL;
391 PRINTER_INFO_CTR ctr;
392 PRINTER_INFO_2 info2;
395 if (!PyArg_ParseTupleAndKeywords(
396 args, kw, "ssO!|O!", kwlist, &server, &printername,
397 &PyDict_Type, &info, &PyDict_Type, &creds))
400 if (!(cli = open_pipe_creds(
401 server, creds, cli_spoolss_initialise, &errstr))) {
402 PyErr_SetString(spoolss_error, errstr);
407 if (!(mem_ctx = talloc_init())) {
409 spoolss_error, "unable to init talloc context\n");
413 if (!py_to_PRINTER_INFO_2(&info2, info, mem_ctx)) {
414 PyErr_SetString(spoolss_error,
415 "error converting to printer info 2");
419 ctr.printers_2 = &info2;
421 werror = cli_spoolss_addprinterex(cli, mem_ctx, 2, &ctr);
428 talloc_destroy(mem_ctx);