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