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