Enhanced update logic to respect not activated hostnames. If a hostname could not...
[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
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>
28 #include <boost/algorithm/string.hpp>
29
30
31 namespace po = boost::program_options;
32 namespace fs = boost::filesystem;
33 namespace ba = boost::algorithm;
34
35 typedef boost::shared_ptr<boost::program_options::options_description> Options_descriptionPtr;
36
37 using namespace std;
38
39
40 /**
41  * Default Constructor. Available command line and config file options with their default values are defined here.
42  */
43 Config::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)
55     , StartOffline(false)
56     , WebcheckEnabled(false)
57     , ExternalLogOnlyOnce(false)
58     , DialupMode(false)
59     , DialupBurstPeriodSeconds(120)
60     , DialupSleepSeconds(10 * 60)
61 {
62     define_config_options();
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.
68  * @todo Move the program options init code to a function used by both constructors
69  */
70 Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
71     : Log(_log)
72     , ServiceHolder(_serviceholder)
73     , DaemonMode(false)
74     , Syslog(false)
75     , EnableIPv6(false)
76     , Loglevel(0)
77     , ConfigPath("/etc/bpdyndnsd")
78     , WebcheckInterval(0)
79     , ProxyPort(0)
80     , ExternalWarningLog("")
81     , ExternalWarningLevel(0)
82     , StartOffline(false)
83     , WebcheckEnabled(false)
84     , ExternalLogOnlyOnce(false)
85     , DialupMode(false)
86     , DialupBurstPeriodSeconds(120)
87     , DialupSleepSeconds(10 * 60)
88 {
89     define_config_options();
90 }
91
92
93 /**
94  * Default Destructor
95  */
96 Config::~Config()
97 {
98 }
99
100
101 /**
102 * Define valid config options with default parameters.
103 */
104 void Config::define_config_options()
105 {
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.")
110         ("server",po::value<string>(),"Servername needed for gnudip/dyndns protocol.")
111         ("host",po::value<string>(),"The hostname to update.")
112         ("login",po::value<string>(),"Login name.")
113         ("password",po::value<string>(),"Corresponding password.")
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.")
116         ("max_equal_updates_in_succession",po::value<int>()->default_value(-1),"How many updates with the same IP in succession should be made.")
117         ("dns_cache_ttl",po::value<int>()->default_value(-1),"How long a dns record is valid.")
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()
123         ("help,?","Show help.")
124         ("version,v","Show version.")
125         ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
126     ;
127
128     // Available generic options. Valid on cmd or in config file.
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.")
132         ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
133         ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
134         ("enable_ipv6",po::value<bool>()->default_value(false),"Try to use IPv6.")
135         ("webcheck_enabled",po::value<bool>()->default_value(false),"Use webcheck url to determine actual IP address.")
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.")
138         ("webcheck_interval",po::value<int>()->default_value(10),"The webcheck interval in minutes.")
139         ("http_proxy",po::value<string>(),"Use this proxy for all http requests.")
140         ("http_proxy_port",po::value<int>(),"Port of the proxy.")
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.")
143         ("external_log_only_once",po::value<bool>()->default_value(false),"Log the same external message only once until next reload or restart.")
144         ("start_offline",po::value<bool>()->default_value(false),"Start in offline mode.")
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")
148         ("wan_ip_override",po::value<string>(),"Manual override for automatic WAN IP detection")
149     ;
150
151     // Define valid command line parameters
152     OptDescCmd = Options_descriptionPtr(new po::options_description("Command line options"));
153     OptDescCmd->add(opt_desc_cmd_only);
154     OptDescCmd->add(opt_desc_generic);
155     OptDescCmd->add(opt_desc_service);
156
157     // Define valid config file options
158     OptDescConfMain = Options_descriptionPtr(new po::options_description("Config file options"));
159     OptDescConfMain->add(opt_desc_generic);
160
161     // Define valid service file options
162     OptDescConfService = Options_descriptionPtr(new po::options_description("Service file options"));
163     OptDescConfService->add(opt_desc_service);
164 }
165
166
167 /**
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.
171  * @return 0 if all is fine, -1 if not.
172  */
173 int Config::parse_cmd_line(int argc, char *argv[])
174 {
175     try
176     {
177         po::store(po::parse_command_line(argc, argv, *this->OptDescCmd), VariablesMap);
178         po::notify(VariablesMap);
179
180         if ( VariablesMap.count("help") )
181         {
182             Log->print_usage(OptDescCmd);
183             return -1;
184         }
185         else if ( VariablesMap.count("version") )
186         {
187             Log->print_version();
188             return -1;
189         }
190
191         // Create a service object if all needed options are set on the command line
192         if ( VariablesMap.count("protocol") && VariablesMap.count("host") && VariablesMap.count("login") && VariablesMap.count("password") )
193         {
194             // Get the cmd parameter values for protocol host login and password
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>();
199
200             protocol = ba::to_lower_copy(protocol);
201
202             string server;
203             if ( VariablesMap.count("server") )
204                 server = VariablesMap["server"].as<string>();
205
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
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
218             int dns_cache_ttl = 0;
219             if ( VariablesMap.count("dns_cache_ttl") )
220                 dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
221
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);
223             if ( service )
224             {
225                 ServiceHolder->add_service(service);
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());
227             }
228             else
229             {
230                 Log->print_invalid_service_config();
231             }
232         }
233         else if ( VariablesMap.count("protocol") || VariablesMap.count("host") || VariablesMap.count("login") || VariablesMap.count("password") )
234         {
235             Log->print_missing_cmd_service_option();
236             Log->print_usage(OptDescCmd);
237             return -1;
238         }
239
240         if ( VariablesMap.count("config") )
241         {
242             fs::path full_config_path = fs::system_complete(fs::path(VariablesMap["config"].as<string>()));
243             ConfigPath = full_config_path.string();
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
247                 Log->print_error_config_path(ConfigPath);
248                 return -1;
249             }
250         }
251
252         if ( VariablesMap.count("daemon_mode") )
253             DaemonMode = VariablesMap["daemon_mode"].as<bool>();
254
255         if ( VariablesMap.count("loglevel") )
256             Loglevel = VariablesMap["loglevel"].as<int>();
257
258         if ( VariablesMap.count("syslog") )
259             Syslog = VariablesMap["syslog"].as<bool>();
260
261         if ( VariablesMap.count("enable_ipv6") )
262             EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
263
264         if ( VariablesMap.count("webcheck_enabled") )
265             WebcheckEnabled = VariablesMap["webcheck_enabled"].as<bool>();
266
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
273         if ( VariablesMap.count("webcheck_interval") )
274             WebcheckInterval = VariablesMap["webcheck_interval"].as<int>();
275
276         if ( VariablesMap.count("http_proxy") && VariablesMap.count("http_proxy_port") )
277         {
278             Proxy = VariablesMap["http_proxy"].as<string>();
279             ProxyPort = VariablesMap["http_proxy_port"].as<int>();
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
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>();
293
294         if ( VariablesMap.count("external_log_only_once") )
295             ExternalLogOnlyOnce = VariablesMap["external_log_only_once"].as<bool>();
296
297         if ( VariablesMap.count("start_offline") )
298             StartOffline = VariablesMap["start_offline"].as<bool>();
299
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>();
306
307         if ( VariablesMap.count("wan_ip_override") )
308             WanIpOverride = VariablesMap["wan_ip_override"].as<string>();
309     }
310     catch( const po::unknown_option& e )
311     {
312         Log->print_unknown_cmd_option(e.what());
313         Log->print_usage(OptDescCmd);
314         return -1;
315     }
316     catch( const po::multiple_occurrences& e )
317     {
318         Log->print_multiple_cmd_option(e.what());
319         Log->print_usage(OptDescCmd);
320         return -1;
321     }
322     catch( const po::error& e )
323     {
324         Log->print_error_parsing_cmd(e.what());
325         Log->print_usage(OptDescCmd);
326         return -1;
327     }
328     return 0;
329 }
330
331
332 /**
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.
338  * @return A pointer to the created Service object.
339  */
340 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)
341 {
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     {
347         Log->print_invalid_hostname(hostname);
348         Service::Ptr service;
349         return service;
350     }
351
352     if(protocol == "dhs")
353     {
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));
355         return service_dhs;
356     }
357     else if(protocol == "ods")
358     {
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));
360         return service_ods;
361     }
362     else if(protocol == "dyndns")
363     {
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));
365         return service_dyndns;
366     }
367     else if(protocol == "dyns")
368     {
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));
370         return service_dyns;
371     }
372     else if(protocol == "easydns")
373     {
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));
375         return service_easydns;
376     }
377     else if(protocol == "tzo")
378     {
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));
380         return service_tzo;
381     }
382     else if(protocol == "zoneedit")
383     {
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));
385         return service_zoneedit;
386     }
387     else if(protocol == "gnudip")
388     {
389         if ( !server.empty() )
390         {
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));
392             return service_gnudip;
393         }
394         else
395         {
396             Log->print_gnudip_requires_servername();
397             Service::Ptr service;
398             return service;
399         }
400     }
401     else
402     {
403         Log->print_unknown_protocol(protocol);
404         Service::Ptr service;
405         return service;
406     }
407 }
408
409
410 /**
411  * Loads a service config file, invoked by load_config_from_files.
412  * @param full_filename Filename of the service config file to load.
413  * @return 0 if all is fine, -1 otherwise.
414  */
415 int Config::load_service_config_file(const string& full_filename)
416 {
417     Log->print_load_service_conf(full_filename);
418
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;
425             po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->OptDescConfService,false);
426             po::store(parsed_service_options,vm);
427             po::notify(vm);
428
429             if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
430             {
431                 // create the corresponding service
432                 string protocol = vm["protocol"].as<string>();
433                 string host = vm["host"].as<string>();
434                 string login = vm["login"].as<string>();
435                     string password = vm["password"].as<string>();
436
437                 protocol = ba::to_lower_copy(protocol);
438
439                 string server;
440                 if ( vm.count("server") )
441                     server = vm["server"].as<string>();
442
443                 int update_interval = 0;
444                 if ( vm.count("update_interval") )
445                     update_interval = vm["update_interval"].as<int>();
446
447                 int max_updates_within_interval = 0;
448                 if ( vm.count("max_updates_within_interval") )
449                     max_updates_within_interval = vm["max_updates_within_interval"].as<int>();
450
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
455                 int dns_cache_ttl = 0;
456                 if ( vm.count("dns_cache_ttl") )
457                     dns_cache_ttl = vm["dns_cache_ttl"].as<int>();
458
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);
460                 if ( service )
461                 {
462                     ServiceHolder->add_service(service);
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());
464                 }
465                 else
466                 {
467                     Log->print_invalid_service_config();
468                 }
469             }
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             }
476         }
477         catch( const po::unknown_option& e )
478         {
479             // unknown option in config file detected
480             service_config_file.close();
481             Log->print_unknown_service_conf_option(full_filename,e.what());
482             return -1;
483         }
484         catch( const po::multiple_occurrences& e )
485         {
486             service_config_file.close();
487             Log->print_multiple_service_conf_option(full_filename,e.what());
488             return -1;
489         }
490         catch( const po::error& e )
491         {
492             service_config_file.close();
493             Log->print_error_parsing_config_file(full_filename,e.what());
494             return -1;
495         }
496         service_config_file.close();
497     }
498     else
499     {
500         // error opening service config file for reading
501         Log->print_error_opening_r(full_filename);
502         return -1;
503     }
504     return 0;
505 }
506
507
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
511  * @return 0 if all is fine, -1 otherwise
512  */
513 int Config::load_main_config_file(const string& full_filename)
514 {
515     Log->print_load_main_conf(full_filename);
516
517     ifstream main_config_file(full_filename.c_str(),ifstream::in);
518     if(main_config_file.is_open())
519     {
520         try
521         {
522             po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->OptDescConfMain,false);
523             po::store(parsed_main_options,VariablesMap);
524             po::notify(VariablesMap);
525
526             if ( VariablesMap.count("daemon_mode") )
527                 DaemonMode = VariablesMap["daemon_mode"].as<bool>();
528
529             if ( VariablesMap.count("loglevel") )
530                 Loglevel = VariablesMap["loglevel"].as<int>();
531
532             if ( VariablesMap.count("syslog") )
533                 Syslog = VariablesMap["syslog"].as<bool>();
534
535             if ( VariablesMap.count("enable_ipv6") )
536                 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
537
538             if ( VariablesMap.count("webcheck_enabled") )
539                 WebcheckEnabled = VariablesMap["webcheck_enabled"].as<bool>();
540
541             if ( VariablesMap.count("webcheck_url") )
542                 WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>();
543
544             if ( VariablesMap.count("webcheck_url_alt") )
545                 WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>();
546
547             if ( VariablesMap.count("webcheck_interval") )
548                 WebcheckInterval = VariablesMap["webcheck_interval"].as<int>();
549
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             }
561
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
568             if ( VariablesMap.count("external_log_only_once") )
569                 ExternalLogOnlyOnce = VariablesMap["external_log_only_once"].as<bool>();
570
571             if ( VariablesMap.count("start_offline") )
572                 StartOffline = VariablesMap["start_offline"].as<bool>();
573
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>();
580
581             if ( VariablesMap.count("wan_ip_override") )
582                 WanIpOverride = VariablesMap["wan_ip_override"].as<string>();
583         }
584         catch( const po::unknown_option& e )
585         {
586             // unknown option in main config file detected
587             main_config_file.close();
588             Log->print_unknown_main_conf_option(e.what());
589             return -1;
590         }
591         catch( const po::multiple_occurrences& e )
592         {
593             main_config_file.close();
594             Log->print_multiple_main_conf_option(full_filename,e.what());
595             return -1;
596         }
597         main_config_file.close();
598     }
599     else
600     {
601         // error opening main config file for reading
602         Log->print_error_opening_r(full_filename);
603         return -1;
604     }
605     return 0;
606 }
607
608
609 /**
610  * Loads the main and the service config file and does the needed action.
611  * @param config_path The path to the config directory.
612  * @return 0 if all is fine, -1 otherwise
613  */
614 int Config::load_config_from_files()
615 {
616     fs::path full_config_path = fs::path(ConfigPath);
617
618     fs::directory_iterator end_iter;
619     for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr )
620     {
621         if( fs::is_regular_file( dir_itr->status() ) )
622         {
623             string actual_file = dir_itr->path().filename();
624             boost::regex expr(".*\\.conf$");
625              // If it is the main config file do the following
626             if ( actual_file == "bpdyndnsd.conf" )
627             {
628                 // Load the main config file
629                 string full_filename = dir_itr->path().string();
630                 if ( load_main_config_file(full_filename) != 0 )
631                     return -1;
632             }
633             // If it is a service definition file *.conf, parse it and generate the corresponding service
634             else if ( boost::regex_search( actual_file,expr ) )
635             {
636                 string full_filename = dir_itr->path().string();
637                 if ( load_service_config_file(full_filename) != 0 )
638                     return -1;
639             }
640         }
641     }
642     // Config file successfully loaded
643     Log->print_conf_loaded(ConfigPath);
644     return 0;
645 }
646
647
648 /**
649  * Getter method for member OptDescCmd.
650  * @return options_description*.
651  */
652 Options_descriptionPtr Config::get_opt_desc_cmd() const
653 {
654     return OptDescCmd;
655 }
656
657
658 /**
659  * Getter method for member OptDescConfMain.
660  * @return options_description*.
661  */
662 Options_descriptionPtr Config::get_opt_desc_conf_main() const
663 {
664     return OptDescConfMain;
665 }
666
667
668 /**
669  * Getter method for member OptDescConfService.
670  * @return options_description*.
671  */
672 Options_descriptionPtr Config::get_opt_desc_conf_service() const
673 {
674     return OptDescConfService;
675 }
676
677
678 /**
679  * Getter for member Loglevel.
680  * @return Member Loglevel.
681  */
682 int Config::get_loglevel() const
683 {
684     return Loglevel;
685 }
686
687
688 /**
689  * Getter for member DaemonMode.
690  * @return TRUE if enabled, FALSE if disabled.
691  */
692 bool Config::get_daemon_mode() const
693 {
694     return DaemonMode;
695 }
696
697
698 /**
699  * Deletes the map with the previously parsed options.
700  * This is needed in case we reload the config and don't want the old cmd options to overwrite new config file options.
701  */
702 void Config::delete_variables_map()
703 {
704     VariablesMap.clear();
705
706     po::variables_map _variables_map;
707     VariablesMap = _variables_map;
708 }
709
710
711 /**
712  * Getter for member Syslog.
713  * @return True if logging through syslog is enabled, false otherwise.
714  */
715 bool Config::get_syslog() const
716 {
717     return Syslog;
718 }
719
720
721 /**
722  * Getter for member EnableIPv6
723  * @return Wether IPv6 should be used or not.
724  */
725 bool Config::get_enable_ipv6() const
726 {
727     return EnableIPv6;
728 }
729
730
731 /**
732  * Getter for member WebcheckEnabled
733  * @return Is webcheck enabled by default.
734  */
735 bool Config::get_webcheck_enabled() const
736 {
737     return WebcheckEnabled;
738 }
739
740
741 /**
742  * Setter for member WebcheckEnabled
743  * @return Is webcheck enabled by default.
744  */
745 void Config::set_webcheck_enabled( bool webcheck_enabled )
746 {
747     WebcheckEnabled = webcheck_enabled;
748 }
749
750
751 /**
752  * Getter for member WebcheckIpUrl
753  * @return The primary IP Webcheck URL
754  */
755 string Config::get_webcheck_ip_url() const
756 {
757     return WebcheckIpUrl;
758 }
759
760
761 /**
762  * Getter for member WebcheckIpUrlAlt
763  * @return The alternative IP Webcheck URL
764  */
765 string Config::get_webcheck_ip_url_alt() const
766 {
767     return WebcheckIpUrlAlt;
768 }
769
770
771 /**
772  * Get member WebcheckInterval
773  * @return WebcheckInterval
774  */
775 int Config::get_webcheck_interval() const
776 {
777     return WebcheckInterval;
778 }
779
780
781 /**
782  * Get member Proxy
783  * @return Proxy
784  */
785 string Config::get_proxy() const
786 {
787     return Proxy;
788 }
789
790
791 /**
792  * Get member ProxyPort
793  * @return ProxyPort
794  */
795 int Config::get_proxy_port() const
796 {
797     return ProxyPort;
798 }
799
800
801 /**
802  * Get member ExternalWarningLog
803  * @return ExternalWarningLog
804  */
805 string Config::get_external_warning_log() const
806 {
807     return ExternalWarningLog;
808 }
809
810
811 /**
812  * Get member ExternalWarningLevel
813  * @return ExternalWaringLevel
814  */
815 int Config::get_external_warning_level() const
816 {
817     return ExternalWarningLevel;
818 }
819
820
821 /**
822  * Get member StartOffline
823  * @return StartOffline
824  */
825 bool Config::get_start_offline() const
826 {
827     return StartOffline;
828 }
829
830
831 /**
832  * Get member ExternalLogOnlyOnce
833  * @return StartOffline
834  */
835 bool Config::get_external_log_only_once() const
836 {
837     return ExternalLogOnlyOnce;
838 }
839
840
841 /**
842  * Get member DialupMode
843  * @return DIalupMode
844 */
845 bool Config::get_dialup_mode() const
846 {
847     return DialupMode;
848 }
849
850 /**
851  * Get member DialupBurstPeriodSeconds
852  * @return DialupBurstPeriodSeconds
853 */
854 int Config::get_dialup_burst_period_seconds() const
855 {
856     return DialupBurstPeriodSeconds;
857 }
858
859 /**
860  * Get member DialupSleepSeconds
861  * @return DialupSleepSeconds
862 */
863 int Config::get_dialup_sleep_seconds() const
864 {
865     return DialupSleepSeconds;
866 }
867
868 /**
869  * Get WAN override IP (if present)
870  * @return WanIpOverride
871 */
872 std::string Config::get_wan_ip_override() const
873 {
874     return WanIpOverride;
875 }