fix compile with newer boost version
[bpdyndnsd] / src / config.cpp
CommitLineData
5c460c94
BS
1/** @file
2 * @brief Config class implementation. This class represents the actual configuration.
3 *
4 *
5 *
6 * @copyright Intra2net AG
7 * @license GPLv2
8*/
4545a371 9
4de6a9b8 10#include "config.hpp"
fc87cdbb 11
4de6a9b8
BS
12#include "service_dhs.hpp"
13#include "service_ods.hpp"
14#include "service_dyndns.hpp"
15#include "service_dyns.hpp"
16#include "service_easydns.hpp"
17#include "service_tzo.hpp"
18#include "service_zoneedit.hpp"
19#include "service_gnudip.hpp"
88a594e8
BS
20
21#include <time.h>
22#include <iostream>
23#include <fstream>
24
25#include <boost/foreach.hpp>
26#include <boost/filesystem.hpp>
27#include <boost/regex.hpp>
4a527a29 28#include <boost/algorithm/string.hpp>
88a594e8 29
e0080b78 30
8bca3c5d
BS
31namespace po = boost::program_options;
32namespace fs = boost::filesystem;
4a527a29 33namespace ba = boost::algorithm;
8bca3c5d 34
92beaba3
BS
35typedef boost::shared_ptr<boost::program_options::options_description> Options_descriptionPtr;
36
8bca3c5d 37using namespace std;
254bbf53 38
31af6a2e 39
5c460c94 40/**
85a0abf9 41 * Default Constructor. Available command line and config file options with their default values are defined here.
5c460c94 42 */
31af6a2e
BS
43Config::Config()
44 : Log(new Logger)
45 , ServiceHolder(new Serviceholder)
46 , DaemonMode(false)
47 , Syslog(false)
48 , EnableIPv6(false)
49 , Loglevel(0)
50 , ConfigPath("/etc/bpdyndnsd")
51 , WebcheckInterval(0)
52 , ProxyPort(0)
53 , ExternalWarningLog("")
54 , ExternalWarningLevel(0)
6b63b758 55 , StartOffline(false)
46fffdd6 56 , WebcheckEnabled(false)
4475e30a 57 , ExternalLogOnlyOnce(false)
2e9bd873
TJ
58 , DialupMode(false)
59 , DialupBurstPeriodSeconds(120)
60 , DialupSleepSeconds(10 * 60)
31af6a2e 61{
38a04d54 62 define_config_options();
31af6a2e
BS
63}
64
65
66/**
67 * Constructor with Logger and Serviceholder objects. Available command line and config file options with their default values are defined here.
2e9bd873 68 * @todo Move the program options init code to a function used by both constructors
31af6a2e 69 */
e0080b78 70Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
9a0aff44
BS
71 : Log(_log)
72 , ServiceHolder(_serviceholder)
73 , DaemonMode(false)
85a0abf9 74 , Syslog(false)
019dc0d9 75 , EnableIPv6(false)
e0080b78
BS
76 , Loglevel(0)
77 , ConfigPath("/etc/bpdyndnsd")
2b0f7c11 78 , WebcheckInterval(0)
891ae3b9 79 , ProxyPort(0)
cbbdeb6c
BS
80 , ExternalWarningLog("")
81 , ExternalWarningLevel(0)
6b63b758 82 , StartOffline(false)
46fffdd6 83 , WebcheckEnabled(false)
4475e30a 84 , ExternalLogOnlyOnce(false)
2e9bd873
TJ
85 , DialupMode(false)
86 , DialupBurstPeriodSeconds(120)
87 , DialupSleepSeconds(10 * 60)
4545a371 88{
38a04d54
BS
89 define_config_options();
90}
91
92
93/**
94 * Default Destructor
95 */
96Config::~Config()
97{
98}
99
100
101/**
102* Define valid config options with default parameters.
103*/
104void Config::define_config_options()
105{
3434b35f
BS
106 // Available service description config options
107 po::options_description opt_desc_service("Service description options");
108 opt_desc_service.add_options()
109 ("protocol",po::value<string>(),"The service protocol.")
f04a7cb4 110 ("server",po::value<string>(),"Servername needed for gnudip/dyndns protocol.")
3434b35f
BS
111 ("host",po::value<string>(),"The hostname to update.")
112 ("login",po::value<string>(),"Login name.")
113 ("password",po::value<string>(),"Corresponding password.")
3c0cd271
BS
114 ("update_interval",po::value<int>()->default_value(-1),"Update interval in minutes.")
115 ("max_updates_within_interval",po::value<int>()->default_value(-1),"How many updates can be made in one interval.")
4553e833 116 ("max_equal_updates_in_succession",po::value<int>()->default_value(-1),"How many updates with the same IP in succession should be made.")
c3dea5dc 117 ("dns_cache_ttl",po::value<int>()->default_value(-1),"How long a dns record is valid.")
3434b35f
BS
118 ;
119
120 // Available command line only options
121 po::options_description opt_desc_cmd_only("Command line only options");
122 opt_desc_cmd_only.add_options()
4545a371
BS
123 ("help,?","Show help.")
124 ("version,v","Show version.")
254bbf53 125 ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
4545a371
BS
126 ;
127
38a04d54 128 // Available generic options. Valid on cmd or in config file.
3434b35f
BS
129 po::options_description opt_desc_generic("Generic config options");
130 opt_desc_generic.add_options()
131 ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
3434b35f
BS
132 ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
133 ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
019dc0d9 134 ("enable_ipv6",po::value<bool>()->default_value(false),"Try to use IPv6.")
46fffdd6 135 ("webcheck_enabled",po::value<bool>()->default_value(false),"Use webcheck url to determine actual IP address.")
019dc0d9
BS
136 ("webcheck_url",po::value<string>()->default_value(""),"Use this URL to determine IP.")
137 ("webcheck_url_alt",po::value<string>()->default_value(""),"Use this alternative URL to determine IP.")
2b0f7c11 138 ("webcheck_interval",po::value<int>()->default_value(10),"The webcheck interval in minutes.")
8a00a649
BS
139 ("http_proxy",po::value<string>(),"Use this proxy for all http requests.")
140 ("http_proxy_port",po::value<int>(),"Port of the proxy.")
cbbdeb6c
BS
141 ("external_warning_log",po::value<string>()->default_value(""),"External programm to pass warning log messages to.")
142 ("external_warning_level",po::value<int>()->default_value(0),"Warning messages of which loglevel should be passed to external programm.")
e8787e2e 143 ("external_log_only_once",po::value<bool>()->default_value(false),"Log the same external message only once until next reload or restart.")
6b63b758 144 ("start_offline",po::value<bool>()->default_value(false),"Start in offline mode.")
2e9bd873
TJ
145 ("dialup_mode",po::value<bool>()->default_value(false),"Enable dialup mode (sleep periods between network traffic)")
146 ("dialup_burst_period_seconds",po::value<int>()->default_value(120),"Seconds of normal operation before entering dialup mode")
147 ("dialup_sleep_seconds",po::value<int>()->default_value(10 * 60),"Seconds to sleep between network traffic")
6114d87c 148 ("wan_ip_override",po::value<string>(),"Manual override for automatic WAN IP detection")
fc87cdbb 149 ;
4545a371 150
3434b35f 151 // Define valid command line parameters
c3c84086 152 OptDescCmd = Options_descriptionPtr(new po::options_description("Command line options"));
025abebb
BS
153 OptDescCmd->add(opt_desc_cmd_only);
154 OptDescCmd->add(opt_desc_generic);
155 OptDescCmd->add(opt_desc_service);
3434b35f
BS
156
157 // Define valid config file options
c3c84086 158 OptDescConfMain = Options_descriptionPtr(new po::options_description("Config file options"));
025abebb 159 OptDescConfMain->add(opt_desc_generic);
3434b35f
BS
160
161 // Define valid service file options
c3c84086 162 OptDescConfService = Options_descriptionPtr(new po::options_description("Service file options"));
025abebb 163 OptDescConfService->add(opt_desc_service);
4545a371
BS
164}
165
fc87cdbb 166
5c460c94 167/**
5c460c94
BS
168 * Parses the command line arguments and does the needed actions.
169 * @param argc Command line argument number given to main.
170 * @param argv[] Pointer to command line argument array given to main.
3404d89f 171 * @return 0 if all is fine, -1 if not.
5c460c94 172 */
e95a6634 173int Config::parse_cmd_line(int argc, char *argv[])
4545a371
BS
174{
175 try
176 {
025abebb
BS
177 po::store(po::parse_command_line(argc, argv, *this->OptDescCmd), VariablesMap);
178 po::notify(VariablesMap);
4545a371 179
025abebb 180 if ( VariablesMap.count("help") )
254bbf53 181 {
025abebb 182 Log->print_usage(OptDescCmd);
3404d89f 183 return -1;
254bbf53 184 }
025abebb 185 else if ( VariablesMap.count("version") )
254bbf53
BS
186 {
187 Log->print_version();
3404d89f 188 return -1;
254bbf53 189 }
4545a371 190
254bbf53 191 // Create a service object if all needed options are set on the command line
025abebb 192 if ( VariablesMap.count("protocol") && VariablesMap.count("host") && VariablesMap.count("login") && VariablesMap.count("password") )
4545a371 193 {
fc87cdbb 194 // Get the cmd parameter values for protocol host login and password
025abebb
BS
195 string protocol = VariablesMap["protocol"].as<string>();
196 string host = VariablesMap["host"].as<string>();
197 string login = VariablesMap["login"].as<string>();
198 string password = VariablesMap["password"].as<string>();
4545a371 199
4a527a29 200 protocol = ba::to_lower_copy(protocol);
4545a371 201
a78b44b5
BS
202 string server;
203 if ( VariablesMap.count("server") )
204 server = VariablesMap["server"].as<string>();
205
3c0cd271
BS
206 int update_interval = 0;
207 if ( VariablesMap.count("update_interval") )
208 update_interval = VariablesMap["update_interval"].as<int>();
209
210 int max_updates_within_interval = 0;
211 if ( VariablesMap.count("max_updates_within_interval") )
212 max_updates_within_interval = VariablesMap["max_updates_within_interval"].as<int>();
213
4553e833
BS
214 int max_equal_updates_in_succession = 0;
215 if ( VariablesMap.count("max_equal_updates_in_succession") )
216 max_equal_updates_in_succession = VariablesMap["max_equal_updates_in_succession"].as<int>();
217
c3dea5dc
BS
218 int dns_cache_ttl = 0;
219 if ( VariablesMap.count("dns_cache_ttl") )
220 dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
221
4553e833 222 Service::Ptr service = create_service(protocol,server,host,login,password,update_interval,max_updates_within_interval,max_equal_updates_in_succession,dns_cache_ttl);
3a3f3566 223 if ( service )
e0080b78
BS
224 {
225 ServiceHolder->add_service(service);
7335d7a7 226 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_max_equal_updates_in_succession(), service->get_dns_cache_ttl() , service->get_actual_ip(), service->get_last_updates(), service->get_activated());
e0080b78 227 }
254bbf53 228 else
60657d55
BS
229 {
230 Log->print_invalid_service_config();
231 }
4545a371 232 }
025abebb 233 else if ( VariablesMap.count("protocol") || VariablesMap.count("host") || VariablesMap.count("login") || VariablesMap.count("password") )
4545a371 234 {
254bbf53 235 Log->print_missing_cmd_service_option();
025abebb 236 Log->print_usage(OptDescCmd);
3404d89f 237 return -1;
4545a371 238 }
254bbf53 239
025abebb 240 if ( VariablesMap.count("config") )
254bbf53 241 {
025abebb
BS
242 fs::path full_config_path = fs::system_complete(fs::path(VariablesMap["config"].as<string>()));
243 ConfigPath = full_config_path.string();
254bbf53
BS
244 if ( !fs::exists(full_config_path) || !fs::is_directory(full_config_path) )
245 {
246 // Config path doesn't exist or is not a directory
025abebb 247 Log->print_error_config_path(ConfigPath);
3404d89f 248 return -1;
254bbf53
BS
249 }
250 }
59c8d63c 251
019dc0d9
BS
252 if ( VariablesMap.count("daemon_mode") )
253 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
254
025abebb
BS
255 if ( VariablesMap.count("loglevel") )
256 Loglevel = VariablesMap["loglevel"].as<int>();
59c8d63c 257
025abebb
BS
258 if ( VariablesMap.count("syslog") )
259 Syslog = VariablesMap["syslog"].as<bool>();
59c8d63c 260
019dc0d9
BS
261 if ( VariablesMap.count("enable_ipv6") )
262 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
263
46fffdd6
BS
264 if ( VariablesMap.count("webcheck_enabled") )
265 WebcheckEnabled = VariablesMap["webcheck_enabled"].as<bool>();
266
019dc0d9
BS
267 if ( VariablesMap.count("webcheck_url") )
268 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
269
270 if ( VariablesMap.count("webcheck_url_alt") )
271 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
272
2b0f7c11
BS
273 if ( VariablesMap.count("webcheck_interval") )
274 WebcheckInterval = VariablesMap["webcheck_interval"].as<int>();
275
8a00a649
BS
276 if ( VariablesMap.count("http_proxy") && VariablesMap.count("http_proxy_port") )
277 {
4eb87664 278 Proxy = VariablesMap["http_proxy"].as<string>();
4eb87664 279 ProxyPort = VariablesMap["http_proxy_port"].as<int>();
8a00a649
BS
280 }
281 else if ( VariablesMap.count("http_proxy") || VariablesMap.count("http_proxy_port") )
282 {
283 Log->print_missing_cmd_proxy_option();
284 Log->print_usage(OptDescCmd);
285 return -1;
286 }
287
cbbdeb6c
BS
288 if ( VariablesMap.count("external_warning_log") )
289 ExternalWarningLog = VariablesMap["external_warning_log"].as<string>();
290
291 if ( VariablesMap.count("external_warning_level") )
292 ExternalWarningLevel = VariablesMap["external_warning_level"].as<int>();
4eb87664 293
e8787e2e 294 if ( VariablesMap.count("external_log_only_once") )
4475e30a 295 ExternalLogOnlyOnce = VariablesMap["external_log_only_once"].as<bool>();
e8787e2e 296
6b63b758
BS
297 if ( VariablesMap.count("start_offline") )
298 StartOffline = VariablesMap["start_offline"].as<bool>();
299
2e9bd873
TJ
300 if ( VariablesMap.count("dialup_mode") )
301 DialupMode = VariablesMap["dialup_mode"].as<bool>();
302 if ( VariablesMap.count("dialup_burst_period_seconds") )
303 DialupBurstPeriodSeconds = VariablesMap["dialup_burst_period_seconds"].as<int>();
304 if ( VariablesMap.count("dialup_sleep_seconds") )
305 DialupSleepSeconds = VariablesMap["dialup_sleep_seconds"].as<int>();
6114d87c
TJ
306
307 if ( VariablesMap.count("wan_ip_override") )
308 WanIpOverride = VariablesMap["wan_ip_override"].as<string>();
4545a371 309 }
08a5a621 310 catch( const po::unknown_option& e )
4545a371 311 {
254bbf53 312 Log->print_unknown_cmd_option(e.what());
025abebb 313 Log->print_usage(OptDescCmd);
3404d89f 314 return -1;
4545a371 315 }
08a5a621 316 catch( const po::multiple_occurrences& e )
3c0cd271
BS
317 {
318 Log->print_multiple_cmd_option(e.what());
319 Log->print_usage(OptDescCmd);
320 return -1;
321 }
08a5a621 322 catch( const po::error& e )
c1b8cb79
BS
323 {
324 Log->print_error_parsing_cmd(e.what());
325 Log->print_usage(OptDescCmd);
326 return -1;
327 }
4545a371
BS
328 return 0;
329}
330
fc87cdbb 331
5c460c94 332/**
019dc0d9
BS
333 * Creates a Service object from the given parameters.
334 * @param protocol Protocol to use.
335 * @param host Hostname to update.
336 * @param login Login.
337 * @param password Password.
0680036d
BS
338 * @return A pointer to the created Service object.
339 */
4553e833 340Service::Ptr Config::create_service(const string &protocol, const string& server, const string& hostname, const string& login, const string& password, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const int dns_cache_ttl)
0680036d 341{
1a00eac6
BS
342 // Test for valid hostname. Must contain 3 parts minimum.
343 list<string> fqhn_parts;
344 ba::split(fqhn_parts,hostname,boost::is_any_of("."));
345 if ( fqhn_parts.size() < 3 )
346 {
60657d55 347 Log->print_invalid_hostname(hostname);
1a00eac6
BS
348 Service::Ptr service;
349 return service;
350 }
351
0680036d
BS
352 if(protocol == "dhs")
353 {
4553e833 354 Service::Ptr service_dhs(new ServiceDhs(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,max_equal_updates_in_succession,dns_cache_ttl,Proxy,ProxyPort));
85a0abf9 355 return service_dhs;
0680036d
BS
356 }
357 else if(protocol == "ods")
358 {
4553e833 359 Service::Ptr service_ods(new ServiceOds(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,max_equal_updates_in_succession,dns_cache_ttl));
85a0abf9
BS
360 return service_ods;
361 }
b6228761
BS
362 else if(protocol == "dyndns")
363 {
4553e833 364 Service::Ptr service_dyndns(new ServiceDyndns(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,max_equal_updates_in_succession,dns_cache_ttl,Proxy,ProxyPort,server));
b6228761
BS
365 return service_dyndns;
366 }
b30f392d
BS
367 else if(protocol == "dyns")
368 {
4553e833 369 Service::Ptr service_dyns(new ServiceDyns(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,max_equal_updates_in_succession,dns_cache_ttl,Proxy,ProxyPort));
b30f392d
BS
370 return service_dyns;
371 }
1a00eac6
BS
372 else if(protocol == "easydns")
373 {
4553e833 374 Service::Ptr service_easydns(new ServiceEasydns(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,max_equal_updates_in_succession,dns_cache_ttl,Proxy,ProxyPort));
1a00eac6
BS
375 return service_easydns;
376 }
b17fd691
BS
377 else if(protocol == "tzo")
378 {
4553e833 379 Service::Ptr service_tzo(new ServiceTzo(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,max_equal_updates_in_succession,dns_cache_ttl,Proxy,ProxyPort));
b17fd691
BS
380 return service_tzo;
381 }
3f8b2557
BS
382 else if(protocol == "zoneedit")
383 {
4553e833 384 Service::Ptr service_zoneedit(new ServiceZoneedit(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,max_equal_updates_in_succession,dns_cache_ttl,Proxy,ProxyPort));
3f8b2557
BS
385 return service_zoneedit;
386 }
a78b44b5
BS
387 else if(protocol == "gnudip")
388 {
a78b44b5
BS
389 if ( !server.empty() )
390 {
4553e833 391 Service::Ptr service_gnudip(new ServiceGnudip(protocol,server,hostname,login,password,Log,update_interval,max_updates_within_interval,max_equal_updates_in_succession,dns_cache_ttl,Proxy,ProxyPort));
a78b44b5
BS
392 return service_gnudip;
393 }
394 else
395 {
396 Log->print_gnudip_requires_servername();
397 Service::Ptr service;
398 return service;
399 }
400 }
85a0abf9
BS
401 else
402 {
254bbf53 403 Log->print_unknown_protocol(protocol);
88a594e8 404 Service::Ptr service;
83b60bd6 405 return service;
0680036d 406 }
0680036d
BS
407}
408
409
410/**
1cf4e2b5
BS
411 * Loads a service config file, invoked by load_config_from_files.
412 * @param full_filename Filename of the service config file to load.
3404d89f 413 * @return 0 if all is fine, -1 otherwise.
1cf4e2b5 414 */
e95a6634 415int Config::load_service_config_file(const string& full_filename)
1cf4e2b5 416{
254bbf53
BS
417 Log->print_load_service_conf(full_filename);
418
1cf4e2b5
BS
419 ifstream service_config_file(full_filename.c_str(),ifstream::in);
420 if(service_config_file.is_open())
421 {
422 try
423 {
424 po::variables_map vm;
a05aae3d 425 po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->OptDescConfService,false);
1cf4e2b5
BS
426 po::store(parsed_service_options,vm);
427 po::notify(vm);
428
3434b35f 429 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
1cf4e2b5
BS
430 {
431 // create the corresponding service
3434b35f
BS
432 string protocol = vm["protocol"].as<string>();
433 string host = vm["host"].as<string>();
434 string login = vm["login"].as<string>();
c1b8cb79 435 string password = vm["password"].as<string>();
1cf4e2b5 436
4a527a29 437 protocol = ba::to_lower_copy(protocol);
1cf4e2b5 438
a78b44b5
BS
439 string server;
440 if ( vm.count("server") )
441 server = vm["server"].as<string>();
442
3c0cd271 443 int update_interval = 0;
8a00a649 444 if ( vm.count("update_interval") )
a78b44b5 445 update_interval = vm["update_interval"].as<int>();
3c0cd271
BS
446
447 int max_updates_within_interval = 0;
8a00a649 448 if ( vm.count("max_updates_within_interval") )
a78b44b5 449 max_updates_within_interval = vm["max_updates_within_interval"].as<int>();
3c0cd271 450
4553e833
BS
451 int max_equal_updates_in_succession = 0;
452 if ( vm.count("max_equal_updates_in_succession") )
453 max_equal_updates_in_succession = vm["max_equal_updates_in_succession"].as<int>();
454
c3dea5dc 455 int dns_cache_ttl = 0;
8a00a649 456 if ( vm.count("dns_cache_ttl") )
a78b44b5 457 dns_cache_ttl = vm["dns_cache_ttl"].as<int>();
c3dea5dc 458
4553e833 459 Service::Ptr service = create_service(protocol,server,host,login,password,update_interval,max_updates_within_interval,max_equal_updates_in_succession,dns_cache_ttl);
3a3f3566 460 if ( service )
1cf4e2b5 461 {
e0080b78 462 ServiceHolder->add_service(service);
7335d7a7 463 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_max_equal_updates_in_succession(), service->get_dns_cache_ttl() , service->get_actual_ip(), service->get_last_updates(), service->get_activated());
1cf4e2b5 464 }
e0080b78 465 else
60657d55
BS
466 {
467 Log->print_invalid_service_config();
468 }
1cf4e2b5 469 }
8a00a649
BS
470 else if ( vm.count("protocol") || vm.count("host") || vm.count("login") || vm.count("password") )
471 {
472 service_config_file.close();
473 Log->print_missing_service_conf_option(full_filename);
474 return -1;
475 }
1cf4e2b5 476 }
08a5a621 477 catch( const po::unknown_option& e )
1cf4e2b5
BS
478 {
479 // unknown option in config file detected
480 service_config_file.close();
8a00a649
BS
481 Log->print_unknown_service_conf_option(full_filename,e.what());
482 return -1;
483 }
08a5a621 484 catch( const po::multiple_occurrences& e )
8a00a649
BS
485 {
486 service_config_file.close();
487 Log->print_multiple_service_conf_option(full_filename,e.what());
3404d89f 488 return -1;
1cf4e2b5 489 }
08a5a621 490 catch( const po::error& e )
c1b8cb79
BS
491 {
492 service_config_file.close();
493 Log->print_error_parsing_config_file(full_filename,e.what());
494 return -1;
495 }
1cf4e2b5
BS
496 service_config_file.close();
497 }
498 else
499 {
254bbf53 500 // error opening service config file for reading
667c672c 501 Log->print_error_opening_r(full_filename);
3404d89f 502 return -1;
1cf4e2b5
BS
503 }
504 return 0;
505}
506
254bbf53 507
1cf4e2b5
BS
508/**
509 * Loads the main config file, invoked by load_config_from_files
510 * @param full_filename The full filename of the main config file to load
3404d89f 511 * @return 0 if all is fine, -1 otherwise
1cf4e2b5 512 */
254bbf53 513int Config::load_main_config_file(const string& full_filename)
1cf4e2b5 514{
254bbf53
BS
515 Log->print_load_main_conf(full_filename);
516
1cf4e2b5
BS
517 ifstream main_config_file(full_filename.c_str(),ifstream::in);
518 if(main_config_file.is_open())
519 {
520 try
521 {
a05aae3d 522 po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->OptDescConfMain,false);
025abebb
BS
523 po::store(parsed_main_options,VariablesMap);
524 po::notify(VariablesMap);
1cf4e2b5 525
8a00a649
BS
526 if ( VariablesMap.count("daemon_mode") )
527 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
019dc0d9 528
8a00a649
BS
529 if ( VariablesMap.count("loglevel") )
530 Loglevel = VariablesMap["loglevel"].as<int>();
019dc0d9 531
8a00a649
BS
532 if ( VariablesMap.count("syslog") )
533 Syslog = VariablesMap["syslog"].as<bool>();
019dc0d9 534
8a00a649
BS
535 if ( VariablesMap.count("enable_ipv6") )
536 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
019dc0d9 537
46fffdd6
BS
538 if ( VariablesMap.count("webcheck_enabled") )
539 WebcheckEnabled = VariablesMap["webcheck_enabled"].as<bool>();
540
8a00a649
BS
541 if ( VariablesMap.count("webcheck_url") )
542 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
019dc0d9 543
8a00a649
BS
544 if ( VariablesMap.count("webcheck_url_alt") )
545 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
4eb87664 546
2b0f7c11
BS
547 if ( VariablesMap.count("webcheck_interval") )
548 WebcheckInterval = VariablesMap["webcheck_interval"].as<int>();
549
8a00a649
BS
550 if ( VariablesMap.count("http_proxy") && VariablesMap.count("http_proxy_port") )
551 {
552 Proxy = VariablesMap["http_proxy"].as<string>();
553 ProxyPort = VariablesMap["http_proxy_port"].as<int>();
554 }
555 else if ( VariablesMap.count("http_proxy") || VariablesMap.count("http_proxy_port") )
556 {
557 main_config_file.close();
558 Log->print_missing_conf_proxy_option(full_filename);
559 return -1;
560 }
4eb87664 561
cbbdeb6c
BS
562 if ( VariablesMap.count("external_warning_log") )
563 ExternalWarningLog = VariablesMap["external_warning_log"].as<string>();
564
565 if ( VariablesMap.count("external_warning_level") )
566 ExternalWarningLevel = VariablesMap["external_warning_level"].as<int>();
567
e8787e2e 568 if ( VariablesMap.count("external_log_only_once") )
4475e30a 569 ExternalLogOnlyOnce = VariablesMap["external_log_only_once"].as<bool>();
e8787e2e 570
6b63b758
BS
571 if ( VariablesMap.count("start_offline") )
572 StartOffline = VariablesMap["start_offline"].as<bool>();
573
2e9bd873
TJ
574 if ( VariablesMap.count("dialup_mode") )
575 DialupMode = VariablesMap["dialup_mode"].as<bool>();
576 if ( VariablesMap.count("dialup_burst_period_seconds") )
577 DialupBurstPeriodSeconds = VariablesMap["dialup_burst_period_seconds"].as<int>();
578 if ( VariablesMap.count("dialup_sleep_seconds") )
579 DialupSleepSeconds = VariablesMap["dialup_sleep_seconds"].as<int>();
6114d87c
TJ
580
581 if ( VariablesMap.count("wan_ip_override") )
582 WanIpOverride = VariablesMap["wan_ip_override"].as<string>();
1cf4e2b5 583 }
a05aae3d 584 catch( const po::unknown_option& e )
1cf4e2b5 585 {
254bbf53 586 // unknown option in main config file detected
1cf4e2b5 587 main_config_file.close();
254bbf53 588 Log->print_unknown_main_conf_option(e.what());
3404d89f 589 return -1;
1cf4e2b5 590 }
08a5a621 591 catch( const po::multiple_occurrences& e )
8a00a649
BS
592 {
593 main_config_file.close();
594 Log->print_multiple_main_conf_option(full_filename,e.what());
595 return -1;
596 }
1cf4e2b5
BS
597 main_config_file.close();
598 }
599 else
600 {
254bbf53 601 // error opening main config file for reading
667c672c 602 Log->print_error_opening_r(full_filename);
3404d89f 603 return -1;
1cf4e2b5
BS
604 }
605 return 0;
606}
607
254bbf53 608
1cf4e2b5 609/**
5c460c94
BS
610 * Loads the main and the service config file and does the needed action.
611 * @param config_path The path to the config directory.
3404d89f 612 * @return 0 if all is fine, -1 otherwise
5c460c94 613 */
e95a6634 614int Config::load_config_from_files()
4545a371 615{
025abebb 616 fs::path full_config_path = fs::path(ConfigPath);
7ec77cf1 617
254bbf53
BS
618 fs::directory_iterator end_iter;
619 for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr )
7ec77cf1 620 {
254bbf53 621 if( fs::is_regular_file( dir_itr->status() ) )
7ec77cf1 622 {
f2d91b41
GE
623#if BOOST_FILESYSTEM_VERSION >= 3
624 string actual_file = dir_itr->path().filename().string();
625#else
254bbf53 626 string actual_file = dir_itr->path().filename();
f2d91b41 627#endif
b4d8d055 628 boost::regex expr(".*\\.conf$");
254bbf53
BS
629 // If it is the main config file do the following
630 if ( actual_file == "bpdyndnsd.conf" )
7ec77cf1 631 {
254bbf53
BS
632 // Load the main config file
633 string full_filename = dir_itr->path().string();
634 if ( load_main_config_file(full_filename) != 0 )
3404d89f 635 return -1;
254bbf53
BS
636 }
637 // If it is a service definition file *.conf, parse it and generate the corresponding service
638 else if ( boost::regex_search( actual_file,expr ) )
639 {
640 string full_filename = dir_itr->path().string();
e95a6634 641 if ( load_service_config_file(full_filename) != 0 )
3404d89f 642 return -1;
fc87cdbb
BS
643 }
644 }
fc87cdbb 645 }
254bbf53 646 // Config file successfully loaded
025abebb 647 Log->print_conf_loaded(ConfigPath);
b1be615b 648 return 0;
4545a371
BS
649}
650
fc87cdbb 651
5c460c94 652/**
025abebb 653 * Getter method for member OptDescCmd.
254bbf53 654 * @return options_description*.
5c460c94 655 */
92beaba3 656Options_descriptionPtr Config::get_opt_desc_cmd() const
fc87cdbb 657{
025abebb 658 return OptDescCmd;
4545a371
BS
659}
660
fc87cdbb 661
5c460c94 662/**
025abebb 663 * Getter method for member OptDescConfMain.
254bbf53 664 * @return options_description*.
5c460c94 665 */
92beaba3 666Options_descriptionPtr Config::get_opt_desc_conf_main() const
4545a371 667{
025abebb 668 return OptDescConfMain;
4545a371 669}
38060291 670
254bbf53 671
38060291 672/**
025abebb 673 * Getter method for member OptDescConfService.
254bbf53 674 * @return options_description*.
38060291 675 */
92beaba3 676Options_descriptionPtr Config::get_opt_desc_conf_service() const
38060291 677{
025abebb 678 return OptDescConfService;
38060291 679}
3434b35f 680
c5675c01 681
3434b35f
BS
682/**
683 * Getter for member Loglevel.
684 * @return Member Loglevel.
685 */
b38684ce 686int Config::get_loglevel() const
3434b35f
BS
687{
688 return Loglevel;
689}
388f4ab0 690
c5675c01 691
388f4ab0 692/**
025abebb 693 * Getter for member DaemonMode.
388f4ab0
BS
694 * @return TRUE if enabled, FALSE if disabled.
695 */
b38684ce 696bool Config::get_daemon_mode() const
388f4ab0 697{
025abebb 698 return DaemonMode;
388f4ab0 699}
c5675c01
BS
700
701
702/**
8bca3c5d
BS
703 * Deletes the map with the previously parsed options.
704 * This is needed in case we reload the config and don't want the old cmd options to overwrite new config file options.
705 */
706void Config::delete_variables_map()
707{
025abebb 708 VariablesMap.clear();
8bca3c5d
BS
709
710 po::variables_map _variables_map;
025abebb 711 VariablesMap = _variables_map;
8bca3c5d
BS
712}
713
714
715/**
716 * Getter for member Syslog.
717 * @return True if logging through syslog is enabled, false otherwise.
718 */
b38684ce 719bool Config::get_syslog() const
8bca3c5d
BS
720{
721 return Syslog;
722}
019dc0d9
BS
723
724
725/**
726 * Getter for member EnableIPv6
727 * @return Wether IPv6 should be used or not.
728 */
729bool Config::get_enable_ipv6() const
730{
731 return EnableIPv6;
732}
733
734
735/**
46fffdd6
BS
736 * Getter for member WebcheckEnabled
737 * @return Is webcheck enabled by default.
738 */
739bool Config::get_webcheck_enabled() const
740{
741 return WebcheckEnabled;
742}
743
744
745/**
746 * Setter for member WebcheckEnabled
747 * @return Is webcheck enabled by default.
748 */
749void Config::set_webcheck_enabled( bool webcheck_enabled )
750{
751 WebcheckEnabled = webcheck_enabled;
752}
753
754
755/**
019dc0d9
BS
756 * Getter for member WebcheckIpUrl
757 * @return The primary IP Webcheck URL
758 */
759string Config::get_webcheck_ip_url() const
760{
761 return WebcheckIpUrl;
762}
763
764
765/**
766 * Getter for member WebcheckIpUrlAlt
767 * @return The alternative IP Webcheck URL
768 */
769string Config::get_webcheck_ip_url_alt() const
770{
771 return WebcheckIpUrlAlt;
772}
4eb87664
BS
773
774
775/**
2b0f7c11
BS
776 * Get member WebcheckInterval
777 * @return WebcheckInterval
778 */
779int Config::get_webcheck_interval() const
780{
781 return WebcheckInterval;
782}
783
784
785/**
4eb87664
BS
786 * Get member Proxy
787 * @return Proxy
788 */
789string Config::get_proxy() const
790{
791 return Proxy;
792}
793
794
795/**
796 * Get member ProxyPort
797 * @return ProxyPort
798 */
799int Config::get_proxy_port() const
800{
801 return ProxyPort;
802}
cbbdeb6c
BS
803
804
805/**
806 * Get member ExternalWarningLog
807 * @return ExternalWarningLog
808 */
809string Config::get_external_warning_log() const
810{
811 return ExternalWarningLog;
812}
813
814
815/**
816 * Get member ExternalWarningLevel
817 * @return ExternalWaringLevel
818 */
819int Config::get_external_warning_level() const
820{
821 return ExternalWarningLevel;
822}
823
6b63b758
BS
824
825/**
826 * Get member StartOffline
827 * @return StartOffline
828 */
829bool Config::get_start_offline() const
830{
831 return StartOffline;
832}
e8787e2e
BS
833
834
835/**
836 * Get member ExternalLogOnlyOnce
837 * @return StartOffline
838 */
839bool Config::get_external_log_only_once() const
840{
841 return ExternalLogOnlyOnce;
842}
2e9bd873
TJ
843
844
845/**
846 * Get member DialupMode
847 * @return DIalupMode
848*/
849bool Config::get_dialup_mode() const
850{
851 return DialupMode;
852}
853
854/**
855 * Get member DialupBurstPeriodSeconds
856 * @return DialupBurstPeriodSeconds
857*/
858int Config::get_dialup_burst_period_seconds() const
859{
860 return DialupBurstPeriodSeconds;
861}
862
863/**
864 * Get member DialupSleepSeconds
865 * @return DialupSleepSeconds
866*/
867int Config::get_dialup_sleep_seconds() const
868{
869 return DialupSleepSeconds;
870}
6114d87c
TJ
871
872/**
873 * Get WAN override IP (if present)
874 * @return WanIpOverride
875*/
876std::string Config::get_wan_ip_override() const
877{
878 return WanIpOverride;
879}