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 PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None, *py_basis = Py_None;
96 const char *kwnames[] = {
97 "binding", "lp_ctx", "credentials", "basis_connection", NULL
100 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO:samr", discard_const_p(char *, kwnames), &binding_string, &py_lp_ctx, &py_credentials, &py_basis)) {
104 status = dcerpc_init();
105 if (!NT_STATUS_IS_OK(status)) {
106 PyErr_SetNTSTATUS(status);
110 ret = PyObject_New(dcerpc_InterfaceObject, type);
112 ret->binding_handle = NULL;
113 ret->mem_ctx = talloc_new(NULL);
114 if (ret->mem_ctx == NULL) {
119 if (strncmp(binding_string, "irpc:", 5) == 0) {
120 struct tevent_context *event_ctx;
121 struct loadparm_context *lp_ctx;
123 event_ctx = s4_event_context_init(ret->mem_ctx);
124 if (event_ctx == NULL) {
125 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
126 TALLOC_FREE(ret->mem_ctx);
130 lp_ctx = lpcfg_from_py_object(event_ctx, py_lp_ctx);
131 if (lp_ctx == NULL) {
132 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
133 TALLOC_FREE(ret->mem_ctx);
137 status = pyrpc_irpc_connect(ret->mem_ctx, binding_string+5, table,
138 event_ctx, lp_ctx, &ret->binding_handle);
139 if (!NT_STATUS_IS_OK(status)) {
140 PyErr_SetNTSTATUS(status);
141 TALLOC_FREE(ret->mem_ctx);
144 } else if (py_basis != Py_None) {
145 struct dcerpc_pipe *base_pipe;
147 PyTypeObject *ClientConnection_Type;
149 py_base = PyImport_ImportModule("samba.dcerpc.base");
150 if (py_base == NULL) {
151 TALLOC_FREE(ret->mem_ctx);
155 ClientConnection_Type = (PyTypeObject *)PyObject_GetAttrString(py_base, "ClientConnection");
156 if (ClientConnection_Type == NULL) {
157 PyErr_SetNone(PyExc_TypeError);
158 TALLOC_FREE(ret->mem_ctx);
162 if (!PyObject_TypeCheck(py_basis, ClientConnection_Type)) {
163 PyErr_SetString(PyExc_TypeError, "basis_connection must be a DCE/RPC connection");
164 TALLOC_FREE(ret->mem_ctx);
168 base_pipe = talloc_reference(ret->mem_ctx,
169 ((dcerpc_InterfaceObject *)py_basis)->pipe);
170 if (base_pipe == NULL) {
172 TALLOC_FREE(ret->mem_ctx);
176 status = dcerpc_secondary_context(base_pipe, &ret->pipe, table);
177 if (!NT_STATUS_IS_OK(status)) {
178 PyErr_SetNTSTATUS(status);
179 TALLOC_FREE(ret->mem_ctx);
183 ret->pipe = talloc_steal(ret->mem_ctx, ret->pipe);
185 struct tevent_context *event_ctx;
186 struct loadparm_context *lp_ctx;
187 struct cli_credentials *credentials;
189 event_ctx = s4_event_context_init(ret->mem_ctx);
190 if (event_ctx == NULL) {
191 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
192 TALLOC_FREE(ret->mem_ctx);
196 lp_ctx = lpcfg_from_py_object(event_ctx, py_lp_ctx);
197 if (lp_ctx == NULL) {
198 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
199 TALLOC_FREE(ret->mem_ctx);
203 credentials = cli_credentials_from_py_object(py_credentials);
204 if (credentials == NULL) {
205 PyErr_SetString(PyExc_TypeError, "Expected credentials");
206 TALLOC_FREE(ret->mem_ctx);
209 status = dcerpc_pipe_connect(ret->mem_ctx, &ret->pipe, binding_string,
210 table, credentials, event_ctx, lp_ctx);
211 if (!NT_STATUS_IS_OK(status)) {
212 PyErr_SetNTSTATUS(status);
213 TALLOC_FREE(ret->mem_ctx);
218 * the event context is cached under the connection,
219 * so let it be a child of it.
221 talloc_steal(ret->pipe->conn, event_ctx);
225 ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
226 ret->binding_handle = ret->pipe->binding_handle;
228 return (PyObject *)ret;
231 static PyObject *py_dcerpc_run_function(dcerpc_InterfaceObject *iface,
232 const struct PyNdrRpcMethodDef *md,
233 PyObject *args, PyObject *kwargs)
238 PyObject *result = Py_None;
240 if (md->pack_in_data == NULL || md->unpack_out_data == NULL) {
241 PyErr_SetString(PyExc_NotImplementedError, "No marshalling code available yet");
245 mem_ctx = talloc_new(NULL);
246 if (mem_ctx == NULL) {
251 r = talloc_zero_size(mem_ctx, md->table->calls[md->opnum].struct_size);
257 if (!md->pack_in_data(args, kwargs, r)) {
258 talloc_free(mem_ctx);
262 status = md->call(iface->binding_handle, mem_ctx, r);
263 if (!NT_STATUS_IS_OK(status)) {
264 PyErr_SetDCERPCStatus(iface->pipe, status);
265 talloc_free(mem_ctx);
269 result = md->unpack_out_data(r);
271 talloc_free(mem_ctx);
275 static PyObject *py_dcerpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs)
277 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
278 const struct PyNdrRpcMethodDef *md = (const struct PyNdrRpcMethodDef *)wrapped;
280 return py_dcerpc_run_function(iface, md, args, kwargs);
283 bool PyInterface_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds)
286 for (i = 0; mds[i].name; i++) {
288 struct wrapperbase *wb = (struct wrapperbase *)calloc(sizeof(struct wrapperbase), 1);
293 wb->name = discard_const_p(char, mds[i].name);
294 wb->flags = PyWrapperFlag_KEYWORDS;
295 wb->wrapper = (wrapperfunc)py_dcerpc_call_wrapper;
296 wb->doc = discard_const_p(char, mds[i].doc);
298 ret = PyDescr_NewWrapper(ifacetype, wb, discard_const_p(void, &mds[i]));
300 PyDict_SetItemString(ifacetype->tp_dict, mds[i].name,
307 void PyErr_SetDCERPCStatus(struct dcerpc_pipe *p, NTSTATUS status)
309 if (p && NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
310 status = dcerpc_fault_to_nt_status(p->last_fault_code);
312 PyErr_SetNTSTATUS(status);
317 take a NDR structure that has a type in a python module and return
318 it as a python object
320 r is the NDR structure pointer (a C structure)
322 r_ctx is the context that is a parent of r. It will be referenced by
323 the resulting python object
325 PyObject *py_return_ndr_struct(const char *module_name, const char *type_name,
326 TALLOC_CTX *r_ctx, void *r)
328 PyTypeObject *py_type;
335 module = PyImport_ImportModule(module_name);
336 if (module == NULL) {
340 py_type = (PyTypeObject *)PyObject_GetAttrString(module, type_name);
341 if (py_type == NULL) {
345 return pytalloc_reference_ex(py_type, r_ctx, r);
348 PyObject *PyString_FromStringOrNULL(const char *str)
353 return PyString_FromString(str);