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/>.
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"
34 PyAPI_DATA(PyTypeObject) messaging_Type;
35 PyAPI_DATA(PyTypeObject) irpc_ClientConnectionType;
37 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
39 if (!PyTuple_Check(object)) {
40 PyErr_SetString(PyExc_ValueError, "Expected tuple");
44 if (PyTuple_Size(object) == 3) {
45 return PyArg_ParseTuple(object, "iii", &server_id->id, &server_id->id2, &server_id->node);
48 if (!PyArg_ParseTuple(object, "ii", &id, &id2))
50 *server_id = cluster_id(id, id2);
58 struct messaging_context *msg_ctx;
61 PyObject *py_messaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
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;
69 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oz:connect",
70 discard_const_p(char *, kwnames), &own_id, &messaging_path)) {
74 ret = PyObject_New(messaging_Object, &messaging_Type);
78 ret->mem_ctx = talloc_new(NULL);
80 ev = s4_event_context_init(ret->mem_ctx);
82 if (messaging_path == NULL) {
83 messaging_path = lp_messaging_path(ret, global_loadparm);
85 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
88 if (own_id != Py_None) {
89 struct server_id server_id;
91 if (!server_id_from_py(own_id, &server_id))
94 ret->msg_ctx = messaging_init(ret->mem_ctx,
97 py_iconv_convenience(ret->mem_ctx),
100 ret->msg_ctx = messaging_client_init(ret->mem_ctx,
102 py_iconv_convenience(ret->mem_ctx),
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);
112 return (PyObject *)ret;
115 static void py_messaging_dealloc(PyObject *self)
117 messaging_Object *iface = (messaging_Object *)self;
118 talloc_free(iface->msg_ctx);
122 static PyObject *py_messaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
124 messaging_Object *iface = (messaging_Object *)self;
129 struct server_id server;
130 const char *kwnames[] = { "target", "msg_type", "data", NULL };
133 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#|:send",
134 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
138 data.length = length;
140 if (!server_id_from_py(target, &server))
143 status = messaging_send(iface->msg_ctx, server, msg_type, &data);
144 if (NT_STATUS_IS_ERR(status)) {
145 PyErr_SetNTSTATUS(status);
152 static void py_msg_callback_wrapper(struct messaging_context *msg, void *private,
154 struct server_id server_id, DATA_BLOB *data)
156 PyObject *callback = (PyObject *)private;
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);
163 static PyObject *py_messaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
165 messaging_Object *iface = (messaging_Object *)self;
169 const char *kwnames[] = { "callback", "msg_type", NULL };
171 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:send",
172 discard_const_p(char *, kwnames), &callback, &msg_type)) {
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;
184 status = messaging_register(iface->msg_ctx, callback,
185 msg_type, py_msg_callback_wrapper);
187 if (NT_STATUS_IS_ERR(status)) {
188 PyErr_SetNTSTATUS(status);
192 return PyLong_FromLong(msg_type);
195 static PyObject *py_messaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
197 messaging_Object *iface = (messaging_Object *)self;
200 const char *kwnames[] = { "callback", "msg_type", NULL };
202 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:send",
203 discard_const_p(char *, kwnames), &callback, &msg_type)) {
207 messaging_deregister(iface->msg_ctx, msg_type, callback);
214 static PyObject *py_messaging_add_name(PyObject *self, PyObject *args, PyObject *kwargs)
216 messaging_Object *iface = (messaging_Object *)self;
219 const char *kwnames[] = { "name", NULL };
221 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:send",
222 discard_const_p(char *, kwnames), &name)) {
226 status = irpc_add_name(iface->msg_ctx, name);
227 if (NT_STATUS_IS_ERR(status)) {
228 PyErr_SetNTSTATUS(status);
236 static PyObject *py_messaging_remove_name(PyObject *self, PyObject *args, PyObject *kwargs)
238 messaging_Object *iface = (messaging_Object *)self;
240 const char *kwnames[] = { "name", NULL };
242 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:send",
243 discard_const_p(char *, kwnames), &name)) {
247 irpc_remove_name(iface->msg_ctx, name);
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 }
264 static PyObject *py_messaging_server_id(PyObject *obj, void *closure)
266 messaging_Object *iface = (messaging_Object *)obj;
267 struct server_id server_id = messaging_get_server_id(iface->msg_ctx);
269 return Py_BuildValue("(iii)", server_id.id, server_id.id2,
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") },
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."
296 state of a irpc 'connection'
300 const char *server_name;
301 struct server_id *dest_ids;
302 struct messaging_context *msg_ctx;
304 } irpc_ClientConnectionObject;
307 setup a context for talking to a irpc server
309 status = irpc.connect("smb_server");
312 PyObject *py_irpc_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
314 struct event_context *ev;
315 const char *kwnames[] = { "server", "own_id", "messaging_path", NULL };
317 const char *messaging_path = NULL;
318 PyObject *own_id = Py_None;
319 irpc_ClientConnectionObject *ret;
321 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|Oz:connect",
322 discard_const_p(char *, kwnames), &server, &own_id, &messaging_path)) {
326 ret = PyObject_New(irpc_ClientConnectionObject, &irpc_ClientConnectionType);
330 ret->mem_ctx = talloc_new(NULL);
332 ret->server_name = server;
334 ev = s4_event_context_init(ret->mem_ctx);
336 if (messaging_path == NULL) {
337 messaging_path = lp_messaging_path(ret, global_loadparm);
339 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
342 if (own_id != Py_None) {
343 struct server_id server_id;
345 if (!server_id_from_py(own_id, &server_id))
348 ret->msg_ctx = messaging_init(ret->mem_ctx,
351 py_iconv_convenience(ret->mem_ctx),
354 ret->msg_ctx = messaging_client_init(ret->mem_ctx,
356 py_iconv_convenience(ret->mem_ctx),
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);
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);
372 return (PyObject *)ret;
378 struct irpc_request **reqs;
382 py_data_unpack_fn unpack_fn;
386 static PyObject *irpc_result_next(irpc_ResultObject *iterator)
390 if (iterator->current >= iterator->count) {
391 PyErr_SetString(PyExc_StopIteration, "No more results");
395 status = irpc_call_recv(iterator->reqs[iterator->current]);
397 if (!NT_STATUS_IS_OK(status)) {
398 PyErr_SetNTSTATUS(status);
402 return iterator->unpack_fn(iterator->reqs[iterator->current-1]->r);
405 static PyObject *irpc_result_len(irpc_ResultObject *self)
407 return PyLong_FromLong(self->count);
410 static PyMethodDef irpc_result_methods[] = {
411 { "__len__", (PyCFunction)irpc_result_len, METH_NOARGS,
412 "Number of elements returned"},
416 static void irpc_result_dealloc(PyObject *self)
418 talloc_free(((irpc_ResultObject *)self)->mem_ctx);
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,
433 static PyObject *py_irpc_call(irpc_ClientConnectionObject *p, struct PyNdrRpcMethodDef *method_def, PyObject *args, PyObject *kwargs)
436 struct irpc_request **reqs;
439 TALLOC_CTX *mem_ctx = talloc_new(NULL);
440 irpc_ResultObject *ret;
442 /* allocate the C structure */
443 ptr = talloc_zero_size(mem_ctx, method_def->table->calls[method_def->opnum].struct_size);
445 status = NT_STATUS_NO_MEMORY;
449 /* convert the mpr object into a C structure */
450 if (!method_def->pack_in_data(args, kwargs, ptr)) {
451 talloc_free(mem_ctx);
455 for (count=0;p->dest_ids[count].id;count++) /* noop */ ;
457 /* we need to make a call per server */
458 reqs = talloc_array(mem_ctx, struct irpc_request *, count);
460 status = NT_STATUS_NO_MEMORY;
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;
472 talloc_steal(reqs, reqs[i]);
475 ret = PyObject_New(irpc_ResultObject, &irpc_ResultIteratorType);
476 ret->mem_ctx = mem_ctx;
480 ret->unpack_fn = method_def->unpack_out_data;
482 return (PyObject *)ret;
484 talloc_free(mem_ctx);
485 PyErr_SetNTSTATUS(status);
489 static PyObject *py_irpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs)
491 irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self;
492 struct PyNdrRpcMethodDef *md = wrapped;
494 return py_irpc_call(iface, md, args, kwargs);
497 static void py_irpc_dealloc(PyObject *self)
499 irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self;
500 talloc_free(iface->mem_ctx);
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."
516 static bool irpc_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds)
519 for (i = 0; mds[i].name; i++) {
521 struct wrapperbase *wb = calloc(sizeof(struct wrapperbase), 1);
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);
528 ret = PyDescr_NewWrapper(ifacetype, wb, discard_const_p(void, &mds[i]));
530 PyDict_SetItemString(ifacetype->tp_dict, mds[i].name,
537 void initmessaging(void)
539 extern void initirpc(void);
542 if (PyType_Ready(&irpc_ClientConnectionType) < 0)
545 if (PyType_Ready(&messaging_Type) < 0)
548 if (PyType_Ready(&irpc_ResultIteratorType) < 0)
551 if (!irpc_AddNdrRpcMethods(&irpc_ClientConnectionType, py_ndr_irpc_methods))
554 mod = Py_InitModule3("messaging", NULL, "Internal RPC");
560 Py_INCREF((PyObject *)&irpc_ClientConnectionType);
561 PyModule_AddObject(mod, "ClientConnection", (PyObject *)&irpc_ClientConnectionType);
563 Py_INCREF((PyObject *)&messaging_Type);
564 PyModule_AddObject(mod, "Messaging", (PyObject *)&messaging_Type);