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.
21 #include "python/py_smb.h"
23 /* Create a new cli_state python object */
25 PyObject *new_cli_state_object(struct cli_state *cli)
29 o = PyObject_New(cli_state_object, &cli_state_type);
36 static PyObject *py_smb_connect(PyObject *self, PyObject *args, PyObject *kw)
38 static char *kwlist[] = { "server", NULL };
39 struct cli_state *cli;
43 if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &server))
46 if (!(cli = cli_initialise(NULL)))
51 if (!cli_connect(cli, server, &ip))
54 return new_cli_state_object(cli);
57 static PyObject *py_smb_session_request(PyObject *self, PyObject *args,
60 cli_state_object *cli = (cli_state_object *)self;
61 static char *kwlist[] = { "called", "calling", NULL };
62 char *calling_name = NULL, *called_name;
63 struct nmb_name calling, called;
64 extern pstring global_myname;
67 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|s", kwlist, &called_name,
72 calling_name = global_myname;
74 make_nmb_name(&calling, calling_name, 0x00);
75 make_nmb_name(&called, called_name, 0x20);
77 result = cli_session_request(cli->cli, &calling, &called);
79 return Py_BuildValue("i", result);
82 static PyObject *py_smb_negprot(PyObject *self, PyObject *args, PyObject *kw)
84 cli_state_object *cli = (cli_state_object *)self;
85 static char *kwlist[] = { NULL };
88 if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
91 result = cli_negprot(cli->cli);
93 return Py_BuildValue("i", result);
96 static PyObject *py_smb_session_setup(PyObject *self, PyObject *args,
99 cli_state_object *cli = (cli_state_object *)self;
100 static char *kwlist[] = { "creds" };
102 char *username, *domain, *password, *errstr;
105 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O", kwlist, &creds))
108 if (!py_parse_creds(creds, &username, &domain, &password, &errstr)) {
113 result = cli_session_setup(
114 cli->cli, username, password, strlen(password) + 1,
115 password, strlen(password) + 1, domain);
117 return Py_BuildValue("i", result);
120 static PyObject *py_smb_tconx(PyObject *self, PyObject *args, PyObject *kw)
122 cli_state_object *cli = (cli_state_object *)self;
123 static char *kwlist[] = { "service", NULL };
127 if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &service))
130 result = cli_send_tconX(
131 cli->cli, service, strequal(service, "IPC$") ? "IPC" :
134 return Py_BuildValue("i", result);
137 static PyObject *py_smb_nt_create_andx(PyObject *self, PyObject *args,
140 cli_state_object *cli = (cli_state_object *)self;
141 static char *kwlist[] = { "filename", "desired_access",
142 "file_attributes", "share_access",
143 "create_disposition", NULL };
145 uint32 desired_access, file_attributes = 0,
146 share_access = FILE_SHARE_READ | FILE_SHARE_WRITE,
147 create_disposition = FILE_EXISTS_OPEN, create_options = 0;
150 /* Parse parameters */
152 if (!PyArg_ParseTupleAndKeywords(
153 args, kw, "si|iiii", kwlist, &filename, &desired_access,
154 &file_attributes, &share_access, &create_disposition,
158 result = cli_nt_create_full(
159 cli->cli, filename, desired_access, file_attributes,
160 share_access, create_disposition, create_options);
164 return PyInt_FromLong(result);
167 static PyObject *py_smb_query_secdesc(PyObject *self, PyObject *args,
170 cli_state_object *cli = (cli_state_object *)self;
171 static char *kwlist[] = { "fnum", NULL };
173 SEC_DESC *secdesc = NULL;
177 /* Parse parameters */
179 if (!PyArg_ParseTupleAndKeywords(
180 args, kw, "i", kwlist, &fnum))
183 mem_ctx = talloc_init();
185 secdesc = cli_query_secdesc(cli->cli, fnum, mem_ctx);
187 /* FIXME: we should raise an exception here */
195 if (!py_from_SECDESC(&result, secdesc)) {
198 "Invalid security descriptor returned");
204 talloc_destroy(mem_ctx);
210 static PyObject *py_smb_set_secdesc(PyObject *self, PyObject *args,
213 cli_state_object *cli = (cli_state_object *)self;
214 static char *kwlist[] = { "fnum", "security_descriptor", NULL };
215 PyObject *py_secdesc;
217 TALLOC_CTX *mem_ctx = talloc_init();
221 /* Parse parameters */
223 if (!PyArg_ParseTupleAndKeywords(
224 args, kw, "iO", kwlist, &fnum, &py_secdesc))
227 if (!py_to_SECDESC(&secdesc, py_secdesc, mem_ctx)) {
228 PyErr_SetString(PyExc_TypeError,
229 "Invalid security descriptor");
233 result = cli_set_secdesc(cli->cli, fnum, secdesc);
235 return PyInt_FromLong(result);
238 static PyMethodDef smb_hnd_methods[] = {
240 /* Session and connection handling */
242 { "session_request", (PyCFunction)py_smb_session_request,
243 METH_VARARGS | METH_KEYWORDS, "Request a session" },
245 { "negprot", (PyCFunction)py_smb_negprot,
246 METH_VARARGS | METH_KEYWORDS, "Protocol negotiation" },
248 { "session_setup", (PyCFunction)py_smb_session_setup,
249 METH_VARARGS | METH_KEYWORDS, "Session setup" },
251 { "tconx", (PyCFunction)py_smb_tconx,
252 METH_VARARGS | METH_KEYWORDS, "Tree connect" },
254 /* File operations */
256 { "nt_create_andx", (PyCFunction)py_smb_nt_create_andx,
257 METH_VARARGS | METH_KEYWORDS, "NT Create&X" },
259 /* Security descriptors */
261 { "query_secdesc", (PyCFunction)py_smb_query_secdesc,
262 METH_VARARGS | METH_KEYWORDS, "Query security descriptor" },
264 { "set_secdesc", (PyCFunction)py_smb_set_secdesc,
265 METH_VARARGS | METH_KEYWORDS, "Set security descriptor" },
271 * Method dispatch tables
274 static PyMethodDef smb_methods[] = {
276 { "connect", (PyCFunction)py_smb_connect, METH_VARARGS | METH_KEYWORDS,
277 "Connect to a host" },
282 static void py_cli_state_dealloc(PyObject* self)
287 static PyObject *py_cli_state_getattr(PyObject *self, char *attrname)
289 return Py_FindMethod(smb_hnd_methods, self, attrname);
292 PyTypeObject cli_state_type = {
293 PyObject_HEAD_INIT(NULL)
295 "SMB client connection",
296 sizeof(cli_state_object),
298 py_cli_state_dealloc, /*tp_dealloc*/
300 py_cli_state_getattr, /*tp_getattr*/
305 0, /*tp_as_sequence*/
311 * Module initialisation
316 PyObject *module, *dict;
318 /* Initialise module */
320 module = Py_InitModule("smb", smb_methods);
321 dict = PyModule_GetDict(module);
323 /* Initialise policy handle object */
325 cli_state_type.ob_type = &PyType_Type;
327 /* Do samba initialisation */
331 setup_logging("smb", True);