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/>.
23 #include "../lib/util/python_util.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"
33 PyAPI_DATA(PyTypeObject) messaging_Type;
34 PyAPI_DATA(PyTypeObject) irpc_ClientConnectionType;
36 /* FIXME: This prototype should be in param/pyparam.h */
37 struct loadparm_context *py_default_loadparm_context(TALLOC_CTX *mem_ctx);
39 /* FIXME: This prototype should be in py_irpc.h, or shared otherwise */
40 extern const struct PyNdrRpcMethodDef py_ndr_irpc_methods[];
42 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
44 if (!PyTuple_Check(object)) {
45 PyErr_SetString(PyExc_ValueError, "Expected tuple");
49 if (PyTuple_Size(object) == 3) {
50 return PyArg_ParseTuple(object, "iii", &server_id->id, &server_id->id2, &server_id->node);
53 if (!PyArg_ParseTuple(object, "ii", &id, &id2))
55 *server_id = cluster_id(id, id2);
63 struct messaging_context *msg_ctx;
66 PyObject *py_messaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
68 struct tevent_context *ev;
69 const char *kwnames[] = { "own_id", "messaging_path", NULL };
70 PyObject *own_id = Py_None;
71 const char *messaging_path = NULL;
72 messaging_Object *ret;
74 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oz:connect",
75 discard_const_p(char *, kwnames), &own_id, &messaging_path)) {
79 ret = PyObject_New(messaging_Object, &messaging_Type);
83 ret->mem_ctx = talloc_new(NULL);
85 ev = s4_event_context_init(ret->mem_ctx);
87 if (messaging_path == NULL) {
88 messaging_path = lp_messaging_path(ret->mem_ctx,
89 py_default_loadparm_context(ret->mem_ctx));
91 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
94 if (own_id != Py_None) {
95 struct server_id server_id;
97 if (!server_id_from_py(own_id, &server_id))
100 ret->msg_ctx = messaging_init(ret->mem_ctx,
103 py_iconv_convenience(ret->mem_ctx),
106 ret->msg_ctx = messaging_client_init(ret->mem_ctx,
108 py_iconv_convenience(ret->mem_ctx),
112 if (ret->msg_ctx == NULL) {
113 PyErr_SetString(PyExc_RuntimeError, "messaging_connect unable to create a messaging context");
114 talloc_free(ret->mem_ctx);
118 return (PyObject *)ret;
121 static void py_messaging_dealloc(PyObject *self)
123 messaging_Object *iface = (messaging_Object *)self;
124 talloc_free(iface->msg_ctx);
128 static PyObject *py_messaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
130 messaging_Object *iface = (messaging_Object *)self;
135 struct server_id server;
136 const char *kwnames[] = { "target", "msg_type", "data", NULL };
139 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#|:send",
140 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
144 data.length = length;
146 if (!server_id_from_py(target, &server))
149 status = messaging_send(iface->msg_ctx, server, msg_type, &data);
150 if (NT_STATUS_IS_ERR(status)) {
151 PyErr_SetNTSTATUS(status);
158 static void py_msg_callback_wrapper(struct messaging_context *msg, void *private,
160 struct server_id server_id, DATA_BLOB *data)
162 PyObject *callback = (PyObject *)private;
164 PyObject_CallFunction(callback, discard_const_p(char, "i(iii)s#"), msg_type,
165 server_id.id, server_id.id2, server_id.node,
166 data->data, data->length);
169 static PyObject *py_messaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
171 messaging_Object *iface = (messaging_Object *)self;
175 const char *kwnames[] = { "callback", "msg_type", NULL };
177 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:send",
178 discard_const_p(char *, kwnames), &callback, &msg_type)) {
184 if (msg_type == -1) {
185 uint32_t msg_type32 = msg_type;
186 status = messaging_register_tmp(iface->msg_ctx, callback,
187 py_msg_callback_wrapper, &msg_type32);
188 msg_type = msg_type32;
190 status = messaging_register(iface->msg_ctx, callback,
191 msg_type, py_msg_callback_wrapper);
193 if (NT_STATUS_IS_ERR(status)) {
194 PyErr_SetNTSTATUS(status);
198 return PyLong_FromLong(msg_type);
201 static PyObject *py_messaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
203 messaging_Object *iface = (messaging_Object *)self;
206 const char *kwnames[] = { "callback", "msg_type", NULL };
208 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:send",
209 discard_const_p(char *, kwnames), &callback, &msg_type)) {
213 messaging_deregister(iface->msg_ctx, msg_type, callback);
220 static PyObject *py_messaging_add_name(PyObject *self, PyObject *args, PyObject *kwargs)
222 messaging_Object *iface = (messaging_Object *)self;
225 const char *kwnames[] = { "name", NULL };
227 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:send",
228 discard_const_p(char *, kwnames), &name)) {
232 status = irpc_add_name(iface->msg_ctx, name);
233 if (NT_STATUS_IS_ERR(status)) {
234 PyErr_SetNTSTATUS(status);
242 static PyObject *py_messaging_remove_name(PyObject *self, PyObject *args, PyObject *kwargs)
244 messaging_Object *iface = (messaging_Object *)self;
246 const char *kwnames[] = { "name", NULL };
248 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:send",
249 discard_const_p(char *, kwnames), &name)) {
253 irpc_remove_name(iface->msg_ctx, name);
258 static PyMethodDef py_messaging_methods[] = {
259 { "send", (PyCFunction)py_messaging_send, METH_VARARGS|METH_KEYWORDS,
260 "S.send(target, msg_type, data) -> None\nSend a message" },
261 { "register", (PyCFunction)py_messaging_register, METH_VARARGS|METH_KEYWORDS,
262 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
263 { "deregister", (PyCFunction)py_messaging_deregister, METH_VARARGS|METH_KEYWORDS,
264 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
265 { "add_name", (PyCFunction)py_messaging_add_name, METH_VARARGS|METH_KEYWORDS, "S.add_name(name) -> None\nListen on another name" },
266 { "remove_name", (PyCFunction)py_messaging_remove_name, METH_VARARGS|METH_KEYWORDS, "S.remove_name(name) -> None\nStop listening on a name" },
267 { NULL, NULL, 0, NULL }
270 static PyObject *py_messaging_server_id(PyObject *obj, void *closure)
272 messaging_Object *iface = (messaging_Object *)obj;
273 struct server_id server_id = messaging_get_server_id(iface->msg_ctx);
275 return Py_BuildValue("(iii)", server_id.id, server_id.id2,
279 static PyGetSetDef py_messaging_getset[] = {
280 { discard_const_p(char, "server_id"), py_messaging_server_id, NULL,
281 discard_const_p(char, "local server id") },
286 PyTypeObject messaging_Type = {
287 PyObject_HEAD_INIT(NULL) 0,
288 .tp_name = "irpc.Messaging",
289 .tp_basicsize = sizeof(messaging_Object),
290 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
291 .tp_new = py_messaging_connect,
292 .tp_dealloc = py_messaging_dealloc,
293 .tp_methods = py_messaging_methods,
294 .tp_getset = py_messaging_getset,
295 .tp_doc = "Messaging(own_id=None, messaging_path=None)\n" \
296 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n" \
297 "If no path is specified, the default path from smb.conf will be used."
302 state of a irpc 'connection'
306 const char *server_name;
307 struct server_id *dest_ids;
308 struct messaging_context *msg_ctx;
310 } irpc_ClientConnectionObject;
313 setup a context for talking to a irpc server
315 status = irpc.connect("smb_server");
318 PyObject *py_irpc_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
320 struct tevent_context *ev;
321 const char *kwnames[] = { "server", "own_id", "messaging_path", NULL };
323 const char *messaging_path = NULL;
324 PyObject *own_id = Py_None;
325 irpc_ClientConnectionObject *ret;
327 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|Oz:connect",
328 discard_const_p(char *, kwnames), &server, &own_id, &messaging_path)) {
332 ret = PyObject_New(irpc_ClientConnectionObject, &irpc_ClientConnectionType);
336 ret->mem_ctx = talloc_new(NULL);
338 ret->server_name = server;
340 ev = s4_event_context_init(ret->mem_ctx);
342 if (messaging_path == NULL) {
343 messaging_path = lp_messaging_path(ret->mem_ctx,
344 py_default_loadparm_context(ret->mem_ctx));
346 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
349 if (own_id != Py_None) {
350 struct server_id server_id;
352 if (!server_id_from_py(own_id, &server_id))
355 ret->msg_ctx = messaging_init(ret->mem_ctx,
358 py_iconv_convenience(ret->mem_ctx),
361 ret->msg_ctx = messaging_client_init(ret->mem_ctx,
363 py_iconv_convenience(ret->mem_ctx),
367 if (ret->msg_ctx == NULL) {
368 PyErr_SetString(PyExc_RuntimeError, "irpc_connect unable to create a messaging context");
369 talloc_free(ret->mem_ctx);
373 ret->dest_ids = irpc_servers_byname(ret->msg_ctx, ret->mem_ctx, ret->server_name);
374 if (ret->dest_ids == NULL || ret->dest_ids[0].id == 0) {
375 talloc_free(ret->mem_ctx);
376 PyErr_SetNTSTATUS(NT_STATUS_OBJECT_NAME_NOT_FOUND);
379 return (PyObject *)ret;
385 struct irpc_request **reqs;
389 py_data_unpack_fn unpack_fn;
393 static PyObject *irpc_result_next(irpc_ResultObject *iterator)
397 if (iterator->current >= iterator->count) {
398 PyErr_SetString(PyExc_StopIteration, "No more results");
402 status = irpc_call_recv(iterator->reqs[iterator->current]);
404 if (!NT_STATUS_IS_OK(status)) {
405 PyErr_SetNTSTATUS(status);
409 return iterator->unpack_fn(iterator->reqs[iterator->current-1]->r);
412 static PyObject *irpc_result_len(irpc_ResultObject *self)
414 return PyLong_FromLong(self->count);
417 static PyMethodDef irpc_result_methods[] = {
418 { "__len__", (PyCFunction)irpc_result_len, METH_NOARGS,
419 "Number of elements returned"},
423 static void irpc_result_dealloc(PyObject *self)
425 talloc_free(((irpc_ResultObject *)self)->mem_ctx);
429 PyTypeObject irpc_ResultIteratorType = {
430 PyObject_HEAD_INIT(NULL) 0,
431 .tp_name = "irpc.ResultIterator",
432 .tp_basicsize = sizeof(irpc_ResultObject),
433 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
434 .tp_iternext = (iternextfunc)irpc_result_next,
435 .tp_iter = PyObject_SelfIter,
436 .tp_methods = irpc_result_methods,
437 .tp_dealloc = irpc_result_dealloc,
440 static PyObject *py_irpc_call(irpc_ClientConnectionObject *p, struct PyNdrRpcMethodDef *method_def, PyObject *args, PyObject *kwargs)
443 struct irpc_request **reqs;
446 TALLOC_CTX *mem_ctx = talloc_new(NULL);
447 irpc_ResultObject *ret;
449 /* allocate the C structure */
450 ptr = talloc_zero_size(mem_ctx, method_def->table->calls[method_def->opnum].struct_size);
452 status = NT_STATUS_NO_MEMORY;
456 /* convert the mpr object into a C structure */
457 if (!method_def->pack_in_data(args, kwargs, ptr)) {
458 talloc_free(mem_ctx);
462 for (count=0;p->dest_ids[count].id;count++) /* noop */ ;
464 /* we need to make a call per server */
465 reqs = talloc_array(mem_ctx, struct irpc_request *, count);
467 status = NT_STATUS_NO_MEMORY;
471 /* make the actual calls */
472 for (i=0;i<count;i++) {
473 reqs[i] = irpc_call_send(p->msg_ctx, p->dest_ids[i],
474 method_def->table, method_def->opnum, ptr, ptr);
475 if (reqs[i] == NULL) {
476 status = NT_STATUS_NO_MEMORY;
479 talloc_steal(reqs, reqs[i]);
482 ret = PyObject_New(irpc_ResultObject, &irpc_ResultIteratorType);
483 ret->mem_ctx = mem_ctx;
487 ret->unpack_fn = method_def->unpack_out_data;
489 return (PyObject *)ret;
491 talloc_free(mem_ctx);
492 PyErr_SetNTSTATUS(status);
496 static PyObject *py_irpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs)
498 irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self;
499 struct PyNdrRpcMethodDef *md = wrapped;
501 return py_irpc_call(iface, md, args, kwargs);
504 static void py_irpc_dealloc(PyObject *self)
506 irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self;
507 talloc_free(iface->mem_ctx);
511 PyTypeObject irpc_ClientConnectionType = {
512 PyObject_HEAD_INIT(NULL) 0,
513 .tp_name = "irpc.ClientConnection",
514 .tp_basicsize = sizeof(irpc_ClientConnectionObject),
515 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
516 .tp_new = py_irpc_connect,
517 .tp_dealloc = py_irpc_dealloc,
518 .tp_doc = "ClientConnection(server, own_id=None, messaging_path=None)\n" \
519 "Create a new IRPC client connection to communicate with the servers in the specified path.\n" \
520 "If no path is specified, the default path from smb.conf will be used."
523 static bool irpc_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds)
526 for (i = 0; mds[i].name; i++) {
528 struct wrapperbase *wb = calloc(sizeof(struct wrapperbase), 1);
530 wb->name = discard_const_p(char, mds[i].name);
531 wb->flags = PyWrapperFlag_KEYWORDS;
532 wb->wrapper = (wrapperfunc)py_irpc_call_wrapper;
533 wb->doc = discard_const_p(char, mds[i].doc);
535 ret = PyDescr_NewWrapper(ifacetype, wb, discard_const_p(void, &mds[i]));
537 PyDict_SetItemString(ifacetype->tp_dict, mds[i].name,
544 void initmessaging(void)
549 dep_irpc = PyImport_ImportModule("samba.dcerpc.irpc");
550 if (dep_irpc == NULL)
553 if (PyType_Ready(&irpc_ClientConnectionType) < 0)
556 if (PyType_Ready(&messaging_Type) < 0)
559 if (PyType_Ready(&irpc_ResultIteratorType) < 0)
562 if (!irpc_AddNdrRpcMethods(&irpc_ClientConnectionType, py_ndr_irpc_methods))
565 mod = Py_InitModule3("messaging", NULL, "Internal RPC");
569 Py_INCREF((PyObject *)&irpc_ClientConnectionType);
570 PyModule_AddObject(mod, "ClientConnection", (PyObject *)&irpc_ClientConnectionType);
572 Py_INCREF((PyObject *)&messaging_Type);
573 PyModule_AddObject(mod, "Messaging", (PyObject *)&messaging_Type);