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