source: vendor/current/source4/lib/messaging/pymessaging.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 10.9 KB
Line 
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 <Python.h>
23#include "includes.h"
24#include "python/modules.h"
25#include "libcli/util/pyerrors.h"
26#include "librpc/rpc/pyrpc_util.h"
27#include "librpc/ndr/libndr.h"
28#include "lib/messaging/messaging.h"
29#include "lib/messaging/irpc.h"
30#include "lib/events/events.h"
31#include "cluster/cluster.h"
32#include "param/param.h"
33#include "param/pyparam.h"
34#include "librpc/rpc/dcerpc.h"
35#include "librpc/gen_ndr/server_id.h"
36#include <pytalloc.h>
37
38void initmessaging(void);
39
40extern PyTypeObject imessaging_Type;
41
42static bool server_id_from_py(PyObject *object, struct server_id *server_id)
43{
44 if (!PyTuple_Check(object)) {
45 if (!py_check_dcerpc_type(object, "samba.dcerpc.server_id", "server_id")) {
46
47 PyErr_SetString(PyExc_ValueError, "Expected tuple or server_id");
48 return false;
49 }
50 *server_id = *pytalloc_get_type(object, struct server_id);
51 return true;
52 }
53 if (PyTuple_Size(object) == 3) {
54 unsigned long long pid;
55 int task_id, vnn;
56
57 if (!PyArg_ParseTuple(object, "KII", &pid, &task_id, &vnn)) {
58 return false;
59 }
60 server_id->pid = pid;
61 server_id->task_id = task_id;
62 server_id->vnn = vnn;
63 return true;
64 } else {
65 unsigned long long pid;
66 int task_id;
67 if (!PyArg_ParseTuple(object, "KI", &pid, &task_id))
68 return false;
69 *server_id = cluster_id(pid, task_id);
70 return true;
71 }
72}
73
74typedef struct {
75 PyObject_HEAD
76 TALLOC_CTX *mem_ctx;
77 struct imessaging_context *msg_ctx;
78} imessaging_Object;
79
80static PyObject *py_imessaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
81{
82 struct tevent_context *ev;
83 const char *kwnames[] = { "own_id", "lp_ctx", NULL };
84 PyObject *own_id = Py_None;
85 PyObject *py_lp_ctx = Py_None;
86 imessaging_Object *ret;
87 struct loadparm_context *lp_ctx;
88
89 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO:connect",
90 discard_const_p(char *, kwnames), &own_id, &py_lp_ctx)) {
91 return NULL;
92 }
93
94 ret = PyObject_New(imessaging_Object, &imessaging_Type);
95 if (ret == NULL)
96 return NULL;
97
98 ret->mem_ctx = talloc_new(NULL);
99
100 lp_ctx = lpcfg_from_py_object(ret->mem_ctx, py_lp_ctx);
101 if (lp_ctx == NULL) {
102 PyErr_SetString(PyExc_RuntimeError, "imessaging_connect unable to interpret loadparm_context");
103 talloc_free(ret->mem_ctx);
104 return NULL;
105 }
106
107 ev = s4_event_context_init(ret->mem_ctx);
108
109 if (own_id != Py_None) {
110 struct server_id server_id;
111
112 if (!server_id_from_py(own_id, &server_id))
113 return NULL;
114
115 ret->msg_ctx = imessaging_init(ret->mem_ctx,
116 lp_ctx,
117 server_id,
118 ev, true);
119 } else {
120 ret->msg_ctx = imessaging_client_init(ret->mem_ctx,
121 lp_ctx,
122 ev);
123 }
124
125 if (ret->msg_ctx == NULL) {
126 PyErr_SetString(PyExc_RuntimeError, "imessaging_connect unable to create a messaging context");
127 talloc_free(ret->mem_ctx);
128 return NULL;
129 }
130
131 return (PyObject *)ret;
132}
133
134static void py_imessaging_dealloc(PyObject *self)
135{
136 imessaging_Object *iface = (imessaging_Object *)self;
137 talloc_free(iface->msg_ctx);
138 self->ob_type->tp_free(self);
139}
140
141static PyObject *py_imessaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
142{
143 imessaging_Object *iface = (imessaging_Object *)self;
144 uint32_t msg_type;
145 DATA_BLOB data;
146 PyObject *target;
147 NTSTATUS status;
148 struct server_id server;
149 const char *kwnames[] = { "target", "msg_type", "data", NULL };
150 Py_ssize_t length;
151
152 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#:send",
153 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
154
155 return NULL;
156 }
157
158 data.length = length;
159
160 if (!server_id_from_py(target, &server))
161 return NULL;
162
163 status = imessaging_send(iface->msg_ctx, server, msg_type, &data);
164 if (NT_STATUS_IS_ERR(status)) {
165 PyErr_SetNTSTATUS(status);
166 return NULL;
167 }
168
169 Py_RETURN_NONE;
170}
171
172static void py_msg_callback_wrapper(struct imessaging_context *msg, void *private_data,
173 uint32_t msg_type,
174 struct server_id server_id, DATA_BLOB *data)
175{
176 PyObject *py_server_id, *callback = (PyObject *)private_data;
177
178 struct server_id *p_server_id = talloc(NULL, struct server_id);
179 if (!p_server_id) {
180 PyErr_NoMemory();
181 return;
182 }
183 *p_server_id = server_id;
184
185 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
186 talloc_unlink(NULL, p_server_id);
187
188 PyObject_CallFunction(callback, discard_const_p(char, "i(O)s#"), msg_type,
189 py_server_id,
190 data->data, data->length);
191}
192
193static PyObject *py_imessaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
194{
195 imessaging_Object *iface = (imessaging_Object *)self;
196 int msg_type = -1;
197 PyObject *callback;
198 NTSTATUS status;
199 const char *kwnames[] = { "callback", "msg_type", NULL };
200
201 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register",
202 discard_const_p(char *, kwnames), &callback, &msg_type)) {
203 return NULL;
204 }
205
206 Py_INCREF(callback);
207
208 if (msg_type == -1) {
209 uint32_t msg_type32 = msg_type;
210 status = imessaging_register_tmp(iface->msg_ctx, callback,
211 py_msg_callback_wrapper, &msg_type32);
212 msg_type = msg_type32;
213 } else {
214 status = imessaging_register(iface->msg_ctx, callback,
215 msg_type, py_msg_callback_wrapper);
216 }
217 if (NT_STATUS_IS_ERR(status)) {
218 PyErr_SetNTSTATUS(status);
219 return NULL;
220 }
221
222 return PyLong_FromLong(msg_type);
223}
224
225static PyObject *py_imessaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
226{
227 imessaging_Object *iface = (imessaging_Object *)self;
228 int msg_type = -1;
229 PyObject *callback;
230 const char *kwnames[] = { "callback", "msg_type", NULL };
231
232 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
233 discard_const_p(char *, kwnames), &callback, &msg_type)) {
234 return NULL;
235 }
236
237 imessaging_deregister(iface->msg_ctx, msg_type, callback);
238
239 Py_DECREF(callback);
240
241 Py_RETURN_NONE;
242}
243
244static PyObject *py_irpc_servers_byname(PyObject *self, PyObject *args, PyObject *kwargs)
245{
246 imessaging_Object *iface = (imessaging_Object *)self;
247 char *server_name;
248 unsigned i, num_ids;
249 struct server_id *ids;
250 PyObject *pylist;
251 TALLOC_CTX *mem_ctx = talloc_new(NULL);
252 NTSTATUS status;
253
254 if (!mem_ctx) {
255 PyErr_NoMemory();
256 return NULL;
257 }
258
259 if (!PyArg_ParseTuple(args, "s", &server_name)) {
260 TALLOC_FREE(mem_ctx);
261 return NULL;
262 }
263
264 status = irpc_servers_byname(iface->msg_ctx, mem_ctx, server_name,
265 &num_ids, &ids);
266 if (!NT_STATUS_IS_OK(status)) {
267 TALLOC_FREE(mem_ctx);
268 PyErr_SetString(PyExc_KeyError, "No such name");
269 return NULL;
270 }
271
272 pylist = PyList_New(num_ids);
273 if (pylist == NULL) {
274 TALLOC_FREE(mem_ctx);
275 PyErr_NoMemory();
276 return NULL;
277 }
278 for (i = 0; i < num_ids; i++) {
279 PyObject *py_server_id;
280 struct server_id *p_server_id = talloc(NULL, struct server_id);
281 if (!p_server_id) {
282 PyErr_NoMemory();
283 return NULL;
284 }
285 *p_server_id = ids[i];
286
287 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
288 if (!py_server_id) {
289 return NULL;
290 }
291 PyList_SetItem(pylist, i, py_server_id);
292 talloc_unlink(NULL, p_server_id);
293 }
294 TALLOC_FREE(mem_ctx);
295 return pylist;
296}
297
298static PyObject *py_irpc_all_servers(PyObject *self, PyObject *args, PyObject *kwargs)
299{
300 imessaging_Object *iface = (imessaging_Object *)self;
301 PyObject *pylist;
302 int i;
303 struct irpc_name_records *records;
304 TALLOC_CTX *mem_ctx = talloc_new(NULL);
305 if (!mem_ctx) {
306 PyErr_NoMemory();
307 return NULL;
308 }
309
310 records = irpc_all_servers(iface->msg_ctx, mem_ctx);
311 if (records == NULL) {
312 return NULL;
313 }
314
315 pylist = PyList_New(records->num_records);
316 if (pylist == NULL) {
317 TALLOC_FREE(mem_ctx);
318 PyErr_NoMemory();
319 return NULL;
320 }
321 for (i = 0; i < records->num_records; i++) {
322 PyObject *py_name_record
323 = py_return_ndr_struct("samba.dcerpc.irpc",
324 "name_record",
325 records->names[i],
326 records->names[i]);
327 if (!py_name_record) {
328 return NULL;
329 }
330 PyList_SetItem(pylist, i,
331 py_name_record);
332 }
333 TALLOC_FREE(mem_ctx);
334 return pylist;
335}
336
337static PyMethodDef py_imessaging_methods[] = {
338 { "send", (PyCFunction)py_imessaging_send, METH_VARARGS|METH_KEYWORDS,
339 "S.send(target, msg_type, data) -> None\nSend a message" },
340 { "register", (PyCFunction)py_imessaging_register, METH_VARARGS|METH_KEYWORDS,
341 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
342 { "deregister", (PyCFunction)py_imessaging_deregister, METH_VARARGS|METH_KEYWORDS,
343 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
344 { "irpc_servers_byname", (PyCFunction)py_irpc_servers_byname, METH_VARARGS,
345 "S.irpc_servers_byname(name) -> list\nGet list of server_id values that are registered for a particular name" },
346 { "irpc_all_servers", (PyCFunction)py_irpc_all_servers, METH_NOARGS,
347 "S.irpc_servers_byname() -> list\nGet list of all registered names and the associated server_id values" },
348 { NULL, NULL, 0, NULL }
349};
350
351static PyObject *py_imessaging_server_id(PyObject *obj, void *closure)
352{
353 imessaging_Object *iface = (imessaging_Object *)obj;
354 PyObject *py_server_id;
355 struct server_id server_id = imessaging_get_server_id(iface->msg_ctx);
356 struct server_id *p_server_id = talloc(NULL, struct server_id);
357 if (!p_server_id) {
358 PyErr_NoMemory();
359 return NULL;
360 }
361 *p_server_id = server_id;
362
363 py_server_id = py_return_ndr_struct("samba.dcerpc.server_id", "server_id", p_server_id, p_server_id);
364 talloc_unlink(NULL, p_server_id);
365
366 return py_server_id;
367}
368
369static PyGetSetDef py_imessaging_getset[] = {
370 { discard_const_p(char, "server_id"), py_imessaging_server_id, NULL,
371 discard_const_p(char, "local server id") },
372 { NULL },
373};
374
375
376PyTypeObject imessaging_Type = {
377 PyObject_HEAD_INIT(NULL) 0,
378 .tp_name = "messaging.Messaging",
379 .tp_basicsize = sizeof(imessaging_Object),
380 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
381 .tp_new = py_imessaging_connect,
382 .tp_dealloc = py_imessaging_dealloc,
383 .tp_methods = py_imessaging_methods,
384 .tp_getset = py_imessaging_getset,
385 .tp_doc = "Messaging(own_id=None)\n" \
386 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n"
387};
388
389void initmessaging(void)
390{
391 PyObject *mod;
392
393 if (PyType_Ready(&imessaging_Type) < 0)
394 return;
395
396 mod = Py_InitModule3("messaging", NULL, "Internal RPC");
397 if (mod == NULL)
398 return;
399
400 Py_INCREF((PyObject *)&imessaging_Type);
401 PyModule_AddObject(mod, "Messaging", (PyObject *)&imessaging_Type);
402}
Note: See TracBrowser for help on using the repository browser.