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", NULL };
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 if (cli_is_error(cli->cli)) {
118 PyErr_SetString(PyExc_RuntimeError, "session setup failed");
122 return Py_BuildValue("i", result);
125 static PyObject *py_smb_tconx(PyObject *self, PyObject *args, PyObject *kw)
127 cli_state_object *cli = (cli_state_object *)self;
128 static char *kwlist[] = { "service", NULL };
132 if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &service))
135 result = cli_send_tconX(
136 cli->cli, service, strequal(service, "IPC$") ? "IPC" :
139 if (cli_is_error(cli->cli)) {
140 PyErr_SetString(PyExc_RuntimeError, "tconx failed");
144 return Py_BuildValue("i", result);
147 static PyObject *py_smb_nt_create_andx(PyObject *self, PyObject *args,
150 cli_state_object *cli = (cli_state_object *)self;
151 static char *kwlist[] = { "filename", "desired_access",
152 "file_attributes", "share_access",
153 "create_disposition", NULL };
155 uint32 desired_access, file_attributes = 0,
156 share_access = FILE_SHARE_READ | FILE_SHARE_WRITE,
157 create_disposition = FILE_EXISTS_OPEN, create_options = 0;
160 /* Parse parameters */
162 if (!PyArg_ParseTupleAndKeywords(
163 args, kw, "si|iii", kwlist, &filename, &desired_access,
164 &file_attributes, &share_access, &create_disposition,
168 result = cli_nt_create_full(
169 cli->cli, filename, desired_access, file_attributes,
170 share_access, create_disposition, create_options);
172 if (cli_is_error(cli->cli)) {
173 PyErr_SetString(PyExc_RuntimeError, "nt_create_andx failed");
179 return PyInt_FromLong(result);
182 static PyObject *py_smb_close(PyObject *self, PyObject *args,
185 cli_state_object *cli = (cli_state_object *)self;
186 static char *kwlist[] = { "fnum", NULL };
190 /* Parse parameters */
192 if (!PyArg_ParseTupleAndKeywords(
193 args, kw, "i", kwlist, &fnum))
196 result = cli_close(cli->cli, fnum);
198 return PyInt_FromLong(result);
201 static PyObject *py_smb_unlink(PyObject *self, PyObject *args,
204 cli_state_object *cli = (cli_state_object *)self;
205 static char *kwlist[] = { "filename", NULL };
209 /* Parse parameters */
211 if (!PyArg_ParseTupleAndKeywords(
212 args, kw, "s", kwlist, &filename))
215 result = cli_unlink(cli->cli, filename);
217 return PyInt_FromLong(result);
220 static PyObject *py_smb_query_secdesc(PyObject *self, PyObject *args,
223 cli_state_object *cli = (cli_state_object *)self;
224 static char *kwlist[] = { "fnum", NULL };
226 SEC_DESC *secdesc = NULL;
230 /* Parse parameters */
232 if (!PyArg_ParseTupleAndKeywords(
233 args, kw, "i", kwlist, &fnum))
236 mem_ctx = talloc_init();
238 secdesc = cli_query_secdesc(cli->cli, fnum, mem_ctx);
240 if (cli_is_error(cli->cli)) {
241 PyErr_SetString(PyExc_RuntimeError, "query_secdesc failed");
251 if (!py_from_SECDESC(&result, secdesc)) {
254 "Invalid security descriptor returned");
260 talloc_destroy(mem_ctx);
266 static PyObject *py_smb_set_secdesc(PyObject *self, PyObject *args,
269 cli_state_object *cli = (cli_state_object *)self;
270 static char *kwlist[] = { "fnum", "security_descriptor", NULL };
271 PyObject *py_secdesc;
273 TALLOC_CTX *mem_ctx = talloc_init();
277 /* Parse parameters */
279 if (!PyArg_ParseTupleAndKeywords(
280 args, kw, "iO", kwlist, &fnum, &py_secdesc))
283 if (!py_to_SECDESC(&secdesc, py_secdesc, mem_ctx)) {
284 PyErr_SetString(PyExc_TypeError,
285 "Invalid security descriptor");
289 result = cli_set_secdesc(cli->cli, fnum, secdesc);
291 if (cli_is_error(cli->cli)) {
292 PyErr_SetString(PyExc_RuntimeError, "set_secdesc failed");
296 return PyInt_FromLong(result);
299 static PyMethodDef smb_hnd_methods[] = {
301 /* Session and connection handling */
303 { "session_request", (PyCFunction)py_smb_session_request,
304 METH_VARARGS | METH_KEYWORDS, "Request a session" },
306 { "negprot", (PyCFunction)py_smb_negprot,
307 METH_VARARGS | METH_KEYWORDS, "Protocol negotiation" },
309 { "session_setup", (PyCFunction)py_smb_session_setup,
310 METH_VARARGS | METH_KEYWORDS, "Session setup" },
312 { "tconx", (PyCFunction)py_smb_tconx,
313 METH_VARARGS | METH_KEYWORDS, "Tree connect" },
315 /* File operations */
317 { "nt_create_andx", (PyCFunction)py_smb_nt_create_andx,
318 METH_VARARGS | METH_KEYWORDS, "NT Create&X" },
320 { "close", (PyCFunction)py_smb_close,
321 METH_VARARGS | METH_KEYWORDS, "Close" },
323 { "unlink", (PyCFunction)py_smb_unlink,
324 METH_VARARGS | METH_KEYWORDS, "Unlink" },
326 /* Security descriptors */
328 { "query_secdesc", (PyCFunction)py_smb_query_secdesc,
329 METH_VARARGS | METH_KEYWORDS, "Query security descriptor" },
331 { "set_secdesc", (PyCFunction)py_smb_set_secdesc,
332 METH_VARARGS | METH_KEYWORDS, "Set security descriptor" },
338 * Method dispatch tables
341 static PyMethodDef smb_methods[] = {
343 { "connect", (PyCFunction)py_smb_connect, METH_VARARGS | METH_KEYWORDS,
344 "Connect to a host" },
349 static void py_cli_state_dealloc(PyObject* self)
354 static PyObject *py_cli_state_getattr(PyObject *self, char *attrname)
356 return Py_FindMethod(smb_hnd_methods, self, attrname);
359 PyTypeObject cli_state_type = {
360 PyObject_HEAD_INIT(NULL)
362 "SMB client connection",
363 sizeof(cli_state_object),
365 py_cli_state_dealloc, /*tp_dealloc*/
367 py_cli_state_getattr, /*tp_getattr*/
372 0, /*tp_as_sequence*/
378 * Module initialisation
383 PyObject *module, *dict;
385 /* Initialise module */
387 module = Py_InitModule("smb", smb_methods);
388 dict = PyModule_GetDict(module);
390 /* Initialise policy handle object */
392 cli_state_type.ob_type = &PyType_Type;
394 /* Do samba initialisation */
398 setup_logging("smb", True);