import HEAD into svn+ssh://svn.samba.org/home/svn/samba/trunk
[metze/old/v3-2-winbind-ndr.git] / source / 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         BOOL result;
65
66         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|s", kwlist, &called_name, 
67                                          &calling_name))
68                 return NULL;
69
70         if (!calling_name)
71                 calling_name = global_myname();
72
73         make_nmb_name(&calling, calling_name, 0x00);
74         make_nmb_name(&called, called_name, 0x20);
75
76         result = cli_session_request(cli->cli, &calling, &called);
77
78         return Py_BuildValue("i", result);
79 }
80                                       
81 static PyObject *py_smb_negprot(PyObject *self, PyObject *args, PyObject *kw)
82 {
83         cli_state_object *cli = (cli_state_object *)self;
84         static char *kwlist[] = { NULL };
85         BOOL result;
86
87         if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
88                 return NULL;
89
90         result = cli_negprot(cli->cli);
91
92         return Py_BuildValue("i", result);
93 }
94
95 static PyObject *py_smb_session_setup(PyObject *self, PyObject *args, 
96                                       PyObject *kw)
97 {
98         cli_state_object *cli = (cli_state_object *)self;
99         static char *kwlist[] = { "creds", NULL };
100         PyObject *creds;
101         char *username, *domain, *password, *errstr;
102         BOOL result;
103
104         if (!PyArg_ParseTupleAndKeywords(args, kw, "|O", kwlist, &creds))
105                 return NULL;
106
107         if (!py_parse_creds(creds, &username, &domain, &password, &errstr)) {
108                 free(errstr);
109                 return NULL;
110         }
111
112         result = cli_session_setup(
113                 cli->cli, username, password, strlen(password) + 1,
114                 password, strlen(password) + 1, domain);
115
116         if (cli_is_error(cli->cli)) {
117                 PyErr_SetString(PyExc_RuntimeError, "session setup failed");
118                 return NULL;
119         }
120
121         return Py_BuildValue("i", result);
122 }
123
124 static PyObject *py_smb_tconx(PyObject *self, PyObject *args, PyObject *kw)
125 {
126         cli_state_object *cli = (cli_state_object *)self;
127         static char *kwlist[] = { "service", NULL };
128         char *service;
129         BOOL result;
130
131         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &service))
132                 return NULL;
133
134         result = cli_send_tconX(
135                 cli->cli, service, strequal(service, "IPC$") ? "IPC" : 
136                 "?????", "", 1);
137
138         if (cli_is_error(cli->cli)) {
139                 PyErr_SetString(PyExc_RuntimeError, "tconx failed");
140                 return NULL;
141         }
142
143         return Py_BuildValue("i", result);
144 }
145
146 static PyObject *py_smb_nt_create_andx(PyObject *self, PyObject *args,
147                                        PyObject *kw)
148 {
149         cli_state_object *cli = (cli_state_object *)self;
150         static char *kwlist[] = { "filename", "desired_access", 
151                                   "file_attributes", "share_access",
152                                   "create_disposition", NULL };
153         char *filename;
154         uint32 desired_access, file_attributes = 0, 
155                 share_access = FILE_SHARE_READ | FILE_SHARE_WRITE,
156                 create_disposition = FILE_EXISTS_OPEN, create_options = 0;
157         int result;
158
159         /* Parse parameters */
160
161         if (!PyArg_ParseTupleAndKeywords(
162                     args, kw, "si|iii", kwlist, &filename, &desired_access,
163                     &file_attributes, &share_access, &create_disposition,
164                     &create_options))
165                 return NULL;
166
167         result = cli_nt_create_full(
168                 cli->cli, filename, 0, desired_access, file_attributes,
169                 share_access, create_disposition, create_options, 0);
170
171         if (cli_is_error(cli->cli)) {
172                 PyErr_SetString(PyExc_RuntimeError, "nt_create_andx failed");
173                 return NULL;
174         }
175
176         /* Return FID */
177
178         return PyInt_FromLong(result);
179 }
180
181 static PyObject *py_smb_close(PyObject *self, PyObject *args,
182                               PyObject *kw)
183 {
184         cli_state_object *cli = (cli_state_object *)self;
185         static char *kwlist[] = { "fnum", NULL };
186         BOOL result;
187         int fnum;
188
189         /* Parse parameters */
190
191         if (!PyArg_ParseTupleAndKeywords(
192                     args, kw, "i", kwlist, &fnum))
193                 return NULL;
194
195         result = cli_close(cli->cli, fnum);
196
197         return PyInt_FromLong(result);
198 }
199
200 static PyObject *py_smb_unlink(PyObject *self, PyObject *args,
201                                PyObject *kw)
202 {
203         cli_state_object *cli = (cli_state_object *)self;
204         static char *kwlist[] = { "filename", NULL };
205         char *filename;
206         BOOL result;
207
208         /* Parse parameters */
209
210         if (!PyArg_ParseTupleAndKeywords(
211                     args, kw, "s", kwlist, &filename))
212                 return NULL;
213
214         result = cli_unlink(cli->cli, filename);
215
216         return PyInt_FromLong(result);
217 }
218
219 static PyObject *py_smb_query_secdesc(PyObject *self, PyObject *args,
220                                       PyObject *kw)
221 {
222         cli_state_object *cli = (cli_state_object *)self;
223         static char *kwlist[] = { "fnum", NULL };
224         PyObject *result = NULL;
225         SEC_DESC *secdesc = NULL;
226         int fnum;
227         TALLOC_CTX *mem_ctx = NULL;
228
229         /* Parse parameters */
230
231         if (!PyArg_ParseTupleAndKeywords(
232                     args, kw, "i", kwlist, &fnum))
233                 return NULL;
234
235         mem_ctx = talloc_init("py_smb_query_secdesc");
236
237         secdesc = cli_query_secdesc(cli->cli, fnum, mem_ctx);
238
239         if (cli_is_error(cli->cli)) {
240                 PyErr_SetString(PyExc_RuntimeError, "query_secdesc failed");
241                 goto done;
242         }
243
244         if (!secdesc) {
245                 Py_INCREF(Py_None);
246                 result = Py_None;
247                 goto done;
248         }
249
250         if (!py_from_SECDESC(&result, secdesc)) {
251                 PyErr_SetString(
252                         PyExc_TypeError,
253                         "Invalid security descriptor returned");
254                 goto done;
255         }
256
257  done:
258         talloc_destroy(mem_ctx);
259
260         return result;
261         
262 }
263
264 static PyObject *py_smb_set_secdesc(PyObject *self, PyObject *args,
265                                     PyObject *kw)
266 {
267         cli_state_object *cli = (cli_state_object *)self;
268         static char *kwlist[] = { "fnum", "security_descriptor", NULL };
269         PyObject *result = NULL;
270         PyObject *py_secdesc;
271         SEC_DESC *secdesc;
272         TALLOC_CTX *mem_ctx = NULL;
273         int fnum;
274         BOOL err;
275
276         /* Parse parameters */
277
278         if (!PyArg_ParseTupleAndKeywords(
279                     args, kw, "iO", kwlist, &fnum, &py_secdesc))
280                 return NULL;
281
282         mem_ctx = talloc_init("py_smb_set_secdesc");
283
284         if (!py_to_SECDESC(&secdesc, py_secdesc, mem_ctx)) {
285                 PyErr_SetString(PyExc_TypeError, 
286                                 "Invalid security descriptor");
287                 goto done;
288         }
289
290         err = cli_set_secdesc(cli->cli, fnum, secdesc);
291
292         if (cli_is_error(cli->cli)) {
293                 PyErr_SetString(PyExc_RuntimeError, "set_secdesc failed");
294                 goto done;
295         }
296
297         result =  PyInt_FromLong(err);
298  done:
299         talloc_destroy(mem_ctx);
300
301         return result;
302 }
303
304 static PyMethodDef smb_hnd_methods[] = {
305
306         /* Session and connection handling */
307
308         { "session_request", (PyCFunction)py_smb_session_request, 
309           METH_VARARGS | METH_KEYWORDS, "Request a session" },
310
311         { "negprot", (PyCFunction)py_smb_negprot, 
312           METH_VARARGS | METH_KEYWORDS, "Protocol negotiation" },
313
314         { "session_setup", (PyCFunction)py_smb_session_setup,
315           METH_VARARGS | METH_KEYWORDS, "Session setup" },
316
317         { "tconx", (PyCFunction)py_smb_tconx,
318           METH_VARARGS | METH_KEYWORDS, "Tree connect" },
319
320         /* File operations */
321
322         { "nt_create_andx", (PyCFunction)py_smb_nt_create_andx,
323           METH_VARARGS | METH_KEYWORDS, "NT Create&X" },
324
325         { "close", (PyCFunction)py_smb_close,
326           METH_VARARGS | METH_KEYWORDS, "Close" },
327
328         { "unlink", (PyCFunction)py_smb_unlink,
329           METH_VARARGS | METH_KEYWORDS, "Unlink" },
330
331         /* Security descriptors */
332
333         { "query_secdesc", (PyCFunction)py_smb_query_secdesc,
334           METH_VARARGS | METH_KEYWORDS, "Query security descriptor" },
335
336         { "set_secdesc", (PyCFunction)py_smb_set_secdesc,
337           METH_VARARGS | METH_KEYWORDS, "Set security descriptor" },
338
339         { NULL }
340 };
341
342 /*
343  * Method dispatch tables
344  */
345
346 static PyMethodDef smb_methods[] = {
347
348         { "connect", (PyCFunction)py_smb_connect, METH_VARARGS | METH_KEYWORDS,
349           "Connect to a host" },
350
351         /* Other stuff - this should really go into a samba config module
352            but for the moment let's leave it here. */
353
354         { "setup_logging", (PyCFunction)py_setup_logging, 
355           METH_VARARGS | METH_KEYWORDS, 
356           "Set up debug logging.\n"
357 "\n"
358 "Initialises Samba's debug logging system.  One argument is expected which\n"
359 "is a boolean specifying whether debugging is interactive and sent to stdout\n"
360 "or logged to a file.\n"
361 "\n"
362 "Example:\n"
363 "\n"
364 ">>> smb.setup_logging(interactive = 1)" },
365
366         { "get_debuglevel", (PyCFunction)get_debuglevel, 
367           METH_VARARGS, 
368           "Set the current debug level.\n"
369 "\n"
370 "Example:\n"
371 "\n"
372 ">>> smb.get_debuglevel()\n"
373 "0" },
374
375         { "set_debuglevel", (PyCFunction)set_debuglevel, 
376           METH_VARARGS, 
377           "Get the current debug level.\n"
378 "\n"
379 "Example:\n"
380 "\n"
381 ">>> smb.set_debuglevel(10)" },
382
383         { NULL }
384 };
385
386 static void py_cli_state_dealloc(PyObject* self)
387 {
388         cli_state_object *cli = (cli_state_object *)self;
389
390         if (cli->cli)
391                 cli_shutdown(cli->cli);
392
393         PyObject_Del(self);
394 }
395
396 static PyObject *py_cli_state_getattr(PyObject *self, char *attrname)
397 {
398         return Py_FindMethod(smb_hnd_methods, self, attrname);
399 }
400
401 PyTypeObject cli_state_type = {
402         PyObject_HEAD_INIT(NULL)
403         0,
404         "SMB client connection",
405         sizeof(cli_state_object),
406         0,
407         py_cli_state_dealloc, /*tp_dealloc*/
408         0,          /*tp_print*/
409         py_cli_state_getattr,          /*tp_getattr*/
410         0,          /*tp_setattr*/
411         0,          /*tp_compare*/
412         0,          /*tp_repr*/
413         0,          /*tp_as_number*/
414         0,          /*tp_as_sequence*/
415         0,          /*tp_as_mapping*/
416         0,          /*tp_hash */
417 };
418
419 /*
420  * Module initialisation 
421  */
422
423 void initsmb(void)
424 {
425         PyObject *module, *dict;
426
427         /* Initialise module */
428
429         module = Py_InitModule("smb", smb_methods);
430         dict = PyModule_GetDict(module);
431
432         /* Initialise policy handle object */
433
434         cli_state_type.ob_type = &PyType_Type;
435
436         /* Do samba initialisation */
437
438         py_samba_init();
439
440         setup_logging("smb", True);
441         DEBUGLEVEL = 3;
442 }