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