Fixed up tconx function. Implemented nt_create_andx, query_secdesc
[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" };
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", NULL };
124         char *service;
125         BOOL result;
126
127         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &service))
128                 return NULL;
129
130         result = cli_send_tconX(
131                 cli->cli, service, strequal(service, "IPC$") ? "IPC" : 
132                 "?????", "", 1);
133
134         return Py_BuildValue("i", result);
135 }
136
137 static PyObject *py_smb_nt_create_andx(PyObject *self, PyObject *args,
138                                        PyObject *kw)
139 {
140         cli_state_object *cli = (cli_state_object *)self;
141         static char *kwlist[] = { "filename", "desired_access", 
142                                   "file_attributes", "share_access",
143                                   "create_disposition", NULL };
144         char *filename;
145         uint32 desired_access, file_attributes = 0, 
146                 share_access = FILE_SHARE_READ | FILE_SHARE_WRITE,
147                 create_disposition = FILE_EXISTS_OPEN, create_options = 0;
148         int result;
149
150         /* Parse parameters */
151
152         if (!PyArg_ParseTupleAndKeywords(
153                     args, kw, "si|iiii", kwlist, &filename, &desired_access,
154                     &file_attributes, &share_access, &create_disposition,
155                     &create_options))
156                 return NULL;
157
158         result = cli_nt_create_full(
159                 cli->cli, filename, desired_access, file_attributes,
160                 share_access, create_disposition, create_options);
161
162         /* Return FID */
163
164         return PyInt_FromLong(result);
165 }
166
167 static PyObject *py_smb_query_secdesc(PyObject *self, PyObject *args,
168                                       PyObject *kw)
169 {
170         cli_state_object *cli = (cli_state_object *)self;
171         static char *kwlist[] = { "fnum", NULL };
172         PyObject *result;
173         SEC_DESC *secdesc = NULL;
174         int fnum;
175         TALLOC_CTX *mem_ctx;
176
177         /* Parse parameters */
178
179         if (!PyArg_ParseTupleAndKeywords(
180                     args, kw, "i", kwlist, &fnum))
181                 return NULL;
182
183         mem_ctx = talloc_init();
184
185         secdesc = cli_query_secdesc(cli->cli, fnum, mem_ctx);
186
187         /* FIXME: we should raise an exception here */
188
189         if (!secdesc) {
190                 Py_INCREF(Py_None);
191                 result = Py_None;
192                 goto done;
193         }
194
195         if (!py_from_SECDESC(&result, secdesc)) {
196                 PyErr_SetString(
197                         PyExc_TypeError,
198                         "Invalid security descriptor returned");
199                 result = NULL;
200                 goto done;
201         }
202
203  done:
204         talloc_destroy(mem_ctx);
205
206         return result;
207         
208 }
209
210 static PyObject *py_smb_set_secdesc(PyObject *self, PyObject *args,
211                                     PyObject *kw)
212 {
213         cli_state_object *cli = (cli_state_object *)self;
214         static char *kwlist[] = { "fnum", "security_descriptor", NULL };
215         PyObject *py_secdesc;
216         SEC_DESC *secdesc;
217         TALLOC_CTX *mem_ctx = talloc_init();
218         int fnum;
219         BOOL result;
220
221         /* Parse parameters */
222
223         if (!PyArg_ParseTupleAndKeywords(
224                     args, kw, "iO", kwlist, &fnum, &py_secdesc))
225                 return NULL;
226
227         if (!py_to_SECDESC(&secdesc, py_secdesc, mem_ctx)) {
228                 PyErr_SetString(PyExc_TypeError, 
229                                 "Invalid security descriptor");
230                 return NULL;
231         }
232
233         result = cli_set_secdesc(cli->cli, fnum, secdesc);
234
235         return PyInt_FromLong(result);
236 }
237
238 static PyMethodDef smb_hnd_methods[] = {
239
240         /* Session and connection handling */
241
242         { "session_request", (PyCFunction)py_smb_session_request, 
243           METH_VARARGS | METH_KEYWORDS, "Request a session" },
244
245         { "negprot", (PyCFunction)py_smb_negprot, 
246           METH_VARARGS | METH_KEYWORDS, "Protocol negotiation" },
247
248         { "session_setup", (PyCFunction)py_smb_session_setup,
249           METH_VARARGS | METH_KEYWORDS, "Session setup" },
250
251         { "tconx", (PyCFunction)py_smb_tconx,
252           METH_VARARGS | METH_KEYWORDS, "Tree connect" },
253
254         /* File operations */
255
256         { "nt_create_andx", (PyCFunction)py_smb_nt_create_andx,
257           METH_VARARGS | METH_KEYWORDS, "NT Create&X" },
258
259         /* Security descriptors */
260
261         { "query_secdesc", (PyCFunction)py_smb_query_secdesc,
262           METH_VARARGS | METH_KEYWORDS, "Query security descriptor" },
263
264         { "set_secdesc", (PyCFunction)py_smb_set_secdesc,
265           METH_VARARGS | METH_KEYWORDS, "Set security descriptor" },
266
267         { NULL }
268 };
269
270 /*
271  * Method dispatch tables
272  */
273
274 static PyMethodDef smb_methods[] = {
275
276         { "connect", (PyCFunction)py_smb_connect, METH_VARARGS | METH_KEYWORDS,
277           "Connect to a host" },
278
279         { NULL }
280 };
281
282 static void py_cli_state_dealloc(PyObject* self)
283 {
284         PyObject_Del(self);
285 }
286
287 static PyObject *py_cli_state_getattr(PyObject *self, char *attrname)
288 {
289         return Py_FindMethod(smb_hnd_methods, self, attrname);
290 }
291
292 PyTypeObject cli_state_type = {
293         PyObject_HEAD_INIT(NULL)
294         0,
295         "SMB client connection",
296         sizeof(cli_state_object),
297         0,
298         py_cli_state_dealloc, /*tp_dealloc*/
299         0,          /*tp_print*/
300         py_cli_state_getattr,          /*tp_getattr*/
301         0,          /*tp_setattr*/
302         0,          /*tp_compare*/
303         0,          /*tp_repr*/
304         0,          /*tp_as_number*/
305         0,          /*tp_as_sequence*/
306         0,          /*tp_as_mapping*/
307         0,          /*tp_hash */
308 };
309
310 /*
311  * Module initialisation 
312  */
313
314 void initsmb(void)
315 {
316         PyObject *module, *dict;
317
318         /* Initialise module */
319
320         module = Py_InitModule("smb", smb_methods);
321         dict = PyModule_GetDict(module);
322
323         /* Initialise policy handle object */
324
325         cli_state_type.ob_type = &PyType_Type;
326
327         /* Do samba initialisation */
328
329         py_samba_init();
330
331         setup_logging("smb", True);
332         DEBUGLEVEL = 10;
333 }