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