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_spoolss_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_2 printers_2;
185 PRINTER_INFO_3 printers_3;
188 /* Parse parameters */
190 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist,
191 &PyDict_Type, &info))
194 /* Check dictionary contains a level */
196 if ((level_obj = PyDict_GetItemString(info, "level"))) {
198 if (!PyInt_Check(level_obj)) {
199 DEBUG(0, ("** level not an integer\n"));
203 level = PyInt_AsLong(level_obj);
205 /* Only level 2, 3 supported by NT */
207 if (level != 2 && level != 3) {
208 DEBUG(0, ("** unsupported info level\n"));
213 DEBUG(0, ("** no level info\n"));
215 PyErr_SetString(spoolss_error, "invalid info");
219 /* Fill in printer info */
225 ctr.printers_2 = &pinfo.printers_2;
227 if (!py_to_PRINTER_INFO_2(&pinfo.printers_2, info,
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, *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", "name", "level", "flags",
264 TALLOC_CTX *mem_ctx = NULL;
265 struct cli_state *cli = NULL;
266 char *server, *name = NULL;
268 /* Parse parameters */
270 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|siiO!", kwlist,
271 &server, &name, &level, &flags,
272 &PyDict_Type, &creds))
275 if (server[0] == '\\' && server[1] == '\\')
278 mem_ctx = talloc_init();
279 cli = open_pipe_creds(server, creds, cli_spoolss_initialise, NULL);
281 /* Call rpc function */
283 werror = cli_spoolss_enum_printers(
284 cli, mem_ctx, 0, &needed, flags, level,
285 &num_printers, &ctr);
287 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
288 werror = cli_spoolss_enum_printers(
289 cli, mem_ctx, needed, NULL, flags, level,
290 &num_printers, &ctr);
294 if (!W_ERROR_IS_OK(werror)) {
295 PyErr_SetObject(spoolss_werror,
296 PyInt_FromLong(W_ERROR_V(werror)));
300 result = PyList_New(num_printers);
304 for (i = 0; i < num_printers; i++) {
307 py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
309 PyList_SetItem(result, i, value);
314 for(i = 0; i < num_printers; i++) {
317 py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
319 PyList_SetItem(result, i, value);
324 for(i = 0; i < num_printers; i++) {
327 py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
329 PyList_SetItem(result, i, value);
334 for(i = 0; i < num_printers; i++) {
337 py_from_PRINTER_INFO_3(&value, &ctr.printers_3[i]);
339 PyList_SetItem(result, i, value);