verify username, password, domain from configuration
[dyn-nsupdate.git] / dyn-nsupdate.cpp
index e378082c30ea4c9f0471e830f983c72535944621..cdd1dc493db31a9859108ddadd94e9328ba7bfac 100644 (file)
@@ -1,37 +1,56 @@
 #include <iostream>
+#include <fstream>
+
+#include <boost/regex.hpp>
 #include <boost/property_tree/ptree.hpp>
 #include <boost/property_tree/ini_parser.hpp>
 
-#include <sys/stat.h>
+using namespace boost;
 
-int main(int argc, const char **argv)
+int main(int argc, const char ** argv)
 {
-       if (argc < 2) {
-               std::cerr << "Usage: " << argv[0] << " <configuration file>" << std::endl;
+       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] << " <username> <password> <domain> <IP address>" << std::endl;
                return 1;
        }
-       const char *filename = argv[1];
        
-       struct stat file_stat;
-       int ret = lstat(filename, &file_stat);
-       if (ret != 0) {
-               std::cerr << "Unable to stat " << filename << "." << std::endl;
+       /* 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;
        }
-       /* Check if the file is suited */
-       if (!S_ISREG(file_stat.st_mode)) {
-               std::cerr << filename << " is not a file." << std::endl;
+       if (!regex_match(user, regex_user)) {
+               std::cerr << "Invalid username " << user << "." << std::endl;
                return 1;
        }
-       if (file_stat.st_uid != geteuid()) {
-               std::cerr << filename << " must be owned by user executing " << argv[0] << "." << std::endl;
+       
+       /* read configuration */
+       property_tree::ptree config;
+       property_tree::ini_parser::read_ini(CONFIG_FILE, config);
+       std::string keyfile = config.get<std::string>("key");
+       
+       /* Check username, password, domain */
+       optional<std::string> correct_password = config.get_optional<std::string>(user+".password");
+       if (!correct_password || *correct_password != password) {
+               std::cerr << "Username or password incorrect." << std::endl;
                return 1;
        }
-       if (file_stat.st_mode & (S_IWGRP | S_IWOTH)) { /* can be written by group/others */
-               std::cerr << filename << " must not be writeable by group or others." << std::endl;
+       if (config.get<std::string>(user+".domain") != domain) {
+               std::cerr << "Domain incorrect." << std::endl;
                return 1;
        }
        
-       std::cout << "Hi world!" << std::endl;
+       std::cout << "It's all right, using key " << keyfile << std::endl;
+       
+       
        return 0;
 }