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 "scripting/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/events/events.h"
|
---|
30 | #include "cluster/cluster.h"
|
---|
31 | #include "param/param.h"
|
---|
32 | #include "param/pyparam.h"
|
---|
33 | #include "librpc/rpc/dcerpc.h"
|
---|
34 | #include "librpc/gen_ndr/server_id4.h"
|
---|
35 |
|
---|
36 | void initmessaging(void);
|
---|
37 |
|
---|
38 | extern PyTypeObject messaging_Type;
|
---|
39 |
|
---|
40 | static bool server_id_from_py(PyObject *object, struct server_id *server_id)
|
---|
41 | {
|
---|
42 | if (!PyTuple_Check(object)) {
|
---|
43 | PyErr_SetString(PyExc_ValueError, "Expected tuple");
|
---|
44 | return false;
|
---|
45 | }
|
---|
46 |
|
---|
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;
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | typedef struct {
|
---|
59 | PyObject_HEAD
|
---|
60 | TALLOC_CTX *mem_ctx;
|
---|
61 | struct messaging_context *msg_ctx;
|
---|
62 | } messaging_Object;
|
---|
63 |
|
---|
64 | static PyObject *py_messaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
|
---|
65 | {
|
---|
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;
|
---|
71 |
|
---|
72 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oz:connect",
|
---|
73 | discard_const_p(char *, kwnames), &own_id, &messaging_path)) {
|
---|
74 | return NULL;
|
---|
75 | }
|
---|
76 |
|
---|
77 | ret = PyObject_New(messaging_Object, &messaging_Type);
|
---|
78 | if (ret == NULL)
|
---|
79 | return NULL;
|
---|
80 |
|
---|
81 | ret->mem_ctx = talloc_new(NULL);
|
---|
82 |
|
---|
83 | ev = s4_event_context_init(ret->mem_ctx);
|
---|
84 |
|
---|
85 | if (messaging_path == NULL) {
|
---|
86 | messaging_path = lpcfg_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);
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (own_id != Py_None) {
|
---|
93 | struct server_id server_id;
|
---|
94 |
|
---|
95 | if (!server_id_from_py(own_id, &server_id))
|
---|
96 | return NULL;
|
---|
97 |
|
---|
98 | ret->msg_ctx = messaging_init(ret->mem_ctx,
|
---|
99 | messaging_path,
|
---|
100 | server_id,
|
---|
101 | ev);
|
---|
102 | } else {
|
---|
103 | ret->msg_ctx = messaging_client_init(ret->mem_ctx,
|
---|
104 | messaging_path,
|
---|
105 | ev);
|
---|
106 | }
|
---|
107 |
|
---|
108 | if (ret->msg_ctx == NULL) {
|
---|
109 | PyErr_SetString(PyExc_RuntimeError, "messaging_connect unable to create a messaging context");
|
---|
110 | talloc_free(ret->mem_ctx);
|
---|
111 | return NULL;
|
---|
112 | }
|
---|
113 |
|
---|
114 | return (PyObject *)ret;
|
---|
115 | }
|
---|
116 |
|
---|
117 | static void py_messaging_dealloc(PyObject *self)
|
---|
118 | {
|
---|
119 | messaging_Object *iface = (messaging_Object *)self;
|
---|
120 | talloc_free(iface->msg_ctx);
|
---|
121 | self->ob_type->tp_free(self);
|
---|
122 | }
|
---|
123 |
|
---|
124 | static PyObject *py_messaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
|
---|
125 | {
|
---|
126 | messaging_Object *iface = (messaging_Object *)self;
|
---|
127 | uint32_t msg_type;
|
---|
128 | DATA_BLOB data;
|
---|
129 | PyObject *target;
|
---|
130 | NTSTATUS status;
|
---|
131 | struct server_id server;
|
---|
132 | const char *kwnames[] = { "target", "msg_type", "data", NULL };
|
---|
133 | int length;
|
---|
134 |
|
---|
135 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#:send",
|
---|
136 | discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
|
---|
137 |
|
---|
138 | return NULL;
|
---|
139 | }
|
---|
140 |
|
---|
141 | data.length = length;
|
---|
142 |
|
---|
143 | if (!server_id_from_py(target, &server))
|
---|
144 | return NULL;
|
---|
145 |
|
---|
146 | status = messaging_send(iface->msg_ctx, server, msg_type, &data);
|
---|
147 | if (NT_STATUS_IS_ERR(status)) {
|
---|
148 | PyErr_SetNTSTATUS(status);
|
---|
149 | return NULL;
|
---|
150 | }
|
---|
151 |
|
---|
152 | Py_RETURN_NONE;
|
---|
153 | }
|
---|
154 |
|
---|
155 | static void py_msg_callback_wrapper(struct messaging_context *msg, void *private_data,
|
---|
156 | uint32_t msg_type,
|
---|
157 | struct server_id server_id, DATA_BLOB *data)
|
---|
158 | {
|
---|
159 | PyObject *callback = (PyObject *)private_data;
|
---|
160 |
|
---|
161 | PyObject_CallFunction(callback, discard_const_p(char, "i(iii)s#"), msg_type,
|
---|
162 | server_id.id, server_id.id2, server_id.node,
|
---|
163 | data->data, data->length);
|
---|
164 | }
|
---|
165 |
|
---|
166 | static PyObject *py_messaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
|
---|
167 | {
|
---|
168 | messaging_Object *iface = (messaging_Object *)self;
|
---|
169 | int msg_type = -1;
|
---|
170 | PyObject *callback;
|
---|
171 | NTSTATUS status;
|
---|
172 | const char *kwnames[] = { "callback", "msg_type", NULL };
|
---|
173 |
|
---|
174 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register",
|
---|
175 | discard_const_p(char *, kwnames), &callback, &msg_type)) {
|
---|
176 | return NULL;
|
---|
177 | }
|
---|
178 |
|
---|
179 | Py_INCREF(callback);
|
---|
180 |
|
---|
181 | if (msg_type == -1) {
|
---|
182 | uint32_t msg_type32 = msg_type;
|
---|
183 | status = messaging_register_tmp(iface->msg_ctx, callback,
|
---|
184 | py_msg_callback_wrapper, &msg_type32);
|
---|
185 | msg_type = msg_type32;
|
---|
186 | } else {
|
---|
187 | status = messaging_register(iface->msg_ctx, callback,
|
---|
188 | msg_type, py_msg_callback_wrapper);
|
---|
189 | }
|
---|
190 | if (NT_STATUS_IS_ERR(status)) {
|
---|
191 | PyErr_SetNTSTATUS(status);
|
---|
192 | return NULL;
|
---|
193 | }
|
---|
194 |
|
---|
195 | return PyLong_FromLong(msg_type);
|
---|
196 | }
|
---|
197 |
|
---|
198 | static PyObject *py_messaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
|
---|
199 | {
|
---|
200 | messaging_Object *iface = (messaging_Object *)self;
|
---|
201 | int msg_type = -1;
|
---|
202 | PyObject *callback;
|
---|
203 | const char *kwnames[] = { "callback", "msg_type", NULL };
|
---|
204 |
|
---|
205 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
|
---|
206 | discard_const_p(char *, kwnames), &callback, &msg_type)) {
|
---|
207 | return NULL;
|
---|
208 | }
|
---|
209 |
|
---|
210 | messaging_deregister(iface->msg_ctx, msg_type, callback);
|
---|
211 |
|
---|
212 | Py_DECREF(callback);
|
---|
213 |
|
---|
214 | Py_RETURN_NONE;
|
---|
215 | }
|
---|
216 |
|
---|
217 | static PyMethodDef py_messaging_methods[] = {
|
---|
218 | { "send", (PyCFunction)py_messaging_send, METH_VARARGS|METH_KEYWORDS,
|
---|
219 | "S.send(target, msg_type, data) -> None\nSend a message" },
|
---|
220 | { "register", (PyCFunction)py_messaging_register, METH_VARARGS|METH_KEYWORDS,
|
---|
221 | "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
|
---|
222 | { "deregister", (PyCFunction)py_messaging_deregister, METH_VARARGS|METH_KEYWORDS,
|
---|
223 | "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
|
---|
224 | { NULL, NULL, 0, NULL }
|
---|
225 | };
|
---|
226 |
|
---|
227 | static PyObject *py_messaging_server_id(PyObject *obj, void *closure)
|
---|
228 | {
|
---|
229 | messaging_Object *iface = (messaging_Object *)obj;
|
---|
230 | struct server_id server_id = messaging_get_server_id(iface->msg_ctx);
|
---|
231 |
|
---|
232 | return Py_BuildValue("(iii)", server_id.id, server_id.id2,
|
---|
233 | server_id.node);
|
---|
234 | }
|
---|
235 |
|
---|
236 | static PyGetSetDef py_messaging_getset[] = {
|
---|
237 | { discard_const_p(char, "server_id"), py_messaging_server_id, NULL,
|
---|
238 | discard_const_p(char, "local server id") },
|
---|
239 | { NULL },
|
---|
240 | };
|
---|
241 |
|
---|
242 |
|
---|
243 | PyTypeObject messaging_Type = {
|
---|
244 | PyObject_HEAD_INIT(NULL) 0,
|
---|
245 | .tp_name = "messaging.Messaging",
|
---|
246 | .tp_basicsize = sizeof(messaging_Object),
|
---|
247 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
|
---|
248 | .tp_new = py_messaging_connect,
|
---|
249 | .tp_dealloc = py_messaging_dealloc,
|
---|
250 | .tp_methods = py_messaging_methods,
|
---|
251 | .tp_getset = py_messaging_getset,
|
---|
252 | .tp_doc = "Messaging(own_id=None, messaging_path=None)\n" \
|
---|
253 | "Create a new object that can be used to communicate with the peers in the specified messaging path.\n" \
|
---|
254 | "If no path is specified, the default path from smb.conf will be used."
|
---|
255 | };
|
---|
256 |
|
---|
257 | void initmessaging(void)
|
---|
258 | {
|
---|
259 | PyObject *mod;
|
---|
260 |
|
---|
261 | if (PyType_Ready(&messaging_Type) < 0)
|
---|
262 | return;
|
---|
263 |
|
---|
264 | mod = Py_InitModule3("messaging", NULL, "Internal RPC");
|
---|
265 | if (mod == NULL)
|
---|
266 | return;
|
---|
267 |
|
---|
268 | Py_INCREF((PyObject *)&messaging_Type);
|
---|
269 | PyModule_AddObject(mod, "Messaging", (PyObject *)&messaging_Type);
|
---|
270 | }
|
---|