s4 librpc rpc pyrpc: Fix flapping dcerpc.bare tests
[Samba.git] / source4 / librpc / rpc / pyrpc.c
blobd56eb023d96a5ecc439a890ed5b9bdd38a1d7e07
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 "python/py3compat.h"
22 #include "includes.h"
23 #include <structmember.h>
24 #include "librpc/rpc/pyrpc.h"
25 #include "lib/events/events.h"
26 #include "param/pyparam.h"
27 #include "librpc/rpc/dcerpc.h"
28 #include "librpc/rpc/pyrpc_util.h"
29 #include "auth/credentials/pycredentials.h"
30 #include "auth/gensec/gensec.h"
32 void initbase(void);
34 static PyTypeObject dcerpc_InterfaceType;
36 static PyTypeObject *BaseObject_Type;
38 static PyTypeObject *ndr_syntax_id_Type;
40 static bool PyString_AsGUID(PyObject *object, struct GUID *uuid)
42 NTSTATUS status;
43 status = GUID_from_string(PyStr_AsString(object), uuid);
44 if (NT_STATUS_IS_ERR(status)) {
45 PyErr_SetNTSTATUS(status);
46 return false;
48 return true;
51 static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *syntax_id)
53 ZERO_STRUCTP(syntax_id);
55 if (PyStr_Check(object) || PyUnicode_Check(object)) {
56 return PyString_AsGUID(object, &syntax_id->uuid);
57 } else if (PyTuple_Check(object)) {
58 PyObject *item = NULL;
59 if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) {
60 PyErr_SetString(PyExc_ValueError, "Syntax ID tuple has invalid size");
61 return false;
64 item = PyTuple_GetItem(object, 0);
65 if (!(PyStr_Check(item) || PyUnicode_Check(item))) {
66 PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple");
67 return false;
70 if (!PyString_AsGUID(item, &syntax_id->uuid)) {
71 return false;
74 item = PyTuple_GetItem(object, 1);
75 if (!PyInt_Check(item)) {
76 PyErr_SetString(PyExc_ValueError, "Expected version as second element in tuple");
77 return false;
80 syntax_id->if_version = PyInt_AsLong(item);
81 return true;
84 PyErr_SetString(PyExc_TypeError, "Expected UUID or syntax id tuple");
85 return false;
88 static PyObject *py_iface_server_name(PyObject *obj, void *closure)
90 const char *server_name;
91 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
93 server_name = dcerpc_server_name(iface->pipe);
94 if (server_name == NULL)
95 Py_RETURN_NONE;
97 return PyStr_FromString(server_name);
100 static PyObject *py_ndr_syntax_id(struct ndr_syntax_id *syntax_id)
102 PyObject *ret;
103 char *uuid_str;
105 uuid_str = GUID_string(NULL, &syntax_id->uuid);
106 if (uuid_str == NULL)
107 return NULL;
109 ret = Py_BuildValue("(s,i)", uuid_str, syntax_id->if_version);
111 talloc_free(uuid_str);
113 return ret;
116 static PyObject *py_iface_abstract_syntax(PyObject *obj, void *closure)
118 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
120 return py_ndr_syntax_id(&iface->pipe->syntax);
123 static PyObject *py_iface_transfer_syntax(PyObject *obj, void *closure)
125 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
127 return py_ndr_syntax_id(&iface->pipe->transfer_syntax);
130 static PyObject *py_iface_session_key(PyObject *obj, void *closure)
132 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
133 DATA_BLOB session_key;
135 NTSTATUS status = dcerpc_fetch_session_key(iface->pipe, &session_key);
136 PyErr_NTSTATUS_IS_ERR_RAISE(status);
138 return PyBytes_FromStringAndSize((const char *)session_key.data, session_key.length);
141 static PyObject *py_iface_user_session_key(PyObject *obj, void *closure)
143 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
144 TALLOC_CTX *mem_ctx;
145 NTSTATUS status;
146 struct gensec_security *security = NULL;
147 DATA_BLOB session_key = data_blob_null;
148 static PyObject *session_key_obj = NULL;
150 if (iface->pipe == NULL) {
151 PyErr_SetNTSTATUS(NT_STATUS_NO_USER_SESSION_KEY);
152 return NULL;
155 if (iface->pipe->conn == NULL) {
156 PyErr_SetNTSTATUS(NT_STATUS_NO_USER_SESSION_KEY);
157 return NULL;
160 if (iface->pipe->conn->security_state.generic_state == NULL) {
161 PyErr_SetNTSTATUS(NT_STATUS_NO_USER_SESSION_KEY);
162 return NULL;
165 security = iface->pipe->conn->security_state.generic_state;
167 mem_ctx = talloc_new(NULL);
169 status = gensec_session_key(security, mem_ctx, &session_key);
170 if (!NT_STATUS_IS_OK(status)) {
171 talloc_free(mem_ctx);
172 PyErr_SetNTSTATUS(status);
173 return NULL;
176 session_key_obj = PyBytes_FromStringAndSize((const char *)session_key.data,
177 session_key.length);
178 talloc_free(mem_ctx);
179 return session_key_obj;
182 static PyObject *py_iface_get_timeout(PyObject *obj, void *closure)
184 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
185 uint32_t timeout;
187 timeout = dcerpc_binding_handle_set_timeout(iface->binding_handle, 0);
188 dcerpc_binding_handle_set_timeout(iface->binding_handle, timeout);
190 return PyLong_FromUnsignedLong(timeout);
193 static int py_iface_set_timeout(PyObject *obj, PyObject *value, void *closure)
195 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)obj;
196 uint32_t timeout;
198 timeout = PyLong_AsUnsignedLong(value);
199 if (PyErr_Occurred() != NULL) {
200 return -1;
203 dcerpc_binding_handle_set_timeout(iface->binding_handle, timeout);
204 return 0;
207 static PyGetSetDef dcerpc_interface_getsetters[] = {
208 { discard_const_p(char, "server_name"), py_iface_server_name, NULL,
209 discard_const_p(char, "name of the server, if connected over SMB") },
210 { discard_const_p(char, "abstract_syntax"), py_iface_abstract_syntax, NULL,
211 discard_const_p(char, "syntax id of the abstract syntax") },
212 { discard_const_p(char, "transfer_syntax"), py_iface_transfer_syntax, NULL,
213 discard_const_p(char, "syntax id of the transfersyntax") },
214 { discard_const_p(char, "session_key"), py_iface_session_key, NULL,
215 discard_const_p(char, "session key (as used for blob encryption on LSA and SAMR)") },
216 { discard_const_p(char, "user_session_key"), py_iface_user_session_key, NULL,
217 discard_const_p(char, "user_session key (as used for blob encryption on DRSUAPI)") },
218 { discard_const_p(char, "request_timeout"), py_iface_get_timeout, py_iface_set_timeout,
219 discard_const_p(char, "request timeout, in seconds") },
220 { NULL }
223 static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwargs)
225 dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self;
226 int opnum;
227 DATA_BLOB data_in, data_out;
228 NTSTATUS status;
229 char *in_data;
230 Py_ssize_t in_length;
231 PyObject *ret;
232 PyObject *object = NULL;
233 struct GUID object_guid;
234 TALLOC_CTX *mem_ctx = talloc_new(NULL);
235 uint32_t out_flags = 0;
236 const char *kwnames[] = { "opnum", "data", "object", NULL };
238 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "is#|O:request",
239 discard_const_p(char *, kwnames), &opnum, &in_data, &in_length, &object)) {
240 talloc_free(mem_ctx);
241 return NULL;
244 data_in.data = (uint8_t *)talloc_memdup(mem_ctx, in_data, in_length);
245 data_in.length = in_length;
247 ZERO_STRUCT(data_out);
249 if (object != NULL && !PyString_AsGUID(object, &object_guid)) {
250 talloc_free(mem_ctx);
251 return NULL;
254 status = dcerpc_binding_handle_raw_call(iface->binding_handle,
255 object?&object_guid:NULL,
256 opnum,
257 0, /* in_flags */
258 data_in.data,
259 data_in.length,
260 mem_ctx,
261 &data_out.data,
262 &data_out.length,
263 &out_flags);
264 if (!NT_STATUS_IS_OK(status)) {
265 PyErr_SetDCERPCStatus(iface->pipe, status);
266 talloc_free(mem_ctx);
267 return NULL;
270 ret = PyBytes_FromStringAndSize((char *)data_out.data, data_out.length);
272 talloc_free(mem_ctx);
273 return ret;
276 static PyMethodDef dcerpc_interface_methods[] = {
277 { "request", (PyCFunction)py_iface_request, METH_VARARGS|METH_KEYWORDS, "S.request(opnum, data, object=None) -> data\nMake a raw request" },
278 { NULL, NULL, 0, NULL },
281 static void dcerpc_interface_dealloc(PyObject* self)
283 dcerpc_InterfaceObject *interface = (dcerpc_InterfaceObject *)self;
285 struct tevent_context *ev_save = talloc_reparent(
286 interface->mem_ctx, NULL, interface->ev);
287 SMB_ASSERT(ev_save != NULL);
289 interface->binding_handle = NULL;
290 interface->pipe = NULL;
293 * Free everything *except* the event context, which must go
294 * away last
296 TALLOC_FREE(interface->mem_ctx);
299 * Now wish a fond goodbye to the event context itself
301 talloc_unlink(NULL, ev_save);
302 self->ob_type->tp_free(self);
305 static PyObject *dcerpc_interface_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
307 PyObject *ret;
308 const char *binding_string = NULL;
309 PyObject *py_lp_ctx = Py_None;
310 PyObject *py_credentials = Py_None;
311 PyObject *syntax = Py_None;
312 PyObject *py_basis = Py_None;
313 const char *kwnames[] = {
314 "binding", "syntax", "lp_ctx", "credentials", "basis_connection", NULL
316 static struct ndr_interface_table dummy_table;
317 static struct ndr_interface_string_array dummy_endpoints;
318 PyObject *args2 = Py_None;
319 PyObject *kwargs2 = Py_None;
321 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|OOO:connect", discard_const_p(char *, kwnames), &binding_string, &syntax, &py_lp_ctx, &py_credentials, &py_basis)) {
322 return NULL;
325 if (strncmp(binding_string, "irpc:", 5) == 0) {
326 PyErr_SetString(PyExc_ValueError, "irpc: transport not supported");
327 return NULL;
331 * Fill a dummy interface table struct. TODO: In the future, we should
332 * rather just allow connecting without requiring an interface table.
334 * We just fill the syntax during the connect, but keep the memory valid
335 * the whole time.
337 if (!ndr_syntax_from_py_object(syntax, &dummy_table.syntax_id)) {
338 return NULL;
342 * Initialise the endpoints list in dummy_table if required
344 if (dummy_table.endpoints == NULL) {
345 dummy_table.endpoints = &dummy_endpoints;
348 args2 = Py_BuildValue("(s)", binding_string);
349 if (args2 == NULL) {
350 return NULL;
353 kwargs2 = Py_BuildValue("{s:O,s:O,s:O}",
354 "lp_ctx", py_lp_ctx,
355 "credentials", py_credentials,
356 "basis_connection", py_basis);
357 if (kwargs2 == NULL) {
358 Py_DECREF(args2);
359 return NULL;
362 ret = py_dcerpc_interface_init_helper(type, args2, kwargs2, &dummy_table);
363 ZERO_STRUCT(dummy_table.syntax_id);
364 Py_DECREF(args2);
365 Py_DECREF(kwargs2);
366 return ret;
369 static PyTypeObject dcerpc_InterfaceType = {
370 PyVarObject_HEAD_INIT(NULL, 0)
371 .tp_name = "dcerpc.ClientConnection",
372 .tp_basicsize = sizeof(dcerpc_InterfaceObject),
373 .tp_dealloc = dcerpc_interface_dealloc,
374 .tp_getset = dcerpc_interface_getsetters,
375 .tp_methods = dcerpc_interface_methods,
376 .tp_doc = "ClientConnection(binding, syntax, lp_ctx=None, credentials=None) -> connection\n"
377 "\n"
378 "binding should be a DCE/RPC binding string (for example: ncacn_ip_tcp:127.0.0.1)\n"
379 "syntax should be a tuple with a GUID and version number of an interface\n"
380 "lp_ctx should be a path to a smb.conf file or a param.LoadParm object\n"
381 "credentials should be a credentials.Credentials object.\n\n",
382 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
383 .tp_new = dcerpc_interface_new,
386 static PyObject *py_transfer_syntax_ndr_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
388 return py_dcerpc_syntax_init_helper(type, args, kwargs, &ndr_transfer_syntax_ndr);
391 static PyTypeObject py_transfer_syntax_ndr_SyntaxType = {
392 PyVarObject_HEAD_INIT(NULL, 0)
393 .tp_name = "base.transfer_syntax_ndr",
394 .tp_doc = "transfer_syntax_ndr()\n",
395 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
396 .tp_new = py_transfer_syntax_ndr_new,
399 static PyObject *py_transfer_syntax_ndr64_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
401 return py_dcerpc_syntax_init_helper(type, args, kwargs, &ndr_transfer_syntax_ndr64);
404 static PyTypeObject py_transfer_syntax_ndr64_SyntaxType = {
405 PyVarObject_HEAD_INIT(NULL, 0)
406 .tp_name = "base.transfer_syntax_ndr64",
407 .tp_doc = "transfer_syntax_ndr64()\n",
408 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
409 .tp_new = py_transfer_syntax_ndr64_new,
412 static PyObject *py_bind_time_features_syntax_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
414 const char *kwnames[] = {
415 "features", NULL
417 unsigned long long features = 0;
418 struct ndr_syntax_id syntax;
419 PyObject *args2 = Py_None;
420 PyObject *kwargs2 = Py_None;
422 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "K:features", discard_const_p(char *, kwnames), &features)) {
423 return NULL;
426 args2 = Py_BuildValue("()");
427 if (args2 == NULL) {
428 return NULL;
431 kwargs2 = Py_BuildValue("{}");
432 if (kwargs2 == NULL) {
433 Py_DECREF(args2);
434 return NULL;
437 syntax = dcerpc_construct_bind_time_features(features);
439 return py_dcerpc_syntax_init_helper(type, args2, kwargs2, &syntax);
442 static PyTypeObject py_bind_time_features_syntax_SyntaxType = {
443 PyVarObject_HEAD_INIT(NULL, 0)
444 .tp_name = "base.bind_time_features_syntax",
445 .tp_doc = "bind_time_features_syntax(features)\n",
446 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
447 .tp_new = py_bind_time_features_syntax_new,
450 struct py_dcerpc_ndr_pointer {
451 PyObject *value;
454 static void py_dcerpc_ndr_pointer_dealloc(PyObject* self)
456 struct py_dcerpc_ndr_pointer *obj =
457 pytalloc_get_type(self, struct py_dcerpc_ndr_pointer);
459 Py_DECREF(obj->value);
460 obj->value = NULL;
462 self->ob_type->tp_free(self);
465 static PyObject *py_dcerpc_ndr_pointer_get_value(PyObject *self, void *closure)
467 struct py_dcerpc_ndr_pointer *obj =
468 pytalloc_get_type(self, struct py_dcerpc_ndr_pointer);
470 Py_INCREF(obj->value);
471 return obj->value;
474 static int py_dcerpc_ndr_pointer_set_value(PyObject *self, PyObject *value, void *closure)
476 struct py_dcerpc_ndr_pointer *obj =
477 pytalloc_get_type(self, struct py_dcerpc_ndr_pointer);
479 Py_DECREF(obj->value);
480 obj->value = value;
481 Py_INCREF(obj->value);
482 return 0;
485 static PyGetSetDef py_dcerpc_ndr_pointer_getsetters[] = {
486 { discard_const_p(char, "value"),
487 py_dcerpc_ndr_pointer_get_value,
488 py_dcerpc_ndr_pointer_set_value,
489 discard_const_p(char, "the value store by the pointer") },
490 { NULL }
493 static PyObject *py_dcerpc_ndr_pointer_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
495 PyObject *ret = NULL;
496 struct py_dcerpc_ndr_pointer *obj = NULL;
497 const char *kwnames[] = { "value", NULL };
498 PyObject *value = NULL;
499 bool ok;
501 ok = PyArg_ParseTupleAndKeywords(args, kwargs, "O:value",
502 discard_const_p(char *, kwnames),
503 &value);
504 if (!ok) {
505 return NULL;
508 ret = pytalloc_new(struct py_dcerpc_ndr_pointer, type);
509 if (ret == NULL) {
510 return NULL;
513 obj = pytalloc_get_type(ret, struct py_dcerpc_ndr_pointer);
514 *obj = (struct py_dcerpc_ndr_pointer) {
515 .value = value,
518 Py_INCREF(obj->value);
519 return ret;
522 static PyTypeObject py_dcerpc_ndr_pointer_type = {
523 PyVarObject_HEAD_INIT(NULL, 0)
524 .tp_name = "base.ndr_pointer",
525 .tp_dealloc = py_dcerpc_ndr_pointer_dealloc,
526 .tp_getset = py_dcerpc_ndr_pointer_getsetters,
527 .tp_doc = "ndr_pointer(value)\n",
528 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
529 .tp_new = py_dcerpc_ndr_pointer_new,
532 static struct PyModuleDef moduledef = {
533 PyModuleDef_HEAD_INIT,
534 .m_name = "base",
535 .m_doc = "DCE/RPC protocol implementation",
536 .m_size = -1,
539 MODULE_INIT_FUNC(base)
541 PyObject *m;
542 PyObject *dep_talloc;
543 PyObject *dep_samba_dcerpc_misc;
545 dep_talloc = PyImport_ImportModule("talloc");
546 if (dep_talloc == NULL)
547 return NULL;
549 BaseObject_Type = (PyTypeObject *)PyObject_GetAttrString(dep_talloc, "BaseObject");
550 if (BaseObject_Type == NULL)
551 return NULL;
553 dep_samba_dcerpc_misc = PyImport_ImportModule("samba.dcerpc.misc");
554 if (dep_samba_dcerpc_misc == NULL)
555 return NULL;
557 ndr_syntax_id_Type = (PyTypeObject *)PyObject_GetAttrString(dep_samba_dcerpc_misc, "ndr_syntax_id");
558 if (ndr_syntax_id_Type == NULL)
559 return NULL;
561 py_transfer_syntax_ndr_SyntaxType.tp_base = ndr_syntax_id_Type;
562 py_transfer_syntax_ndr_SyntaxType.tp_basicsize = pytalloc_BaseObject_size();
563 py_transfer_syntax_ndr64_SyntaxType.tp_base = ndr_syntax_id_Type;
564 py_transfer_syntax_ndr64_SyntaxType.tp_basicsize = pytalloc_BaseObject_size();
565 py_bind_time_features_syntax_SyntaxType.tp_base = ndr_syntax_id_Type;
566 py_bind_time_features_syntax_SyntaxType.tp_basicsize = pytalloc_BaseObject_size();
568 py_dcerpc_ndr_pointer_type.tp_base = BaseObject_Type;
569 py_dcerpc_ndr_pointer_type.tp_basicsize = pytalloc_BaseObject_size();
571 if (PyType_Ready(&dcerpc_InterfaceType) < 0)
572 return NULL;
574 if (PyType_Ready(&py_transfer_syntax_ndr_SyntaxType) < 0)
575 return NULL;
576 if (PyType_Ready(&py_transfer_syntax_ndr64_SyntaxType) < 0)
577 return NULL;
578 if (PyType_Ready(&py_bind_time_features_syntax_SyntaxType) < 0)
579 return NULL;
581 if (PyType_Ready(&py_dcerpc_ndr_pointer_type) < 0)
582 return NULL;
584 m = PyModule_Create(&moduledef);
585 if (m == NULL)
586 return NULL;
588 Py_INCREF((PyObject *)&dcerpc_InterfaceType);
589 PyModule_AddObject(m, "ClientConnection", (PyObject *)&dcerpc_InterfaceType);
591 Py_INCREF((PyObject *)(void *)&py_transfer_syntax_ndr_SyntaxType);
592 PyModule_AddObject(m, "transfer_syntax_ndr", (PyObject *)(void *)&py_transfer_syntax_ndr_SyntaxType);
593 Py_INCREF((PyObject *)(void *)&py_transfer_syntax_ndr64_SyntaxType);
594 PyModule_AddObject(m, "transfer_syntax_ndr64", (PyObject *)(void *)&py_transfer_syntax_ndr64_SyntaxType);
595 Py_INCREF((PyObject *)(void *)&py_bind_time_features_syntax_SyntaxType);
596 PyModule_AddObject(m, "bind_time_features_syntax", (PyObject *)(void *)&py_bind_time_features_syntax_SyntaxType);
597 Py_INCREF((PyObject *)(void *)&py_dcerpc_ndr_pointer_type);
598 PyModule_AddObject(m, "ndr_pointer", (PyObject *)(void *)&py_dcerpc_ndr_pointer_type);
599 return m;