2 * @brief Config class implementation. This class represents the actual configuration.
6 * @copyright Intra2net AG
15 * Default Constructor. Available command line and config file options with their default values are defined here.
17 Config::Config(LoggerPtr _log)
25 // Available service description config options
26 po::options_description opt_desc_service("Service description options");
27 opt_desc_service.add_options()
28 ("protocol",po::value<string>(),"The service protocol.")
29 ("host",po::value<string>(),"The hostname to update.")
30 ("login",po::value<string>(),"Login name.")
31 ("password",po::value<string>(),"Corresponding password.")
34 // Available command line only options
35 po::options_description opt_desc_cmd_only("Command line only options");
36 opt_desc_cmd_only.add_options()
37 ("help,?","Show help.")
38 ("version,v","Show version.")
39 ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
42 // Available generic options. Valid on cmd or in config file.
43 po::options_description opt_desc_generic("Generic config options");
44 opt_desc_generic.add_options()
45 ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
46 ("logfile",po::value<string>()->default_value("/var/log/bpdyndns.log"),"Where to log.")
47 ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
48 ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
51 // Define valid command line parameters
52 Opt_desc_cmd = new po::options_description("Command line options");
53 Opt_desc_cmd->add(opt_desc_cmd_only);
54 Opt_desc_cmd->add(opt_desc_generic);
55 Opt_desc_cmd->add(opt_desc_service);
57 // Define valid config file options
58 Opt_desc_conf_main = new po::options_description("Config file options");
59 Opt_desc_conf_main->add(opt_desc_generic);
61 // Define valid service file options
62 Opt_desc_conf_service = new po::options_description("Service file options");
63 Opt_desc_conf_service->add(opt_desc_service);
65 Log->print_constructor_call("Config");
75 delete Opt_desc_conf_main;
76 delete Opt_desc_conf_service;
78 Log->print_destructor_call("Config");
83 * Parses the command line arguments and does the needed actions.
84 * @param argc Command line argument number given to main.
85 * @param argv[] Pointer to command line argument array given to main.
86 * @return 0 if all is fine, 1 if not.
88 int Config::parse_cmd_line(int argc, char *argv[])
92 po::store(po::parse_command_line(argc, argv, *this->Opt_desc_cmd), Variables_map);
93 po::notify(Variables_map);
95 if ( Variables_map.count("help") )
97 Log->print_usage(Opt_desc_cmd);
100 else if ( Variables_map.count("version") )
102 Log->print_version();
106 // Create a service object if all needed options are set on the command line
107 if ( Variables_map.count("protocol") && Variables_map.count("host") && Variables_map.count("login") && Variables_map.count("password") )
109 // Get the cmd parameter values for protocol host login and password
110 string protocol = Variables_map["protocol"].as<string>();
111 string host = Variables_map["host"].as<string>();
112 string login = Variables_map["login"].as<string>();
113 string password = Variables_map["password"].as<string>();
115 //TODO: convert protocol option to lowercase
117 ServicePtr service = create_service(protocol,host,login,password);
118 if ( service != NULL )
119 Services.push_back(service);
123 else if ( Variables_map.count("protocol") || Variables_map.count("host") || Variables_map.count("login") || Variables_map.count("password") )
125 Log->print_missing_cmd_service_option();
126 Log->print_usage(Opt_desc_cmd);
130 if ( Variables_map.count("config") )
132 fs::path full_config_path = fs::system_complete(fs::path(Variables_map["config"].as<string>()));
133 Config_path = full_config_path.string();
134 if ( !fs::exists(full_config_path) || !fs::is_directory(full_config_path) )
136 // Config path doesn't exist or is not a directory
137 Log->print_error_config_path(Config_path);
142 catch(po::unknown_option e)
144 Log->print_unknown_cmd_option(e.what());
145 Log->print_usage(Opt_desc_cmd);
158 * @return A pointer to the created Service object.
160 ServicePtr Config::create_service(const string &protocol,const string &host, const string &login, const string &password)
162 if(protocol == "dhs")
164 ServicePtr service_dhs(new DHS(Log,host,login,password));
167 else if(protocol == "ods")
169 ServicePtr service_ods(new ODS(Log,host,login,password));
174 Log->print_unknown_protocol(protocol);
182 * Loads a service config file, invoked by load_config_from_files.
183 * @param full_filename Filename of the service config file to load.
184 * @return 0 if all is fine, 3 if an unknown option was detected, 4 if the service file could not be opened for reading.
186 int Config::load_service_config_file(const string& full_filename)
188 Log->print_load_service_conf(full_filename);
190 ifstream service_config_file(full_filename.c_str(),ifstream::in);
191 if(service_config_file.is_open())
195 po::variables_map vm;
196 po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->Opt_desc_conf_service,true);
197 po::store(parsed_service_options,vm);
200 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
202 // create the corresponding service
203 string protocol = vm["protocol"].as<string>();
204 string host = vm["host"].as<string>();
205 string login = vm["login"].as<string>();
206 string password = vm["password"].as<string>();
208 // TODO: convert protocol to lowercase
209 //protocol = tolower(protocol.c_str());
211 ServicePtr service = create_service(protocol,host,login,password);
212 if ( service != NULL )
214 Services.push_back(service);
218 catch ( po::unknown_option e )
220 // unknown option in config file detected
221 service_config_file.close();
222 Log->print_unknown_service_conf_option(e.what());
225 service_config_file.close();
229 // error opening service config file for reading
230 Log->print_error_opening(full_filename);
238 * Loads the main config file, invoked by load_config_from_files
239 * @param full_filename The full filename of the main config file to load
240 * @return 0 if all is fine. 3 if unknown option was detected, 4 if main config file could not be opened for reading
242 int Config::load_main_config_file(const string& full_filename)
244 Log->print_load_main_conf(full_filename);
246 ifstream main_config_file(full_filename.c_str(),ifstream::in);
247 if(main_config_file.is_open())
251 po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->Opt_desc_conf_main,true);
252 po::store(parsed_main_options,Variables_map);
253 po::notify(Variables_map);
255 if(Variables_map.count("daemon_mode") && Variables_map.count("logfile") && Variables_map.count("loglevel") && Variables_map.count("syslog"))
257 Daemon_mode = Variables_map["daemon_mode"].as<bool>();
258 Logfile = Variables_map["logfile"].as<string>();
259 Loglevel = Variables_map["loglevel"].as<int>();
260 Syslog = Variables_map["syslog"].as<bool>();
263 catch ( po::unknown_option e )
265 // unknown option in main config file detected
266 main_config_file.close();
267 Log->print_unknown_main_conf_option(e.what());
270 main_config_file.close();
274 // error opening main config file for reading
275 Log->print_error_opening(full_filename);
283 * Loads the main and the service config file and does the needed action.
284 * @param config_path The path to the config directory.
285 * @return 0 if all is fine.
287 int Config::load_config_from_files()
289 fs::path full_config_path = fs::path(Config_path);
291 fs::directory_iterator end_iter;
292 for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr )
294 if( fs::is_regular_file( dir_itr->status() ) )
296 string actual_file = dir_itr->path().filename();
297 boost::regex expr(".*\.conf?");
298 // If it is the main config file do the following
299 if ( actual_file == "bpdyndnsd.conf" )
301 // Load the main config file
302 string full_filename = dir_itr->path().string();
303 if ( load_main_config_file(full_filename) != 0 )
306 // If it is a service definition file *.conf, parse it and generate the corresponding service
307 else if ( boost::regex_search( actual_file,expr ) )
309 string full_filename = dir_itr->path().string();
310 if ( load_service_config_file(full_filename) != 0 )
315 // Config file successfully loaded
316 Log->print_conf_loaded(Config_path);
322 * Getter method for Service list member.
323 * @return Pointer to a list of Service's.
325 list<ServicePtr> Config::get_services()
327 return this->Services;
332 * Getter method for member Opt_desc_cmd.
333 * @return options_description*.
335 po::options_description* Config::get_opt_desc_cmd()
342 * Getter method for member Opt_desc_conf_main.
343 * @return options_description*.
345 po::options_description* Config::get_opt_desc_conf_main()
347 return Opt_desc_conf_main;
352 * Getter method for member Opt_desc_conf_service.
353 * @return options_description*.
355 po::options_description* Config::get_opt_desc_conf_service()
357 return Opt_desc_conf_service;
361 * Getter for member Loglevel.
362 * @return Member Loglevel.
364 int Config::get_loglevel()