Shutdown cli_state in the dealloc function. This happens automatically
[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_policy_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
32                                 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 
57 dictionary of (username, password) may be given in which case they are
58 used when opening the RPC pipe.  An access mask may also be given which
59 defaults 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, [level, arch, creds]) -> 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         { NULL }
190
191 };
192
193 static void py_policy_hnd_dealloc(PyObject* self)
194 {
195         spoolss_policy_hnd_object *hnd;
196
197         /* Close down policy handle and free talloc context */
198
199         hnd = (spoolss_policy_hnd_object*)self;
200
201         cli_shutdown(hnd->cli);
202         talloc_destroy(hnd->mem_ctx);
203
204         PyObject_Del(self);
205 }
206
207 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
208 {
209         return Py_FindMethod(spoolss_hnd_methods, self, attrname);
210 }
211
212 static char spoolss_type_doc[] = 
213 "Python wrapper for Windows NT SPOOLSS rpc pipe.";
214
215 PyTypeObject spoolss_policy_hnd_type = {
216         PyObject_HEAD_INIT(NULL)
217         0,
218         "spoolss.hnd",
219         sizeof(spoolss_policy_hnd_object),
220         0,
221         py_policy_hnd_dealloc,  /* tp_dealloc*/
222         0,                      /* tp_print*/
223         py_policy_hnd_getattr,  /* tp_getattr*/
224         0,                      /* tp_setattr*/
225         0,                      /* tp_compare*/
226         0,                      /* tp_repr*/
227         0,                      /* tp_as_number*/
228         0,                      /* tp_as_sequence*/
229         0,                      /* tp_as_mapping*/
230         0,                      /* tp_hash */
231         0,                      /* tp_call */
232         0,                      /* tp_str */
233         0,                      /* tp_getattro */
234         0,                      /* tp_setattro */
235         0,                      /* tp_as_buffer*/
236         Py_TPFLAGS_DEFAULT,     /* tp_flags */
237         spoolss_type_doc,       /* tp_doc */
238 };
239
240 /* Initialise constants */
241
242 struct spoolss_const {
243         char *name;
244         uint32 value;
245 } spoolss_const_vals[] = {
246         
247         /* Access permissions */
248
249         { "MAXIMUM_ALLOWED_ACCESS", MAXIMUM_ALLOWED_ACCESS },
250         { "SERVER_ALL_ACCESS", SERVER_ALL_ACCESS },
251         { "SERVER_READ", SERVER_READ },
252         { "SERVER_WRITE", SERVER_WRITE },
253         { "SERVER_EXECUTE", SERVER_EXECUTE },
254         { "SERVER_ACCESS_ADMINISTER", SERVER_ACCESS_ADMINISTER },
255         { "SERVER_ACCESS_ENUMERATE", SERVER_ACCESS_ENUMERATE },
256         { "PRINTER_ALL_ACCESS", PRINTER_ALL_ACCESS },
257         { "PRINTER_READ", PRINTER_READ },
258         { "PRINTER_WRITE", PRINTER_WRITE },
259         { "PRINTER_EXECUTE", PRINTER_EXECUTE },
260         { "PRINTER_ACCESS_ADMINISTER", PRINTER_ACCESS_ADMINISTER },
261         { "PRINTER_ACCESS_USE", PRINTER_ACCESS_USE },
262         { "JOB_ACCESS_ADMINISTER", JOB_ACCESS_ADMINISTER },
263         { "JOB_ALL_ACCESS", JOB_ALL_ACCESS },
264         { "JOB_READ", JOB_READ },
265         { "JOB_WRITE", JOB_WRITE },
266         { "JOB_EXECUTE", JOB_EXECUTE },
267         { "STANDARD_RIGHTS_ALL_ACCESS", STANDARD_RIGHTS_ALL_ACCESS },
268         { "STANDARD_RIGHTS_EXECUTE_ACCESS", STANDARD_RIGHTS_EXECUTE_ACCESS },
269         { "STANDARD_RIGHTS_READ_ACCESS", STANDARD_RIGHTS_READ_ACCESS },
270         { "STANDARD_RIGHTS_REQUIRED_ACCESS", STANDARD_RIGHTS_REQUIRED_ACCESS },
271         { "STANDARD_RIGHTS_WRITE_ACCESS", STANDARD_RIGHTS_WRITE_ACCESS },
272
273         /* Printer enumeration flags */
274
275         { "PRINTER_ENUM_DEFAULT", PRINTER_ENUM_DEFAULT },
276         { "PRINTER_ENUM_LOCAL", PRINTER_ENUM_LOCAL },
277         { "PRINTER_ENUM_CONNECTIONS", PRINTER_ENUM_CONNECTIONS },
278         { "PRINTER_ENUM_FAVORITE", PRINTER_ENUM_FAVORITE },
279         { "PRINTER_ENUM_NAME", PRINTER_ENUM_NAME },
280         { "PRINTER_ENUM_REMOTE", PRINTER_ENUM_REMOTE },
281         { "PRINTER_ENUM_SHARED", PRINTER_ENUM_SHARED },
282         { "PRINTER_ENUM_NETWORK", PRINTER_ENUM_NETWORK },
283
284         /* Form types */
285
286         { "FORM_USER", FORM_USER },
287         { "FORM_BUILTIN", FORM_BUILTIN },
288         { "FORM_PRINTER", FORM_PRINTER },
289
290         /* WERRORs */
291
292         { "WERR_OK", 0 },
293         { "WERR_BADFILE", 2 },
294         { "WERR_ACCESS_DENIED", 5 },
295         { "WERR_BADFID", 6 },
296         { "WERR_BADFUNC", 1 },
297         { "WERR_INSUFFICIENT_BUFFER", 122 },
298         { "WERR_NO_SUCH_SHARE", 67 },
299         { "WERR_ALREADY_EXISTS", 80 },
300         { "WERR_INVALID_PARAM", 87 },
301         { "WERR_NOT_SUPPORTED", 50 },
302         { "WERR_BAD_PASSWORD", 86 },
303         { "WERR_NOMEM", 8 },
304         { "WERR_INVALID_NAME", 123 },
305         { "WERR_UNKNOWN_LEVEL", 124 },
306         { "WERR_OBJECT_PATH_INVALID", 161 },
307         { "WERR_NO_MORE_ITEMS", 259 },
308         { "WERR_MORE_DATA", 234 },
309         { "WERR_UNKNOWN_PRINTER_DRIVER", 1797 },
310         { "WERR_INVALID_PRINTER_NAME", 1801 },
311         { "WERR_PRINTER_ALREADY_EXISTS", 1802 },
312         { "WERR_INVALID_DATATYPE", 1804 },
313         { "WERR_INVALID_ENVIRONMENT", 1805 },
314         { "WERR_INVALID_FORM_NAME", 1902 },
315         { "WERR_INVALID_FORM_SIZE", 1903 },
316         { "WERR_BUF_TOO_SMALL", 2123 },
317         { "WERR_JOB_NOT_FOUND", 2151 },
318         { "WERR_DEST_NOT_FOUND", 2152 },
319         { "WERR_NOT_LOCAL_DOMAIN", 2320 },
320         { "WERR_PRINTER_DRIVER_IN_USE", 3001 },
321         { "WERR_STATUS_MORE_ENTRIES  ", 0x0105 },
322
323         { NULL },
324 };
325
326 static void const_init(PyObject *dict)
327 {
328         struct spoolss_const *tmp;
329         PyObject *obj;
330
331         for (tmp = spoolss_const_vals; tmp->name; tmp++) {
332                 obj = PyInt_FromLong(tmp->value);
333                 PyDict_SetItemString(dict, tmp->name, obj);
334                 Py_DECREF(obj);
335         }
336 }
337
338 /* Module initialisation */
339
340 void initspoolss(void)
341 {
342         PyObject *module, *dict;
343
344         /* Initialise module */
345
346         module = Py_InitModule("spoolss", spoolss_methods);
347         dict = PyModule_GetDict(module);
348
349         /* Exceptions we can raise */
350
351         spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
352         PyDict_SetItemString(dict, "error", spoolss_error);
353
354         spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
355         PyDict_SetItemString(dict, "werror", spoolss_werror);
356
357         /* Initialise policy handle object */
358
359         spoolss_policy_hnd_type.ob_type = &PyType_Type;
360
361         PyDict_SetItemString(dict, "spoolss.hnd", 
362                              (PyObject *)&spoolss_policy_hnd_type);
363
364         /* Initialise constants */
365
366         const_init(dict);
367
368         /* Do samba initialisation */
369
370         py_samba_init();
371 }