Use a custom init function for samba4 that sets a samba4
[Samba.git] / source4 / librpc / rpc / pyrpc.c
bloba59f355d436411732f819969808d59e4777bbac4
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include <Python.h>
22 #include <structmember.h>
23 #include "librpc/rpc/pyrpc.h"
24 #include "librpc/rpc/dcerpc.h"
25 #include "lib/events/events.h"
27 static PyObject *py_dcerpc_run_function(dcerpc_InterfaceObject *iface, struct PyNdrRpcMethodDef *md, PyObject *args, PyObject *kwargs)
29 TALLOC_CTX *mem_ctx;
30 NTSTATUS status;
31 void *r;
32 PyObject *result = Py_None;
34 if (md->pack_in_data == NULL || md->unpack_out_data == NULL) {
35 PyErr_SetString(PyExc_NotImplementedError, "No marshalling code available yet");
36 return NULL;
39 mem_ctx = talloc_new(NULL);
40 if (mem_ctx == NULL)
41 return NULL;
43 r = talloc_zero_size(mem_ctx, md->table->calls[md->opnum].struct_size);
44 if (r == NULL)
45 return NULL;
47 if (!md->pack_in_data(args, kwargs, r)) {
48 talloc_free(mem_ctx);
49 return NULL;
52 status = md->call(iface->pipe, mem_ctx, r);
53 if (NT_STATUS_IS_ERR(status)) {
54 PyErr_SetDCERPCStatus(iface->pipe, status);
55 talloc_free(mem_ctx);
56 return NULL;
59 result = md->unpack_out_data(r);
61 talloc_free(mem_ctx);
62 return result;
65 static PyObject *py_dcerpc_call_wrapper(PyObject *self, PyObject *args, void *wrapped, PyObject *kwargs)
67 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
68 struct PyNdrRpcMethodDef *md = wrapped;
70 return py_dcerpc_run_function(iface, md, args, kwargs);
74 bool PyInterface_AddNdrRpcMethods(PyTypeObject *ifacetype, const struct PyNdrRpcMethodDef *mds)
76 int i;
77 for (i = 0; mds[i].name; i++) {
78 PyObject *ret;
79 struct wrapperbase *wb = calloc(sizeof(struct wrapperbase), 1);
81 wb->name = discard_const_p(char, mds[i].name);
82 wb->flags = PyWrapperFlag_KEYWORDS;
83 wb->wrapper = (wrapperfunc)py_dcerpc_call_wrapper;
84 wb->doc = discard_const_p(char, mds[i].doc);
86 ret = PyDescr_NewWrapper(ifacetype, wb, discard_const_p(void, &mds[i]));
88 PyDict_SetItemString(ifacetype->tp_dict, mds[i].name,
89 (PyObject *)ret);
92 return true;
95 static bool PyString_AsGUID(PyObject *object, struct GUID *uuid)
97 NTSTATUS status;
98 status = GUID_from_string(PyString_AsString(object), uuid);
99 if (NT_STATUS_IS_ERR(status)) {
100 PyErr_SetNTSTATUS(status);
101 return false;
103 return true;
106 static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *syntax_id)
108 ZERO_STRUCTP(syntax_id);
110 if (PyString_Check(object)) {
111 return PyString_AsGUID(object, &syntax_id->uuid);
112 } else if (PyTuple_Check(object)) {
113 if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) {
114 PyErr_SetString(PyExc_ValueError, "Syntax ID tuple has invalid size");
115 return false;
118 if (!PyString_Check(PyTuple_GetItem(object, 0))) {
119 PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple");
120 return false;
123 if (!PyString_AsGUID(PyTuple_GetItem(object, 0), &syntax_id->uuid))
124 return false;
126 if (!PyInt_Check(PyTuple_GetItem(object, 1))) {
127 PyErr_SetString(PyExc_ValueError, "Expected version as second element in tuple");
128 return false;
131 syntax_id->if_version = PyInt_AsLong(PyTuple_GetItem(object, 1));
132 return true;
135 PyErr_SetString(PyExc_TypeError, "Expected UUID or syntax id tuple");
136 return false;
139 static PyObject *py_iface_server_name(PyObject *obj, void *closure)
141 const char *server_name;
142 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
144 server_name = dcerpc_server_name(iface->pipe);
145 if (server_name == NULL)
146 return Py_None;
148 return PyString_FromString(server_name);
151 static PyObject *py_ndr_syntax_id(struct ndr_syntax_id *syntax_id)
153 PyObject *ret;
154 char *uuid_str;
156 uuid_str = GUID_string(NULL, &syntax_id->uuid);
157 if (uuid_str == NULL)
158 return NULL;
160 ret = Py_BuildValue("(s,i)", uuid_str, syntax_id->if_version);
162 talloc_free(uuid_str);
164 return ret;
167 static PyObject *py_iface_abstract_syntax(PyObject *obj, void *closure)
169 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
171 return py_ndr_syntax_id(&iface->pipe->syntax);
174 static PyObject *py_iface_transfer_syntax(PyObject *obj, void *closure)
176 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
178 return py_ndr_syntax_id(&iface->pipe->transfer_syntax);
181 static PyGetSetDef dcerpc_interface_getsetters[] = {
182 { discard_const_p(char, "server_name"), py_iface_server_name, NULL,
183 discard_const_p(char, "name of the server, if connected over SMB") },
184 { discard_const_p(char, "abstract_syntax"), py_iface_abstract_syntax, NULL,
185 discard_const_p(char, "syntax id of the abstract syntax") },
186 { discard_const_p(char, "transfer_syntax"), py_iface_transfer_syntax, NULL,
187 discard_const_p(char, "syntax id of the transfersyntax") },
188 { NULL }
191 static PyMemberDef dcerpc_interface_members[] = {
192 { discard_const_p(char, "request_timeout"), T_INT,
193 offsetof(struct dcerpc_pipe, request_timeout), 0,
194 discard_const_p(char, "request timeout, in seconds") },
195 { NULL }
198 void PyErr_SetDCERPCStatus(struct dcerpc_pipe *p, NTSTATUS status)
200 if (p != NULL && NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
201 const char *errstr = dcerpc_errstr(NULL, p->last_fault_code);
202 PyErr_SetObject(PyExc_RuntimeError,
203 Py_BuildValue("(i,s)", p->last_fault_code,
204 errstr));
205 } else {
206 PyErr_SetNTSTATUS(status);
210 static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwargs)
212 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
213 int opnum;
214 DATA_BLOB data_in, data_out;
215 NTSTATUS status;
216 char *in_data;
217 int in_length;
218 PyObject *ret;
219 PyObject *object = NULL;
220 struct GUID object_guid;
221 TALLOC_CTX *mem_ctx = talloc_new(NULL);
222 const char *kwnames[] = { "opnum", "data", "object", NULL };
224 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "is#|O:request",
225 discard_const_p(char *, kwnames), &opnum, &in_data, &in_length, &object)) {
226 return NULL;
229 data_in.data = (uint8_t *)talloc_memdup(mem_ctx, in_data, in_length);
230 data_in.length = in_length;
232 ZERO_STRUCT(data_out);
234 if (object != NULL && !PyString_AsGUID(object, &object_guid)) {
235 return NULL;
238 status = dcerpc_request(iface->pipe, object?&object_guid:NULL,
239 opnum, false, mem_ctx, &data_in, &data_out);
241 if (NT_STATUS_IS_ERR(status)) {
242 PyErr_SetDCERPCStatus(iface->pipe, status);
243 talloc_free(mem_ctx);
244 return NULL;
247 ret = PyString_FromStringAndSize((char *)data_out.data, data_out.length);
249 talloc_free(mem_ctx);
250 return ret;
253 static PyObject *py_iface_later_context(PyObject *self, PyObject *args, PyObject *kwargs)
255 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
256 NTSTATUS status;
257 const char *kwnames[] = { "abstract_syntax", "transfer_syntax", NULL };
258 PyObject *py_abstract_syntax = Py_None, *py_transfer_syntax = Py_None;
259 struct ndr_syntax_id abstract_syntax, transfer_syntax;
261 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:alter_context",
262 discard_const_p(char *, kwnames), &py_abstract_syntax,
263 &py_transfer_syntax)) {
264 return NULL;
267 if (!ndr_syntax_from_py_object(py_abstract_syntax, &abstract_syntax))
268 return NULL;
270 if (py_transfer_syntax == Py_None) {
271 transfer_syntax = ndr_transfer_syntax;
272 } else {
273 if (!ndr_syntax_from_py_object(py_transfer_syntax,
274 &transfer_syntax))
275 return NULL;
278 status = dcerpc_alter_context(iface->pipe, iface->pipe, &abstract_syntax,
279 &transfer_syntax);
281 if (NT_STATUS_IS_ERR(status)) {
282 PyErr_SetDCERPCStatus(iface->pipe, status);
283 return NULL;
286 return Py_None;
289 static PyMethodDef dcerpc_interface_methods[] = {
290 { "request", (PyCFunction)py_iface_request, METH_VARARGS|METH_KEYWORDS, "S.request(opnum, data, object=None) -> data\nMake a raw request" },
291 { "alter_context", (PyCFunction)py_iface_later_context, METH_VARARGS|METH_KEYWORDS, "S.alter_context(syntax)\nChange to a different interface" },
292 { NULL, NULL, 0, NULL },
296 static void dcerpc_interface_dealloc(PyObject* self)
298 dcerpc_InterfaceObject *interface = (dcerpc_InterfaceObject *)self;
299 talloc_free(interface->pipe);
300 PyObject_Del(self);
303 static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
305 dcerpc_InterfaceObject *ret;
306 const char *binding_string;
307 struct cli_credentials *credentials;
308 struct loadparm_context *lp_ctx = NULL;
309 PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None;
310 TALLOC_CTX *mem_ctx = NULL;
311 struct event_context *event_ctx;
312 NTSTATUS status;
314 PyObject *syntax, *py_basis = Py_None;
315 const char *kwnames[] = {
316 "binding", "syntax", "lp_ctx", "credentials", "basis_connection", NULL
318 extern struct loadparm_context *lp_from_py_object(PyObject *py_obj);
319 extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);
320 struct ndr_interface_table *table;
322 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|OOO:connect", discard_const_p(char *, kwnames), &binding_string, &syntax, &py_lp_ctx, &py_credentials, &py_basis)) {
323 return NULL;
326 lp_ctx = lp_from_py_object(py_lp_ctx);
327 if (lp_ctx == NULL) {
328 PyErr_SetString(PyExc_TypeError, "Expected loadparm context");
329 return NULL;
332 credentials = cli_credentials_from_py_object(py_credentials);
333 if (credentials == NULL) {
334 PyErr_SetString(PyExc_TypeError, "Expected credentials");
335 return NULL;
337 ret = PyObject_New(dcerpc_InterfaceObject, &dcerpc_InterfaceType);
339 event_ctx = s4_event_context_init(mem_ctx);
341 /* Create a dummy interface table struct. TODO: In the future, we should rather just allow
342 * connecting without requiring an interface table.
345 table = talloc_zero(mem_ctx, struct ndr_interface_table);
347 if (table == NULL) {
348 PyErr_SetString(PyExc_MemoryError, "Allocating interface table");
349 return NULL;
352 if (!ndr_syntax_from_py_object(syntax, &table->syntax_id)) {
353 return NULL;
356 ret->pipe = NULL;
358 if (py_basis != Py_None) {
359 struct dcerpc_pipe *base_pipe;
361 if (!PyObject_TypeCheck(py_basis, &dcerpc_InterfaceType)) {
362 PyErr_SetString(PyExc_ValueError, "basis_connection must be a DCE/RPC connection");
363 talloc_free(mem_ctx);
364 return NULL;
367 base_pipe = ((dcerpc_InterfaceObject *)py_basis)->pipe;
369 status = dcerpc_secondary_context(base_pipe, &ret->pipe,
370 table);
371 ret->pipe = talloc_steal(NULL, ret->pipe);
372 } else {
373 status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string,
374 table, credentials, event_ctx, lp_ctx);
377 if (NT_STATUS_IS_ERR(status)) {
378 PyErr_SetDCERPCStatus(ret->pipe, status);
379 talloc_free(mem_ctx);
380 return NULL;
382 ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
383 return (PyObject *)ret;
386 PyTypeObject dcerpc_InterfaceType = {
387 PyObject_HEAD_INIT(NULL) 0,
388 .tp_name = "dcerpc.ClientConnection",
389 .tp_basicsize = sizeof(dcerpc_InterfaceObject),
390 .tp_dealloc = dcerpc_interface_dealloc,
391 .tp_getset = dcerpc_interface_getsetters,
392 .tp_members = dcerpc_interface_members,
393 .tp_methods = dcerpc_interface_methods,
394 .tp_doc = "ClientConnection(binding, syntax, lp_ctx=None, credentials=None) -> connection\n"
395 "\n"
396 "binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\n"
397 "syntax should be a tuple with a GUID and version number of an interface\n"
398 "lp_ctx should be a path to a smb.conf file or a param.LoadParm object\n"
399 "credentials should be a credentials.Credentials object.\n\n",
400 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
401 .tp_new = dcerpc_interface_new,
404 void initbase(void)
406 PyObject *m;
408 if (PyType_Ready(&dcerpc_InterfaceType) < 0)
409 return;
411 m = Py_InitModule3("base", NULL, "DCE/RPC protocol implementation");
412 if (m == NULL)
413 return;
415 Py_INCREF((PyObject *)&dcerpc_InterfaceType);
416 PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);