| 1 | /** @file |
| 2 | * @brief TZO Service class implementation. This class represents the TZO service. |
| 3 | * |
| 4 | * |
| 5 | * |
| 6 | * @copyright Intra2net AG |
| 7 | * @license GPLv2 |
| 8 | */ |
| 9 | |
| 10 | #include "service_tzo.hpp" |
| 11 | #include "util.hpp" |
| 12 | |
| 13 | #include <time.h> |
| 14 | #include <boost/foreach.hpp> |
| 15 | #include <boost/algorithm/string.hpp> |
| 16 | |
| 17 | namespace ba = boost::algorithm; |
| 18 | |
| 19 | using namespace std; |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Default Constructor, needed for object serialization. |
| 24 | */ |
| 25 | ServiceTzo::ServiceTzo() |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Constructor. |
| 32 | * @param _hostname The hostname to update |
| 33 | * @param _login The login name. |
| 34 | * @param _password The corresponding password. |
| 35 | */ |
| 36 | ServiceTzo::ServiceTzo(const string& _protocol, 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 _max_equal_updates_in_succession, const int _dns_cache_ttl, const string& _proxy, const int _proxy_port) |
| 37 | { |
| 38 | if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config) |
| 39 | set_update_interval(15); // use default protocol value |
| 40 | else |
| 41 | set_update_interval(_update_interval); |
| 42 | |
| 43 | if ( _max_updates_within_interval == -1 ) |
| 44 | set_max_updates_within_interval(3); |
| 45 | else |
| 46 | set_max_updates_within_interval(_max_updates_within_interval); |
| 47 | |
| 48 | if ( _max_equal_updates_in_succession == -1 ) |
| 49 | set_max_equal_updates_in_succession(2); |
| 50 | else |
| 51 | set_max_equal_updates_in_succession(_max_equal_updates_in_succession); |
| 52 | |
| 53 | if ( _dns_cache_ttl == -1 ) |
| 54 | set_dns_cache_ttl(60); |
| 55 | else |
| 56 | set_dns_cache_ttl(_dns_cache_ttl); |
| 57 | |
| 58 | set_protocol(_protocol); |
| 59 | set_hostname(_hostname); |
| 60 | set_login(_login); |
| 61 | set_password(_password); |
| 62 | set_logger(_logger); |
| 63 | |
| 64 | // create http helper class |
| 65 | HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port)); |
| 66 | |
| 67 | BaseUrl = assemble_base_url(get_hostname(),get_login(),get_password()); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * Default destructor |
| 73 | */ |
| 74 | ServiceTzo::~ServiceTzo() |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Assemble the dyns update url from the given fqhn |
| 81 | * @param hostname The fqhn hostname to update IP for. |
| 82 | * @param username The username to use. |
| 83 | * @param hostname The password to use. |
| 84 | * @return The assembled update url without IP. |
| 85 | */ |
| 86 | string ServiceTzo::assemble_base_url(const string& fqhn, const string& username, const string& password) const |
| 87 | { |
| 88 | string base_url; |
| 89 | |
| 90 | base_url = "http://rh.tzo.com"; |
| 91 | base_url.append("/webclient/tzoperl.html?system=tzodns&info=1&tzoname="); |
| 92 | base_url.append(fqhn); |
| 93 | base_url.append("&email="); |
| 94 | base_url.append(username); |
| 95 | base_url.append("&tzokey="); |
| 96 | base_url.append(password); |
| 97 | base_url.append("&ipaddress="); |
| 98 | |
| 99 | return base_url; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * Performs the Service update. |
| 105 | * @param ip IP Address to set. |
| 106 | * @return 0 if all is fine, -1 otherwise. |
| 107 | */ |
| 108 | Service::UpdateErrorCode ServiceTzo::perform_update(const std::string& ip) |
| 109 | { |
| 110 | string url = BaseUrl; |
| 111 | url.append(ip); |
| 112 | |
| 113 | if ( HTTPHelp->is_initialized() ) |
| 114 | { |
| 115 | long http_status_code = HTTPHelp->http_get(url); |
| 116 | |
| 117 | get_logger()->print_http_status_code(url,http_status_code); |
| 118 | |
| 119 | // HTTP operation completed successful. |
| 120 | // Now we have to parse the data received by curl, |
| 121 | // cause http status code is not significant for dyns update errors |
| 122 | if ( http_status_code == 200 ) |
| 123 | { |
| 124 | // Get the received http data and parse the status code. |
| 125 | string curl_data = HTTPHelp->get_curl_data(); |
| 126 | string status_code = Util::parse_status_code(curl_data,"\r\n"); |
| 127 | |
| 128 | if ( status_code == "200" ) |
| 129 | { |
| 130 | return UpdateOk; |
| 131 | } |
| 132 | else if ( status_code == "401" ) |
| 133 | { |
| 134 | get_logger()->print_service_not_authorized(url,get_login(),get_password()); |
| 135 | return NotAuth; |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | get_logger()->print_update_failure(url,curl_data); |
| 140 | return UpdateError; |
| 141 | } |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | get_logger()->print_update_failure(url,http_status_code); |
| 146 | } |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | get_logger()->print_httphelper_not_initialized(); |
| 151 | HTTPHelp->re_initialize(); |
| 152 | } |
| 153 | |
| 154 | return GenericError; |
| 155 | } |