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, *errstr;
28 TALLOC_CTX *mem_ctx = 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|Oi", kwlist, &unc_name, &creds,
41 if (unc_name[0] != '\\' || unc_name[1] != '\\') {
42 PyErr_SetString(PyExc_ValueError, "UNC name required");
46 server = SMB_STRDUP(unc_name + 2);
48 if (strchr(server, '\\')) {
49 char *c = strchr(server, '\\');
53 if (creds && creds != Py_None && !PyDict_Check(creds)) {
54 PyErr_SetString(PyExc_TypeError,
55 "credentials must be dictionary or None");
59 if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
60 PyErr_SetString(spoolss_error, errstr);
65 if (!(mem_ctx = talloc_init("spoolss_openprinter"))) {
66 PyErr_SetString(spoolss_error,
67 "unable to init talloc context\n");
71 werror = cli_spoolss_open_printer_ex(
72 cli, mem_ctx, unc_name, "", desired_access, server,
75 if (!W_ERROR_IS_OK(werror)) {
76 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
80 result = new_spoolss_policy_hnd_object(cli, mem_ctx, &hnd);
88 talloc_destroy(mem_ctx);
98 PyObject *spoolss_closeprinter(PyObject *self, PyObject *args)
101 spoolss_policy_hnd_object *hnd;
104 /* Parse parameters */
106 if (!PyArg_ParseTuple(args, "O!", &spoolss_policy_hnd_type, &po))
109 hnd = (spoolss_policy_hnd_object *)po;
111 /* Call rpc function */
113 result = cli_spoolss_close_printer(hnd->cli, hnd->mem_ctx, &hnd->pol);
121 /* Fetch printer information */
123 PyObject *spoolss_hnd_getprinter(PyObject *self, PyObject *args, PyObject *kw)
125 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
127 PyObject *result = NULL;
128 PRINTER_INFO_CTR ctr;
130 static char *kwlist[] = {"level", NULL};
132 /* Parse parameters */
134 if (!PyArg_ParseTupleAndKeywords(args, kw, "|i", kwlist, &level))
139 /* Call rpc function */
141 werror = cli_spoolss_getprinter(
142 hnd->cli, hnd->mem_ctx, &hnd->pol, level, &ctr);
146 if (!W_ERROR_IS_OK(werror)) {
147 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
156 py_from_PRINTER_INFO_0(&result, ctr.printers_0);
160 py_from_PRINTER_INFO_1(&result, ctr.printers_1);
164 py_from_PRINTER_INFO_2(&result, ctr.printers_2);
168 py_from_PRINTER_INFO_3(&result, ctr.printers_3);
176 /* Set printer information */
178 PyObject *spoolss_hnd_setprinter(PyObject *self, PyObject *args, PyObject *kw)
180 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
183 PRINTER_INFO_CTR ctr;
185 static char *kwlist[] = {"dict", NULL};
187 PRINTER_INFO_1 printers_1;
188 PRINTER_INFO_2 printers_2;
189 PRINTER_INFO_3 printers_3;
192 /* Parse parameters */
194 if (!PyArg_ParseTupleAndKeywords(
195 args, kw, "O!", kwlist, &PyDict_Type, &info))
198 if (!get_level_value(info, &level)) {
199 PyErr_SetString(spoolss_error, "invalid info level");
203 if (level < 1 && level > 3) {
204 PyErr_SetString(spoolss_error, "unsupported info level");
208 /* Fill in printer info */
214 ctr.printers_1 = &pinfo.printers_1;
216 if (!py_to_PRINTER_INFO_1(ctr.printers_1, info)){
217 PyErr_SetString(spoolss_error,
218 "error converting printer to info 1");
224 ctr.printers_2 = &pinfo.printers_2;
226 if (!py_to_PRINTER_INFO_2(ctr.printers_2, info,
228 PyErr_SetString(spoolss_error,
229 "error converting printer to info 2");
235 ctr.printers_3 = &pinfo.printers_3;
237 if (!py_to_PRINTER_INFO_3(ctr.printers_3, info,
239 PyErr_SetString(spoolss_error,
240 "error converting to printer info 3");
246 PyErr_SetString(spoolss_error, "unsupported info level");
250 /* Call rpc function */
252 werror = cli_spoolss_setprinter(hnd->cli, hnd->mem_ctx, &hnd->pol,
257 if (!W_ERROR_IS_OK(werror)) {
258 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
266 /* Enumerate printers */
268 PyObject *spoolss_enumprinters(PyObject *self, PyObject *args, PyObject *kw)
271 PyObject *result = NULL, *creds = NULL;
272 PRINTER_INFO_CTR ctr;
273 int level = 1, flags = PRINTER_ENUM_LOCAL, i;
275 static char *kwlist[] = {"server", "name", "level", "flags",
277 TALLOC_CTX *mem_ctx = NULL;
278 struct cli_state *cli = NULL;
279 char *server, *errstr, *name = NULL;
281 /* Parse parameters */
283 if (!PyArg_ParseTupleAndKeywords(
284 args, kw, "s|siiO", kwlist, &server, &name, &level,
288 if (server[0] != '\\' || server[1] != '\\') {
289 PyErr_SetString(PyExc_ValueError, "UNC name required");
295 if (creds && creds != Py_None && !PyDict_Check(creds)) {
296 PyErr_SetString(PyExc_TypeError,
297 "credentials must be dictionary or None");
301 if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
302 PyErr_SetString(spoolss_error, errstr);
307 if (!(mem_ctx = talloc_init("spoolss_enumprinters"))) {
309 spoolss_error, "unable to init talloc context\n");
313 /* This RPC is weird. By setting the server name to different
314 values we can get different behaviour. If however the server
315 name is not specified, we default it to being the full server
316 name as this is probably what the caller intended. To pass a
317 NULL name, pass a value of "" */
326 /* Call rpc function */
328 werror = cli_spoolss_enum_printers(cli, mem_ctx, name, flags, level,
329 &num_printers, &ctr);
331 if (!W_ERROR_IS_OK(werror)) {
332 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
340 result = PyDict_New();
342 for (i = 0; i < num_printers; i++) {
346 rpcstr_pull(s, ctr.printers_0[i].printername.buffer,
347 sizeof(fstring), -1, STR_TERMINATE);
349 py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
351 PyDict_SetItemString(
352 value, "level", PyInt_FromLong(0));
354 PyDict_SetItemString(result, s, value);
359 result = PyDict_New();
361 for(i = 0; i < num_printers; i++) {
365 rpcstr_pull(s, ctr.printers_1[i].name.buffer,
366 sizeof(fstring), -1, STR_TERMINATE);
368 py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
370 PyDict_SetItemString(
371 value, "level", PyInt_FromLong(1));
373 PyDict_SetItemString(result, s, value);
378 result = PyDict_New();
380 for(i = 0; i < num_printers; i++) {
384 rpcstr_pull(s, ctr.printers_2[i].printername.buffer,
385 sizeof(fstring), -1, STR_TERMINATE);
387 py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
389 PyDict_SetItemString(
390 value, "level", PyInt_FromLong(2));
392 PyDict_SetItemString(result, s, value);
397 PyErr_SetString(spoolss_error, "unknown info level");
406 talloc_destroy(mem_ctx);
413 PyObject *spoolss_addprinterex(PyObject *self, PyObject *args, PyObject *kw)
415 static char *kwlist[] = { "server", "printername", "info", "creds",
417 char *printername, *server, *errstr;
418 PyObject *info, *result = NULL, *creds = NULL;
419 struct cli_state *cli = NULL;
420 TALLOC_CTX *mem_ctx = NULL;
421 PRINTER_INFO_CTR ctr;
422 PRINTER_INFO_2 info2;
425 if (!PyArg_ParseTupleAndKeywords(
426 args, kw, "ssO!|O!", kwlist, &server, &printername,
427 &PyDict_Type, &info, &PyDict_Type, &creds))
430 if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
431 PyErr_SetString(spoolss_error, errstr);
436 if (!(mem_ctx = talloc_init("spoolss_addprinterex"))) {
438 spoolss_error, "unable to init talloc context\n");
442 if (!py_to_PRINTER_INFO_2(&info2, info, mem_ctx)) {
443 PyErr_SetString(spoolss_error,
444 "error converting to printer info 2");
448 ctr.printers_2 = &info2;
450 werror = cli_spoolss_addprinterex(cli, mem_ctx, 2, &ctr);
460 talloc_destroy(mem_ctx);