Explicitly return a list in py_ntstatus_tuple() and py_werror_typle(). Not
[gd/samba-autobuild/.git] / source3 / python / py_common.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
26 /* Return a tuple of (error code, error string) from a WERROR */
27
28 PyObject *py_werror_tuple(WERROR werror)
29 {
30         return Py_BuildValue("[is]", W_ERROR_V(werror), 
31                              dos_errstr(werror));
32 }
33
34 /* Return a tuple of (error code, error string) from a WERROR */
35
36 PyObject *py_ntstatus_tuple(NTSTATUS ntstatus)
37 {
38         return Py_BuildValue("[is]", NT_STATUS_V(ntstatus), 
39                              nt_errstr(ntstatus));
40 }
41
42 /* Initialise samba client routines */
43
44 static BOOL initialised;
45
46 void py_samba_init(void)
47 {
48         if (initialised)
49                 return;
50
51         /* Load configuration file */
52
53         if (!lp_load(dyn_CONFIGFILE, True, False, False))
54                 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
55
56         /* Misc other stuff */
57
58         load_interfaces();
59         
60         initialised = True;
61 }
62
63 /* Debuglevel routines */
64
65 PyObject *get_debuglevel(PyObject *self, PyObject *args)
66 {
67         PyObject *debuglevel;
68
69         if (!PyArg_ParseTuple(args, ""))
70                 return NULL;
71
72         debuglevel = PyInt_FromLong(DEBUGLEVEL);
73
74         return debuglevel;
75 }
76
77 PyObject *set_debuglevel(PyObject *self, PyObject *args)
78 {
79         int debuglevel;
80
81         if (!PyArg_ParseTuple(args, "i", &debuglevel))
82                 return NULL;
83
84         DEBUGLEVEL = debuglevel;
85
86         Py_INCREF(Py_None);
87         return Py_None;
88 }
89
90 /* Initialise logging */
91
92 PyObject *py_setup_logging(PyObject *self, PyObject *args, PyObject *kw)
93 {
94         BOOL interactive = False;
95         char *logfilename = NULL;
96         static char *kwlist[] = {"interactive", "logfilename", NULL};
97
98         if (!PyArg_ParseTupleAndKeywords(args, kw, "|is", kwlist,
99                                          &interactive, &logfilename))
100                 return NULL;
101         
102         if (interactive && logfilename) {
103                 PyErr_SetString(PyExc_RuntimeError,
104                                 "can't be interactive and set log file name");
105                 return NULL;
106         }
107
108         if (interactive)
109                 setup_logging("spoolss", True);
110
111         if (logfilename) {
112                 lp_set_logfile(logfilename);
113                 setup_logging(logfilename, False);
114                 reopen_logs();
115         }
116
117         Py_INCREF(Py_None);
118         return Py_None;
119 }
120
121 struct cli_state *open_pipe_creds(char *system_name, PyObject *creds, 
122                                   cli_pipe_fn *connect_fn,
123                                   struct cli_state *cli)
124 {
125         struct ntuser_creds nt_creds;
126
127         if (!cli) {
128                 cli = (struct cli_state *)malloc(sizeof(struct cli_state));
129                 if (!cli)
130                         return NULL;
131         }
132
133         ZERO_STRUCTP(cli);
134
135         /* Extract credentials from the python dictionary and initialise
136            the ntuser_creds struct from them. */
137
138         ZERO_STRUCT(nt_creds);
139         nt_creds.pwd.null_pwd = True;
140
141         if (creds && PyDict_Size(creds) > 0) {
142                 char *username, *password, *domain;
143                 PyObject *username_obj, *password_obj, *domain_obj;
144
145                 /* Check credentials passed are valid.  This means the
146                    username, domain and password keys must exist and be
147                    string objects. */
148
149                 username_obj = PyDict_GetItemString(creds, "username");
150                 domain_obj = PyDict_GetItemString(creds, "domain");
151                 password_obj = PyDict_GetItemString(creds, "password");
152
153                 if (!username_obj || !domain_obj || !password_obj) {
154                 error:
155
156                         /* TODO: Either pass in the exception for the
157                            module calling open_pipe_creds() or have a
158                            global samba python module exception. */
159
160                         PyErr_SetString(PyExc_RuntimeError, 
161                                         "invalid credentials");
162                         return NULL;
163                 }
164
165                 if (!PyString_Check(username_obj) || 
166                     !PyString_Check(domain_obj) || 
167                     !PyString_Check(password_obj))
168                         goto error;
169
170                 username = PyString_AsString(username_obj);
171                 domain = PyString_AsString(domain_obj);
172                 password = PyString_AsString(password_obj);
173
174                 if (!username || !domain || !password)
175                         goto error;
176
177                 /* Initialise nt_creds structure with passed creds */
178
179                 fstrcpy(nt_creds.user_name, username);
180                 fstrcpy(nt_creds.domain, domain);
181
182                 if (lp_encrypted_passwords())
183                         pwd_make_lm_nt_16(&nt_creds.pwd, password);
184                 else
185                         pwd_set_cleartext(&nt_creds.pwd, password);
186
187                 nt_creds.pwd.null_pwd = False;
188         }
189
190         /* Now try to connect */
191
192         if (!connect_fn(cli, system_name, &nt_creds)) {
193                 if (cli) {
194                         NTSTATUS error = cli_nt_error(cli);
195
196                         /* Raise an exception if something went wrong.
197                            FIXME: This should be a more appropriate
198                            exception than PyExc_RuntimeError */
199
200                         if (!NT_STATUS_IS_OK(error))
201                                 PyErr_SetObject(PyExc_RuntimeError,
202                                                 py_ntstatus_tuple(error));
203                         else
204                                 PyErr_SetString(PyExc_RuntimeError,
205                                                 "error connecting to pipe");
206                 }
207                 
208                 return NULL;
209         }
210
211         return cli;
212 }