Fix 'occurred' typo
[bpdyndnsd] / src / config.cpp
index 77d86dc..b326a11 100644 (file)
@@ -17,6 +17,7 @@
 #include "service_tzo.hpp"
 #include "service_zoneedit.hpp"
 #include "service_gnudip.hpp"
+#include "service_gnudip_fullhostname.hpp"
 
 #include <time.h>
 #include <iostream>
@@ -46,6 +47,7 @@ Config::Config()
     , DaemonMode(false)
     , Syslog(false)
     , EnableIPv6(false)
+    , LogPasswords(false)
     , Loglevel(0)
     , ConfigPath("/etc/bpdyndnsd")
     , WebcheckInterval(0)
@@ -55,65 +57,17 @@ Config::Config()
     , StartOffline(false)
     , WebcheckEnabled(false)
     , ExternalLogOnlyOnce(false)
+    , DialupMode(false)
+    , DialupBurstPeriodSeconds(120)
+    , DialupSleepSeconds(10 * 60)
 {
-    // Available service description config options
-    po::options_description opt_desc_service("Service description options");
-    opt_desc_service.add_options()
-        ("protocol",po::value<string>(),"The service protocol.")
-        ("server",po::value<string>(),"Servername needed for gnudip protocol.")
-        ("host",po::value<string>(),"The hostname to update.")
-        ("login",po::value<string>(),"Login name.")
-        ("password",po::value<string>(),"Corresponding password.")
-        ("update_interval",po::value<int>()->default_value(-1),"Update interval in minutes.")
-        ("max_updates_within_interval",po::value<int>()->default_value(-1),"How many updates can be made in one interval.")
-        ("dns_cache_ttl",po::value<int>()->default_value(-1),"How long a dns record is valid.")
-    ;
-
-    // Available command line only options
-    po::options_description opt_desc_cmd_only("Command line only options");
-    opt_desc_cmd_only.add_options()
-        ("help,?","Show help.")
-        ("version,v","Show version.")
-        ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
-    ;
-
-     // Available generic options. Valid on cmd or in config file.
-    po::options_description opt_desc_generic("Generic config options");
-    opt_desc_generic.add_options()
-        ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
-        ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
-        ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
-        ("enable_ipv6",po::value<bool>()->default_value(false),"Try to use IPv6.")
-        ("webcheck_enabled",po::value<bool>()->default_value(false),"Use webcheck url to determine actual IP address.")
-        ("webcheck_url",po::value<string>()->default_value(""),"Use this URL to determine IP.")
-        ("webcheck_url_alt",po::value<string>()->default_value(""),"Use this alternative URL to determine IP.")
-        ("webcheck_interval",po::value<int>()->default_value(10),"The webcheck interval in minutes.")
-        ("http_proxy",po::value<string>(),"Use this proxy for all http requests.")
-        ("http_proxy_port",po::value<int>(),"Port of the proxy.")
-        ("external_warning_log",po::value<string>()->default_value(""),"External programm to pass warning log messages to.")
-        ("external_warning_level",po::value<int>()->default_value(0),"Warning messages of which loglevel should be passed to external programm.")
-        ("external_log_only_once",po::value<bool>()->default_value(false),"Log the same external message only once until next reload or restart.")
-        ("start_offline",po::value<bool>()->default_value(false),"Start in offline mode.")
-    ;
-
-    // Define valid command line parameters
-    OptDescCmd = Options_descriptionPtr(new po::options_description("Command line options"));
-    OptDescCmd->add(opt_desc_cmd_only);
-    OptDescCmd->add(opt_desc_generic);
-    OptDescCmd->add(opt_desc_service);
-
-    // Define valid config file options
-    OptDescConfMain = Options_descriptionPtr(new po::options_description("Config file options"));
-    OptDescConfMain->add(opt_desc_generic);
-
-    // Define valid service file options
-    OptDescConfService = Options_descriptionPtr(new po::options_description("Service file options"));
-    OptDescConfService->add(opt_desc_service);
+    define_config_options();
 }
 
 
 /**
  * Constructor with Logger and Serviceholder objects. Available command line and config file options with their default values are defined here.
+ * @todo Move the program options init code to a function used by both constructors
  */
 Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
     : Log(_log)
@@ -121,6 +75,7 @@ Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
     , DaemonMode(false)
     , Syslog(false)
     , EnableIPv6(false)
+    , LogPasswords(false)
     , Loglevel(0)
     , ConfigPath("/etc/bpdyndnsd")
     , WebcheckInterval(0)
@@ -130,17 +85,38 @@ Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
     , StartOffline(false)
     , WebcheckEnabled(false)
     , ExternalLogOnlyOnce(false)
+    , DialupMode(false)
+    , DialupBurstPeriodSeconds(120)
+    , DialupSleepSeconds(10 * 60)
+{
+    define_config_options();
+}
+
+
+/**
+ * Default Destructor
+ */
+Config::~Config()
+{
+}
+
+
+/**
+* Define valid config options with default parameters.
+*/
+void Config::define_config_options()
 {
     // Available service description config options
     po::options_description opt_desc_service("Service description options");
     opt_desc_service.add_options()
         ("protocol",po::value<string>(),"The service protocol.")
-        ("server",po::value<string>(),"Servername needed for gnudip protocol.")
+        ("server",po::value<string>(),"Servername needed for gnudip/dyndns protocol.")
         ("host",po::value<string>(),"The hostname to update.")
         ("login",po::value<string>(),"Login name.")
         ("password",po::value<string>(),"Corresponding password.")
         ("update_interval",po::value<int>()->default_value(-1),"Update interval in minutes.")
         ("max_updates_within_interval",po::value<int>()->default_value(-1),"How many updates can be made in one interval.")
+        ("max_equal_updates_in_succession",po::value<int>()->default_value(-1),"How many updates with the same IP in succession should be made.")
         ("dns_cache_ttl",po::value<int>()->default_value(-1),"How long a dns record is valid.")
     ;
 
@@ -152,12 +128,13 @@ Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
         ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.")
     ;
 
-     // Available generic options. Valid on cmd or in config file.
+    // Available generic options. Valid on cmd or in config file.
     po::options_description opt_desc_generic("Generic config options");
     opt_desc_generic.add_options()
         ("daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
         ("loglevel",po::value<int>()->default_value(0),"Loglevel.")
         ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
+        ("log_passwords",po::value<bool>()->default_value(false),"Log passwords in cleartext.")
         ("enable_ipv6",po::value<bool>()->default_value(false),"Try to use IPv6.")
         ("webcheck_enabled",po::value<bool>()->default_value(false),"Use webcheck url to determine actual IP address.")
         ("webcheck_url",po::value<string>()->default_value(""),"Use this URL to determine IP.")
@@ -169,6 +146,10 @@ Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
         ("external_warning_level",po::value<int>()->default_value(0),"Warning messages of which loglevel should be passed to external programm.")
         ("external_log_only_once",po::value<bool>()->default_value(false),"Log the same external message only once until next reload or restart.")
         ("start_offline",po::value<bool>()->default_value(false),"Start in offline mode.")
+        ("dialup_mode",po::value<bool>()->default_value(false),"Enable dialup mode (sleep periods between network traffic)")
+        ("dialup_burst_period_seconds",po::value<int>()->default_value(120),"Seconds of normal operation before entering dialup mode")
+        ("dialup_sleep_seconds",po::value<int>()->default_value(10 * 60),"Seconds to sleep between network traffic")
+        ("wan_ip_override",po::value<string>(),"Manual override for automatic WAN IP detection")
     ;
 
     // Define valid command line parameters
@@ -188,14 +169,6 @@ Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder)
 
 
 /**
- * Default Destructor
- */
-Config::~Config()
-{
-}
-
-
-/**
  * Parses the command line arguments and does the needed actions.
  * @param argc Command line argument number given to main.
  * @param argv[] Pointer to command line argument array given to main.
@@ -242,15 +215,19 @@ int Config::parse_cmd_line(int argc, char *argv[])
             if ( VariablesMap.count("max_updates_within_interval") )
                 max_updates_within_interval = VariablesMap["max_updates_within_interval"].as<int>();
 
+            int max_equal_updates_in_succession = 0;
+            if ( VariablesMap.count("max_equal_updates_in_succession") )
+                max_equal_updates_in_succession = VariablesMap["max_equal_updates_in_succession"].as<int>();
+
             int dns_cache_ttl = 0;
             if ( VariablesMap.count("dns_cache_ttl") )
                 dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>();
 
-            Service::Ptr service = create_service(protocol,server,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
+            Service::Ptr service = create_service(protocol,server,host,login,password,update_interval,max_updates_within_interval,max_equal_updates_in_succession,dns_cache_ttl);
             if ( service )
             {
                 ServiceHolder->add_service(service);
-                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());
+                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());
             }
             else
             {
@@ -285,6 +262,9 @@ int Config::parse_cmd_line(int argc, char *argv[])
         if ( VariablesMap.count("syslog") )
             Syslog = VariablesMap["syslog"].as<bool>();
 
+        if ( VariablesMap.count("log_passwords") )
+            LogPasswords = VariablesMap["log_passwords"].as<bool>();
+
         if ( VariablesMap.count("enable_ipv6") )
             EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
 
@@ -324,6 +304,15 @@ int Config::parse_cmd_line(int argc, char *argv[])
         if ( VariablesMap.count("start_offline") )
             StartOffline = VariablesMap["start_offline"].as<bool>();
 
+        if ( VariablesMap.count("dialup_mode") )
+            DialupMode = VariablesMap["dialup_mode"].as<bool>();
+        if ( VariablesMap.count("dialup_burst_period_seconds") )
+            DialupBurstPeriodSeconds = VariablesMap["dialup_burst_period_seconds"].as<int>();
+        if ( VariablesMap.count("dialup_sleep_seconds") )
+            DialupSleepSeconds = VariablesMap["dialup_sleep_seconds"].as<int>();
+
+        if ( VariablesMap.count("wan_ip_override") )
+            WanIpOverride = VariablesMap["wan_ip_override"].as<string>();
     }
     catch( const po::unknown_option& e )
     {
@@ -355,7 +344,7 @@ int Config::parse_cmd_line(int argc, char *argv[])
  * @param password Password.
  * @return A pointer to the created Service object.
  */
-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)
+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)
 {
     // Test for valid hostname. Must contain 3 parts minimum.
     list<string> fqhn_parts;
@@ -369,44 +358,44 @@ Service::Ptr Config::create_service(const string &protocol, const string& server
 
     if(protocol == "dhs")
     {
-        Service::Ptr service_dhs(new ServiceDhs(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
+        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));
         return service_dhs;
     }
     else if(protocol == "ods")
     {
-        Service::Ptr service_ods(new ServiceOds(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl));
+        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));
         return service_ods;
     }
     else if(protocol == "dyndns")
     {
-        Service::Ptr service_dyndns(new ServiceDyndns(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
+        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));
         return service_dyndns;
     }
     else if(protocol == "dyns")
     {
-        Service::Ptr service_dyns(new ServiceDyns(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
+        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));
         return service_dyns;
     }
     else if(protocol == "easydns")
     {
-        Service::Ptr service_easydns(new ServiceEasydns(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
+        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));
         return service_easydns;
     }
     else if(protocol == "tzo")
     {
-        Service::Ptr service_tzo(new ServiceTzo(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
+        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));
         return service_tzo;
     }
     else if(protocol == "zoneedit")
     {
-        Service::Ptr service_zoneedit(new ServiceZoneedit(protocol,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
+        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));
         return service_zoneedit;
     }
     else if(protocol == "gnudip")
     {
         if ( !server.empty() )
         {
-            Service::Ptr service_gnudip(new ServiceGnudip(protocol,server,hostname,login,password,Log,update_interval,max_updates_within_interval,dns_cache_ttl,Proxy,ProxyPort));
+            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));
             return service_gnudip;
         }
         else
@@ -416,6 +405,20 @@ Service::Ptr Config::create_service(const string &protocol, const string& server
             return service;
         }
     }
+    else if(protocol == "gnudip-fullhostname")
+    {
+        if ( !server.empty() )
+        {
+            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));
+            return service_gnudip_fullhostname;
+        }
+        else
+        {
+            Log->print_gnudip_requires_servername();
+            Service::Ptr service;
+            return service;
+        }
+    }
     else
     {
         Log->print_unknown_protocol(protocol);
@@ -440,7 +443,7 @@ int Config::load_service_config_file(const string& full_filename)
         try
         {
             po::variables_map vm;
-            po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->OptDescConfService,true);
+            po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->OptDescConfService,false);
             po::store(parsed_service_options,vm);
             po::notify(vm);
 
@@ -466,15 +469,19 @@ int Config::load_service_config_file(const string& full_filename)
                 if ( vm.count("max_updates_within_interval") )
                     max_updates_within_interval = vm["max_updates_within_interval"].as<int>();
 
+                int max_equal_updates_in_succession = 0;
+                if ( vm.count("max_equal_updates_in_succession") )
+                    max_equal_updates_in_succession = vm["max_equal_updates_in_succession"].as<int>();
+
                 int dns_cache_ttl = 0;
                 if ( vm.count("dns_cache_ttl") )
                     dns_cache_ttl = vm["dns_cache_ttl"].as<int>();
 
-                Service::Ptr service = create_service(protocol,server,host,login,password,update_interval,max_updates_within_interval,dns_cache_ttl);
+                Service::Ptr service = create_service(protocol,server,host,login,password,update_interval,max_updates_within_interval,max_equal_updates_in_succession,dns_cache_ttl);
                 if ( service )
                 {
                     ServiceHolder->add_service(service);
-                    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());
+                    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());
                 }
                 else
                 {
@@ -533,7 +540,7 @@ int Config::load_main_config_file(const string& full_filename)
     {
         try
         {
-            po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->OptDescConfMain,true);
+            po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->OptDescConfMain,false);
             po::store(parsed_main_options,VariablesMap);
             po::notify(VariablesMap);
 
@@ -546,6 +553,9 @@ int Config::load_main_config_file(const string& full_filename)
             if ( VariablesMap.count("syslog") )
                 Syslog = VariablesMap["syslog"].as<bool>();
 
+            if ( VariablesMap.count("log_passwords") )
+                LogPasswords = VariablesMap["log_passwords"].as<bool>();
+
             if ( VariablesMap.count("enable_ipv6") )
                 EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>();
 
@@ -585,8 +595,17 @@ int Config::load_main_config_file(const string& full_filename)
             if ( VariablesMap.count("start_offline") )
                 StartOffline = VariablesMap["start_offline"].as<bool>();
 
+            if ( VariablesMap.count("dialup_mode") )
+                DialupMode = VariablesMap["dialup_mode"].as<bool>();
+            if ( VariablesMap.count("dialup_burst_period_seconds") )
+                DialupBurstPeriodSeconds = VariablesMap["dialup_burst_period_seconds"].as<int>();
+            if ( VariablesMap.count("dialup_sleep_seconds") )
+                DialupSleepSeconds = VariablesMap["dialup_sleep_seconds"].as<int>();
+
+            if ( VariablesMap.count("wan_ip_override") )
+                WanIpOverride = VariablesMap["wan_ip_override"].as<string>();
         }
-        catch( const po::unknown_option& e )      // at the moment 04-08-2009 this exception is never thrown :-(
+        catch( const po::unknown_option& e )
         {
             // unknown option in main config file detected
             main_config_file.close();
@@ -625,8 +644,12 @@ int Config::load_config_from_files()
     {
         if( fs::is_regular_file( dir_itr->status() ) )
         {
+#if BOOST_FILESYSTEM_VERSION >= 3
+            string actual_file = dir_itr->path().filename().string();
+#else
             string actual_file = dir_itr->path().filename();
-            boost::regex expr(".*\\.conf?");
+#endif
+            boost::regex expr(".*\\.conf$");
              // If it is the main config file do the following
             if ( actual_file == "bpdyndnsd.conf" )
             {
@@ -724,6 +747,16 @@ bool Config::get_syslog() const
 
 
 /**
+ * Getter for member LogPasswords.
+ * @return True if we want to log passwords in cleartext.
+ */
+bool Config::get_log_passwords() const
+{
+    return LogPasswords;
+}
+
+
+/**
  * Getter for member EnableIPv6
  * @return Wether IPv6 should be used or not.
  */
@@ -841,3 +874,40 @@ bool Config::get_external_log_only_once() const
 {
     return ExternalLogOnlyOnce;
 }
+
+
+/**
+ * Get member DialupMode
+ * @return DIalupMode
+*/
+bool Config::get_dialup_mode() const
+{
+    return DialupMode;
+}
+
+/**
+ * Get member DialupBurstPeriodSeconds
+ * @return DialupBurstPeriodSeconds
+*/
+int Config::get_dialup_burst_period_seconds() const
+{
+    return DialupBurstPeriodSeconds;
+}
+
+/**
+ * Get member DialupSleepSeconds
+ * @return DialupSleepSeconds
+*/
+int Config::get_dialup_sleep_seconds() const
+{
+    return DialupSleepSeconds;
+}
+
+/**
+ * Get WAN override IP (if present)
+ * @return WanIpOverride
+*/
+std::string Config::get_wan_ip_override() const
+{
+    return WanIpOverride;
+}