From: Ralf Jung Date: Fri, 9 Aug 2013 19:43:01 +0000 (+0200) Subject: verify username, password, domain from configuration X-Git-Url: https://git.ralfj.de/dyn-nsupdate.git/commitdiff_plain/7d0063d7a7006bfb7b8a3051f42285f894183d1c verify username, password, domain from configuration --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 5879f85..f714aeb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 2.6) project(Dyn-NSupdate) -FIND_PACKAGE( Boost 1.40 REQUIRED ) +FIND_PACKAGE( Boost 1.40 COMPONENTS regex REQUIRED ) INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} ) set(DYNNSUPDATE_CONFIG_FILE "/some/config/file" CACHE FILEPATH "Choose the file dyn-nsupdate reads its configuration from (for security reasons, this is hard-coded in the binary)") diff --git a/dyn-nsupdate.cpp b/dyn-nsupdate.cpp index 745595d..cdd1dc4 100644 --- a/dyn-nsupdate.cpp +++ b/dyn-nsupdate.cpp @@ -1,16 +1,56 @@ #include #include +#include #include #include -using namespace boost::property_tree; +using namespace boost; -int main(int, const char **) +int main(int argc, const char ** argv) { - ptree config; - ini_parser::read_ini(CONFIG_FILE, config); + static const regex regex_ip("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}"); + static const regex regex_user("[a-zA-Z]+"); + + + if (argc != 5) { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + return 1; + } + + /* Obtain and validate inpit */ + std::string user = argv[1]; + std::string password = argv[2]; + std::string domain = argv[3]; + std::string ip = argv[4]; + + if (!regex_match(ip, regex_ip)) { + std::cerr << "Invalid IP address " << ip << "." << std::endl; + return 1; + } + if (!regex_match(user, regex_user)) { + std::cerr << "Invalid username " << user << "." << std::endl; + return 1; + } + + /* read configuration */ + property_tree::ptree config; + property_tree::ini_parser::read_ini(CONFIG_FILE, config); + std::string keyfile = config.get("key"); + + /* Check username, password, domain */ + optional correct_password = config.get_optional(user+".password"); + if (!correct_password || *correct_password != password) { + std::cerr << "Username or password incorrect." << std::endl; + return 1; + } + if (config.get(user+".domain") != domain) { + std::cerr << "Domain incorrect." << std::endl; + return 1; + } + + std::cout << "It's all right, using key " << keyfile << std::endl; + - std::cout << "Hi world! " << CONFIG_FILE << std::endl; return 0; }