From c3612be4c3c51ccbe0e4ab27a9d7cf02e0ff3819 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Fri, 1 May 2020 13:08:38 +0200 Subject: Check for double values in config --- TODO | 1 - config.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/TODO b/TODO index 8ece901..9fe3dc2 100644 --- a/TODO +++ b/TODO @@ -5,7 +5,6 @@ Integrate into Debian: WNPP read: The socket was closed due to a timeout webbox: Info if not selected: all webbox: Copy function -config consistency: check double keys new plugin: redirect-plugin new plugin: fcgi support alternative SSL libs: mbedtls, gnutls, wolfssl, botan, (matrixssl, libressl, cryptlib: not in debian) diff --git a/config.cpp b/config.cpp index 793fa67..a805028 100644 --- a/config.cpp +++ b/config.cpp @@ -48,7 +48,10 @@ void Config::readConfigfile(std::string filename) if (elements) { for (const auto& element: *elements) { if (element.first == "plugin-directory"s) { - m_plugin_directories.push_back(element.second.data()); + if (std::find(m_plugin_directories.begin(), m_plugin_directories.end(), element.second.data()) == m_plugin_directories.end()) + m_plugin_directories.push_back(element.second.data()); + else + throw std::runtime_error("Found double plugin-directory element: "s + element.second.data()); } else if (element.first == "sites"s) { for (const auto& site: element.second) { if (site.first != "site"s) @@ -56,9 +59,15 @@ void Config::readConfigfile(std::string filename) Site site_struct; for (const auto& x: site.second) { if (x.first == "name"s) { - site_struct.name = x.second.data(); + if (site_struct.name == "") + site_struct.name = x.second.data(); + else + throw std::runtime_error("Found double site name: "s + x.second.data()); } else if (x.first == "host"s) { - site_struct.hosts.insert(x.second.data()); + if (std::find(site_struct.hosts.begin(), site_struct.hosts.end(), x.second.data()) == site_struct.hosts.end()) + site_struct.hosts.insert(x.second.data()); + else + throw std::runtime_error("Found double site host element: "s + x.second.data()); } else if (x.first == "path"s) { Path path; auto attrs = x.second.get_child(""); @@ -68,25 +77,43 @@ void Config::readConfigfile(std::string filename) if (param.first == "auth") { try { std::string login{param.second.get(".login")}; - std::string password{param.second.get(".password")}; - path.auth[login] = password; + if (path.auth.find(login) == path.auth.end()) { + std::string password{param.second.get(".password")}; + path.auth[login] = password; + } else + throw std::runtime_error("Found double auth (login): "s + login); } catch (const std::exception& ex) { std::cerr << "Warning: Can't read auth data from config: " << ex.what() << std::endl; } } else { - path.params[param.first] = param.second.data(); + if (path.params.find(param.first) == path.params.end()) + path.params[param.first] = param.second.data(); + else + throw std::runtime_error("Found double path param: "s + param.first + ": " + param.second.data()); } } } - site_struct.paths.push_back(path); + if (std::find_if(site_struct.paths.begin(), site_struct.paths.end(), [&](const Path& p){ return p.requested == path.requested;}) == site_struct.paths.end()) + site_struct.paths.push_back(path); + else + throw std::runtime_error("Found double path spec: "s + path.requested); } else if (x.first == "certpath"s) { - site_struct.cert_path = x.second.data(); + if (site_struct.cert_path == "") + site_struct.cert_path = x.second.data(); + else + throw std::runtime_error("Found double certpath: "s + x.second.data()); } else if (x.first == "keypath"s) { - site_struct.key_path = x.second.data(); + if (site_struct.key_path == "") + site_struct.key_path = x.second.data(); + else + throw std::runtime_error("Found double keypath: "s + x.second.data()); } else throw std::runtime_error("Unknown element: "s + x.first); } - m_sites.push_back(site_struct); + if (std::find_if(m_sites.begin(), m_sites.end(), [&](const Site& site){return site.name == site_struct.name;}) == m_sites.end()) + m_sites.push_back(site_struct); + else + throw std::runtime_error("Found double site spec: "s + site_struct.name); } } else if (element.first == "sockets"s) { for (const auto& socket: element.second) { @@ -95,9 +122,15 @@ void Config::readConfigfile(std::string filename) Socket socket_struct; for (const auto& x: socket.second) { if (x.first == "address"s) { - socket_struct.address = x.second.data(); + if (socket_struct.address == "") + socket_struct.address = x.second.data(); + else + throw std::runtime_error("Found double address spec: "s + x.second.data()); } else if (x.first == "port"s) { - socket_struct.port = x.second.data(); + if (socket_struct.port == "") + socket_struct.port = x.second.data(); + else + throw std::runtime_error("Found double port spec: "s + x.second.data()); } else if (x.first == "protocol"s) { if (x.second.data() == "http"s) socket_struct.protocol = SocketProtocol::HTTP; -- cgit v1.2.3