Added setjob() command.
[samba.git] / source / python / py_spoolss.c
1 /* 
2    Python wrappers for DCERPC/SMB client routines.
3
4    Copyright (C) Tim Potter, 2002
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "python/py_spoolss.h"
22
23 /* Exceptions this module can raise */
24
25 PyObject *spoolss_error, *spoolss_werror;
26
27 /*
28  * Routines to convert from python hashes to Samba structures
29  */
30
31 PyObject *new_spoolss_policy_hnd_object(struct cli_state *cli, 
32                                         TALLOC_CTX *mem_ctx, POLICY_HND *pol)
33 {
34         spoolss_policy_hnd_object *o;
35
36         o = PyObject_New(spoolss_policy_hnd_object, &spoolss_policy_hnd_type);
37
38         o->cli = cli;
39         o->mem_ctx = mem_ctx;
40         memcpy(&o->pol, pol, sizeof(POLICY_HND));
41
42         return (PyObject*)o;
43 }
44      
45 /* 
46  * Method dispatch table
47  */
48
49 static PyMethodDef spoolss_methods[] = {
50
51         /* Open/close printer handles */
52         
53         { "openprinter", spoolss_openprinter, METH_VARARGS | METH_KEYWORDS, 
54           "openprinter(printername, [creds, access]) -> <spoolss hnd object>
55
56 Open a printer given by printername in UNC format.  Optionally a dictionary
57 of (domain, username, password) may be given in which case they are used
58 when opening the RPC pipe.  An access mask may also be given which defaults
59 to MAXIMUM_ALLOWED_ACCESS.
60
61 Example:
62
63 >>> hnd = spoolss.openprinter(\"\\\\\\\\NPSD-PDC2\\\\meanie\")
64 "},
65         
66         { "closeprinter", spoolss_closeprinter, METH_VARARGS, 
67           "closeprinter()
68
69 Close a printer handle opened with openprinter or addprinter.
70
71 Example:
72
73 >>> spoolss.closeprinter(hnd)
74 "},
75
76         /* Server enumeratation functions */
77
78         { "enumprinters", spoolss_enumprinters, METH_VARARGS | METH_KEYWORDS,
79           "enumprinters(server, [creds, level, flags]) -> list
80
81 Return a list of printers on a print server.  The credentials, info level
82 and flags may be specified as keyword arguments.
83
84 Example:
85
86 >>> print spoolss.enumprinters(\"\\\\\\\\npsd-pdc2\")
87 [{'comment': 'i am a comment', 'printer_name': 'meanie', 'flags': 8388608, 
88   'description': 'meanie,Generic / Text Only,i am a location'}, 
89  {'comment': '', 'printer_name': 'fileprint', 'flags': 8388608, 
90   'description': 'fileprint,Generic / Text Only,'}]
91 "},
92
93         { "enumports", spoolss_enumports, METH_VARARGS | METH_KEYWORDS,
94           "enumports(server, [creds, level]) -> list
95
96 Return a list of ports on a print server.
97
98 Example:
99
100 >>> print spoolss.enumports(\"\\\\\\\\npsd-pdc2\")
101 [{'name': 'LPT1:'}, {'name': 'LPT2:'}, {'name': 'COM1:'}, {'name': 'COM2:'}, 
102  {'name': 'FILE:'}, {'name': '\\\\nautilus1\\zpekt3r'}]
103 "},
104
105         { "enumprinterdrivers", spoolss_enumprinterdrivers, METH_VARARGS |
106           METH_KEYWORDS, 
107 "enumprinterdrivers(server, [creds, level, arch]) -> list
108
109 Return a list of printer drivers.
110 "},
111         /* Miscellaneous other commands */
112
113         { "getprinterdriverdir", spoolss_getprinterdriverdir, METH_VARARGS |
114           METH_KEYWORDS, "getprinterdriverdir(server, [creds]) -> string
115
116 Return the printer driver directory for a given architecture.  The 
117 architecture defaults to \"Windows NT x86\".
118 "},
119
120         /* Other stuff - this should really go into a samba config module
121            but for the moment let's leave it here. */
122
123         { "setup_logging", py_setup_logging, METH_VARARGS | METH_KEYWORDS, 
124           "" },
125
126         { "get_debuglevel", get_debuglevel, METH_VARARGS, "" },
127         { "set_debuglevel", set_debuglevel, METH_VARARGS, "" },
128
129         { NULL }
130 };
131
132 /* Methods attached to a spoolss handle object */
133
134 static PyMethodDef spoolss_hnd_methods[] = {
135
136         /* Printer info */
137
138         { "getprinter", spoolss_getprinter, METH_VARARGS | METH_KEYWORDS,
139           "getprinter([level]) -> dict
140
141 Return a dictionary of print information.  The info level defaults to 1.
142
143 Example:
144
145 >>> hnd.getprinter()
146 {'comment': 'i am a comment', 'printer_name': '\\\\NPSD-PDC2\\meanie', 
147  'description': '\\\\NPSD-PDC2\\meanie,Generic / Text Only,i am a location',
148  'flags': 8388608}
149 "},
150
151         { "setprinter", spoolss_setprinter, METH_VARARGS | METH_KEYWORDS,
152           "setprinter(dict) -> None
153
154 Set printer information.
155 "},
156
157         /* Printer drivers */
158
159         { "getprinterdriver", spoolss_getprinterdriver, 
160           METH_VARARGS | METH_KEYWORDS, 
161           "getprinterdriver([level = 1, arch = \"Windows NT x86\"] -> dict
162
163 Return a dictionary of printer driver information.
164 "},
165
166         /* Forms */
167
168         { "enumforms", spoolss_enumforms, METH_VARARGS | METH_KEYWORDS,
169           "enumforms([level = 1]) -> list
170
171 Return a list of forms supported by a printer.
172 "},
173
174         { "setform", spoolss_setform, METH_VARARGS | METH_KEYWORDS,
175           "setform(dict) -> None
176
177 Set the form given by the dictionary argument.
178 "},
179
180         { "addform", spoolss_addform, METH_VARARGS | METH_KEYWORDS,
181           "Insert a form" },
182
183         { "getform", spoolss_getform, METH_VARARGS | METH_KEYWORDS,
184           "Fetch form properties" },
185
186         { "deleteform", spoolss_deleteform, METH_VARARGS | METH_KEYWORDS,
187           "Delete a form" },
188
189         /* Job related methods */
190
191         { "enumjobs", spoolss_enumjobs, METH_VARARGS | METH_KEYWORDS,
192           "Enumerate jobs" },
193
194         { "setjob", spoolss_setjob, METH_VARARGS | METH_KEYWORDS,
195           "Set job command" },
196
197         { NULL }
198
199 };
200
201 static void py_policy_hnd_dealloc(PyObject* self)
202 {
203         spoolss_policy_hnd_object *hnd;
204
205         /* Close down policy handle and free talloc context */
206
207         hnd = (spoolss_policy_hnd_object*)self;
208
209         cli_shutdown(hnd->cli);
210         talloc_destroy(hnd->mem_ctx);
211
212         PyObject_Del(self);
213 }
214
215 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
216 {
217         return Py_FindMethod(spoolss_hnd_methods, self, attrname);
218 }
219
220 static char spoolss_type_doc[] = 
221 "Python wrapper for Windows NT SPOOLSS rpc pipe.";
222
223 PyTypeObject spoolss_policy_hnd_type = {
224         PyObject_HEAD_INIT(NULL)
225         0,
226         "spoolss.hnd",
227         sizeof(spoolss_policy_hnd_object),
228         0,
229         py_policy_hnd_dealloc,  /* tp_dealloc*/
230         0,                      /* tp_print*/
231         py_policy_hnd_getattr,  /* tp_getattr*/
232         0,                      /* tp_setattr*/
233         0,                      /* tp_compare*/
234         0,                      /* tp_repr*/
235         0,                      /* tp_as_number*/
236         0,                      /* tp_as_sequence*/
237         0,                      /* tp_as_mapping*/
238         0,                      /* tp_hash */
239         0,                      /* tp_call */
240         0,                      /* tp_str */
241         0,                      /* tp_getattro */
242         0,                      /* tp_setattro */
243         0,                      /* tp_as_buffer*/
244         Py_TPFLAGS_DEFAULT,     /* tp_flags */
245         spoolss_type_doc,       /* tp_doc */
246 };
247
248 /* Initialise constants */
249
250 struct spoolss_const {
251         char *name;
252         uint32 value;
253 } spoolss_const_vals[] = {
254         
255         /* Access permissions */
256
257         { "MAXIMUM_ALLOWED_ACCESS", MAXIMUM_ALLOWED_ACCESS },
258         { "SERVER_ALL_ACCESS", SERVER_ALL_ACCESS },
259         { "SERVER_READ", SERVER_READ },
260         { "SERVER_WRITE", SERVER_WRITE },
261         { "SERVER_EXECUTE", SERVER_EXECUTE },
262         { "SERVER_ACCESS_ADMINISTER", SERVER_ACCESS_ADMINISTER },
263         { "SERVER_ACCESS_ENUMERATE", SERVER_ACCESS_ENUMERATE },
264         { "PRINTER_ALL_ACCESS", PRINTER_ALL_ACCESS },
265         { "PRINTER_READ", PRINTER_READ },
266         { "PRINTER_WRITE", PRINTER_WRITE },
267         { "PRINTER_EXECUTE", PRINTER_EXECUTE },
268         { "PRINTER_ACCESS_ADMINISTER", PRINTER_ACCESS_ADMINISTER },
269         { "PRINTER_ACCESS_USE", PRINTER_ACCESS_USE },
270         { "JOB_ACCESS_ADMINISTER", JOB_ACCESS_ADMINISTER },
271         { "JOB_ALL_ACCESS", JOB_ALL_ACCESS },
272         { "JOB_READ", JOB_READ },
273         { "JOB_WRITE", JOB_WRITE },
274         { "JOB_EXECUTE", JOB_EXECUTE },
275         { "STANDARD_RIGHTS_ALL_ACCESS", STANDARD_RIGHTS_ALL_ACCESS },
276         { "STANDARD_RIGHTS_EXECUTE_ACCESS", STANDARD_RIGHTS_EXECUTE_ACCESS },
277         { "STANDARD_RIGHTS_READ_ACCESS", STANDARD_RIGHTS_READ_ACCESS },
278         { "STANDARD_RIGHTS_REQUIRED_ACCESS", STANDARD_RIGHTS_REQUIRED_ACCESS },
279         { "STANDARD_RIGHTS_WRITE_ACCESS", STANDARD_RIGHTS_WRITE_ACCESS },
280
281         /* Printer enumeration flags */
282
283         { "PRINTER_ENUM_DEFAULT", PRINTER_ENUM_DEFAULT },
284         { "PRINTER_ENUM_LOCAL", PRINTER_ENUM_LOCAL },
285         { "PRINTER_ENUM_CONNECTIONS", PRINTER_ENUM_CONNECTIONS },
286         { "PRINTER_ENUM_FAVORITE", PRINTER_ENUM_FAVORITE },
287         { "PRINTER_ENUM_NAME", PRINTER_ENUM_NAME },
288         { "PRINTER_ENUM_REMOTE", PRINTER_ENUM_REMOTE },
289         { "PRINTER_ENUM_SHARED", PRINTER_ENUM_SHARED },
290         { "PRINTER_ENUM_NETWORK", PRINTER_ENUM_NETWORK },
291
292         /* Form types */
293
294         { "FORM_USER", FORM_USER },
295         { "FORM_BUILTIN", FORM_BUILTIN },
296         { "FORM_PRINTER", FORM_PRINTER },
297
298         /* WERRORs */
299
300         { "WERR_OK", 0 },
301         { "WERR_BADFILE", 2 },
302         { "WERR_ACCESS_DENIED", 5 },
303         { "WERR_BADFID", 6 },
304         { "WERR_BADFUNC", 1 },
305         { "WERR_INSUFFICIENT_BUFFER", 122 },
306         { "WERR_NO_SUCH_SHARE", 67 },
307         { "WERR_ALREADY_EXISTS", 80 },
308         { "WERR_INVALID_PARAM", 87 },
309         { "WERR_NOT_SUPPORTED", 50 },
310         { "WERR_BAD_PASSWORD", 86 },
311         { "WERR_NOMEM", 8 },
312         { "WERR_INVALID_NAME", 123 },
313         { "WERR_UNKNOWN_LEVEL", 124 },
314         { "WERR_OBJECT_PATH_INVALID", 161 },
315         { "WERR_NO_MORE_ITEMS", 259 },
316         { "WERR_MORE_DATA", 234 },
317         { "WERR_UNKNOWN_PRINTER_DRIVER", 1797 },
318         { "WERR_INVALID_PRINTER_NAME", 1801 },
319         { "WERR_PRINTER_ALREADY_EXISTS", 1802 },
320         { "WERR_INVALID_DATATYPE", 1804 },
321         { "WERR_INVALID_ENVIRONMENT", 1805 },
322         { "WERR_INVALID_FORM_NAME", 1902 },
323         { "WERR_INVALID_FORM_SIZE", 1903 },
324         { "WERR_BUF_TOO_SMALL", 2123 },
325         { "WERR_JOB_NOT_FOUND", 2151 },
326         { "WERR_DEST_NOT_FOUND", 2152 },
327         { "WERR_NOT_LOCAL_DOMAIN", 2320 },
328         { "WERR_PRINTER_DRIVER_IN_USE", 3001 },
329         { "WERR_STATUS_MORE_ENTRIES  ", 0x0105 },
330
331         /* Job control constants */
332
333         { "JOB_CONTROL_PAUSE", JOB_CONTROL_PAUSE },
334         { "JOB_CONTROL_RESUME", JOB_CONTROL_RESUME },
335         { "JOB_CONTROL_CANCEL", JOB_CONTROL_CANCEL },
336         { "JOB_CONTROL_RESTART", JOB_CONTROL_RESTART },
337         { "JOB_CONTROL_DELETE", JOB_CONTROL_DELETE },
338
339         { NULL },
340 };
341
342 static void const_init(PyObject *dict)
343 {
344         struct spoolss_const *tmp;
345         PyObject *obj;
346
347         for (tmp = spoolss_const_vals; tmp->name; tmp++) {
348                 obj = PyInt_FromLong(tmp->value);
349                 PyDict_SetItemString(dict, tmp->name, obj);
350                 Py_DECREF(obj);
351         }
352 }
353
354 /* Module initialisation */
355
356 void initspoolss(void)
357 {
358         PyObject *module, *dict;
359
360         /* Initialise module */
361
362         module = Py_InitModule("spoolss", spoolss_methods);
363         dict = PyModule_GetDict(module);
364
365         /* Exceptions we can raise */
366
367         spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
368         PyDict_SetItemString(dict, "error", spoolss_error);
369
370         spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
371         PyDict_SetItemString(dict, "werror", spoolss_werror);
372
373         /* Initialise policy handle object */
374
375         spoolss_policy_hnd_type.ob_type = &PyType_Type;
376
377         PyDict_SetItemString(dict, "spoolss.hnd", 
378                              (PyObject *)&spoolss_policy_hnd_type);
379
380         /* Initialise constants */
381
382         const_init(dict);
383
384         /* Do samba initialisation */
385
386         py_samba_init();
387 }