Give better error messages for TypeError, which will arise if e.g. you
[samba.git] / source3 / python / py_smb.c
1 /* 
2    Python wrappers for DCERPC/SMB client routines.
3
4    Copyright (C) Tim Potter, 2002
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "python/py_smb.h"
22
23 /* Create a new cli_state python object */
24
25 PyObject *new_cli_state_object(struct cli_state *cli)
26 {
27         cli_state_object *o;
28
29         o = PyObject_New(cli_state_object, &cli_state_type);
30
31         o->cli = cli;
32
33         return (PyObject*)o;
34 }
35
36 static PyObject *py_smb_connect(PyObject *self, PyObject *args, PyObject *kw)
37 {
38         static char *kwlist[] = { "server", NULL };
39         struct cli_state *cli;
40         char *server;
41         struct in_addr ip;
42
43         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &server))
44                 return NULL;
45
46         if (!(cli = cli_initialise(NULL)))
47                 return NULL;
48
49         ZERO_STRUCT(ip);
50
51         if (!cli_connect(cli, server, &ip))
52                 return NULL;
53
54         return new_cli_state_object(cli);
55 }
56
57 static PyObject *py_smb_session_request(PyObject *self, PyObject *args,
58                                         PyObject *kw)
59 {
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;
65         BOOL result;
66
67         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|s", kwlist, &called_name, 
68                                          &calling_name))
69                 return NULL;
70
71         if (!calling_name)
72                 calling_name = global_myname;
73
74         make_nmb_name(&calling, calling_name, 0x00);
75         make_nmb_name(&called, called_name, 0x20);
76
77         result = cli_session_request(cli->cli, &calling, &called);
78
79         return Py_BuildValue("i", result);
80 }
81                                       
82 static PyObject *py_smb_negprot(PyObject *self, PyObject *args, PyObject *kw)
83 {
84         cli_state_object *cli = (cli_state_object *)self;
85         static char *kwlist[] = { NULL };
86         BOOL result;
87
88         if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
89                 return NULL;
90
91         result = cli_negprot(cli->cli);
92
93         return Py_BuildValue("i", result);
94 }
95
96 static PyObject *py_smb_session_setup(PyObject *self, PyObject *args, 
97                                       PyObject *kw)
98 {
99         cli_state_object *cli = (cli_state_object *)self;
100         static char *kwlist[] = { "creds" };
101         PyObject *creds;
102         char *username, *domain, *password, *errstr;
103         BOOL result;
104
105         if (!PyArg_ParseTupleAndKeywords(args, kw, "O", kwlist, &creds))
106                 return NULL;
107
108         if (!py_parse_creds(creds, &username, &domain, &password, &errstr)) {
109                 free(errstr);
110                 return NULL;
111         }
112
113         result = cli_session_setup(
114                 cli->cli, username, password, strlen(password) + 1,
115                 password, strlen(password) + 1, domain);
116
117         return Py_BuildValue("i", result);
118 }
119
120 static PyObject *py_smb_tconx(PyObject *self, PyObject *args, PyObject *kw)
121 {
122         cli_state_object *cli = (cli_state_object *)self;
123         static char *kwlist[] = { "service", "creds" };
124         PyObject *creds;
125         char *service, *username, *domain, *password, *errstr;
126         BOOL result;
127
128         if (!PyArg_ParseTupleAndKeywords(args, kw, "sO", kwlist, &service, 
129                                          &creds))
130                 return NULL;
131
132         if (!py_parse_creds(creds, &username, &domain, &password, &errstr)) {
133                 free(errstr);
134                 return NULL;
135         }
136
137         result = cli_send_tconX(
138                 cli->cli, service, strequal(service, "IPC$") ? "IPC" : "?????", 
139                 password, strlen(password) + 1);
140
141         return Py_BuildValue("i", result);
142 }
143
144 static PyMethodDef smb_hnd_methods[] = {
145
146         { "session_request", (PyCFunction)py_smb_session_request, 
147           METH_VARARGS | METH_KEYWORDS, "Request a session" },
148
149         { "negprot", (PyCFunction)py_smb_negprot, 
150           METH_VARARGS | METH_KEYWORDS, "Protocol negotiation" },
151
152         { "session_setup", (PyCFunction)py_smb_session_setup,
153           METH_VARARGS | METH_KEYWORDS, "Session setup" },
154
155         { "tconx", (PyCFunction)py_smb_tconx,
156           METH_VARARGS | METH_KEYWORDS, "Tree connect" },
157
158         { NULL }
159 };
160
161 /*
162  * Method dispatch tables
163  */
164
165 static PyMethodDef smb_methods[] = {
166
167         { "connect", (PyCFunction)py_smb_connect, METH_VARARGS | METH_KEYWORDS,
168           "Connect to a host" },
169
170         { NULL }
171 };
172
173 static void py_cli_state_dealloc(PyObject* self)
174 {
175         PyObject_Del(self);
176 }
177
178 static PyObject *py_cli_state_getattr(PyObject *self, char *attrname)
179 {
180         return Py_FindMethod(smb_hnd_methods, self, attrname);
181 }
182
183 PyTypeObject cli_state_type = {
184         PyObject_HEAD_INIT(NULL)
185         0,
186         "SMB client connection",
187         sizeof(cli_state_object),
188         0,
189         py_cli_state_dealloc, /*tp_dealloc*/
190         0,          /*tp_print*/
191         py_cli_state_getattr,          /*tp_getattr*/
192         0,          /*tp_setattr*/
193         0,          /*tp_compare*/
194         0,          /*tp_repr*/
195         0,          /*tp_as_number*/
196         0,          /*tp_as_sequence*/
197         0,          /*tp_as_mapping*/
198         0,          /*tp_hash */
199 };
200
201 /*
202  * Module initialisation 
203  */
204
205 void initsmb(void)
206 {
207         PyObject *module, *dict;
208
209         /* Initialise module */
210
211         module = Py_InitModule("smb", smb_methods);
212         dict = PyModule_GetDict(module);
213
214         /* Initialise policy handle object */
215
216         cli_state_type.ob_type = &PyType_Type;
217
218         /* Do samba initialisation */
219
220         py_samba_init();
221
222         setup_logging("smb", True);
223         DEBUGLEVEL = 10;
224 }