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 | ||
10 | #include "service_dhs.h" | |
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 | |
60 | HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password)); | |
4dfdba68 | 61 | HTTPHelp.swap(_http_help); |
089a7152 BS |
62 | |
63 | // extract domain part from hostname | |
64 | list<string> host_domain_part = separate_domain_and_host_part(get_hostname()); | |
65 | ||
66 | BaseUrl = assemble_base_url(host_domain_part.front(),host_domain_part.back()); | |
67 | } | |
68 | ||
69 | ||
70 | /** | |
71 | * Default destructor | |
72 | */ | |
629d8110 | 73 | ServiceDhs::~ServiceDhs() |
089a7152 BS |
74 | { |
75 | } | |
76 | ||
77 | ||
78 | /** | |
79 | * Assemble the dhs update url from the given hostname and domain part | |
80 | * @param hostname The hostname to update IP for. | |
81 | * @param domain_part The domain_part in which the hostname is located. | |
82 | * @return The assembled update url without IP. | |
83 | */ | |
629d8110 | 84 | string ServiceDhs::assemble_base_url(const string& hostname, const string& domain_part) const |
089a7152 BS |
85 | { |
86 | string base_url; | |
87 | ||
88 | base_url = "http://members.dhs.org"; | |
89 | base_url.append("/nic/hosts?hostscmd=edit&hostscmdstage=2&type=4&domain="); | |
90 | base_url.append(domain_part); | |
91 | base_url.append("&hostname="); | |
92 | base_url.append(hostname); | |
93 | base_url.append("&updatetype=Online&ip="); | |
94 | ||
95 | return base_url; | |
96 | } | |
97 | ||
98 | ||
99 | /** | |
100 | * Separates the hostname from the domain part. | |
101 | * @param fqdn Hostname with domain part. | |
102 | * @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. | |
103 | */ | |
629d8110 | 104 | list<string> ServiceDhs::separate_domain_and_host_part(const string& fqdn) const |
089a7152 BS |
105 | { |
106 | list<string> splitted; | |
107 | ba::split(splitted,fqdn,boost::is_any_of(".")); | |
108 | ||
109 | if ( splitted.size() > 1 ) | |
110 | { | |
111 | string host = splitted.front(); | |
112 | splitted.pop_front(); | |
113 | ||
114 | string domain = splitted.front(); | |
115 | splitted.pop_front(); | |
116 | ||
117 | BOOST_FOREACH(string domain_part, splitted) | |
118 | { | |
119 | domain.append("."); | |
120 | domain.append(domain_part); | |
121 | } | |
122 | ||
123 | splitted.clear(); | |
124 | splitted.push_back(host); | |
125 | splitted.push_back(domain); | |
126 | } | |
127 | ||
128 | return splitted; | |
129 | } | |
130 | ||
131 | ||
132 | /** | |
133 | * Performs the Service update. | |
134 | * @param ip IP Address to set. | |
135 | * @return 0 if all is fine, -1 otherwise. | |
136 | */ | |
629d8110 | 137 | int ServiceDhs::perform_update(const std::string& ip) |
089a7152 | 138 | { |
089a7152 BS |
139 | string url = BaseUrl; |
140 | url.append(ip); | |
141 | ||
31af6a2e | 142 | if ( HTTPHelp->is_initialized() == true ) |
089a7152 | 143 | { |
31af6a2e BS |
144 | // Perform curl operation on given url. |
145 | long http_status_code = HTTPHelp->http_get(url); | |
113a03a9 | 146 | |
31af6a2e BS |
147 | get_logger()->print_http_status_code(url,http_status_code); |
148 | ||
149 | // Check the status code for protocol errors. | |
150 | if ( http_status_code == 200 ) | |
151 | { | |
152 | // Get the received http data. | |
153 | string curl_data = HTTPHelp->get_curl_data(); | |
154 | ||
155 | // Search for the following string in the received http data | |
156 | boost::regex expr_done("Updating ip on .*: done"); | |
157 | if ( boost::regex_search(curl_data,expr_done) ) | |
158 | { | |
159 | // Update successful | |
160 | return 0; | |
161 | } | |
162 | else | |
163 | { | |
164 | get_logger()->print_update_failure(url,curl_data); | |
165 | } | |
166 | } | |
167 | else if ( http_status_code == 401 ) | |
113a03a9 | 168 | { |
31af6a2e | 169 | get_logger()->print_service_not_authorized(url,get_login(),get_password()); |
113a03a9 BS |
170 | } |
171 | else | |
172 | { | |
31af6a2e | 173 | get_logger()->print_update_failure(url,http_status_code); |
113a03a9 | 174 | } |
089a7152 | 175 | } |
089a7152 | 176 | else |
a78b44b5 | 177 | { |
31af6a2e | 178 | get_logger()->print_service_not_initialized(url); |
a78b44b5 | 179 | } |
a78b44b5 | 180 | return -1; |
089a7152 | 181 | } |