Added close and unlink functions.
[samba.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         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", NULL };
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         if (cli_is_error(cli->cli)) {
118                 PyErr_SetString(PyExc_RuntimeError, "session setup failed");
119                 return NULL;
120         }
121
122         return Py_BuildValue("i", result);
123 }
124
125 static PyObject *py_smb_tconx(PyObject *self, PyObject *args, PyObject *kw)
126 {
127         cli_state_object *cli = (cli_state_object *)self;
128         static char *kwlist[] = { "service", NULL };
129         char *service;
130         BOOL result;
131
132         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &service))
133                 return NULL;
134
135         result = cli_send_tconX(
136                 cli->cli, service, strequal(service, "IPC$") ? "IPC" : 
137                 "?????", "", 1);
138
139         if (cli_is_error(cli->cli)) {
140                 PyErr_SetString(PyExc_RuntimeError, "tconx failed");
141                 return NULL;
142         }
143
144         return Py_BuildValue("i", result);
145 }
146
147 static PyObject *py_smb_nt_create_andx(PyObject *self, PyObject *args,
148                                        PyObject *kw)
149 {
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 };
154         char *filename;
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;
158         int result;
159
160         /* Parse parameters */
161
162         if (!PyArg_ParseTupleAndKeywords(
163                     args, kw, "si|iiii", kwlist, &filename, &desired_access,
164                     &file_attributes, &share_access, &create_disposition,
165                     &create_options))
166                 return NULL;
167
168         result = cli_nt_create_full(
169                 cli->cli, filename, desired_access, file_attributes,
170                 share_access, create_disposition, create_options);
171
172         if (cli_is_error(cli->cli)) {
173                 PyErr_SetString(PyExc_RuntimeError, "nt_create_andx failed");
174                 return NULL;
175         }
176
177         /* Return FID */
178
179         return PyInt_FromLong(result);
180 }
181
182 static PyObject *py_smb_close(PyObject *self, PyObject *args,
183                               PyObject *kw)
184 {
185         cli_state_object *cli = (cli_state_object *)self;
186         static char *kwlist[] = { "fnum", NULL };
187         BOOL result;
188         int fnum;
189
190         /* Parse parameters */
191
192         if (!PyArg_ParseTupleAndKeywords(
193                     args, kw, "i", kwlist, &fnum))
194                 return NULL;
195
196         result = cli_close(cli->cli, fnum);
197
198         return PyInt_FromLong(result);
199 }
200
201 static PyObject *py_smb_unlink(PyObject *self, PyObject *args,
202                                PyObject *kw)
203 {
204         cli_state_object *cli = (cli_state_object *)self;
205         static char *kwlist[] = { "filename", NULL };
206         char *filename;
207         BOOL result;
208
209         /* Parse parameters */
210
211         if (!PyArg_ParseTupleAndKeywords(
212                     args, kw, "s", kwlist, &filename))
213                 return NULL;
214
215         result = cli_unlink(cli->cli, filename);
216
217         return PyInt_FromLong(result);
218 }
219
220 static PyObject *py_smb_query_secdesc(PyObject *self, PyObject *args,
221                                       PyObject *kw)
222 {
223         cli_state_object *cli = (cli_state_object *)self;
224         static char *kwlist[] = { "fnum", NULL };
225         PyObject *result;
226         SEC_DESC *secdesc = NULL;
227         int fnum;
228         TALLOC_CTX *mem_ctx;
229
230         /* Parse parameters */
231
232         if (!PyArg_ParseTupleAndKeywords(
233                     args, kw, "i", kwlist, &fnum))
234                 return NULL;
235
236         mem_ctx = talloc_init();
237
238         secdesc = cli_query_secdesc(cli->cli, fnum, mem_ctx);
239
240         if (cli_is_error(cli->cli)) {
241                 PyErr_SetString(PyExc_RuntimeError, "query_secdesc failed");
242                 return NULL;
243         }
244
245         if (!secdesc) {
246                 Py_INCREF(Py_None);
247                 result = Py_None;
248                 goto done;
249         }
250
251         if (!py_from_SECDESC(&result, secdesc)) {
252                 PyErr_SetString(
253                         PyExc_TypeError,
254                         "Invalid security descriptor returned");
255                 result = NULL;
256                 goto done;
257         }
258
259  done:
260         talloc_destroy(mem_ctx);
261
262         return result;
263         
264 }
265
266 static PyObject *py_smb_set_secdesc(PyObject *self, PyObject *args,
267                                     PyObject *kw)
268 {
269         cli_state_object *cli = (cli_state_object *)self;
270         static char *kwlist[] = { "fnum", "security_descriptor", NULL };
271         PyObject *py_secdesc;
272         SEC_DESC *secdesc;
273         TALLOC_CTX *mem_ctx = talloc_init();
274         int fnum;
275         BOOL result;
276
277         /* Parse parameters */
278
279         if (!PyArg_ParseTupleAndKeywords(
280                     args, kw, "iO", kwlist, &fnum, &py_secdesc))
281                 return NULL;
282
283         if (!py_to_SECDESC(&secdesc, py_secdesc, mem_ctx)) {
284                 PyErr_SetString(PyExc_TypeError, 
285                                 "Invalid security descriptor");
286                 return NULL;
287         }
288
289         result = cli_set_secdesc(cli->cli, fnum, secdesc);
290
291         if (cli_is_error(cli->cli)) {
292                 PyErr_SetString(PyExc_RuntimeError, "set_secdesc failed");
293                 return NULL;
294         }
295
296         return PyInt_FromLong(result);
297 }
298
299 static PyMethodDef smb_hnd_methods[] = {
300
301         /* Session and connection handling */
302
303         { "session_request", (PyCFunction)py_smb_session_request, 
304           METH_VARARGS | METH_KEYWORDS, "Request a session" },
305
306         { "negprot", (PyCFunction)py_smb_negprot, 
307           METH_VARARGS | METH_KEYWORDS, "Protocol negotiation" },
308
309         { "session_setup", (PyCFunction)py_smb_session_setup,
310           METH_VARARGS | METH_KEYWORDS, "Session setup" },
311
312         { "tconx", (PyCFunction)py_smb_tconx,
313           METH_VARARGS | METH_KEYWORDS, "Tree connect" },
314
315         /* File operations */
316
317         { "nt_create_andx", (PyCFunction)py_smb_nt_create_andx,
318           METH_VARARGS | METH_KEYWORDS, "NT Create&X" },
319
320         { "close", (PyCFunction)py_smb_close,
321           METH_VARARGS | METH_KEYWORDS, "Close" },
322
323         { "unlink", (PyCFunction)py_smb_unlink,
324           METH_VARARGS | METH_KEYWORDS, "Unlink" },
325
326         /* Security descriptors */
327
328         { "query_secdesc", (PyCFunction)py_smb_query_secdesc,
329           METH_VARARGS | METH_KEYWORDS, "Query security descriptor" },
330
331         { "set_secdesc", (PyCFunction)py_smb_set_secdesc,
332           METH_VARARGS | METH_KEYWORDS, "Set security descriptor" },
333
334         { NULL }
335 };
336
337 /*
338  * Method dispatch tables
339  */
340
341 static PyMethodDef smb_methods[] = {
342
343         { "connect", (PyCFunction)py_smb_connect, METH_VARARGS | METH_KEYWORDS,
344           "Connect to a host" },
345
346         { NULL }
347 };
348
349 static void py_cli_state_dealloc(PyObject* self)
350 {
351         PyObject_Del(self);
352 }
353
354 static PyObject *py_cli_state_getattr(PyObject *self, char *attrname)
355 {
356         return Py_FindMethod(smb_hnd_methods, self, attrname);
357 }
358
359 PyTypeObject cli_state_type = {
360         PyObject_HEAD_INIT(NULL)
361         0,
362         "SMB client connection",
363         sizeof(cli_state_object),
364         0,
365         py_cli_state_dealloc, /*tp_dealloc*/
366         0,          /*tp_print*/
367         py_cli_state_getattr,          /*tp_getattr*/
368         0,          /*tp_setattr*/
369         0,          /*tp_compare*/
370         0,          /*tp_repr*/
371         0,          /*tp_as_number*/
372         0,          /*tp_as_sequence*/
373         0,          /*tp_as_mapping*/
374         0,          /*tp_hash */
375 };
376
377 /*
378  * Module initialisation 
379  */
380
381 void initsmb(void)
382 {
383         PyObject *module, *dict;
384
385         /* Initialise module */
386
387         module = Py_InitModule("smb", smb_methods);
388         dict = PyModule_GetDict(module);
389
390         /* Initialise policy handle object */
391
392         cli_state_type.ob_type = &PyType_Type;
393
394         /* Do samba initialisation */
395
396         py_samba_init();
397
398         setup_logging("smb", True);
399         DEBUGLEVEL = 10;
400 }