|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Wrapper script to be deployed on machines whose network interfaces should be |
| 4 | +# controllable via the RawNetworkInterfaceDriver. A /etc/labgrid/helpers.yaml |
| 5 | +# can deny access to network interfaces. See below. |
| 6 | +# |
| 7 | +# This is intended to be used via sudo. For example, add via visudo: |
| 8 | +# %developers ALL = NOPASSWD: /usr/sbin/labgrid-raw-interface |
| 9 | + |
| 10 | +import argparse |
| 11 | +import os |
| 12 | +import sys |
| 13 | + |
| 14 | +import yaml |
| 15 | + |
| 16 | + |
| 17 | +def get_denylist(): |
| 18 | + denylist_file = "/etc/labgrid/helpers.yaml" |
| 19 | + try: |
| 20 | + with open(denylist_file) as stream: |
| 21 | + data = yaml.load(stream, Loader=yaml.SafeLoader) |
| 22 | + except (PermissionError, FileNotFoundError, AttributeError) as e: |
| 23 | + raise Exception(f"No configuration file ({denylist_file}), inaccessable or invalid yaml") from e |
| 24 | + |
| 25 | + denylist = data.get("raw-interface", {}).get("denied-interfaces", []) |
| 26 | + |
| 27 | + if not isinstance(denylist, list): |
| 28 | + raise Exception("No explicit denied-interfaces or not a list, please check your configuration") |
| 29 | + |
| 30 | + denylist.append("lo") |
| 31 | + |
| 32 | + return denylist |
| 33 | + |
| 34 | + |
| 35 | +def main(program, ifname, count): |
| 36 | + if not ifname: |
| 37 | + raise ValueError("Empty interface name.") |
| 38 | + if any((c == "/" or c.isspace()) for c in ifname): |
| 39 | + raise ValueError(f"Interface name '{ifname}' contains invalid characters.") |
| 40 | + if len(ifname) > 16: |
| 41 | + raise ValueError(f"Interface name '{ifname}' is too long.") |
| 42 | + |
| 43 | + denylist = get_denylist() |
| 44 | + |
| 45 | + if ifname in denylist: |
| 46 | + raise ValueError(f"Interface name '{ifname}' is denied in denylist.") |
| 47 | + |
| 48 | + programs = ["tcpreplay", "tcpdump"] |
| 49 | + if program not in programs: |
| 50 | + raise ValueError(f"Invalid program {program} called with wrapper, valid programs are: {programs}") |
| 51 | + |
| 52 | + args = [ |
| 53 | + program, |
| 54 | + ] |
| 55 | + |
| 56 | + if program == "tcpreplay": |
| 57 | + args.append(f"--intf1={ifname}") |
| 58 | + args.append('-') |
| 59 | + |
| 60 | + if program == "tcpdump": |
| 61 | + args.append("-n") |
| 62 | + args.append(f"--interface={ifname}") |
| 63 | + args.append("-w") |
| 64 | + args.append('-') |
| 65 | + |
| 66 | + if count: |
| 67 | + args.append("-c") |
| 68 | + args.append(str(count)) |
| 69 | + |
| 70 | + try: |
| 71 | + os.execvp(args[0], args) |
| 72 | + except FileNotFoundError as e: |
| 73 | + raise RuntimeError(f"Missing {program} binary") from e |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + parser = argparse.ArgumentParser() |
| 78 | + parser.add_argument( |
| 79 | + '-d', |
| 80 | + '--debug', |
| 81 | + action='store_true', |
| 82 | + default=False, |
| 83 | + help="enable debug mode" |
| 84 | + ) |
| 85 | + parser.add_argument('program', type=str, help='program to run, either tcpreplay or tcpdump') |
| 86 | + parser.add_argument('interface', type=str, help='interface name') |
| 87 | + parser.add_argument('count', nargs="?", type=int, default=None, help='amount of frames to capture while recording') |
| 88 | + args = parser.parse_args() |
| 89 | + try: |
| 90 | + main(args.program, args.interface, args.count) |
| 91 | + except Exception as e: # pylint: disable=broad-except |
| 92 | + if args.debug: |
| 93 | + import traceback |
| 94 | + traceback.print_exc(file=sys.stderr) |
| 95 | + print(f"ERROR: {e}", file=sys.stderr) |
| 96 | + exit(1) |
0 commit comments