Commit | Line | Data |
---|---|---|
089a7152 BS |
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.h" | |
52e7ca71 | 11 | #include "util.h" |
089a7152 BS |
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 | */ | |
629d8110 | 25 | ServiceTzo::ServiceTzo() |
089a7152 BS |
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 | */ | |
629d8110 | 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 _dns_cache_ttl, const string& _proxy, const int _proxy_port) |
089a7152 BS |
37 | { |
38 | if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config) | |
39 | set_update_interval(0); // 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(0); | |
45 | else | |
46 | set_max_updates_within_interval(_max_updates_within_interval); | |
47 | ||
48 | if ( _dns_cache_ttl == -1 ) | |
49 | set_dns_cache_ttl(60); | |
50 | else | |
51 | set_dns_cache_ttl(_dns_cache_ttl); | |
52 | ||
53 | set_protocol(_protocol); | |
54 | set_hostname(_hostname); | |
55 | set_login(_login); | |
56 | set_password(_password); | |
57 | set_logger(_logger); | |
58 | ||
59 | // create http helper class | |
60 | HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port)); | |
4dfdba68 | 61 | HTTPHelp.swap(_http_help); |
089a7152 BS |
62 | |
63 | BaseUrl = assemble_base_url(get_hostname(),get_login(),get_password()); | |
64 | } | |
65 | ||
66 | ||
67 | /** | |
68 | * Default destructor | |
69 | */ | |
629d8110 | 70 | ServiceTzo::~ServiceTzo() |
089a7152 BS |
71 | { |
72 | } | |
73 | ||
74 | ||
75 | /** | |
76 | * Assemble the dyns update url from the given fqhn | |
77 | * @param hostname The fqhn hostname to update IP for. | |
78 | * @param username The username to use. | |
79 | * @param hostname The password to use. | |
80 | * @return The assembled update url without IP. | |
81 | */ | |
629d8110 | 82 | string ServiceTzo::assemble_base_url(const string& fqhn, const string& username, const string& password) const |
089a7152 BS |
83 | { |
84 | string base_url; | |
85 | ||
86 | base_url = "http://rh.tzo.com"; | |
87 | base_url.append("/webclient/tzoperl.html?system=tzodns&info=1&tzoname="); | |
88 | base_url.append(fqhn); | |
89 | base_url.append("&email="); | |
90 | base_url.append(username); | |
91 | base_url.append("&tzokey="); | |
92 | base_url.append(password); | |
93 | base_url.append("&ipaddress="); | |
94 | ||
95 | return base_url; | |
96 | } | |
97 | ||
98 | ||
99 | /** | |
100 | * Performs the Service update. | |
101 | * @param ip IP Address to set. | |
102 | * @return 0 if all is fine, -1 otherwise. | |
103 | */ | |
629d8110 | 104 | int ServiceTzo::perform_update(const std::string& ip) |
089a7152 | 105 | { |
089a7152 BS |
106 | string url = BaseUrl; |
107 | url.append(ip); | |
108 | ||
31af6a2e | 109 | if ( HTTPHelp->is_initialized() == true ) |
089a7152 | 110 | { |
31af6a2e | 111 | long http_status_code = HTTPHelp->http_get(url); |
089a7152 | 112 | |
31af6a2e BS |
113 | get_logger()->print_http_status_code(url,http_status_code); |
114 | ||
115 | // HTTP operation completed successful. | |
116 | // Now we have to parse the data received by curl, | |
117 | // cause http status code is not significant for dyns update errors | |
118 | if ( http_status_code == 200 ) | |
a78b44b5 | 119 | { |
31af6a2e BS |
120 | // Get the received http data and parse the status code. |
121 | string curl_data = HTTPHelp->get_curl_data(); | |
122 | string status_code = Util::parse_status_code(curl_data,"\r\n"); | |
123 | ||
124 | if ( status_code == "200" ) | |
125 | { | |
126 | return 0; | |
127 | } | |
128 | else if ( status_code == "401" ) | |
129 | { | |
130 | get_logger()->print_service_not_authorized(url,get_login(),get_password()); | |
131 | } | |
132 | else | |
133 | { | |
134 | get_logger()->print_update_failure(url,curl_data); | |
135 | } | |
a78b44b5 | 136 | } |
089a7152 | 137 | else |
a78b44b5 | 138 | { |
31af6a2e | 139 | get_logger()->print_update_failure(url,http_status_code); |
a78b44b5 BS |
140 | } |
141 | } | |
142 | else | |
143 | { | |
31af6a2e | 144 | get_logger()->print_service_not_initialized(url); |
089a7152 | 145 | } |
a78b44b5 | 146 | return -1; |
089a7152 | 147 | } |