diff options
author | sarthurdev <965089+sarthurdev@users.noreply.github.com> | 2022-01-17 16:46:03 +0100 |
---|---|---|
committer | sarthurdev <965089+sarthurdev@users.noreply.github.com> | 2022-01-17 16:46:03 +0100 |
commit | 53c2b62dda5bcd1f605a8b9ea438f0f76e366e36 (patch) | |
tree | 844e28cf34ec23d3be05d435887f9f4ae9016071 /src | |
parent | ab4dd3b7a65d10a936071ee1bf0c5c2c884bb1f5 (diff) | |
download | vyos-1x-53c2b62dda5bcd1f605a8b9ea438f0f76e366e36.tar.gz vyos-1x-53c2b62dda5bcd1f605a8b9ea438f0f76e366e36.zip |
firewall: T2199: Fix `port-range` validator to accept service names
Diffstat (limited to 'src')
-rwxr-xr-x | src/validators/port-range | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/validators/port-range b/src/validators/port-range index 6c01048f0..5468000a7 100755 --- a/src/validators/port-range +++ b/src/validators/port-range @@ -3,6 +3,19 @@ import sys import re +from vyos.util import read_file + +services_file = '/etc/services' + +def get_services(): + names = [] + service_data = read_file(services_file, "") + for line in service_data.split("\n"): + if not line or line[0] == '#': + continue + names.append(line.split(None, 1)[0]) + return names + def error(port_range): print(f'Error: {port_range} is not a valid port or port range') sys.exit(1) @@ -16,8 +29,11 @@ if __name__ == '__main__': error(port_range) if int(port_1) > int(port_2): error(port_range) - elif not port_range.isnumeric() or int(port_range) not in range(1, 65536): - error(port_range) + elif port_range.isnumeric() and int(port_range) not in range(1, 65536): + error(port_range) + elif not port_range.isnumeric() and port_range not in get_services(): + print(f'Error: {port_range} is not a valid service name') + sys.exit(1) else: sys.exit(2) |