From ba0d96eda743543418fc45872828202bf7c78e07 Mon Sep 17 00:00:00 2001 From: Steve McMaster Date: Sun, 16 Oct 2022 20:48:49 +0000 Subject: [PATCH] Add subnet support for client secrets --- config.sample.yaml | 2 +- test.py | 19 +++++++++++++++++-- vlanassign.py | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/config.sample.yaml b/config.sample.yaml index 728049c..23cc15a 100644 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -4,7 +4,7 @@ assign: "BE39.34DD.76AC": 30 "C919.2AC9.7E20": 40 clients: - 127.0.0.1: 070c58eb-f79d-4cbb-8482-3aaab089cc88 + 127.0.0.0/8: 0f5c6a8b-bb18-46fc-8fea-2c2032cb0754 192.168.200.123: 1231fa06-593b-4f37-a4a1-646e22c34928 10.244.0.1: 83f271a8-285d-4830-b4ae-2c91d460b7e1 default_vlan: 100 diff --git a/test.py b/test.py index 38d2147..4601efe 100644 --- a/test.py +++ b/test.py @@ -1,12 +1,27 @@ +import tempfile + import pyrad.packet +import yaml from pyrad.client import Client from pyrad.dictionary import Dictionary +with open("config.sample.yaml", "r", encoding="utf-8") as f: + config = yaml.load(f, Loader=yaml.SafeLoader) + + +with tempfile.NamedTemporaryFile() as tmpfile: + with open(tmpfile.name, "w", encoding="utf-8") as f: + for attribute in config["dictionary"]: + f.write(f"ATTRIBUTE {attribute['name']} {attribute['id']} {attribute['type']}\n") + + dictionary = Dictionary(tmpfile.name) + + srv = Client( server="127.0.0.1", - secret=b"070c58eb-f79d-4cbb-8482-3aaab089cc88", - dict=Dictionary("dictionary") + secret=b"0f5c6a8b-bb18-46fc-8fea-2c2032cb0754z", + dict=dictionary ) req = srv.CreateAuthPacket( diff --git a/vlanassign.py b/vlanassign.py index 6f2e493..0fa9713 100644 --- a/vlanassign.py +++ b/vlanassign.py @@ -1,8 +1,10 @@ import argparse import logging +import ipaddress import os import re import tempfile +from collections import UserDict import asyncio import yaml @@ -21,6 +23,39 @@ except ImportError: logger = logging.getLogger(__name__) +class IpaddressDict(UserDict): + def __getitem__(self, key): + # If `key` exists as a key, just return that + if super().__contains__(key): + return super().__getitem__(key) + + # Test that `key` is actually an ip_address + try: + key = ipaddress.ip_address(key) + except ValueError as e: + raise KeyError(key) from e + + # Check each key we have as an ip_network + for (test, value) in self.items(): + try: + test = ipaddress.ip_network(test) + except ValueError: + continue + + if key in test: + return value + + # No dice + raise KeyError(key) + + def __contains__(self, key): + try: + self[key] + except KeyError: + return False + return True + + class VlanAssign(ServerAsync): def __init__(self, usermap, *args, default_vlan=1, **kwargs): super().__init__(*args, **kwargs) @@ -84,7 +119,7 @@ def main(): config["assign"] = {normalize_mac(k): v for (k, v) in config["assign"].items()} with tempfile.NamedTemporaryFile() as tmpfile: - with open(tmpfile.name, "w") as f: + with open(tmpfile.name, "w", encoding="utf-8") as f: for attribute in config["dictionary"]: f.write(f"ATTRIBUTE {attribute['name']} {attribute['id']} {attribute['type']}\n") @@ -95,6 +130,7 @@ def main(): server = VlanAssign( loop=loop, dictionary=dictionary, + enable_pkt_verify=True, usermap=config["assign"] ) @@ -105,6 +141,8 @@ def main(): address ) + server.hosts = IpaddressDict(server.hosts) + try: # Initialize transports loop.run_until_complete(