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 #ifndef Py_RETURN_NONE
35 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
38 PyAPI_DATA(PyTypeObject
) messaging_Type
;
39 PyAPI_DATA(PyTypeObject
) irpc_ClientConnectionType
;
41 /* FIXME: This prototype should be in py_irpc.h, or shared otherwise */
42 extern const struct PyNdrRpcMethodDef py_ndr_irpc_methods
[];
44 static bool server_id_from_py(PyObject
*object
, struct server_id
*server_id
)
46 if (!PyTuple_Check(object
)) {
47 PyErr_SetString(PyExc_ValueError
, "Expected tuple");
51 if (PyTuple_Size(object
) == 3) {
52 return PyArg_ParseTuple(object
, "iii", &server_id
->id
, &server_id
->id2
, &server_id
->node
);
55 if (!PyArg_ParseTuple(object
, "ii", &id
, &id2
))
57 *server_id
= cluster_id(id
, id2
);
65 struct messaging_context
*msg_ctx
;
68 PyObject
*py_messaging_connect(PyTypeObject
*self
, PyObject
*args
, PyObject
*kwargs
)
70 struct tevent_context
*ev
;
71 const char *kwnames
[] = { "own_id", "messaging_path", NULL
};
72 PyObject
*own_id
= Py_None
;
73 const char *messaging_path
= NULL
;
74 messaging_Object
*ret
;
76 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|Oz:connect",
77 discard_const_p(char *, kwnames
), &own_id
, &messaging_path
)) {
81 ret
= PyObject_New(messaging_Object
, &messaging_Type
);
85 ret
->mem_ctx
= talloc_new(NULL
);
87 ev
= s4_event_context_init(ret
->mem_ctx
);
89 if (messaging_path
== NULL
) {
90 messaging_path
= lp_messaging_path(ret
->mem_ctx
,
91 py_default_loadparm_context(ret
->mem_ctx
));
93 messaging_path
= talloc_strdup(ret
->mem_ctx
, messaging_path
);
96 if (own_id
!= Py_None
) {
97 struct server_id server_id
;
99 if (!server_id_from_py(own_id
, &server_id
))
102 ret
->msg_ctx
= messaging_init(ret
->mem_ctx
,
105 py_iconv_convenience(ret
->mem_ctx
),
108 ret
->msg_ctx
= messaging_client_init(ret
->mem_ctx
,
110 py_iconv_convenience(ret
->mem_ctx
),
114 if (ret
->msg_ctx
== NULL
) {
115 PyErr_SetString(PyExc_RuntimeError
, "messaging_connect unable to create a messaging context");
116 talloc_free(ret
->mem_ctx
);
120 return (PyObject
*)ret
;
123 static void py_messaging_dealloc(PyObject
*self
)
125 messaging_Object
*iface
= (messaging_Object
*)self
;
126 talloc_free(iface
->msg_ctx
);
130 static PyObject
*py_messaging_send(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
132 messaging_Object
*iface
= (messaging_Object
*)self
;
137 struct server_id server
;
138 const char *kwnames
[] = { "target", "msg_type", "data", NULL
};
141 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "Ois#:send",
142 discard_const_p(char *, kwnames
), &target
, &msg_type
, &data
.data
, &length
)) {
147 data
.length
= length
;
149 if (!server_id_from_py(target
, &server
))
152 status
= messaging_send(iface
->msg_ctx
, server
, msg_type
, &data
);
153 if (NT_STATUS_IS_ERR(status
)) {
154 PyErr_SetNTSTATUS(status
);
161 static void py_msg_callback_wrapper(struct messaging_context
*msg
, void *private_data
,
163 struct server_id server_id
, DATA_BLOB
*data
)
165 PyObject
*callback
= (PyObject
*)private_data
;
167 PyObject_CallFunction(callback
, discard_const_p(char, "i(iii)s#"), msg_type
,
168 server_id
.id
, server_id
.id2
, server_id
.node
,
169 data
->data
, data
->length
);
172 static PyObject
*py_messaging_register(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
174 messaging_Object
*iface
= (messaging_Object
*)self
;
178 const char *kwnames
[] = { "callback", "msg_type", NULL
};
180 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|i:register",
181 discard_const_p(char *, kwnames
), &callback
, &msg_type
)) {
187 if (msg_type
== -1) {
188 uint32_t msg_type32
= msg_type
;
189 status
= messaging_register_tmp(iface
->msg_ctx
, callback
,
190 py_msg_callback_wrapper
, &msg_type32
);
191 msg_type
= msg_type32
;
193 status
= messaging_register(iface
->msg_ctx
, callback
,
194 msg_type
, py_msg_callback_wrapper
);
196 if (NT_STATUS_IS_ERR(status
)) {
197 PyErr_SetNTSTATUS(status
);
201 return PyLong_FromLong(msg_type
);
204 static PyObject
*py_messaging_deregister(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
206 messaging_Object
*iface
= (messaging_Object
*)self
;
209 const char *kwnames
[] = { "callback", "msg_type", NULL
};
211 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|i:deregister",
212 discard_const_p(char *, kwnames
), &callback
, &msg_type
)) {
216 messaging_deregister(iface
->msg_ctx
, msg_type
, callback
);
223 static PyObject
*py_messaging_add_name(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
225 messaging_Object
*iface
= (messaging_Object
*)self
;
228 const char *kwnames
[] = { "name", NULL
};
230 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|:add_name",
231 discard_const_p(char *, kwnames
), &name
)) {
235 status
= irpc_add_name(iface
->msg_ctx
, name
);
236 if (NT_STATUS_IS_ERR(status
)) {
237 PyErr_SetNTSTATUS(status
);
245 static PyObject
*py_messaging_remove_name(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
247 messaging_Object
*iface
= (messaging_Object
*)self
;
249 const char *kwnames
[] = { "name", NULL
};
251 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|:remove_name",
252 discard_const_p(char *, kwnames
), &name
)) {
256 irpc_remove_name(iface
->msg_ctx
, name
);
261 static PyMethodDef py_messaging_methods
[] = {
262 { "send", (PyCFunction
)py_messaging_send
, METH_VARARGS
|METH_KEYWORDS
,
263 "S.send(target, msg_type, data) -> None\nSend a message" },
264 { "register", (PyCFunction
)py_messaging_register
, METH_VARARGS
|METH_KEYWORDS
,
265 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
266 { "deregister", (PyCFunction
)py_messaging_deregister
, METH_VARARGS
|METH_KEYWORDS
,
267 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
268 { "add_name", (PyCFunction
)py_messaging_add_name
, METH_VARARGS
|METH_KEYWORDS
, "S.add_name(name) -> None\nListen on another name" },
269 { "remove_name", (PyCFunction
)py_messaging_remove_name
, METH_VARARGS
|METH_KEYWORDS
, "S.remove_name(name) -> None\nStop listening on a name" },
270 { NULL
, NULL
, 0, NULL
}
273 static PyObject
*py_messaging_server_id(PyObject
*obj
, void *closure
)
275 messaging_Object
*iface
= (messaging_Object
*)obj
;
276 struct server_id server_id
= messaging_get_server_id(iface
->msg_ctx
);
278 return Py_BuildValue("(iii)", server_id
.id
, server_id
.id2
,
282 static PyGetSetDef py_messaging_getset
[] = {
283 { discard_const_p(char, "server_id"), py_messaging_server_id
, NULL
,
284 discard_const_p(char, "local server id") },
289 PyTypeObject messaging_Type
= {
290 PyObject_HEAD_INIT(NULL
) 0,
291 .tp_name
= "irpc.Messaging",
292 .tp_basicsize
= sizeof(messaging_Object
),
293 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
294 .tp_new
= py_messaging_connect
,
295 .tp_dealloc
= py_messaging_dealloc
,
296 .tp_methods
= py_messaging_methods
,
297 .tp_getset
= py_messaging_getset
,
298 .tp_doc
= "Messaging(own_id=None, messaging_path=None)\n" \
299 "Create a new object that can be used to communicate with the peers in the specified messaging path.\n" \
300 "If no path is specified, the default path from smb.conf will be used."
305 state of a irpc 'connection'
309 const char *server_name
;
310 struct server_id
*dest_ids
;
311 struct messaging_context
*msg_ctx
;
313 } irpc_ClientConnectionObject
;
316 setup a context for talking to a irpc server
318 status = irpc.connect("smb_server");
321 PyObject
*py_irpc_connect(PyTypeObject
*self
, PyObject
*args
, PyObject
*kwargs
)
323 struct tevent_context
*ev
;
324 const char *kwnames
[] = { "server", "own_id", "messaging_path", NULL
};
326 const char *messaging_path
= NULL
;
327 PyObject
*own_id
= Py_None
;
328 irpc_ClientConnectionObject
*ret
;
330 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|Oz:connect",
331 discard_const_p(char *, kwnames
), &server
, &own_id
, &messaging_path
)) {
335 ret
= PyObject_New(irpc_ClientConnectionObject
, &irpc_ClientConnectionType
);
339 ret
->mem_ctx
= talloc_new(NULL
);
341 ret
->server_name
= server
;
343 ev
= s4_event_context_init(ret
->mem_ctx
);
345 if (messaging_path
== NULL
) {
346 messaging_path
= lp_messaging_path(ret
->mem_ctx
,
347 py_default_loadparm_context(ret
->mem_ctx
));
349 messaging_path
= talloc_strdup(ret
->mem_ctx
, messaging_path
);
352 if (own_id
!= Py_None
) {
353 struct server_id server_id
;
355 if (!server_id_from_py(own_id
, &server_id
))
358 ret
->msg_ctx
= messaging_init(ret
->mem_ctx
,
361 py_iconv_convenience(ret
->mem_ctx
),
364 ret
->msg_ctx
= messaging_client_init(ret
->mem_ctx
,
366 py_iconv_convenience(ret
->mem_ctx
),
370 if (ret
->msg_ctx
== NULL
) {
371 PyErr_SetString(PyExc_RuntimeError
, "irpc_connect unable to create a messaging context");
372 talloc_free(ret
->mem_ctx
);
376 ret
->dest_ids
= irpc_servers_byname(ret
->msg_ctx
, ret
->mem_ctx
, ret
->server_name
);
377 if (ret
->dest_ids
== NULL
|| ret
->dest_ids
[0].id
== 0) {
378 talloc_free(ret
->mem_ctx
);
379 PyErr_SetNTSTATUS(NT_STATUS_OBJECT_NAME_NOT_FOUND
);
382 return (PyObject
*)ret
;
388 struct irpc_request
**reqs
;
392 py_data_unpack_fn unpack_fn
;
396 static PyObject
*irpc_result_next(irpc_ResultObject
*iterator
)
400 if (iterator
->current
>= iterator
->count
) {
401 PyErr_SetString(PyExc_StopIteration
, "No more results");
405 status
= irpc_call_recv(iterator
->reqs
[iterator
->current
]);
407 if (!NT_STATUS_IS_OK(status
)) {
408 PyErr_SetNTSTATUS(status
);
412 return iterator
->unpack_fn(iterator
->reqs
[iterator
->current
-1]->r
);
415 static PyObject
*irpc_result_len(irpc_ResultObject
*self
)
417 return PyLong_FromLong(self
->count
);
420 static PyMethodDef irpc_result_methods
[] = {
421 { "__len__", (PyCFunction
)irpc_result_len
, METH_NOARGS
,
422 "Number of elements returned"},
426 static void irpc_result_dealloc(PyObject
*self
)
428 talloc_free(((irpc_ResultObject
*)self
)->mem_ctx
);
432 PyTypeObject irpc_ResultIteratorType
= {
433 PyObject_HEAD_INIT(NULL
) 0,
434 .tp_name
= "irpc.ResultIterator",
435 .tp_basicsize
= sizeof(irpc_ResultObject
),
436 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
437 .tp_iternext
= (iternextfunc
)irpc_result_next
,
438 .tp_iter
= PyObject_SelfIter
,
439 .tp_methods
= irpc_result_methods
,
440 .tp_dealloc
= irpc_result_dealloc
,
443 static PyObject
*py_irpc_call(irpc_ClientConnectionObject
*p
, struct PyNdrRpcMethodDef
*method_def
, PyObject
*args
, PyObject
*kwargs
)
446 struct irpc_request
**reqs
;
449 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
450 irpc_ResultObject
*ret
;
452 /* allocate the C structure */
453 ptr
= talloc_zero_size(mem_ctx
, method_def
->table
->calls
[method_def
->opnum
].struct_size
);
455 status
= NT_STATUS_NO_MEMORY
;
459 /* convert the mpr object into a C structure */
460 if (!method_def
->pack_in_data(args
, kwargs
, ptr
)) {
461 talloc_free(mem_ctx
);
465 for (count
=0;p
->dest_ids
[count
].id
;count
++) /* noop */ ;
467 /* we need to make a call per server */
468 reqs
= talloc_array(mem_ctx
, struct irpc_request
*, count
);
470 status
= NT_STATUS_NO_MEMORY
;
474 /* make the actual calls */
475 for (i
=0;i
<count
;i
++) {
476 reqs
[i
] = irpc_call_send(p
->msg_ctx
, p
->dest_ids
[i
],
477 method_def
->table
, method_def
->opnum
, ptr
, ptr
);
478 if (reqs
[i
] == NULL
) {
479 status
= NT_STATUS_NO_MEMORY
;
482 talloc_steal(reqs
, reqs
[i
]);
485 ret
= PyObject_New(irpc_ResultObject
, &irpc_ResultIteratorType
);
486 ret
->mem_ctx
= mem_ctx
;
490 ret
->unpack_fn
= method_def
->unpack_out_data
;
492 return (PyObject
*)ret
;
494 talloc_free(mem_ctx
);
495 PyErr_SetNTSTATUS(status
);
499 static PyObject
*py_irpc_call_wrapper(PyObject
*self
, PyObject
*args
, void *wrapped
, PyObject
*kwargs
)
501 irpc_ClientConnectionObject
*iface
= (irpc_ClientConnectionObject
*)self
;
502 struct PyNdrRpcMethodDef
*md
= wrapped
;
504 return py_irpc_call(iface
, md
, args
, kwargs
);
507 static void py_irpc_dealloc(PyObject
*self
)
509 irpc_ClientConnectionObject
*iface
= (irpc_ClientConnectionObject
*)self
;
510 talloc_free(iface
->mem_ctx
);
514 PyTypeObject irpc_ClientConnectionType
= {
515 PyObject_HEAD_INIT(NULL
) 0,
516 .tp_name
= "irpc.ClientConnection",
517 .tp_basicsize
= sizeof(irpc_ClientConnectionObject
),
518 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
519 .tp_new
= py_irpc_connect
,
520 .tp_dealloc
= py_irpc_dealloc
,
521 .tp_doc
= "ClientConnection(server, own_id=None, messaging_path=None)\n" \
522 "Create a new IRPC client connection to communicate with the servers in the specified path.\n" \
523 "If no path is specified, the default path from smb.conf will be used."
526 static bool irpc_AddNdrRpcMethods(PyTypeObject
*ifacetype
, const struct PyNdrRpcMethodDef
*mds
)
529 for (i
= 0; mds
[i
].name
; i
++) {
531 struct wrapperbase
*wb
= calloc(sizeof(struct wrapperbase
), 1);
533 wb
->name
= discard_const_p(char, mds
[i
].name
);
534 wb
->flags
= PyWrapperFlag_KEYWORDS
;
535 wb
->wrapper
= (wrapperfunc
)py_irpc_call_wrapper
;
536 wb
->doc
= discard_const_p(char, mds
[i
].doc
);
538 ret
= PyDescr_NewWrapper(ifacetype
, wb
, discard_const_p(void, &mds
[i
]));
540 PyDict_SetItemString(ifacetype
->tp_dict
, mds
[i
].name
,
547 void initmessaging(void)
552 dep_irpc
= PyImport_ImportModule("samba.dcerpc.irpc");
553 if (dep_irpc
== NULL
)
556 if (PyType_Ready(&irpc_ClientConnectionType
) < 0)
559 if (PyType_Ready(&messaging_Type
) < 0)
562 if (PyType_Ready(&irpc_ResultIteratorType
) < 0)
565 if (!irpc_AddNdrRpcMethods(&irpc_ClientConnectionType
, py_ndr_irpc_methods
))
568 mod
= Py_InitModule3("messaging", NULL
, "Internal RPC");
572 Py_INCREF((PyObject
*)&irpc_ClientConnectionType
);
573 PyModule_AddObject(mod
, "ClientConnection", (PyObject
*)&irpc_ClientConnectionType
);
575 Py_INCREF((PyObject
*)&messaging_Type
);
576 PyModule_AddObject(mod
, "Messaging", (PyObject
*)&messaging_Type
);