Commit | Line | Data |
---|---|---|
a78b44b5 BS |
1 | /** @file |
2 | * @brief GNUDIP Service class implementation. This class represents the GNUDIP service. | |
3 | * | |
4 | * | |
5 | * | |
6 | * @copyright Intra2net AG | |
7 | * @license GPLv2 | |
8 | */ | |
9 | ||
4de6a9b8 BS |
10 | #include "service_gnudip.hpp" |
11 | #include "util.hpp" | |
a78b44b5 BS |
12 | |
13 | #include <time.h> | |
14 | #include <boost/foreach.hpp> | |
15 | #include <boost/regex.hpp> | |
16 | ||
17 | using namespace std; | |
18 | ||
19 | ||
20 | /** | |
21 | * Default Constructor, needed for object serialization. | |
22 | */ | |
23 | ServiceGnudip::ServiceGnudip() | |
24 | { | |
25 | } | |
26 | ||
27 | ||
28 | /** | |
29 | * Constructor. | |
30 | * @param _hostname The hostname to update | |
31 | * @param _login The login name. | |
32 | * @param _password The corresponding password. | |
33 | */ | |
34 | ServiceGnudip::ServiceGnudip(const string& _protocol, const string& _gnudip_server, const string& _hostname, const string& _login, const string& _password, const Logger::Ptr& _logger, const int _update_interval, const int _max_updates_within_interval, const int _dns_cache_ttl, const string& _proxy, const int _proxy_port) | |
35 | : GnudipServer(_gnudip_server) | |
36 | { | |
37 | if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config) | |
a01ce6d7 | 38 | set_update_interval(15); // use default protocol value |
a78b44b5 BS |
39 | else |
40 | set_update_interval(_update_interval); | |
41 | ||
42 | if ( _max_updates_within_interval == -1 ) | |
a01ce6d7 | 43 | set_max_updates_within_interval(3); |
a78b44b5 BS |
44 | else |
45 | set_max_updates_within_interval(_max_updates_within_interval); | |
46 | ||
47 | if ( _dns_cache_ttl == -1 ) | |
48 | set_dns_cache_ttl(60); | |
49 | else | |
50 | set_dns_cache_ttl(_dns_cache_ttl); | |
51 | ||
52 | set_protocol(_protocol); | |
53 | set_hostname(_hostname); | |
54 | set_login(_login); | |
55 | set_password(_password); | |
56 | set_logger(_logger); | |
57 | ||
58 | // create http helper class | |
ce70569b | 59 | HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password)); |
a78b44b5 | 60 | |
62df5f33 | 61 | BaseUrl = assemble_base_url(_gnudip_server); |
a78b44b5 BS |
62 | } |
63 | ||
64 | ||
65 | /** | |
66 | * Default destructor | |
67 | */ | |
68 | ServiceGnudip::~ServiceGnudip() | |
69 | { | |
70 | } | |
71 | ||
72 | ||
73 | /** | |
74 | * Assemble the dyndns update url from the given fqhn | |
62df5f33 | 75 | * @param gnudip_server The gnudip update server. |
a78b44b5 BS |
76 | * @return The assembled update url without IP. |
77 | */ | |
62df5f33 | 78 | string ServiceGnudip::assemble_base_url(const string& gnudip_server) const |
a78b44b5 BS |
79 | { |
80 | string base_url; | |
81 | ||
82 | base_url = "http://"; | |
83 | base_url.append(gnudip_server); | |
84 | base_url.append("/gnudip/cgi-bin/gdipupdt.cgi"); | |
85 | ||
86 | return base_url; | |
87 | } | |
88 | ||
89 | ||
90 | /** | |
91 | * Parses the data received from the initial request, which should contain salt, time and sign. | |
92 | * @param curl_data The complete received curl data. | |
93 | * @return A map with salt, time and sign or an empty map. | |
94 | */ | |
95 | map<string,string> ServiceGnudip::parse_initial_request(const string& curl_data) const | |
96 | { | |
97 | map<string,string> response; | |
98 | ||
99 | // regex for salt | |
3208f713 | 100 | boost::regex expr_salt("<meta name=\"salt\" content=\"([^\"]*)\""); |
a78b44b5 | 101 | // regex for time |
3208f713 | 102 | boost::regex expr_time("<meta name=\"time\" content=\"([^\"]*)\""); |
a78b44b5 | 103 | // regex for sign |
3208f713 | 104 | boost::regex expr_sign("<meta name=\"sign\" content=\"([^\"]*)\""); |
a78b44b5 BS |
105 | |
106 | boost::smatch matches; | |
107 | ||
108 | // Get the salt out of received http data | |
109 | if ( boost::regex_search(curl_data,matches,expr_salt) ) | |
110 | { | |
557b6f56 | 111 | response.insert(pair<string,string>("salt",matches[1].str())); /*lint !e534 */ |
62df5f33 | 112 | get_logger()->print_regex_match(expr_salt.str(),matches[1].str()); |
a78b44b5 BS |
113 | } |
114 | else | |
115 | { | |
116 | get_logger()->print_no_regex_match(expr_salt.str(),curl_data); | |
117 | response.clear(); | |
118 | return response; | |
119 | } | |
120 | ||
121 | // Get the time out of received http data | |
122 | if ( boost::regex_search(curl_data,matches,expr_time) ) | |
123 | { | |
557b6f56 | 124 | response.insert(pair<string,string>("time",matches[1].str())); /*lint !e534 */ |
62df5f33 | 125 | get_logger()->print_regex_match(expr_salt.str(),matches[1].str()); |
a78b44b5 BS |
126 | } |
127 | else | |
128 | { | |
129 | get_logger()->print_no_regex_match(expr_salt.str(),curl_data); | |
130 | response.clear(); | |
131 | return response; | |
132 | } | |
133 | ||
134 | // Get the sign out of received http data | |
135 | if ( boost::regex_search(curl_data,matches,expr_sign) ) | |
136 | { | |
557b6f56 | 137 | response.insert(pair<string,string>("sign",matches[1].str())); /*lint !e534 */ |
62df5f33 | 138 | get_logger()->print_regex_match(expr_salt.str(),matches[1].str()); |
a78b44b5 BS |
139 | } |
140 | else | |
141 | { | |
142 | get_logger()->print_no_regex_match(expr_salt.str(),curl_data); | |
143 | response.clear(); | |
144 | return response; | |
145 | } | |
146 | ||
147 | return response; | |
148 | } | |
149 | ||
150 | ||
151 | /** | |
3208f713 BS |
152 | * Parses the data received from the update request, which should contain the return code. |
153 | * @param curl_data The complete received curl data. | |
154 | * @return A string containing the return code of the update request. | |
155 | */ | |
156 | string ServiceGnudip::parse_return_code(const string& curl_data) const | |
157 | { | |
158 | string return_code; | |
159 | ||
160 | // regex for return code | |
161 | boost::regex expr_retc("<meta name=\"retc\" content=\"([^\"]*)\""); | |
162 | boost::smatch matches; | |
163 | ||
164 | // Get the return code out of received http data | |
165 | if ( boost::regex_search(curl_data,matches,expr_retc) ) | |
166 | { | |
167 | return_code = matches[1].str(); | |
168 | get_logger()->print_regex_match(expr_retc.str(),matches[1].str()); | |
169 | } | |
170 | else | |
171 | { | |
172 | get_logger()->print_no_regex_match(expr_retc.str(),curl_data); | |
173 | return return_code; | |
174 | } | |
175 | ||
176 | return return_code; | |
177 | } | |
178 | ||
179 | ||
180 | /** | |
a78b44b5 BS |
181 | * Get the assembled update url. |
182 | * @param salt Salt from the initial request | |
183 | * @param time Time from the initial request | |
184 | * @param sign Sign from the initial request | |
185 | * @param secret Computed md5 secret in HEX | |
186 | * @param ip IP to update | |
187 | * @return The assembled update url. | |
188 | */ | |
62df5f33 | 189 | string ServiceGnudip::assemble_update_url(const string& salt, const string& curr_time, const string& sign, const string& secret, const string& ip) const |
a78b44b5 BS |
190 | { |
191 | string url = BaseUrl; | |
192 | ||
193 | url.append("?salt="); | |
194 | url.append(salt); | |
195 | url.append("&time="); | |
62df5f33 | 196 | url.append(curr_time); |
a78b44b5 BS |
197 | url.append("&sign="); |
198 | url.append(sign); | |
199 | url.append("&user="); | |
200 | url.append(get_login()); | |
201 | url.append("&domn="); | |
3208f713 BS |
202 | string fqhn = get_hostname(); |
203 | string domain = fqhn.substr(fqhn.find('.')+1); | |
204 | url.append(domain); | |
a78b44b5 BS |
205 | url.append("&pass="); |
206 | url.append(secret); | |
207 | url.append("&reqc=0&addr="); | |
208 | url.append(ip); | |
209 | ||
210 | return url; | |
211 | } | |
212 | ||
213 | ||
214 | /** | |
215 | * Performs the Service update. | |
216 | * @param ip IP Address to set. | |
217 | * @return 0 if all is fine, -1 otherwise. | |
218 | */ | |
219 | int ServiceGnudip::perform_update(const std::string& ip) | |
220 | { | |
08a5a621 | 221 | if ( HTTPHelp->is_initialized() ) |
a78b44b5 | 222 | { |
31af6a2e BS |
223 | // initial request |
224 | long http_status_code = HTTPHelp->http_get(BaseUrl); | |
a78b44b5 | 225 | |
31af6a2e | 226 | get_logger()->print_http_status_code(BaseUrl,http_status_code); |
a78b44b5 | 227 | |
31af6a2e | 228 | if ( http_status_code == 200 ) |
a78b44b5 | 229 | { |
31af6a2e BS |
230 | // Get the received http data which should contain the salt, time and sign |
231 | string curl_data = HTTPHelp->get_curl_data(); | |
a78b44b5 | 232 | |
31af6a2e BS |
233 | // Parse salt, time and sign out of the received data |
234 | map<string,string> salt_time_sign = parse_initial_request(curl_data); | |
a78b44b5 | 235 | |
31af6a2e BS |
236 | if ( salt_time_sign.empty() ) |
237 | { | |
238 | get_logger()->print_could_not_parse_received_data(curl_data); | |
239 | return -1; | |
240 | } | |
a78b44b5 | 241 | |
31af6a2e | 242 | // at this point we have salt, time and sign parsed successfully |
62df5f33 | 243 | string salt, sign_time, sign; |
a78b44b5 | 244 | |
31af6a2e BS |
245 | map<string,string>::iterator iter = salt_time_sign.find("salt"); |
246 | if ( iter != salt_time_sign.end() ) | |
247 | salt = iter->second; | |
a78b44b5 | 248 | |
31af6a2e BS |
249 | iter = salt_time_sign.find("time"); |
250 | if ( iter != salt_time_sign.end() ) | |
62df5f33 | 251 | sign_time = iter->second; |
a78b44b5 | 252 | |
31af6a2e BS |
253 | iter = salt_time_sign.find("sign"); |
254 | if ( iter != salt_time_sign.end() ) | |
255 | sign = iter->second; | |
a78b44b5 | 256 | |
62df5f33 | 257 | if ( salt.empty() || sign_time.empty() || sign.empty() ) |
31af6a2e | 258 | get_logger()->print_could_not_get_initial_gnudip_data(); |
a78b44b5 | 259 | |
31af6a2e BS |
260 | // compute md5 sum from users password and get the HEX representation |
261 | string pw_md5_hex; | |
262 | try | |
263 | { | |
264 | pw_md5_hex = Util::compute_md5_digest(get_password()); | |
265 | } | |
c730deea | 266 | catch ( exception& e ) |
31af6a2e BS |
267 | { |
268 | get_logger()->print_exception_md5_sum(e.what()); | |
269 | return -1; | |
270 | } | |
c730deea BS |
271 | catch ( ... ) |
272 | { | |
273 | get_logger()->print_exception_md5_sum("Unknown exception"); | |
274 | return -1; | |
275 | } | |
a78b44b5 | 276 | |
31af6a2e BS |
277 | // append "." and salt and compute md5 sum and get the HEX representation |
278 | pw_md5_hex.append("."); | |
279 | pw_md5_hex.append(salt); | |
a78b44b5 | 280 | |
31af6a2e BS |
281 | string secret; |
282 | try | |
a78b44b5 | 283 | { |
31af6a2e | 284 | secret = Util::compute_md5_digest(pw_md5_hex); |
a78b44b5 | 285 | } |
c730deea | 286 | catch ( exception& e ) |
a78b44b5 | 287 | { |
31af6a2e BS |
288 | get_logger()->print_exception_md5_sum(e.what()); |
289 | return -1; | |
290 | } | |
c730deea BS |
291 | catch ( ... ) |
292 | { | |
293 | get_logger()->print_exception_md5_sum("Unknown exception"); | |
294 | return -1; | |
295 | } | |
31af6a2e BS |
296 | |
297 | // Now its time to issue the second http_get operation | |
62df5f33 | 298 | string url = assemble_update_url(salt, sign_time, sign, secret, ip); |
31af6a2e BS |
299 | |
300 | // perform the update operation | |
301 | http_status_code = HTTPHelp->http_get(url); | |
302 | ||
303 | get_logger()->print_http_status_code(url,http_status_code); | |
304 | ||
305 | if ( http_status_code == 200 ) | |
306 | { | |
307 | // parse the update request return code | |
3208f713 BS |
308 | curl_data = HTTPHelp->get_curl_data(); |
309 | string update_return_code = parse_return_code(curl_data); | |
31af6a2e BS |
310 | if ( update_return_code == "0" ) |
311 | { | |
312 | return 0; | |
313 | } | |
314 | else if ( update_return_code == "1" ) | |
315 | { | |
316 | get_logger()->print_service_not_authorized(url,get_login(),get_password()); | |
317 | } | |
318 | else | |
319 | { | |
320 | get_logger()->print_update_failure(url,update_return_code); | |
321 | } | |
a78b44b5 BS |
322 | } |
323 | else | |
324 | { | |
31af6a2e BS |
325 | // second http get operation (update) was not successful |
326 | get_logger()->print_update_failure(url,http_status_code); | |
a78b44b5 BS |
327 | } |
328 | } | |
329 | else | |
330 | { | |
31af6a2e BS |
331 | // first http get operation was not successful |
332 | get_logger()->print_update_failure(BaseUrl,http_status_code); | |
a78b44b5 BS |
333 | } |
334 | } | |
335 | else | |
336 | { | |
e8787e2e | 337 | get_logger()->print_httphelper_not_initialized(); |
e417b034 | 338 | HTTPHelp->re_initialize(); |
a78b44b5 | 339 | } |
a78b44b5 BS |
340 | return -1; |
341 | } |