fixed a segv in the python messaging code on 64 bit systems
[kai/samba.git] / source4 / lib / messaging / pymessaging.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
4
5    Based on the equivalent for EJS:
6    Copyright © Andrew Tridgell <tridge@samba.org> 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include <Python.h>
24 #include "libcli/util/pyerrors.h"
25 #include "librpc/rpc/pyrpc.h"
26 #include "lib/messaging/irpc.h"
27 #include "lib/messaging/messaging.h"
28 #include "lib/events/events.h"
29 #include "cluster/cluster.h"
30 #include "param/param.h"
31 #include "librpc/gen_ndr/py_irpc.h"
32
33 PyAPI_DATA(PyTypeObject) messaging_Type;
34 PyAPI_DATA(PyTypeObject) irpc_ClientConnectionType;
35
36 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
37 {
38         if (!PyTuple_Check(object)) {
39                 PyErr_SetString(PyExc_ValueError, "Expected tuple");
40                 return false;
41         }
42
43         if (PyTuple_Size(object) == 3) {
44                 return PyArg_ParseTuple(object, "iii", &server_id->id, &server_id->id2, &server_id->node);
45         } else {
46                 int id, id2;
47                 if (!PyArg_ParseTuple(object, "ii", &id, &id2))
48                         return false;
49                 *server_id = cluster_id(id, id2);
50                 return true;
51         }
52 }
53
54 typedef struct {
55         PyObject_HEAD
56         TALLOC_CTX *mem_ctx;
57         struct messaging_context *msg_ctx;
58 } messaging_Object;
59
60 PyObject *py_messaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
61 {
62         struct event_context *ev;
63         const char *kwnames[] = { "own_id", "messaging_path", NULL };
64         PyObject *own_id = Py_None;
65         const char *messaging_path = NULL;
66         messaging_Object *ret;
67
68         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oz:connect", 
69                 discard_const_p(char *, kwnames), &own_id, &messaging_path)) {
70                 return NULL;
71         }
72
73         ret = PyObject_New(messaging_Object, &messaging_Type);
74         if (ret == NULL)
75                 return NULL;
76
77         ret->mem_ctx = talloc_new(NULL);
78
79         ev = event_context_init(ret->mem_ctx);
80
81         if (messaging_path == NULL) {
82                 messaging_path = lp_messaging_path(ret, global_loadparm);
83         } else {
84                 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
85         }
86
87         if (own_id != Py_None) {
88                 struct server_id server_id;
89
90                 if (!server_id_from_py(own_id, &server_id)) 
91                         return NULL;
92
93                 ret->msg_ctx = messaging_init(ret->mem_ctx, 
94                                             messaging_path,
95                                             server_id,
96                                             lp_iconv_convenience(global_loadparm),
97                                             ev);
98         } else {
99                 ret->msg_ctx = messaging_client_init(ret->mem_ctx, 
100                                             messaging_path,
101                                             lp_iconv_convenience(global_loadparm),
102                                             ev);
103         }
104
105         if (ret->msg_ctx == NULL) {
106                 PyErr_SetString(PyExc_RuntimeError, "messaging_connect unable to create a messaging context");
107                 talloc_free(ret->mem_ctx);
108                 return NULL;
109         }
110
111         return (PyObject *)ret;
112 }
113
114 static void py_messaging_dealloc(PyObject *self)
115 {
116         messaging_Object *iface = (messaging_Object *)self;
117         talloc_free(iface->msg_ctx);
118         PyObject_Del(self);
119 }
120
121 static PyObject *py_messaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
122 {
123         messaging_Object *iface = (messaging_Object *)self;
124         uint32_t msg_type;
125         DATA_BLOB data;
126         PyObject *target;
127         NTSTATUS status;
128         struct server_id server;
129         const char *kwnames[] = { "target", "msg_type", "data", NULL };
130         int length;
131
132         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#|:send", 
133                 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
134                 return NULL;
135         }
136
137         data.length = length;
138
139         if (!server_id_from_py(target, &server)) 
140                 return NULL;
141
142         status = messaging_send(iface->msg_ctx, server, msg_type, &data);
143         if (NT_STATUS_IS_ERR(status)) {
144                 PyErr_SetNTSTATUS(status);
145                 return NULL;
146         }
147
148         return Py_None;
149 }
150
151 static void py_msg_callback_wrapper(struct messaging_context *msg, void *private, 
152                                uint32_t msg_type, 
153                                struct server_id server_id, DATA_BLOB *data)
154 {
155         PyObject *callback = (PyObject *)private;
156
157         PyObject_CallFunction(callback, discard_const_p(char, "i(iii)s#"), msg_type, 
158                               server_id.id, server_id.id2, server_id.node, 
159                               data->data, data->length);
160 }
161
162 static PyObject *py_messaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
163 {
164         messaging_Object *iface = (messaging_Object *)self;
165         uint32_t msg_type = -1;
166         PyObject *callback;
167         NTSTATUS status;
168         const char *kwnames[] = { "callback", "msg_type", NULL };
169
170         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:send", 
171                 discard_const_p(char *, kwnames), &callback, &msg_type)) {
172                 return NULL;
173         }
174
175         Py_INCREF(callback);
176
177         if (msg_type == -1) {
178                 status = messaging_register_tmp(iface->msg_ctx, callback,
179                                                 py_msg_callback_wrapper, &msg_type);
180         } else {
181                 status = messaging_register(iface->msg_ctx, callback,
182                                     msg_type, py_msg_callback_wrapper);
183         }
184         if (NT_STATUS_IS_ERR(status)) {
185                 PyErr_SetNTSTATUS(status);
186                 return NULL;
187         }
188
189         return PyLong_FromLong(msg_type);
190 }
191
192 static PyObject *py_messaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
193 {
194         messaging_Object *iface = (messaging_Object *)self;
195         uint32_t msg_type = -1;
196         PyObject *callback;
197         const char *kwnames[] = { "callback", "msg_type", NULL };
198
199         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:send", 
200                 discard_const_p(char *, kwnames), &callback, &msg_type)) {
201                 return NULL;
202         }
203
204         messaging_deregister(iface->msg_ctx, msg_type, callback);
205
206         Py_DECREF(callback);
207
208         return Py_None;
209 }
210
211 static PyObject *py_messaging_add_name(PyObject *self, PyObject *args, PyObject *kwargs)
212 {
213         messaging_Object *iface = (messaging_Object *)self;
214         NTSTATUS status;
215         char *name;
216         const char *kwnames[] = { "name", NULL };
217
218         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:send", 
219                 discard_const_p(char *, kwnames), &name)) {
220                 return NULL;
221         }
222
223         status = irpc_add_name(iface->msg_ctx, name);
224         if (NT_STATUS_IS_ERR(status)) {
225                 PyErr_SetNTSTATUS(status);
226                 return NULL;
227         }
228
229         return Py_None;
230 }
231
232
233 static PyObject *py_messaging_remove_name(PyObject *self, PyObject *args, PyObject *kwargs)
234 {
235         messaging_Object *iface = (messaging_Object *)self;
236         char *name;
237         const char *kwnames[] = { "name", NULL };
238
239         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:send", 
240                 discard_const_p(char *, kwnames), &name)) {
241                 return NULL;
242         }
243
244         irpc_remove_name(iface->msg_ctx, name);
245
246         return Py_None;
247 }
248
249 static PyMethodDef py_messaging_methods[] = {
250         { "send", (PyCFunction)py_messaging_send, METH_VARARGS|METH_KEYWORDS, 
251                 "S.send(target, msg_type, data) -> None\nSend a message" },
252         { "register", (PyCFunction)py_messaging_register, METH_VARARGS|METH_KEYWORDS,
253                 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
254         { "deregister", (PyCFunction)py_messaging_deregister, METH_VARARGS|METH_KEYWORDS,
255                 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
256         { "add_name", (PyCFunction)py_messaging_add_name, METH_VARARGS|METH_KEYWORDS, "S.add_name(name) -> None\nListen on another name" },
257         { "remove_name", (PyCFunction)py_messaging_remove_name, METH_VARARGS|METH_KEYWORDS, "S.remove_name(name) -> None\nStop listening on a name" },
258         { NULL, NULL, 0, NULL }
259 };
260
261 static PyObject *py_messaging_server_id(PyObject *obj, void *closure)
262 {
263         messaging_Object *iface = (messaging_Object *)obj;
264         struct server_id server_id = messaging_get_server_id(iface->msg_ctx);
265
266         return Py_BuildValue("(iii)", server_id.id, server_id.id2, 
267                              server_id.node);
268 }
269
270 static PyGetSetDef py_messaging_getset[] = {
271         { discard_const_p(char, "server_id"), py_messaging_server_id, NULL, 
272           discard_const_p(char, "local server id") },
273         { NULL },
274 };
275
276
277 PyTypeObject messaging_Type = {
278         PyObject_HEAD_INIT(NULL) 0,
279         .tp_name = "irpc.Messaging",
280         .tp_basicsize = sizeof(messaging_Object),
281         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
282         .tp_new = py_messaging_connect,
283         .tp_dealloc = py_messaging_dealloc,
284         .tp_methods = py_messaging_methods,
285         .tp_getset = py_messaging_getset,
286         .tp_doc = "Messaging(own_id=None, messaging_path=None)\n" \
287                   "Create a new object that can be used to communicate with the peers in the specified messaging path.\n" \
288                   "If no path is specified, the default path from smb.conf will be used."
289 };
290
291
292 /*
293   state of a irpc 'connection'
294 */
295 typedef struct {
296         PyObject_HEAD
297         const char *server_name;
298         struct server_id *dest_ids;
299         struct messaging_context *msg_ctx;
300         TALLOC_CTX *mem_ctx;
301 } irpc_ClientConnectionObject;
302
303 /*
304   setup a context for talking to a irpc server
305      example: 
306         status = irpc.connect("smb_server");
307 */
308
309 PyObject *py_irpc_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
310 {
311         struct event_context *ev;
312         const char *kwnames[] = { "server", "own_id", "messaging_path", NULL };
313         char *server;
314         const char *messaging_path = NULL;
315         PyObject *own_id = Py_None;
316         irpc_ClientConnectionObject *ret;
317
318         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|Oz:connect", 
319                 discard_const_p(char *, kwnames), &server, &own_id, &messaging_path)) {
320                 return NULL;
321         }
322
323         ret = PyObject_New(irpc_ClientConnectionObject, &irpc_ClientConnectionType);
324         if (ret == NULL)
325                 return NULL;
326
327         ret->mem_ctx = talloc_new(NULL);
328
329         ret->server_name = server;
330
331         ev = event_context_init(ret->mem_ctx);
332
333         if (messaging_path == NULL) {
334                 messaging_path = lp_messaging_path(ret, global_loadparm);
335         } else {
336                 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
337         }
338
339         if (own_id != Py_None) {
340                 struct server_id server_id;
341
342                 if (!server_id_from_py(own_id, &server_id)) 
343                         return NULL;
344
345                 ret->msg_ctx = messaging_init(ret->mem_ctx, 
346                                             messaging_path,
347                                             server_id,
348                                             lp_iconv_convenience(global_loadparm),
349                                             ev);
350         } else {
351                 ret->msg_ctx = messaging_client_init(ret->mem_ctx, 
352                                             messaging_path,
353                                             lp_iconv_convenience(global_loadparm),
354                                             ev);
355         }
356
357         if (ret->msg_ctx == NULL) {
358                 PyErr_SetString(PyExc_RuntimeError, "irpc_connect unable to create a messaging context");
359                 talloc_free(ret->mem_ctx);
360                 return NULL;
361         }
362
363         ret->dest_ids = irpc_servers_byname(ret->msg_ctx, ret->mem_ctx, ret->server_name);
364         if (ret->dest_ids == NULL || ret->dest_ids[0].id == 0) {
365                 talloc_free(ret->mem_ctx);
366                 PyErr_SetNTSTATUS(NT_STATUS_OBJECT_NAME_NOT_FOUND);
367                 return NULL;
368         } else {
369                 return (PyObject *)ret;
370         }
371 }
372
373 typedef struct {
374         PyObject_HEAD
375         struct irpc_request **reqs;
376         int count;
377         int current;
378         TALLOC_CTX *mem_ctx;
379         py_data_unpack_fn unpack_fn;
380 } irpc_ResultObject;
381
382         
383 static PyObject *irpc_result_next(irpc_ResultObject *iterator)
384 {
385         NTSTATUS status;
386
387         if (iterator->current >= iterator->count) {
388                 PyErr_SetString(PyExc_StopIteration, "No more results");
389                 return NULL;
390         }
391
392         status = irpc_call_recv(iterator->reqs[iterator->current]);
393         iterator->current++;
394         if (!NT_STATUS_IS_OK(status)) {
395                 PyErr_SetNTSTATUS(status);
396                 return NULL;
397         }
398
399         return iterator->unpack_fn(iterator->reqs[iterator->current-1]->r);
400 }
401
402 static PyObject *irpc_result_len(irpc_ResultObject *self)
403 {
404         return PyLong_FromLong(self->count);
405 }
406
407 static PyMethodDef irpc_result_methods[] = {
408         { "__len__", (PyCFunction)irpc_result_len, METH_NOARGS, 
409                 "Number of elements returned"},
410         { NULL }
411 };
412
413 static void irpc_result_dealloc(PyObject *self)
414 {
415         talloc_free(((irpc_ResultObject *)self)->mem_ctx);
416         PyObject_Del(self);
417 }
418
419 PyTypeObject irpc_ResultIteratorType = {
420         PyObject_HEAD_INIT(NULL) 0,
421         .tp_name = "irpc.ResultIterator",
422         .tp_basicsize = sizeof(irpc_ResultObject),
423         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
424         .tp_iternext = (iternextfunc)irpc_result_next,
425         .tp_iter = PyObject_SelfIter,
426         .tp_methods = irpc_result_methods,
427         .tp_dealloc = irpc_result_dealloc,
428 };
429
430 static PyObject *py_irpc_call(irpc_ClientConnectionObject *p, struct PyNdrRpcMethodDef *method_def, PyObject *args, PyObject *kwargs)
431 {
432         void *ptr;
433         struct irpc_request **reqs;
434         int i, count;
435         NTSTATUS status;
436         TALLOC_CTX *mem_ctx = talloc_new(NULL);
437         irpc_ResultObject *ret;
438
439         /* allocate the C structure */
440         ptr = talloc_zero_size(mem_ctx, method_def->table->calls[method_def->opnum].struct_size);
441         if (ptr == NULL) {
442                 status = NT_STATUS_NO_MEMORY;
443                 goto done;
444         }
445
446         /* convert the mpr object into a C structure */
447         if (!method_def->pack_in_data(args, kwargs, ptr)) {
448                 talloc_free(mem_ctx);
449                 return NULL;
450         }
451
452         for (count=0;p->dest_ids[count].id;count++) /* noop */ ;
453
454         /* we need to make a call per server */
455         reqs = talloc_array(mem_ctx, struct irpc_request *, count);
456         if (reqs == NULL) {
457                 status = NT_STATUS_NO_MEMORY;
458                 goto done;
459         }
460
461         /* make the actual calls */
462         for (i=0;i<count;i++) {
463                 reqs[i] = irpc_call_send(p->msg_ctx, p->dest_ids[i], 
464                                          method_def->table, method_def->opnum, ptr, ptr);
465                 if (reqs[i] == NULL) {
466                         status = NT_STATUS_NO_MEMORY;
467                         goto done;
468                 }
469                 talloc_steal(reqs, reqs[i]);
470         }
471
472         ret = PyObject_New(irpc_ResultObject, &irpc_ResultIteratorType);
473         ret->mem_ctx = mem_ctx;
474         ret->reqs = reqs;
475         ret->count = count;
476         ret->current = 0;
477         ret->unpack_fn = method_def->unpack_out_data;
478
479         return (PyObject *)ret;
480 done:
481         talloc_free(mem_ctx);
482         PyErr_SetNTSTATUS(status);
483         return NULL;
484 }
485
486 static PyObject *py_irpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs)
487 {       
488         irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self;
489         struct PyNdrRpcMethodDef *md = wrapped;
490
491         return py_irpc_call(iface, md, args, kwargs);
492 }
493
494 static void py_irpc_dealloc(PyObject *self)
495 {
496         irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self;
497         talloc_free(iface->mem_ctx);
498         PyObject_Del(self);
499 }
500
501 PyTypeObject irpc_ClientConnectionType = {
502         PyObject_HEAD_INIT(NULL) 0,
503         .tp_name = "irpc.ClientConnection",
504         .tp_basicsize = sizeof(irpc_ClientConnectionObject),
505         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
506         .tp_new = py_irpc_connect,
507         .tp_dealloc = py_irpc_dealloc,
508         .tp_doc = "ClientConnection(server, own_id=None, messaging_path=None)\n" \
509                   "Create a new IRPC client connection to communicate with the servers in the specified path.\n" \
510                   "If no path is specified, the default path from smb.conf will be used."
511 };
512
513 static bool irpc_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds)
514 {
515         int i;
516         for (i = 0; mds[i].name; i++) {
517                 PyObject *ret;
518                 struct wrapperbase *wb = calloc(sizeof(struct wrapperbase), 1);
519
520                 wb->name = discard_const_p(char, mds[i].name);
521                 wb->flags = PyWrapperFlag_KEYWORDS;
522                 wb->wrapper = (wrapperfunc)py_irpc_call_wrapper;
523                 wb->doc = discard_const_p(char, mds[i].doc);
524                 
525                 ret = PyDescr_NewWrapper(ifacetype, wb, discard_const_p(void, &mds[i]));
526
527                 PyDict_SetItemString(ifacetype->tp_dict, mds[i].name, 
528                                      (PyObject *)ret);
529         }
530
531         return true;
532 }
533
534 void initmessaging(void)
535 {
536         extern void initirpc(void);
537         PyObject *mod;
538
539         if (PyType_Ready(&irpc_ClientConnectionType) < 0)
540                 return;
541
542         if (PyType_Ready(&messaging_Type) < 0)
543                 return;
544
545         if (PyType_Ready(&irpc_ResultIteratorType) < 0) 
546                 return;
547
548         if (!irpc_AddNdrRpcMethods(&irpc_ClientConnectionType, py_ndr_irpc_methods))
549                 return;
550
551         mod = Py_InitModule3("messaging", NULL, "Internal RPC");
552         if (mod == NULL)
553                 return;
554
555         initirpc();
556
557         Py_INCREF((PyObject *)&irpc_ClientConnectionType);
558         PyModule_AddObject(mod, "ClientConnection", (PyObject *)&irpc_ClientConnectionType);
559
560         Py_INCREF((PyObject *)&messaging_Type);
561         PyModule_AddObject(mod, "Messaging", (PyObject *)&messaging_Type);
562 }