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 "libcli/util/pyerrors.h"
25 #include "librpc/rpc/pyrpc.h"
26 #include "lib/messaging/irpc.h"
27 #include "lib/messaging/messaging.h"
28 #include "lib/events/events.h"
29 #include "cluster/cluster.h"
30 #include "param/param.h"
31 #include "librpc/gen_ndr/py_irpc.h"
33 PyAPI_DATA(PyTypeObject) messaging_Type;
34 PyAPI_DATA(PyTypeObject) irpc_ClientConnectionType;
36 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
38 if (!PyTuple_Check(object)) {
39 PyErr_SetString(PyExc_ValueError, "Expected tuple");
43 if (PyTuple_Size(object) == 3) {
44 return PyArg_ParseTuple(object, "iii", &server_id->id, &server_id->id2, &server_id->node);
47 if (!PyArg_ParseTuple(object, "ii", &id, &id2))
49 *server_id = cluster_id(id, id2);
57 struct messaging_context *msg_ctx;
60 PyObject *py_messaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
62 struct event_context *ev;
63 const char *kwnames[] = { "own_id", "messaging_path", NULL };
64 PyObject *own_id = Py_None;
65 const char *messaging_path = NULL;
66 messaging_Object *ret;
68 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oz:connect",
69 discard_const_p(char *, kwnames), &own_id, &messaging_path)) {
73 ret = PyObject_New(messaging_Object, &messaging_Type);
77 ret->mem_ctx = talloc_new(NULL);
79 ev = s4_event_context_init(ret->mem_ctx);
81 if (messaging_path == NULL) {
82 messaging_path = lp_messaging_path(ret, global_loadparm);
84 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
87 if (own_id != Py_None) {
88 struct server_id server_id;
90 if (!server_id_from_py(own_id, &server_id))
93 ret->msg_ctx = messaging_init(ret->mem_ctx,
96 lp_iconv_convenience(global_loadparm),
99 ret->msg_ctx = messaging_client_init(ret->mem_ctx,
101 lp_iconv_convenience(global_loadparm),
105 if (ret->msg_ctx == NULL) {
106 PyErr_SetString(PyExc_RuntimeError, "messaging_connect unable to create a messaging context");
107 talloc_free(ret->mem_ctx);
111 return (PyObject *)ret;
114 static void py_messaging_dealloc(PyObject *self)
116 messaging_Object *iface = (messaging_Object *)self;
117 talloc_free(iface->msg_ctx);
121 static PyObject *py_messaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
123 messaging_Object *iface = (messaging_Object *)self;
128 struct server_id server;
129 const char *kwnames[] = { "target", "msg_type", "data", NULL };
132 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#|:send",
133 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
137 data.length = length;
139 if (!server_id_from_py(target, &server))
142 status = messaging_send(iface->msg_ctx, server, msg_type, &data);
143 if (NT_STATUS_IS_ERR(status)) {
144 PyErr_SetNTSTATUS(status);
151 static void py_msg_callback_wrapper(struct messaging_context *msg, void *private,
153 struct server_id server_id, DATA_BLOB *data)
155 PyObject *callback = (PyObject *)private;
157 PyObject_CallFunction(callback, discard_const_p(char, "i(iii)s#"), msg_type,
158 server_id.id, server_id.id2, server_id.node,
159 data->data, data->length);
162 static PyObject *py_messaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
164 messaging_Object *iface = (messaging_Object *)self;
168 const char *kwnames[] = { "callback", "msg_type", NULL };
170 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:send",
171 discard_const_p(char *, kwnames), &callback, &msg_type)) {
177 if (msg_type == -1) {
178 uint32_t msg_type32 = msg_type;
179 status = messaging_register_tmp(iface->msg_ctx, callback,
180 py_msg_callback_wrapper, &msg_type32);
181 msg_type = msg_type32;
183 status = messaging_register(iface->msg_ctx, callback,
184 msg_type, py_msg_callback_wrapper);
186 if (NT_STATUS_IS_ERR(status)) {
187 PyErr_SetNTSTATUS(status);
191 return PyLong_FromLong(msg_type);
194 static PyObject *py_messaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
196 messaging_Object *iface = (messaging_Object *)self;
199 const char *kwnames[] = { "callback", "msg_type", NULL };
201 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:send",
202 discard_const_p(char *, kwnames), &callback, &msg_type)) {
206 messaging_deregister(iface->msg_ctx, msg_type, callback);
213 static PyObject *py_messaging_add_name(PyObject *self, PyObject *args, PyObject *kwargs)
215 messaging_Object *iface = (messaging_Object *)self;
218 const char *kwnames[] = { "name", NULL };
220 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:send",
221 discard_const_p(char *, kwnames), &name)) {
225 status = irpc_add_name(iface->msg_ctx, name);
226 if (NT_STATUS_IS_ERR(status)) {
227 PyErr_SetNTSTATUS(status);
235 static PyObject *py_messaging_remove_name(PyObject *self, PyObject *args, PyObject *kwargs)
237 messaging_Object *iface = (messaging_Object *)self;
239 const char *kwnames[] = { "name", NULL };
241 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:send",
242 discard_const_p(char *, kwnames), &name)) {
246 irpc_remove_name(iface->msg_ctx, name);
251 static PyMethodDef py_messaging_methods[] = {
252 { "send", (PyCFunction)py_messaging_send, METH_VARARGS|METH_KEYWORDS,
253 "S.send(target, msg_type, data) -> None\nSend a message" },
254 { "register", (PyCFunction)py_messaging_register, METH_VARARGS|METH_KEYWORDS,
255 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
256 { "deregister", (PyCFunction)py_messaging_deregister, METH_VARARGS|METH_KEYWORDS,
257 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
258 { "add_name", (PyCFunction)py_messaging_add_name, METH_VARARGS|METH_KEYWORDS, "S.add_name(name) -> None\nListen on another name" },
259 { "remove_name", (PyCFunction)py_messaging_remove_name, METH_VARARGS|METH_KEYWORDS, "S.remove_name(name) -> None\nStop listening on a name" },
260 { NULL, NULL, 0, NULL }
263 static PyObject *py_messaging_server_id(PyObject *obj, void *closure)
265 messaging_Object *iface = (messaging_Object *)obj;
266 struct server_id server_id = messaging_get_server_id(iface->msg_ctx);
268 return Py_BuildValue("(iii)", server_id.id, server_id.id2,
272 static PyGetSetDef py_messaging_getset[] = {
273 { discard_const_p(char, "server_id"), py_messaging_server_id, NULL,
274 discard_const_p(char, "local server id") },
279 PyTypeObject messaging_Type = {
280 PyObject_HEAD_INIT(NULL) 0,
281 .tp_name = "irpc.Messaging",
282 .tp_basicsize = sizeof(messaging_Object),
283 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
284 .tp_new = py_messaging_connect,
285 .tp_dealloc = py_messaging_dealloc,
286 .tp_methods = py_messaging_methods,
287 .tp_getset = py_messaging_getset,
288 .tp_doc = "Messaging(own_id=None, messaging_path=None)\n" \
289 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n" \
290 "If no path is specified, the default path from smb.conf will be used."
295 state of a irpc 'connection'
299 const char *server_name;
300 struct server_id *dest_ids;
301 struct messaging_context *msg_ctx;
303 } irpc_ClientConnectionObject;
306 setup a context for talking to a irpc server
308 status = irpc.connect("smb_server");
311 PyObject *py_irpc_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
313 struct event_context *ev;
314 const char *kwnames[] = { "server", "own_id", "messaging_path", NULL };
316 const char *messaging_path = NULL;
317 PyObject *own_id = Py_None;
318 irpc_ClientConnectionObject *ret;
320 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|Oz:connect",
321 discard_const_p(char *, kwnames), &server, &own_id, &messaging_path)) {
325 ret = PyObject_New(irpc_ClientConnectionObject, &irpc_ClientConnectionType);
329 ret->mem_ctx = talloc_new(NULL);
331 ret->server_name = server;
333 ev = s4_event_context_init(ret->mem_ctx);
335 if (messaging_path == NULL) {
336 messaging_path = lp_messaging_path(ret, global_loadparm);
338 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
341 if (own_id != Py_None) {
342 struct server_id server_id;
344 if (!server_id_from_py(own_id, &server_id))
347 ret->msg_ctx = messaging_init(ret->mem_ctx,
350 lp_iconv_convenience(global_loadparm),
353 ret->msg_ctx = messaging_client_init(ret->mem_ctx,
355 lp_iconv_convenience(global_loadparm),
359 if (ret->msg_ctx == NULL) {
360 PyErr_SetString(PyExc_RuntimeError, "irpc_connect unable to create a messaging context");
361 talloc_free(ret->mem_ctx);
365 ret->dest_ids = irpc_servers_byname(ret->msg_ctx, ret->mem_ctx, ret->server_name);
366 if (ret->dest_ids == NULL || ret->dest_ids[0].id == 0) {
367 talloc_free(ret->mem_ctx);
368 PyErr_SetNTSTATUS(NT_STATUS_OBJECT_NAME_NOT_FOUND);
371 return (PyObject *)ret;
377 struct irpc_request **reqs;
381 py_data_unpack_fn unpack_fn;
385 static PyObject *irpc_result_next(irpc_ResultObject *iterator)
389 if (iterator->current >= iterator->count) {
390 PyErr_SetString(PyExc_StopIteration, "No more results");
394 status = irpc_call_recv(iterator->reqs[iterator->current]);
396 if (!NT_STATUS_IS_OK(status)) {
397 PyErr_SetNTSTATUS(status);
401 return iterator->unpack_fn(iterator->reqs[iterator->current-1]->r);
404 static PyObject *irpc_result_len(irpc_ResultObject *self)
406 return PyLong_FromLong(self->count);
409 static PyMethodDef irpc_result_methods[] = {
410 { "__len__", (PyCFunction)irpc_result_len, METH_NOARGS,
411 "Number of elements returned"},
415 static void irpc_result_dealloc(PyObject *self)
417 talloc_free(((irpc_ResultObject *)self)->mem_ctx);
421 PyTypeObject irpc_ResultIteratorType = {
422 PyObject_HEAD_INIT(NULL) 0,
423 .tp_name = "irpc.ResultIterator",
424 .tp_basicsize = sizeof(irpc_ResultObject),
425 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
426 .tp_iternext = (iternextfunc)irpc_result_next,
427 .tp_iter = PyObject_SelfIter,
428 .tp_methods = irpc_result_methods,
429 .tp_dealloc = irpc_result_dealloc,
432 static PyObject *py_irpc_call(irpc_ClientConnectionObject *p, struct PyNdrRpcMethodDef *method_def, PyObject *args, PyObject *kwargs)
435 struct irpc_request **reqs;
438 TALLOC_CTX *mem_ctx = talloc_new(NULL);
439 irpc_ResultObject *ret;
441 /* allocate the C structure */
442 ptr = talloc_zero_size(mem_ctx, method_def->table->calls[method_def->opnum].struct_size);
444 status = NT_STATUS_NO_MEMORY;
448 /* convert the mpr object into a C structure */
449 if (!method_def->pack_in_data(args, kwargs, ptr)) {
450 talloc_free(mem_ctx);
454 for (count=0;p->dest_ids[count].id;count++) /* noop */ ;
456 /* we need to make a call per server */
457 reqs = talloc_array(mem_ctx, struct irpc_request *, count);
459 status = NT_STATUS_NO_MEMORY;
463 /* make the actual calls */
464 for (i=0;i<count;i++) {
465 reqs[i] = irpc_call_send(p->msg_ctx, p->dest_ids[i],
466 method_def->table, method_def->opnum, ptr, ptr);
467 if (reqs[i] == NULL) {
468 status = NT_STATUS_NO_MEMORY;
471 talloc_steal(reqs, reqs[i]);
474 ret = PyObject_New(irpc_ResultObject, &irpc_ResultIteratorType);
475 ret->mem_ctx = mem_ctx;
479 ret->unpack_fn = method_def->unpack_out_data;
481 return (PyObject *)ret;
483 talloc_free(mem_ctx);
484 PyErr_SetNTSTATUS(status);
488 static PyObject *py_irpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs)
490 irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self;
491 struct PyNdrRpcMethodDef *md = wrapped;
493 return py_irpc_call(iface, md, args, kwargs);
496 static void py_irpc_dealloc(PyObject *self)
498 irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self;
499 talloc_free(iface->mem_ctx);
503 PyTypeObject irpc_ClientConnectionType = {
504 PyObject_HEAD_INIT(NULL) 0,
505 .tp_name = "irpc.ClientConnection",
506 .tp_basicsize = sizeof(irpc_ClientConnectionObject),
507 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
508 .tp_new = py_irpc_connect,
509 .tp_dealloc = py_irpc_dealloc,
510 .tp_doc = "ClientConnection(server, own_id=None, messaging_path=None)\n" \
511 "Create a new IRPC client connection to communicate with the servers in the specified path.\n" \
512 "If no path is specified, the default path from smb.conf will be used."
515 static bool irpc_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds)
518 for (i = 0; mds[i].name; i++) {
520 struct wrapperbase *wb = calloc(sizeof(struct wrapperbase), 1);
522 wb->name = discard_const_p(char, mds[i].name);
523 wb->flags = PyWrapperFlag_KEYWORDS;
524 wb->wrapper = (wrapperfunc)py_irpc_call_wrapper;
525 wb->doc = discard_const_p(char, mds[i].doc);
527 ret = PyDescr_NewWrapper(ifacetype, wb, discard_const_p(void, &mds[i]));
529 PyDict_SetItemString(ifacetype->tp_dict, mds[i].name,
536 void initmessaging(void)
538 extern void initirpc(void);
541 if (PyType_Ready(&irpc_ClientConnectionType) < 0)
544 if (PyType_Ready(&messaging_Type) < 0)
547 if (PyType_Ready(&irpc_ResultIteratorType) < 0)
550 if (!irpc_AddNdrRpcMethods(&irpc_ClientConnectionType, py_ndr_irpc_methods))
553 mod = Py_InitModule3("messaging", NULL, "Internal RPC");
559 Py_INCREF((PyObject *)&irpc_ClientConnectionType);
560 PyModule_AddObject(mod, "ClientConnection", (PyObject *)&irpc_ClientConnectionType);
562 Py_INCREF((PyObject *)&messaging_Type);
563 PyModule_AddObject(mod, "Messaging", (PyObject *)&messaging_Type);