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", "creds" };
125 char *service, *username, *domain, *password, *errstr;
128 if (!PyArg_ParseTupleAndKeywords(args, kw, "sO", kwlist, &service,
132 if (!py_parse_creds(creds, &username, &domain, &password, &errstr)) {
137 result = cli_send_tconX(
138 cli->cli, service, strequal(service, "IPC$") ? "IPC" : "?????",
139 password, strlen(password) + 1);
141 return Py_BuildValue("i", result);
144 static PyMethodDef smb_hnd_methods[] = {
146 { "session_request", (PyCFunction)py_smb_session_request,
147 METH_VARARGS | METH_KEYWORDS, "Request a session" },
149 { "negprot", (PyCFunction)py_smb_negprot,
150 METH_VARARGS | METH_KEYWORDS, "Protocol negotiation" },
152 { "session_setup", (PyCFunction)py_smb_session_setup,
153 METH_VARARGS | METH_KEYWORDS, "Session setup" },
155 { "tconx", (PyCFunction)py_smb_tconx,
156 METH_VARARGS | METH_KEYWORDS, "Tree connect" },
162 * Method dispatch tables
165 static PyMethodDef smb_methods[] = {
167 { "connect", (PyCFunction)py_smb_connect, METH_VARARGS | METH_KEYWORDS,
168 "Connect to a host" },
173 static void py_cli_state_dealloc(PyObject* self)
178 static PyObject *py_cli_state_getattr(PyObject *self, char *attrname)
180 return Py_FindMethod(smb_hnd_methods, self, attrname);
183 PyTypeObject cli_state_type = {
184 PyObject_HEAD_INIT(NULL)
186 "SMB client connection",
187 sizeof(cli_state_object),
189 py_cli_state_dealloc, /*tp_dealloc*/
191 py_cli_state_getattr, /*tp_getattr*/
196 0, /*tp_as_sequence*/
202 * Module initialisation
207 PyObject *module, *dict;
209 /* Initialise module */
211 module = Py_InitModule("smb", smb_methods);
212 dict = PyModule_GetDict(module);
214 /* Initialise policy handle object */
216 cli_state_type.ob_type = &PyType_Type;
218 /* Do samba initialisation */
222 setup_logging("smb", True);