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_proto.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 */
64 /* Debuglevel routines */
66 PyObject *get_debuglevel(PyObject *self, PyObject *args)
70 if (!PyArg_ParseTuple(args, ""))
73 debuglevel = PyInt_FromLong(DEBUGLEVEL);
78 PyObject *set_debuglevel(PyObject *self, PyObject *args)
82 if (!PyArg_ParseTuple(args, "i", &debuglevel))
85 DEBUGLEVEL = debuglevel;
91 /* Initialise logging */
93 PyObject *py_setup_logging(PyObject *self, PyObject *args, PyObject *kw)
95 BOOL interactive = False;
96 char *logfilename = NULL;
97 static char *kwlist[] = {"interactive", "logfilename", NULL};
99 if (!PyArg_ParseTupleAndKeywords(
100 args, kw, "|is", kwlist, &interactive, &logfilename))
103 if (interactive && logfilename) {
104 PyErr_SetString(PyExc_RuntimeError,
105 "can't be interactive and set log file name");
110 setup_logging("spoolss", True);
113 lp_set_logfile(logfilename);
114 setup_logging(logfilename, False);
122 /* Parse credentials from a python dictionary. The dictionary can
123 only have the keys "username", "domain" and "password". Return
124 True for valid credentials in which case the username, domain and
125 password are set to pointers to their values from the dicationary.
126 If returns False, the errstr is set to point at some mallocated
127 memory describing the error. */
129 BOOL py_parse_creds(PyObject *creds, char **username, char **domain,
130 char **password, char **errstr)
132 /* Initialise anonymous credentials */
138 if (creds && PyDict_Size(creds) > 0) {
139 PyObject *username_obj, *password_obj, *domain_obj;
140 PyObject *key, *value;
143 /* Check for presence of required fields */
145 username_obj = PyDict_GetItemString(creds, "username");
146 domain_obj = PyDict_GetItemString(creds, "domain");
147 password_obj = PyDict_GetItemString(creds, "password");
150 *errstr = strdup("no username field in credential");
155 *errstr = strdup("no domain field in credential");
160 *errstr = strdup("no password field in credential");
164 /* Check type of required fields */
166 if (!PyString_Check(username_obj)) {
167 *errstr = strdup("username field is not string type");
171 if (!PyString_Check(domain_obj)) {
172 *errstr = strdup("domain field is not string type");
176 if (!PyString_Check(password_obj)) {
177 *errstr = strdup("password field is not string type");
181 /* Look for any extra fields */
185 while (PyDict_Next(creds, &i, &key, &value)) {
186 if (strcmp(PyString_AsString(key), "domain") != 0 &&
187 strcmp(PyString_AsString(key), "username") != 0 &&
188 strcmp(PyString_AsString(key), "password") != 0) {
190 "creds contain extra field '%s'",
191 PyString_AsString(key));
198 *username = PyString_AsString(username_obj);
199 *domain = PyString_AsString(domain_obj);
200 *password = PyString_AsString(password_obj);
208 /* Return a cli_state to a RPC pipe on the given server. Use the
209 credentials passed if not NULL. If an error occurs errstr is set to a
210 string describing the error and NULL is returned. If set, errstr must
211 be freed by calling free(). */
213 struct cli_state *open_pipe_creds(char *server, PyObject *creds,
214 int pipe_idx, char **errstr)
216 char *username, *password, *domain;
217 struct cli_state *cli;
220 /* Extract credentials from the python dictionary */
222 if (!py_parse_creds(creds, &username, &domain, &password, errstr))
225 /* Now try to connect */
227 result = cli_full_connection(
228 &cli, NULL, server, NULL, 0, "IPC$", "IPC",
229 username, domain, password, 0, NULL);
231 if (!NT_STATUS_IS_OK(result)) {
232 *errstr = strdup("error connecting to IPC$ pipe");
236 if (!cli_nt_session_open(cli, pipe_idx)) {
238 asprintf(errstr, "error opening pipe index %d", pipe_idx);
247 /* Return true if a dictionary contains a "level" key with an integer
248 value. Set the value if so. */
250 BOOL get_level_value(PyObject *dict, uint32 *level)
254 if (!(obj = PyDict_GetItemString(dict, "level")) ||
259 *level = PyInt_AsLong(obj);