From 928ecbaebbde00515d08fd530db7c99169c905ef Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 25 May 2008 04:23:03 +0200 Subject: Add support for secondary contexts from Python. (This used to be commit 16d1ad050546ae6500153438db8d3c857e6f3ad5) --- source4/librpc/rpc/pyrpc.c | 136 +++++++++++++++------ source4/pidl/lib/Parse/Pidl/Samba4/Python.pm | 26 +++- source4/samba4-knownfail | 2 - .../scripting/python/samba/tests/dcerpc/bare.py | 13 ++ .../scripting/python/samba/tests/dcerpc/rpcecho.py | 4 + 5 files changed, 136 insertions(+), 45 deletions(-) (limited to 'source4') diff --git a/source4/librpc/rpc/pyrpc.c b/source4/librpc/rpc/pyrpc.c index 1fc6e4e3c7..aebd484b49 100644 --- a/source4/librpc/rpc/pyrpc.c +++ b/source4/librpc/rpc/pyrpc.c @@ -34,6 +34,41 @@ static bool PyString_AsGUID(PyObject *object, struct GUID *uuid) return true; } +static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *syntax_id) +{ + ZERO_STRUCTP(syntax_id); + + if (PyString_Check(object)) { + return PyString_AsGUID(object, &syntax_id->uuid); + } else if (PyTuple_Check(object)) { + if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) { + PyErr_SetString(PyExc_ValueError, "Syntax ID tuple has invalid size"); + return false; + } + + if (!PyString_Check(PyTuple_GetItem(object, 0))) { + PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple"); + return false; + } + + if (!PyString_AsGUID(PyTuple_GetItem(object, 0), &syntax_id->uuid)) + return false; + + if (!PyInt_Check(PyTuple_GetItem(object, 1))) { + PyErr_SetString(PyExc_ValueError, "Expected version as second element in tuple"); + return false; + } + + syntax_id->if_version = PyInt_AsLong(PyTuple_GetItem(object, 1)); + return true; + } + + PyErr_SetString(PyExc_TypeError, "Expected UUID or syntax id tuple"); + return false; +} + + + static PyObject *py_iface_server_name(PyObject *obj, void *closure) { const char *server_name; @@ -47,8 +82,8 @@ static PyObject *py_iface_server_name(PyObject *obj, void *closure) } static PyGetSetDef dcerpc_interface_getsetters[] = { - { discard_const_p(char, "server_name"), py_iface_server_name, - "name of the server, if connected over SMB"}, + { discard_const_p(char, "server_name"), py_iface_server_name, NULL, + discard_const_p(char, "name of the server, if connected over SMB") }, { NULL } }; @@ -107,8 +142,45 @@ static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwar return ret; } +static PyObject *py_iface_later_context(PyObject *self, PyObject *args, PyObject *kwargs) +{ + dcerpc_InterfaceObject *iface = (dcerpc_InterfaceObject *)self; + NTSTATUS status; + const char *kwnames[] = { "abstract_syntax", "transfer_syntax", NULL }; + PyObject *py_abstract_syntax = Py_None, *py_transfer_syntax = Py_None; + struct ndr_syntax_id abstract_syntax, transfer_syntax; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:alter_context", + discard_const_p(char *, kwnames), &py_abstract_syntax, + &py_transfer_syntax)) { + return NULL; + } + + if (!ndr_syntax_from_py_object(py_abstract_syntax, &abstract_syntax)) + return NULL; + + if (py_transfer_syntax == Py_None) { + transfer_syntax = ndr_transfer_syntax; + } else { + if (!ndr_syntax_from_py_object(py_transfer_syntax, + &transfer_syntax)) + return NULL; + } + + status = dcerpc_alter_context(iface->pipe, iface->pipe, &abstract_syntax, + &transfer_syntax); + + if (NT_STATUS_IS_ERR(status)) { + PyErr_SetDCERPCStatus(iface->pipe, status); + return NULL; + } + + return Py_None; +} + static PyMethodDef dcerpc_interface_methods[] = { { "request", (PyCFunction)py_iface_request, METH_VARARGS|METH_KEYWORDS, "S.request(opnum, data, object=None) -> data\nMake a raw request" }, + { "alter_context", (PyCFunction)py_iface_later_context, METH_VARARGS|METH_KEYWORDS, "S.alter_context(syntax)\nChange to a different interface" }, { NULL, NULL, 0, NULL }, }; @@ -120,39 +192,6 @@ static void dcerpc_interface_dealloc(PyObject* self) PyObject_Del(self); } -static bool ndr_syntax_from_py_object(PyObject *object, struct ndr_syntax_id *syntax_id) -{ - ZERO_STRUCTP(syntax_id); - - if (PyString_Check(object)) { - return PyString_AsGUID(object, &syntax_id->uuid); - } else if (PyTuple_Check(object)) { - if (PyTuple_Size(object) < 1 || PyTuple_Size(object) > 2) { - PyErr_SetString(PyExc_ValueError, "Syntax ID tuple has invalid size"); - return false; - } - - if (!PyString_Check(PyTuple_GetItem(object, 0))) { - PyErr_SetString(PyExc_ValueError, "Expected GUID as first element in tuple"); - return false; - } - - if (!PyString_AsGUID(PyTuple_GetItem(object, 0), &syntax_id->uuid)) - return false; - - if (!PyInt_Check(PyTuple_GetItem(object, 1))) { - PyErr_SetString(PyExc_ValueError, "Expected version as second element in tuple"); - return false; - } - - syntax_id->if_version = PyInt_AsLong(PyTuple_GetItem(object, 1)); - return true; - } - - PyErr_SetString(PyExc_TypeError, "Expected UUID or syntax id tuple"); - return false; -} - static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObject *kwargs) { dcerpc_InterfaceObject *ret; @@ -164,15 +203,15 @@ static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObje struct event_context *event_ctx; NTSTATUS status; - PyObject *syntax; + PyObject *syntax, *py_basis = Py_None; const char *kwnames[] = { - "binding", "syntax", "lp_ctx", "credentials", NULL + "binding", "syntax", "lp_ctx", "credentials", "basis_connection", NULL }; extern struct loadparm_context *lp_from_py_object(PyObject *py_obj); extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj); struct ndr_interface_table *table; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|OO:connect", discard_const_p(char *, kwnames), &binding_string, &syntax, &py_lp_ctx, &py_credentials)) { + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|OOO:connect", discard_const_p(char *, kwnames), &binding_string, &syntax, &py_lp_ctx, &py_credentials, &py_basis)) { return NULL; } @@ -208,8 +247,25 @@ static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObje ret->pipe = NULL; - status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, - table, credentials, event_ctx, lp_ctx); + if (py_basis != Py_None) { + struct dcerpc_pipe *base_pipe; + + if (!PyObject_TypeCheck(py_basis, &dcerpc_InterfaceType)) { + PyErr_SetString(PyExc_ValueError, "basis_connection must be a DCE/RPC connection"); + talloc_free(mem_ctx); + return NULL; + } + + base_pipe = ((dcerpc_InterfaceObject *)py_basis)->pipe; + + status = dcerpc_secondary_context(base_pipe, &ret->pipe, + table); + ret->pipe = talloc_steal(NULL, ret->pipe); + } else { + status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, + table, credentials, event_ctx, lp_ctx); + } + if (NT_STATUS_IS_ERR(status)) { PyErr_SetDCERPCStatus(ret->pipe, status); talloc_free(mem_ctx); diff --git a/source4/pidl/lib/Parse/Pidl/Samba4/Python.pm b/source4/pidl/lib/Parse/Pidl/Samba4/Python.pm index 2795d987f7..dbbdb6bca1 100644 --- a/source4/pidl/lib/Parse/Pidl/Samba4/Python.pm +++ b/source4/pidl/lib/Parse/Pidl/Samba4/Python.pm @@ -650,20 +650,20 @@ sub Interface($$$) $self->pidl("const char *binding_string;"); $self->pidl("struct cli_credentials *credentials;"); $self->pidl("struct loadparm_context *lp_ctx = NULL;"); - $self->pidl("PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None;"); + $self->pidl("PyObject *py_lp_ctx = Py_None, *py_credentials = Py_None, *py_basis = Py_None;"); $self->pidl("TALLOC_CTX *mem_ctx = NULL;"); $self->pidl("struct event_context *event_ctx;"); $self->pidl("NTSTATUS status;"); $self->pidl(""); $self->pidl("const char *kwnames[] = {"); $self->indent; - $self->pidl("\"binding\", \"lp_ctx\", \"credentials\", NULL"); + $self->pidl("\"binding\", \"lp_ctx\", \"credentials\", \"basis_connection\", NULL"); $self->deindent; $self->pidl("};"); $self->pidl("extern struct loadparm_context *lp_from_py_object(PyObject *py_obj);"); $self->pidl("extern struct cli_credentials *cli_credentials_from_py_object(PyObject *py_obj);"); $self->pidl(""); - $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"s|OO:$interface->{NAME}\", discard_const_p(char *, kwnames), &binding_string, &py_lp_ctx, &py_credentials)) {"); + $self->pidl("if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"s|OOO:$interface->{NAME}\", discard_const_p(char *, kwnames), &binding_string, &py_lp_ctx, &py_credentials, &py_basis)) {"); $self->indent; $self->pidl("return NULL;"); $self->deindent; @@ -691,8 +691,28 @@ sub Interface($$$) $self->pidl("event_ctx = event_context_init(mem_ctx);"); $self->pidl(""); + $self->pidl("if (py_basis != Py_None) {"); + $self->indent; + $self->pidl("struct dcerpc_pipe *base_pipe;"); + $self->pidl(""); + $self->pidl("if (!PyObject_TypeCheck(py_basis, &dcerpc_InterfaceType)) {"); + $self->indent; + $self->pidl("PyErr_SetString(PyExc_ValueError, \"basis_connection must be a DCE/RPC connection\");"); + $self->pidl("talloc_free(mem_ctx);"); + $self->pidl("return NULL;"); + $self->deindent; + $self->pidl("}"); + $self->pidl(""); + $self->pidl("base_pipe = ((dcerpc_InterfaceObject *)py_basis)->pipe;"); + $self->pidl(""); + $self->pidl("status = dcerpc_secondary_context(base_pipe, &ret->pipe, &ndr_table_$interface->{NAME});"); + $self->deindent; + $self->pidl("} else {"); + $self->indent; $self->pidl("status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, "); $self->pidl(" &ndr_table_$interface->{NAME}, credentials, event_ctx, lp_ctx);"); + $self->deindent; + $self->pidl("}"); $self->handle_ntstatus("status", "NULL", "mem_ctx"); $self->pidl("ret->pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;"); diff --git a/source4/samba4-knownfail b/source4/samba4-knownfail index 1f780f13f0..d489dd3d70 100644 --- a/source4/samba4-knownfail +++ b/source4/samba4-knownfail @@ -40,5 +40,3 @@ samba4.winbind.struct.*.SHOW_SEQUENCE # Not yet working in winbind samba4.winbind.struct.*.GETPWENT # Not yet working in winbind samba4.winbind.struct.*.SETPWENT # Not yet working in winbind samba4.winbind.struct.*.LOOKUP_NAME_SID # Not yet working in winbind - - diff --git a/source4/scripting/python/samba/tests/dcerpc/bare.py b/source4/scripting/python/samba/tests/dcerpc/bare.py index dae1dedeb3..eea6744e42 100644 --- a/source4/scripting/python/samba/tests/dcerpc/bare.py +++ b/source4/scripting/python/samba/tests/dcerpc/bare.py @@ -28,3 +28,16 @@ class BareTestCase(TestCase): ("60a15ec5-4de8-11d7-a637-005056a20182", 1)) self.assertEquals("\x01\x00\x00\x00", x.request(0, chr(0) * 4)) + #def test_alter_context(self): + # x = ClientConnection("ncalrpc:localhost[DEFAULT]", + # ("12345778-1234-abcd-ef00-0123456789ac", 1)) + # x.alter_context(("60a15ec5-4de8-11d7-a637-005056a20182", 1)) + # self.assertEquals("\x01\x00\x00\x00", x.request(0, chr(0) * 4)) + + def test_two_connections(self): + x = ClientConnection("ncalrpc:localhost[DEFAULT]", + ("60a15ec5-4de8-11d7-a637-005056a20182", 1)) + y = ClientConnection("ncalrpc:localhost", + ("60a15ec5-4de8-11d7-a637-005056a20182", 1), + basis_connection=x) + self.assertEquals("\x01\x00\x00\x00", y.request(0, chr(0) * 4)) diff --git a/source4/scripting/python/samba/tests/dcerpc/rpcecho.py b/source4/scripting/python/samba/tests/dcerpc/rpcecho.py index 83279a0b30..7fd1bcc5b8 100644 --- a/source4/scripting/python/samba/tests/dcerpc/rpcecho.py +++ b/source4/scripting/python/samba/tests/dcerpc/rpcecho.py @@ -26,6 +26,10 @@ class RpcEchoTests(RpcInterfaceTestCase): def setUp(self): self.conn = echo.rpcecho("ncalrpc:", self.get_loadparm()) + def test_two_contexts(self): + self.conn2 = echo.rpcecho("ncalrpc", basis_connection=self.conn) + self.assertEquals(3, self.conn2.AddOne(2)) + def test_addone(self): self.assertEquals(2, self.conn.AddOne(1)) -- cgit