Added a utility function to extract the info key from a dictionary.
[tprouty/samba.git] / source / 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(
99                     args, kw, "|is", kwlist, &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 /* Return a cli_state to a RPC pipe on the given server.  Use the
122    credentials passed if not NULL.  If an error occurs errstr is set to a
123    string describing the error and NULL is returned.  If set, errstr must
124    be freed by calling free(). */
125
126 struct cli_state *open_pipe_creds(char *server, PyObject *creds, 
127                                   cli_pipe_fn *connect_fn, char **errstr)
128 {
129         struct ntuser_creds nt_creds;
130         struct cli_state *cli;
131         
132         cli = (struct cli_state *)malloc(sizeof(struct cli_state));
133         if (!cli) {
134                 *errstr = strdup("out of memory");
135                 return NULL;
136         }
137
138         ZERO_STRUCTP(cli);
139
140         /* Extract credentials from the python dictionary and initialise
141            the ntuser_creds struct from them. */
142
143         ZERO_STRUCT(nt_creds);
144         nt_creds.pwd.null_pwd = True;
145
146         if (creds && PyDict_Size(creds) > 0) {
147                 char *username, *password, *domain;
148                 PyObject *username_obj, *password_obj, *domain_obj;
149
150                 /* Check credentials passed are valid.  This means the
151                    username, domain and password keys must exist and be
152                    string objects. */
153
154                 username_obj = PyDict_GetItemString(creds, "username");
155                 domain_obj = PyDict_GetItemString(creds, "domain");
156                 password_obj = PyDict_GetItemString(creds, "password");
157
158                 if (!username_obj || !domain_obj || !password_obj) {
159                 creds_error:
160                         *errstr = strdup("invalid credentials");
161                         return NULL;
162                 }
163
164                 if (!PyString_Check(username_obj) || 
165                     !PyString_Check(domain_obj) || 
166                     !PyString_Check(password_obj))
167                         goto creds_error;
168
169                 username = PyString_AsString(username_obj);
170                 domain = PyString_AsString(domain_obj);
171                 password = PyString_AsString(password_obj);
172
173                 if (!username || !domain || !password)
174                         goto creds_error;
175
176                 /* Initialise nt_creds structure with passed creds */
177
178                 fstrcpy(nt_creds.user_name, username);
179                 fstrcpy(nt_creds.domain, domain);
180
181                 if (lp_encrypted_passwords())
182                         pwd_make_lm_nt_16(&nt_creds.pwd, password);
183                 else
184                         pwd_set_cleartext(&nt_creds.pwd, password);
185
186                 nt_creds.pwd.null_pwd = False;
187         }
188
189         /* Now try to connect */
190
191         if (!connect_fn(cli, server, &nt_creds)) {
192                 *errstr = strdup("error connecting to RPC pipe");
193                 return NULL;
194         }
195
196         *errstr = NULL;
197
198         return cli;
199 }
200
201 /* Return true if a dictionary contains a "level" key with an integer
202    value.  Set the value if so. */
203
204 BOOL get_level_value(PyObject *dict, uint32 *level)
205 {
206         PyObject *obj;
207
208         if (!(obj = PyDict_GetItemString(dict, "level")) ||
209             !PyInt_Check(obj))
210                 return False;
211
212         if (level)
213                 *level = PyInt_AsLong(obj);
214
215         return True;
216 }