s4:pyrpc: correctly implement .request_timeout
[Samba.git] / source4 / librpc / rpc / pyrpc.c
blob834000c65746da19303f8b34c13630a67c2066e6
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 <Python.h>
21 #include "includes.h"
22 #include <structmember.h>
23 #include "librpc/rpc/pyrpc.h"
24 #include "lib/events/events.h"
25 #include "param/pyparam.h"
26 #include "librpc/rpc/dcerpc.h"
27 #include "librpc/rpc/pyrpc_util.h"
28 #include "auth/credentials/pycredentials.h"
29 #include "auth/gensec/gensec.h"
31 void initbase(void);
33 static PyTypeObject dcerpc_InterfaceType;
35 static PyTypeObject *ndr_syntax_id_Type;
37 static bool PyString_AsGUID(PyObject *object, struct GUID *uuid)
39 NTSTATUS status;
40 status = GUID_from_string(PyString_AsString(object), uuid);
41 if (NT_STATUS_IS_ERR(status)) {
42 PyErr_SetNTSTATUS(status);
43 return false;
45 return true;
48 static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *syntax_id)
50 ZERO_STRUCTP(syntax_id);
52 if (PyString_Check(object)) {
53 return PyString_AsGUID(object, &syntax_id->uuid);
54 } else if (PyTuple_Check(object)) {
55 if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) {
56 PyErr_SetString(PyExc_ValueError, "Syntax ID tuple has invalid size");
57 return false;
60 if (!PyString_Check(PyTuple_GetItem(object, 0))) {
61 PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple");
62 return false;
65 if (!PyString_AsGUID(PyTuple_GetItem(object, 0), &syntax_id->uuid))
66 return false;
68 if (!PyInt_Check(PyTuple_GetItem(object, 1))) {
69 PyErr_SetString(PyExc_ValueError, "Expected version as second element in tuple");
70 return false;
73 syntax_id->if_version = PyInt_AsLong(PyTuple_GetItem(object, 1));
74 return true;
77 PyErr_SetString(PyExc_TypeError, "Expected UUID or syntax id tuple");
78 return false;
81 static PyObject *py_iface_server_name(PyObject *obj, void *closure)
83 const char *server_name;
84 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
86 server_name = dcerpc_server_name(iface->pipe);
87 if (server_name == NULL)
88 Py_RETURN_NONE;
90 return PyString_FromString(server_name);
93 static PyObject *py_ndr_syntax_id(struct ndr_syntax_id *syntax_id)
95 PyObject *ret;
96 char *uuid_str;
98 uuid_str = GUID_string(NULL, &syntax_id->uuid);
99 if (uuid_str == NULL)
100 return NULL;
102 ret = Py_BuildValue("(s,i)", uuid_str, syntax_id->if_version);
104 talloc_free(uuid_str);
106 return ret;
109 static PyObject *py_iface_abstract_syntax(PyObject *obj, void *closure)
111 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
113 return py_ndr_syntax_id(&iface->pipe->syntax);
116 static PyObject *py_iface_transfer_syntax(PyObject *obj, void *closure)
118 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
120 return py_ndr_syntax_id(&iface->pipe->transfer_syntax);
123 static PyObject *py_iface_session_key(PyObject *obj, void *closure)
125 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
126 DATA_BLOB session_key;
128 NTSTATUS status = dcerpc_fetch_session_key(iface->pipe, &session_key);
129 PyErr_NTSTATUS_IS_ERR_RAISE(status);
131 return PyString_FromStringAndSize((const char *)session_key.data, session_key.length);
134 static PyObject *py_iface_user_session_key(PyObject *obj, void *closure)
136 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
137 TALLOC_CTX *mem_ctx;
138 NTSTATUS status;
139 struct gensec_security *security = NULL;
140 DATA_BLOB session_key = data_blob_null;
141 static PyObject *session_key_obj = NULL;
143 if (iface->pipe == NULL) {
144 PyErr_SetNTSTATUS(NT_STATUS_NO_USER_SESSION_KEY);
145 return NULL;
148 if (iface->pipe->conn == NULL) {
149 PyErr_SetNTSTATUS(NT_STATUS_NO_USER_SESSION_KEY);
150 return NULL;
153 if (iface->pipe->conn->security_state.generic_state == NULL) {
154 PyErr_SetNTSTATUS(NT_STATUS_NO_USER_SESSION_KEY);
155 return NULL;
158 security = iface->pipe->conn->security_state.generic_state;
160 mem_ctx = talloc_new(NULL);
162 status = gensec_session_key(security, mem_ctx, &session_key);
163 if (!NT_STATUS_IS_OK(status)) {
164 talloc_free(mem_ctx);
165 PyErr_SetNTSTATUS(status);
166 return NULL;
169 session_key_obj = PyString_FromStringAndSize((const char *)session_key.data,
170 session_key.length);
171 talloc_free(mem_ctx);
172 return session_key_obj;
175 static PyObject *py_iface_get_timeout(PyObject *obj, void *closure)
177 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
178 uint32_t timeout;
180 timeout = dcerpc_binding_handle_set_timeout(iface->binding_handle, 0);
181 dcerpc_binding_handle_set_timeout(iface->binding_handle, timeout);
183 return PyLong_FromUnsignedLong(timeout);
186 static int py_iface_set_timeout(PyObject *obj, PyObject *value, void *closure)
188 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
189 uint32_t timeout;
191 timeout = PyLong_AsUnsignedLong(value);
192 if (PyErr_Occurred() != NULL) {
193 return -1;
196 dcerpc_binding_handle_set_timeout(iface->binding_handle, timeout);
197 return 0;
200 static PyGetSetDef dcerpc_interface_getsetters[] = {
201 { discard_const_p(char, "server_name"), py_iface_server_name, NULL,
202 discard_const_p(char, "name of the server, if connected over SMB") },
203 { discard_const_p(char, "abstract_syntax"), py_iface_abstract_syntax, NULL,
204 discard_const_p(char, "syntax id of the abstract syntax") },
205 { discard_const_p(char, "transfer_syntax"), py_iface_transfer_syntax, NULL,
206 discard_const_p(char, "syntax id of the transfersyntax") },
207 { discard_const_p(char, "session_key"), py_iface_session_key, NULL,
208 discard_const_p(char, "session key (as used for blob encryption on LSA and SAMR)") },
209 { discard_const_p(char, "user_session_key"), py_iface_user_session_key, NULL,
210 discard_const_p(char, "user_session key (as used for blob encryption on DRSUAPI)") },
211 { discard_const_p(char, "request_timeout"), py_iface_get_timeout, py_iface_set_timeout,
212 discard_const_p(char, "request timeout, in seconds") },
213 { NULL }
216 static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwargs)
218 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
219 int opnum;
220 DATA_BLOB data_in, data_out;
221 NTSTATUS status;
222 char *in_data;
223 Py_ssize_t in_length;
224 PyObject *ret;
225 PyObject *object = NULL;
226 struct GUID object_guid;
227 TALLOC_CTX *mem_ctx = talloc_new(NULL);
228 uint32_t out_flags = 0;
229 const char *kwnames[] = { "opnum", "data", "object", NULL };
231 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "is#|O:request",
232 discard_const_p(char *, kwnames), &opnum, &in_data, &in_length, &object)) {
233 talloc_free(mem_ctx);
234 return NULL;
237 data_in.data = (uint8_t *)talloc_memdup(mem_ctx, in_data, in_length);
238 data_in.length = in_length;
240 ZERO_STRUCT(data_out);
242 if (object != NULL && !PyString_AsGUID(object, &object_guid)) {
243 talloc_free(mem_ctx);
244 return NULL;
247 status = dcerpc_binding_handle_raw_call(iface->binding_handle,
248 object?&object_guid:NULL,
249 opnum,
250 0, /* in_flags */
251 data_in.data,
252 data_in.length,
253 mem_ctx,
254 &data_out.data,
255 &data_out.length,
256 &out_flags);
257 if (!NT_STATUS_IS_OK(status)) {
258 PyErr_SetDCERPCStatus(iface->pipe, status);
259 talloc_free(mem_ctx);
260 return NULL;
263 ret = PyString_FromStringAndSize((char *)data_out.data, data_out.length);
265 talloc_free(mem_ctx);
266 return ret;
269 static PyMethodDef dcerpc_interface_methods[] = {
270 { "request", (PyCFunction)py_iface_request, METH_VARARGS|METH_KEYWORDS, "S.request(opnum, data, object=None) -> data\nMake a raw request" },
271 { NULL, NULL, 0, NULL },
274 static void dcerpc_interface_dealloc(PyObject* self)
276 dcerpc_InterfaceObject *interface = (dcerpc_InterfaceObject *)self;
277 interface->binding_handle = NULL;
278 interface->pipe = NULL;
279 TALLOC_FREE(interface->mem_ctx);
280 self->ob_type->tp_free(self);
283 static PyObject *dcerpc_interface_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
285 PyObject *ret;
286 const char *binding_string = NULL;
287 PyObject *py_lp_ctx = Py_None;
288 PyObject *py_credentials = Py_None;
289 PyObject *syntax = Py_None;
290 PyObject *py_basis = Py_None;
291 const char *kwnames[] = {
292 "binding", "syntax", "lp_ctx", "credentials", "basis_connection", NULL
294 static struct ndr_interface_table dummy_table;
295 PyObject *args2 = Py_None;
296 PyObject *kwargs2 = Py_None;
298 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|OOO:connect", discard_const_p(char *, kwnames), &binding_string, &syntax, &py_lp_ctx, &py_credentials, &py_basis)) {
299 return NULL;
302 if (strncmp(binding_string, "irpc:", 5) == 0) {
303 PyErr_SetString(PyExc_ValueError, "irpc: transport not supported");
304 return NULL;
308 * Fill a dummy interface table struct. TODO: In the future, we should
309 * rather just allow connecting without requiring an interface table.
311 * We just fill the syntax during the connect, but keep the memory valid
312 * the whole time.
314 if (!ndr_syntax_from_py_object(syntax, &dummy_table.syntax_id)) {
315 return NULL;
318 args2 = Py_BuildValue("(s)", binding_string);
319 if (args2 == NULL) {
320 return NULL;
323 kwargs2 = Py_BuildValue("{s:O,s:O,s:O}",
324 "lp_ctx", py_lp_ctx,
325 "credentials", py_credentials,
326 "basis_connection", py_basis);
327 if (kwargs2 == NULL) {
328 Py_DECREF(args2);
329 return NULL;
332 ret = py_dcerpc_interface_init_helper(type, args2, kwargs2, &dummy_table);
333 ZERO_STRUCT(dummy_table.syntax_id);
334 Py_DECREF(args2);
335 Py_DECREF(kwargs2);
336 return ret;
339 static PyTypeObject dcerpc_InterfaceType = {
340 PyObject_HEAD_INIT(NULL) 0,
341 .tp_name = "dcerpc.ClientConnection",
342 .tp_basicsize = sizeof(dcerpc_InterfaceObject),
343 .tp_dealloc = dcerpc_interface_dealloc,
344 .tp_getset = dcerpc_interface_getsetters,
345 .tp_methods = dcerpc_interface_methods,
346 .tp_doc = "ClientConnection(binding, syntax, lp_ctx=None, credentials=None) -> connection\n"
347 "\n"
348 "binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\n"
349 "syntax should be a tuple with a GUID and version number of an interface\n"
350 "lp_ctx should be a path to a smb.conf file or a param.LoadParm object\n"
351 "credentials should be a credentials.Credentials object.\n\n",
352 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
353 .tp_new = dcerpc_interface_new,
356 static PyObject *py_transfer_syntax_ndr_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
358 return py_dcerpc_syntax_init_helper(type, args, kwargs, &ndr_transfer_syntax_ndr);
361 static PyTypeObject py_transfer_syntax_ndr_SyntaxType = {
362 PyObject_HEAD_INIT(NULL) 0,
363 .tp_name = "base.transfer_syntax_ndr",
364 .tp_doc = "transfer_syntax_ndr()\n",
365 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
366 .tp_new = py_transfer_syntax_ndr_new,
369 static PyObject *py_transfer_syntax_ndr64_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
371 return py_dcerpc_syntax_init_helper(type, args, kwargs, &ndr_transfer_syntax_ndr64);
374 static PyTypeObject py_transfer_syntax_ndr64_SyntaxType = {
375 PyObject_HEAD_INIT(NULL) 0,
376 .tp_name = "base.transfer_syntax_ndr64",
377 .tp_doc = "transfer_syntax_ndr64()\n",
378 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
379 .tp_new = py_transfer_syntax_ndr64_new,
382 static PyObject *py_bind_time_features_syntax_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
384 const char *kwnames[] = {
385 "features", NULL
387 unsigned long long features = 0;
388 struct ndr_syntax_id syntax;
389 PyObject *args2 = Py_None;
390 PyObject *kwargs2 = Py_None;
392 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "K:features", discard_const_p(char *, kwnames), &features)) {
393 return NULL;
396 args2 = Py_BuildValue("()");
397 if (args2 == NULL) {
398 return NULL;
401 kwargs2 = Py_BuildValue("{}");
402 if (kwargs2 == NULL) {
403 Py_DECREF(args2);
404 return NULL;
407 syntax = dcerpc_construct_bind_time_features(features);
409 return py_dcerpc_syntax_init_helper(type, args2, kwargs2, &syntax);
412 static PyTypeObject py_bind_time_features_syntax_SyntaxType = {
413 PyObject_HEAD_INIT(NULL) 0,
414 .tp_name = "base.bind_time_features_syntax",
415 .tp_doc = "bind_time_features_syntax(features)\n",
416 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
417 .tp_new = py_bind_time_features_syntax_new,
420 void initbase(void)
422 PyObject *m;
423 PyObject *dep_samba_dcerpc_misc;
425 dep_samba_dcerpc_misc = PyImport_ImportModule("samba.dcerpc.misc");
426 if (dep_samba_dcerpc_misc == NULL)
427 return;
429 ndr_syntax_id_Type = (PyTypeObject *)PyObject_GetAttrString(dep_samba_dcerpc_misc, "ndr_syntax_id");
430 if (ndr_syntax_id_Type == NULL)
431 return;
433 py_transfer_syntax_ndr_SyntaxType.tp_base = ndr_syntax_id_Type;
434 py_transfer_syntax_ndr_SyntaxType.tp_basicsize = pytalloc_BaseObject_size();
435 py_transfer_syntax_ndr64_SyntaxType.tp_base = ndr_syntax_id_Type;
436 py_transfer_syntax_ndr64_SyntaxType.tp_basicsize = pytalloc_BaseObject_size();
437 py_bind_time_features_syntax_SyntaxType.tp_base = ndr_syntax_id_Type;
438 py_bind_time_features_syntax_SyntaxType.tp_basicsize = pytalloc_BaseObject_size();
440 if (PyType_Ready(&dcerpc_InterfaceType) < 0)
441 return;
443 if (PyType_Ready(&py_transfer_syntax_ndr_SyntaxType) < 0)
444 return;
445 if (PyType_Ready(&py_transfer_syntax_ndr64_SyntaxType) < 0)
446 return;
447 if (PyType_Ready(&py_bind_time_features_syntax_SyntaxType) < 0)
448 return;
450 m = Py_InitModule3("base", NULL, "DCE/RPC protocol implementation");
451 if (m == NULL)
452 return;
454 Py_INCREF((PyObject *)&dcerpc_InterfaceType);
455 PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);
457 Py_INCREF((PyObject *)(void *)&py_transfer_syntax_ndr_SyntaxType);
458 PyModule_AddObject(m, "transfer_syntax_ndr", (PyObject *)(void *)&py_transfer_syntax_ndr_SyntaxType);
459 Py_INCREF((PyObject *)(void *)&py_transfer_syntax_ndr64_SyntaxType);
460 PyModule_AddObject(m, "transfer_syntax_ndr64", (PyObject *)(void *)&py_transfer_syntax_ndr64_SyntaxType);
461 Py_INCREF((PyObject *)(void *)&py_bind_time_features_syntax_SyntaxType);
462 PyModule_AddObject(m, "bind_time_features_syntax", (PyObject *)(void *)&py_bind_time_features_syntax_SyntaxType);