2 * @brief Config class implementation. This class represents the actual configuration.
6 * @copyright Intra2net AG
12 #include "service_dhs.h"
13 #include "service_ods.h"
14 #include "service_dyndns.h"
15 #include "service_dyns.h"
16 #include "service_easydns.h"
17 #include "service_tzo.h"
18 #include "service_zoneedit.h"
19 #include "service_gnudip.h"
25 #include <boost/foreach.hpp>
26 #include <boost/filesystem.hpp>
27 #include <boost/regex.hpp>
28 #include <boost/algorithm/string.hpp>
31 namespace po = boost::program_options;
32 namespace fs = boost::filesystem;
33 namespace ba = boost::algorithm;
35 typedef boost::shared_ptr<boost::program_options::options_description> Options_descriptionPtr;
41 * Default Constructor. Available command line and config file options with their default values are defined here.
45 , ServiceHolder(new Serviceholder)
50 , ConfigPath("/etc/bpdyndnsd")
53 , ExternalWarningLog("")
54 , ExternalWarningLevel(0)
56 , WebcheckEnabled(false)
58 // Available service description config options
59 po::options_description opt_desc_service("Service description options");
60 opt_desc_service.add_options()
61 ("protocol",po::value<string>(),"The service protocol.")
62 ("server",po::value<string>(),"Servername needed for gnudip protocol.")
63 ("host",po::value<string>(),"The hostname to update.")
64 ("login",po::value<string>(),"Login name.")
65 ("password",po::value<string>(),"Corresponding password.")
66 ("update_interval",po::value<int>()->default_value(-1),"Update interval in minutes.")
67 ("max_updates_within_interval",po::value<int>()->default_value(-1),"How many updates can be made in one interval.")
68 ("dns_cache_ttl",po::value<int>()->default_value(-1),"How long a dns record is valid.")
71 // Available command line only options
72 po::options_description opt_desc_cmd_only("Command line only options");
73 opt_desc_cmd_only.add_options()
74 ("help,?","Show help.")
75 ("version,v","Show version.")
76 ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
79 // Available generic options. Valid on cmd or in config file.
80 po::options_description opt_desc_generic("Generic config options");
81 opt_desc_generic.add_options()
82 ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
83 ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
84 ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
85 ("enable_ipv6",po::value<bool>()->default_value(false),"Try to use IPv6.")
86 ("webcheck_enabled",po::value<bool>()->default_value(false),"Use webcheck url to determine actual IP address.")
87 ("webcheck_url",po::value<string>()->default_value(""),"Use this URL to determine IP.")
88 ("webcheck_url_alt",po::value<string>()->default_value(""),"Use this alternative URL to determine IP.")
89 ("webcheck_interval",po::value<int>()->default_value(10),"The webcheck interval in minutes.")
90 ("http_proxy",po::value<string>(),"Use this proxy for all http requests.")
91 ("http_proxy_port",po::value<int>(),"Port of the proxy.")
92 ("external_warning_log",po::value<string>()->default_value(""),"External programm to pass warning log messages to.")
93 ("external_warning_level",po::value<int>()->default_value(0),"Warning messages of which loglevel should be passed to external programm.")
94 ("start_offline",po::value<bool>()->default_value(false),"Start in offline mode.")
97 // Define valid command line parameters
98 OptDescCmd = Options_descriptionPtr(new po::options_description("Command line options"));
99 OptDescCmd->add(opt_desc_cmd_only);
100 OptDescCmd->add(opt_desc_generic);
101 OptDescCmd->add(opt_desc_service);
103 // Define valid config file options
104 OptDescConfMain = Options_descriptionPtr(new po::options_description("Config file options"));
105 OptDescConfMain->add(opt_desc_generic);
107 // Define valid service file options
108 OptDescConfService = Options_descriptionPtr(new po::options_description("Service file options"));
109 OptDescConfService->add(opt_desc_service);
114 * Constructor with Logger and Serviceholder objects. Available command line and config file options with their default values are defined here.
116 Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
118 , ServiceHolder(_serviceholder)
123 , ConfigPath("/etc/bpdyndnsd")
124 , WebcheckInterval(0)
126 , ExternalWarningLog("")
127 , ExternalWarningLevel(0)
128 , StartOffline(false)
129 , WebcheckEnabled(false)
131 // Available service description config options
132 po::options_description opt_desc_service("Service description options");
133 opt_desc_service.add_options()
134 ("protocol",po::value<string>(),"The service protocol.")
135 ("server",po::value<string>(),"Servername needed for gnudip protocol.")
136 ("host",po::value<string>(),"The hostname to update.")
137 ("login",po::value<string>(),"Login name.")
138 ("password",po::value<string>(),"Corresponding password.")
139 ("update_interval",po::value<int>()->default_value(-1),"Update interval in minutes.")
140 ("max_updates_within_interval",po::value<int>()->default_value(-1),"How many updates can be made in one interval.")
141 ("dns_cache_ttl",po::value<int>()->default_value(-1),"How long a dns record is valid.")
144 // Available command line only options
145 po::options_description opt_desc_cmd_only("Command line only options");
146 opt_desc_cmd_only.add_options()
147 ("help,?","Show help.")
148 ("version,v","Show version.")
149 ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
152 // Available generic options. Valid on cmd or in config file.
153 po::options_description opt_desc_generic("Generic config options");
154 opt_desc_generic.add_options()
155 ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
156 ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
157 ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
158 ("enable_ipv6",po::value<bool>()->default_value(false),"Try to use IPv6.")
159 ("webcheck_enabled",po::value<bool>()->default_value(false),"Use webcheck url to determine actual IP address.")
160 ("webcheck_url",po::value<string>()->default_value(""),"Use this URL to determine IP.")
161 ("webcheck_url_alt",po::value<string>()->default_value(""),"Use this alternative URL to determine IP.")
162 ("webcheck_interval",po::value<int>()->default_value(10),"The webcheck interval in minutes.")
163 ("http_proxy",po::value<string>(),"Use this proxy for all http requests.")
164 ("http_proxy_port",po::value<int>(),"Port of the proxy.")
165 ("external_warning_log",po::value<string>()->default_value(""),"External programm to pass warning log messages to.")
166 ("external_warning_level",po::value<int>()->default_value(0),"Warning messages of which loglevel should be passed to external programm.")
167 ("start_offline",po::value<bool>()->default_value(false),"Start in offline mode.")
170 // Define valid command line parameters
171 OptDescCmd = Options_descriptionPtr(new po::options_description("Command line options"));
172 OptDescCmd->add(opt_desc_cmd_only);
173 OptDescCmd->add(opt_desc_generic);
174 OptDescCmd->add(opt_desc_service);
176 // Define valid config file options
177 OptDescConfMain = Options_descriptionPtr(new po::options_description("Config file options"));
178 OptDescConfMain->add(opt_desc_generic);
180 // Define valid service file options
181 OptDescConfService = Options_descriptionPtr(new po::options_description("Service file options"));
182 OptDescConfService->add(opt_desc_service);
195 * Parses the command line arguments and does the needed actions.
196 * @param argc Command line argument number given to main.
197 * @param argv[] Pointer to command line argument array given to main.
198 * @return 0 if all is fine, -1 if not.
200 int Config::parse_cmd_line(int argc, char *argv[])
204 po::store(po::parse_command_line(argc, argv, *this->OptDescCmd), VariablesMap);
205 po::notify(VariablesMap);
207 if ( VariablesMap.count("help") )
209 Log->print_usage(OptDescCmd);
212 else if ( VariablesMap.count("version") )
214 Log->print_version();
218 // Create a service object if all needed options are set on the command line
219 if ( VariablesMap.count("protocol") && VariablesMap.count("host") && VariablesMap.count("login") && VariablesMap.count("password") )
221 // Get the cmd parameter values for protocol host login and password
222 string protocol = VariablesMap["protocol"].as<string>();
223 string host = VariablesMap["host"].as<string>();
224 string login = VariablesMap["login"].as<string>();
225 string password = VariablesMap["password"].as<string>();
227 protocol = ba::to_lower_copy(protocol);
230 if ( VariablesMap.count("server") )
231 server = VariablesMap["server"].as<string>();
233 int update_interval = 0;
234 if ( VariablesMap.count("update_interval") )
235 update_interval = VariablesMap["update_interval"].as<int>();
237 int max_updates_within_interval = 0;
238 if ( VariablesMap.count("max_updates_within_interval") )
239 max_updates_within_interval = VariablesMap["max_updates_within_interval"].as<int>();
241 int dns_cache_ttl = 0;
242 if ( VariablesMap.count("dns_cache_ttl") )
243 dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
245 Service::Ptr service = create_service(protocol,server,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
248 ServiceHolder->add_service(service);
249 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());
254 else if ( VariablesMap.count("protocol") || VariablesMap.count("host") || VariablesMap.count("login") || VariablesMap.count("password") )
256 Log->print_missing_cmd_service_option();
257 Log->print_usage(OptDescCmd);
261 if ( VariablesMap.count("config") )
263 fs::path full_config_path = fs::system_complete(fs::path(VariablesMap["config"].as<string>()));
264 ConfigPath = full_config_path.string();
265 if ( !fs::exists(full_config_path) || !fs::is_directory(full_config_path) )
267 // Config path doesn't exist or is not a directory
268 Log->print_error_config_path(ConfigPath);
273 if ( VariablesMap.count("daemon_mode") )
274 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
276 if ( VariablesMap.count("loglevel") )
277 Loglevel = VariablesMap["loglevel"].as<int>();
279 if ( VariablesMap.count("syslog") )
280 Syslog = VariablesMap["syslog"].as<bool>();
282 if ( VariablesMap.count("enable_ipv6") )
283 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
285 if ( VariablesMap.count("webcheck_enabled") )
286 WebcheckEnabled = VariablesMap["webcheck_enabled"].as<bool>();
288 if ( VariablesMap.count("webcheck_url") )
289 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
291 if ( VariablesMap.count("webcheck_url_alt") )
292 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
294 if ( VariablesMap.count("webcheck_interval") )
295 WebcheckInterval = VariablesMap["webcheck_interval"].as<int>();
297 if ( VariablesMap.count("http_proxy") && VariablesMap.count("http_proxy_port") )
299 Proxy = VariablesMap["http_proxy"].as<string>();
300 ProxyPort = VariablesMap["http_proxy_port"].as<int>();
302 else if ( VariablesMap.count("http_proxy") || VariablesMap.count("http_proxy_port") )
304 Log->print_missing_cmd_proxy_option();
305 Log->print_usage(OptDescCmd);
309 if ( VariablesMap.count("external_warning_log") )
310 ExternalWarningLog = VariablesMap["external_warning_log"].as<string>();
312 if ( VariablesMap.count("external_warning_level") )
313 ExternalWarningLevel = VariablesMap["external_warning_level"].as<int>();
315 if ( VariablesMap.count("start_offline") )
316 StartOffline = VariablesMap["start_offline"].as<bool>();
319 catch( const po::unknown_option& e )
321 Log->print_unknown_cmd_option(e.what());
322 Log->print_usage(OptDescCmd);
325 catch( const po::multiple_occurrences& e )
327 Log->print_multiple_cmd_option(e.what());
328 Log->print_usage(OptDescCmd);
331 catch( const po::error& e )
333 Log->print_error_parsing_cmd(e.what());
334 Log->print_usage(OptDescCmd);
342 * Creates a Service object from the given parameters.
343 * @param protocol Protocol to use.
344 * @param host Hostname to update.
345 * @param login Login.
346 * @param password Password.
347 * @return A pointer to the created Service object.
349 Service::Ptr Config::create_service(const string &protocol, const string& server, const string& hostname, const string& login, const string& password, const int update_interval, const int max_updates_within_interval, const int dns_cache_ttl)
351 // Test for valid hostname. Must contain 3 parts minimum.
352 list<string> fqhn_parts;
353 ba::split(fqhn_parts,hostname,boost::is_any_of("."));
354 if ( fqhn_parts.size() < 3 )
356 Log->print_invalid_hostname(protocol);
357 Service::Ptr service;
361 if(protocol == "dhs")
363 Service::Ptr service_dhs(new ServiceDhs(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
366 else if(protocol == "ods")
368 Service::Ptr service_ods(new ServiceOds(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl));
371 else if(protocol == "dyndns")
373 Service::Ptr service_dyndns(new ServiceDyndns(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
374 return service_dyndns;
376 else if(protocol == "dyns")
378 Service::Ptr service_dyns(new ServiceDyns(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
381 else if(protocol == "easydns")
383 Service::Ptr service_easydns(new ServiceEasydns(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
384 return service_easydns;
386 else if(protocol == "tzo")
388 Service::Ptr service_tzo(new ServiceTzo(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
391 else if(protocol == "zoneedit")
393 Service::Ptr service_zoneedit(new ServiceZoneedit(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
394 return service_zoneedit;
396 else if(protocol == "gnudip")
398 cout << "Server: " << server << endl;
399 if ( !server.empty() )
401 Service::Ptr service_gnudip(new ServiceGnudip(protocol,server,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
402 return service_gnudip;
406 Log->print_gnudip_requires_servername();
407 Service::Ptr service;
413 Log->print_unknown_protocol(protocol);
414 Service::Ptr service;
421 * Loads a service config file, invoked by load_config_from_files.
422 * @param full_filename Filename of the service config file to load.
423 * @return 0 if all is fine, -1 otherwise.
425 int Config::load_service_config_file(const string& full_filename)
427 Log->print_load_service_conf(full_filename);
429 ifstream service_config_file(full_filename.c_str(),ifstream::in);
430 if(service_config_file.is_open())
434 po::variables_map vm;
435 po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->OptDescConfService,true);
436 po::store(parsed_service_options,vm);
439 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
441 // create the corresponding service
442 string protocol = vm["protocol"].as<string>();
443 string host = vm["host"].as<string>();
444 string login = vm["login"].as<string>();
445 string password = vm["password"].as<string>();
447 protocol = ba::to_lower_copy(protocol);
450 if ( vm.count("server") )
451 server = vm["server"].as<string>();
453 int update_interval = 0;
454 if ( vm.count("update_interval") )
455 update_interval = vm["update_interval"].as<int>();
457 int max_updates_within_interval = 0;
458 if ( vm.count("max_updates_within_interval") )
459 max_updates_within_interval = vm["max_updates_within_interval"].as<int>();
461 int dns_cache_ttl = 0;
462 if ( vm.count("dns_cache_ttl") )
463 dns_cache_ttl = vm["dns_cache_ttl"].as<int>();
465 Service::Ptr service = create_service(protocol,server,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
468 ServiceHolder->add_service(service);
469 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());
474 else if ( vm.count("protocol") || vm.count("host") || vm.count("login") || vm.count("password") )
476 service_config_file.close();
477 Log->print_missing_service_conf_option(full_filename);
481 catch( const po::unknown_option& e )
483 // unknown option in config file detected
484 service_config_file.close();
485 Log->print_unknown_service_conf_option(full_filename,e.what());
488 catch( const po::multiple_occurrences& e )
490 service_config_file.close();
491 Log->print_multiple_service_conf_option(full_filename,e.what());
494 catch( const po::error& e )
496 service_config_file.close();
497 Log->print_error_parsing_config_file(full_filename,e.what());
500 service_config_file.close();
504 // error opening service config file for reading
505 Log->print_error_opening_r(full_filename);
513 * Loads the main config file, invoked by load_config_from_files
514 * @param full_filename The full filename of the main config file to load
515 * @return 0 if all is fine, -1 otherwise
517 int Config::load_main_config_file(const string& full_filename)
519 Log->print_load_main_conf(full_filename);
521 ifstream main_config_file(full_filename.c_str(),ifstream::in);
522 if(main_config_file.is_open())
526 po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->OptDescConfMain,true);
527 po::store(parsed_main_options,VariablesMap);
528 po::notify(VariablesMap);
530 if ( VariablesMap.count("daemon_mode") )
531 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
533 if ( VariablesMap.count("loglevel") )
534 Loglevel = VariablesMap["loglevel"].as<int>();
536 if ( VariablesMap.count("syslog") )
537 Syslog = VariablesMap["syslog"].as<bool>();
539 if ( VariablesMap.count("enable_ipv6") )
540 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
542 if ( VariablesMap.count("webcheck_enabled") )
543 WebcheckEnabled = VariablesMap["webcheck_enabled"].as<bool>();
545 if ( VariablesMap.count("webcheck_url") )
546 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
548 if ( VariablesMap.count("webcheck_url_alt") )
549 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
551 if ( VariablesMap.count("webcheck_interval") )
552 WebcheckInterval = VariablesMap["webcheck_interval"].as<int>();
554 if ( VariablesMap.count("http_proxy") && VariablesMap.count("http_proxy_port") )
556 Proxy = VariablesMap["http_proxy"].as<string>();
557 ProxyPort = VariablesMap["http_proxy_port"].as<int>();
559 else if ( VariablesMap.count("http_proxy") || VariablesMap.count("http_proxy_port") )
561 main_config_file.close();
562 Log->print_missing_conf_proxy_option(full_filename);
566 if ( VariablesMap.count("external_warning_log") )
567 ExternalWarningLog = VariablesMap["external_warning_log"].as<string>();
569 if ( VariablesMap.count("external_warning_level") )
570 ExternalWarningLevel = VariablesMap["external_warning_level"].as<int>();
572 if ( VariablesMap.count("start_offline") )
573 StartOffline = VariablesMap["start_offline"].as<bool>();
576 catch( const po::unknown_option& e ) // at the moment 04-08-2009 this exception is never thrown :-(
578 // unknown option in main config file detected
579 main_config_file.close();
580 Log->print_unknown_main_conf_option(e.what());
583 catch( const po::multiple_occurrences& e )
585 main_config_file.close();
586 Log->print_multiple_main_conf_option(full_filename,e.what());
589 main_config_file.close();
593 // error opening main config file for reading
594 Log->print_error_opening_r(full_filename);
602 * Loads the main and the service config file and does the needed action.
603 * @param config_path The path to the config directory.
604 * @return 0 if all is fine, -1 otherwise
606 int Config::load_config_from_files()
608 fs::path full_config_path = fs::path(ConfigPath);
610 fs::directory_iterator end_iter;
611 for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr )
613 if( fs::is_regular_file( dir_itr->status() ) )
615 string actual_file = dir_itr->path().filename();
616 boost::regex expr(".*\\.conf?");
617 // If it is the main config file do the following
618 if ( actual_file == "bpdyndnsd.conf" )
620 // Load the main config file
621 string full_filename = dir_itr->path().string();
622 if ( load_main_config_file(full_filename) != 0 )
625 // If it is a service definition file *.conf, parse it and generate the corresponding service
626 else if ( boost::regex_search( actual_file,expr ) )
628 string full_filename = dir_itr->path().string();
629 if ( load_service_config_file(full_filename) != 0 )
634 // Config file successfully loaded
635 Log->print_conf_loaded(ConfigPath);
641 * Getter method for member OptDescCmd.
642 * @return options_description*.
644 Options_descriptionPtr Config::get_opt_desc_cmd() const
651 * Getter method for member OptDescConfMain.
652 * @return options_description*.
654 Options_descriptionPtr Config::get_opt_desc_conf_main() const
656 return OptDescConfMain;
661 * Getter method for member OptDescConfService.
662 * @return options_description*.
664 Options_descriptionPtr Config::get_opt_desc_conf_service() const
666 return OptDescConfService;
671 * Getter for member Loglevel.
672 * @return Member Loglevel.
674 int Config::get_loglevel() const
681 * Getter for member DaemonMode.
682 * @return TRUE if enabled, FALSE if disabled.
684 bool Config::get_daemon_mode() const
691 * Deletes the map with the previously parsed options.
692 * This is needed in case we reload the config and don't want the old cmd options to overwrite new config file options.
694 void Config::delete_variables_map()
696 VariablesMap.clear();
698 po::variables_map _variables_map;
699 VariablesMap = _variables_map;
704 * Getter for member Syslog.
705 * @return True if logging through syslog is enabled, false otherwise.
707 bool Config::get_syslog() const
714 * Getter for member EnableIPv6
715 * @return Wether IPv6 should be used or not.
717 bool Config::get_enable_ipv6() const
724 * Getter for member WebcheckEnabled
725 * @return Is webcheck enabled by default.
727 bool Config::get_webcheck_enabled() const
729 return WebcheckEnabled;
734 * Setter for member WebcheckEnabled
735 * @return Is webcheck enabled by default.
737 void Config::set_webcheck_enabled( bool webcheck_enabled )
739 WebcheckEnabled = webcheck_enabled;
744 * Getter for member WebcheckIpUrl
745 * @return The primary IP Webcheck URL
747 string Config::get_webcheck_ip_url() const
749 return WebcheckIpUrl;
754 * Getter for member WebcheckIpUrlAlt
755 * @return The alternative IP Webcheck URL
757 string Config::get_webcheck_ip_url_alt() const
759 return WebcheckIpUrlAlt;
764 * Get member WebcheckInterval
765 * @return WebcheckInterval
767 int Config::get_webcheck_interval() const
769 return WebcheckInterval;
777 string Config::get_proxy() const
784 * Get member ProxyPort
787 int Config::get_proxy_port() const
794 * Get member ExternalWarningLog
795 * @return ExternalWarningLog
797 string Config::get_external_warning_log() const
799 return ExternalWarningLog;
804 * Get member ExternalWarningLevel
805 * @return ExternalWaringLevel
807 int Config::get_external_warning_level() const
809 return ExternalWarningLevel;
814 * Get member StartOffline
815 * @return StartOffline
817 bool Config::get_start_offline() const