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;
66 if (!PyArg_ParseTupleAndKeywords(args, kw, "s|s", kwlist, &called_name,
71 calling_name = global_myname();
73 make_nmb_name(&calling, calling_name, 0x00);
74 make_nmb_name(&called, called_name, 0x20);
76 result = cli_session_request(cli->cli, &calling, &called);
78 return Py_BuildValue("i", result);
81 static PyObject *py_smb_negprot(PyObject *self, PyObject *args, PyObject *kw)
83 cli_state_object *cli = (cli_state_object *)self;
84 static char *kwlist[] = { NULL };
87 if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
90 result = cli_negprot(cli->cli);
92 return Py_BuildValue("i", result);
95 static PyObject *py_smb_session_setup(PyObject *self, PyObject *args,
98 cli_state_object *cli = (cli_state_object *)self;
99 static char *kwlist[] = { "creds", NULL };
101 char *username, *domain, *password, *errstr;
104 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O", kwlist, &creds))
107 if (!py_parse_creds(creds, &username, &domain, &password, &errstr)) {
112 result = cli_session_setup(
113 cli->cli, username, password, strlen(password) + 1,
114 password, strlen(password) + 1, domain);
116 if (cli_is_error(cli->cli)) {
117 PyErr_SetString(PyExc_RuntimeError, "session setup failed");
121 return Py_BuildValue("i", result);
124 static PyObject *py_smb_tconx(PyObject *self, PyObject *args, PyObject *kw)
126 cli_state_object *cli = (cli_state_object *)self;
127 static char *kwlist[] = { "service", NULL };
131 if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &service))
134 result = cli_send_tconX(
135 cli->cli, service, strequal(service, "IPC$") ? "IPC" :
138 if (cli_is_error(cli->cli)) {
139 PyErr_SetString(PyExc_RuntimeError, "tconx failed");
143 return Py_BuildValue("i", result);
146 static PyObject *py_smb_nt_create_andx(PyObject *self, PyObject *args,
149 cli_state_object *cli = (cli_state_object *)self;
150 static char *kwlist[] = { "filename", "desired_access",
151 "file_attributes", "share_access",
152 "create_disposition", "create_options",
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|iiii", kwlist, &filename, &desired_access,
164 &file_attributes, &share_access, &create_disposition,
168 result = cli_nt_create_full(
169 cli->cli, filename, 0, desired_access, file_attributes,
170 share_access, create_disposition, create_options, 0);
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 };
225 PyObject *result = NULL;
226 SEC_DESC *secdesc = NULL;
228 TALLOC_CTX *mem_ctx = NULL;
230 /* Parse parameters */
232 if (!PyArg_ParseTupleAndKeywords(
233 args, kw, "i", kwlist, &fnum))
236 mem_ctx = talloc_init("py_smb_query_secdesc");
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");
259 talloc_destroy(mem_ctx);
265 static PyObject *py_smb_set_secdesc(PyObject *self, PyObject *args,
268 cli_state_object *cli = (cli_state_object *)self;
269 static char *kwlist[] = { "fnum", "security_descriptor", NULL };
270 PyObject *result = NULL;
271 PyObject *py_secdesc;
273 TALLOC_CTX *mem_ctx = NULL;
277 /* Parse parameters */
279 if (!PyArg_ParseTupleAndKeywords(
280 args, kw, "iO", kwlist, &fnum, &py_secdesc))
283 mem_ctx = talloc_init("py_smb_set_secdesc");
285 if (!py_to_SECDESC(&secdesc, py_secdesc, mem_ctx)) {
286 PyErr_SetString(PyExc_TypeError,
287 "Invalid security descriptor");
291 err = cli_set_secdesc(cli->cli, fnum, secdesc);
293 if (cli_is_error(cli->cli)) {
294 PyErr_SetString(PyExc_RuntimeError, "set_secdesc failed");
298 result = PyInt_FromLong(err);
300 talloc_destroy(mem_ctx);
305 static PyMethodDef smb_hnd_methods[] = {
307 /* Session and connection handling */
309 { "session_request", (PyCFunction)py_smb_session_request,
310 METH_VARARGS | METH_KEYWORDS, "Request a session" },
312 { "negprot", (PyCFunction)py_smb_negprot,
313 METH_VARARGS | METH_KEYWORDS, "Protocol negotiation" },
315 { "session_setup", (PyCFunction)py_smb_session_setup,
316 METH_VARARGS | METH_KEYWORDS, "Session setup" },
318 { "tconx", (PyCFunction)py_smb_tconx,
319 METH_VARARGS | METH_KEYWORDS, "Tree connect" },
321 /* File operations */
323 { "nt_create_andx", (PyCFunction)py_smb_nt_create_andx,
324 METH_VARARGS | METH_KEYWORDS, "NT Create&X" },
326 { "close", (PyCFunction)py_smb_close,
327 METH_VARARGS | METH_KEYWORDS, "Close" },
329 { "unlink", (PyCFunction)py_smb_unlink,
330 METH_VARARGS | METH_KEYWORDS, "Unlink" },
332 /* Security descriptors */
334 { "query_secdesc", (PyCFunction)py_smb_query_secdesc,
335 METH_VARARGS | METH_KEYWORDS, "Query security descriptor" },
337 { "set_secdesc", (PyCFunction)py_smb_set_secdesc,
338 METH_VARARGS | METH_KEYWORDS, "Set security descriptor" },
344 * Method dispatch tables
347 static PyMethodDef smb_methods[] = {
349 { "connect", (PyCFunction)py_smb_connect, METH_VARARGS | METH_KEYWORDS,
350 "Connect to a host" },
352 /* Other stuff - this should really go into a samba config module
353 but for the moment let's leave it here. */
355 { "setup_logging", (PyCFunction)py_setup_logging,
356 METH_VARARGS | METH_KEYWORDS,
357 "Set up debug logging.\n"
359 "Initialises Samba's debug logging system. One argument is expected which\n"
360 "is a boolean specifying whether debugging is interactive and sent to stdout\n"
361 "or logged to a file.\n"
365 ">>> smb.setup_logging(interactive = 1)" },
367 { "get_debuglevel", (PyCFunction)get_debuglevel,
369 "Set the current debug level.\n"
373 ">>> smb.get_debuglevel()\n"
376 { "set_debuglevel", (PyCFunction)set_debuglevel,
378 "Get the current debug level.\n"
382 ">>> smb.set_debuglevel(10)" },
387 static void py_cli_state_dealloc(PyObject* self)
389 cli_state_object *cli = (cli_state_object *)self;
392 cli_shutdown(cli->cli);
397 static PyObject *py_cli_state_getattr(PyObject *self, char *attrname)
399 return Py_FindMethod(smb_hnd_methods, self, attrname);
402 PyTypeObject cli_state_type = {
403 PyObject_HEAD_INIT(NULL)
405 "SMB client connection",
406 sizeof(cli_state_object),
408 py_cli_state_dealloc, /*tp_dealloc*/
410 py_cli_state_getattr, /*tp_getattr*/
415 0, /*tp_as_sequence*/
421 * Module initialisation
426 PyObject *module, *dict;
428 /* Initialise module */
430 module = Py_InitModule("smb", smb_methods);
431 dict = PyModule_GetDict(module);
433 /* Initialise policy handle object */
435 cli_state_type.ob_type = &PyType_Type;
437 /* Do samba initialisation */
441 setup_logging("smb", True);