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