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)
22 , Logfile("/var/log/bpdyndns.log")
25 , Config_path("/etc/bpdyndnsd")
30 // Available service description config options
31 po::options_description opt_desc_service("Service description options");
32 opt_desc_service.add_options()
33 ("protocol",po::value<string>(),"The service protocol.")
34 ("host",po::value<string>(),"The hostname to update.")
35 ("login",po::value<string>(),"Login name.")
36 ("password",po::value<string>(),"Corresponding password.")
39 // Available command line only options
40 po::options_description opt_desc_cmd_only("Command line only options");
41 opt_desc_cmd_only.add_options()
42 ("help,?","Show help.")
43 ("version,v","Show version.")
44 ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
47 // Available generic options. Valid on cmd or in config file.
48 po::options_description opt_desc_generic("Generic config options");
49 opt_desc_generic.add_options()
50 ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
51 ("logfile",po::value<string>()->default_value("/var/log/bpdyndns.log"),"Where to log.")
52 ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
53 ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
56 // Define valid command line parameters
57 Opt_desc_cmd = new po::options_description("Command line options");
58 Opt_desc_cmd->add(opt_desc_cmd_only);
59 Opt_desc_cmd->add(opt_desc_generic);
60 Opt_desc_cmd->add(opt_desc_service);
62 // Define valid config file options
63 Opt_desc_conf_main = new po::options_description("Config file options");
64 Opt_desc_conf_main->add(opt_desc_generic);
66 // Define valid service file options
67 Opt_desc_conf_service = new po::options_description("Service file options");
68 Opt_desc_conf_service->add(opt_desc_service);
70 Log->print_constructor_call("Config");
80 delete Opt_desc_conf_main;
81 delete Opt_desc_conf_service;
83 Log->print_destructor_call("Config");
88 * Parses the command line arguments and does the needed actions.
89 * @param argc Command line argument number given to main.
90 * @param argv[] Pointer to command line argument array given to main.
91 * @return 0 if all is fine, 1 if not.
93 int Config::parse_cmd_line(int argc, char *argv[])
97 po::store(po::parse_command_line(argc, argv, *this->Opt_desc_cmd), Variables_map);
98 po::notify(Variables_map);
100 if ( Variables_map.count("help") )
102 Log->print_usage(Opt_desc_cmd);
105 else if ( Variables_map.count("version") )
107 Log->print_version();
111 // Create a service object if all needed options are set on the command line
112 if ( Variables_map.count("protocol") && Variables_map.count("host") && Variables_map.count("login") && Variables_map.count("password") )
114 // Get the cmd parameter values for protocol host login and password
115 string protocol = Variables_map["protocol"].as<string>();
116 string host = Variables_map["host"].as<string>();
117 string login = Variables_map["login"].as<string>();
118 string password = Variables_map["password"].as<string>();
120 //TODO: convert protocol option to lowercase
122 ServicePtr service = create_service(protocol,host,login,password);
124 Services.push_back(service);
128 else if ( Variables_map.count("protocol") || Variables_map.count("host") || Variables_map.count("login") || Variables_map.count("password") )
130 Log->print_missing_cmd_service_option();
131 Log->print_usage(Opt_desc_cmd);
135 if ( Variables_map.count("config") )
137 fs::path full_config_path = fs::system_complete(fs::path(Variables_map["config"].as<string>()));
138 Config_path = full_config_path.string();
139 if ( !fs::exists(full_config_path) || !fs::is_directory(full_config_path) )
141 // Config path doesn't exist or is not a directory
142 Log->print_error_config_path(Config_path);
147 catch(po::unknown_option e)
149 Log->print_unknown_cmd_option(e.what());
150 Log->print_usage(Opt_desc_cmd);
163 * @return A pointer to the created Service object.
165 ServicePtr Config::create_service(const string &protocol,const string &host, const string &login, const string &password)
167 if(protocol == "dhs")
169 ServicePtr service_dhs(new DHS(Log,host,login,password));
172 else if(protocol == "ods")
174 ServicePtr service_ods(new ODS(Log,host,login,password));
179 Log->print_unknown_protocol(protocol);
187 * Loads a service config file, invoked by load_config_from_files.
188 * @param full_filename Filename of the service config file to load.
189 * @return 0 if all is fine, 3 if an unknown option was detected, 4 if the service file could not be opened for reading.
191 int Config::load_service_config_file(const string& full_filename)
193 Log->print_load_service_conf(full_filename);
195 ifstream service_config_file(full_filename.c_str(),ifstream::in);
196 if(service_config_file.is_open())
200 po::variables_map vm;
201 po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->Opt_desc_conf_service,true);
202 po::store(parsed_service_options,vm);
205 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
207 // create the corresponding service
208 string protocol = vm["protocol"].as<string>();
209 string host = vm["host"].as<string>();
210 string login = vm["login"].as<string>();
211 string password = vm["password"].as<string>();
213 // TODO: convert protocol to lowercase
214 //protocol = tolower(protocol.c_str());
216 ServicePtr service = create_service(protocol,host,login,password);
219 Services.push_back(service);
223 catch ( po::unknown_option e )
225 // unknown option in config file detected
226 service_config_file.close();
227 Log->print_unknown_service_conf_option(e.what());
230 service_config_file.close();
234 // error opening service config file for reading
235 Log->print_error_opening(full_filename);
243 * Loads the main config file, invoked by load_config_from_files
244 * @param full_filename The full filename of the main config file to load
245 * @return 0 if all is fine. 3 if unknown option was detected, 4 if main config file could not be opened for reading
247 int Config::load_main_config_file(const string& full_filename)
249 Log->print_load_main_conf(full_filename);
251 ifstream main_config_file(full_filename.c_str(),ifstream::in);
252 if(main_config_file.is_open())
256 po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->Opt_desc_conf_main,true);
257 po::store(parsed_main_options,Variables_map);
258 po::notify(Variables_map);
260 if(Variables_map.count("daemon_mode") && Variables_map.count("logfile") && Variables_map.count("loglevel") && Variables_map.count("syslog"))
262 Daemon_mode = Variables_map["daemon_mode"].as<bool>();
263 Logfile = Variables_map["logfile"].as<string>();
264 Loglevel = Variables_map["loglevel"].as<int>();
265 Syslog = Variables_map["syslog"].as<bool>();
268 catch ( po::unknown_option e )
270 // unknown option in main config file detected
271 main_config_file.close();
272 Log->print_unknown_main_conf_option(e.what());
275 main_config_file.close();
279 // error opening main config file for reading
280 Log->print_error_opening(full_filename);
288 * Loads the main and the service config file and does the needed action.
289 * @param config_path The path to the config directory.
290 * @return 0 if all is fine.
292 int Config::load_config_from_files()
294 fs::path full_config_path = fs::path(Config_path);
296 fs::directory_iterator end_iter;
297 for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr )
299 if( fs::is_regular_file( dir_itr->status() ) )
301 string actual_file = dir_itr->path().filename();
302 boost::regex expr(".*\\.conf?");
303 // If it is the main config file do the following
304 if ( actual_file == "bpdyndnsd.conf" )
306 // Load the main config file
307 string full_filename = dir_itr->path().string();
308 if ( load_main_config_file(full_filename) != 0 )
311 // If it is a service definition file *.conf, parse it and generate the corresponding service
312 else if ( boost::regex_search( actual_file,expr ) )
314 string full_filename = dir_itr->path().string();
315 if ( load_service_config_file(full_filename) != 0 )
320 // Config file successfully loaded
321 Log->print_conf_loaded(Config_path);
327 * Getter method for Service list member.
328 * @return Pointer to a list of Service's.
330 list<ServicePtr> Config::get_services()
332 return this->Services;
337 * Getter method for member Opt_desc_cmd.
338 * @return options_description*.
340 po::options_description* Config::get_opt_desc_cmd()
347 * Getter method for member Opt_desc_conf_main.
348 * @return options_description*.
350 po::options_description* Config::get_opt_desc_conf_main()
352 return Opt_desc_conf_main;
357 * Getter method for member Opt_desc_conf_service.
358 * @return options_description*.
360 po::options_description* Config::get_opt_desc_conf_service()
362 return Opt_desc_conf_service;
367 * Getter for member Loglevel.
368 * @return Member Loglevel.
370 int Config::get_loglevel()
377 * Getter for member Daemon_mode.
378 * @return TRUE if enabled, FALSE if disabled.
380 bool Config::get_daemon_mode()
387 * Resets all shared Service pointers and clears the Services list.
389 void Config::delete_services()
391 BOOST_FOREACH( ServicePtr service, Services )
400 * Deletes the map with the previously parsed options.
401 * This is needed in case we reload the config and don't want the old cmd options to overwrite new config file options.
403 void Config::delete_variables_map()
405 Variables_map.clear();
407 po::variables_map _variables_map;
408 Variables_map = _variables_map;
413 * Getter for member Syslog.
414 * @return True if logging through syslog is enabled, false otherwise.
416 bool Config::get_syslog()