2 * @brief Config class implementation. This class represents the actual configuration.
6 * @copyright Intra2net AG
19 #include <boost/foreach.hpp>
20 #include <boost/filesystem.hpp>
21 #include <boost/regex.hpp>
22 #include <boost/algorithm/string.hpp>
26 namespace po = boost::program_options;
27 namespace fs = boost::filesystem;
28 namespace ba = boost::algorithm;
33 * Default Constructor. Available command line and config file options with their default values are defined here.
35 Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
40 , ConfigPath("/etc/bpdyndnsd")
45 // initialize Serviceholder
46 ServiceHolder = _serviceholder;
48 // Available service description config options
49 po::options_description opt_desc_service("Service description options");
50 opt_desc_service.add_options()
51 ("protocol",po::value<string>(),"The service protocol.")
52 ("host",po::value<string>(),"The hostname to update.")
53 ("login",po::value<string>(),"Login name.")
54 ("password",po::value<string>(),"Corresponding password.")
55 ("update_interval",po::value<int>()->default_value(-1),"Update interval in minutes.")
56 ("max_updates_within_interval",po::value<int>()->default_value(-1),"How many updates can be made in one interval.")
57 ("dns_cache_ttl",po::value<int>()->default_value(-1),"How long a dns record is valid.")
60 // Available command line only options
61 po::options_description opt_desc_cmd_only("Command line only options");
62 opt_desc_cmd_only.add_options()
63 ("help,?","Show help.")
64 ("version,v","Show version.")
65 ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
68 // Available generic options. Valid on cmd or in config file.
69 po::options_description opt_desc_generic("Generic config options");
70 opt_desc_generic.add_options()
71 ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
72 ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
73 ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
74 ("enable_ipv6",po::value<bool>()->default_value(false),"Try to use IPv6.")
75 ("webcheck_url",po::value<string>()->default_value(""),"Use this URL to determine IP.")
76 ("webcheck_url_alt",po::value<string>()->default_value(""),"Use this alternative URL to determine IP.")
77 ("http_proxy",po::value<string>()->default_value(""),"Use this proxy for all http requests.")
78 ("http_proxy_port",po::value<int>()->default_value(0),"Port of the proxy.")
81 // Define valid command line parameters
82 Options_descriptionPtr _opt_desc_cmd(new po::options_description("Command line options"));
83 OptDescCmd = _opt_desc_cmd;
84 _opt_desc_cmd.reset();
85 OptDescCmd->add(opt_desc_cmd_only);
86 OptDescCmd->add(opt_desc_generic);
87 OptDescCmd->add(opt_desc_service);
89 // Define valid config file options
90 Options_descriptionPtr _opt_desc_conf_main(new po::options_description("Config file options"));
91 OptDescConfMain = _opt_desc_conf_main;
92 _opt_desc_conf_main.reset();
93 OptDescConfMain->add(opt_desc_generic);
95 // Define valid service file options
96 Options_descriptionPtr _opt_desc_conf_service(new po::options_description("Service file options"));
97 OptDescConfService = _opt_desc_conf_service;
98 _opt_desc_conf_service.reset();
99 OptDescConfService->add(opt_desc_service);
112 * Parses the command line arguments and does the needed actions.
113 * @param argc Command line argument number given to main.
114 * @param argv[] Pointer to command line argument array given to main.
115 * @return 0 if all is fine, -1 if not.
117 int Config::parse_cmd_line(int argc, char *argv[])
121 po::store(po::parse_command_line(argc, argv, *this->OptDescCmd), VariablesMap);
122 po::notify(VariablesMap);
124 if ( VariablesMap.count("help") )
126 Log->print_usage(OptDescCmd);
129 else if ( VariablesMap.count("version") )
131 Log->print_version();
135 // Create a service object if all needed options are set on the command line
136 if ( VariablesMap.count("protocol") && VariablesMap.count("host") && VariablesMap.count("login") && VariablesMap.count("password") )
138 // Get the cmd parameter values for protocol host login and password
139 string protocol = VariablesMap["protocol"].as<string>();
140 string host = VariablesMap["host"].as<string>();
141 string login = VariablesMap["login"].as<string>();
142 string password = VariablesMap["password"].as<string>();
144 protocol = ba::to_lower_copy(protocol);
146 int update_interval = 0;
147 if ( VariablesMap.count("update_interval") )
148 update_interval = VariablesMap["update_interval"].as<int>();
150 int max_updates_within_interval = 0;
151 if ( VariablesMap.count("max_updates_within_interval") )
152 max_updates_within_interval = VariablesMap["max_updates_within_interval"].as<int>();
154 int dns_cache_ttl = 0;
155 if ( VariablesMap.count("dns_cache_ttl") )
156 dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
158 Service::Ptr service = create_service(protocol,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
161 ServiceHolder->add_service(service);
162 Log->print_service_object("New Service object from command line options:", service->get_protocol(), service->get_hostname(), service->get_login() ,service->get_password(), service->get_update_interval(), service->get_max_updates_within_interval(), service->get_actual_ip(), service->get_last_updates());
167 else if ( VariablesMap.count("protocol") || VariablesMap.count("host") || VariablesMap.count("login") || VariablesMap.count("password") )
169 Log->print_missing_cmd_service_option();
170 Log->print_usage(OptDescCmd);
174 if ( VariablesMap.count("config") )
176 fs::path full_config_path = fs::system_complete(fs::path(VariablesMap["config"].as<string>()));
177 ConfigPath = full_config_path.string();
178 if ( !fs::exists(full_config_path) || !fs::is_directory(full_config_path) )
180 // Config path doesn't exist or is not a directory
181 Log->print_error_config_path(ConfigPath);
186 if ( VariablesMap.count("daemon_mode") )
187 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
189 if ( VariablesMap.count("loglevel") )
190 Loglevel = VariablesMap["loglevel"].as<int>();
192 if ( VariablesMap.count("syslog") )
193 Syslog = VariablesMap["syslog"].as<bool>();
195 if ( VariablesMap.count("enable_ipv6") )
196 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
198 if ( VariablesMap.count("webcheck_url") )
199 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
201 if ( VariablesMap.count("webcheck_url_alt") )
202 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
204 if ( VariablesMap.count("http_proxy") )
205 Proxy = VariablesMap["http_proxy"].as<string>();
207 if ( VariablesMap.count("http_proxy_port") )
208 ProxyPort = VariablesMap["http_proxy_port"].as<int>();
211 catch(po::unknown_option e)
213 Log->print_unknown_cmd_option(e.what());
214 Log->print_usage(OptDescCmd);
217 catch(po::multiple_occurrences e)
219 Log->print_multiple_cmd_option(e.what());
220 Log->print_usage(OptDescCmd);
228 * Creates a Service object from the given parameters.
229 * @param protocol Protocol to use.
230 * @param host Hostname to update.
231 * @param login Login.
232 * @param password Password.
233 * @return A pointer to the created Service object.
235 Service::Ptr Config::create_service(const string &protocol,const string &hostname, const string &login, const string &password, const int update_interval, const int max_updates_within_interval, const int dns_cache_ttl)
237 if(protocol == "dhs")
239 Service::Ptr service_dhs(new DHS(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl));
242 else if(protocol == "ods")
244 Service::Ptr service_ods(new ODS(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl));
249 Log->print_unknown_protocol(protocol);
250 Service::Ptr service;
257 * Loads a service config file, invoked by load_config_from_files.
258 * @param full_filename Filename of the service config file to load.
259 * @return 0 if all is fine, -1 otherwise.
261 int Config::load_service_config_file(const string& full_filename)
263 Log->print_load_service_conf(full_filename);
265 ifstream service_config_file(full_filename.c_str(),ifstream::in);
266 if(service_config_file.is_open())
270 po::variables_map vm;
271 po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->OptDescConfService,true);
272 po::store(parsed_service_options,vm);
275 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
277 // create the corresponding service
278 string protocol = vm["protocol"].as<string>();
279 string host = vm["host"].as<string>();
280 string login = vm["login"].as<string>();
281 string password = vm["password"].as<string>();
283 protocol = ba::to_lower_copy(protocol);
285 int update_interval = 0;
286 if ( VariablesMap.count("update_interval") )
287 update_interval = VariablesMap["update_interval"].as<int>();
289 int max_updates_within_interval = 0;
290 if ( VariablesMap.count("max_updates_within_interval") )
291 max_updates_within_interval = VariablesMap["max_updates_within_interval"].as<int>();
293 int dns_cache_ttl = 0;
294 if ( VariablesMap.count("dns_cache_ttl") )
295 dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
297 Service::Ptr service = create_service(protocol,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
300 ServiceHolder->add_service(service);
301 Log->print_service_object("New Service object from config:", service->get_protocol(), service->get_hostname(), service->get_login() ,service->get_password(), service->get_update_interval(), service->get_max_updates_within_interval(), service->get_actual_ip(), service->get_last_updates());
307 catch ( po::unknown_option e )
309 // unknown option in config file detected
310 service_config_file.close();
311 Log->print_unknown_service_conf_option(e.what());
314 service_config_file.close();
318 // error opening service config file for reading
319 Log->print_error_opening_r(full_filename);
327 * Loads the main config file, invoked by load_config_from_files
328 * @param full_filename The full filename of the main config file to load
329 * @return 0 if all is fine, -1 otherwise
331 int Config::load_main_config_file(const string& full_filename)
333 Log->print_load_main_conf(full_filename);
335 ifstream main_config_file(full_filename.c_str(),ifstream::in);
336 if(main_config_file.is_open())
340 po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->OptDescConfMain,true);
341 po::store(parsed_main_options,VariablesMap);
342 po::notify(VariablesMap);
344 if ( VariablesMap.count("daemon_mode") )
345 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
347 if ( VariablesMap.count("loglevel") )
348 Loglevel = VariablesMap["loglevel"].as<int>();
350 if ( VariablesMap.count("syslog") )
351 Syslog = VariablesMap["syslog"].as<bool>();
353 if ( VariablesMap.count("enable_ipv6") )
354 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
356 if ( VariablesMap.count("webcheck_url") )
357 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
359 if ( VariablesMap.count("webcheck_url_alt") )
360 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
362 if ( VariablesMap.count("http_proxy") )
363 Proxy = VariablesMap["http_proxy"].as<string>();
365 if ( VariablesMap.count("http_proxy_port") )
366 ProxyPort = VariablesMap["http_proxy_port"].as<int>();
369 catch ( po::unknown_option e ) // at the moment 04-08-2009 this exception is never thrown :-(
371 // unknown option in main config file detected
372 main_config_file.close();
373 Log->print_unknown_main_conf_option(e.what());
376 main_config_file.close();
380 // error opening main config file for reading
381 Log->print_error_opening_r(full_filename);
389 * Loads the main and the service config file and does the needed action.
390 * @param config_path The path to the config directory.
391 * @return 0 if all is fine, -1 otherwise
393 int Config::load_config_from_files()
395 fs::path full_config_path = fs::path(ConfigPath);
397 fs::directory_iterator end_iter;
398 for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr )
400 if( fs::is_regular_file( dir_itr->status() ) )
402 string actual_file = dir_itr->path().filename();
403 boost::regex expr(".*\\.conf?");
404 // If it is the main config file do the following
405 if ( actual_file == "bpdyndnsd.conf" )
407 // Load the main config file
408 string full_filename = dir_itr->path().string();
409 if ( load_main_config_file(full_filename) != 0 )
412 // If it is a service definition file *.conf, parse it and generate the corresponding service
413 else if ( boost::regex_search( actual_file,expr ) )
415 string full_filename = dir_itr->path().string();
416 if ( load_service_config_file(full_filename) != 0 )
421 // Config file successfully loaded
422 Log->print_conf_loaded(ConfigPath);
428 * Getter method for member OptDescCmd.
429 * @return options_description*.
431 Config::Options_descriptionPtr Config::get_opt_desc_cmd() const
438 * Getter method for member OptDescConfMain.
439 * @return options_description*.
441 Config::Options_descriptionPtr Config::get_opt_desc_conf_main() const
443 return OptDescConfMain;
448 * Getter method for member OptDescConfService.
449 * @return options_description*.
451 Config::Options_descriptionPtr Config::get_opt_desc_conf_service() const
453 return OptDescConfService;
458 * Getter for member Loglevel.
459 * @return Member Loglevel.
461 int Config::get_loglevel() const
468 * Getter for member DaemonMode.
469 * @return TRUE if enabled, FALSE if disabled.
471 bool Config::get_daemon_mode() const
478 * Deletes the map with the previously parsed options.
479 * This is needed in case we reload the config and don't want the old cmd options to overwrite new config file options.
481 void Config::delete_variables_map()
483 VariablesMap.clear();
485 po::variables_map _variables_map;
486 VariablesMap = _variables_map;
491 * Getter for member Syslog.
492 * @return True if logging through syslog is enabled, false otherwise.
494 bool Config::get_syslog() const
501 * Getter for member EnableIPv6
502 * @return Wether IPv6 should be used or not.
504 bool Config::get_enable_ipv6() const
511 * Getter for member WebcheckIpUrl
512 * @return The primary IP Webcheck URL
514 string Config::get_webcheck_ip_url() const
516 return WebcheckIpUrl;
521 * Getter for member WebcheckIpUrlAlt
522 * @return The alternative IP Webcheck URL
524 string Config::get_webcheck_ip_url_alt() const
526 return WebcheckIpUrlAlt;
534 string Config::get_proxy() const
541 * Get member ProxyPort
544 int Config::get_proxy_port() const