/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- PyAPI_DATA
- py_messaging_connect
- py_messaging_dealloc
- py_messaging_send
- py_msg_callback_wrapper
- py_messaging_register
- py_messaging_deregister
- py_messaging_add_name
- py_messaging_remove_name
- py_messaging_server_id
- py_irpc_connect
- irpc_result_next
- irpc_result_len
- irpc_result_dealloc
- py_irpc_call
- py_irpc_call_wrapper
- py_irpc_dealloc
- irpc_AddNdrRpcMethods
- initmessaging
1 /*
2 Unix SMB/CIFS implementation.
3 Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
4
5 Based on the equivalent for EJS:
6 Copyright © Andrew Tridgell <tridge@samba.org> 2005
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include <Python.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
33 #ifndef Py_RETURN_NONE
34 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
35 #endif
36
37 PyAPI_DATA(PyTypeObject) messaging_Type;
/* [<][>][^][v][top][bottom][index][help] */
38 PyAPI_DATA(PyTypeObject) irpc_ClientConnectionType;
39
40 /* FIXME: This prototype should be in param/pyparam.h */
41 struct loadparm_context *py_default_loadparm_context(TALLOC_CTX *mem_ctx);
42
43 /* FIXME: This prototype should be in py_irpc.h, or shared otherwise */
44 extern const struct PyNdrRpcMethodDef py_ndr_irpc_methods[];
45
46 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
47 {
48 if (!PyTuple_Check(object)) {
49 PyErr_SetString(PyExc_ValueError, "Expected tuple");
50 return false;
51 }
52
53 if (PyTuple_Size(object) == 3) {
54 return PyArg_ParseTuple(object, "iii", &server_id->id, &server_id->id2, &server_id->node);
55 } else {
56 int id, id2;
57 if (!PyArg_ParseTuple(object, "ii", &id, &id2))
58 return false;
59 *server_id = cluster_id(id, id2);
60 return true;
61 }
62 }
63
64 typedef struct {
65 PyObject_HEAD
66 TALLOC_CTX *mem_ctx;
67 struct messaging_context *msg_ctx;
68 } messaging_Object;
69
70 PyObject *py_messaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
71 {
72 struct tevent_context *ev;
73 const char *kwnames[] = { "own_id", "messaging_path", NULL };
74 PyObject *own_id = Py_None;
75 const char *messaging_path = NULL;
76 messaging_Object *ret;
77
78 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oz:connect",
79 discard_const_p(char *, kwnames), &own_id, &messaging_path)) {
80 return NULL;
81 }
82
83 ret = PyObject_New(messaging_Object, &messaging_Type);
84 if (ret == NULL)
85 return NULL;
86
87 ret->mem_ctx = talloc_new(NULL);
88
89 ev = s4_event_context_init(ret->mem_ctx);
90
91 if (messaging_path == NULL) {
92 messaging_path = lp_messaging_path(ret->mem_ctx,
93 py_default_loadparm_context(ret->mem_ctx));
94 } else {
95 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
96 }
97
98 if (own_id != Py_None) {
99 struct server_id server_id;
100
101 if (!server_id_from_py(own_id, &server_id))
102 return NULL;
103
104 ret->msg_ctx = messaging_init(ret->mem_ctx,
105 messaging_path,
106 server_id,
107 py_iconv_convenience(ret->mem_ctx),
108 ev);
109 } else {
110 ret->msg_ctx = messaging_client_init(ret->mem_ctx,
111 messaging_path,
112 py_iconv_convenience(ret->mem_ctx),
113 ev);
114 }
115
116 if (ret->msg_ctx == NULL) {
117 PyErr_SetString(PyExc_RuntimeError, "messaging_connect unable to create a messaging context");
118 talloc_free(ret->mem_ctx);
119 return NULL;
120 }
121
122 return (PyObject *)ret;
123 }
124
125 static void py_messaging_dealloc(PyObject *self)
/* [<][>][^][v][top][bottom][index][help] */
126 {
127 messaging_Object *iface = (messaging_Object *)self;
128 talloc_free(iface->msg_ctx);
129 PyObject_Del(self);
130 }
131
132 static PyObject *py_messaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
133 {
134 messaging_Object *iface = (messaging_Object *)self;
135 uint32_t msg_type;
136 DATA_BLOB data;
137 PyObject *target;
138 NTSTATUS status;
139 struct server_id server;
140 const char *kwnames[] = { "target", "msg_type", "data", NULL };
141 int length;
142
143 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#|:send",
144 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
145 return NULL;
146 }
147
148 data.length = length;
149
150 if (!server_id_from_py(target, &server))
151 return NULL;
152
153 status = messaging_send(iface->msg_ctx, server, msg_type, &data);
154 if (NT_STATUS_IS_ERR(status)) {
155 PyErr_SetNTSTATUS(status);
156 return NULL;
157 }
158
159 Py_RETURN_NONE;
160 }
161
162 static void py_msg_callback_wrapper(struct messaging_context *msg, void *private_data,
/* [<][>][^][v][top][bottom][index][help] */
163 uint32_t msg_type,
164 struct server_id server_id, DATA_BLOB *data)
165 {
166 PyObject *callback = (PyObject *)private_data;
167
168 PyObject_CallFunction(callback, discard_const_p(char, "i(iii)s#"), msg_type,
169 server_id.id, server_id.id2, server_id.node,
170 data->data, data->length);
171 }
172
173 static PyObject *py_messaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
174 {
175 messaging_Object *iface = (messaging_Object *)self;
176 int msg_type = -1;
177 PyObject *callback;
178 NTSTATUS status;
179 const char *kwnames[] = { "callback", "msg_type", NULL };
180
181 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:send",
182 discard_const_p(char *, kwnames), &callback, &msg_type)) {
183 return NULL;
184 }
185
186 Py_INCREF(callback);
187
188 if (msg_type == -1) {
189 uint32_t msg_type32 = msg_type;
190 status = messaging_register_tmp(iface->msg_ctx, callback,
191 py_msg_callback_wrapper, &msg_type32);
192 msg_type = msg_type32;
193 } else {
194 status = messaging_register(iface->msg_ctx, callback,
195 msg_type, py_msg_callback_wrapper);
196 }
197 if (NT_STATUS_IS_ERR(status)) {
198 PyErr_SetNTSTATUS(status);
199 return NULL;
200 }
201
202 return PyLong_FromLong(msg_type);
203 }
204
205 static PyObject *py_messaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
206 {
207 messaging_Object *iface = (messaging_Object *)self;
208 int msg_type = -1;
209 PyObject *callback;
210 const char *kwnames[] = { "callback", "msg_type", NULL };
211
212 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:send",
213 discard_const_p(char *, kwnames), &callback, &msg_type)) {
214 return NULL;
215 }
216
217 messaging_deregister(iface->msg_ctx, msg_type, callback);
218
219 Py_DECREF(callback);
220
221 Py_RETURN_NONE;
222 }
223
224 static PyObject *py_messaging_add_name(PyObject *self, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
225 {
226 messaging_Object *iface = (messaging_Object *)self;
227 NTSTATUS status;
228 char *name;
229 const char *kwnames[] = { "name", NULL };
230
231 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:send",
232 discard_const_p(char *, kwnames), &name)) {
233 return NULL;
234 }
235
236 status = irpc_add_name(iface->msg_ctx, name);
237 if (NT_STATUS_IS_ERR(status)) {
238 PyErr_SetNTSTATUS(status);
239 return NULL;
240 }
241
242 Py_RETURN_NONE;
243 }
244
245
246 static PyObject *py_messaging_remove_name(PyObject *self, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
247 {
248 messaging_Object *iface = (messaging_Object *)self;
249 char *name;
250 const char *kwnames[] = { "name", NULL };
251
252 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|:send",
253 discard_const_p(char *, kwnames), &name)) {
254 return NULL;
255 }
256
257 irpc_remove_name(iface->msg_ctx, name);
258
259 Py_RETURN_NONE;
260 }
261
262 static PyMethodDef py_messaging_methods[] = {
263 { "send", (PyCFunction)py_messaging_send, METH_VARARGS|METH_KEYWORDS,
264 "S.send(target, msg_type, data) -> None\nSend a message" },
265 { "register", (PyCFunction)py_messaging_register, METH_VARARGS|METH_KEYWORDS,
266 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
267 { "deregister", (PyCFunction)py_messaging_deregister, METH_VARARGS|METH_KEYWORDS,
268 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
269 { "add_name", (PyCFunction)py_messaging_add_name, METH_VARARGS|METH_KEYWORDS, "S.add_name(name) -> None\nListen on another name" },
270 { "remove_name", (PyCFunction)py_messaging_remove_name, METH_VARARGS|METH_KEYWORDS, "S.remove_name(name) -> None\nStop listening on a name" },
271 { NULL, NULL, 0, NULL }
272 };
273
274 static PyObject *py_messaging_server_id(PyObject *obj, void *closure)
/* [<][>][^][v][top][bottom][index][help] */
275 {
276 messaging_Object *iface = (messaging_Object *)obj;
277 struct server_id server_id = messaging_get_server_id(iface->msg_ctx);
278
279 return Py_BuildValue("(iii)", server_id.id, server_id.id2,
280 server_id.node);
281 }
282
283 static PyGetSetDef py_messaging_getset[] = {
284 { discard_const_p(char, "server_id"), py_messaging_server_id, NULL,
285 discard_const_p(char, "local server id") },
286 { NULL },
287 };
288
289
290 PyTypeObject messaging_Type = {
291 PyObject_HEAD_INIT(NULL) 0,
292 .tp_name = "irpc.Messaging",
293 .tp_basicsize = sizeof(messaging_Object),
294 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
295 .tp_new = py_messaging_connect,
296 .tp_dealloc = py_messaging_dealloc,
297 .tp_methods = py_messaging_methods,
298 .tp_getset = py_messaging_getset,
299 .tp_doc = "Messaging(own_id=None, messaging_path=None)\n" \
300 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n" \
301 "If no path is specified, the default path from smb.conf will be used."
302 };
303
304
305 /*
306 state of a irpc 'connection'
307 */
308 typedef struct {
309 PyObject_HEAD
310 const char *server_name;
311 struct server_id *dest_ids;
312 struct messaging_context *msg_ctx;
313 TALLOC_CTX *mem_ctx;
314 } irpc_ClientConnectionObject;
315
316 /*
317 setup a context for talking to a irpc server
318 example:
319 status = irpc.connect("smb_server");
320 */
321
322 PyObject *py_irpc_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
323 {
324 struct tevent_context *ev;
325 const char *kwnames[] = { "server", "own_id", "messaging_path", NULL };
326 char *server;
327 const char *messaging_path = NULL;
328 PyObject *own_id = Py_None;
329 irpc_ClientConnectionObject *ret;
330
331 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|Oz:connect",
332 discard_const_p(char *, kwnames), &server, &own_id, &messaging_path)) {
333 return NULL;
334 }
335
336 ret = PyObject_New(irpc_ClientConnectionObject, &irpc_ClientConnectionType);
337 if (ret == NULL)
338 return NULL;
339
340 ret->mem_ctx = talloc_new(NULL);
341
342 ret->server_name = server;
343
344 ev = s4_event_context_init(ret->mem_ctx);
345
346 if (messaging_path == NULL) {
347 messaging_path = lp_messaging_path(ret->mem_ctx,
348 py_default_loadparm_context(ret->mem_ctx));
349 } else {
350 messaging_path = talloc_strdup(ret->mem_ctx, messaging_path);
351 }
352
353 if (own_id != Py_None) {
354 struct server_id server_id;
355
356 if (!server_id_from_py(own_id, &server_id))
357 return NULL;
358
359 ret->msg_ctx = messaging_init(ret->mem_ctx,
360 messaging_path,
361 server_id,
362 py_iconv_convenience(ret->mem_ctx),
363 ev);
364 } else {
365 ret->msg_ctx = messaging_client_init(ret->mem_ctx,
366 messaging_path,
367 py_iconv_convenience(ret->mem_ctx),
368 ev);
369 }
370
371 if (ret->msg_ctx == NULL) {
372 PyErr_SetString(PyExc_RuntimeError, "irpc_connect unable to create a messaging context");
373 talloc_free(ret->mem_ctx);
374 return NULL;
375 }
376
377 ret->dest_ids = irpc_servers_byname(ret->msg_ctx, ret->mem_ctx, ret->server_name);
378 if (ret->dest_ids == NULL || ret->dest_ids[0].id == 0) {
379 talloc_free(ret->mem_ctx);
380 PyErr_SetNTSTATUS(NT_STATUS_OBJECT_NAME_NOT_FOUND);
381 return NULL;
382 } else {
383 return (PyObject *)ret;
384 }
385 }
386
387 typedef struct {
388 PyObject_HEAD
389 struct irpc_request **reqs;
390 int count;
391 int current;
392 TALLOC_CTX *mem_ctx;
393 py_data_unpack_fn unpack_fn;
394 } irpc_ResultObject;
395
396
397 static PyObject *irpc_result_next(irpc_ResultObject *iterator)
/* [<][>][^][v][top][bottom][index][help] */
398 {
399 NTSTATUS status;
400
401 if (iterator->current >= iterator->count) {
402 PyErr_SetString(PyExc_StopIteration, "No more results");
403 return NULL;
404 }
405
406 status = irpc_call_recv(iterator->reqs[iterator->current]);
407 iterator->current++;
408 if (!NT_STATUS_IS_OK(status)) {
409 PyErr_SetNTSTATUS(status);
410 return NULL;
411 }
412
413 return iterator->unpack_fn(iterator->reqs[iterator->current-1]->r);
414 }
415
416 static PyObject *irpc_result_len(irpc_ResultObject *self)
/* [<][>][^][v][top][bottom][index][help] */
417 {
418 return PyLong_FromLong(self->count);
419 }
420
421 static PyMethodDef irpc_result_methods[] = {
422 { "__len__", (PyCFunction)irpc_result_len, METH_NOARGS,
423 "Number of elements returned"},
424 { NULL }
425 };
426
427 static void irpc_result_dealloc(PyObject *self)
/* [<][>][^][v][top][bottom][index][help] */
428 {
429 talloc_free(((irpc_ResultObject *)self)->mem_ctx);
430 PyObject_Del(self);
431 }
432
433 PyTypeObject irpc_ResultIteratorType = {
434 PyObject_HEAD_INIT(NULL) 0,
435 .tp_name = "irpc.ResultIterator",
436 .tp_basicsize = sizeof(irpc_ResultObject),
437 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
438 .tp_iternext = (iternextfunc)irpc_result_next,
439 .tp_iter = PyObject_SelfIter,
440 .tp_methods = irpc_result_methods,
441 .tp_dealloc = irpc_result_dealloc,
442 };
443
444 static PyObject *py_irpc_call(irpc_ClientConnectionObject *p, struct PyNdrRpcMethodDef *method_def, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
445 {
446 void *ptr;
447 struct irpc_request **reqs;
448 int i, count;
449 NTSTATUS status;
450 TALLOC_CTX *mem_ctx = talloc_new(NULL);
451 irpc_ResultObject *ret;
452
453 /* allocate the C structure */
454 ptr = talloc_zero_size(mem_ctx, method_def->table->calls[method_def->opnum].struct_size);
455 if (ptr == NULL) {
456 status = NT_STATUS_NO_MEMORY;
457 goto done;
458 }
459
460 /* convert the mpr object into a C structure */
461 if (!method_def->pack_in_data(args, kwargs, ptr)) {
462 talloc_free(mem_ctx);
463 return NULL;
464 }
465
466 for (count=0;p->dest_ids[count].id;count++) /* noop */ ;
467
468 /* we need to make a call per server */
469 reqs = talloc_array(mem_ctx, struct irpc_request *, count);
470 if (reqs == NULL) {
471 status = NT_STATUS_NO_MEMORY;
472 goto done;
473 }
474
475 /* make the actual calls */
476 for (i=0;i<count;i++) {
477 reqs[i] = irpc_call_send(p->msg_ctx, p->dest_ids[i],
478 method_def->table, method_def->opnum, ptr, ptr);
479 if (reqs[i] == NULL) {
480 status = NT_STATUS_NO_MEMORY;
481 goto done;
482 }
483 talloc_steal(reqs, reqs[i]);
484 }
485
486 ret = PyObject_New(irpc_ResultObject, &irpc_ResultIteratorType);
487 ret->mem_ctx = mem_ctx;
488 ret->reqs = reqs;
489 ret->count = count;
490 ret->current = 0;
491 ret->unpack_fn = method_def->unpack_out_data;
492
493 return (PyObject *)ret;
494 done:
495 talloc_free(mem_ctx);
496 PyErr_SetNTSTATUS(status);
497 return NULL;
498 }
499
500 static PyObject *py_irpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
501 {
502 irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self;
503 struct PyNdrRpcMethodDef *md = wrapped;
504
505 return py_irpc_call(iface, md, args, kwargs);
506 }
507
508 static void py_irpc_dealloc(PyObject *self)
/* [<][>][^][v][top][bottom][index][help] */
509 {
510 irpc_ClientConnectionObject *iface = (irpc_ClientConnectionObject *)self;
511 talloc_free(iface->mem_ctx);
512 PyObject_Del(self);
513 }
514
515 PyTypeObject irpc_ClientConnectionType = {
516 PyObject_HEAD_INIT(NULL) 0,
517 .tp_name = "irpc.ClientConnection",
518 .tp_basicsize = sizeof(irpc_ClientConnectionObject),
519 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
520 .tp_new = py_irpc_connect,
521 .tp_dealloc = py_irpc_dealloc,
522 .tp_doc = "ClientConnection(server, own_id=None, messaging_path=None)\n" \
523 "Create a new IRPC client connection to communicate with the servers in the specified path.\n" \
524 "If no path is specified, the default path from smb.conf will be used."
525 };
526
527 static bool irpc_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds)
/* [<][>][^][v][top][bottom][index][help] */
528 {
529 int i;
530 for (i = 0; mds[i].name; i++) {
531 PyObject *ret;
532 struct wrapperbase *wb = calloc(sizeof(struct wrapperbase), 1);
533
534 wb->name = discard_const_p(char, mds[i].name);
535 wb->flags = PyWrapperFlag_KEYWORDS;
536 wb->wrapper = (wrapperfunc)py_irpc_call_wrapper;
537 wb->doc = discard_const_p(char, mds[i].doc);
538
539 ret = PyDescr_NewWrapper(ifacetype, wb, discard_const_p(void, &mds[i]));
540
541 PyDict_SetItemString(ifacetype->tp_dict, mds[i].name,
542 (PyObject *)ret);
543 }
544
545 return true;
546 }
547
548 void initmessaging(void)
/* [<][>][^][v][top][bottom][index][help] */
549 {
550 PyObject *mod;
551 PyObject *dep_irpc;
552
553 dep_irpc = PyImport_ImportModule("samba.dcerpc.irpc");
554 if (dep_irpc == NULL)
555 return;
556
557 if (PyType_Ready(&irpc_ClientConnectionType) < 0)
558 return;
559
560 if (PyType_Ready(&messaging_Type) < 0)
561 return;
562
563 if (PyType_Ready(&irpc_ResultIteratorType) < 0)
564 return;
565
566 if (!irpc_AddNdrRpcMethods(&irpc_ClientConnectionType, py_ndr_irpc_methods))
567 return;
568
569 mod = Py_InitModule3("messaging", NULL, "Internal RPC");
570 if (mod == NULL)
571 return;
572
573 Py_INCREF((PyObject *)&irpc_ClientConnectionType);
574 PyModule_AddObject(mod, "ClientConnection", (PyObject *)&irpc_ClientConnectionType);
575
576 Py_INCREF((PyObject *)&messaging_Type);
577 PyModule_AddObject(mod, "Messaging", (PyObject *)&messaging_Type);
578 }