2 Python wrappers for DCERPC/SMB client routines.
4 Copyright (C) Tim Potter, 2002
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.
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.
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.
24 #include "python/py_common.h"
26 /* Return a tuple of (error code, error string) from a WERROR */
28 PyObject *py_werror_tuple(WERROR werror)
30 return Py_BuildValue("[is]", W_ERROR_V(werror),
34 /* Return a tuple of (error code, error string) from a WERROR */
36 PyObject *py_ntstatus_tuple(NTSTATUS ntstatus)
38 return Py_BuildValue("[is]", NT_STATUS_V(ntstatus),
42 /* Initialise samba client routines */
44 static BOOL initialised;
46 void py_samba_init(void)
51 /* Load configuration file */
53 if (!lp_load(dyn_CONFIGFILE, True, False, False))
54 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
56 /* Misc other stuff */
63 /* Debuglevel routines */
65 PyObject *get_debuglevel(PyObject *self, PyObject *args)
69 if (!PyArg_ParseTuple(args, ""))
72 debuglevel = PyInt_FromLong(DEBUGLEVEL);
77 PyObject *set_debuglevel(PyObject *self, PyObject *args)
81 if (!PyArg_ParseTuple(args, "i", &debuglevel))
84 DEBUGLEVEL = debuglevel;
90 /* Initialise logging */
92 PyObject *py_setup_logging(PyObject *self, PyObject *args, PyObject *kw)
94 BOOL interactive = False;
95 char *logfilename = NULL;
96 static char *kwlist[] = {"interactive", "logfilename", NULL};
98 if (!PyArg_ParseTupleAndKeywords(args, kw, "|is", kwlist,
99 &interactive, &logfilename))
102 if (interactive && logfilename) {
103 PyErr_SetString(PyExc_RuntimeError,
104 "can't be interactive and set log file name");
109 setup_logging("spoolss", True);
112 lp_set_logfile(logfilename);
113 setup_logging(logfilename, False);
121 struct cli_state *open_pipe_creds(char *system_name, PyObject *creds,
122 cli_pipe_fn *connect_fn,
123 struct cli_state *cli)
125 struct ntuser_creds nt_creds;
128 cli = (struct cli_state *)malloc(sizeof(struct cli_state));
135 /* Extract credentials from the python dictionary and initialise
136 the ntuser_creds struct from them. */
138 ZERO_STRUCT(nt_creds);
139 nt_creds.pwd.null_pwd = True;
141 if (creds && PyDict_Size(creds) > 0) {
142 char *username, *password, *domain;
143 PyObject *username_obj, *password_obj, *domain_obj;
145 /* Check credentials passed are valid. This means the
146 username, domain and password keys must exist and be
149 username_obj = PyDict_GetItemString(creds, "username");
150 domain_obj = PyDict_GetItemString(creds, "domain");
151 password_obj = PyDict_GetItemString(creds, "password");
153 if (!username_obj || !domain_obj || !password_obj) {
156 /* TODO: Either pass in the exception for the
157 module calling open_pipe_creds() or have a
158 global samba python module exception. */
160 PyErr_SetString(PyExc_RuntimeError,
161 "invalid credentials");
165 if (!PyString_Check(username_obj) ||
166 !PyString_Check(domain_obj) ||
167 !PyString_Check(password_obj))
170 username = PyString_AsString(username_obj);
171 domain = PyString_AsString(domain_obj);
172 password = PyString_AsString(password_obj);
174 if (!username || !domain || !password)
177 /* Initialise nt_creds structure with passed creds */
179 fstrcpy(nt_creds.user_name, username);
180 fstrcpy(nt_creds.domain, domain);
182 if (lp_encrypted_passwords())
183 pwd_make_lm_nt_16(&nt_creds.pwd, password);
185 pwd_set_cleartext(&nt_creds.pwd, password);
187 nt_creds.pwd.null_pwd = False;
190 /* Now try to connect */
192 if (!connect_fn(cli, system_name, &nt_creds)) {
194 NTSTATUS error = cli_nt_error(cli);
196 /* Raise an exception if something went wrong.
197 FIXME: This should be a more appropriate
198 exception than PyExc_RuntimeError */
200 if (!NT_STATUS_IS_OK(error))
201 PyErr_SetObject(PyExc_RuntimeError,
202 py_ntstatus_tuple(error));
204 PyErr_SetString(PyExc_RuntimeError,
205 "error connecting to pipe");