2 * @brief Config class implementation. This class represents the actual configuration.
6 * @copyright Intra2net AG
20 #include <boost/foreach.hpp>
21 #include <boost/filesystem.hpp>
22 #include <boost/regex.hpp>
23 #include <boost/algorithm/string.hpp>
27 namespace po = boost::program_options;
28 namespace fs = boost::filesystem;
29 namespace ba = boost::algorithm;
34 * Default Constructor. Available command line and config file options with their default values are defined here.
36 Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
41 , ConfigPath("/etc/bpdyndnsd")
42 , ExternalWarningLog("")
43 , ExternalWarningLevel(0)
48 // initialize Serviceholder
49 ServiceHolder = _serviceholder;
51 // Available service description config options
52 po::options_description opt_desc_service("Service description options");
53 opt_desc_service.add_options()
54 ("protocol",po::value<string>(),"The service protocol.")
55 ("host",po::value<string>(),"The hostname to update.")
56 ("login",po::value<string>(),"Login name.")
57 ("password",po::value<string>(),"Corresponding password.")
58 ("update_interval",po::value<int>()->default_value(-1),"Update interval in minutes.")
59 ("max_updates_within_interval",po::value<int>()->default_value(-1),"How many updates can be made in one interval.")
60 ("dns_cache_ttl",po::value<int>()->default_value(-1),"How long a dns record is valid.")
63 // Available command line only options
64 po::options_description opt_desc_cmd_only("Command line only options");
65 opt_desc_cmd_only.add_options()
66 ("help,?","Show help.")
67 ("version,v","Show version.")
68 ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
71 // Available generic options. Valid on cmd or in config file.
72 po::options_description opt_desc_generic("Generic config options");
73 opt_desc_generic.add_options()
74 ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
75 ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
76 ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
77 ("enable_ipv6",po::value<bool>()->default_value(false),"Try to use IPv6.")
78 ("webcheck_url",po::value<string>()->default_value(""),"Use this URL to determine IP.")
79 ("webcheck_url_alt",po::value<string>()->default_value(""),"Use this alternative URL to determine IP.")
80 ("http_proxy",po::value<string>(),"Use this proxy for all http requests.")
81 ("http_proxy_port",po::value<int>(),"Port of the proxy.")
82 ("external_warning_log",po::value<string>()->default_value(""),"External programm to pass warning log messages to.")
83 ("external_warning_level",po::value<int>()->default_value(0),"Warning messages of which loglevel should be passed to external programm.")
86 // Define valid command line parameters
87 Options_descriptionPtr _opt_desc_cmd(new po::options_description("Command line options"));
88 OptDescCmd = _opt_desc_cmd;
89 _opt_desc_cmd.reset();
90 OptDescCmd->add(opt_desc_cmd_only);
91 OptDescCmd->add(opt_desc_generic);
92 OptDescCmd->add(opt_desc_service);
94 // Define valid config file options
95 Options_descriptionPtr _opt_desc_conf_main(new po::options_description("Config file options"));
96 OptDescConfMain = _opt_desc_conf_main;
97 _opt_desc_conf_main.reset();
98 OptDescConfMain->add(opt_desc_generic);
100 // Define valid service file options
101 Options_descriptionPtr _opt_desc_conf_service(new po::options_description("Service file options"));
102 OptDescConfService = _opt_desc_conf_service;
103 _opt_desc_conf_service.reset();
104 OptDescConfService->add(opt_desc_service);
117 * Parses the command line arguments and does the needed actions.
118 * @param argc Command line argument number given to main.
119 * @param argv[] Pointer to command line argument array given to main.
120 * @return 0 if all is fine, -1 if not.
122 int Config::parse_cmd_line(int argc, char *argv[])
126 po::store(po::parse_command_line(argc, argv, *this->OptDescCmd), VariablesMap);
127 po::notify(VariablesMap);
129 if ( VariablesMap.count("help") )
131 Log->print_usage(OptDescCmd);
134 else if ( VariablesMap.count("version") )
136 Log->print_version();
140 // Create a service object if all needed options are set on the command line
141 if ( VariablesMap.count("protocol") && VariablesMap.count("host") && VariablesMap.count("login") && VariablesMap.count("password") )
143 // Get the cmd parameter values for protocol host login and password
144 string protocol = VariablesMap["protocol"].as<string>();
145 string host = VariablesMap["host"].as<string>();
146 string login = VariablesMap["login"].as<string>();
147 string password = VariablesMap["password"].as<string>();
149 protocol = ba::to_lower_copy(protocol);
151 int update_interval = 0;
152 if ( VariablesMap.count("update_interval") )
153 update_interval = VariablesMap["update_interval"].as<int>();
155 int max_updates_within_interval = 0;
156 if ( VariablesMap.count("max_updates_within_interval") )
157 max_updates_within_interval = VariablesMap["max_updates_within_interval"].as<int>();
159 int dns_cache_ttl = 0;
160 if ( VariablesMap.count("dns_cache_ttl") )
161 dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
163 Service::Ptr service = create_service(protocol,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
166 ServiceHolder->add_service(service);
167 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_dns_cache_ttl() , service->get_actual_ip(), service->get_last_updates());
172 else if ( VariablesMap.count("protocol") || VariablesMap.count("host") || VariablesMap.count("login") || VariablesMap.count("password") )
174 Log->print_missing_cmd_service_option();
175 Log->print_usage(OptDescCmd);
179 if ( VariablesMap.count("config") )
181 fs::path full_config_path = fs::system_complete(fs::path(VariablesMap["config"].as<string>()));
182 ConfigPath = full_config_path.string();
183 if ( !fs::exists(full_config_path) || !fs::is_directory(full_config_path) )
185 // Config path doesn't exist or is not a directory
186 Log->print_error_config_path(ConfigPath);
191 if ( VariablesMap.count("daemon_mode") )
192 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
194 if ( VariablesMap.count("loglevel") )
195 Loglevel = VariablesMap["loglevel"].as<int>();
197 if ( VariablesMap.count("syslog") )
198 Syslog = VariablesMap["syslog"].as<bool>();
200 if ( VariablesMap.count("enable_ipv6") )
201 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
203 if ( VariablesMap.count("webcheck_url") )
204 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
206 if ( VariablesMap.count("webcheck_url_alt") )
207 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
209 if ( VariablesMap.count("http_proxy") && VariablesMap.count("http_proxy_port") )
211 Proxy = VariablesMap["http_proxy"].as<string>();
212 ProxyPort = VariablesMap["http_proxy_port"].as<int>();
214 else if ( VariablesMap.count("http_proxy") || VariablesMap.count("http_proxy_port") )
216 Log->print_missing_cmd_proxy_option();
217 Log->print_usage(OptDescCmd);
221 if ( VariablesMap.count("external_warning_log") )
222 ExternalWarningLog = VariablesMap["external_warning_log"].as<string>();
224 if ( VariablesMap.count("external_warning_level") )
225 ExternalWarningLevel = VariablesMap["external_warning_level"].as<int>();
228 catch(po::unknown_option e)
230 Log->print_unknown_cmd_option(e.what());
231 Log->print_usage(OptDescCmd);
234 catch(po::multiple_occurrences e)
236 Log->print_multiple_cmd_option(e.what());
237 Log->print_usage(OptDescCmd);
245 * Creates a Service object from the given parameters.
246 * @param protocol Protocol to use.
247 * @param host Hostname to update.
248 * @param login Login.
249 * @param password Password.
250 * @return A pointer to the created Service object.
252 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)
254 if(protocol == "dhs")
256 Service::Ptr service_dhs(new DHS(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
259 else if(protocol == "ods")
261 Service::Ptr service_ods(new ODS(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl));
264 else if(protocol == "dyndns")
266 Service::Ptr service_dyndns(new DYNDNS(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
267 return service_dyndns;
271 Log->print_unknown_protocol(protocol);
272 Service::Ptr service;
279 * Loads a service config file, invoked by load_config_from_files.
280 * @param full_filename Filename of the service config file to load.
281 * @return 0 if all is fine, -1 otherwise.
283 int Config::load_service_config_file(const string& full_filename)
285 Log->print_load_service_conf(full_filename);
287 ifstream service_config_file(full_filename.c_str(),ifstream::in);
288 if(service_config_file.is_open())
292 po::variables_map vm;
293 po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->OptDescConfService,true);
294 po::store(parsed_service_options,vm);
297 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
299 // create the corresponding service
300 string protocol = vm["protocol"].as<string>();
301 string host = vm["host"].as<string>();
302 string login = vm["login"].as<string>();
303 string password = vm["password"].as<string>();
305 protocol = ba::to_lower_copy(protocol);
307 int update_interval = 0;
308 if ( vm.count("update_interval") )
309 update_interval = VariablesMap["update_interval"].as<int>();
311 int max_updates_within_interval = 0;
312 if ( vm.count("max_updates_within_interval") )
313 max_updates_within_interval = VariablesMap["max_updates_within_interval"].as<int>();
315 int dns_cache_ttl = 0;
316 if ( vm.count("dns_cache_ttl") )
317 dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
319 Service::Ptr service = create_service(protocol,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
322 ServiceHolder->add_service(service);
323 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_dns_cache_ttl() , service->get_actual_ip(), service->get_last_updates());
328 else if ( vm.count("protocol") || vm.count("host") || vm.count("login") || vm.count("password") )
330 service_config_file.close();
331 Log->print_missing_service_conf_option(full_filename);
335 catch ( po::unknown_option e )
337 // unknown option in config file detected
338 service_config_file.close();
339 Log->print_unknown_service_conf_option(full_filename,e.what());
342 catch(po::multiple_occurrences e)
344 service_config_file.close();
345 Log->print_multiple_service_conf_option(full_filename,e.what());
348 service_config_file.close();
352 // error opening service config file for reading
353 Log->print_error_opening_r(full_filename);
361 * Loads the main config file, invoked by load_config_from_files
362 * @param full_filename The full filename of the main config file to load
363 * @return 0 if all is fine, -1 otherwise
365 int Config::load_main_config_file(const string& full_filename)
367 Log->print_load_main_conf(full_filename);
369 ifstream main_config_file(full_filename.c_str(),ifstream::in);
370 if(main_config_file.is_open())
374 po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->OptDescConfMain,true);
375 po::store(parsed_main_options,VariablesMap);
376 po::notify(VariablesMap);
378 if ( VariablesMap.count("daemon_mode") )
379 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
381 if ( VariablesMap.count("loglevel") )
382 Loglevel = VariablesMap["loglevel"].as<int>();
384 if ( VariablesMap.count("syslog") )
385 Syslog = VariablesMap["syslog"].as<bool>();
387 if ( VariablesMap.count("enable_ipv6") )
388 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
390 if ( VariablesMap.count("webcheck_url") )
391 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
393 if ( VariablesMap.count("webcheck_url_alt") )
394 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
396 if ( VariablesMap.count("http_proxy") && VariablesMap.count("http_proxy_port") )
398 Proxy = VariablesMap["http_proxy"].as<string>();
399 ProxyPort = VariablesMap["http_proxy_port"].as<int>();
401 else if ( VariablesMap.count("http_proxy") || VariablesMap.count("http_proxy_port") )
403 main_config_file.close();
404 Log->print_missing_conf_proxy_option(full_filename);
408 if ( VariablesMap.count("external_warning_log") )
409 ExternalWarningLog = VariablesMap["external_warning_log"].as<string>();
411 if ( VariablesMap.count("external_warning_level") )
412 ExternalWarningLevel = VariablesMap["external_warning_level"].as<int>();
415 catch ( po::unknown_option e ) // at the moment 04-08-2009 this exception is never thrown :-(
417 // unknown option in main config file detected
418 main_config_file.close();
419 Log->print_unknown_main_conf_option(e.what());
422 catch(po::multiple_occurrences e)
424 main_config_file.close();
425 Log->print_multiple_main_conf_option(full_filename,e.what());
428 main_config_file.close();
432 // error opening main config file for reading
433 Log->print_error_opening_r(full_filename);
441 * Loads the main and the service config file and does the needed action.
442 * @param config_path The path to the config directory.
443 * @return 0 if all is fine, -1 otherwise
445 int Config::load_config_from_files()
447 fs::path full_config_path = fs::path(ConfigPath);
449 fs::directory_iterator end_iter;
450 for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr )
452 if( fs::is_regular_file( dir_itr->status() ) )
454 string actual_file = dir_itr->path().filename();
455 boost::regex expr(".*\\.conf?");
456 // If it is the main config file do the following
457 if ( actual_file == "bpdyndnsd.conf" )
459 // Load the main config file
460 string full_filename = dir_itr->path().string();
461 if ( load_main_config_file(full_filename) != 0 )
464 // If it is a service definition file *.conf, parse it and generate the corresponding service
465 else if ( boost::regex_search( actual_file,expr ) )
467 string full_filename = dir_itr->path().string();
468 if ( load_service_config_file(full_filename) != 0 )
473 // Config file successfully loaded
474 Log->print_conf_loaded(ConfigPath);
480 * Getter method for member OptDescCmd.
481 * @return options_description*.
483 Config::Options_descriptionPtr Config::get_opt_desc_cmd() const
490 * Getter method for member OptDescConfMain.
491 * @return options_description*.
493 Config::Options_descriptionPtr Config::get_opt_desc_conf_main() const
495 return OptDescConfMain;
500 * Getter method for member OptDescConfService.
501 * @return options_description*.
503 Config::Options_descriptionPtr Config::get_opt_desc_conf_service() const
505 return OptDescConfService;
510 * Getter for member Loglevel.
511 * @return Member Loglevel.
513 int Config::get_loglevel() const
520 * Getter for member DaemonMode.
521 * @return TRUE if enabled, FALSE if disabled.
523 bool Config::get_daemon_mode() const
530 * Deletes the map with the previously parsed options.
531 * This is needed in case we reload the config and don't want the old cmd options to overwrite new config file options.
533 void Config::delete_variables_map()
535 VariablesMap.clear();
537 po::variables_map _variables_map;
538 VariablesMap = _variables_map;
543 * Getter for member Syslog.
544 * @return True if logging through syslog is enabled, false otherwise.
546 bool Config::get_syslog() const
553 * Getter for member EnableIPv6
554 * @return Wether IPv6 should be used or not.
556 bool Config::get_enable_ipv6() const
563 * Getter for member WebcheckIpUrl
564 * @return The primary IP Webcheck URL
566 string Config::get_webcheck_ip_url() const
568 return WebcheckIpUrl;
573 * Getter for member WebcheckIpUrlAlt
574 * @return The alternative IP Webcheck URL
576 string Config::get_webcheck_ip_url_alt() const
578 return WebcheckIpUrlAlt;
586 string Config::get_proxy() const
593 * Get member ProxyPort
596 int Config::get_proxy_port() const
603 * Get member ExternalWarningLog
604 * @return ExternalWarningLog
606 string Config::get_external_warning_log() const
608 return ExternalWarningLog;
613 * Get member ExternalWarningLevel
614 * @return ExternalWaringLevel
616 int Config::get_external_warning_level() const
618 return ExternalWarningLevel;