2 * @brief Config class implementation. This class represents the actual configuration.
6 * @copyright Intra2net AG
12 // Following boost macros are needed for serialization of derived classes through a base class pointer (Service *).
13 BOOST_CLASS_EXPORT_GUID(ODS, "ODS")
14 BOOST_CLASS_EXPORT_GUID(DHS, "DHS")
16 namespace po = boost::program_options;
17 namespace fs = boost::filesystem;
22 * Default Constructor. Available command line and config file options with their default values are defined here.
24 Config::Config(LoggerPtr _log)
28 , Config_path("/etc/bpdyndnsd")
33 // Available service description config options
34 po::options_description opt_desc_service("Service description options");
35 opt_desc_service.add_options()
36 ("protocol",po::value<string>(),"The service protocol.")
37 ("host",po::value<string>(),"The hostname to update.")
38 ("login",po::value<string>(),"Login name.")
39 ("password",po::value<string>(),"Corresponding password.")
42 // Available command line only options
43 po::options_description opt_desc_cmd_only("Command line only options");
44 opt_desc_cmd_only.add_options()
45 ("help,?","Show help.")
46 ("version,v","Show version.")
47 ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
50 // Available generic options. Valid on cmd or in config file.
51 po::options_description opt_desc_generic("Generic config options");
52 opt_desc_generic.add_options()
53 ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
54 ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
55 ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
58 // Define valid command line parameters
59 Opt_desc_cmd = new po::options_description("Command line options");
60 Opt_desc_cmd->add(opt_desc_cmd_only);
61 Opt_desc_cmd->add(opt_desc_generic);
62 Opt_desc_cmd->add(opt_desc_service);
64 // Define valid config file options
65 Opt_desc_conf_main = new po::options_description("Config file options");
66 Opt_desc_conf_main->add(opt_desc_generic);
68 // Define valid service file options
69 Opt_desc_conf_service = new po::options_description("Service file options");
70 Opt_desc_conf_service->add(opt_desc_service);
72 Log->print_constructor_call("Config");
82 delete Opt_desc_conf_main;
83 delete Opt_desc_conf_service;
85 Log->print_destructor_call("Config");
90 * This function serializes all Service objects in Services into a specified file.
91 * @return 0 if all is fine, -1 otherwise.
93 int Config::serialize_services()
95 //TODO: error handling
97 cout << "starting serialize objects" << endl;
98 // serialize service objects
99 ofstream ofs("/home/bjoern/intranator/bpdyndnsd/objects.ser");
100 boost::archive::text_oarchive oa(ofs);
102 BOOST_FOREACH(ServicePtr service, Services)
104 Service* ptr = service.get();
105 oa << BOOST_SERIALIZATION_NVP(ptr);
114 * Parses the command line arguments and does the needed actions.
115 * @param argc Command line argument number given to main.
116 * @param argv[] Pointer to command line argument array given to main.
117 * @return 0 if all is fine, 1 if not.
119 int Config::parse_cmd_line(int argc, char *argv[])
123 po::store(po::parse_command_line(argc, argv, *this->Opt_desc_cmd), Variables_map);
124 po::notify(Variables_map);
126 if ( Variables_map.count("help") )
128 Log->print_usage(Opt_desc_cmd);
131 else if ( Variables_map.count("version") )
133 Log->print_version();
137 // Create a service object if all needed options are set on the command line
138 if ( Variables_map.count("protocol") && Variables_map.count("host") && Variables_map.count("login") && Variables_map.count("password") )
140 // Get the cmd parameter values for protocol host login and password
141 string protocol = Variables_map["protocol"].as<string>();
142 string host = Variables_map["host"].as<string>();
143 string login = Variables_map["login"].as<string>();
144 string password = Variables_map["password"].as<string>();
146 //TODO: convert protocol option to lowercase
148 ServicePtr service = create_service(protocol,host,login,password);
150 Services.push_back(service);
154 else if ( Variables_map.count("protocol") || Variables_map.count("host") || Variables_map.count("login") || Variables_map.count("password") )
156 Log->print_missing_cmd_service_option();
157 Log->print_usage(Opt_desc_cmd);
161 if ( Variables_map.count("config") )
163 fs::path full_config_path = fs::system_complete(fs::path(Variables_map["config"].as<string>()));
164 Config_path = full_config_path.string();
165 if ( !fs::exists(full_config_path) || !fs::is_directory(full_config_path) )
167 // Config path doesn't exist or is not a directory
168 Log->print_error_config_path(Config_path);
173 if ( Variables_map.count("loglevel") )
174 Loglevel = Variables_map["loglevel"].as<int>();
176 if ( Variables_map.count("syslog") )
177 Syslog = Variables_map["syslog"].as<bool>();
180 catch(po::unknown_option e)
182 Log->print_unknown_cmd_option(e.what());
183 Log->print_usage(Opt_desc_cmd);
196 * @return A pointer to the created Service object.
198 ServicePtr Config::create_service(const string &protocol,const string &hostname, const string &login, const string &password)
200 if(protocol == "dhs")
202 ServicePtr service_dhs(new DHS(protocol,hostname,login,password,Log,0,0,1));
205 else if(protocol == "ods")
207 ServicePtr service_ods(new ODS(protocol,hostname,login,password,Log,0,0,1));
212 Log->print_unknown_protocol(protocol);
220 * Loads a service config file, invoked by load_config_from_files.
221 * @param full_filename Filename of the service config file to load.
222 * @return 0 if all is fine, 3 if an unknown option was detected, 4 if the service file could not be opened for reading.
224 int Config::load_service_config_file(const string& full_filename)
226 Log->print_load_service_conf(full_filename);
228 ifstream service_config_file(full_filename.c_str(),ifstream::in);
229 if(service_config_file.is_open())
233 po::variables_map vm;
234 po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->Opt_desc_conf_service,true);
235 po::store(parsed_service_options,vm);
238 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
240 // create the corresponding service
241 string protocol = vm["protocol"].as<string>();
242 string host = vm["host"].as<string>();
243 string login = vm["login"].as<string>();
244 string password = vm["password"].as<string>();
246 // TODO: convert protocol to lowercase
247 //protocol = tolower(protocol.c_str());
249 ServicePtr service = create_service(protocol,host,login,password);
252 Services.push_back(service);
256 catch ( po::unknown_option e )
258 // unknown option in config file detected
259 service_config_file.close();
260 Log->print_unknown_service_conf_option(e.what());
263 service_config_file.close();
267 // error opening service config file for reading
268 Log->print_error_opening(full_filename);
276 * Loads the main config file, invoked by load_config_from_files
277 * @param full_filename The full filename of the main config file to load
278 * @return 0 if all is fine. 3 if unknown option was detected, 4 if main config file could not be opened for reading
280 int Config::load_main_config_file(const string& full_filename)
282 Log->print_load_main_conf(full_filename);
284 ifstream main_config_file(full_filename.c_str(),ifstream::in);
285 if(main_config_file.is_open())
289 po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->Opt_desc_conf_main,true);
290 po::store(parsed_main_options,Variables_map);
291 po::notify(Variables_map);
293 if(Variables_map.count("daemon_mode") && Variables_map.count("loglevel") && Variables_map.count("syslog"))
295 Daemon_mode = Variables_map["daemon_mode"].as<bool>();
296 Loglevel = Variables_map["loglevel"].as<int>();
297 Syslog = Variables_map["syslog"].as<bool>();
300 catch ( po::unknown_option e ) // at the moment 04-08-2009 this exception is never thrown :-(
302 // unknown option in main config file detected
303 main_config_file.close();
304 Log->print_unknown_main_conf_option(e.what());
307 main_config_file.close();
311 // error opening main config file for reading
312 Log->print_error_opening(full_filename);
320 * Loads the main and the service config file and does the needed action.
321 * @param config_path The path to the config directory.
322 * @return 0 if all is fine.
324 int Config::load_config_from_files()
326 fs::path full_config_path = fs::path(Config_path);
328 fs::directory_iterator end_iter;
329 for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr )
331 if( fs::is_regular_file( dir_itr->status() ) )
333 string actual_file = dir_itr->path().filename();
334 boost::regex expr(".*\\.conf?");
335 // If it is the main config file do the following
336 if ( actual_file == "bpdyndnsd.conf" )
338 // Load the main config file
339 string full_filename = dir_itr->path().string();
340 if ( load_main_config_file(full_filename) != 0 )
343 // If it is a service definition file *.conf, parse it and generate the corresponding service
344 else if ( boost::regex_search( actual_file,expr ) )
346 string full_filename = dir_itr->path().string();
347 if ( load_service_config_file(full_filename) != 0 )
352 // Config file successfully loaded
353 Log->print_conf_loaded(Config_path);
359 * Getter method for Service list member.
360 * @return Pointer to a list of Service's.
362 list<ServicePtr> Config::get_services()
364 return this->Services;
369 * Getter method for member Opt_desc_cmd.
370 * @return options_description*.
372 po::options_description* Config::get_opt_desc_cmd()
379 * Getter method for member Opt_desc_conf_main.
380 * @return options_description*.
382 po::options_description* Config::get_opt_desc_conf_main()
384 return Opt_desc_conf_main;
389 * Getter method for member Opt_desc_conf_service.
390 * @return options_description*.
392 po::options_description* Config::get_opt_desc_conf_service()
394 return Opt_desc_conf_service;
399 * Getter for member Loglevel.
400 * @return Member Loglevel.
402 int Config::get_loglevel()
409 * Getter for member Daemon_mode.
410 * @return TRUE if enabled, FALSE if disabled.
412 bool Config::get_daemon_mode()
419 * Resets all shared Service pointers and clears the Services list.
421 void Config::delete_services()
423 BOOST_FOREACH( ServicePtr service, Services )
432 * Deletes the map with the previously parsed options.
433 * This is needed in case we reload the config and don't want the old cmd options to overwrite new config file options.
435 void Config::delete_variables_map()
437 Variables_map.clear();
439 po::variables_map _variables_map;
440 Variables_map = _variables_map;
445 * Getter for member Syslog.
446 * @return True if logging through syslog is enabled, false otherwise.
448 bool Config::get_syslog()