Allow connecting to a DCE/RPC interface with Python for which we don't have IDL.
authorJelmer Vernooij <jelmer@samba.org>
Sat, 24 May 2008 20:56:49 +0000 (22:56 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Sat, 24 May 2008 20:56:49 +0000 (22:56 +0200)
(This used to be commit e3178d522c95871f1db35d7e058906502387a553)

source4/librpc/rpc/pyrpc.c
source4/scripting/python/samba/tests/dcerpc/bare.py [new file with mode: 0644]
source4/selftest/samba4_tests.sh

index 33b3c63571da547fb3c178f1e0d7da5bd2c49e5d..b87628f345f7fba901ee0698ff07d26cc3a36bf0 100644 (file)
@@ -61,6 +61,8 @@ static PyObject *py_iface_request(PyObject *self, PyObject *args, PyObject *kwar
        data_in.data = (uint8_t *)talloc_strndup(mem_ctx, in_data, in_length);
        data_in.length = in_length;
 
+       ZERO_STRUCT(data_out);
+
        status = dcerpc_request(iface->pipe, NULL /* FIXME: object GUID */, 
                                opnum, false, mem_ctx, &data_in, &data_out);
 
@@ -89,6 +91,50 @@ static void dcerpc_interface_dealloc(PyObject* self)
        PyObject_Del(self);
 }
 
+static bool PyString_AsGUID(PyObject *object, struct GUID *uuid)
+{
+       NTSTATUS status;
+       status = GUID_from_string(PyString_AsString(object), uuid);
+       if (NT_STATUS_IS_ERR(status)) {
+               PyErr_SetNTSTATUS(status);
+               return false;
+       }
+       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 *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObject *kwargs)
 {
        dcerpc_InterfaceObject *ret;
@@ -106,6 +152,7 @@ static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObje
        };
        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)) {
                return NULL;
@@ -126,8 +173,23 @@ static PyObject *dcerpc_interface_new(PyTypeObject *self, PyObject *args, PyObje
 
        event_ctx = event_context_init(mem_ctx);
 
+       /* Create a dummy interface table struct. TODO: In the future, we should rather just allow 
+        * connecting without requiring an interface table.
+        */
+
+       table = talloc_zero(mem_ctx, struct ndr_interface_table);
+
+       if (table == NULL) {
+               PyErr_SetString(PyExc_MemoryError, "Allocating interface table");
+               return NULL;
+       }
+
+       if (!ndr_syntax_from_py_object(syntax, &table->syntax_id)) {
+               return NULL;
+       }
+
        status = dcerpc_pipe_connect(NULL, &ret->pipe, binding_string, 
-                    NULL, credentials, event_ctx, lp_ctx);
+                    table, credentials, event_ctx, lp_ctx);
        if (NT_STATUS_IS_ERR(status)) {
                PyErr_SetString(PyExc_RuntimeError, nt_errstr(status));
                talloc_free(mem_ctx);
diff --git a/source4/scripting/python/samba/tests/dcerpc/bare.py b/source4/scripting/python/samba/tests/dcerpc/bare.py
new file mode 100644 (file)
index 0000000..dae1ded
--- /dev/null
@@ -0,0 +1,30 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# Unix SMB/CIFS implementation.
+# Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
+#   
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#   
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#   
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+from samba.dcerpc import ClientConnection
+from unittest import TestCase
+
+class BareTestCase(TestCase):
+    def test_bare(self):
+        # Connect to the echo pipe
+        x = ClientConnection("ncalrpc:localhost[DEFAULT]", 
+                ("60a15ec5-4de8-11d7-a637-005056a20182", 1))
+        self.assertEquals("\x01\x00\x00\x00", x.request(0, chr(0) * 4))
+
index 36cf2adccbbdd0ca92717b38605d66888723b959..2076ed9c65ff21b9b03158a5690a368ae4eb124e 100755 (executable)
@@ -64,7 +64,6 @@ SCRIPTDIR=$samba4srcdir/../testprogs/ejs
 smb4torture="$samba4bindir/smbtorture $TORTURE_OPTIONS"
 
 plantest "js.base" dc "$SCRIPTDIR/base.js" $CONFIGURATION
-plantest "samr.python" dc "$samba4bindir/../scripting/bin/samr.py" ncalrpc: 
 #plantest "ejsnet.js" dc "$SCRIPTDIR/ejsnet.js" $CONFIGURATION -U\$USERNAME%\$PASSWORD \$DOMAIN ejstestuser
 plantest "js.ldb" none "$SCRIPTDIR/ldb.js" `pwd` $CONFIGURATION -d 10
 plantest "js.winreg" dc $samba4srcdir/scripting/bin/winreg $CONFIGURATION ncalrpc: 'HKLM' -U\$USERNAME%\$PASSWORD
@@ -341,6 +340,7 @@ plantest "samba.python" none $SUBUNITRUN samba.tests
 plantest "provision.python" none $SUBUNITRUN samba.tests.provision
 plantest "samba3.python" none $SUBUNITRUN samba.tests.samba3
 plantest "samr.python" dc $SUBUNITRUN samba.tests.dcerpc.sam
+plantest "dcerpc.bare.python" dc $SUBUNITRUN samba.tests.dcerpc.bare
 plantest "samdb.python" dc $SUBUNITRUN samba.tests.samdb
 plantest "unixinfo.python" dc $SUBUNITRUN samba.tests.dcerpc.unix
 plantest "events.python" none PYTHONPATH="$PYTHONPATH:lib/events" $SUBUNITRUN tests