Commit | Line | Data |
---|---|---|
5c460c94 BS |
1 | /** @file |
2 | * @brief Config class implementation. This class represents the actual configuration. | |
3 | * | |
4 | * | |
5 | * | |
6 | * @copyright Intra2net AG | |
7 | * @license GPLv2 | |
8 | */ | |
4545a371 | 9 | |
4de6a9b8 | 10 | #include "config.hpp" |
fc87cdbb | 11 | |
4de6a9b8 BS |
12 | #include "service_dhs.hpp" |
13 | #include "service_ods.hpp" | |
14 | #include "service_dyndns.hpp" | |
15 | #include "service_dyns.hpp" | |
16 | #include "service_easydns.hpp" | |
17 | #include "service_tzo.hpp" | |
18 | #include "service_zoneedit.hpp" | |
19 | #include "service_gnudip.hpp" | |
ca28857e | 20 | #include "service_gnudip_fullhostname.hpp" |
88a594e8 BS |
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> | |
4a527a29 | 29 | #include <boost/algorithm/string.hpp> |
88a594e8 | 30 | |
e0080b78 | 31 | |
8bca3c5d BS |
32 | namespace po = boost::program_options; |
33 | namespace fs = boost::filesystem; | |
4a527a29 | 34 | namespace ba = boost::algorithm; |
8bca3c5d | 35 | |
92beaba3 BS |
36 | typedef boost::shared_ptr<boost::program_options::options_description> Options_descriptionPtr; |
37 | ||
8bca3c5d | 38 | using namespace std; |
254bbf53 | 39 | |
31af6a2e | 40 | |
5c460c94 | 41 | /** |
85a0abf9 | 42 | * Default Constructor. Available command line and config file options with their default values are defined here. |
5c460c94 | 43 | */ |
31af6a2e BS |
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) | |
6b63b758 | 56 | , StartOffline(false) |
46fffdd6 | 57 | , WebcheckEnabled(false) |
4475e30a | 58 | , ExternalLogOnlyOnce(false) |
2e9bd873 TJ |
59 | , DialupMode(false) |
60 | , DialupBurstPeriodSeconds(120) | |
61 | , DialupSleepSeconds(10 * 60) | |
31af6a2e | 62 | { |
38a04d54 | 63 | define_config_options(); |
31af6a2e BS |
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. | |
2e9bd873 | 69 | * @todo Move the program options init code to a function used by both constructors |
31af6a2e | 70 | */ |
e0080b78 | 71 | Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder) |
9a0aff44 BS |
72 | : Log(_log) |
73 | , ServiceHolder(_serviceholder) | |
74 | , DaemonMode(false) | |
85a0abf9 | 75 | , Syslog(false) |
019dc0d9 | 76 | , EnableIPv6(false) |
e0080b78 BS |
77 | , Loglevel(0) |
78 | , ConfigPath("/etc/bpdyndnsd") | |
2b0f7c11 | 79 | , WebcheckInterval(0) |
891ae3b9 | 80 | , ProxyPort(0) |
cbbdeb6c BS |
81 | , ExternalWarningLog("") |
82 | , ExternalWarningLevel(0) | |
6b63b758 | 83 | , StartOffline(false) |
46fffdd6 | 84 | , WebcheckEnabled(false) |
4475e30a | 85 | , ExternalLogOnlyOnce(false) |
2e9bd873 TJ |
86 | , DialupMode(false) |
87 | , DialupBurstPeriodSeconds(120) | |
88 | , DialupSleepSeconds(10 * 60) | |
4545a371 | 89 | { |
38a04d54 BS |
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 | { | |
3434b35f BS |
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.") | |
f04a7cb4 | 111 | ("server",po::value<string>(),"Servername needed for gnudip/dyndns protocol.") |
3434b35f BS |
112 | ("host",po::value<string>(),"The hostname to update.") |
113 | ("login",po::value<string>(),"Login name.") | |
114 | ("password",po::value<string>(),"Corresponding password.") | |
3c0cd271 BS |
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.") | |
4553e833 | 117 | ("max_equal_updates_in_succession",po::value<int>()->default_value(-1),"How many updates with the same IP in succession should be made.") |
c3dea5dc | 118 | ("dns_cache_ttl",po::value<int>()->default_value(-1),"How long a dns record is valid.") |
3434b35f BS |
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() | |
4545a371 BS |
124 | ("help,?","Show help.") |
125 | ("version,v","Show version.") | |
254bbf53 | 126 | ("config,c",po::value<string>()->default_value("/etc/bpdyndnsd"),"Set the config path.") |
4545a371 BS |
127 | ; |
128 | ||
38a04d54 | 129 | // Available generic options. Valid on cmd or in config file. |
3434b35f BS |
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.") | |
3434b35f BS |
133 | ("loglevel",po::value<int>()->default_value(0),"Loglevel.") |
134 | ("syslog",po::value<bool>()->default_value(false),"Use syslog facility.") | |
019dc0d9 | 135 | ("enable_ipv6",po::value<bool>()->default_value(false),"Try to use IPv6.") |
46fffdd6 | 136 | ("webcheck_enabled",po::value<bool>()->default_value(false),"Use webcheck url to determine actual IP address.") |
019dc0d9 BS |
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.") | |
2b0f7c11 | 139 | ("webcheck_interval",po::value<int>()->default_value(10),"The webcheck interval in minutes.") |
8a00a649 BS |
140 | ("http_proxy",po::value<string>(),"Use this proxy for all http requests.") |
141 | ("http_proxy_port",po::value<int>(),"Port of the proxy.") | |
cbbdeb6c BS |
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.") | |
e8787e2e | 144 | ("external_log_only_once",po::value<bool>()->default_value(false),"Log the same external message only once until next reload or restart.") |
6b63b758 | 145 | ("start_offline",po::value<bool>()->default_value(false),"Start in offline mode.") |
2e9bd873 TJ |
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") | |
6114d87c | 149 | ("wan_ip_override",po::value<string>(),"Manual override for automatic WAN IP detection") |
fc87cdbb | 150 | ; |
4545a371 | 151 | |
3434b35f | 152 | // Define valid command line parameters |
c3c84086 | 153 | OptDescCmd = Options_descriptionPtr(new po::options_description("Command line options")); |
025abebb BS |
154 | OptDescCmd->add(opt_desc_cmd_only); |
155 | OptDescCmd->add(opt_desc_generic); | |
156 | OptDescCmd->add(opt_desc_service); | |
3434b35f BS |
157 | |
158 | // Define valid config file options | |
c3c84086 | 159 | OptDescConfMain = Options_descriptionPtr(new po::options_description("Config file options")); |
025abebb | 160 | OptDescConfMain->add(opt_desc_generic); |
3434b35f BS |
161 | |
162 | // Define valid service file options | |
c3c84086 | 163 | OptDescConfService = Options_descriptionPtr(new po::options_description("Service file options")); |
025abebb | 164 | OptDescConfService->add(opt_desc_service); |
4545a371 BS |
165 | } |
166 | ||
fc87cdbb | 167 | |
5c460c94 | 168 | /** |
5c460c94 BS |
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. | |
3404d89f | 172 | * @return 0 if all is fine, -1 if not. |
5c460c94 | 173 | */ |
e95a6634 | 174 | int Config::parse_cmd_line(int argc, char *argv[]) |
4545a371 BS |
175 | { |
176 | try | |
177 | { | |
025abebb BS |
178 | po::store(po::parse_command_line(argc, argv, *this->OptDescCmd), VariablesMap); |
179 | po::notify(VariablesMap); | |
4545a371 | 180 | |
025abebb | 181 | if ( VariablesMap.count("help") ) |
254bbf53 | 182 | { |
025abebb | 183 | Log->print_usage(OptDescCmd); |
3404d89f | 184 | return -1; |
254bbf53 | 185 | } |
025abebb | 186 | else if ( VariablesMap.count("version") ) |
254bbf53 BS |
187 | { |
188 | Log->print_version(); | |
3404d89f | 189 | return -1; |
254bbf53 | 190 | } |
4545a371 | 191 | |
254bbf53 | 192 | // Create a service object if all needed options are set on the command line |
025abebb | 193 | if ( VariablesMap.count("protocol") && VariablesMap.count("host") && VariablesMap.count("login") && VariablesMap.count("password") ) |
4545a371 | 194 | { |
fc87cdbb | 195 | // Get the cmd parameter values for protocol host login and password |
025abebb BS |
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>(); | |
4545a371 | 200 | |
4a527a29 | 201 | protocol = ba::to_lower_copy(protocol); |
4545a371 | 202 | |
a78b44b5 BS |
203 | string server; |
204 | if ( VariablesMap.count("server") ) | |
205 | server = VariablesMap["server"].as<string>(); | |
206 | ||
3c0cd271 BS |
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 | ||
4553e833 BS |
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 | ||
c3dea5dc BS |
219 | int dns_cache_ttl = 0; |
220 | if ( VariablesMap.count("dns_cache_ttl") ) | |
221 | dns_cache_ttl = VariablesMap["dns_cache_ttl"].as<int>(); | |
222 | ||
4553e833 | 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); |
3a3f3566 | 224 | if ( service ) |
e0080b78 BS |
225 | { |
226 | ServiceHolder->add_service(service); | |
7335d7a7 | 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()); |
e0080b78 | 228 | } |
254bbf53 | 229 | else |
60657d55 BS |
230 | { |
231 | Log->print_invalid_service_config(); | |
232 | } | |
4545a371 | 233 | } |
025abebb | 234 | else if ( VariablesMap.count("protocol") || VariablesMap.count("host") || VariablesMap.count("login") || VariablesMap.count("password") ) |
4545a371 | 235 | { |
254bbf53 | 236 | Log->print_missing_cmd_service_option(); |
025abebb | 237 | Log->print_usage(OptDescCmd); |
3404d89f | 238 | return -1; |
4545a371 | 239 | } |
254bbf53 | 240 | |
025abebb | 241 | if ( VariablesMap.count("config") ) |
254bbf53 | 242 | { |
025abebb BS |
243 | fs::path full_config_path = fs::system_complete(fs::path(VariablesMap["config"].as<string>())); |
244 | ConfigPath = full_config_path.string(); | |
254bbf53 BS |
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 | |
025abebb | 248 | Log->print_error_config_path(ConfigPath); |
3404d89f | 249 | return -1; |
254bbf53 BS |
250 | } |
251 | } | |
59c8d63c | 252 | |
019dc0d9 BS |
253 | if ( VariablesMap.count("daemon_mode") ) |
254 | DaemonMode = VariablesMap["daemon_mode"].as<bool>(); | |
255 | ||
025abebb BS |
256 | if ( VariablesMap.count("loglevel") ) |
257 | Loglevel = VariablesMap["loglevel"].as<int>(); | |
59c8d63c | 258 | |
025abebb BS |
259 | if ( VariablesMap.count("syslog") ) |
260 | Syslog = VariablesMap["syslog"].as<bool>(); | |
59c8d63c | 261 | |
019dc0d9 BS |
262 | if ( VariablesMap.count("enable_ipv6") ) |
263 | EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>(); | |
264 | ||
46fffdd6 BS |
265 | if ( VariablesMap.count("webcheck_enabled") ) |
266 | WebcheckEnabled = VariablesMap["webcheck_enabled"].as<bool>(); | |
267 | ||
019dc0d9 BS |
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 | ||
2b0f7c11 BS |
274 | if ( VariablesMap.count("webcheck_interval") ) |
275 | WebcheckInterval = VariablesMap["webcheck_interval"].as<int>(); | |
276 | ||
8a00a649 BS |
277 | if ( VariablesMap.count("http_proxy") && VariablesMap.count("http_proxy_port") ) |
278 | { | |
4eb87664 | 279 | Proxy = VariablesMap["http_proxy"].as<string>(); |
4eb87664 | 280 | ProxyPort = VariablesMap["http_proxy_port"].as<int>(); |
8a00a649 BS |
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 | ||
cbbdeb6c BS |
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>(); | |
4eb87664 | 294 | |
e8787e2e | 295 | if ( VariablesMap.count("external_log_only_once") ) |
4475e30a | 296 | ExternalLogOnlyOnce = VariablesMap["external_log_only_once"].as<bool>(); |
e8787e2e | 297 | |
6b63b758 BS |
298 | if ( VariablesMap.count("start_offline") ) |
299 | StartOffline = VariablesMap["start_offline"].as<bool>(); | |
300 | ||
2e9bd873 TJ |
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>(); | |
6114d87c TJ |
307 | |
308 | if ( VariablesMap.count("wan_ip_override") ) | |
309 | WanIpOverride = VariablesMap["wan_ip_override"].as<string>(); | |
4545a371 | 310 | } |
08a5a621 | 311 | catch( const po::unknown_option& e ) |
4545a371 | 312 | { |
254bbf53 | 313 | Log->print_unknown_cmd_option(e.what()); |
025abebb | 314 | Log->print_usage(OptDescCmd); |
3404d89f | 315 | return -1; |
4545a371 | 316 | } |
08a5a621 | 317 | catch( const po::multiple_occurrences& e ) |
3c0cd271 BS |
318 | { |
319 | Log->print_multiple_cmd_option(e.what()); | |
320 | Log->print_usage(OptDescCmd); | |
321 | return -1; | |
322 | } | |
08a5a621 | 323 | catch( const po::error& e ) |
c1b8cb79 BS |
324 | { |
325 | Log->print_error_parsing_cmd(e.what()); | |
326 | Log->print_usage(OptDescCmd); | |
327 | return -1; | |
328 | } | |
4545a371 BS |
329 | return 0; |
330 | } | |
331 | ||
fc87cdbb | 332 | |
5c460c94 | 333 | /** |
019dc0d9 BS |
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. | |
0680036d BS |
339 | * @return A pointer to the created Service object. |
340 | */ | |
4553e833 | 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) |
0680036d | 342 | { |
1a00eac6 BS |
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 | { | |
60657d55 | 348 | Log->print_invalid_hostname(hostname); |
1a00eac6 BS |
349 | Service::Ptr service; |
350 | return service; | |
351 | } | |
352 | ||
0680036d BS |
353 | if(protocol == "dhs") |
354 | { | |
4553e833 | 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)); |
85a0abf9 | 356 | return service_dhs; |
0680036d BS |
357 | } |
358 | else if(protocol == "ods") | |
359 | { | |
4553e833 | 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)); |
85a0abf9 BS |
361 | return service_ods; |
362 | } | |
b6228761 BS |
363 | else if(protocol == "dyndns") |
364 | { | |
4553e833 | 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)); |
b6228761 BS |
366 | return service_dyndns; |
367 | } | |
b30f392d BS |
368 | else if(protocol == "dyns") |
369 | { | |
4553e833 | 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)); |
b30f392d BS |
371 | return service_dyns; |
372 | } | |
1a00eac6 BS |
373 | else if(protocol == "easydns") |
374 | { | |
4553e833 | 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)); |
1a00eac6 BS |
376 | return service_easydns; |
377 | } | |
b17fd691 BS |
378 | else if(protocol == "tzo") |
379 | { | |
4553e833 | 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)); |
b17fd691 BS |
381 | return service_tzo; |
382 | } | |
3f8b2557 BS |
383 | else if(protocol == "zoneedit") |
384 | { | |
4553e833 | 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)); |
3f8b2557 BS |
386 | return service_zoneedit; |
387 | } | |
a78b44b5 BS |
388 | else if(protocol == "gnudip") |
389 | { | |
a78b44b5 BS |
390 | if ( !server.empty() ) |
391 | { | |
4553e833 | 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)); |
a78b44b5 BS |
393 | return service_gnudip; |
394 | } | |
395 | else | |
396 | { | |
397 | Log->print_gnudip_requires_servername(); | |
398 | Service::Ptr service; | |
399 | return service; | |
400 | } | |
401 | } | |
ca28857e GE |
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 | } | |
85a0abf9 BS |
416 | else |
417 | { | |
254bbf53 | 418 | Log->print_unknown_protocol(protocol); |
88a594e8 | 419 | Service::Ptr service; |
83b60bd6 | 420 | return service; |
0680036d | 421 | } |
0680036d BS |
422 | } |
423 | ||
424 | ||
425 | /** | |
1cf4e2b5 BS |
426 | * Loads a service config file, invoked by load_config_from_files. |
427 | * @param full_filename Filename of the service config file to load. | |
3404d89f | 428 | * @return 0 if all is fine, -1 otherwise. |
1cf4e2b5 | 429 | */ |
e95a6634 | 430 | int Config::load_service_config_file(const string& full_filename) |
1cf4e2b5 | 431 | { |
254bbf53 BS |
432 | Log->print_load_service_conf(full_filename); |
433 | ||
1cf4e2b5 BS |
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; | |
a05aae3d | 440 | po::parsed_options parsed_service_options = po::parse_config_file(service_config_file,*this->OptDescConfService,false); |
1cf4e2b5 BS |
441 | po::store(parsed_service_options,vm); |
442 | po::notify(vm); | |
443 | ||
3434b35f | 444 | if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password")) |
1cf4e2b5 BS |
445 | { |
446 | // create the corresponding service | |
3434b35f BS |
447 | string protocol = vm["protocol"].as<string>(); |
448 | string host = vm["host"].as<string>(); | |
449 | string login = vm["login"].as<string>(); | |
c1b8cb79 | 450 | string password = vm["password"].as<string>(); |
1cf4e2b5 | 451 | |
4a527a29 | 452 | protocol = ba::to_lower_copy(protocol); |
1cf4e2b5 | 453 | |
a78b44b5 BS |
454 | string server; |
455 | if ( vm.count("server") ) | |
456 | server = vm["server"].as<string>(); | |
457 | ||
3c0cd271 | 458 | int update_interval = 0; |
8a00a649 | 459 | if ( vm.count("update_interval") ) |
a78b44b5 | 460 | update_interval = vm["update_interval"].as<int>(); |
3c0cd271 BS |
461 | |
462 | int max_updates_within_interval = 0; | |
8a00a649 | 463 | if ( vm.count("max_updates_within_interval") ) |
a78b44b5 | 464 | max_updates_within_interval = vm["max_updates_within_interval"].as<int>(); |
3c0cd271 | 465 | |
4553e833 BS |
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 | ||
c3dea5dc | 470 | int dns_cache_ttl = 0; |
8a00a649 | 471 | if ( vm.count("dns_cache_ttl") ) |
a78b44b5 | 472 | dns_cache_ttl = vm["dns_cache_ttl"].as<int>(); |
c3dea5dc | 473 | |
4553e833 | 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); |
3a3f3566 | 475 | if ( service ) |
1cf4e2b5 | 476 | { |
e0080b78 | 477 | ServiceHolder->add_service(service); |
7335d7a7 | 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()); |
1cf4e2b5 | 479 | } |
e0080b78 | 480 | else |
60657d55 BS |
481 | { |
482 | Log->print_invalid_service_config(); | |
483 | } | |
1cf4e2b5 | 484 | } |
8a00a649 BS |
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 | } | |
1cf4e2b5 | 491 | } |
08a5a621 | 492 | catch( const po::unknown_option& e ) |
1cf4e2b5 BS |
493 | { |
494 | // unknown option in config file detected | |
495 | service_config_file.close(); | |
8a00a649 BS |
496 | Log->print_unknown_service_conf_option(full_filename,e.what()); |
497 | return -1; | |
498 | } | |
08a5a621 | 499 | catch( const po::multiple_occurrences& e ) |
8a00a649 BS |
500 | { |
501 | service_config_file.close(); | |
502 | Log->print_multiple_service_conf_option(full_filename,e.what()); | |
3404d89f | 503 | return -1; |
1cf4e2b5 | 504 | } |
08a5a621 | 505 | catch( const po::error& e ) |
c1b8cb79 BS |
506 | { |
507 | service_config_file.close(); | |
508 | Log->print_error_parsing_config_file(full_filename,e.what()); | |
509 | return -1; | |
510 | } | |
1cf4e2b5 BS |
511 | service_config_file.close(); |
512 | } | |
513 | else | |
514 | { | |
254bbf53 | 515 | // error opening service config file for reading |
667c672c | 516 | Log->print_error_opening_r(full_filename); |
3404d89f | 517 | return -1; |
1cf4e2b5 BS |
518 | } |
519 | return 0; | |
520 | } | |
521 | ||
254bbf53 | 522 | |
1cf4e2b5 BS |
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 | |
3404d89f | 526 | * @return 0 if all is fine, -1 otherwise |
1cf4e2b5 | 527 | */ |
254bbf53 | 528 | int Config::load_main_config_file(const string& full_filename) |
1cf4e2b5 | 529 | { |
254bbf53 BS |
530 | Log->print_load_main_conf(full_filename); |
531 | ||
1cf4e2b5 BS |
532 | ifstream main_config_file(full_filename.c_str(),ifstream::in); |
533 | if(main_config_file.is_open()) | |
534 | { | |
535 | try | |
536 | { | |
a05aae3d | 537 | po::parsed_options parsed_main_options = po::parse_config_file(main_config_file,*this->OptDescConfMain,false); |
025abebb BS |
538 | po::store(parsed_main_options,VariablesMap); |
539 | po::notify(VariablesMap); | |
1cf4e2b5 | 540 | |
8a00a649 BS |
541 | if ( VariablesMap.count("daemon_mode") ) |
542 | DaemonMode = VariablesMap["daemon_mode"].as<bool>(); | |
019dc0d9 | 543 | |
8a00a649 BS |
544 | if ( VariablesMap.count("loglevel") ) |
545 | Loglevel = VariablesMap["loglevel"].as<int>(); | |
019dc0d9 | 546 | |
8a00a649 BS |
547 | if ( VariablesMap.count("syslog") ) |
548 | Syslog = VariablesMap["syslog"].as<bool>(); | |
019dc0d9 | 549 | |
8a00a649 BS |
550 | if ( VariablesMap.count("enable_ipv6") ) |
551 | EnableIPv6 = VariablesMap["enable_ipv6"].as<bool>(); | |
019dc0d9 | 552 | |
46fffdd6 BS |
553 | if ( VariablesMap.count("webcheck_enabled") ) |
554 | WebcheckEnabled = VariablesMap["webcheck_enabled"].as<bool>(); | |
555 | ||
8a00a649 BS |
556 | if ( VariablesMap.count("webcheck_url") ) |
557 | WebcheckIpUrl = VariablesMap["webcheck_url"].as<string>(); | |
019dc0d9 | 558 | |
8a00a649 BS |
559 | if ( VariablesMap.count("webcheck_url_alt") ) |
560 | WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as<string>(); | |
4eb87664 | 561 | |
2b0f7c11 BS |
562 | if ( VariablesMap.count("webcheck_interval") ) |
563 | WebcheckInterval = VariablesMap["webcheck_interval"].as<int>(); | |
564 | ||
8a00a649 BS |
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 | } | |
4eb87664 | 576 | |
cbbdeb6c BS |
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 | ||
e8787e2e | 583 | if ( VariablesMap.count("external_log_only_once") ) |
4475e30a | 584 | ExternalLogOnlyOnce = VariablesMap["external_log_only_once"].as<bool>(); |
e8787e2e | 585 | |
6b63b758 BS |
586 | if ( VariablesMap.count("start_offline") ) |
587 | StartOffline = VariablesMap["start_offline"].as<bool>(); | |
588 | ||
2e9bd873 TJ |
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>(); | |
6114d87c TJ |
595 | |
596 | if ( VariablesMap.count("wan_ip_override") ) | |
597 | WanIpOverride = VariablesMap["wan_ip_override"].as<string>(); | |
1cf4e2b5 | 598 | } |
a05aae3d | 599 | catch( const po::unknown_option& e ) |
1cf4e2b5 | 600 | { |
254bbf53 | 601 | // unknown option in main config file detected |
1cf4e2b5 | 602 | main_config_file.close(); |
254bbf53 | 603 | Log->print_unknown_main_conf_option(e.what()); |
3404d89f | 604 | return -1; |
1cf4e2b5 | 605 | } |
08a5a621 | 606 | catch( const po::multiple_occurrences& e ) |
8a00a649 BS |
607 | { |
608 | main_config_file.close(); | |
609 | Log->print_multiple_main_conf_option(full_filename,e.what()); | |
610 | return -1; | |
611 | } | |
1cf4e2b5 BS |
612 | main_config_file.close(); |
613 | } | |
614 | else | |
615 | { | |
254bbf53 | 616 | // error opening main config file for reading |
667c672c | 617 | Log->print_error_opening_r(full_filename); |
3404d89f | 618 | return -1; |
1cf4e2b5 BS |
619 | } |
620 | return 0; | |
621 | } | |
622 | ||
254bbf53 | 623 | |
1cf4e2b5 | 624 | /** |
5c460c94 BS |
625 | * Loads the main and the service config file and does the needed action. |
626 | * @param config_path The path to the config directory. | |
3404d89f | 627 | * @return 0 if all is fine, -1 otherwise |
5c460c94 | 628 | */ |
e95a6634 | 629 | int Config::load_config_from_files() |
4545a371 | 630 | { |
025abebb | 631 | fs::path full_config_path = fs::path(ConfigPath); |
7ec77cf1 | 632 | |
254bbf53 BS |
633 | fs::directory_iterator end_iter; |
634 | for ( fs::directory_iterator dir_itr(full_config_path) ; dir_itr != end_iter ; ++dir_itr ) | |
7ec77cf1 | 635 | { |
254bbf53 | 636 | if( fs::is_regular_file( dir_itr->status() ) ) |
7ec77cf1 | 637 | { |
f2d91b41 GE |
638 | #if BOOST_FILESYSTEM_VERSION >= 3 |
639 | string actual_file = dir_itr->path().filename().string(); | |
640 | #else | |
254bbf53 | 641 | string actual_file = dir_itr->path().filename(); |
f2d91b41 | 642 | #endif |
b4d8d055 | 643 | boost::regex expr(".*\\.conf$"); |
254bbf53 BS |
644 | // If it is the main config file do the following |
645 | if ( actual_file == "bpdyndnsd.conf" ) | |
7ec77cf1 | 646 | { |
254bbf53 BS |
647 | // Load the main config file |
648 | string full_filename = dir_itr->path().string(); | |
649 | if ( load_main_config_file(full_filename) != 0 ) | |
3404d89f | 650 | return -1; |
254bbf53 BS |
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(); | |
e95a6634 | 656 | if ( load_service_config_file(full_filename) != 0 ) |
3404d89f | 657 | return -1; |
fc87cdbb BS |
658 | } |
659 | } | |
fc87cdbb | 660 | } |
254bbf53 | 661 | // Config file successfully loaded |
025abebb | 662 | Log->print_conf_loaded(ConfigPath); |
b1be615b | 663 | return 0; |
4545a371 BS |
664 | } |
665 | ||
fc87cdbb | 666 | |
5c460c94 | 667 | /** |
025abebb | 668 | * Getter method for member OptDescCmd. |
254bbf53 | 669 | * @return options_description*. |
5c460c94 | 670 | */ |
92beaba3 | 671 | Options_descriptionPtr Config::get_opt_desc_cmd() const |
fc87cdbb | 672 | { |
025abebb | 673 | return OptDescCmd; |
4545a371 BS |
674 | } |
675 | ||
fc87cdbb | 676 | |
5c460c94 | 677 | /** |
025abebb | 678 | * Getter method for member OptDescConfMain. |
254bbf53 | 679 | * @return options_description*. |
5c460c94 | 680 | */ |
92beaba3 | 681 | Options_descriptionPtr Config::get_opt_desc_conf_main() const |
4545a371 | 682 | { |
025abebb | 683 | return OptDescConfMain; |
4545a371 | 684 | } |
38060291 | 685 | |
254bbf53 | 686 | |
38060291 | 687 | /** |
025abebb | 688 | * Getter method for member OptDescConfService. |
254bbf53 | 689 | * @return options_description*. |
38060291 | 690 | */ |
92beaba3 | 691 | Options_descriptionPtr Config::get_opt_desc_conf_service() const |
38060291 | 692 | { |
025abebb | 693 | return OptDescConfService; |
38060291 | 694 | } |
3434b35f | 695 | |
c5675c01 | 696 | |
3434b35f BS |
697 | /** |
698 | * Getter for member Loglevel. | |
699 | * @return Member Loglevel. | |
700 | */ | |
b38684ce | 701 | int Config::get_loglevel() const |
3434b35f BS |
702 | { |
703 | return Loglevel; | |
704 | } | |
388f4ab0 | 705 | |
c5675c01 | 706 | |
388f4ab0 | 707 | /** |
025abebb | 708 | * Getter for member DaemonMode. |
388f4ab0 BS |
709 | * @return TRUE if enabled, FALSE if disabled. |
710 | */ | |
b38684ce | 711 | bool Config::get_daemon_mode() const |
388f4ab0 | 712 | { |
025abebb | 713 | return DaemonMode; |
388f4ab0 | 714 | } |
c5675c01 BS |
715 | |
716 | ||
717 | /** | |
8bca3c5d BS |
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 | { | |
025abebb | 723 | VariablesMap.clear(); |
8bca3c5d BS |
724 | |
725 | po::variables_map _variables_map; | |
025abebb | 726 | VariablesMap = _variables_map; |
8bca3c5d BS |
727 | } |
728 | ||
729 | ||
730 | /** | |
731 | * Getter for member Syslog. | |
732 | * @return True if logging through syslog is enabled, false otherwise. | |
733 | */ | |
b38684ce | 734 | bool Config::get_syslog() const |
8bca3c5d BS |
735 | { |
736 | return Syslog; | |
737 | } | |
019dc0d9 BS |
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 | /** | |
46fffdd6 BS |
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 | /** | |
019dc0d9 BS |
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 | } | |
4eb87664 BS |
788 | |
789 | ||
790 | /** | |
2b0f7c11 BS |
791 | * Get member WebcheckInterval |
792 | * @return WebcheckInterval | |
793 | */ | |
794 | int Config::get_webcheck_interval() const | |
795 | { | |
796 | return WebcheckInterval; | |
797 | } | |
798 | ||
799 | ||
800 | /** | |
4eb87664 BS |
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 | } | |
cbbdeb6c BS |
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 | ||
6b63b758 BS |
839 | |
840 | /** | |
841 | * Get member StartOffline | |
842 | * @return StartOffline | |
843 | */ | |
844 | bool Config::get_start_offline() const | |
845 | { | |
846 | return StartOffline; | |
847 | } | |
e8787e2e BS |
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 | } | |
2e9bd873 TJ |
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 | } | |
6114d87c TJ |
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 | } |