2 * @brief GNUDIP Service class implementation. This class represents the GNUDIP service.
6 * @copyright Intra2net AG
10 #include "service_gnudip.h"
14 #include <boost/foreach.hpp>
15 #include <boost/regex.hpp>
21 * Default Constructor, needed for object serialization.
23 ServiceGnudip::ServiceGnudip()
30 * @param _hostname The hostname to update
31 * @param _login The login name.
32 * @param _password The corresponding password.
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)
37 if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config)
38 set_update_interval(0); // use default protocol value
40 set_update_interval(_update_interval);
42 if ( _max_updates_within_interval == -1 )
43 set_max_updates_within_interval(0);
45 set_max_updates_within_interval(_max_updates_within_interval);
47 if ( _dns_cache_ttl == -1 )
48 set_dns_cache_ttl(60);
50 set_dns_cache_ttl(_dns_cache_ttl);
52 set_protocol(_protocol);
53 set_hostname(_hostname);
55 set_password(_password);
58 // create http helper class
59 HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password));
60 HTTPHelp = _http_help;
63 BaseUrl = assemble_base_url(get_hostname(),_gnudip_server);
70 ServiceGnudip::~ServiceGnudip()
76 * Assemble the dyndns update url from the given fqhn
77 * @param hostname The fqhn hostname to update IP for.
78 * @return The assembled update url without IP.
80 string ServiceGnudip::assemble_base_url(const string& fqhn, const string& gnudip_server) const
85 base_url.append(gnudip_server);
86 base_url.append("/gnudip/cgi-bin/gdipupdt.cgi");
93 * Parses the data received from the initial request, which should contain salt, time and sign.
94 * @param curl_data The complete received curl data.
95 * @return A map with salt, time and sign or an empty map.
97 map<string,string> ServiceGnudip::parse_initial_request(const string& curl_data) const
99 map<string,string> response;
102 boost::regex expr_salt("<meta name=\"salt\" content=\"(.*)\">");
104 boost::regex expr_time("<meta name=\"time\" content=\"(.*)\">");
106 boost::regex expr_sign("<meta name=\"sign\" content=\"(.*)\">");
108 boost::smatch matches;
110 // Get the salt out of received http data
111 if ( boost::regex_search(curl_data,matches,expr_salt) )
113 response.insert(pair<string,string>("salt",matches[1]));
114 get_logger()->print_regex_match(expr_salt.str(),matches[1]);
118 get_logger()->print_no_regex_match(expr_salt.str(),curl_data);
123 // Get the time out of received http data
124 if ( boost::regex_search(curl_data,matches,expr_time) )
126 response.insert(pair<string,string>("time",matches[1]));
127 get_logger()->print_regex_match(expr_salt.str(),matches[1]);
131 get_logger()->print_no_regex_match(expr_salt.str(),curl_data);
136 // Get the sign out of received http data
137 if ( boost::regex_search(curl_data,matches,expr_sign) )
139 response.insert(pair<string,string>("sign",matches[1]));
140 get_logger()->print_regex_match(expr_salt.str(),matches[1]);
144 get_logger()->print_no_regex_match(expr_salt.str(),curl_data);
154 * Get the assembled update url.
155 * @param salt Salt from the initial request
156 * @param time Time from the initial request
157 * @param sign Sign from the initial request
158 * @param secret Computed md5 secret in HEX
159 * @param ip IP to update
160 * @return The assembled update url.
162 string ServiceGnudip::assemble_update_url(const string& salt, const string& time, const string& sign, const string& secret, const string& ip) const
164 string url = BaseUrl;
166 url.append("?salt=");
168 url.append("&time=");
170 url.append("&sign=");
172 url.append("&user=");
173 url.append(get_login());
174 url.append("&domn=");
175 url.append(get_hostname());
176 url.append("&pass=");
178 url.append("&reqc=0&addr=");
186 * Performs the Service update.
187 * @param ip IP Address to set.
188 * @return 0 if all is fine, -1 otherwise.
190 int ServiceGnudip::perform_update(const std::string& ip)
193 long http_status_code = HTTPHelp->http_get(BaseUrl);
195 get_logger()->print_http_status_code(BaseUrl,http_status_code);
197 if ( http_status_code == 200 )
199 // Get the received http data which should contain the salt, time and sign
200 string curl_data = HTTPHelp->get_curl_data();
202 // Parse salt, time and sign out of the received data
203 map<string,string> salt_time_sign = parse_initial_request(curl_data);
205 if ( salt_time_sign.empty() )
207 get_logger()->print_could_not_parse_received_data(curl_data);
211 // at this point we have salt, time and sign parsed successfully
212 string salt, time, sign;
214 map<string,string>::iterator iter = salt_time_sign.find("salt");
215 if ( iter != salt_time_sign.end() )
218 iter = salt_time_sign.find("time");
219 if ( iter != salt_time_sign.end() )
222 iter = salt_time_sign.find("sign");
223 if ( iter != salt_time_sign.end() )
226 if ( salt.empty() || time.empty() || sign.empty() )
227 get_logger()->print_could_not_get_initial_gnudip_data();
229 // compute md5 sum from users password and get the HEX representation
230 string pw_md5_hex = Util::compute_md5_digest(get_password());
232 // append "." and salt and compute md5 sum and get the HEX representation
233 pw_md5_hex.append(".");
234 pw_md5_hex.append(salt);
235 string secret = Util::compute_md5_digest(pw_md5_hex);
237 // Now its time to issue the second http_get operation
238 string url = assemble_update_url(salt, time, sign, secret, ip);
240 // perform the update operation
241 http_status_code = HTTPHelp->http_get(url);
243 get_logger()->print_http_status_code(url,http_status_code);
245 if ( http_status_code == 200 )
247 // parse the update request return code
248 string update_return_code = HTTPHelp->get_curl_data();
249 if ( update_return_code == "0" )
253 else if ( update_return_code == "1" )
255 get_logger()->print_http_not_authorized(url,get_login(),get_password());
259 get_logger()->print_update_failure(url,update_return_code);
264 // second http get operation (update) was not successful
265 get_logger()->print_update_failure(url,http_status_code);
270 // first http get operation was not successful
271 get_logger()->print_update_failure(BaseUrl,http_status_code);
279 * Serialize function needed by boost/serialization to define which members should be stored as the object state.
281 * @param version Version
283 template<class Archive>
284 void ServiceGnudip::serialize(Archive & ar, const unsigned int version)
286 ar & boost::serialization::base_object<Service>(*this);