2 Unix SMB/CIFS implementation.
3 Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
5 Based on the equivalent for EJS:
6 Copyright © Andrew Tridgell <tridge@samba.org> 2005
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.
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.
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/>.
22 #include "lib/replace/system/python.h"
23 #include "python/py3compat.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"
38 #include "messaging_internal.h"
41 extern PyTypeObject imessaging_Type
;
43 static bool server_id_from_py(PyObject
*object
, struct server_id
*server_id
)
45 Py_ssize_t tuple_size
;
47 if (!PyTuple_Check(object
)) {
48 if (!py_check_dcerpc_type(object
, "samba.dcerpc.server_id", "server_id")) {
50 PyErr_SetString(PyExc_ValueError
, "Expected tuple or server_id");
53 *server_id
= *pytalloc_get_type(object
, struct server_id
);
57 tuple_size
= PyTuple_Size(object
);
58 if (tuple_size
== 3) {
59 unsigned long long pid
;
62 if (!PyArg_ParseTuple(object
, "Kii", &pid
, &task_id
, &vnn
)) {
66 server_id
->task_id
= task_id
;
69 } else if (tuple_size
== 2) {
70 unsigned long long pid
;
72 if (!PyArg_ParseTuple(object
, "Ki", &pid
, &task_id
))
74 *server_id
= cluster_id(pid
, task_id
);
76 } else if (tuple_size
== 1) {
77 unsigned long long pid
= getpid();
79 if (!PyArg_ParseTuple(object
, "i", &task_id
))
81 *server_id
= cluster_id(pid
, task_id
);
84 PyErr_SetString(PyExc_ValueError
, "Expected tuple containing one, two, or three elements");
92 struct imessaging_context
*msg_ctx
;
95 static PyObject
*py_imessaging_connect(PyTypeObject
*self
, PyObject
*args
, PyObject
*kwargs
)
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
;
104 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|OO",
105 discard_const_p(char *, kwnames
), &own_id
, &py_lp_ctx
)) {
109 ret
= PyObject_New(imessaging_Object
, &imessaging_Type
);
113 ret
->mem_ctx
= talloc_new(NULL
);
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
);
122 ev
= s4_event_context_init(ret
->mem_ctx
);
124 if (own_id
!= Py_None
) {
125 struct server_id server_id
;
127 if (!server_id_from_py(own_id
, &server_id
)) {
128 talloc_free(ret
->mem_ctx
);
132 ret
->msg_ctx
= imessaging_init(ret
->mem_ctx
,
137 ret
->msg_ctx
= imessaging_client_init(ret
->mem_ctx
,
142 if (ret
->msg_ctx
== NULL
) {
143 PyErr_SetString(PyExc_RuntimeError
, "unable to create a messaging context");
144 talloc_free(ret
->mem_ctx
);
148 return (PyObject
*)ret
;
151 static void py_imessaging_dealloc(PyObject
*self
)
153 imessaging_Object
*iface
= (imessaging_Object
*)self
;
154 talloc_free(iface
->msg_ctx
);
155 self
->ob_type
->tp_free(self
);
158 static PyObject
*py_imessaging_send(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
160 imessaging_Object
*iface
= (imessaging_Object
*)self
;
165 struct server_id server
;
166 const char *kwnames
[] = { "target", "msg_type", "data", NULL
};
169 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OIs#:send",
170 discard_const_p(char *, kwnames
), &target
, &msg_type
, &data
.data
, &length
)) {
175 data
.length
= length
;
177 if (!server_id_from_py(target
, &server
))
180 status
= imessaging_send(iface
->msg_ctx
, server
, msg_type
, &data
);
181 if (NT_STATUS_IS_ERR(status
)) {
182 PyErr_SetNTSTATUS(status
);
189 static void py_msg_callback_wrapper(struct imessaging_context
*msg
,
192 struct server_id server_id
,
197 PyObject
*py_server_id
, *callback_and_tuple
= (PyObject
*)private_data
;
198 PyObject
*callback
, *py_private
;
199 PyObject
*result
= NULL
;
201 struct server_id
*p_server_id
= NULL
;
204 DBG_WARNING("Received %zu fds, ignoring message\n", num_fds
);
208 p_server_id
= talloc(NULL
, struct server_id
);
213 *p_server_id
= server_id
;
215 py_server_id
= py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id
, p_server_id
);
216 talloc_unlink(NULL
, p_server_id
);
217 if (py_server_id
== NULL
) {
221 if (!PyArg_ParseTuple(callback_and_tuple
, "OO",
227 result
= PyObject_CallFunction(callback
, discard_const_p(char, "OiOs#"),
231 data
->data
, data
->length
);
235 static PyObject
*py_imessaging_register(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
237 imessaging_Object
*iface
= (imessaging_Object
*)self
;
239 PyObject
*callback_and_context
;
241 const char *kwnames
[] = { "callback_and_context", "msg_type", NULL
};
243 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|i:register",
244 discard_const_p(char *, kwnames
),
245 &callback_and_context
, &msg_type
)) {
248 if (!PyTuple_Check(callback_and_context
)
249 || PyTuple_Size(callback_and_context
) != 2) {
250 PyErr_SetString(PyExc_ValueError
, "Expected tuple of size 2 for callback_and_context");
254 Py_INCREF(callback_and_context
);
256 if (msg_type
== -1) {
257 uint32_t msg_type32
= msg_type
;
258 status
= imessaging_register_tmp(iface
->msg_ctx
, callback_and_context
,
259 py_msg_callback_wrapper
, &msg_type32
);
260 msg_type
= msg_type32
;
262 status
= imessaging_register(iface
->msg_ctx
, callback_and_context
,
263 msg_type
, py_msg_callback_wrapper
);
265 if (NT_STATUS_IS_ERR(status
)) {
266 Py_DECREF(callback_and_context
);
267 PyErr_SetNTSTATUS(status
);
271 return PyLong_FromLong(msg_type
);
274 static PyObject
*py_imessaging_deregister(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
276 imessaging_Object
*iface
= (imessaging_Object
*)self
;
279 const char *kwnames
[] = { "callback", "msg_type", NULL
};
282 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|i:deregister",
283 discard_const_p(char *, kwnames
), &callback
, &msg_type
)) {
287 removed
= imessaging_deregister(iface
->msg_ctx
, msg_type
, callback
);
288 while (removed
-- > 0) {
295 static void simple_timer_handler(struct tevent_context
*ev
,
296 struct tevent_timer
*te
,
297 struct timeval current_time
,
303 static PyObject
*py_imessaging_loop_once(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
305 imessaging_Object
*iface
= (imessaging_Object
*)self
;
308 struct timeval next_event
;
309 struct tevent_timer
*timer
= NULL
;
310 const char *kwnames
[] = { "timeout", NULL
};
312 TALLOC_CTX
*frame
= talloc_stackframe();
314 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "d",
315 discard_const_p(char *, kwnames
), &offset
)) {
323 next_event
= tevent_timeval_current_ofs(seconds
, (int)(offset
*1000000));
325 timer
= tevent_add_timer(iface
->msg_ctx
->ev
, frame
, next_event
, simple_timer_handler
,
334 tevent_loop_once(iface
->msg_ctx
->ev
);
341 static PyObject
*py_irpc_add_name(PyObject
*self
, PyObject
*args
)
343 imessaging_Object
*iface
= (imessaging_Object
*)self
;
347 if (!PyArg_ParseTuple(args
, "s", &server_name
)) {
351 status
= irpc_add_name(iface
->msg_ctx
, server_name
);
352 if (!NT_STATUS_IS_OK(status
)) {
353 PyErr_SetNTSTATUS(status
);
360 static PyObject
*py_irpc_remove_name(PyObject
*self
, PyObject
*args
)
362 imessaging_Object
*iface
= (imessaging_Object
*)self
;
365 if (!PyArg_ParseTuple(args
, "s", &server_name
)) {
369 irpc_remove_name(iface
->msg_ctx
, server_name
);
374 static PyObject
*py_irpc_servers_byname(PyObject
*self
, PyObject
*args
)
376 imessaging_Object
*iface
= (imessaging_Object
*)self
;
379 struct server_id
*ids
;
381 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
389 if (!PyArg_ParseTuple(args
, "s", &server_name
)) {
390 TALLOC_FREE(mem_ctx
);
394 status
= irpc_servers_byname(iface
->msg_ctx
, mem_ctx
, server_name
,
396 if (!NT_STATUS_IS_OK(status
)) {
397 TALLOC_FREE(mem_ctx
);
398 PyErr_SetString(PyExc_KeyError
, "No such name");
402 pylist
= PyList_New(num_ids
);
403 if (pylist
== NULL
) {
404 TALLOC_FREE(mem_ctx
);
408 for (i
= 0; i
< num_ids
; i
++) {
409 PyObject
*py_server_id
;
410 struct server_id
*p_server_id
= talloc(NULL
, struct server_id
);
412 TALLOC_FREE(mem_ctx
);
416 *p_server_id
= ids
[i
];
418 py_server_id
= py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id
, p_server_id
);
420 TALLOC_FREE(mem_ctx
);
423 PyList_SetItem(pylist
, i
, py_server_id
);
424 talloc_unlink(NULL
, p_server_id
);
426 TALLOC_FREE(mem_ctx
);
430 static PyObject
*py_irpc_all_servers(PyObject
*self
,
431 PyObject
*Py_UNUSED(ignored
))
433 imessaging_Object
*iface
= (imessaging_Object
*)self
;
436 struct irpc_name_records
*records
;
437 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
443 records
= irpc_all_servers(iface
->msg_ctx
, mem_ctx
);
444 if (records
== NULL
) {
445 TALLOC_FREE(mem_ctx
);
450 pylist
= PyList_New(records
->num_records
);
451 if (pylist
== NULL
) {
452 TALLOC_FREE(mem_ctx
);
456 for (i
= 0; i
< records
->num_records
; i
++) {
457 PyObject
*py_name_record
458 = py_return_ndr_struct("samba.dcerpc.irpc",
462 if (!py_name_record
) {
463 TALLOC_FREE(mem_ctx
);
466 PyList_SetItem(pylist
, i
,
469 TALLOC_FREE(mem_ctx
);
473 static PyMethodDef py_imessaging_methods
[] = {
474 { "send", PY_DISCARD_FUNC_SIG(PyCFunction
, py_imessaging_send
),
475 METH_VARARGS
|METH_KEYWORDS
,
476 "S.send(target, msg_type, data) -> None\nSend a message" },
477 { "register", PY_DISCARD_FUNC_SIG(PyCFunction
, py_imessaging_register
),
478 METH_VARARGS
|METH_KEYWORDS
,
479 "S.register((callback, context), msg_type=None) -> msg_type\nRegister a message handler. "
480 "The callback and context must be supplied as a two-element tuple." },
481 { "deregister", PY_DISCARD_FUNC_SIG(PyCFunction
,
482 py_imessaging_deregister
),
483 METH_VARARGS
|METH_KEYWORDS
,
484 "S.deregister((callback, context), msg_type) -> None\nDeregister a message handler "
485 "The callback and context must be supplied as the exact same two-element tuple "
486 "as was used at registration time." },
487 { "loop_once", PY_DISCARD_FUNC_SIG(PyCFunction
,
488 py_imessaging_loop_once
),
489 METH_VARARGS
|METH_KEYWORDS
,
490 "S.loop_once(timeout) -> None\n"
491 "Loop on the internal event context until we get an event "
492 "(which might be a message calling the callback), "
493 "timeout after timeout seconds (if not 0)" },
494 { "irpc_add_name", (PyCFunction
)py_irpc_add_name
, METH_VARARGS
,
495 "S.irpc_add_name(name) -> None\n"
496 "Add this context to the list of server_id values that "
497 "are registered for a particular name" },
498 { "irpc_remove_name", (PyCFunction
)py_irpc_remove_name
, METH_VARARGS
,
499 "S.irpc_remove_name(name) -> None\n"
500 "Remove this context from the list of server_id values that "
501 "are registered for a particular name" },
502 { "irpc_servers_byname", (PyCFunction
)py_irpc_servers_byname
, METH_VARARGS
,
503 "S.irpc_servers_byname(name) -> list\nGet list of server_id values that are registered for a particular name" },
504 { "irpc_all_servers", (PyCFunction
)py_irpc_all_servers
, METH_NOARGS
,
505 "S.irpc_all_servers() -> list\n"
506 "Get list of all registered names and the associated server_id values" },
507 { NULL
, NULL
, 0, NULL
}
510 static PyObject
*py_imessaging_server_id(PyObject
*obj
, void *closure
)
512 imessaging_Object
*iface
= (imessaging_Object
*)obj
;
513 PyObject
*py_server_id
;
514 struct server_id server_id
= imessaging_get_server_id(iface
->msg_ctx
);
515 struct server_id
*p_server_id
= talloc(NULL
, struct server_id
);
520 *p_server_id
= server_id
;
522 py_server_id
= py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id
, p_server_id
);
523 talloc_unlink(NULL
, p_server_id
);
528 static PyGetSetDef py_imessaging_getset
[] = {
530 .name
= discard_const_p(char, "server_id"),
531 .get
= py_imessaging_server_id
,
532 .doc
= discard_const_p(char, "local server id")
538 PyTypeObject imessaging_Type
= {
539 PyVarObject_HEAD_INIT(NULL
, 0)
540 .tp_name
= "messaging.Messaging",
541 .tp_basicsize
= sizeof(imessaging_Object
),
542 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
543 .tp_new
= py_imessaging_connect
,
544 .tp_dealloc
= py_imessaging_dealloc
,
545 .tp_methods
= py_imessaging_methods
,
546 .tp_getset
= py_imessaging_getset
,
547 .tp_doc
= "Messaging(own_id=None, lp_ctx=None)\n" \
548 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n"
551 static struct PyModuleDef moduledef
= {
552 PyModuleDef_HEAD_INIT
,
553 .m_name
= "messaging",
554 .m_doc
= "Internal RPC",
559 MODULE_INIT_FUNC(messaging
)
563 if (PyType_Ready(&imessaging_Type
) < 0)
566 mod
= PyModule_Create(&moduledef
);
570 Py_INCREF((PyObject
*)&imessaging_Type
);
571 PyModule_AddObject(mod
, "Messaging", (PyObject
*)&imessaging_Type
);
572 PyModule_AddObject(mod
, "IRPC_CALL_TIMEOUT", PyLong_FromLong(IRPC_CALL_TIMEOUT
));
573 PyModule_AddObject(mod
, "IRPC_CALL_TIMEOUT_INF", PyLong_FromLong(IRPC_CALL_TIMEOUT_INF
));