2 Unix SMB/CIFS implementation.
4 Python interface to DCE/RPC library - utility functions.
6 Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
7 Copyright (C) 2010 Andrew Tridgell <tridge@samba.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "librpc/rpc/pyrpc_util.h"
26 #include "librpc/rpc/dcerpc.h"
27 #include "librpc/rpc/pyrpc.h"
28 #include "param/pyparam.h"
29 #include "auth/credentials/pycredentials.h"
30 #include "lib/events/events.h"
31 #include "lib/messaging/messaging.h"
32 #include "lib/messaging/irpc.h"
34 bool py_check_dcerpc_type(PyObject
*obj
, const char *module
, const char *type_name
)
40 mod
= PyImport_ImportModule(module
);
43 PyErr_Format(PyExc_RuntimeError
, "Unable to import %s to check type %s",
48 type
= (PyTypeObject
*)PyObject_GetAttrString(mod
, type_name
);
51 PyErr_Format(PyExc_RuntimeError
, "Unable to find type %s in module %s",
56 ret
= PyObject_TypeCheck(obj
, type
);
60 PyErr_Format(PyExc_TypeError
, "Expected type %s.%s, got %s",
61 module
, type_name
, Py_TYPE(obj
)->tp_name
);
67 connect to a IRPC pipe from python
69 static NTSTATUS
pyrpc_irpc_connect(TALLOC_CTX
*mem_ctx
, const char *irpc_server
,
70 const struct ndr_interface_table
*table
,
71 struct tevent_context
*event_ctx
,
72 struct loadparm_context
*lp_ctx
,
73 struct dcerpc_binding_handle
**binding_handle
)
75 struct imessaging_context
*msg
;
77 msg
= imessaging_client_init(mem_ctx
, lp_ctx
, event_ctx
);
78 NT_STATUS_HAVE_NO_MEMORY(msg
);
80 *binding_handle
= irpc_binding_handle_by_name(mem_ctx
, msg
, irpc_server
, table
);
81 if (*binding_handle
== NULL
) {
83 return NT_STATUS_INVALID_PIPE_STATE
;
89 PyObject
*py_dcerpc_interface_init_helper(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
,
90 const struct ndr_interface_table
*table
)
92 dcerpc_InterfaceObject
*ret
;
93 const char *binding_string
;
94 struct cli_credentials
*credentials
;
95 struct loadparm_context
*lp_ctx
= NULL
;
96 PyObject
*py_lp_ctx
= Py_None
, *py_credentials
= Py_None
, *py_basis
= Py_None
;
97 TALLOC_CTX
*mem_ctx
= NULL
;
98 struct tevent_context
*event_ctx
;
101 const char *kwnames
[] = {
102 "binding", "lp_ctx", "credentials", "basis_connection", NULL
105 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "s|OOO:samr", discard_const_p(char *, kwnames
), &binding_string
, &py_lp_ctx
, &py_credentials
, &py_basis
)) {
109 mem_ctx
= talloc_new(NULL
);
110 if (mem_ctx
== NULL
) {
115 lp_ctx
= lpcfg_from_py_object(mem_ctx
, py_lp_ctx
);
116 if (lp_ctx
== NULL
) {
117 PyErr_SetString(PyExc_TypeError
, "Expected loadparm context");
118 talloc_free(mem_ctx
);
122 status
= dcerpc_init();
123 if (!NT_STATUS_IS_OK(status
)) {
124 PyErr_SetNTSTATUS(status
);
125 talloc_free(mem_ctx
);
129 ret
= PyObject_New(dcerpc_InterfaceObject
, type
);
130 ret
->mem_ctx
= mem_ctx
;
132 event_ctx
= s4_event_context_init(ret
->mem_ctx
);
134 if (strncmp(binding_string
, "irpc:", 5) == 0) {
136 status
= pyrpc_irpc_connect(ret
->mem_ctx
, binding_string
+5, table
,
137 event_ctx
, lp_ctx
, &ret
->binding_handle
);
138 } else if (py_basis
!= Py_None
) {
139 struct dcerpc_pipe
*base_pipe
;
141 PyTypeObject
*ClientConnection_Type
;
143 py_base
= PyImport_ImportModule("samba.dcerpc.base");
144 if (py_base
== NULL
) {
145 talloc_free(mem_ctx
);
149 ClientConnection_Type
= (PyTypeObject
*)PyObject_GetAttrString(py_base
, "ClientConnection");
150 if (ClientConnection_Type
== NULL
) {
151 PyErr_SetNone(PyExc_TypeError
);
152 talloc_free(mem_ctx
);
156 if (!PyObject_TypeCheck(py_basis
, ClientConnection_Type
)) {
157 PyErr_SetString(PyExc_TypeError
, "basis_connection must be a DCE/RPC connection");
158 talloc_free(mem_ctx
);
162 base_pipe
= talloc_reference(mem_ctx
, ((dcerpc_InterfaceObject
*)py_basis
)->pipe
);
164 status
= dcerpc_secondary_context(base_pipe
, &ret
->pipe
, table
);
166 ret
->pipe
= talloc_steal(ret
->mem_ctx
, ret
->pipe
);
168 credentials
= cli_credentials_from_py_object(py_credentials
);
169 if (credentials
== NULL
) {
170 PyErr_SetString(PyExc_TypeError
, "Expected credentials");
171 talloc_free(mem_ctx
);
174 status
= dcerpc_pipe_connect(event_ctx
, &ret
->pipe
, binding_string
,
175 table
, credentials
, event_ctx
, lp_ctx
);
177 if (NT_STATUS_IS_ERR(status
)) {
178 PyErr_SetNTSTATUS(status
);
179 talloc_free(mem_ctx
);
184 ret
->pipe
->conn
->flags
|= DCERPC_NDR_REF_ALLOC
;
185 ret
->binding_handle
= ret
->pipe
->binding_handle
;
187 return (PyObject
*)ret
;
190 static PyObject
*py_dcerpc_run_function(dcerpc_InterfaceObject
*iface
,
191 const struct PyNdrRpcMethodDef
*md
,
192 PyObject
*args
, PyObject
*kwargs
)
197 PyObject
*result
= Py_None
;
199 if (md
->pack_in_data
== NULL
|| md
->unpack_out_data
== NULL
) {
200 PyErr_SetString(PyExc_NotImplementedError
, "No marshalling code available yet");
204 mem_ctx
= talloc_new(NULL
);
205 if (mem_ctx
== NULL
) {
210 r
= talloc_zero_size(mem_ctx
, md
->table
->calls
[md
->opnum
].struct_size
);
216 if (!md
->pack_in_data(args
, kwargs
, r
)) {
217 talloc_free(mem_ctx
);
221 status
= md
->call(iface
->binding_handle
, mem_ctx
, r
);
222 if (!NT_STATUS_IS_OK(status
)) {
223 PyErr_SetDCERPCStatus(iface
->pipe
, status
);
224 talloc_free(mem_ctx
);
228 result
= md
->unpack_out_data(r
);
230 talloc_free(mem_ctx
);
234 static PyObject
*py_dcerpc_call_wrapper(PyObject
*self
, PyObject
*args
, void *wrapped
, PyObject
*kwargs
)
236 dcerpc_InterfaceObject
*iface
= (dcerpc_InterfaceObject
*)self
;
237 const struct PyNdrRpcMethodDef
*md
= (const struct PyNdrRpcMethodDef
*)wrapped
;
239 return py_dcerpc_run_function(iface
, md
, args
, kwargs
);
242 bool PyInterface_AddNdrRpcMethods(PyTypeObject
*ifacetype
, const struct PyNdrRpcMethodDef
*mds
)
245 for (i
= 0; mds
[i
].name
; i
++) {
247 struct wrapperbase
*wb
= (struct wrapperbase
*)calloc(sizeof(struct wrapperbase
), 1);
249 wb
->name
= discard_const_p(char, mds
[i
].name
);
250 wb
->flags
= PyWrapperFlag_KEYWORDS
;
251 wb
->wrapper
= (wrapperfunc
)py_dcerpc_call_wrapper
;
252 wb
->doc
= discard_const_p(char, mds
[i
].doc
);
254 ret
= PyDescr_NewWrapper(ifacetype
, wb
, discard_const_p(void, &mds
[i
]));
256 PyDict_SetItemString(ifacetype
->tp_dict
, mds
[i
].name
,
263 void PyErr_SetDCERPCStatus(struct dcerpc_pipe
*p
, NTSTATUS status
)
265 if (p
&& NT_STATUS_EQUAL(status
, NT_STATUS_NET_WRITE_FAULT
)) {
266 status
= dcerpc_fault_to_nt_status(p
->last_fault_code
);
268 PyErr_SetNTSTATUS(status
);
273 take a NDR structure that has a type in a python module and return
274 it as a python object
276 r is the NDR structure pointer (a C structure)
278 r_ctx is the context that is a parent of r. It will be referenced by
279 the resulting python object
281 PyObject
*py_return_ndr_struct(const char *module_name
, const char *type_name
,
282 TALLOC_CTX
*r_ctx
, void *r
)
284 PyTypeObject
*py_type
;
291 module
= PyImport_ImportModule(module_name
);
292 if (module
== NULL
) {
296 py_type
= (PyTypeObject
*)PyObject_GetAttrString(module
, type_name
);
297 if (py_type
== NULL
) {
301 return pytalloc_reference_ex(py_type
, r_ctx
, r
);
304 PyObject
*PyString_FromStringOrNULL(const char *str
)
309 return PyString_FromString(str
);