s4:pyrpc: fix talloc hierachy in py_dcerpc_interface_init_helper()
[gd/samba-autobuild/.git] / source4 / librpc / rpc / pyrpc_util.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Python interface to DCE/RPC library - utility functions.
5
6    Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
7    Copyright (C) 2010 Andrew Tridgell <tridge@samba.org>
8
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.
13
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.
18
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/>.
21 */
22
23 #include <Python.h>
24 #include "includes.h"
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"
33
34 bool py_check_dcerpc_type(PyObject *obj, const char *module, const char *type_name)
35 {
36         PyObject *mod;
37         PyTypeObject *type;
38         bool ret;
39
40         mod = PyImport_ImportModule(module);
41
42         if (mod == NULL) {
43                 PyErr_Format(PyExc_RuntimeError, "Unable to import %s to check type %s",
44                         module, type_name);
45                 return false;
46         }
47
48         type = (PyTypeObject *)PyObject_GetAttrString(mod, type_name);
49         Py_DECREF(mod);
50         if (type == NULL) {
51                 PyErr_Format(PyExc_RuntimeError, "Unable to find type %s in module %s",
52                         module, type_name);
53                 return false;
54         }
55
56         ret = PyObject_TypeCheck(obj, type);
57         Py_DECREF(type);
58
59         if (!ret)
60                 PyErr_Format(PyExc_TypeError, "Expected type %s.%s, got %s",
61                         module, type_name, Py_TYPE(obj)->tp_name);
62
63         return ret;
64 }
65
66 /*
67   connect to a IRPC pipe from python
68  */
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)
74 {
75         struct imessaging_context *msg;
76
77         msg = imessaging_client_init(mem_ctx, lp_ctx, event_ctx);
78         NT_STATUS_HAVE_NO_MEMORY(msg);
79
80         *binding_handle = irpc_binding_handle_by_name(mem_ctx, msg, irpc_server, table);
81         if (*binding_handle == NULL) {
82                 talloc_free(msg);
83                 return NT_STATUS_INVALID_PIPE_STATE;
84         }
85
86         return NT_STATUS_OK;
87 }
88
89 PyObject *py_dcerpc_interface_init_helper(PyTypeObject *type, PyObject *args, PyObject *kwargs,
90                                           const struct ndr_interface_table *table)
91 {
92         dcerpc_InterfaceObject *ret;
93         const char *binding_string;
94         PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None, *py_basis = Py_None;
95         NTSTATUS status;
96         const char *kwnames[] = {
97                 "binding", "lp_ctx", "credentials", "basis_connection", NULL
98         };
99
100         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|OOO:samr", discard_const_p(char *, kwnames), &binding_string, &py_lp_ctx, &py_credentials, &py_basis)) {
101                 return NULL;
102         }
103
104         status = dcerpc_init();
105         if (!NT_STATUS_IS_OK(status)) {
106                 PyErr_SetNTSTATUS(status);
107                 return NULL;
108         }
109
110         ret = PyObject_New(dcerpc_InterfaceObject, type);
111         ret->pipe = NULL;
112         ret->binding_handle = NULL;
113         ret->mem_ctx = talloc_new(NULL);
114         if (ret->mem_ctx == NULL) {
115                 PyErr_NoMemory();
116                 return NULL;
117         }
118
119         if (strncmp(binding_string, "irpc:", 5) == 0) {
120                 struct tevent_context *event_ctx;
121                 struct loadparm_context *lp_ctx;
122
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);
127                         return NULL;
128                 }
129
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);
134                         return NULL;
135                 }
136
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);
142                         return NULL;
143                 }
144         } else if (py_basis != Py_None) {
145                 struct dcerpc_pipe *base_pipe;
146                 PyObject *py_base;
147                 PyTypeObject *ClientConnection_Type;
148
149                 py_base = PyImport_ImportModule("samba.dcerpc.base");
150                 if (py_base == NULL) {
151                         TALLOC_FREE(ret->mem_ctx);
152                         return NULL;
153                 }
154
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);
159                         return NULL;
160                 }
161
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);
165                         return NULL;
166                 }
167
168                 base_pipe = talloc_reference(ret->mem_ctx,
169                                          ((dcerpc_InterfaceObject *)py_basis)->pipe);
170                 if (base_pipe == NULL) {
171                         PyErr_NoMemory();
172                         TALLOC_FREE(ret->mem_ctx);
173                         return NULL;
174                 }
175
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);
180                         return NULL;
181                 }
182
183                 ret->pipe = talloc_steal(ret->mem_ctx, ret->pipe);
184         } else {
185                 struct tevent_context *event_ctx;
186                 struct loadparm_context *lp_ctx;
187                 struct cli_credentials *credentials;
188
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);
193                         return NULL;
194                 }
195
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);
200                         return NULL;
201                 }
202
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);
207                         return NULL;
208                 }
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);
214                         return NULL;
215                 }
216
217                 /*
218                  * the event context is cached under the connection,
219                  * so let it be a child of it.
220                  */
221                 talloc_steal(ret->pipe->conn, event_ctx);
222         }
223
224         if (ret->pipe) {
225                 ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
226                 ret->binding_handle = ret->pipe->binding_handle;
227         }
228         return (PyObject *)ret;
229 }
230
231 static PyObject *py_dcerpc_run_function(dcerpc_InterfaceObject *iface,
232                                         const struct PyNdrRpcMethodDef *md,
233                                         PyObject *args, PyObject *kwargs)
234 {
235         TALLOC_CTX *mem_ctx;
236         NTSTATUS status;
237         void *r;
238         PyObject *result = Py_None;
239
240         if (md->pack_in_data == NULL || md->unpack_out_data == NULL) {
241                 PyErr_SetString(PyExc_NotImplementedError, "No marshalling code available yet");
242                 return NULL;
243         }
244
245         mem_ctx = talloc_new(NULL);
246         if (mem_ctx == NULL) {
247                 PyErr_NoMemory();
248                 return NULL;
249         }
250
251         r = talloc_zero_size(mem_ctx, md->table->calls[md->opnum].struct_size);
252         if (r == NULL) {
253                 PyErr_NoMemory();
254                 return NULL;
255         }
256
257         if (!md->pack_in_data(args, kwargs, r)) {
258                 talloc_free(mem_ctx);
259                 return NULL;
260         }
261
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);
266                 return NULL;
267         }
268
269         result = md->unpack_out_data(r);
270
271         talloc_free(mem_ctx);
272         return result;
273 }
274
275 static PyObject *py_dcerpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs)
276 {       
277         dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
278         const struct PyNdrRpcMethodDef *md = (const struct PyNdrRpcMethodDef *)wrapped;
279
280         return py_dcerpc_run_function(iface, md, args, kwargs);
281 }
282
283 bool PyInterface_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds)
284 {
285         int i;
286         for (i = 0; mds[i].name; i++) {
287                 PyObject *ret;
288                 struct wrapperbase *wb = (struct wrapperbase *)calloc(sizeof(struct wrapperbase), 1);
289
290                 if (wb == NULL) {
291                         return false;
292                 }
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);
297
298                 ret = PyDescr_NewWrapper(ifacetype, wb, discard_const_p(void, &mds[i]));
299
300                 PyDict_SetItemString(ifacetype->tp_dict, mds[i].name, 
301                                      (PyObject *)ret);
302         }
303
304         return true;
305 }
306
307 void PyErr_SetDCERPCStatus(struct dcerpc_pipe *p, NTSTATUS status)
308 {
309         if (p && NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
310                 status = dcerpc_fault_to_nt_status(p->last_fault_code);
311         }
312         PyErr_SetNTSTATUS(status);
313 }
314
315
316 /*
317   take a NDR structure that has a type in a python module and return
318   it as a python object
319
320   r is the NDR structure pointer (a C structure)
321
322   r_ctx is the context that is a parent of r. It will be referenced by
323   the resulting python object
324  */
325 PyObject *py_return_ndr_struct(const char *module_name, const char *type_name,
326                                TALLOC_CTX *r_ctx, void *r)
327 {
328         PyTypeObject *py_type;
329         PyObject *module;
330
331         if (r == NULL) {
332                 Py_RETURN_NONE;
333         }
334
335         module = PyImport_ImportModule(module_name);
336         if (module == NULL) {
337                 return NULL;
338         }
339
340         py_type = (PyTypeObject *)PyObject_GetAttrString(module, type_name);
341         if (py_type == NULL) {
342                 return NULL;
343         }
344
345         return pytalloc_reference_ex(py_type, r_ctx, r);
346 }
347
348 PyObject *PyString_FromStringOrNULL(const char *str)
349 {
350         if (str == NULL) {
351                 Py_RETURN_NONE;
352         }
353         return PyString_FromString(str);
354 }