5dd5839b629562e2322759900a219c398f5ab609
[gd/samba-autobuild/.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 <Python.h>
23 #include "python/py3compat.h"
24 #include "includes.h"
25 #include "python/modules.h"
26 #include "libcli/util/pyerrors.h"
27 #include "librpc/rpc/pyrpc_util.h"
28 #include "librpc/ndr/libndr.h"
29 #include "lib/messaging/messaging.h"
30 #include "lib/messaging/irpc.h"
31 #include "lib/events/events.h"
32 #include "cluster/cluster.h"
33 #include "param/param.h"
34 #include "param/pyparam.h"
35 #include "librpc/rpc/dcerpc.h"
36 #include "librpc/gen_ndr/server_id.h"
37 #include <pytalloc.h>
38 #include "messaging_internal.h"
39
40
41 extern PyTypeObject imessaging_Type;
42
43 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
44 {
45         Py_ssize_t tuple_size;
46
47         if (!PyTuple_Check(object)) {
48                 if (!py_check_dcerpc_type(object, "samba.dcerpc.server_id", "server_id")) {
49
50                         PyErr_SetString(PyExc_ValueError, "Expected tuple or server_id");
51                         return false;
52                 }
53                 *server_id = *pytalloc_get_type(object, struct server_id);
54                 return true;
55         }
56
57         tuple_size = PyTuple_Size(object);
58         if (tuple_size == 3) {
59                 unsigned long long pid;
60                 int task_id, vnn;
61
62                 if (!PyArg_ParseTuple(object, "Kii", &pid, &task_id, &vnn)) {
63                         return false;
64                 }
65                 server_id->pid = pid;
66                 server_id->task_id = task_id;
67                 server_id->vnn = vnn;
68                 return true;
69         } else if (tuple_size == 2) {
70                 unsigned long long pid;
71                 int task_id;
72                 if (!PyArg_ParseTuple(object, "Ki", &pid, &task_id))
73                         return false;
74                 *server_id = cluster_id(pid, task_id);
75                 return true;
76         } else if (tuple_size == 1) {
77                 unsigned long long pid = getpid();
78                 int task_id;
79                 if (!PyArg_ParseTuple(object, "i", &task_id))
80                         return false;
81                 *server_id = cluster_id(pid, task_id);
82                 return true;
83         } else {
84                 PyErr_SetString(PyExc_ValueError, "Expected tuple containing one, two, or three elements");
85                 return false;
86         }
87 }
88
89 typedef struct {
90         PyObject_HEAD
91         TALLOC_CTX *mem_ctx;
92         struct imessaging_context *msg_ctx;
93 } imessaging_Object;
94
95 static PyObject *py_imessaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
96 {
97         struct tevent_context *ev;
98         const char *kwnames[] = { "own_id", "lp_ctx", NULL };
99         PyObject *own_id = Py_None;
100         PyObject *py_lp_ctx = Py_None;
101         imessaging_Object *ret;
102         struct loadparm_context *lp_ctx;
103
104         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO",
105                 discard_const_p(char *, kwnames), &own_id, &py_lp_ctx)) {
106                 return NULL;
107         }
108
109         ret = PyObject_New(imessaging_Object, &imessaging_Type);
110         if (ret == NULL)
111                 return NULL;
112
113         ret->mem_ctx = talloc_new(NULL);
114
115         lp_ctx = lpcfg_from_py_object(ret->mem_ctx, py_lp_ctx);
116         if (lp_ctx == NULL) {
117                 PyErr_SetString(PyExc_RuntimeError, "unable to interpret loadparm_context");
118                 talloc_free(ret->mem_ctx);
119                 return NULL;
120         }
121
122         ev = s4_event_context_init(ret->mem_ctx);
123
124         if (own_id != Py_None) {
125                 struct server_id server_id;
126
127                 if (!server_id_from_py(own_id, &server_id)) 
128                         return NULL;
129
130                 ret->msg_ctx = imessaging_init(ret->mem_ctx,
131                                                lp_ctx,
132                                                server_id,
133                                                ev);
134         } else {
135                 ret->msg_ctx = imessaging_client_init(ret->mem_ctx,
136                                                       lp_ctx,
137                                                       ev);
138         }
139
140         if (ret->msg_ctx == NULL) {
141                 PyErr_SetString(PyExc_RuntimeError, "unable to create a messaging context");
142                 talloc_free(ret->mem_ctx);
143                 return NULL;
144         }
145
146         return (PyObject *)ret;
147 }
148
149 static void py_imessaging_dealloc(PyObject *self)
150 {
151         imessaging_Object *iface = (imessaging_Object *)self;
152         talloc_free(iface->msg_ctx);
153         self->ob_type->tp_free(self);
154 }
155
156 static PyObject *py_imessaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
157 {
158         imessaging_Object *iface = (imessaging_Object *)self;
159         uint32_t msg_type;
160         DATA_BLOB data;
161         PyObject *target;
162         NTSTATUS status;
163         struct server_id server;
164         const char *kwnames[] = { "target", "msg_type", "data", NULL };
165         Py_ssize_t length;
166
167         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OIs#:send",
168                 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
169
170                 return NULL;
171         }
172
173         data.length = length;
174
175         if (!server_id_from_py(target, &server))
176                 return NULL;
177
178         status = imessaging_send(iface->msg_ctx, server, msg_type, &data);
179         if (NT_STATUS_IS_ERR(status)) {
180                 PyErr_SetNTSTATUS(status);
181                 return NULL;
182         }
183
184         Py_RETURN_NONE;
185 }
186
187 static void py_msg_callback_wrapper(struct imessaging_context *msg,
188                                     void *private_data,
189                                     uint32_t msg_type,
190                                     struct server_id server_id,
191                                     size_t num_fds,
192                                     int *fds,
193                                     DATA_BLOB *data)
194 {
195         PyObject *py_server_id, *callback_and_tuple = (PyObject *)private_data;
196         PyObject *callback, *py_private;
197
198         struct server_id *p_server_id = NULL;
199
200         if (num_fds != 0) {
201                 DBG_WARNING("Received %zu fds, ignoring message\n", num_fds);
202                 return;
203         }
204
205         p_server_id = talloc(NULL, struct server_id);
206         if (!p_server_id) {
207                 PyErr_NoMemory();
208                 return;
209         }
210         *p_server_id = server_id;
211
212         if (!PyArg_ParseTuple(callback_and_tuple, "OO",
213                               &callback,
214                               &py_private)) {
215                 return;
216         }
217
218         py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
219         talloc_unlink(NULL, p_server_id);
220
221         PyObject_CallFunction(callback, discard_const_p(char, "OiOs#"),
222                               py_private,
223                               msg_type,
224                               py_server_id,
225                               data->data, data->length);
226 }
227
228 static PyObject *py_imessaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
229 {
230         imessaging_Object *iface = (imessaging_Object *)self;
231         int msg_type = -1;
232         PyObject *callback_and_context;
233         NTSTATUS status;
234         const char *kwnames[] = { "callback_and_context", "msg_type", NULL };
235         
236         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register", 
237                 discard_const_p(char *, kwnames),
238                                          &callback_and_context, &msg_type)) {
239                 return NULL;
240         }
241         if (!PyTuple_Check(callback_and_context)
242             || PyTuple_Size(callback_and_context) != 2) {
243                 PyErr_SetString(PyExc_ValueError, "Expected tuple of size 2 for callback_and_context");
244                 return NULL;
245         }
246
247         Py_INCREF(callback_and_context);
248
249         if (msg_type == -1) {
250                 uint32_t msg_type32 = msg_type;
251                 status = imessaging_register_tmp(iface->msg_ctx, callback_and_context,
252                                                 py_msg_callback_wrapper, &msg_type32);
253                 msg_type = msg_type32;
254         } else {
255                 status = imessaging_register(iface->msg_ctx, callback_and_context,
256                                     msg_type, py_msg_callback_wrapper);
257         }
258         if (NT_STATUS_IS_ERR(status)) {
259                 Py_DECREF(callback_and_context);
260                 PyErr_SetNTSTATUS(status);
261                 return NULL;
262         }
263
264         return PyLong_FromLong(msg_type);
265 }
266
267 static PyObject *py_imessaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
268 {
269         imessaging_Object *iface = (imessaging_Object *)self;
270         int msg_type = -1;
271         PyObject *callback;
272         const char *kwnames[] = { "callback", "msg_type", NULL };
273         size_t removed;
274
275         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
276                 discard_const_p(char *, kwnames), &callback, &msg_type)) {
277                 return NULL;
278         }
279
280         removed = imessaging_deregister(iface->msg_ctx, msg_type, callback);
281         while (removed-- > 0) {
282                 Py_DECREF(callback);
283         }
284
285         Py_RETURN_NONE;
286 }
287
288 static void simple_timer_handler(struct tevent_context *ev,
289                                  struct tevent_timer *te,
290                                  struct timeval current_time,
291                                  void *private_data)
292 {
293         return;
294 }
295
296 static PyObject *py_imessaging_loop_once(PyObject *self, PyObject *args, PyObject *kwargs)
297 {
298         imessaging_Object *iface = (imessaging_Object *)self;
299         double offset;
300         int seconds;
301         struct timeval next_event;
302         struct tevent_timer *timer = NULL;
303         const char *kwnames[] = { "timeout", NULL };
304
305         TALLOC_CTX *frame = talloc_stackframe();
306
307         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "d",
308                                          discard_const_p(char *, kwnames), &offset)) {
309                 TALLOC_FREE(frame);
310                 return NULL;
311         }
312
313         if (offset != 0.0) {
314                 seconds = offset;
315                 offset -= seconds;
316                 next_event = tevent_timeval_current_ofs(seconds, (int)(offset*1000000));
317
318                 timer = tevent_add_timer(iface->msg_ctx->ev, frame, next_event, simple_timer_handler,
319                                          NULL);
320                 if (timer == NULL) {
321                         PyErr_NoMemory();
322                         TALLOC_FREE(frame);
323                         return NULL;
324                 }
325         }
326
327         tevent_loop_once(iface->msg_ctx->ev);
328
329         TALLOC_FREE(frame);
330
331         Py_RETURN_NONE;
332 }
333
334 static PyObject *py_irpc_add_name(PyObject *self, PyObject *args)
335 {
336         imessaging_Object *iface = (imessaging_Object *)self;
337         char *server_name;
338         NTSTATUS status;
339
340         if (!PyArg_ParseTuple(args, "s", &server_name)) {
341                 return NULL;
342         }
343
344         status = irpc_add_name(iface->msg_ctx, server_name);
345         if (!NT_STATUS_IS_OK(status)) {
346                 PyErr_SetNTSTATUS(status);
347                 return NULL;
348         }
349
350         Py_RETURN_NONE;
351 }
352
353 static PyObject *py_irpc_remove_name(PyObject *self, PyObject *args)
354 {
355         imessaging_Object *iface = (imessaging_Object *)self;
356         char *server_name;
357
358         if (!PyArg_ParseTuple(args, "s", &server_name)) {
359                 return NULL;
360         }
361
362         irpc_remove_name(iface->msg_ctx, server_name);
363
364         Py_RETURN_NONE;
365 }
366
367 static PyObject *py_irpc_servers_byname(PyObject *self, PyObject *args)
368 {
369         imessaging_Object *iface = (imessaging_Object *)self;
370         char *server_name;
371         unsigned i, num_ids;
372         struct server_id *ids;
373         PyObject *pylist;
374         TALLOC_CTX *mem_ctx = talloc_new(NULL);
375         NTSTATUS status;
376
377         if (!mem_ctx) {
378                 PyErr_NoMemory();
379                 return NULL;
380         }
381
382         if (!PyArg_ParseTuple(args, "s", &server_name)) {
383                 TALLOC_FREE(mem_ctx);
384                 return NULL;
385         }
386
387         status = irpc_servers_byname(iface->msg_ctx, mem_ctx, server_name,
388                                      &num_ids, &ids);
389         if (!NT_STATUS_IS_OK(status)) {
390                 TALLOC_FREE(mem_ctx);
391                 PyErr_SetString(PyExc_KeyError, "No such name");
392                 return NULL;
393         }
394
395         pylist = PyList_New(num_ids);
396         if (pylist == NULL) {
397                 TALLOC_FREE(mem_ctx);
398                 PyErr_NoMemory();
399                 return NULL;
400         }
401         for (i = 0; i < num_ids; i++) {
402                 PyObject *py_server_id;
403                 struct server_id *p_server_id = talloc(NULL, struct server_id);
404                 if (!p_server_id) {
405                         PyErr_NoMemory();
406                         return NULL;
407                 }
408                 *p_server_id = ids[i];
409
410                 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
411                 if (!py_server_id) {
412                         return NULL;
413                 }
414                 PyList_SetItem(pylist, i, py_server_id);
415                 talloc_unlink(NULL, p_server_id);
416         }
417         TALLOC_FREE(mem_ctx);
418         return pylist;
419 }
420
421 static PyObject *py_irpc_all_servers(PyObject *self,
422                 PyObject *Py_UNUSED(ignored))
423 {
424         imessaging_Object *iface = (imessaging_Object *)self;
425         PyObject *pylist;
426         int i;
427         struct irpc_name_records *records;
428         TALLOC_CTX *mem_ctx = talloc_new(NULL);
429         if (!mem_ctx) {
430                 PyErr_NoMemory();
431                 return NULL;
432         }
433
434         records = irpc_all_servers(iface->msg_ctx, mem_ctx);
435         if (records == NULL) {
436                 return NULL;
437         }
438
439         pylist = PyList_New(records->num_records);
440         if (pylist == NULL) {
441                 TALLOC_FREE(mem_ctx);
442                 PyErr_NoMemory();
443                 return NULL;
444         }
445         for (i = 0; i < records->num_records; i++) {
446                 PyObject *py_name_record
447                         = py_return_ndr_struct("samba.dcerpc.irpc",
448                                                "name_record",
449                                                records->names[i],
450                                                records->names[i]);
451                 if (!py_name_record) {
452                         return NULL;
453                 }
454                 PyList_SetItem(pylist, i,
455                                py_name_record);
456         }
457         TALLOC_FREE(mem_ctx);
458         return pylist;
459 }
460
461 static PyMethodDef py_imessaging_methods[] = {
462         { "send", PY_DISCARD_FUNC_SIG(PyCFunction, py_imessaging_send),
463                 METH_VARARGS|METH_KEYWORDS,
464                 "S.send(target, msg_type, data) -> None\nSend a message" },
465         { "register", PY_DISCARD_FUNC_SIG(PyCFunction, py_imessaging_register),
466                 METH_VARARGS|METH_KEYWORDS,
467                 "S.register((callback, context), msg_type=None) -> msg_type\nRegister a message handler.  "
468                 "The callback and context must be supplied as a two-element tuple." },
469         { "deregister", PY_DISCARD_FUNC_SIG(PyCFunction,
470                                             py_imessaging_deregister),
471                 METH_VARARGS|METH_KEYWORDS,
472                 "S.deregister((callback, context), msg_type) -> None\nDeregister a message handler "
473                 "The callback and context must be supplied as the exact same two-element tuple "
474                 "as was used as registration time." },
475         { "loop_once", PY_DISCARD_FUNC_SIG(PyCFunction,
476                                            py_imessaging_loop_once),
477                 METH_VARARGS|METH_KEYWORDS,
478                 "S.loop_once(timeout) -> None\n"
479                 "Loop on the internal event context until we get an event "
480                 "(which might be a message calling the callback), "
481                 "timeout after timeout seconds (if not 0)" },
482         { "irpc_add_name", (PyCFunction)py_irpc_add_name, METH_VARARGS,
483                 "S.irpc_add_name(name) -> None\n"
484                 "Add this context to the list of server_id values that "
485                 "are registered for a particular name" },
486         { "irpc_remove_name", (PyCFunction)py_irpc_remove_name, METH_VARARGS,
487                 "S.irpc_remove_name(name) -> None\n"
488                 "Remove this context from the list of server_id values that "
489                 "are registered for a particular name" },
490         { "irpc_servers_byname", (PyCFunction)py_irpc_servers_byname, METH_VARARGS,
491                 "S.irpc_servers_byname(name) -> list\nGet list of server_id values that are registered for a particular name" },
492         { "irpc_all_servers", (PyCFunction)py_irpc_all_servers, METH_NOARGS,
493                 "S.irpc_all_servers() -> list\n"
494                 "Get list of all registered names and the associated server_id values" },
495         { NULL, NULL, 0, NULL }
496 };
497
498 static PyObject *py_imessaging_server_id(PyObject *obj, void *closure)
499 {
500         imessaging_Object *iface = (imessaging_Object *)obj;
501         PyObject *py_server_id;
502         struct server_id server_id = imessaging_get_server_id(iface->msg_ctx);
503         struct server_id *p_server_id = talloc(NULL, struct server_id);
504         if (!p_server_id) {
505                 PyErr_NoMemory();
506                 return NULL;
507         }
508         *p_server_id = server_id;
509
510         py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
511         talloc_unlink(NULL, p_server_id);
512
513         return py_server_id;
514 }
515
516 static PyGetSetDef py_imessaging_getset[] = {
517         {
518                 .name = discard_const_p(char, "server_id"),
519                 .get  = py_imessaging_server_id,
520                 .doc  = discard_const_p(char, "local server id")
521         },
522         { .name = NULL },
523 };
524
525
526 PyTypeObject imessaging_Type = {
527         PyVarObject_HEAD_INIT(NULL, 0)
528         .tp_name = "messaging.Messaging",
529         .tp_basicsize = sizeof(imessaging_Object),
530         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
531         .tp_new = py_imessaging_connect,
532         .tp_dealloc = py_imessaging_dealloc,
533         .tp_methods = py_imessaging_methods,
534         .tp_getset = py_imessaging_getset,
535         .tp_doc = "Messaging(own_id=None, lp_ctx=None)\n" \
536                   "Create a new object that can be used to communicate with the peers in the specified messaging path.\n"
537 };
538
539 static struct PyModuleDef moduledef = {
540     PyModuleDef_HEAD_INIT,
541     .m_name = "messaging",
542     .m_doc = "Internal RPC",
543     .m_size = -1,
544     .m_methods = NULL,
545 };
546
547 MODULE_INIT_FUNC(messaging)
548 {
549         PyObject *mod;
550
551         if (PyType_Ready(&imessaging_Type) < 0)
552                 return NULL;
553
554         mod = PyModule_Create(&moduledef);
555         if (mod == NULL)
556                 return NULL;
557
558         Py_INCREF((PyObject *)&imessaging_Type);
559         PyModule_AddObject(mod, "Messaging", (PyObject *)&imessaging_Type);
560         PyModule_AddObject(mod, "IRPC_CALL_TIMEOUT", PyLong_FromLong(IRPC_CALL_TIMEOUT));
561         PyModule_AddObject(mod, "IRPC_CALL_TIMEOUT_INF", PyLong_FromLong(IRPC_CALL_TIMEOUT_INF));
562
563         return mod;
564 }