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>
32 namespace po = boost::program_options;
33 namespace fs = boost::filesystem;
34 namespace ba = boost::algorithm;
39 * Default Constructor. Available command line and config file options with their default values are defined here.
41 Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
43 , ServiceHolder(_serviceholder)
48 , ConfigPath("/etc/bpdyndnsd")
50 , ExternalWarningLog("")
51 , ExternalWarningLevel(0)
53 // Available service description config options
54 po::options_description opt_desc_service("Service description options");
55 opt_desc_service.add_options()
56 ("protocol",po::value<string>(),"The service protocol.")
57 ("server",po::value<string>(),"Servername needed for gnudip protocol.")
58 ("host",po::value<string>(),"The hostname to update.")
59 ("login",po::value<string>(),"Login name.")
60 ("password",po::value<string>(),"Corresponding password.")
61 ("update_interval",po::value<int>()->default_value(-1),"Update interval in minutes.")
62 ("max_updates_within_interval",po::value<int>()->default_value(-1),"How many updates can be made in one interval.")
63 ("dns_cache_ttl",po::value<int>()->default_value(-1),"How long a dns record is valid.")
66 // Available command line only options
67 po::options_description opt_desc_cmd_only("Command line only options");
68 opt_desc_cmd_only.add_options()
69 ("help,?","Show help.")
70 ("version,v","Show version.")
71 ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
74 // Available generic options. Valid on cmd or in config file.
75 po::options_description opt_desc_generic("Generic config options");
76 opt_desc_generic.add_options()
77 ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
78 ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
79 ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
80 ("enable_ipv6",po::value<bool>()->default_value(false),"Try to use IPv6.")
81 ("webcheck_url",po::value<string>()->default_value(""),"Use this URL to determine IP.")
82 ("webcheck_url_alt",po::value<string>()->default_value(""),"Use this alternative URL to determine IP.")
83 ("http_proxy",po::value<string>(),"Use this proxy for all http requests.")
84 ("http_proxy_port",po::value<int>(),"Port of the proxy.")
85 ("external_warning_log",po::value<string>()->default_value(""),"External programm to pass warning log messages to.")
86 ("external_warning_level",po::value<int>()->default_value(0),"Warning messages of which loglevel should be passed to external programm.")
89 // Define valid command line parameters
90 Options_descriptionPtr _opt_desc_cmd(new po::options_description("Command line options"));
91 OptDescCmd = _opt_desc_cmd;
92 _opt_desc_cmd.reset();
93 OptDescCmd->add(opt_desc_cmd_only);
94 OptDescCmd->add(opt_desc_generic);
95 OptDescCmd->add(opt_desc_service);
97 // Define valid config file options
98 Options_descriptionPtr _opt_desc_conf_main(new po::options_description("Config file options"));
99 OptDescConfMain = _opt_desc_conf_main;
100 _opt_desc_conf_main.reset();
101 OptDescConfMain->add(opt_desc_generic);
103 // Define valid service file options
104 Options_descriptionPtr _opt_desc_conf_service(new po::options_description("Service file options"));
105 OptDescConfService = _opt_desc_conf_service;
106 _opt_desc_conf_service.reset();
107 OptDescConfService->add(opt_desc_service);
120 * Parses the command line arguments and does the needed actions.
121 * @param argc Command line argument number given to main.
122 * @param argv[] Pointer to command line argument array given to main.
123 * @return 0 if all is fine, -1 if not.
125 int Config::parse_cmd_line(int argc, char *argv[])
129 po::store(po::parse_command_line(argc, argv, *this->OptDescCmd), VariablesMap);
130 po::notify(VariablesMap);
132 if ( VariablesMap.count("help") )
134 Log->print_usage(OptDescCmd);
137 else if ( VariablesMap.count("version") )
139 Log->print_version();
143 // Create a service object if all needed options are set on the command line
144 if ( VariablesMap.count("protocol") && VariablesMap.count("host") && VariablesMap.count("login") && VariablesMap.count("password") )
146 // Get the cmd parameter values for protocol host login and password
147 string protocol = VariablesMap["protocol"].as<string>();
148 string host = VariablesMap["host"].as<string>();
149 string login = VariablesMap["login"].as<string>();
150 string password = VariablesMap["password"].as<string>();
152 protocol = ba::to_lower_copy(protocol);
155 if ( VariablesMap.count("server") )
156 server = VariablesMap["server"].as<string>();
158 int update_interval = 0;
159 if ( VariablesMap.count("update_interval") )
160 update_interval = VariablesMap["update_interval"].as<int>();
162 int max_updates_within_interval = 0;
163 if ( VariablesMap.count("max_updates_within_interval") )
164 max_updates_within_interval = VariablesMap["max_updates_within_interval"].as<int>();
166 int dns_cache_ttl = 0;
167 if ( VariablesMap.count("dns_cache_ttl") )
168 dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
170 Service::Ptr service = create_service(protocol,server,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
173 ServiceHolder->add_service(service);
174 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());
179 else if ( VariablesMap.count("protocol") || VariablesMap.count("host") || VariablesMap.count("login") || VariablesMap.count("password") )
181 Log->print_missing_cmd_service_option();
182 Log->print_usage(OptDescCmd);
186 if ( VariablesMap.count("config") )
188 fs::path full_config_path = fs::system_complete(fs::path(VariablesMap["config"].as<string>()));
189 ConfigPath = full_config_path.string();
190 if ( !fs::exists(full_config_path) || !fs::is_directory(full_config_path) )
192 // Config path doesn't exist or is not a directory
193 Log->print_error_config_path(ConfigPath);
198 if ( VariablesMap.count("daemon_mode") )
199 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
201 if ( VariablesMap.count("loglevel") )
202 Loglevel = VariablesMap["loglevel"].as<int>();
204 if ( VariablesMap.count("syslog") )
205 Syslog = VariablesMap["syslog"].as<bool>();
207 if ( VariablesMap.count("enable_ipv6") )
208 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
210 if ( VariablesMap.count("webcheck_url") )
211 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
213 if ( VariablesMap.count("webcheck_url_alt") )
214 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
216 if ( VariablesMap.count("http_proxy") && VariablesMap.count("http_proxy_port") )
218 Proxy = VariablesMap["http_proxy"].as<string>();
219 ProxyPort = VariablesMap["http_proxy_port"].as<int>();
221 else if ( VariablesMap.count("http_proxy") || VariablesMap.count("http_proxy_port") )
223 Log->print_missing_cmd_proxy_option();
224 Log->print_usage(OptDescCmd);
228 if ( VariablesMap.count("external_warning_log") )
229 ExternalWarningLog = VariablesMap["external_warning_log"].as<string>();
231 if ( VariablesMap.count("external_warning_level") )
232 ExternalWarningLevel = VariablesMap["external_warning_level"].as<int>();
235 catch(po::unknown_option e)
237 Log->print_unknown_cmd_option(e.what());
238 Log->print_usage(OptDescCmd);
241 catch(po::multiple_occurrences e)
243 Log->print_multiple_cmd_option(e.what());
244 Log->print_usage(OptDescCmd);
252 * Creates a Service object from the given parameters.
253 * @param protocol Protocol to use.
254 * @param host Hostname to update.
255 * @param login Login.
256 * @param password Password.
257 * @return A pointer to the created Service object.
259 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)
261 // Test for valid hostname. Must contain 3 parts minimum.
262 list<string> fqhn_parts;
263 ba::split(fqhn_parts,hostname,boost::is_any_of("."));
264 if ( fqhn_parts.size() < 3 )
266 Log->print_invalid_hostname(protocol);
267 Service::Ptr service;
271 if(protocol == "dhs")
273 Service::Ptr service_dhs(new ServiceDhs(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
276 else if(protocol == "ods")
278 Service::Ptr service_ods(new ServiceOds(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl));
281 else if(protocol == "dyndns")
283 Service::Ptr service_dyndns(new ServiceDyndns(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
284 return service_dyndns;
286 else if(protocol == "dyns")
288 Service::Ptr service_dyns(new ServiceDyns(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
291 else if(protocol == "easydns")
293 Service::Ptr service_easydns(new ServiceEasydns(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
294 return service_easydns;
296 else if(protocol == "tzo")
298 Service::Ptr service_tzo(new ServiceTzo(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
301 else if(protocol == "zoneedit")
303 Service::Ptr service_zoneedit(new ServiceZoneedit(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
304 return service_zoneedit;
306 else if(protocol == "gnudip")
308 cout << "Server: " << server << endl;
309 if ( !server.empty() )
311 Service::Ptr service_gnudip(new ServiceGnudip(protocol,server,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
312 return service_gnudip;
316 Log->print_gnudip_requires_servername();
317 Service::Ptr service;
323 Log->print_unknown_protocol(protocol);
324 Service::Ptr service;
331 * Loads a service config file, invoked by load_config_from_files.
332 * @param full_filename Filename of the service config file to load.
333 * @return 0 if all is fine, -1 otherwise.
335 int Config::load_service_config_file(const string& full_filename)
337 Log->print_load_service_conf(full_filename);
339 ifstream service_config_file(full_filename.c_str(),ifstream::in);
340 if(service_config_file.is_open())
344 po::variables_map vm;
345 po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->OptDescConfService,true);
346 po::store(parsed_service_options,vm);
349 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
351 // create the corresponding service
352 string protocol = vm["protocol"].as<string>();
353 string host = vm["host"].as<string>();
354 string login = vm["login"].as<string>();
355 string password = vm["password"].as<string>();
357 protocol = ba::to_lower_copy(protocol);
360 if ( vm.count("server") )
361 server = vm["server"].as<string>();
363 int update_interval = 0;
364 if ( vm.count("update_interval") )
365 update_interval = vm["update_interval"].as<int>();
367 int max_updates_within_interval = 0;
368 if ( vm.count("max_updates_within_interval") )
369 max_updates_within_interval = vm["max_updates_within_interval"].as<int>();
371 int dns_cache_ttl = 0;
372 if ( vm.count("dns_cache_ttl") )
373 dns_cache_ttl = vm["dns_cache_ttl"].as<int>();
375 Service::Ptr service = create_service(protocol,server,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
378 ServiceHolder->add_service(service);
379 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());
384 else if ( vm.count("protocol") || vm.count("host") || vm.count("login") || vm.count("password") )
386 service_config_file.close();
387 Log->print_missing_service_conf_option(full_filename);
391 catch ( po::unknown_option e )
393 // unknown option in config file detected
394 service_config_file.close();
395 Log->print_unknown_service_conf_option(full_filename,e.what());
398 catch(po::multiple_occurrences e)
400 service_config_file.close();
401 Log->print_multiple_service_conf_option(full_filename,e.what());
404 service_config_file.close();
408 // error opening service config file for reading
409 Log->print_error_opening_r(full_filename);
417 * Loads the main config file, invoked by load_config_from_files
418 * @param full_filename The full filename of the main config file to load
419 * @return 0 if all is fine, -1 otherwise
421 int Config::load_main_config_file(const string& full_filename)
423 Log->print_load_main_conf(full_filename);
425 ifstream main_config_file(full_filename.c_str(),ifstream::in);
426 if(main_config_file.is_open())
430 po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->OptDescConfMain,true);
431 po::store(parsed_main_options,VariablesMap);
432 po::notify(VariablesMap);
434 if ( VariablesMap.count("daemon_mode") )
435 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
437 if ( VariablesMap.count("loglevel") )
438 Loglevel = VariablesMap["loglevel"].as<int>();
440 if ( VariablesMap.count("syslog") )
441 Syslog = VariablesMap["syslog"].as<bool>();
443 if ( VariablesMap.count("enable_ipv6") )
444 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
446 if ( VariablesMap.count("webcheck_url") )
447 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
449 if ( VariablesMap.count("webcheck_url_alt") )
450 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
452 if ( VariablesMap.count("http_proxy") && VariablesMap.count("http_proxy_port") )
454 Proxy = VariablesMap["http_proxy"].as<string>();
455 ProxyPort = VariablesMap["http_proxy_port"].as<int>();
457 else if ( VariablesMap.count("http_proxy") || VariablesMap.count("http_proxy_port") )
459 main_config_file.close();
460 Log->print_missing_conf_proxy_option(full_filename);
464 if ( VariablesMap.count("external_warning_log") )
465 ExternalWarningLog = VariablesMap["external_warning_log"].as<string>();
467 if ( VariablesMap.count("external_warning_level") )
468 ExternalWarningLevel = VariablesMap["external_warning_level"].as<int>();
471 catch ( po::unknown_option e ) // at the moment 04-08-2009 this exception is never thrown :-(
473 // unknown option in main config file detected
474 main_config_file.close();
475 Log->print_unknown_main_conf_option(e.what());
478 catch(po::multiple_occurrences e)
480 main_config_file.close();
481 Log->print_multiple_main_conf_option(full_filename,e.what());
484 main_config_file.close();
488 // error opening main config file for reading
489 Log->print_error_opening_r(full_filename);
497 * Loads the main and the service config file and does the needed action.
498 * @param config_path The path to the config directory.
499 * @return 0 if all is fine, -1 otherwise
501 int Config::load_config_from_files()
503 fs::path full_config_path = fs::path(ConfigPath);
505 fs::directory_iterator end_iter;
506 for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr )
508 if( fs::is_regular_file( dir_itr->status() ) )
510 string actual_file = dir_itr->path().filename();
511 boost::regex expr(".*\\.conf?");
512 // If it is the main config file do the following
513 if ( actual_file == "bpdyndnsd.conf" )
515 // Load the main config file
516 string full_filename = dir_itr->path().string();
517 if ( load_main_config_file(full_filename) != 0 )
520 // If it is a service definition file *.conf, parse it and generate the corresponding service
521 else if ( boost::regex_search( actual_file,expr ) )
523 string full_filename = dir_itr->path().string();
524 if ( load_service_config_file(full_filename) != 0 )
529 // Config file successfully loaded
530 Log->print_conf_loaded(ConfigPath);
536 * Getter method for member OptDescCmd.
537 * @return options_description*.
539 Config::Options_descriptionPtr Config::get_opt_desc_cmd() const
546 * Getter method for member OptDescConfMain.
547 * @return options_description*.
549 Config::Options_descriptionPtr Config::get_opt_desc_conf_main() const
551 return OptDescConfMain;
556 * Getter method for member OptDescConfService.
557 * @return options_description*.
559 Config::Options_descriptionPtr Config::get_opt_desc_conf_service() const
561 return OptDescConfService;
566 * Getter for member Loglevel.
567 * @return Member Loglevel.
569 int Config::get_loglevel() const
576 * Getter for member DaemonMode.
577 * @return TRUE if enabled, FALSE if disabled.
579 bool Config::get_daemon_mode() const
586 * Deletes the map with the previously parsed options.
587 * This is needed in case we reload the config and don't want the old cmd options to overwrite new config file options.
589 void Config::delete_variables_map()
591 VariablesMap.clear();
593 po::variables_map _variables_map;
594 VariablesMap = _variables_map;
599 * Getter for member Syslog.
600 * @return True if logging through syslog is enabled, false otherwise.
602 bool Config::get_syslog() const
609 * Getter for member EnableIPv6
610 * @return Wether IPv6 should be used or not.
612 bool Config::get_enable_ipv6() const
619 * Getter for member WebcheckIpUrl
620 * @return The primary IP Webcheck URL
622 string Config::get_webcheck_ip_url() const
624 return WebcheckIpUrl;
629 * Getter for member WebcheckIpUrlAlt
630 * @return The alternative IP Webcheck URL
632 string Config::get_webcheck_ip_url_alt() const
634 return WebcheckIpUrlAlt;
642 string Config::get_proxy() const
649 * Get member ProxyPort
652 int Config::get_proxy_port() const
659 * Get member ExternalWarningLog
660 * @return ExternalWarningLog
662 string Config::get_external_warning_log() const
664 return ExternalWarningLog;
669 * Get member ExternalWarningLevel
670 * @return ExternalWaringLevel
672 int Config::get_external_warning_level() const
674 return ExternalWarningLevel;