diff options
author | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-12 05:21:44 -0500 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-12 05:21:44 -0500 |
commit | 3def7b5e02e052ddc0993d2022d4f0f6ea2ae389 (patch) | |
tree | c12210e11cf6100c803c4032b6be9ec1c280cfad | |
parent | a833356c754c2a6f5adcf0da1d024259cb8be96e (diff) | |
download | horizon-3def7b5e02e052ddc0993d2022d4f0f6ea2ae389.tar.gz horizon-3def7b5e02e052ddc0993d2022d4f0f6ea2ae389.tar.bz2 horizon-3def7b5e02e052ddc0993d2022d4f0f6ea2ae389.tar.xz horizon-3def7b5e02e052ddc0993d2022d4f0f6ea2ae389.zip |
hscript: Implement a lot of 'netaddress' validation logic
-rw-r--r-- | hscript/network.cc | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/hscript/network.cc b/hscript/network.cc index e5ca787..9bcd9a8 100644 --- a/hscript/network.cc +++ b/hscript/network.cc @@ -10,7 +10,9 @@ * SPDX-License-Identifier: AGPL-3.0-only */ +#include <algorithm> #include "network.hh" +#include "util/output.hh" using namespace Horizon::Keys; @@ -31,11 +33,75 @@ bool Network::execute(ScriptOptions) const { Key *NetAddress::parseFromData(const std::string data, int lineno, int *errors, int *warnings) { + long elements = std::count(data.cbegin(), data.cend(), ' ') + 1; + std::string::size_type type_pos, addr_pos, prefix_pos, gw_pos; + std::string iface, type, addr, prefix, gw; + + if(elements < 2) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netaddress: missing address type", + "one of 'dhcp', 'slaac', 'static' required"); + return nullptr; + } + + type_pos = data.find_first_of(' '); + iface = data.substr(0, type_pos); + /* theory: addr_pos could be npos, but that means 'read to end' anyway */ + addr_pos = data.find_first_of(' ', type_pos + 1); + type = data.substr(type_pos + 1, (addr_pos - type_pos - 1)); + + /* ensure type is lower-cased, in case someone types 'DHCP' or 'SLAAC' */ + std::transform(type.begin(), type.end(), type.begin(), ::tolower); + + if(!type.compare("dhcp")) { + if(elements > 2) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netaddress: address type 'dhcp' does not " + "accept further elements"); + return nullptr; + } + return new NetAddress(lineno, iface, AddressType::DHCP, "", 0, ""); + } else if(!type.compare("slaac")) { + if(elements > 2) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netaddress: address type 'slaac' does not " + "accept further elements"); + return nullptr; + } + return new NetAddress(lineno, iface, AddressType::SLAAC, "", 0, ""); + } else if(type.compare("static")) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netaddress: invalid address type '" + type + "'", + "one of 'dhcp', 'slaac', 'static' required"); + return nullptr; + } + + /* static address */ + if(elements < 4) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netaddress: address type 'static' requires at least " + "an IP address and prefix length"); + return nullptr; + } + + if(elements > 5) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netaddress: too many elements to address type 'static'"); + return nullptr; + } + return nullptr; } bool NetAddress::validate(ScriptOptions) const { - return false; + /* possible to validate an address in the Installation Environment? */ + return true; } bool NetAddress::execute(ScriptOptions) const { |