Commit | Line | Data |
---|---|---|
089a7152 BS |
1 | /** @file |
2 | * @brief DHS Service class implementation. This class represents the DHS service. | |
3 | * | |
4 | * | |
5 | * | |
6 | * @copyright Intra2net AG | |
7 | * @license GPLv2 | |
8 | */ | |
9 | ||
4de6a9b8 | 10 | #include "service_dhs.hpp" |
089a7152 BS |
11 | |
12 | #include <time.h> | |
13 | #include <boost/foreach.hpp> | |
14 | #include <boost/algorithm/string.hpp> | |
113a03a9 | 15 | #include <boost/regex.hpp> |
089a7152 BS |
16 | |
17 | namespace ba = boost::algorithm; | |
18 | ||
19 | using namespace std; | |
20 | ||
21 | ||
22 | /** | |
23 | * Default Constructor, needed for object serialization. | |
24 | */ | |
629d8110 | 25 | ServiceDhs::ServiceDhs() |
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 | ServiceDhs::ServiceDhs(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(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 ( _dns_cache_ttl == -1 ) | |
49 | set_dns_cache_ttl(7200); | |
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 | |
c3c84086 | 60 | HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password)); |
089a7152 BS |
61 | |
62 | // extract domain part from hostname | |
63 | list<string> host_domain_part = separate_domain_and_host_part(get_hostname()); | |
64 | ||
08a5a621 | 65 | BaseUrl = assemble_base_url(host_domain_part.front(),host_domain_part.back()); /*lint !e864 */ |
089a7152 BS |
66 | } |
67 | ||
68 | ||
69 | /** | |
70 | * Default destructor | |
71 | */ | |
629d8110 | 72 | ServiceDhs::~ServiceDhs() |
089a7152 BS |
73 | { |
74 | } | |
75 | ||
76 | ||
77 | /** | |
78 | * Assemble the dhs update url from the given hostname and domain part | |
79 | * @param hostname The hostname to update IP for. | |
80 | * @param domain_part The domain_part in which the hostname is located. | |
81 | * @return The assembled update url without IP. | |
82 | */ | |
629d8110 | 83 | string ServiceDhs::assemble_base_url(const string& hostname, const string& domain_part) const |
089a7152 BS |
84 | { |
85 | string base_url; | |
86 | ||
87 | base_url = "http://members.dhs.org"; | |
88 | base_url.append("/nic/hosts?hostscmd=edit&hostscmdstage=2&type=4&domain="); | |
89 | base_url.append(domain_part); | |
90 | base_url.append("&hostname="); | |
91 | base_url.append(hostname); | |
92 | base_url.append("&updatetype=Online&ip="); | |
93 | ||
94 | return base_url; | |
95 | } | |
96 | ||
97 | ||
98 | /** | |
99 | * Separates the hostname from the domain part. | |
100 | * @param fqdn Hostname with domain part. | |
101 | * @return A list with 2 elements (first element is the hostname, second element the domain part), or a list with 1 element if the domain part couldn't be determined. | |
102 | */ | |
629d8110 | 103 | list<string> ServiceDhs::separate_domain_and_host_part(const string& fqdn) const |
089a7152 BS |
104 | { |
105 | list<string> splitted; | |
106 | ba::split(splitted,fqdn,boost::is_any_of(".")); | |
107 | ||
108 | if ( splitted.size() > 1 ) | |
109 | { | |
110 | string host = splitted.front(); | |
111 | splitted.pop_front(); | |
112 | ||
113 | string domain = splitted.front(); | |
114 | splitted.pop_front(); | |
115 | ||
116 | BOOST_FOREACH(string domain_part, splitted) | |
117 | { | |
118 | domain.append("."); | |
119 | domain.append(domain_part); | |
120 | } | |
121 | ||
122 | splitted.clear(); | |
123 | splitted.push_back(host); | |
124 | splitted.push_back(domain); | |
125 | } | |
126 | ||
127 | return splitted; | |
128 | } | |
129 | ||
130 | ||
131 | /** | |
132 | * Performs the Service update. | |
133 | * @param ip IP Address to set. | |
134 | * @return 0 if all is fine, -1 otherwise. | |
135 | */ | |
629d8110 | 136 | int ServiceDhs::perform_update(const std::string& ip) |
089a7152 | 137 | { |
089a7152 BS |
138 | string url = BaseUrl; |
139 | url.append(ip); | |
140 | ||
08a5a621 | 141 | if ( HTTPHelp->is_initialized() ) |
089a7152 | 142 | { |
31af6a2e BS |
143 | // Perform curl operation on given url. |
144 | long http_status_code = HTTPHelp->http_get(url); | |
113a03a9 | 145 | |
31af6a2e BS |
146 | get_logger()->print_http_status_code(url,http_status_code); |
147 | ||
148 | // Check the status code for protocol errors. | |
149 | if ( http_status_code == 200 ) | |
150 | { | |
151 | // Get the received http data. | |
152 | string curl_data = HTTPHelp->get_curl_data(); | |
153 | ||
154 | // Search for the following string in the received http data | |
155 | boost::regex expr_done("Updating ip on .*: done"); | |
156 | if ( boost::regex_search(curl_data,expr_done) ) | |
157 | { | |
158 | // Update successful | |
159 | return 0; | |
160 | } | |
161 | else | |
162 | { | |
163 | get_logger()->print_update_failure(url,curl_data); | |
164 | } | |
165 | } | |
166 | else if ( http_status_code == 401 ) | |
113a03a9 | 167 | { |
31af6a2e | 168 | get_logger()->print_service_not_authorized(url,get_login(),get_password()); |
113a03a9 BS |
169 | } |
170 | else | |
171 | { | |
31af6a2e | 172 | get_logger()->print_update_failure(url,http_status_code); |
113a03a9 | 173 | } |
089a7152 | 174 | } |
089a7152 | 175 | else |
a78b44b5 | 176 | { |
e8787e2e | 177 | get_logger()->print_httphelper_not_initialized(); |
e417b034 | 178 | HTTPHelp->re_initialize(); |
a78b44b5 | 179 | } |
a78b44b5 | 180 | return -1; |
089a7152 | 181 | } |