Broke out stuff into other files.
[bbaumbach/samba-autobuild/.git] / source3 / 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 "includes.h"
22 #include "Python.h"
23
24 #include "python/py_common.h"
25 #include "python/py_conv.h"
26 #include "python/py_spoolss.h"
27
28 /* Exceptions this module can raise */
29
30 PyObject *spoolss_error, *spoolss_werror;
31
32 /*
33  * Routines to convert from python hashes to Samba structures
34  */
35
36 /* Return a cli_state struct opened on the SPOOLSS pipe.  If credentials
37    are passed use them. */
38
39 typedef struct cli_state *(cli_pipe_fn)(
40         struct cli_state *cli, char *system_name,
41         struct ntuser_creds *creds);
42
43 struct cli_state *open_pipe_creds(char *system_name, PyObject *creds, 
44                                   cli_pipe_fn *connect_fn,
45                                   struct cli_state *cli)
46 {
47         struct ntuser_creds nt_creds;
48
49         if (!cli) {
50                 cli = (struct cli_state *)malloc(sizeof(struct cli_state));
51                 if (!cli)
52                         return NULL;
53         }
54
55         ZERO_STRUCTP(cli);
56
57         /* Extract credentials from the python dictionary and initialise
58            the ntuser_creds struct from them. */
59
60         ZERO_STRUCT(nt_creds);
61         nt_creds.pwd.null_pwd = True;
62
63         if (creds) {
64                 char *username, *password, *domain;
65                 PyObject *username_obj, *password_obj, *domain_obj;
66
67                 /* Check credentials passed are valid.  This means the
68                    username, domain and password keys must exist and be
69                    string objects. */
70
71                 username_obj = PyDict_GetItemString(creds, "username");
72                 domain_obj = PyDict_GetItemString(creds, "domain");
73                 password_obj = PyDict_GetItemString(creds, "password");
74
75                 if (!username_obj || !domain_obj || !password_obj) {
76                 error:
77                         PyErr_SetString(spoolss_error, "invalid credentials");
78                         return NULL;
79                 }
80
81                 if (!PyString_Check(username_obj) || 
82                     !PyString_Check(domain_obj) || 
83                     !PyString_Check(password_obj))
84                         goto error;
85
86                 username = PyString_AsString(username_obj);
87                 domain = PyString_AsString(domain_obj);
88                 password = PyString_AsString(password_obj);
89
90                 if (!username || !domain || !password)
91                         goto error;
92
93                 /* Initialise nt_creds structure with passed creds */
94
95                 fstrcpy(nt_creds.user_name, username);
96                 fstrcpy(nt_creds.domain, domain);
97
98                 if (lp_encrypted_passwords())
99                         pwd_make_lm_nt_16(&nt_creds.pwd, password);
100                 else
101                         pwd_set_cleartext(&nt_creds.pwd, password);
102
103                 nt_creds.pwd.null_pwd = False;
104         }
105
106         /* Now try to connect */
107
108         connect_fn(cli, system_name, &nt_creds);
109
110         return cli;
111 }
112
113 static PyObject *new_policy_hnd_object(struct cli_state *cli, 
114                                        TALLOC_CTX *mem_ctx, POLICY_HND *pol)
115 {
116         spoolss_policy_hnd_object *o;
117
118         o = PyObject_New(spoolss_policy_hnd_object, &spoolss_policy_hnd_type);
119
120         o->cli = cli;
121         o->mem_ctx = mem_ctx;
122         memcpy(&o->pol, pol, sizeof(POLICY_HND));
123
124         return (PyObject*)o;
125 }
126      
127 /* 
128  * Method dispatch table
129  */
130
131 #include "py_spoolss_printers.c"
132 #include "py_spoolss_drivers.c"
133 #include "py_spoolss_ports.c"
134 #include "py_spoolss_forms.c"
135
136 static PyMethodDef spoolss_methods[] = {
137
138         /* Open/close printer handles */
139         
140         { "openprinter", spoolss_openprinter, METH_VARARGS | METH_KEYWORDS, 
141           "Open printer" },
142         
143         { "closeprinter", spoolss_closeprinter, METH_VARARGS, 
144           "Close printer" },
145
146         /* Server enumeratation functions */
147
148         { "enumprinters", spoolss_enumprinters, METH_VARARGS | METH_KEYWORDS,
149           "Enumerate printers" },
150
151         { "enumports", spoolss_enumports, METH_VARARGS | METH_KEYWORDS,
152           "Enumerate ports" },
153
154         { "enumprinterdrivers", spoolss_enumprinterdrivers, METH_VARARGS |
155           METH_KEYWORDS, "Enumerate printer drivers" },
156
157         /* Miscellaneous other commands */
158
159         { "getprinterdriverdir", spoolss_getprinterdriverdir, METH_VARARGS |
160           METH_KEYWORDS, "Get printer driver directory" },
161
162         { NULL }
163 };
164
165 /* Methods attached to a spoolss handle object */
166
167 static PyMethodDef spoolss_hnd_methods[] = {
168
169         /* Printer info */
170
171         { "getprinter", spoolss_getprinter, METH_VARARGS | METH_KEYWORDS,
172           "Fetch printer information" },
173
174         { "setprinter", spoolss_setprinter, METH_VARARGS | METH_KEYWORDS,
175           "Set printer information" },
176
177         /* Printer drivers */
178
179         { "getprinterdriver", spoolss_getprinterdriver, 
180           METH_VARARGS | METH_KEYWORDS, "Fetch printer driver" },
181
182         /* Forms */
183
184         { "enumforms", spoolss_enumforms, METH_VARARGS | METH_KEYWORDS,
185           "Enumerate forms" },
186
187         { "setform", spoolss_setform, METH_VARARGS | METH_KEYWORDS,
188           "Modify properties of a form" },
189
190         { "addform", spoolss_addform, METH_VARARGS | METH_KEYWORDS,
191           "Insert a form" },
192
193         { "getform", spoolss_getform, METH_VARARGS | METH_KEYWORDS,
194           "Fetch form properties" },
195
196         { "deleteform", spoolss_deleteform, METH_VARARGS | METH_KEYWORDS,
197           "Delete a form" },
198
199         { NULL }
200
201 };
202
203 static void py_policy_hnd_dealloc(PyObject* self)
204 {
205         PyObject_Del(self);
206 }
207
208 static PyObject *py_policy_hnd_getattr(PyObject *self, char *attrname)
209 {
210         return Py_FindMethod(spoolss_hnd_methods, self, attrname);
211 }
212
213 PyTypeObject spoolss_policy_hnd_type = {
214         PyObject_HEAD_INIT(NULL)
215         0,
216         "Spoolss Policy Handle",
217         sizeof(spoolss_policy_hnd_object),
218         0,
219         py_policy_hnd_dealloc, /*tp_dealloc*/
220         0,          /*tp_print*/
221         py_policy_hnd_getattr,          /*tp_getattr*/
222         0,          /*tp_setattr*/
223         0,          /*tp_compare*/
224         0,          /*tp_repr*/
225         0,          /*tp_as_number*/
226         0,          /*tp_as_sequence*/
227         0,          /*tp_as_mapping*/
228         0,          /*tp_hash */
229 };
230
231 /* Initialise constants */
232
233 struct spoolss_const {
234         char *name;
235         uint32 value;
236 } spoolss_const_vals[] = {
237         
238         /* Access permissions */
239
240         { "MAXIMUM_ALLOWED_ACCESS", MAXIMUM_ALLOWED_ACCESS },
241         { "SERVER_ALL_ACCESS", SERVER_ALL_ACCESS },
242         { "PRINTER_ALL_ACCESS", PRINTER_ALL_ACCESS },
243
244         /* Printer enumeration flags */
245
246         { "PRINTER_ENUM_DEFAULT", PRINTER_ENUM_DEFAULT },
247         { "PRINTER_ENUM_LOCAL", PRINTER_ENUM_LOCAL },
248         { "PRINTER_ENUM_CONNECTIONS", PRINTER_ENUM_CONNECTIONS },
249         { "PRINTER_ENUM_FAVORITE", PRINTER_ENUM_FAVORITE },
250         { "PRINTER_ENUM_NAME", PRINTER_ENUM_NAME },
251         { "PRINTER_ENUM_REMOTE", PRINTER_ENUM_REMOTE },
252         { "PRINTER_ENUM_SHARED", PRINTER_ENUM_SHARED },
253         { "PRINTER_ENUM_NETWORK", PRINTER_ENUM_NETWORK },
254
255         { NULL },
256 };
257
258 static void const_init(PyObject *dict)
259 {
260         struct spoolss_const *tmp;
261         PyObject *obj;
262
263         for (tmp = spoolss_const_vals; tmp->name; tmp++) {
264                 obj = PyInt_FromLong(tmp->value);
265                 PyDict_SetItemString(dict, tmp->name, obj);
266                 Py_DECREF(obj);
267         }
268 }
269
270 /* Module initialisation */
271
272 void initspoolss(void)
273 {
274         PyObject *module, *dict;
275
276         /* Initialise module */
277
278         module = Py_InitModule("spoolss", spoolss_methods);
279         dict = PyModule_GetDict(module);
280
281         /* Make spools_error global an exception we can raise when an error
282            occurs. */
283
284         spoolss_error = PyErr_NewException("spoolss.error", NULL, NULL);
285         PyDict_SetItemString(dict, "error", spoolss_error);
286
287         spoolss_werror = PyErr_NewException("spoolss.werror", NULL, NULL);
288         PyDict_SetItemString(dict, "werror", spoolss_werror);
289
290         /* Initialise policy handle object */
291
292         spoolss_policy_hnd_type.ob_type = &PyType_Type;
293
294         /* Initialise constants */
295
296         const_init(dict);
297
298         /* Do samba initialisation */
299
300         py_samba_init();
301 }