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;
180 PyObject *info, *level_obj;
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 /* Check dictionary contains a level */
197 if ((level_obj = PyDict_GetItemString(info, "level"))) {
199 if (!PyInt_Check(level_obj)) {
200 PyErr_SetString(spoolss_error,
201 "level not an integer");
205 level = PyInt_AsLong(level_obj);
207 /* Only level 2, 3 supported by NT */
209 if (level != 2 && level != 3) {
210 PyErr_SetString(spoolss_error,
211 "unsupported info level");
216 PyErr_SetString(spoolss_error, "no info level present");
220 /* Fill in printer info */
226 ctr.printers_2 = &pinfo.printers_2;
228 if (!py_to_PRINTER_INFO_2(&pinfo.printers_2, info,
230 PyErr_SetString(spoolss_error,
231 "error converting printer to info 2");
237 ctr.printers_3 = &pinfo.printers_3;
239 if (!py_to_PRINTER_INFO_3(&pinfo.printers_3, info,
241 PyErr_SetString(spoolss_error,
242 "error converting to printer info 3");
248 PyErr_SetString(spoolss_error, "unsupported info level");
252 /* Call rpc function */
254 werror = cli_spoolss_setprinter(hnd->cli, hnd->mem_ctx, &hnd->pol,
259 if (!W_ERROR_IS_OK(werror)) {
260 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
268 /* Enumerate printers */
270 PyObject *spoolss_enumprinters(PyObject *self, PyObject *args, PyObject *kw)
273 PyObject *result = NULL, *creds = NULL;
274 PRINTER_INFO_CTR ctr;
275 int level = 1, flags = PRINTER_ENUM_LOCAL, i;
276 uint32 needed, num_printers;
277 static char *kwlist[] = {"server", "level", "flags", "creds", NULL};
278 TALLOC_CTX *mem_ctx = NULL;
279 struct cli_state *cli = NULL;
280 char *server, *errstr;
282 /* Parse parameters */
284 if (!PyArg_ParseTupleAndKeywords(
285 args, kw, "s|iiO!", kwlist, &server, &level, &flags,
286 &PyDict_Type, &creds))
289 if (server[0] == '\\' && server[1] == '\\')
292 if (!(cli = open_pipe_creds(
293 server, creds, cli_spoolss_initialise, &errstr))) {
294 PyErr_SetString(spoolss_error, errstr);
299 if (!(mem_ctx = talloc_init())) {
301 spoolss_error, "unable to init talloc context\n");
305 /* Call rpc function */
307 werror = cli_spoolss_enum_printers(
308 cli, mem_ctx, 0, &needed, flags, level,
309 &num_printers, &ctr);
311 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
312 werror = cli_spoolss_enum_printers(
313 cli, mem_ctx, needed, NULL, flags, level,
314 &num_printers, &ctr);
316 if (!W_ERROR_IS_OK(werror)) {
317 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
325 result = PyDict_New();
327 for (i = 0; i < num_printers; i++) {
331 rpcstr_pull(name, ctr.printers_0[i].printername.buffer,
332 sizeof(fstring), -1, STR_TERMINATE);
334 py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
336 PyDict_SetItemString(
337 value, "level", PyInt_FromLong(0));
339 PyDict_SetItemString(result, name, value);
344 result = PyDict_New();
346 for(i = 0; i < num_printers; i++) {
350 rpcstr_pull(name, ctr.printers_1[i].name.buffer,
351 sizeof(fstring), -1, STR_TERMINATE);
353 py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
355 PyDict_SetItemString(
356 value, "level", PyInt_FromLong(1));
358 PyDict_SetItemString(result, name, value);
363 result = PyDict_New();
365 for(i = 0; i < num_printers; i++) {
369 rpcstr_pull(name, ctr.printers_2[i].printername.buffer,
370 sizeof(fstring), -1, STR_TERMINATE);
372 py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
374 PyDict_SetItemString(
375 value, "level", PyInt_FromLong(2));
377 PyDict_SetItemString(result, name, value);
382 PyErr_SetString(spoolss_error, "unknown info level");
391 talloc_destroy(mem_ctx);
398 PyObject *spoolss_addprinterex(PyObject *self, PyObject *args, PyObject *kw)
400 static char *kwlist[] = { "server", "printername", "info", "creds",
402 char *printername, *server, *errstr;
403 PyObject *info, *result = NULL, *creds = NULL;
404 struct cli_state *cli = NULL;
405 TALLOC_CTX *mem_ctx = NULL;
406 PRINTER_INFO_CTR ctr;
407 PRINTER_INFO_2 info2;
410 if (!PyArg_ParseTupleAndKeywords(
411 args, kw, "ssO!|O!", kwlist, &server, &printername,
412 &PyDict_Type, &info, &PyDict_Type, &creds))
415 if (!(cli = open_pipe_creds(
416 server, creds, cli_spoolss_initialise, &errstr))) {
417 PyErr_SetString(spoolss_error, errstr);
422 if (!(mem_ctx = talloc_init())) {
424 spoolss_error, "unable to init talloc context\n");
428 if (!py_to_PRINTER_INFO_2(&info2, info, mem_ctx)) {
429 PyErr_SetString(spoolss_error,
430 "error converting to printer info 2");
434 ctr.printers_2 = &info2;
436 werror = cli_spoolss_addprinterex(cli, mem_ctx, 2, &ctr);
443 talloc_destroy(mem_ctx);