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