s4/net_drs: 'net drs' utility initial creation
[Samba/nascimento.git] / source4 / lib / messaging / pymessaging.c
blob1f3b687e6fab296fe57111177529f2a84c788640
1 /*
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 <Python.h>
23 #include "includes.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"
32 #include "param/pyparam.h"
34 PyAPI_DATA(PyTypeObject) messaging_Type;
35 PyAPI_DATA(PyTypeObject) irpc_ClientConnectionType;
37 /* FIXME: This prototype should be in py_irpc.h, or shared otherwise */
38 extern const struct PyNdrRpcMethodDef py_ndr_irpc_methods[];
40 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
42 if (!PyTuple_Check(object)) {
43 PyErr_SetString(PyExc_ValueError, "Expected tuple");
44 return false;
47 if (PyTuple_Size(object) == 3) {
48 return PyArg_ParseTuple(object, "iii", &server_id->id, &server_id->id2, &server_id->node);
49 } else {
50 int id, id2;
51 if (!PyArg_ParseTuple(object, "ii", &id, &id2))
52 return false;
53 *server_id = cluster_id(id, id2);
54 return true;
58 typedef struct {
59 PyObject_HEAD
60 TALLOC_CTX *mem_ctx;
61 struct messaging_context *msg_ctx;
62 } messaging_Object;
64 PyObject *py_messaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
66 struct tevent_context *ev;
67 const char *kwnames[] = { "own_id", "messaging_path", NULL };
68 PyObject *own_id = Py_None;
69 const char *messaging_path = NULL;
70 messaging_Object *ret;
72 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oz:connect",
73 discard_const_p(char *, kwnames), &own_id, &messaging_path)) {
74 return NULL;
77 ret = PyObject_New(messaging_Object, &messaging_Type);
78 if (ret == NULL)
79 return NULL;
81 ret->mem_ctx = talloc_new(NULL);
83 ev = s4_event_context_init(ret->mem_ctx);
85 if (messaging_path == NULL) {
86 messaging_path = lp_messaging_path(ret->mem_ctx,
87 py_default_loadparm_context(ret->mem_ctx));
88 } else {
89 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
92 if (own_id != Py_None) {
93 struct server_id server_id;
95 if (!server_id_from_py(own_id, &server_id))
96 return NULL;
98 ret->msg_ctx = messaging_init(ret->mem_ctx,
99 messaging_path,
100 server_id,
101 py_iconv_convenience(ret->mem_ctx),
102 ev);
103 } else {
104 ret->msg_ctx = messaging_client_init(ret->mem_ctx,
105 messaging_path,
106 py_iconv_convenience(ret->mem_ctx),
107 ev);
110 if (ret->msg_ctx == NULL) {
111 PyErr_SetString(PyExc_RuntimeError, "messaging_connect unable to create a messaging context");
112 talloc_free(ret->mem_ctx);
113 return NULL;
116 return (PyObject *)ret;
119 static void py_messaging_dealloc(PyObject *self)
121 messaging_Object *iface = (messaging_Object *)self;
122 talloc_free(iface->msg_ctx);
123 PyObject_Del(self);
126 static PyObject *py_messaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
128 messaging_Object *iface = (messaging_Object *)self;
129 uint32_t msg_type;
130 DATA_BLOB data;
131 PyObject *target;
132 NTSTATUS status;
133 struct server_id server;
134 const char *kwnames[] = { "target", "msg_type", "data", NULL };
135 int length;
137 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#:send",
138 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
140 return NULL;
143 data.length = length;
145 if (!server_id_from_py(target, &server))
146 return NULL;
148 status = messaging_send(iface->msg_ctx, server, msg_type, &data);
149 if (NT_STATUS_IS_ERR(status)) {
150 PyErr_SetNTSTATUS(status);
151 return NULL;
154 Py_RETURN_NONE;
157 static void py_msg_callback_wrapper(struct messaging_context *msg, void *private_data,
158 uint32_t msg_type,
159 struct server_id server_id, DATA_BLOB *data)
161 PyObject *callback = (PyObject *)private_data;
163 PyObject_CallFunction(callback, discard_const_p(char, "i(iii)s#"), msg_type,
164 server_id.id, server_id.id2, server_id.node,
165 data->data, data->length);
168 static PyObject *py_messaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
170 messaging_Object *iface = (messaging_Object *)self;
171 int msg_type = -1;
172 PyObject *callback;
173 NTSTATUS status;
174 const char *kwnames[] = { "callback", "msg_type", NULL };
176 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register",
177 discard_const_p(char *, kwnames), &callback, &msg_type)) {
178 return NULL;
181 Py_INCREF(callback);
183 if (msg_type == -1) {
184 uint32_t msg_type32 = msg_type;
185 status = messaging_register_tmp(iface->msg_ctx, callback,
186 py_msg_callback_wrapper, &msg_type32);
187 msg_type = msg_type32;
188 } else {
189 status = messaging_register(iface->msg_ctx, callback,
190 msg_type, py_msg_callback_wrapper);
192 if (NT_STATUS_IS_ERR(status)) {
193 PyErr_SetNTSTATUS(status);
194 return NULL;
197 return PyLong_FromLong(msg_type);
200 static PyObject *py_messaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
202 messaging_Object *iface = (messaging_Object *)self;
203 int msg_type = -1;
204 PyObject *callback;
205 const char *kwnames[] = { "callback", "msg_type", NULL };
207 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
208 discard_const_p(char *, kwnames), &callback, &msg_type)) {
209 return NULL;
212 messaging_deregister(iface->msg_ctx, msg_type, callback);
214 Py_DECREF(callback);
216 Py_RETURN_NONE;
219 static PyObject *py_messaging_add_name(PyObject *self, PyObject *args, PyObject *kwargs)
221 messaging_Object *iface = (messaging_Object *)self;
222 NTSTATUS status;
223 char *name;
224 const char *kwnames[] = { "name", NULL };
226 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:add_name",
227 discard_const_p(char *, kwnames), &name)) {
228 return NULL;
231 status = irpc_add_name(iface->msg_ctx, name);
232 if (NT_STATUS_IS_ERR(status)) {
233 PyErr_SetNTSTATUS(status);
234 return NULL;
237 Py_RETURN_NONE;
241 static PyObject *py_messaging_remove_name(PyObject *self, PyObject *args, PyObject *kwargs)
243 messaging_Object *iface = (messaging_Object *)self;
244 char *name;
245 const char *kwnames[] = { "name", NULL };
247 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:remove_name",
248 discard_const_p(char *, kwnames), &name)) {
249 return NULL;
252 irpc_remove_name(iface->msg_ctx, name);
254 Py_RETURN_NONE;
257 static PyMethodDef py_messaging_methods[] = {
258 { "send", (PyCFunction)py_messaging_send, METH_VARARGS|METH_KEYWORDS,
259 "S.send(target, msg_type, data) -> None\nSend a message" },
260 { "register", (PyCFunction)py_messaging_register, METH_VARARGS|METH_KEYWORDS,
261 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
262 { "deregister", (PyCFunction)py_messaging_deregister, METH_VARARGS|METH_KEYWORDS,
263 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
264 { "add_name", (PyCFunction)py_messaging_add_name, METH_VARARGS|METH_KEYWORDS, "S.add_name(name) -> None\nListen on another name" },
265 { "remove_name", (PyCFunction)py_messaging_remove_name, METH_VARARGS|METH_KEYWORDS, "S.remove_name(name) -> None\nStop listening on a name" },
266 { NULL, NULL, 0, NULL }
269 static PyObject *py_messaging_server_id(PyObject *obj, void *closure)
271 messaging_Object *iface = (messaging_Object *)obj;
272 struct server_id server_id = messaging_get_server_id(iface->msg_ctx);
274 return Py_BuildValue("(iii)", server_id.id, server_id.id2,
275 server_id.node);
278 static PyGetSetDef py_messaging_getset[] = {
279 { discard_const_p(char, "server_id"), py_messaging_server_id, NULL,
280 discard_const_p(char, "local server id") },
281 { NULL },
285 PyTypeObject messaging_Type = {
286 PyObject_HEAD_INIT(NULL) 0,
287 .tp_name = "irpc.Messaging",
288 .tp_basicsize = sizeof(messaging_Object),
289 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
290 .tp_new = py_messaging_connect,
291 .tp_dealloc = py_messaging_dealloc,
292 .tp_methods = py_messaging_methods,
293 .tp_getset = py_messaging_getset,
294 .tp_doc = "Messaging(own_id=None, messaging_path=None)\n" \
295 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n" \
296 "If no path is specified, the default path from smb.conf will be used."
301 state of a irpc 'connection'
303 typedef struct {
304 PyObject_HEAD
305 const char *server_name;
306 struct server_id *dest_ids;
307 struct messaging_context *msg_ctx;
308 TALLOC_CTX *mem_ctx;
309 } irpc_ClientConnectionObject;
312 setup a context for talking to a irpc server
313 example:
314 status = irpc.connect("smb_server");
317 PyObject *py_irpc_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
319 struct tevent_context *ev;
320 const char *kwnames[] = { "server", "own_id", "messaging_path", NULL };
321 char *server;
322 const char *messaging_path = NULL;
323 PyObject *own_id = Py_None;
324 irpc_ClientConnectionObject *ret;
326 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|Oz:connect",
327 discard_const_p(char *, kwnames), &server, &own_id, &messaging_path)) {
328 return NULL;
331 ret = PyObject_New(irpc_ClientConnectionObject, &irpc_ClientConnectionType);
332 if (ret == NULL)
333 return NULL;
335 ret->mem_ctx = talloc_new(NULL);
337 ret->server_name = server;
339 ev = s4_event_context_init(ret->mem_ctx);
341 if (messaging_path == NULL) {
342 messaging_path = lp_messaging_path(ret->mem_ctx,
343 py_default_loadparm_context(ret->mem_ctx));
344 } else {
345 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
348 if (own_id != Py_None) {
349 struct server_id server_id;
351 if (!server_id_from_py(own_id, &server_id))
352 return NULL;
354 ret->msg_ctx = messaging_init(ret->mem_ctx,
355 messaging_path,
356 server_id,
357 py_iconv_convenience(ret->mem_ctx),
358 ev);
359 } else {
360 ret->msg_ctx = messaging_client_init(ret->mem_ctx,
361 messaging_path,
362 py_iconv_convenience(ret->mem_ctx),
363 ev);
366 if (ret->msg_ctx == NULL) {
367 PyErr_SetString(PyExc_RuntimeError, "irpc_connect unable to create a messaging context");
368 talloc_free(ret->mem_ctx);
369 return NULL;
372 ret->dest_ids = irpc_servers_byname(ret->msg_ctx, ret->mem_ctx, ret->server_name);
373 if (ret->dest_ids == NULL || ret->dest_ids[0].id == 0) {
374 talloc_free(ret->mem_ctx);
375 PyErr_SetNTSTATUS(NT_STATUS_OBJECT_NAME_NOT_FOUND);
376 return NULL;
377 } else {
378 return (PyObject *)ret;
382 typedef struct {
383 PyObject_HEAD
384 struct irpc_request **reqs;
385 int count;
386 int current;
387 TALLOC_CTX *mem_ctx;
388 py_data_unpack_fn unpack_fn;
389 } irpc_ResultObject;
392 static PyObject *irpc_result_next(irpc_ResultObject *iterator)
394 NTSTATUS status;
396 if (iterator->current >= iterator->count) {
397 PyErr_SetString(PyExc_StopIteration, "No more results");
398 return NULL;
401 status = irpc_call_recv(iterator->reqs[iterator->current]);
402 iterator->current++;
403 if (!NT_STATUS_IS_OK(status)) {
404 PyErr_SetNTSTATUS(status);
405 return NULL;
408 return iterator->unpack_fn(iterator->reqs[iterator->current-1]->r);
411 static PyObject *irpc_result_len(irpc_ResultObject *self)
413 return PyLong_FromLong(self->count);
416 static PyMethodDef irpc_result_methods[] = {
417 { "__len__", (PyCFunction)irpc_result_len, METH_NOARGS,
418 "Number of elements returned"},
419 { NULL }
422 static void irpc_result_dealloc(PyObject *self)
424 talloc_free(((irpc_ResultObject *)self)->mem_ctx);
425 PyObject_Del(self);
428 PyTypeObject irpc_ResultIteratorType = {
429 PyObject_HEAD_INIT(NULL) 0,
430 .tp_name = "irpc.ResultIterator",
431 .tp_basicsize = sizeof(irpc_ResultObject),
432 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
433 .tp_iternext = (iternextfunc)irpc_result_next,
434 .tp_iter = PyObject_SelfIter,
435 .tp_methods = irpc_result_methods,
436 .tp_dealloc = irpc_result_dealloc,
439 static PyObject *py_irpc_call(irpc_ClientConnectionObject *p, struct PyNdrRpcMethodDef *method_def, PyObject *args, PyObject *kwargs)
441 void *ptr;
442 struct irpc_request **reqs;
443 int i, count;
444 NTSTATUS status;
445 TALLOC_CTX *mem_ctx = talloc_new(NULL);
446 irpc_ResultObject *ret;
448 /* allocate the C structure */
449 ptr = talloc_zero_size(mem_ctx, method_def->table->calls[method_def->opnum].struct_size);
450 if (ptr == NULL) {
451 status = NT_STATUS_NO_MEMORY;
452 goto done;
455 /* convert the mpr object into a C structure */
456 if (!method_def->pack_in_data(args, kwargs, ptr)) {
457 talloc_free(mem_ctx);
458 return NULL;
461 for (count=0;p->dest_ids[count].id;count++) /* noop */ ;
463 /* we need to make a call per server */
464 reqs = talloc_array(mem_ctx, struct irpc_request *, count);
465 if (reqs == NULL) {
466 status = NT_STATUS_NO_MEMORY;
467 goto done;
470 /* make the actual calls */
471 for (i=0;i<count;i++) {
472 reqs[i] = irpc_call_send(p->msg_ctx, p->dest_ids[i],
473 method_def->table, method_def->opnum, ptr, ptr);
474 if (reqs[i] == NULL) {
475 status = NT_STATUS_NO_MEMORY;
476 goto done;
478 talloc_steal(reqs, reqs[i]);
481 ret = PyObject_New(irpc_ResultObject, &irpc_ResultIteratorType);
482 ret->mem_ctx = mem_ctx;
483 ret->reqs = reqs;
484 ret->count = count;
485 ret->current = 0;
486 ret->unpack_fn = method_def->unpack_out_data;
488 return (PyObject *)ret;
489 done:
490 talloc_free(mem_ctx);
491 PyErr_SetNTSTATUS(status);
492 return NULL;
495 static PyObject *py_irpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs)
497 irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self;
498 struct PyNdrRpcMethodDef *md = wrapped;
500 return py_irpc_call(iface, md, args, kwargs);
503 static void py_irpc_dealloc(PyObject *self)
505 irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self;
506 talloc_free(iface->mem_ctx);
507 PyObject_Del(self);
510 PyTypeObject irpc_ClientConnectionType = {
511 PyObject_HEAD_INIT(NULL) 0,
512 .tp_name = "irpc.ClientConnection",
513 .tp_basicsize = sizeof(irpc_ClientConnectionObject),
514 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
515 .tp_new = py_irpc_connect,
516 .tp_dealloc = py_irpc_dealloc,
517 .tp_doc = "ClientConnection(server, own_id=None, messaging_path=None)\n" \
518 "Create a new IRPC client connection to communicate with the servers in the specified path.\n" \
519 "If no path is specified, the default path from smb.conf will be used."
522 static bool irpc_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds)
524 int i;
525 for (i = 0; mds[i].name; i++) {
526 PyObject *ret;
527 struct wrapperbase *wb = calloc(sizeof(struct wrapperbase), 1);
529 wb->name = discard_const_p(char, mds[i].name);
530 wb->flags = PyWrapperFlag_KEYWORDS;
531 wb->wrapper = (wrapperfunc)py_irpc_call_wrapper;
532 wb->doc = discard_const_p(char, mds[i].doc);
534 ret = PyDescr_NewWrapper(ifacetype, wb, discard_const_p(void, &mds[i]));
536 PyDict_SetItemString(ifacetype->tp_dict, mds[i].name,
537 (PyObject *)ret);
540 return true;
543 void initmessaging(void)
545 PyObject *mod;
546 PyObject *dep_irpc;
548 dep_irpc = PyImport_ImportModule("samba.dcerpc.irpc");
549 if (dep_irpc == NULL)
550 return;
552 if (PyType_Ready(&irpc_ClientConnectionType) < 0)
553 return;
555 if (PyType_Ready(&messaging_Type) < 0)
556 return;
558 if (PyType_Ready(&irpc_ResultIteratorType) < 0)
559 return;
561 if (!irpc_AddNdrRpcMethods(&irpc_ClientConnectionType, py_ndr_irpc_methods))
562 return;
564 mod = Py_InitModule3("messaging", NULL, "Internal RPC");
565 if (mod == NULL)
566 return;
568 Py_INCREF((PyObject *)&irpc_ClientConnectionType);
569 PyModule_AddObject(mod, "ClientConnection", (PyObject *)&irpc_ClientConnectionType);
571 Py_INCREF((PyObject *)&messaging_Type);
572 PyModule_AddObject(mod, "Messaging", (PyObject *)&messaging_Type);