verify username, password, domain from configuration
[dyn-nsupdate.git] / dyn-nsupdate.cpp
1 #include <iostream>
2 #include <fstream>
3
4 #include <boost/regex.hpp>
5 #include <boost/property_tree/ptree.hpp>
6 #include <boost/property_tree/ini_parser.hpp>
7
8 using namespace boost;
9
10 int main(int argc, const char ** argv)
11 {
12         static const regex regex_ip("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}");
13         static const regex regex_user("[a-zA-Z]+");
14         
15         
16         if (argc != 5) {
17                 std::cerr << "Usage: " << argv[0] << " <username> <password> <domain> <IP address>" << std::endl;
18                 return 1;
19         }
20         
21         /* Obtain and validate inpit */
22         std::string user = argv[1];
23         std::string password = argv[2];
24         std::string domain = argv[3];
25         std::string ip = argv[4];
26         
27         if (!regex_match(ip, regex_ip)) {
28                 std::cerr << "Invalid IP address " << ip << "." << std::endl;
29                 return 1;
30         }
31         if (!regex_match(user, regex_user)) {
32                 std::cerr << "Invalid username " << user << "." << std::endl;
33                 return 1;
34         }
35         
36         /* read configuration */
37         property_tree::ptree config;
38         property_tree::ini_parser::read_ini(CONFIG_FILE, config);
39         std::string keyfile = config.get<std::string>("key");
40         
41         /* Check username, password, domain */
42         optional<std::string> correct_password = config.get_optional<std::string>(user+".password");
43         if (!correct_password || *correct_password != password) {
44                 std::cerr << "Username or password incorrect." << std::endl;
45                 return 1;
46         }
47         if (config.get<std::string>(user+".domain") != domain) {
48                 std::cerr << "Domain incorrect." << std::endl;
49                 return 1;
50         }
51         
52         std::cout << "It's all right, using key " << keyfile << std::endl;
53         
54         
55         return 0;
56 }