From 93e937c58b80ffd0f4c60050e589ca4f4747ddf5 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Thu, 15 Feb 2018 15:02:58 +0000 Subject: [PATCH 1/4] libcli/nbt: Fix illegal tuple index access. Signed-off-by: Noel Power --- libcli/nbt/pynbt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcli/nbt/pynbt.c b/libcli/nbt/pynbt.c index 7162880c32c..70e08c69de9 100644 --- a/libcli/nbt/pynbt.c +++ b/libcli/nbt/pynbt.c @@ -289,7 +289,7 @@ static PyObject *py_nbt_name_register(PyObject *self, PyObject *args, PyObject * return NULL; } - ret = PyTuple_New(3); + ret = PyTuple_New(4); if (ret == NULL) return NULL; PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from)); From 94adcbb3cb31c44b2f708f26dcf1cda1683b19ca Mon Sep 17 00:00:00 2001 From: Noel Power Date: Thu, 15 Feb 2018 20:43:53 +0000 Subject: [PATCH 2/4] s4/selftest: Add simple python netbios test python2 only for the moment Signed-off-by: Noel Power --- python/samba/tests/netbios.py | 52 +++++++++++++++++++++++++++++++++++++++++++ source4/selftest/tests.py | 3 +++ 2 files changed, 55 insertions(+) create mode 100644 python/samba/tests/netbios.py diff --git a/python/samba/tests/netbios.py b/python/samba/tests/netbios.py new file mode 100644 index 00000000000..65ec5f1d7d1 --- /dev/null +++ b/python/samba/tests/netbios.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Unix SMB/CIFS implementation. Tests for netbios py module +# Copyright (C) Noel Power 2018 +# +# 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 . + +import samba, os, random, sys +from samba import netbios + +class NetBiosTests(samba.tests.TestCase): + def setUp(self): + super(NetBiosTests, self).setUp() + self.n = netbios.Node() + self.ifc = os.environ["SERVER_IP"] + self.dc = os.environ["DC_NETBIOSNAME"] + def tearDown(self): + super(NetBiosTests, self).tearDown() + def test_query_name(self): + (reply_from, names, addresses) = self.n.query_name(self.dc, self.ifc, timeout=4) + assert reply_from == self.ifc + assert names[0] == self.dc + assert addresses[0] == self.ifc + + def test_name_status(self): + (reply_from, name, name_list) = self.n.name_status(self.dc, self.ifc, timeout=4) + assert reply_from == self.ifc + assert name[0] == self.dc + assert len(name_list) > 0 + + def test_register_name(self): + address = '127.0.0.3' + (reply_from, name, reply_address, code) = self.n.register_name((self.dc, 0x20), address, self.ifc, multi_homed=False, timeout=4) + assert reply_from == self.ifc + assert name[0] == self.dc + assert reply_address == address + assert code == 6 + + def disabled_test_refresh(self): + # can't get the below test to work, disabling + address = '127.0.0.3' + res = self.n.refresh_name((self.dc, 0x20), address, self.ifc, timeout=10) diff --git a/source4/selftest/tests.py b/source4/selftest/tests.py index 4e397a8f134..927f347d1fd 100755 --- a/source4/selftest/tests.py +++ b/source4/selftest/tests.py @@ -613,6 +613,9 @@ def planoldpythontestsuite(env, module, name=None, extra_path=[], environ={}, ex planpythontestsuite("ad_dc:local", "samba.tests.samba_tool.dnscmd") planpythontestsuite("ad_dc_ntvfs:local", "samba.tests.dcerpc.rpcecho") + +planoldpythontestsuite("nt4_dc", "samba.tests.netbios", extra_args=['-U"$USERNAME%$PASSWORD"'], py3_compatible=False) + planoldpythontestsuite("ad_dc_ntvfs:local", "samba.tests.dcerpc.registry", extra_args=['-U"$USERNAME%$PASSWORD"']) planoldpythontestsuite("ad_dc_ntvfs", "samba.tests.dcerpc.dnsserver", extra_args=['-U"$USERNAME%$PASSWORD"']) planoldpythontestsuite("ad_dc", "samba.tests.dcerpc.dnsserver", extra_args=['-U"$USERNAME%$PASSWORD"']) From e7412245398003417774deae8ca442294b4ae3b3 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Fri, 2 Feb 2018 17:08:00 +0000 Subject: [PATCH 3/4] python3 port for netbios module Signed-off-by: Noel Power --- libcli/nbt/pynbt.c | 55 +++++++++++++++++++++++++++++------------------- libcli/nbt/wscript_build | 3 ++- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/libcli/nbt/pynbt.c b/libcli/nbt/pynbt.c index 70e08c69de9..6337092b226 100644 --- a/libcli/nbt/pynbt.c +++ b/libcli/nbt/pynbt.c @@ -19,6 +19,7 @@ #include #include "includes.h" +#include "python/py3compat.h" #include "libcli/util/pyerrors.h" #include "python/modules.h" #include "../libcli/nbt/libnbt.h" @@ -37,7 +38,7 @@ typedef struct { static void py_nbt_node_dealloc(nbt_node_Object *self) { talloc_free(self->mem_ctx); - self->ob_type->tp_free(self); + Py_TYPE(self)->tp_free(self); } static PyObject *py_nbt_node_init(PyTypeObject *self, PyObject *args, PyObject *kwargs) @@ -56,8 +57,8 @@ static PyObject *py_nbt_node_init(PyTypeObject *self, PyObject *args, PyObject * static bool PyObject_AsDestinationTuple(PyObject *obj, const char **dest_addr, uint16_t *dest_port) { - if (PyString_Check(obj)) { - *dest_addr = PyString_AsString(obj); + if (PyStr_Check(obj)) { + *dest_addr = PyStr_AsString(obj); *dest_port = NBT_NAME_SERVICE_PORT; return true; } @@ -68,12 +69,12 @@ static bool PyObject_AsDestinationTuple(PyObject *obj, const char **dest_addr, u return false; } - if (!PyString_Check(PyTuple_GetItem(obj, 0))) { + if (!PyStr_Check(PyTuple_GetItem(obj, 0))) { PyErr_SetString(PyExc_TypeError, "Destination tuple first element not string"); return false; } - *dest_addr = PyString_AsString(obj); + *dest_addr = PyStr_AsString(obj); if (PyTuple_Size(obj) == 1) { *dest_port = NBT_NAME_SERVICE_PORT; @@ -95,13 +96,13 @@ static bool PyObject_AsNBTName(PyObject *obj, struct nbt_name_socket *name_socke { if (PyTuple_Check(obj)) { if (PyTuple_Size(obj) == 2) { - name->name = PyString_AsString(PyTuple_GetItem(obj, 0)); + name->name = PyStr_AsString(PyTuple_GetItem(obj, 0)); name->type = PyInt_AsLong(PyTuple_GetItem(obj, 1)); name->scope = NULL; return true; } else if (PyTuple_Size(obj) == 3) { - name->name = PyString_AsString(PyTuple_GetItem(obj, 0)); - name->scope = PyString_AsString(PyTuple_GetItem(obj, 1)); + name->name = PyStr_AsString(PyTuple_GetItem(obj, 0)); + name->scope = PyStr_AsString(PyTuple_GetItem(obj, 1)); name->type = PyInt_AsLong(PyTuple_GetItem(obj, 2)); return true; } else { @@ -110,9 +111,9 @@ static bool PyObject_AsNBTName(PyObject *obj, struct nbt_name_socket *name_socke } } - if (PyString_Check(obj)) { + if (PyStr_Check(obj)) { /* FIXME: Parse string to be able to interpret things like RHONWYN<02> ? */ - name->name = PyString_AsString(obj); + name->name = PyStr_AsString(obj); name->scope = NULL; name->type = 0; return true; @@ -171,7 +172,7 @@ static PyObject *py_nbt_name_query(PyObject *self, PyObject *args, PyObject *kwa ret = PyTuple_New(3); if (ret == NULL) return NULL; - PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from)); + PyTuple_SetItem(ret, 0, PyStr_FromString(io.out.reply_from)); py_name = PyObject_FromNBTName(node->socket, &io.out.name); if (py_name == NULL) @@ -186,7 +187,7 @@ static PyObject *py_nbt_name_query(PyObject *self, PyObject *args, PyObject *kwa } for (i = 0; i < io.out.num_addrs; i++) { - PyList_SetItem(reply_addrs, i, PyString_FromString(io.out.reply_addrs[i])); + PyList_SetItem(reply_addrs, i, PyStr_FromString(io.out.reply_addrs[i])); } PyTuple_SetItem(ret, 2, reply_addrs); @@ -229,7 +230,7 @@ static PyObject *py_nbt_name_status(PyObject *self, PyObject *args, PyObject *kw ret = PyTuple_New(3); if (ret == NULL) return NULL; - PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from)); + PyTuple_SetItem(ret, 0, PyStr_FromString(io.out.reply_from)); py_name = PyObject_FromNBTName(node->socket, &io.out.name); if (py_name == NULL) @@ -292,7 +293,7 @@ static PyObject *py_nbt_name_register(PyObject *self, PyObject *args, PyObject * ret = PyTuple_New(4); if (ret == NULL) return NULL; - PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from)); + PyTuple_SetItem(ret, 0, PyStr_FromString(io.out.reply_from)); py_name = PyObject_FromNBTName(node->socket, &io.out.name); if (py_name == NULL) @@ -300,7 +301,7 @@ static PyObject *py_nbt_name_register(PyObject *self, PyObject *args, PyObject * PyTuple_SetItem(ret, 1, py_name); - PyTuple_SetItem(ret, 2, PyString_FromString(io.out.reply_addr)); + PyTuple_SetItem(ret, 2, PyStr_FromString(io.out.reply_addr)); PyTuple_SetItem(ret, 3, PyInt_FromLong(io.out.rcode)); @@ -347,7 +348,7 @@ static PyObject *py_nbt_name_refresh(PyObject *self, PyObject *args, PyObject *k ret = PyTuple_New(3); if (ret == NULL) return NULL; - PyTuple_SetItem(ret, 0, PyString_FromString(io.out.reply_from)); + PyTuple_SetItem(ret, 0, PyStr_FromString(io.out.reply_from)); py_name = PyObject_FromNBTName(node->socket, &io.out.name); if (py_name == NULL) @@ -355,7 +356,7 @@ static PyObject *py_nbt_name_refresh(PyObject *self, PyObject *args, PyObject *k PyTuple_SetItem(ret, 1, py_name); - PyTuple_SetItem(ret, 2, PyString_FromString(io.out.reply_addr)); + PyTuple_SetItem(ret, 2, PyStr_FromString(io.out.reply_addr)); PyTuple_SetItem(ret, 3, PyInt_FromLong(io.out.rcode)); @@ -386,7 +387,7 @@ static PyMethodDef py_nbt_methods[] = { }; PyTypeObject nbt_node_Type = { - PyObject_HEAD_INIT(NULL) 0, + PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "netbios.Node", .tp_basicsize = sizeof(nbt_node_Object), .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, @@ -397,14 +398,24 @@ PyTypeObject nbt_node_Type = { "Create a new NetBIOS node\n" }; -void initnetbios(void) +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + .m_name = "netbios", + .m_doc = "NetBIOS over TCP/IP support", + .m_size = -1, + .m_methods = NULL, +}; + +MODULE_INIT_FUNC(netbios) { - PyObject *mod; + PyObject *mod = NULL; if (PyType_Ready(&nbt_node_Type) < 0) - return; + return mod; + + mod = PyModule_Create(&moduledef); - mod = Py_InitModule3("netbios", NULL, "NetBIOS over TCP/IP support"); Py_INCREF((PyObject *)&nbt_node_Type); PyModule_AddObject(mod, "Node", (PyObject *)&nbt_node_Type); + return mod; } diff --git a/libcli/nbt/wscript_build b/libcli/nbt/wscript_build index 090789ce99a..e965a347204 100644 --- a/libcli/nbt/wscript_build +++ b/libcli/nbt/wscript_build @@ -24,7 +24,8 @@ bld.SAMBA_BINARY('nmblookup' + bld.env.suffix4, install=False ) -bld.SAMBA_PYTHON('python_netbios', +for env in bld.gen_python_environments(): + bld.SAMBA_PYTHON('python_netbios', source='pynbt.c', public_deps='cli-nbt DYNCONFIG samba-hostconfig', realname='samba/netbios.so' From 6c7014f43d087b43edb9bacfdc023b0929548429 Mon Sep 17 00:00:00 2001 From: Noel Power Date: Thu, 15 Feb 2018 20:46:53 +0000 Subject: [PATCH 4/4] s4/selftest: enable netbios python test for python3 too Signed-off-by: Noel Power --- source4/selftest/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source4/selftest/tests.py b/source4/selftest/tests.py index 927f347d1fd..1b1dbdc5b65 100755 --- a/source4/selftest/tests.py +++ b/source4/selftest/tests.py @@ -614,7 +614,7 @@ def planoldpythontestsuite(env, module, name=None, extra_path=[], environ={}, ex planpythontestsuite("ad_dc_ntvfs:local", "samba.tests.dcerpc.rpcecho") -planoldpythontestsuite("nt4_dc", "samba.tests.netbios", extra_args=['-U"$USERNAME%$PASSWORD"'], py3_compatible=False) +planoldpythontestsuite("nt4_dc", "samba.tests.netbios", extra_args=['-U"$USERNAME%$PASSWORD"'], py3_compatible=True) planoldpythontestsuite("ad_dc_ntvfs:local", "samba.tests.dcerpc.registry", extra_args=['-U"$USERNAME%$PASSWORD"']) planoldpythontestsuite("ad_dc_ntvfs", "samba.tests.dcerpc.dnsserver", extra_args=['-U"$USERNAME%$PASSWORD"'])