2 * @brief Config class implementation. This class represents the actual configuration.
6 * @copyright Intra2net AG
12 namespace po = boost::program_options;
13 namespace fs = boost::filesystem;
18 * Default Constructor. Available command line and config file options with their default values are defined here.
20 Config::Config(LoggerPtr _log)
24 , Config_path("/etc/bpdyndnsd")
29 // Available service description config options
30 po::options_description opt_desc_service("Service description options");
31 opt_desc_service.add_options()
32 ("protocol",po::value<string>(),"The service protocol.")
33 ("host",po::value<string>(),"The hostname to update.")
34 ("login",po::value<string>(),"Login name.")
35 ("password",po::value<string>(),"Corresponding password.")
38 // Available command line only options
39 po::options_description opt_desc_cmd_only("Command line only options");
40 opt_desc_cmd_only.add_options()
41 ("help,?","Show help.")
42 ("version,v","Show version.")
43 ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
46 // Available generic options. Valid on cmd or in config file.
47 po::options_description opt_desc_generic("Generic config options");
48 opt_desc_generic.add_options()
49 ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
50 ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
51 ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
54 // Define valid command line parameters
55 Opt_desc_cmd = new po::options_description("Command line options");
56 Opt_desc_cmd->add(opt_desc_cmd_only);
57 Opt_desc_cmd->add(opt_desc_generic);
58 Opt_desc_cmd->add(opt_desc_service);
60 // Define valid config file options
61 Opt_desc_conf_main = new po::options_description("Config file options");
62 Opt_desc_conf_main->add(opt_desc_generic);
64 // Define valid service file options
65 Opt_desc_conf_service = new po::options_description("Service file options");
66 Opt_desc_conf_service->add(opt_desc_service);
68 Log->print_constructor_call("Config");
78 delete Opt_desc_conf_main;
79 delete Opt_desc_conf_service;
81 Log->print_destructor_call("Config");
86 * Parses the command line arguments and does the needed actions.
87 * @param argc Command line argument number given to main.
88 * @param argv[] Pointer to command line argument array given to main.
89 * @return 0 if all is fine, 1 if not.
91 int Config::parse_cmd_line(int argc, char *argv[])
95 po::store(po::parse_command_line(argc, argv, *this->Opt_desc_cmd), Variables_map);
96 po::notify(Variables_map);
98 if ( Variables_map.count("help") )
100 Log->print_usage(Opt_desc_cmd);
103 else if ( Variables_map.count("version") )
105 Log->print_version();
109 // Create a service object if all needed options are set on the command line
110 if ( Variables_map.count("protocol") && Variables_map.count("host") && Variables_map.count("login") && Variables_map.count("password") )
112 // Get the cmd parameter values for protocol host login and password
113 string protocol = Variables_map["protocol"].as<string>();
114 string host = Variables_map["host"].as<string>();
115 string login = Variables_map["login"].as<string>();
116 string password = Variables_map["password"].as<string>();
118 //TODO: convert protocol option to lowercase
120 ServicePtr service = create_service(protocol,host,login,password);
122 Services.push_back(service);
126 else if ( Variables_map.count("protocol") || Variables_map.count("host") || Variables_map.count("login") || Variables_map.count("password") )
128 Log->print_missing_cmd_service_option();
129 Log->print_usage(Opt_desc_cmd);
133 if ( Variables_map.count("config") )
135 fs::path full_config_path = fs::system_complete(fs::path(Variables_map["config"].as<string>()));
136 Config_path = full_config_path.string();
137 if ( !fs::exists(full_config_path) || !fs::is_directory(full_config_path) )
139 // Config path doesn't exist or is not a directory
140 Log->print_error_config_path(Config_path);
145 if ( Variables_map.count("loglevel") )
146 Loglevel = Variables_map["loglevel"].as<int>();
148 if ( Variables_map.count("syslog") )
149 Syslog = Variables_map["syslog"].as<bool>();
152 catch(po::unknown_option e)
154 Log->print_unknown_cmd_option(e.what());
155 Log->print_usage(Opt_desc_cmd);
168 * @return A pointer to the created Service object.
170 ServicePtr Config::create_service(const string &protocol,const string &host, const string &login, const string &password)
172 if(protocol == "dhs")
174 ServicePtr service_dhs(new DHS(Log,host,login,password));
177 else if(protocol == "ods")
179 ServicePtr service_ods(new ODS(Log,host,login,password));
184 Log->print_unknown_protocol(protocol);
192 * Loads a service config file, invoked by load_config_from_files.
193 * @param full_filename Filename of the service config file to load.
194 * @return 0 if all is fine, 3 if an unknown option was detected, 4 if the service file could not be opened for reading.
196 int Config::load_service_config_file(const string& full_filename)
198 Log->print_load_service_conf(full_filename);
200 ifstream service_config_file(full_filename.c_str(),ifstream::in);
201 if(service_config_file.is_open())
205 po::variables_map vm;
206 po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->Opt_desc_conf_service,true);
207 po::store(parsed_service_options,vm);
210 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
212 // create the corresponding service
213 string protocol = vm["protocol"].as<string>();
214 string host = vm["host"].as<string>();
215 string login = vm["login"].as<string>();
216 string password = vm["password"].as<string>();
218 // TODO: convert protocol to lowercase
219 //protocol = tolower(protocol.c_str());
221 ServicePtr service = create_service(protocol,host,login,password);
224 Services.push_back(service);
228 catch ( po::unknown_option e )
230 // unknown option in config file detected
231 service_config_file.close();
232 Log->print_unknown_service_conf_option(e.what());
235 service_config_file.close();
239 // error opening service config file for reading
240 Log->print_error_opening(full_filename);
248 * Loads the main config file, invoked by load_config_from_files
249 * @param full_filename The full filename of the main config file to load
250 * @return 0 if all is fine. 3 if unknown option was detected, 4 if main config file could not be opened for reading
252 int Config::load_main_config_file(const string& full_filename)
254 Log->print_load_main_conf(full_filename);
256 ifstream main_config_file(full_filename.c_str(),ifstream::in);
257 if(main_config_file.is_open())
261 po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->Opt_desc_conf_main,true);
262 po::store(parsed_main_options,Variables_map);
263 po::notify(Variables_map);
265 if(Variables_map.count("daemon_mode") && Variables_map.count("loglevel") && Variables_map.count("syslog"))
267 Daemon_mode = Variables_map["daemon_mode"].as<bool>();
268 Loglevel = Variables_map["loglevel"].as<int>();
269 Syslog = Variables_map["syslog"].as<bool>();
272 catch ( po::unknown_option e ) // at the moment 04-08-2009 this exception is never thrown :-(
274 // unknown option in main config file detected
275 main_config_file.close();
276 Log->print_unknown_main_conf_option(e.what());
279 main_config_file.close();
283 // error opening main config file for reading
284 Log->print_error_opening(full_filename);
292 * Loads the main and the service config file and does the needed action.
293 * @param config_path The path to the config directory.
294 * @return 0 if all is fine.
296 int Config::load_config_from_files()
298 fs::path full_config_path = fs::path(Config_path);
300 fs::directory_iterator end_iter;
301 for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr )
303 if( fs::is_regular_file( dir_itr->status() ) )
305 string actual_file = dir_itr->path().filename();
306 boost::regex expr(".*\\.conf?");
307 // If it is the main config file do the following
308 if ( actual_file == "bpdyndnsd.conf" )
310 // Load the main config file
311 string full_filename = dir_itr->path().string();
312 if ( load_main_config_file(full_filename) != 0 )
315 // If it is a service definition file *.conf, parse it and generate the corresponding service
316 else if ( boost::regex_search( actual_file,expr ) )
318 string full_filename = dir_itr->path().string();
319 if ( load_service_config_file(full_filename) != 0 )
324 // Config file successfully loaded
325 Log->print_conf_loaded(Config_path);
331 * Getter method for Service list member.
332 * @return Pointer to a list of Service's.
334 list<ServicePtr> Config::get_services()
336 return this->Services;
341 * Getter method for member Opt_desc_cmd.
342 * @return options_description*.
344 po::options_description* Config::get_opt_desc_cmd()
351 * Getter method for member Opt_desc_conf_main.
352 * @return options_description*.
354 po::options_description* Config::get_opt_desc_conf_main()
356 return Opt_desc_conf_main;
361 * Getter method for member Opt_desc_conf_service.
362 * @return options_description*.
364 po::options_description* Config::get_opt_desc_conf_service()
366 return Opt_desc_conf_service;
371 * Getter for member Loglevel.
372 * @return Member Loglevel.
374 int Config::get_loglevel()
381 * Getter for member Daemon_mode.
382 * @return TRUE if enabled, FALSE if disabled.
384 bool Config::get_daemon_mode()
391 * Resets all shared Service pointers and clears the Services list.
393 void Config::delete_services()
395 BOOST_FOREACH( ServicePtr service, Services )
404 * Deletes the map with the previously parsed options.
405 * This is needed in case we reload the config and don't want the old cmd options to overwrite new config file options.
407 void Config::delete_variables_map()
409 Variables_map.clear();
411 po::variables_map _variables_map;
412 Variables_map = _variables_map;
417 * Getter for member Syslog.
418 * @return True if logging through syslog is enabled, false otherwise.
420 bool Config::get_syslog()