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.
23 #include "python/py_common.h"
24 #include "python/py_spoolss.h"
25 #include "python/py_spoolss_forms.h"
27 /* Exceptions this module can raise */
29 PyObject *spoolss_error, *spoolss_werror;
31 static void py_policy_hnd_dealloc(PyObject* self)
38 static PyMethodDef spoolss_hnd_methods[] = {
42 { "enumforms", spoolss_enumforms, METH_VARARGS | METH_KEYWORDS,
45 { "setform", spoolss_setform, METH_VARARGS | METH_KEYWORDS,
46 "Modify properties of a form" },
48 { "addform", spoolss_addform, METH_VARARGS | METH_KEYWORDS,
51 { "getform", spoolss_getform, METH_VARARGS | METH_KEYWORDS,
52 "Fetch form properties" },
54 { "deleteform", spoolss_deleteform, METH_VARARGS | METH_KEYWORDS,
60 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
62 return Py_FindMethod(spoolss_hnd_methods, self, attrname);
65 static PyObject *new_policy_hnd_object(struct cli_state *cli,
66 TALLOC_CTX *mem_ctx, POLICY_HND *pol)
68 spoolss_policy_hnd_object *o;
70 o = PyObject_New(spoolss_policy_hnd_object, &spoolss_policy_hnd_type);
74 memcpy(&o->pol, pol, sizeof(POLICY_HND));
79 PyTypeObject spoolss_policy_hnd_type = {
80 PyObject_HEAD_INIT(NULL)
83 sizeof(spoolss_policy_hnd_object),
85 py_policy_hnd_dealloc, /*tp_dealloc*/
87 py_policy_hnd_getattr, /*tp_getattr*/
98 * Routines to convert from python hashes to Samba structures
101 struct pyconv py_PRINTER_INFO_0[] = {
102 { "printer_name", PY_UNISTR, offsetof(PRINTER_INFO_0, printername) },
103 { "server_name", PY_UNISTR, offsetof(PRINTER_INFO_0, servername) },
105 { "cjobs", PY_UINT32, offsetof(PRINTER_INFO_0, cjobs) },
106 { "total_jobs", PY_UINT32, offsetof(PRINTER_INFO_0, total_jobs) },
107 { "total_bytes", PY_UINT32, offsetof(PRINTER_INFO_0, total_bytes) },
109 { "year", PY_UINT16, offsetof(PRINTER_INFO_0, year) },
110 { "month", PY_UINT16, offsetof(PRINTER_INFO_0, month) },
111 { "day_of_week", PY_UINT16, offsetof(PRINTER_INFO_0, dayofweek) },
112 { "day", PY_UINT16, offsetof(PRINTER_INFO_0, day) },
113 { "hour", PY_UINT16, offsetof(PRINTER_INFO_0, hour) },
114 { "minute", PY_UINT16, offsetof(PRINTER_INFO_0, minute) },
115 { "second", PY_UINT16, offsetof(PRINTER_INFO_0, second) },
116 { "milliseconds", PY_UINT16, offsetof(PRINTER_INFO_0, milliseconds) },
118 { "global_counter", PY_UINT32, offsetof(PRINTER_INFO_0, global_counter) },
119 { "total_pages", PY_UINT32, offsetof(PRINTER_INFO_0, total_pages) },
121 { "major_version", PY_UINT16, offsetof(PRINTER_INFO_0, major_version) },
122 { "build_version", PY_UINT16, offsetof(PRINTER_INFO_0, build_version) },
124 { "unknown7", PY_UINT32, offsetof(PRINTER_INFO_0, unknown7) },
125 { "unknown8", PY_UINT32, offsetof(PRINTER_INFO_0, unknown8) },
126 { "unknown9", PY_UINT32, offsetof(PRINTER_INFO_0, unknown9) },
127 { "session_counter", PY_UINT32, offsetof(PRINTER_INFO_0, session_counter)},
128 { "unknown11", PY_UINT32, offsetof(PRINTER_INFO_0, unknown11) },
129 { "printer_errors", PY_UINT32, offsetof(PRINTER_INFO_0, printer_errors) },
130 { "unknown13", PY_UINT32, offsetof(PRINTER_INFO_0, unknown13) },
131 { "unknown14", PY_UINT32, offsetof(PRINTER_INFO_0, unknown14) },
132 { "unknown15", PY_UINT32, offsetof(PRINTER_INFO_0, unknown15) },
133 { "unknown16", PY_UINT32, offsetof(PRINTER_INFO_0, unknown16) },
134 { "change_id", PY_UINT32, offsetof(PRINTER_INFO_0, change_id) },
135 { "unknown18", PY_UINT32, offsetof(PRINTER_INFO_0, unknown18) },
136 { "status", PY_UINT32, offsetof(PRINTER_INFO_0, status) },
137 { "unknown20", PY_UINT32, offsetof(PRINTER_INFO_0, unknown20) },
138 { "c_setprinter", PY_UINT32, offsetof(PRINTER_INFO_0, c_setprinter) },
139 { "unknown22", PY_UINT32, offsetof(PRINTER_INFO_0, unknown22) },
140 { "unknown23", PY_UINT32, offsetof(PRINTER_INFO_0, unknown23) },
141 { "unknown24", PY_UINT32, offsetof(PRINTER_INFO_0, unknown24) },
142 { "unknown25", PY_UINT32, offsetof(PRINTER_INFO_0, unknown25) },
143 { "unknown26", PY_UINT32, offsetof(PRINTER_INFO_0, unknown26) },
144 { "unknown27", PY_UINT32, offsetof(PRINTER_INFO_0, unknown27) },
145 { "unknown28", PY_UINT32, offsetof(PRINTER_INFO_0, unknown28) },
146 { "unknown29", PY_UINT32, offsetof(PRINTER_INFO_0, unknown29) },
151 struct pyconv py_PRINTER_INFO_1[] = {
152 { "printer_name", PY_UNISTR, offsetof(PRINTER_INFO_1, name) },
153 { "description", PY_UNISTR, offsetof(PRINTER_INFO_1, description) },
154 { "comment", PY_UNISTR, offsetof(PRINTER_INFO_1, comment) },
155 { "flags", PY_UINT32, offsetof(PRINTER_INFO_1, flags) },
159 struct pyconv py_PRINTER_INFO_2[] = {
160 { "server_name", PY_UNISTR, offsetof(PRINTER_INFO_2, servername) },
161 { "printer_name", PY_UNISTR, offsetof(PRINTER_INFO_2, printername) },
162 { "share_name", PY_UNISTR, offsetof(PRINTER_INFO_2, sharename) },
163 { "port_name", PY_UNISTR, offsetof(PRINTER_INFO_2, portname) },
164 { "driver_name", PY_UNISTR, offsetof(PRINTER_INFO_2, drivername) },
165 { "comment", PY_UNISTR, offsetof(PRINTER_INFO_2, comment) },
166 { "location", PY_UNISTR, offsetof(PRINTER_INFO_2, location) },
167 { "datatype", PY_UNISTR, offsetof(PRINTER_INFO_2, datatype) },
168 { "sepfile", PY_UNISTR, offsetof(PRINTER_INFO_2, sepfile) },
169 { "print_processor", PY_UNISTR, offsetof(PRINTER_INFO_2, printprocessor) },
170 { "parameters", PY_UNISTR, offsetof(PRINTER_INFO_2, parameters) },
171 { "attributes", PY_UINT32, offsetof(PRINTER_INFO_2, attributes) },
172 { "default_priority", PY_UINT32, offsetof(PRINTER_INFO_2, defaultpriority) },
173 { "priority", PY_UINT32, offsetof(PRINTER_INFO_2, priority) },
174 { "start_time", PY_UINT32, offsetof(PRINTER_INFO_2, starttime) },
175 { "until_time", PY_UINT32, offsetof(PRINTER_INFO_2, untiltime) },
176 { "status", PY_UINT32, offsetof(PRINTER_INFO_2, status) },
177 { "cjobs", PY_UINT32, offsetof(PRINTER_INFO_2, cjobs) },
178 { "average_ppm", PY_UINT32, offsetof(PRINTER_INFO_2, averageppm) },
182 struct pyconv py_PRINTER_INFO_3[] = {
183 { "flags", PY_UINT32, offsetof(PRINTER_INFO_3, flags) },
187 struct pyconv py_DEVICEMODE[] = {
188 { "device_name", PY_UNISTR, offsetof(DEVICEMODE, devicename) },
189 { "spec_version", PY_UINT16, offsetof(DEVICEMODE, specversion) },
190 { "driver_version", PY_UINT16, offsetof(DEVICEMODE, driverversion) },
191 { "size", PY_UINT16, offsetof(DEVICEMODE, size) },
192 { "fields", PY_UINT16, offsetof(DEVICEMODE, fields) },
193 { "orientation", PY_UINT16, offsetof(DEVICEMODE, orientation) },
194 { "paper_size", PY_UINT16, offsetof(DEVICEMODE, papersize) },
195 { "paper_width", PY_UINT16, offsetof(DEVICEMODE, paperwidth) },
196 { "paper_length", PY_UINT16, offsetof(DEVICEMODE, paperlength) },
197 { "scale", PY_UINT16, offsetof(DEVICEMODE, scale) },
198 { "copies", PY_UINT16, offsetof(DEVICEMODE, copies) },
199 { "default_source", PY_UINT16, offsetof(DEVICEMODE, defaultsource) },
200 { "print_quality", PY_UINT16, offsetof(DEVICEMODE, printquality) },
201 { "color", PY_UINT16, offsetof(DEVICEMODE, color) },
202 { "duplex", PY_UINT16, offsetof(DEVICEMODE, duplex) },
203 { "y_resolution", PY_UINT16, offsetof(DEVICEMODE, yresolution) },
204 { "tt_option", PY_UINT16, offsetof(DEVICEMODE, ttoption) },
205 { "collate", PY_UINT16, offsetof(DEVICEMODE, collate) },
206 { "form_name", PY_UNISTR, offsetof(DEVICEMODE, formname) },
207 { "log_pixels", PY_UINT16, offsetof(DEVICEMODE, logpixels) },
208 { "bits_per_pel", PY_UINT32, offsetof(DEVICEMODE, bitsperpel) },
209 { "pels_width", PY_UINT32, offsetof(DEVICEMODE, pelswidth) },
210 { "pels_height", PY_UINT32, offsetof(DEVICEMODE, pelsheight) },
211 { "display_flags", PY_UINT32, offsetof(DEVICEMODE, displayflags) },
212 { "display_frequency", PY_UINT32, offsetof(DEVICEMODE, displayfrequency) },
213 { "icm_method", PY_UINT32, offsetof(DEVICEMODE, icmmethod) },
214 { "icm_intent", PY_UINT32, offsetof(DEVICEMODE, icmintent) },
215 { "media_type", PY_UINT32, offsetof(DEVICEMODE, mediatype) },
216 { "dither_type", PY_UINT32, offsetof(DEVICEMODE, dithertype) },
217 { "reserved1", PY_UINT32, offsetof(DEVICEMODE, reserved1) },
218 { "reserved2", PY_UINT32, offsetof(DEVICEMODE, reserved2) },
219 { "panning_width", PY_UINT32, offsetof(DEVICEMODE, panningwidth) },
220 { "panning_height", PY_UINT32, offsetof(DEVICEMODE, panningheight) },
224 struct pyconv py_DRIVER_INFO_1[] = {
225 { "name", PY_UNISTR, offsetof(DRIVER_INFO_1, name) },
229 struct pyconv py_DRIVER_INFO_2[] = {
230 { "version", PY_UINT32, offsetof(DRIVER_INFO_2, version) },
231 { "name", PY_UNISTR, offsetof(DRIVER_INFO_2, name) },
232 { "architecture", PY_UNISTR, offsetof(DRIVER_INFO_2, architecture) },
233 { "driver_path", PY_UNISTR, offsetof(DRIVER_INFO_2, driverpath) },
234 { "data_file", PY_UNISTR, offsetof(DRIVER_INFO_2, datafile) },
235 { "config_file", PY_UNISTR, offsetof(DRIVER_INFO_2, configfile) },
239 struct pyconv py_DRIVER_INFO_3[] = {
240 { "version", PY_UINT32, offsetof(DRIVER_INFO_3, version) },
241 { "name", PY_UNISTR, offsetof(DRIVER_INFO_3, name) },
242 { "architecture", PY_UNISTR, offsetof(DRIVER_INFO_3, architecture) },
243 { "driver_path", PY_UNISTR, offsetof(DRIVER_INFO_3, driverpath) },
244 { "data_file", PY_UNISTR, offsetof(DRIVER_INFO_3, datafile) },
245 { "config_file", PY_UNISTR, offsetof(DRIVER_INFO_3, configfile) },
246 { "help_file", PY_UNISTR, offsetof(DRIVER_INFO_3, helpfile) },
248 { "monitor_name", PY_UNISTR, offsetof(DRIVER_INFO_3, monitorname) },
249 { "default_datatype", PY_UNISTR, offsetof(DRIVER_INFO_3, defaultdatatype) },
253 struct pyconv py_DRIVER_INFO_6[] = {
254 { "version", PY_UINT32, offsetof(DRIVER_INFO_6, version) },
255 { "name", PY_UNISTR, offsetof(DRIVER_INFO_6, name) },
256 { "architecture", PY_UNISTR, offsetof(DRIVER_INFO_6, architecture) },
257 { "driver_path", PY_UNISTR, offsetof(DRIVER_INFO_6, driverpath) },
258 { "data_file", PY_UNISTR, offsetof(DRIVER_INFO_6, datafile) },
259 { "config_file", PY_UNISTR, offsetof(DRIVER_INFO_6, configfile) },
260 { "help_file", PY_UNISTR, offsetof(DRIVER_INFO_6, helpfile) },
262 { "monitor_name", PY_UNISTR, offsetof(DRIVER_INFO_6, monitorname) },
263 { "default_datatype", PY_UNISTR, offsetof(DRIVER_INFO_6, defaultdatatype) },
266 { "padding", PY_UINT32, offsetof(DRIVER_INFO_6, padding) },
267 { "driver_version_low", PY_UINT32, offsetof(DRIVER_INFO_6, driver_version_low) },
268 { "driver_version_high", PY_UINT32, offsetof(DRIVER_INFO_6, driver_version_high) },
269 { "mfg_name", PY_UNISTR, offsetof(DRIVER_INFO_6, mfgname) },
270 { "oem_url", PY_UNISTR, offsetof(DRIVER_INFO_6, oem_url) },
271 { "hardware_id", PY_UNISTR, offsetof(DRIVER_INFO_6, hardware_id) },
272 { "provider", PY_UNISTR, offsetof(DRIVER_INFO_6, provider) },
277 static void fstr_pull(fstring str, UNISTR *uni)
279 rpcstr_pull(str, uni->buffer, sizeof(fstring), 0, STR_TERMINATE);
282 /* Convert a structure to a Python dict */
284 PyObject *from_struct(void *s, struct pyconv *conv)
286 PyObject *obj, *item;
291 for (i = 0; conv[i].name; i++) {
292 switch (conv[i].type) {
294 UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
300 item = PyString_FromString(s);
301 PyDict_SetItemString(obj, conv[i].name, item);
306 uint32 *u = (uint32 *)((char *)s + conv[i].offset);
308 item = PyInt_FromLong(*u);
309 PyDict_SetItemString(obj, conv[i].name, item);
314 uint16 *u = (uint16 *)((char *)s + conv[i].offset);
316 item = PyInt_FromLong(*u);
317 PyDict_SetItemString(obj, conv[i].name, item);
329 /* Convert a Python dict to a structure */
331 void to_struct(void *s, PyObject *dict, struct pyconv *conv)
335 for (i = 0; conv[i].name; i++) {
338 obj = PyDict_GetItemString(dict, conv[i].name);
340 switch (conv[i].type) {
342 UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
345 if (obj && PyString_Check(obj))
346 s = PyString_AsString(obj);
353 uint32 *u = (uint32 *)((char *)s + conv[i].offset);
355 if (obj && PyInt_Check(obj))
356 *u = PyInt_AsLong(obj);
363 uint16 *u = (uint16 *)((char *)s + conv[i].offset);
365 if (obj && PyInt_Check(obj))
366 *u = PyInt_AsLong(obj);
378 /* Return a cli_state struct opened on the SPOOLSS pipe. If credentials
379 are passed use them. */
381 typedef struct cli_state *(cli_pipe_fn)(
382 struct cli_state *cli, char *system_name,
383 struct ntuser_creds *creds);
385 static struct cli_state *open_pipe_creds(char *system_name, PyObject *creds,
386 cli_pipe_fn *connect_fn,
387 struct cli_state *cli)
389 struct ntuser_creds nt_creds;
392 cli = (struct cli_state *)malloc(sizeof(struct cli_state));
399 /* Extract credentials from the python dictionary and initialise
400 the ntuser_creds struct from them. */
402 ZERO_STRUCT(nt_creds);
403 nt_creds.pwd.null_pwd = True;
406 char *username, *password, *domain;
407 PyObject *username_obj, *password_obj, *domain_obj;
409 /* Check credentials passed are valid. This means the
410 username, domain and password keys must exist and be
413 username_obj = PyDict_GetItemString(creds, "username");
414 domain_obj = PyDict_GetItemString(creds, "domain");
415 password_obj = PyDict_GetItemString(creds, "password");
417 if (!username_obj || !domain_obj || !password_obj) {
419 PyErr_SetString(spoolss_error, "invalid credentials");
423 if (!PyString_Check(username_obj) ||
424 !PyString_Check(domain_obj) ||
425 !PyString_Check(password_obj))
428 username = PyString_AsString(username_obj);
429 domain = PyString_AsString(domain_obj);
430 password = PyString_AsString(password_obj);
432 if (!username || !domain || !password)
435 /* Initialise nt_creds structure with passed creds */
437 fstrcpy(nt_creds.user_name, username);
438 fstrcpy(nt_creds.domain, domain);
440 if (lp_encrypted_passwords())
441 pwd_make_lm_nt_16(&nt_creds.pwd, password);
443 pwd_set_cleartext(&nt_creds.pwd, password);
445 nt_creds.pwd.null_pwd = False;
448 /* Now try to connect */
450 connect_fn(cli, system_name, &nt_creds);
457 static PyObject *spoolss_openprinter(PyObject *self, PyObject *args,
460 char *full_name, *computer_name = NULL;
464 PyObject *result = NULL, *creds = NULL;
465 static char *kwlist[] = { "printername", "creds", "access", NULL };
466 uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
467 struct cli_state *cli;
469 if (!PyArg_ParseTupleAndKeywords(
470 args, kw, "s|O!i", kwlist, &full_name, &PyDict_Type, &creds,
476 /* FIXME: Return name format exception for names without a UNC
479 computer_name = strdup(full_name + 2);
481 if (strchr(computer_name, '\\')) {
482 char *c = strchr(computer_name, '\\');
486 if (!(cli = open_pipe_creds(computer_name, creds,
487 cli_spoolss_initialise, NULL))) {
488 fprintf(stderr, "could not initialise cli state\n");
492 if (!(mem_ctx = talloc_init())) {
493 fprintf(stderr, "unable to initialise talloc context\n");
497 werror = cli_spoolss_open_printer_ex(
498 cli, mem_ctx, full_name, "", desired_access, computer_name,
501 if (!W_ERROR_IS_OK(werror)) {
504 PyErr_SetObject(spoolss_werror,
505 PyInt_FromLong(W_ERROR_V(werror)));
509 result = new_policy_hnd_object(cli, mem_ctx, &hnd);
512 SAFE_FREE(computer_name);
517 /* Close a printer */
519 static PyObject *spoolss_closeprinter(PyObject *self, PyObject *args)
522 spoolss_policy_hnd_object *hnd;
525 /* Parse parameters */
527 if (!PyArg_ParseTuple(args, "O!", &spoolss_policy_hnd_type, &po))
530 hnd = (spoolss_policy_hnd_object *)po;
532 /* Call rpc function */
534 result = cli_spoolss_close_printer(hnd->cli, hnd->mem_ctx, &hnd->pol);
536 /* Cleanup samba stuf */
538 cli_shutdown(hnd->cli);
539 talloc_destroy(hnd->mem_ctx);
547 /* Fetch printer driver */
549 static PyObject *spoolss_getprinterdriver(PyObject *self, PyObject *args,
553 spoolss_policy_hnd_object *hnd;
556 PRINTER_DRIVER_CTR ctr;
559 char *arch = "Windows NT x86";
560 static char *kwlist[] = {"hnd", "level", "arch", NULL};
562 /* Parse parameters */
564 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!|is", kwlist,
565 &spoolss_policy_hnd_type, &po, &level, &arch))
568 hnd = (spoolss_policy_hnd_object *)po;
570 /* Call rpc function */
572 werror = cli_spoolss_getprinterdriver(
573 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level,
576 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
577 werror = cli_spoolss_getprinterdriver(
578 hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
585 if (W_ERROR_IS_OK(werror)) {
588 result = from_struct(&ctr.info1, py_DRIVER_INFO_1);
591 result = from_struct(&ctr.info2, py_DRIVER_INFO_2);
594 result = from_struct(&ctr.info6, py_DRIVER_INFO_6);
606 /* Enumerate printer drivers */
608 static PyObject *spoolss_enumprinterdrivers(PyObject *self, PyObject *args,
612 spoolss_policy_hnd_object *hnd;
615 PRINTER_DRIVER_CTR ctr;
617 uint32 needed, num_drivers;
618 char *arch = "Windows NT x86";
619 static char *kwlist[] = {"hnd", "level", "arch", NULL};
621 /* Parse parameters */
623 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!|is", kwlist,
624 &spoolss_policy_hnd_type, &po, &level, &arch))
627 hnd = (spoolss_policy_hnd_object *)po;
629 /* Call rpc function */
631 werror = cli_spoolss_enumprinterdrivers(
632 hnd->cli, hnd->mem_ctx, 0, &needed, level, arch,
635 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
636 werror = cli_spoolss_enumprinterdrivers(
637 hnd->cli, hnd->mem_ctx, needed, NULL, level, arch,
644 if (!W_ERROR_IS_OK(werror))
649 result = PyList_New(num_drivers);
651 for (i = 0; i < num_drivers; i++) {
654 value = from_struct(&ctr.info1, py_DRIVER_INFO_1);
655 PyList_SetItem(result, i, value);
660 result = PyList_New(num_drivers);
662 for(i = 0; i < num_drivers; i++) {
665 value = from_struct(&ctr.info2, py_DRIVER_INFO_2);
666 PyList_SetItem(result, i, value);
671 result = PyList_New(num_drivers);
673 for(i = 0; i < num_drivers; i++) {
676 value = from_struct(&ctr.info2, py_DRIVER_INFO_6);
677 PyList_SetItem(result, i, value);
691 /* Convert a security descriptor to a Python dict */
693 static PyObject *PySID_FromSID(DOM_SID *sid)
702 if (sid_to_string(sidstr, sid))
703 return PyString_FromString(sidstr);
709 static PyObject *PyACE_FromACE(SEC_ACE *ace)
720 PyDict_SetItemString(obj, "type", PyInt_FromLong(ace->type));
721 PyDict_SetItemString(obj, "flags", PyInt_FromLong(ace->flags));
722 PyDict_SetItemString(obj, "mask", PyInt_FromLong(ace->info.mask));
724 PyDict_SetItemString(obj, "trustee", PySID_FromSID(&ace->trustee));
729 static PyObject *PyACL_FromACL(SEC_ACL *acl)
731 PyObject *obj, *ace_list;
741 PyDict_SetItemString(obj, "revision", PyInt_FromLong(acl->revision));
743 ace_list = PyList_New(acl->num_aces);
745 for (i = 0; i < acl->num_aces; i++)
746 PyList_SetItem(ace_list, i, PyACE_FromACE(&acl->ace[i]));
748 PyDict_SetItemString(obj, "ace_list", ace_list);
753 static PyObject *PySECDESC_FromSECDESC(SEC_DESC *sd)
755 PyObject *obj = PyDict_New();
757 PyDict_SetItemString(obj, "revision", PyInt_FromLong(sd->revision));
758 PyDict_SetItemString(obj, "type", PyInt_FromLong(sd->type));
760 PyDict_SetItemString(obj, "owner_sid", PySID_FromSID(sd->owner_sid));
761 PyDict_SetItemString(obj, "group_sid", PySID_FromSID(sd->grp_sid));
763 PyDict_SetItemString(obj, "dacl", PyACL_FromACL(sd->dacl));
764 PyDict_SetItemString(obj, "sacl", PyACL_FromACL(sd->sacl));
769 PyObject *PyDEVICEMODE_FromDEVICEMODE(DEVICEMODE *devmode)
773 obj = from_struct(devmode, py_DEVICEMODE);
775 PyDict_SetItemString(obj, "private",
776 PyString_FromStringAndSize(devmode->private,
777 devmode->driverextra));
782 /* Fetch printer information */
784 static PyObject *spoolss_getprinter(PyObject *self, PyObject *args,
788 spoolss_policy_hnd_object *hnd;
791 PRINTER_INFO_CTR ctr;
794 static char *kwlist[] = {"hnd", "level", NULL};
796 /* Parse parameters */
798 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!|i", kwlist,
799 &spoolss_policy_hnd_type, &po, &level))
802 hnd = (spoolss_policy_hnd_object *)po;
804 /* Call rpc function */
806 werror = cli_spoolss_getprinter(
807 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level, &ctr);
809 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
810 werror = cli_spoolss_getprinter(
811 hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
818 if (!W_ERROR_IS_OK(werror))
824 result = from_struct(ctr.printers_0, py_PRINTER_INFO_0);
829 result = from_struct(ctr.printers_1, py_PRINTER_INFO_1);
834 result = from_struct(ctr.printers_2, py_PRINTER_INFO_2);
836 PyDict_SetItemString(result, "security_descriptor",
837 PySECDESC_FromSECDESC(
838 ctr.printers_2->secdesc));
840 PyDict_SetItemString(result, "device_mode",
841 PyDEVICEMODE_FromDEVICEMODE(
842 ctr.printers_2->devmode));
847 result = from_struct(ctr.printers_3, py_PRINTER_INFO_3);
849 PyDict_SetItemString(result, "security_descriptor",
850 PySECDESC_FromSECDESC(
851 ctr.printers_3->secdesc));
863 /* Set printer information */
865 static PyObject *spoolss_setprinter(PyObject *self, PyObject *args,
869 spoolss_policy_hnd_object *hnd;
871 PyObject *result, *info;
872 PRINTER_INFO_CTR ctr;
874 static char *kwlist[] = { "hnd", "dict", "level", NULL };
876 PRINTER_INFO_0 printers_0;
877 PRINTER_INFO_1 printers_1;
878 PRINTER_INFO_2 printers_2;
879 PRINTER_INFO_3 printers_3;
880 PRINTER_INFO_4 printers_4;
881 PRINTER_INFO_5 printers_5;
884 /* Parse parameters */
886 if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|i", kwlist,
887 &spoolss_policy_hnd_type, &po,
888 &PyDict_Type, &info, &level))
891 hnd = (spoolss_policy_hnd_object *)po;
893 /* Fill in printer info */
899 ctr.printers_1 = &pinfo.printers_1;
900 to_struct(&pinfo.printers_1, info, py_PRINTER_INFO_1);
905 /* Call rpc function */
907 werror = cli_spoolss_setprinter(hnd->cli, hnd->mem_ctx, &hnd->pol,
914 if (!W_ERROR_IS_OK(werror))
922 /* Enumerate printers */
924 static PyObject *spoolss_enumprinters(PyObject *self, PyObject *args,
928 PyObject *result, *creds = NULL;
929 PRINTER_INFO_CTR ctr;
930 int level = 1, flags = PRINTER_ENUM_LOCAL, i;
931 uint32 needed, num_printers;
932 static char *kwlist[] = {"server", "name", "level", "flags",
934 TALLOC_CTX *mem_ctx = NULL;
935 struct cli_state *cli = NULL;
936 char *server, *name = NULL;
938 /* Parse parameters */
940 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|siiO!", kwlist,
941 &server, &name, &level, &flags,
942 &PyDict_Type, &creds))
945 if (server[0] == '\\' && server[1] == '\\')
948 mem_ctx = talloc_init();
949 cli = open_pipe_creds(server, creds, cli_spoolss_initialise, NULL);
951 /* Call rpc function */
953 werror = cli_spoolss_enum_printers(
954 cli, mem_ctx, 0, &needed, flags, level,
955 &num_printers, &ctr);
957 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
958 werror = cli_spoolss_enum_printers(
959 cli, mem_ctx, needed, NULL, flags, level,
960 &num_printers, &ctr);
966 if (!W_ERROR_IS_OK(werror))
969 result = PyList_New(num_printers);
973 for (i = 0; i < num_printers; i++) {
976 value = from_struct (
977 &ctr.printers_0[i], py_PRINTER_INFO_0);
979 PyList_SetItem(result, i, value);
984 for(i = 0; i < num_printers; i++) {
988 &ctr.printers_1[i], py_PRINTER_INFO_1);
990 PyList_SetItem(result, i, value);
995 for(i = 0; i < num_printers; i++) {
999 &ctr.printers_2[i], py_PRINTER_INFO_2);
1001 PyList_SetItem(result, i, value);
1006 for(i = 0; i < num_printers; i++) {
1009 value = from_struct(
1010 &ctr.printers_3[i], py_PRINTER_INFO_3);
1012 PyList_SetItem(result, i, value);
1024 * Method dispatch table
1027 #include "python/py_spoolss_forms.h"
1029 static PyMethodDef spoolss_methods[] = {
1031 /* Open/close printer handles */
1033 { "openprinter", spoolss_openprinter, METH_VARARGS | METH_KEYWORDS,
1036 { "closeprinter", spoolss_closeprinter, METH_VARARGS,
1039 /* Printer drivers */
1041 { "getprinterdriver", spoolss_getprinterdriver,
1042 METH_VARARGS | METH_KEYWORDS, "Fetch printer driver" },
1044 { "enumprinterdrivers", spoolss_enumprinterdrivers,
1045 METH_VARARGS | METH_KEYWORDS, "Enumerate printer drivers" },
1047 { "getprinter", spoolss_getprinter, METH_VARARGS | METH_KEYWORDS,
1048 "Fetch printer information" },
1050 { "setprinter", spoolss_setprinter, METH_VARARGS | METH_KEYWORDS,
1051 "Set printer information" },
1053 { "enumprinters", spoolss_enumprinters, METH_VARARGS | METH_KEYWORDS,
1054 "Enumerate printers" },
1059 /* Initialise constants */
1061 struct spoolss_const {
1064 } spoolss_const_vals[] = {
1066 /* Access permissions */
1068 { "MAXIMUM_ALLOWED_ACCESS", MAXIMUM_ALLOWED_ACCESS },
1069 { "SERVER_ALL_ACCESS", SERVER_ALL_ACCESS },
1070 { "PRINTER_ALL_ACCESS", PRINTER_ALL_ACCESS },
1072 /* Printer enumeration flags */
1074 { "PRINTER_ENUM_DEFAULT", PRINTER_ENUM_DEFAULT },
1075 { "PRINTER_ENUM_LOCAL", PRINTER_ENUM_LOCAL },
1076 { "PRINTER_ENUM_CONNECTIONS", PRINTER_ENUM_CONNECTIONS },
1077 { "PRINTER_ENUM_FAVORITE", PRINTER_ENUM_FAVORITE },
1078 { "PRINTER_ENUM_NAME", PRINTER_ENUM_NAME },
1079 { "PRINTER_ENUM_REMOTE", PRINTER_ENUM_REMOTE },
1080 { "PRINTER_ENUM_SHARED", PRINTER_ENUM_SHARED },
1081 { "PRINTER_ENUM_NETWORK", PRINTER_ENUM_NETWORK },
1086 static void const_init(PyObject *dict)
1088 struct spoolss_const *tmp;
1091 for (tmp = spoolss_const_vals; tmp->name; tmp++) {
1092 obj = PyInt_FromLong(tmp->value);
1093 PyDict_SetItemString(dict, tmp->name, obj);
1098 /* Module initialisation */
1100 void initspoolss(void)
1102 PyObject *module, *dict;
1104 /* Initialise module */
1106 module = Py_InitModule("spoolss", spoolss_methods);
1107 dict = PyModule_GetDict(module);
1109 /* Make spools_error global an exception we can raise when an error
1112 spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
1113 PyDict_SetItemString(dict, "error", spoolss_error);
1115 spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
1116 PyDict_SetItemString(dict, "werror", spoolss_werror);
1118 /* Initialise policy handle object */
1120 spoolss_policy_hnd_type.ob_type = &PyType_Type;
1122 /* Initialise constants */
1126 /* Do samba initialisation */