Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ class BaseConvertCalledStationIdCommand(BaseCommand):
help = "Correct Called Station IDs of Radius Sessions"

def _get_raw_management_info(self, host, port, password):
with telnetlib.Telnet(host, port, timeout=TELNET_CONNECTION_TIMEOUT) as tn:
# Exscript's telnetlib.Telnet is a re-implementation that does not
# support the context manager protocol, so a `with` statement raised
# a TypeError that was silently swallowed upstream. Open the
# connection explicitly and always close it in a finally block.
tn = telnetlib.Telnet(host, port, timeout=TELNET_CONNECTION_TIMEOUT)
try:
if password:
tn.read_until(b"ENTER PASSWORD:", timeout=TELNET_CONNECTION_TIMEOUT)
tn.write(password.encode("ascii") + b"\n")
Expand All @@ -36,6 +41,8 @@ def _get_raw_management_info(self, host, port, password):
raw_management_info = tn.read_until(
b"END", timeout=TELNET_CONNECTION_TIMEOUT
)
finally:
tn.close()
return raw_management_info

def _get_openvpn_routing_info(self, host, port=7505, password=None):
Expand Down
60 changes: 60 additions & 0 deletions openwisp_radius/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,66 @@ def _create_old_users():
)
self.assertEqual(User.objects.filter(pk=user.pk).exists(), True)

@capture_any_output()
@patch.object(
app_settings,
"CALLED_STATION_IDS",
{
"1e4a8240-cfc8-4af0-88dd-7d487e3f7aa1": {
"openvpn_config": [
{"host": "127.0.0.1", "port": 7505, "password": "somepassword"}
],
"unconverted_ids": ["AA-AA-AA-AA-AA-0A"],
}
},
)
@patch.object(app_settings, "OPENVPN_DATETIME_FORMAT", "%Y-%m-%d %H:%M:%S")
@patch("openwisp_radius.tasks.convert_called_station_id")
def test_convert_called_station_id_closes_telnet(self, *args):
# regression for #727: Exscript's telnetlib.Telnet has no context
# manager protocol, so `with Telnet(...)` raised a TypeError that was
# swallowed upstream and the id was never converted. The command must
# open the connection and close it explicitly (not via `with`).
org = self._create_org(
id="1e4a8240-cfc8-4af0-88dd-7d487e3f7aa1",
name="telnet close test",
slug="telnet-close-test",
)
options = _RADACCT.copy()
options["calling_station_id"] = str(EUI("bb:bb:bb:bb:bb:0b", dialect=mac_unix))
options["called_station_id"] = "AA-AA-AA-AA-AA-0A"
options["unique_id"] = "727"
options["organization"] = org
radius_acc = self._create_radius_accounting(**options)

status = self._get_openvpn_status().encode()
closed = []

class FakeTelnet:
# mimics Exscript's Telnet: no __enter__/__exit__
def __init__(self, *args, **kwargs):
pass

def read_until(self, *args, **kwargs):
return status

def write(self, *args, **kwargs):
pass

def close(self):
closed.append(True)

with patch(
"openwisp_radius.management.commands.base."
"convert_called_station_id.telnetlib.Telnet",
FakeTelnet,
):
call_command("convert_called_station_id")

radius_acc.refresh_from_db()
self.assertEqual(radius_acc.called_station_id, "CC-CC-CC-CC-CC-0C")
self.assertTrue(closed, "the telnet connection should be closed")

@capture_any_output()
@patch.object(
app_settings,
Expand Down
Loading