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 "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");
47 if (PyTuple_Size(object
) == 3) {
48 return PyArg_ParseTuple(object
, "iii", &server_id
->id
, &server_id
->id2
, &server_id
->node
);
51 if (!PyArg_ParseTuple(object
, "ii", &id
, &id2
))
53 *server_id
= cluster_id(id
, id2
);
61 struct messaging_context
*msg_ctx
;
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
)) {
77 ret
= PyObject_New(messaging_Object
, &messaging_Type
);
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
));
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
))
98 ret
->msg_ctx
= messaging_init(ret
->mem_ctx
,
101 py_iconv_convenience(ret
->mem_ctx
),
104 ret
->msg_ctx
= messaging_client_init(ret
->mem_ctx
,
106 py_iconv_convenience(ret
->mem_ctx
),
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
);
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
);
126 static PyObject
*py_messaging_send(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
128 messaging_Object
*iface
= (messaging_Object
*)self
;
133 struct server_id server
;
134 const char *kwnames
[] = { "target", "msg_type", "data", NULL
};
137 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "Ois#:send",
138 discard_const_p(char *, kwnames
), &target
, &msg_type
, &data
.data
, &length
)) {
143 data
.length
= length
;
145 if (!server_id_from_py(target
, &server
))
148 status
= messaging_send(iface
->msg_ctx
, server
, msg_type
, &data
);
149 if (NT_STATUS_IS_ERR(status
)) {
150 PyErr_SetNTSTATUS(status
);
157 static void py_msg_callback_wrapper(struct messaging_context
*msg
, void *private_data
,
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
;
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
)) {
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
;
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
);
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
;
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
)) {
212 messaging_deregister(iface
->msg_ctx
, msg_type
, callback
);
219 static PyObject
*py_messaging_add_name(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
221 messaging_Object
*iface
= (messaging_Object
*)self
;
224 const char *kwnames
[] = { "name", NULL
};
226 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|:add_name",
227 discard_const_p(char *, kwnames
), &name
)) {
231 status
= irpc_add_name(iface
->msg_ctx
, name
);
232 if (NT_STATUS_IS_ERR(status
)) {
233 PyErr_SetNTSTATUS(status
);
241 static PyObject
*py_messaging_remove_name(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
243 messaging_Object
*iface
= (messaging_Object
*)self
;
245 const char *kwnames
[] = { "name", NULL
};
247 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|:remove_name",
248 discard_const_p(char *, kwnames
), &name
)) {
252 irpc_remove_name(iface
->msg_ctx
, name
);
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
,
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") },
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'
305 const char *server_name
;
306 struct server_id
*dest_ids
;
307 struct messaging_context
*msg_ctx
;
309 } irpc_ClientConnectionObject
;
312 setup a context for talking to a irpc server
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
};
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
)) {
331 ret
= PyObject_New(irpc_ClientConnectionObject
, &irpc_ClientConnectionType
);
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
));
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
))
354 ret
->msg_ctx
= messaging_init(ret
->mem_ctx
,
357 py_iconv_convenience(ret
->mem_ctx
),
360 ret
->msg_ctx
= messaging_client_init(ret
->mem_ctx
,
362 py_iconv_convenience(ret
->mem_ctx
),
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
);
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
);
378 return (PyObject
*)ret
;
384 struct irpc_request
**reqs
;
388 py_data_unpack_fn unpack_fn
;
392 static PyObject
*irpc_result_next(irpc_ResultObject
*iterator
)
396 if (iterator
->current
>= iterator
->count
) {
397 PyErr_SetString(PyExc_StopIteration
, "No more results");
401 status
= irpc_call_recv(iterator
->reqs
[iterator
->current
]);
403 if (!NT_STATUS_IS_OK(status
)) {
404 PyErr_SetNTSTATUS(status
);
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"},
422 static void irpc_result_dealloc(PyObject
*self
)
424 talloc_free(((irpc_ResultObject
*)self
)->mem_ctx
);
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
)
442 struct irpc_request
**reqs
;
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
);
451 status
= NT_STATUS_NO_MEMORY
;
455 /* convert the mpr object into a C structure */
456 if (!method_def
->pack_in_data(args
, kwargs
, ptr
)) {
457 talloc_free(mem_ctx
);
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
);
466 status
= NT_STATUS_NO_MEMORY
;
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
;
478 talloc_steal(reqs
, reqs
[i
]);
481 ret
= PyObject_New(irpc_ResultObject
, &irpc_ResultIteratorType
);
482 ret
->mem_ctx
= mem_ctx
;
486 ret
->unpack_fn
= method_def
->unpack_out_data
;
488 return (PyObject
*)ret
;
490 talloc_free(mem_ctx
);
491 PyErr_SetNTSTATUS(status
);
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
);
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
)
525 for (i
= 0; mds
[i
].name
; i
++) {
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
,
543 void initmessaging(void)
548 dep_irpc
= PyImport_ImportModule("samba.dcerpc.irpc");
549 if (dep_irpc
== NULL
)
552 if (PyType_Ready(&irpc_ClientConnectionType
) < 0)
555 if (PyType_Ready(&messaging_Type
) < 0)
558 if (PyType_Ready(&irpc_ResultIteratorType
) < 0)
561 if (!irpc_AddNdrRpcMethods(&irpc_ClientConnectionType
, py_ndr_irpc_methods
))
564 mod
= Py_InitModule3("messaging", NULL
, "Internal RPC");
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
);