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/>.
24 #include "python/py3compat.h"
26 #include "librpc/rpc/pyrpc_util.h"
27 #include "librpc/rpc/dcerpc.h"
28 #include "librpc/rpc/pyrpc.h"
29 #include "param/pyparam.h"
30 #include "auth/credentials/pycredentials.h"
31 #include "lib/events/events.h"
32 #include "lib/messaging/messaging.h"
33 #include "lib/messaging/irpc.h"
35 bool py_check_dcerpc_type(PyObject *obj, const char *module, const char *type_name)
41 mod = PyImport_ImportModule(module);
44 PyErr_Format(PyExc_RuntimeError, "Unable to import %s to check type %s",
49 type = (PyTypeObject *)PyObject_GetAttrString(mod, type_name);
52 PyErr_Format(PyExc_RuntimeError, "Unable to find type %s in module %s",
57 ret = PyObject_TypeCheck(obj, type);
61 PyErr_Format(PyExc_TypeError, "Expected type %s.%s, got %s",
62 module, type_name, Py_TYPE(obj)->tp_name);
68 connect to a IRPC pipe from python
70 static NTSTATUS pyrpc_irpc_connect(TALLOC_CTX *mem_ctx, const char *irpc_server,
71 const struct ndr_interface_table *table,
72 struct tevent_context *event_ctx,
73 struct loadparm_context *lp_ctx,
74 struct dcerpc_binding_handle **binding_handle)
76 struct imessaging_context *msg;
78 msg = imessaging_client_init(mem_ctx, lp_ctx, event_ctx);
79 NT_STATUS_HAVE_NO_MEMORY(msg);
81 *binding_handle = irpc_binding_handle_by_name(mem_ctx, msg, irpc_server, table);
82 if (*binding_handle == NULL) {
84 return NT_STATUS_INVALID_PIPE_STATE;
88 * Note: this allows nested event loops to happen,
89 * but as there's no top level event loop it's not that critical.
91 dcerpc_binding_handle_set_sync_ev(*binding_handle, event_ctx);
96 PyObject *py_dcerpc_interface_init_helper(PyTypeObject *type, PyObject *args, PyObject *kwargs,
97 const struct ndr_interface_table *table)
99 dcerpc_InterfaceObject *ret;
100 const char *binding_string;
101 PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None, *py_basis = Py_None;
103 unsigned int timeout = (unsigned int)-1;
104 const char *kwnames[] = {
105 "binding", "lp_ctx", "credentials", "timeout", "basis_connection", NULL
108 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOIO:samr", discard_const_p(char *, kwnames), &binding_string, &py_lp_ctx, &py_credentials, &timeout, &py_basis)) {
112 status = dcerpc_init();
113 if (!NT_STATUS_IS_OK(status)) {
114 PyErr_SetNTSTATUS(status);
118 ret = PyObject_New(dcerpc_InterfaceObject, type);
120 ret->binding_handle = NULL;
121 ret->mem_ctx = talloc_new(NULL);
122 if (ret->mem_ctx == NULL) {
127 if (strncmp(binding_string, "irpc:", 5) == 0) {
128 struct tevent_context *event_ctx;
129 struct loadparm_context *lp_ctx;
131 event_ctx = s4_event_context_init(ret->mem_ctx);
132 if (event_ctx == NULL) {
133 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
134 TALLOC_FREE(ret->mem_ctx);
138 lp_ctx = lpcfg_from_py_object(event_ctx, py_lp_ctx);
139 if (lp_ctx == NULL) {
140 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
141 TALLOC_FREE(ret->mem_ctx);
145 status = pyrpc_irpc_connect(ret->mem_ctx, binding_string+5, table,
146 event_ctx, lp_ctx, &ret->binding_handle);
147 if (!NT_STATUS_IS_OK(status)) {
148 PyErr_SetNTSTATUS(status);
149 TALLOC_FREE(ret->mem_ctx);
152 } else if (py_basis != Py_None) {
153 struct dcerpc_pipe *base_pipe;
155 PyTypeObject *ClientConnection_Type;
157 py_base = PyImport_ImportModule("samba.dcerpc.base");
158 if (py_base == NULL) {
159 TALLOC_FREE(ret->mem_ctx);
163 ClientConnection_Type = (PyTypeObject *)PyObject_GetAttrString(py_base, "ClientConnection");
164 if (ClientConnection_Type == NULL) {
165 PyErr_SetNone(PyExc_TypeError);
166 TALLOC_FREE(ret->mem_ctx);
170 if (!PyObject_TypeCheck(py_basis, ClientConnection_Type)) {
171 PyErr_SetString(PyExc_TypeError, "basis_connection must be a DCE/RPC connection");
172 TALLOC_FREE(ret->mem_ctx);
176 base_pipe = talloc_reference(ret->mem_ctx,
177 ((dcerpc_InterfaceObject *)py_basis)->pipe);
178 if (base_pipe == NULL) {
180 TALLOC_FREE(ret->mem_ctx);
184 status = dcerpc_secondary_context(base_pipe, &ret->pipe, table);
185 if (!NT_STATUS_IS_OK(status)) {
186 PyErr_SetNTSTATUS(status);
187 TALLOC_FREE(ret->mem_ctx);
191 ret->pipe = talloc_steal(ret->mem_ctx, ret->pipe);
193 struct tevent_context *event_ctx;
194 struct loadparm_context *lp_ctx;
195 struct cli_credentials *credentials;
197 event_ctx = s4_event_context_init(ret->mem_ctx);
198 if (event_ctx == NULL) {
199 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
200 TALLOC_FREE(ret->mem_ctx);
204 lp_ctx = lpcfg_from_py_object(event_ctx, py_lp_ctx);
205 if (lp_ctx == NULL) {
206 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
207 TALLOC_FREE(ret->mem_ctx);
211 credentials = cli_credentials_from_py_object(py_credentials);
212 if (credentials == NULL) {
213 PyErr_SetString(PyExc_TypeError, "Expected credentials");
214 TALLOC_FREE(ret->mem_ctx);
217 status = dcerpc_pipe_connect(ret->mem_ctx, &ret->pipe, binding_string,
218 table, credentials, event_ctx, lp_ctx);
219 if (!NT_STATUS_IS_OK(status)) {
220 PyErr_SetNTSTATUS(status);
221 TALLOC_FREE(ret->mem_ctx);
226 * the event context is cached under the connection,
227 * so let it be a child of it.
229 talloc_steal(ret->pipe->conn, event_ctx);
233 ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
234 ret->binding_handle = ret->pipe->binding_handle;
237 /* reset timeout for the handle */
238 if ((timeout != ((unsigned int)-1)) && (ret->binding_handle != NULL)) {
239 dcerpc_binding_handle_set_timeout(ret->binding_handle, timeout);
242 return (PyObject *)ret;
245 static PyObject *py_dcerpc_run_function(dcerpc_InterfaceObject *iface,
246 const struct PyNdrRpcMethodDef *md,
247 PyObject *args, PyObject *kwargs)
252 PyObject *result = Py_None;
254 if (md->pack_in_data == NULL || md->unpack_out_data == NULL) {
255 PyErr_SetString(PyExc_NotImplementedError, "No marshalling code available yet");
259 mem_ctx = talloc_new(NULL);
260 if (mem_ctx == NULL) {
265 r = talloc_zero_size(mem_ctx, md->table->calls[md->opnum].struct_size);
271 if (!md->pack_in_data(args, kwargs, r)) {
272 talloc_free(mem_ctx);
276 status = md->call(iface->binding_handle, mem_ctx, r);
277 if (!NT_STATUS_IS_OK(status)) {
278 PyErr_SetDCERPCStatus(iface->pipe, status);
279 talloc_free(mem_ctx);
283 result = md->unpack_out_data(r);
285 talloc_free(mem_ctx);
289 static PyObject *py_dcerpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs)
291 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
292 const struct PyNdrRpcMethodDef *md = (const struct PyNdrRpcMethodDef *)wrapped;
294 return py_dcerpc_run_function(iface, md, args, kwargs);
297 bool PyInterface_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds)
300 for (i = 0; mds[i].name; i++) {
302 struct wrapperbase *wb = (struct wrapperbase *)calloc(sizeof(struct wrapperbase), 1);
307 wb->name = discard_const_p(char, mds[i].name);
308 wb->flags = PyWrapperFlag_KEYWORDS;
309 wb->wrapper = (wrapperfunc)py_dcerpc_call_wrapper;
310 wb->doc = discard_const_p(char, mds[i].doc);
312 ret = PyDescr_NewWrapper(ifacetype, wb, discard_const_p(void, &mds[i]));
314 PyDict_SetItemString(ifacetype->tp_dict, mds[i].name,
321 PyObject *py_dcerpc_syntax_init_helper(PyTypeObject *type, PyObject *args, PyObject *kwargs,
322 const struct ndr_syntax_id *syntax)
325 struct ndr_syntax_id *obj;
326 const char *kwnames[] = { NULL };
328 if (!PyArg_ParseTupleAndKeywords(args, kwargs, ":abstract_syntax", discard_const_p(char *, kwnames))) {
332 ret = pytalloc_new(struct ndr_syntax_id, type);
337 obj = (struct ndr_syntax_id *)pytalloc_get_ptr(ret);
343 void PyErr_SetDCERPCStatus(struct dcerpc_pipe *p, NTSTATUS status)
345 if (p && NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
346 status = dcerpc_fault_to_nt_status(p->last_fault_code);
348 PyErr_SetNTSTATUS(status);
353 take a NDR structure that has a type in a python module and return
354 it as a python object
356 r is the NDR structure pointer (a C structure)
358 r_ctx is the context that is a parent of r. It will be referenced by
359 the resulting python object
361 This MUST only be used by objects that are based on pytalloc_Object
362 otherwise the pytalloc_reference_ex() will fail.
364 PyObject *py_return_ndr_struct(const char *module_name, const char *type_name,
365 TALLOC_CTX *r_ctx, void *r)
367 PyTypeObject *py_type;
374 module = PyImport_ImportModule(module_name);
375 if (module == NULL) {
379 py_type = (PyTypeObject *)PyObject_GetAttrString(module, type_name);
380 if (py_type == NULL) {
384 return pytalloc_reference_ex(py_type, r_ctx, r);
387 PyObject *PyString_FromStringOrNULL(const char *str)
392 return PyStr_FromString(str);
395 PyObject *pyrpc_import_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
396 const void *in, const char *typename)
398 PyObject *mem_ctx_obj = NULL;
399 PyObject *in_obj = NULL;
400 PyObject *ret = NULL;
402 mem_ctx_obj = pytalloc_GenericObject_reference(mem_ctx);
403 if (mem_ctx_obj == NULL) {
407 in_obj = pytalloc_GenericObject_reference_ex(mem_ctx, discard_const(in));
408 if (in_obj == NULL) {
409 Py_XDECREF(mem_ctx_obj);
413 ret = PyObject_CallMethod((PyObject *)type,
414 discard_const_p(char, "__import__"),
415 discard_const_p(char, "OiO"),
416 mem_ctx_obj, level, in_obj);
417 Py_XDECREF(mem_ctx_obj);
426 void *pyrpc_export_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
427 PyObject *in, const char *typename)
429 PyObject *mem_ctx_obj = NULL;
430 PyObject *ret_obj = NULL;
433 mem_ctx_obj = pytalloc_GenericObject_reference(mem_ctx);
434 if (mem_ctx_obj == NULL) {
438 ret_obj = PyObject_CallMethod((PyObject *)type,
439 discard_const_p(char, "__export__"),
440 discard_const_p(char, "OiO"),
441 mem_ctx_obj, level, in);
442 Py_XDECREF(mem_ctx_obj);
443 if (ret_obj == NULL) {
447 ret = _pytalloc_get_type(ret_obj, typename);