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.")
79 // Define valid command line parameters
80 Options_descriptionPtr _opt_desc_cmd(new po::options_description("Command line options"));
81 OptDescCmd = _opt_desc_cmd;
82 _opt_desc_cmd.reset();
83 OptDescCmd->add(opt_desc_cmd_only);
84 OptDescCmd->add(opt_desc_generic);
85 OptDescCmd->add(opt_desc_service);
87 // Define valid config file options
88 Options_descriptionPtr _opt_desc_conf_main(new po::options_description("Config file options"));
89 OptDescConfMain = _opt_desc_conf_main;
90 _opt_desc_conf_main.reset();
91 OptDescConfMain->add(opt_desc_generic);
93 // Define valid service file options
94 Options_descriptionPtr _opt_desc_conf_service(new po::options_description("Service file options"));
95 OptDescConfService = _opt_desc_conf_service;
96 _opt_desc_conf_service.reset();
97 OptDescConfService->add(opt_desc_service);
110 * Parses the command line arguments and does the needed actions.
111 * @param argc Command line argument number given to main.
112 * @param argv[] Pointer to command line argument array given to main.
113 * @return 0 if all is fine, -1 if not.
115 int Config::parse_cmd_line(int argc, char *argv[])
119 po::store(po::parse_command_line(argc, argv, *this->OptDescCmd), VariablesMap);
120 po::notify(VariablesMap);
122 if ( VariablesMap.count("help") )
124 Log->print_usage(OptDescCmd);
127 else if ( VariablesMap.count("version") )
129 Log->print_version();
133 // Create a service object if all needed options are set on the command line
134 if ( VariablesMap.count("protocol") && VariablesMap.count("host") && VariablesMap.count("login") && VariablesMap.count("password") )
136 // Get the cmd parameter values for protocol host login and password
137 string protocol = VariablesMap["protocol"].as<string>();
138 string host = VariablesMap["host"].as<string>();
139 string login = VariablesMap["login"].as<string>();
140 string password = VariablesMap["password"].as<string>();
142 protocol = ba::to_lower_copy(protocol);
144 int update_interval = 0;
145 if ( VariablesMap.count("update_interval") )
146 update_interval = VariablesMap["update_interval"].as<int>();
148 int max_updates_within_interval = 0;
149 if ( VariablesMap.count("max_updates_within_interval") )
150 max_updates_within_interval = VariablesMap["max_updates_within_interval"].as<int>();
152 int dns_cache_ttl = 0;
153 if ( VariablesMap.count("dns_cache_ttl") )
154 dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
156 Service::Ptr service = create_service(protocol,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
159 ServiceHolder->add_service(service);
160 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());
165 else if ( VariablesMap.count("protocol") || VariablesMap.count("host") || VariablesMap.count("login") || VariablesMap.count("password") )
167 Log->print_missing_cmd_service_option();
168 Log->print_usage(OptDescCmd);
172 if ( VariablesMap.count("config") )
174 fs::path full_config_path = fs::system_complete(fs::path(VariablesMap["config"].as<string>()));
175 ConfigPath = full_config_path.string();
176 if ( !fs::exists(full_config_path) || !fs::is_directory(full_config_path) )
178 // Config path doesn't exist or is not a directory
179 Log->print_error_config_path(ConfigPath);
184 if ( VariablesMap.count("daemon_mode") )
185 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
187 if ( VariablesMap.count("loglevel") )
188 Loglevel = VariablesMap["loglevel"].as<int>();
190 if ( VariablesMap.count("syslog") )
191 Syslog = VariablesMap["syslog"].as<bool>();
193 if ( VariablesMap.count("enable_ipv6") )
194 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
196 if ( VariablesMap.count("webcheck_url") )
197 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
199 if ( VariablesMap.count("webcheck_url_alt") )
200 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
203 catch(po::unknown_option e)
205 Log->print_unknown_cmd_option(e.what());
206 Log->print_usage(OptDescCmd);
209 catch(po::multiple_occurrences e)
211 Log->print_multiple_cmd_option(e.what());
212 Log->print_usage(OptDescCmd);
220 * Creates a Service object from the given parameters.
221 * @param protocol Protocol to use.
222 * @param host Hostname to update.
223 * @param login Login.
224 * @param password Password.
225 * @return A pointer to the created Service object.
227 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)
229 if(protocol == "dhs")
231 Service::Ptr service_dhs(new DHS(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl));
234 else if(protocol == "ods")
236 Service::Ptr service_ods(new ODS(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl));
241 Log->print_unknown_protocol(protocol);
242 Service::Ptr service;
249 * Loads a service config file, invoked by load_config_from_files.
250 * @param full_filename Filename of the service config file to load.
251 * @return 0 if all is fine, -1 otherwise.
253 int Config::load_service_config_file(const string& full_filename)
255 Log->print_load_service_conf(full_filename);
257 ifstream service_config_file(full_filename.c_str(),ifstream::in);
258 if(service_config_file.is_open())
262 po::variables_map vm;
263 po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->OptDescConfService,true);
264 po::store(parsed_service_options,vm);
267 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
269 // create the corresponding service
270 string protocol = vm["protocol"].as<string>();
271 string host = vm["host"].as<string>();
272 string login = vm["login"].as<string>();
273 string password = vm["password"].as<string>();
275 protocol = ba::to_lower_copy(protocol);
277 int update_interval = 0;
278 if ( VariablesMap.count("update_interval") )
279 update_interval = VariablesMap["update_interval"].as<int>();
281 int max_updates_within_interval = 0;
282 if ( VariablesMap.count("max_updates_within_interval") )
283 max_updates_within_interval = VariablesMap["max_updates_within_interval"].as<int>();
285 int dns_cache_ttl = 0;
286 if ( VariablesMap.count("dns_cache_ttl") )
287 dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
289 Service::Ptr service = create_service(protocol,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
292 ServiceHolder->add_service(service);
293 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());
299 catch ( po::unknown_option e )
301 // unknown option in config file detected
302 service_config_file.close();
303 Log->print_unknown_service_conf_option(e.what());
306 service_config_file.close();
310 // error opening service config file for reading
311 Log->print_error_opening_r(full_filename);
319 * Loads the main config file, invoked by load_config_from_files
320 * @param full_filename The full filename of the main config file to load
321 * @return 0 if all is fine, -1 otherwise
323 int Config::load_main_config_file(const string& full_filename)
325 Log->print_load_main_conf(full_filename);
327 ifstream main_config_file(full_filename.c_str(),ifstream::in);
328 if(main_config_file.is_open())
332 po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->OptDescConfMain,true);
333 po::store(parsed_main_options,VariablesMap);
334 po::notify(VariablesMap);
336 if ( VariablesMap.count("daemon_mode") )
337 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
339 if ( VariablesMap.count("loglevel") )
340 Loglevel = VariablesMap["loglevel"].as<int>();
342 if ( VariablesMap.count("syslog") )
343 Syslog = VariablesMap["syslog"].as<bool>();
345 if ( VariablesMap.count("enable_ipv6") )
346 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
348 if ( VariablesMap.count("webcheck_url") )
349 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
351 if ( VariablesMap.count("webcheck_url_alt") )
352 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
355 catch ( po::unknown_option e ) // at the moment 04-08-2009 this exception is never thrown :-(
357 // unknown option in main config file detected
358 main_config_file.close();
359 Log->print_unknown_main_conf_option(e.what());
362 main_config_file.close();
366 // error opening main config file for reading
367 Log->print_error_opening_r(full_filename);
375 * Loads the main and the service config file and does the needed action.
376 * @param config_path The path to the config directory.
377 * @return 0 if all is fine, -1 otherwise
379 int Config::load_config_from_files()
381 fs::path full_config_path = fs::path(ConfigPath);
383 fs::directory_iterator end_iter;
384 for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr )
386 if( fs::is_regular_file( dir_itr->status() ) )
388 string actual_file = dir_itr->path().filename();
389 boost::regex expr(".*\\.conf?");
390 // If it is the main config file do the following
391 if ( actual_file == "bpdyndnsd.conf" )
393 // Load the main config file
394 string full_filename = dir_itr->path().string();
395 if ( load_main_config_file(full_filename) != 0 )
398 // If it is a service definition file *.conf, parse it and generate the corresponding service
399 else if ( boost::regex_search( actual_file,expr ) )
401 string full_filename = dir_itr->path().string();
402 if ( load_service_config_file(full_filename) != 0 )
407 // Config file successfully loaded
408 Log->print_conf_loaded(ConfigPath);
414 * Getter method for member OptDescCmd.
415 * @return options_description*.
417 Config::Options_descriptionPtr Config::get_opt_desc_cmd() const
424 * Getter method for member OptDescConfMain.
425 * @return options_description*.
427 Config::Options_descriptionPtr Config::get_opt_desc_conf_main() const
429 return OptDescConfMain;
434 * Getter method for member OptDescConfService.
435 * @return options_description*.
437 Config::Options_descriptionPtr Config::get_opt_desc_conf_service() const
439 return OptDescConfService;
444 * Getter for member Loglevel.
445 * @return Member Loglevel.
447 int Config::get_loglevel() const
454 * Getter for member DaemonMode.
455 * @return TRUE if enabled, FALSE if disabled.
457 bool Config::get_daemon_mode() const
464 * Deletes the map with the previously parsed options.
465 * This is needed in case we reload the config and don't want the old cmd options to overwrite new config file options.
467 void Config::delete_variables_map()
469 VariablesMap.clear();
471 po::variables_map _variables_map;
472 VariablesMap = _variables_map;
477 * Getter for member Syslog.
478 * @return True if logging through syslog is enabled, false otherwise.
480 bool Config::get_syslog() const
487 * Getter for member EnableIPv6
488 * @return Wether IPv6 should be used or not.
490 bool Config::get_enable_ipv6() const
497 * Getter for member WebcheckIpUrl
498 * @return The primary IP Webcheck URL
500 string Config::get_webcheck_ip_url() const
502 return WebcheckIpUrl;
507 * Getter for member WebcheckIpUrlAlt
508 * @return The alternative IP Webcheck URL
510 string Config::get_webcheck_ip_url_alt() const
512 return WebcheckIpUrlAlt;