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)
48 extern pstring global_myname
;
54 /* Load configuration file */
56 if (!lp_load(dyn_CONFIGFILE
, True
, False
, False
))
57 fprintf(stderr
, "Can't load %s\n", dyn_CONFIGFILE
);
59 /* Misc other stuff */
63 fstrcpy(global_myname
, myhostname());
64 p
= strchr(global_myname
, '.');
71 /* Debuglevel routines */
73 PyObject
*get_debuglevel(PyObject
*self
, PyObject
*args
)
77 if (!PyArg_ParseTuple(args
, ""))
80 debuglevel
= PyInt_FromLong(DEBUGLEVEL
);
85 PyObject
*set_debuglevel(PyObject
*self
, PyObject
*args
)
89 if (!PyArg_ParseTuple(args
, "i", &debuglevel
))
92 DEBUGLEVEL
= debuglevel
;
98 /* Initialise logging */
100 PyObject
*py_setup_logging(PyObject
*self
, PyObject
*args
, PyObject
*kw
)
102 BOOL interactive
= False
;
103 char *logfilename
= NULL
;
104 static char *kwlist
[] = {"interactive", "logfilename", NULL
};
106 if (!PyArg_ParseTupleAndKeywords(
107 args
, kw
, "|is", kwlist
, &interactive
, &logfilename
))
110 if (interactive
&& logfilename
) {
111 PyErr_SetString(PyExc_RuntimeError
,
112 "can't be interactive and set log file name");
117 setup_logging("spoolss", True
);
120 lp_set_logfile(logfilename
);
121 setup_logging(logfilename
, False
);
129 /* Return a cli_state to a RPC pipe on the given server. Use the
130 credentials passed if not NULL. If an error occurs errstr is set to a
131 string describing the error and NULL is returned. If set, errstr must
132 be freed by calling free(). */
134 struct cli_state
*open_pipe_creds(char *server
, PyObject
*creds
,
135 char *pipe_name
, char **errstr
)
137 char *username
= "", *password
= "", *domain
= "";
138 struct cli_state
*cli
;
141 /* Extract credentials from the python dictionary */
143 if (creds
&& PyDict_Size(creds
) > 0) {
144 PyObject
*username_obj
, *password_obj
, *domain_obj
;
146 /* Check credentials passed are valid. This means the
147 username, domain and password keys must exist and be
150 username_obj
= PyDict_GetItemString(creds
, "username");
151 domain_obj
= PyDict_GetItemString(creds
, "domain");
152 password_obj
= PyDict_GetItemString(creds
, "password");
154 if (!username_obj
|| !domain_obj
|| !password_obj
) {
156 *errstr
= strdup("invalid credentials");
160 if (!PyString_Check(username_obj
) ||
161 !PyString_Check(domain_obj
) ||
162 !PyString_Check(password_obj
))
165 username
= PyString_AsString(username_obj
);
166 domain
= PyString_AsString(domain_obj
);
167 password
= PyString_AsString(password_obj
);
169 if (!username
|| !domain
|| !password
)
173 /* Now try to connect */
175 result
= cli_full_connection(
176 &cli
, NULL
, server
, NULL
, 0, "IPC$", "IPC",
177 username
, domain
, password
, 0);
179 if (!NT_STATUS_IS_OK(result
)) {
180 *errstr
= strdup("error connecting to IPC$ pipe");
184 if (!cli_nt_session_open(cli
, pipe_name
)) {
187 asprintf(errstr
, "error opening %s", pipe_name
);
196 /* Return true if a dictionary contains a "level" key with an integer
197 value. Set the value if so. */
199 BOOL
get_level_value(PyObject
*dict
, uint32
*level
)
203 if (!(obj
= PyDict_GetItemString(dict
, "level")) ||
208 *level
= PyInt_AsLong(obj
);