Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/openvpn_management_server.h" |
| 6 | |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 7 | #include <arpa/inet.h> |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 8 | #include <netinet/in.h> |
| 9 | |
| 10 | #include <base/bind.h> |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 11 | #include <base/string_number_conversions.h> |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 12 | #include <base/string_split.h> |
| 13 | #include <base/string_util.h> |
| 14 | #include <base/stringprintf.h> |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 15 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 16 | |
| 17 | #include "shill/event_dispatcher.h" |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 18 | #include "shill/glib.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 19 | #include "shill/logging.h" |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 20 | #include "shill/openvpn_driver.h" |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 21 | #include "shill/sockets.h" |
| 22 | |
| 23 | using base::Bind; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 24 | using base::IntToString; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 25 | using base::SplitString; |
| 26 | using base::StringPrintf; |
| 27 | using std::string; |
| 28 | using std::vector; |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 29 | |
| 30 | namespace shill { |
| 31 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 32 | OpenVPNManagementServer::OpenVPNManagementServer(OpenVPNDriver *driver, |
| 33 | GLib *glib) |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 34 | : driver_(driver), |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 35 | glib_(glib), |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 36 | weak_ptr_factory_(this), |
| 37 | ready_callback_(Bind(&OpenVPNManagementServer::OnReady, |
| 38 | weak_ptr_factory_.GetWeakPtr())), |
| 39 | input_callback_(Bind(&OpenVPNManagementServer::OnInput, |
| 40 | weak_ptr_factory_.GetWeakPtr())), |
| 41 | sockets_(NULL), |
| 42 | socket_(-1), |
| 43 | dispatcher_(NULL), |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 44 | connected_socket_(-1), |
| 45 | hold_waiting_(false), |
| 46 | hold_release_(false) {} |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 47 | |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 48 | OpenVPNManagementServer::~OpenVPNManagementServer() { |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 49 | OpenVPNManagementServer::Stop(); |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 50 | } |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 51 | |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 52 | bool OpenVPNManagementServer::Start(EventDispatcher *dispatcher, |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 53 | Sockets *sockets, |
| 54 | vector<string> *options) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 55 | SLOG(VPN, 2) << __func__; |
Darin Petkov | e08084d | 2012-06-11 13:19:35 +0200 | [diff] [blame] | 56 | if (IsStarted()) { |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 57 | return true; |
| 58 | } |
| 59 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 60 | int socket = sockets->Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 61 | if (socket < 0) { |
| 62 | PLOG(ERROR) << "Unable to create management server socket."; |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | struct sockaddr_in addr; |
| 67 | socklen_t addrlen = sizeof(addr); |
| 68 | memset(&addr, 0, sizeof(addr)); |
| 69 | addr.sin_family = AF_INET; |
| 70 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 71 | if (sockets->Bind( |
| 72 | socket, reinterpret_cast<struct sockaddr *>(&addr), addrlen) < 0 || |
| 73 | sockets->Listen(socket, 1) < 0 || |
| 74 | sockets->GetSockName( |
| 75 | socket, reinterpret_cast<struct sockaddr *>(&addr), &addrlen) < 0) { |
| 76 | PLOG(ERROR) << "Socket setup failed."; |
| 77 | sockets->Close(socket); |
| 78 | return false; |
| 79 | } |
| 80 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 81 | SLOG(VPN, 2) << "Listening socket: " << socket; |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 82 | sockets_ = sockets; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 83 | socket_ = socket; |
| 84 | ready_handler_.reset( |
| 85 | dispatcher->CreateReadyHandler( |
| 86 | socket, IOHandler::kModeInput, ready_callback_)); |
| 87 | dispatcher_ = dispatcher; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 88 | |
| 89 | // Append openvpn management API options. |
| 90 | options->push_back("--management"); |
| 91 | options->push_back(inet_ntoa(addr.sin_addr)); |
| 92 | options->push_back(IntToString(ntohs(addr.sin_port))); |
| 93 | options->push_back("--management-client"); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 94 | |
| 95 | options->push_back("--management-hold"); |
| 96 | hold_release_ = false; |
| 97 | hold_waiting_ = false; |
| 98 | |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 99 | options->push_back("--management-query-passwords"); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 100 | if (driver_->AppendValueOption(flimflam::kOpenVPNStaticChallengeProperty, |
| 101 | "--static-challenge", |
| 102 | options)) { |
| 103 | options->push_back("1"); // Force echo. |
| 104 | } |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | |
| 108 | void OpenVPNManagementServer::Stop() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 109 | SLOG(VPN, 2) << __func__; |
Darin Petkov | e08084d | 2012-06-11 13:19:35 +0200 | [diff] [blame] | 110 | if (!IsStarted()) { |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 111 | return; |
| 112 | } |
| 113 | input_handler_.reset(); |
| 114 | if (connected_socket_ >= 0) { |
| 115 | sockets_->Close(connected_socket_); |
| 116 | connected_socket_ = -1; |
| 117 | } |
| 118 | dispatcher_ = NULL; |
| 119 | ready_handler_.reset(); |
| 120 | if (socket_ >= 0) { |
| 121 | sockets_->Close(socket_); |
| 122 | socket_ = -1; |
| 123 | } |
| 124 | sockets_ = NULL; |
| 125 | } |
| 126 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 127 | void OpenVPNManagementServer::ReleaseHold() { |
| 128 | SLOG(VPN, 2) << __func__; |
| 129 | hold_release_ = true; |
| 130 | if (!hold_waiting_) { |
| 131 | return; |
| 132 | } |
| 133 | LOG(INFO) << "Releasing hold."; |
| 134 | hold_waiting_ = false; |
| 135 | SendHoldRelease(); |
| 136 | } |
| 137 | |
| 138 | void OpenVPNManagementServer::Hold() { |
| 139 | SLOG(VPN, 2) << __func__; |
| 140 | hold_release_ = false; |
| 141 | } |
| 142 | |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 143 | void OpenVPNManagementServer::OnReady(int fd) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 144 | SLOG(VPN, 2) << __func__ << "(" << fd << ")"; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 145 | connected_socket_ = sockets_->Accept(fd, NULL, NULL); |
| 146 | if (connected_socket_ < 0) { |
| 147 | PLOG(ERROR) << "Connected socket accept failed."; |
| 148 | return; |
| 149 | } |
| 150 | ready_handler_.reset(); |
| 151 | input_handler_.reset( |
| 152 | dispatcher_->CreateInputHandler(connected_socket_, input_callback_)); |
| 153 | SendState("on"); |
| 154 | } |
| 155 | |
| 156 | void OpenVPNManagementServer::OnInput(InputData *data) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 157 | SLOG(VPN, 2) << __func__ << "(" << data->len << ")"; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 158 | vector<string> messages; |
| 159 | SplitString( |
| 160 | string(reinterpret_cast<char *>(data->buf), data->len), '\n', &messages); |
| 161 | for (vector<string>::const_iterator it = messages.begin(); |
Darin Petkov | e08084d | 2012-06-11 13:19:35 +0200 | [diff] [blame] | 162 | it != messages.end() && IsStarted(); ++it) { |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 163 | ProcessMessage(*it); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void OpenVPNManagementServer::ProcessMessage(const string &message) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 168 | SLOG(VPN, 2) << __func__ << "(" << message << ")"; |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 169 | if (message.empty()) { |
| 170 | return; |
| 171 | } |
| 172 | if (!ProcessInfoMessage(message) && |
| 173 | !ProcessNeedPasswordMessage(message) && |
| 174 | !ProcessFailedPasswordMessage(message) && |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 175 | !ProcessStateMessage(message) && |
| 176 | !ProcessHoldMessage(message)) { |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 177 | LOG(WARNING) << "OpenVPN management message ignored: " << message; |
| 178 | } |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | bool OpenVPNManagementServer::ProcessInfoMessage(const string &message) { |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 182 | if (!StartsWithASCII(message, ">INFO:", true)) { |
| 183 | return false; |
| 184 | } |
| 185 | LOG(INFO) << "Processing info message."; |
| 186 | return true; |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | bool OpenVPNManagementServer::ProcessNeedPasswordMessage( |
| 190 | const string &message) { |
| 191 | if (!StartsWithASCII(message, ">PASSWORD:Need ", true)) { |
| 192 | return false; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 193 | } |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 194 | LOG(INFO) << "Processing need-password message."; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 195 | string tag = ParseNeedPasswordTag(message); |
| 196 | if (tag == "Auth") { |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 197 | if (message.find("SC:") != string::npos) { |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 198 | PerformStaticChallenge(tag); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 199 | } else { |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 200 | PerformAuthentication(tag); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 201 | } |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 202 | } else if (StartsWithASCII(tag, "User-Specific TPM Token", true)) { |
| 203 | SupplyTPMToken(tag); |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 204 | } else { |
| 205 | NOTIMPLEMENTED() << ": Unsupported need-password message: " << message; |
| 206 | driver_->Cleanup(Service::kStateFailure); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 207 | } |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 211 | // static |
| 212 | string OpenVPNManagementServer::ParseNeedPasswordTag(const string &message) { |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 213 | SLOG(VPN, 2) << __func__ << "(" << message << ")"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 214 | size_t start = message.find('\''); |
| 215 | if (start == string::npos) { |
| 216 | return string(); |
| 217 | } |
| 218 | size_t end = message.find('\'', start + 1); |
| 219 | if (end == string::npos) { |
| 220 | return string(); |
| 221 | } |
| 222 | return message.substr(start + 1, end - start - 1); |
| 223 | } |
| 224 | |
| 225 | void OpenVPNManagementServer::PerformStaticChallenge(const string &tag) { |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 226 | LOG(INFO) << "Perform static challenge: " << tag; |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 227 | string user = |
| 228 | driver_->args()->LookupString(flimflam::kOpenVPNUserProperty, ""); |
| 229 | string password = |
| 230 | driver_->args()->LookupString(flimflam::kOpenVPNPasswordProperty, ""); |
| 231 | string otp = |
| 232 | driver_->args()->LookupString(flimflam::kOpenVPNOTPProperty, ""); |
| 233 | if (user.empty() || password.empty() || otp.empty()) { |
Darin Petkov | e08084d | 2012-06-11 13:19:35 +0200 | [diff] [blame] | 234 | NOTIMPLEMENTED() << ": Missing credentials:" |
| 235 | << (user.empty() ? " no-user" : "") |
| 236 | << (password.empty() ? " no-password" : "") |
| 237 | << (otp.empty() ? " no-otp" : ""); |
Darin Petkov | 0440b9b | 2012-04-17 16:11:56 +0200 | [diff] [blame] | 238 | driver_->Cleanup(Service::kStateFailure); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 239 | return; |
| 240 | } |
| 241 | gchar *b64_password = |
| 242 | glib_->Base64Encode(reinterpret_cast<const guchar *>(password.data()), |
| 243 | password.size()); |
| 244 | gchar *b64_otp = |
| 245 | glib_->Base64Encode(reinterpret_cast<const guchar *>(otp.data()), |
| 246 | otp.size()); |
| 247 | if (!b64_password || !b64_otp) { |
| 248 | LOG(ERROR) << "Unable to base64-encode credentials."; |
| 249 | return; |
| 250 | } |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 251 | SendUsername(tag, user); |
| 252 | SendPassword(tag, StringPrintf("SCRV1:%s:%s", b64_password, b64_otp)); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 253 | glib_->Free(b64_otp); |
| 254 | glib_->Free(b64_password); |
| 255 | // Don't reuse OTP. |
| 256 | driver_->args()->RemoveString(flimflam::kOpenVPNOTPProperty); |
| 257 | } |
| 258 | |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 259 | void OpenVPNManagementServer::PerformAuthentication(const string &tag) { |
| 260 | LOG(INFO) << "Perform authentication: " << tag; |
| 261 | string user = |
| 262 | driver_->args()->LookupString(flimflam::kOpenVPNUserProperty, ""); |
| 263 | string password = |
| 264 | driver_->args()->LookupString(flimflam::kOpenVPNPasswordProperty, ""); |
| 265 | if (user.empty() || password.empty()) { |
| 266 | NOTIMPLEMENTED() << ": Missing credentials:" |
| 267 | << (user.empty() ? " no-user" : "") |
| 268 | << (password.empty() ? " no-password" : ""); |
| 269 | driver_->Cleanup(Service::kStateFailure); |
| 270 | return; |
| 271 | } |
| 272 | SendUsername(tag, user); |
| 273 | SendPassword(tag, password); |
| 274 | } |
| 275 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 276 | void OpenVPNManagementServer::SupplyTPMToken(const string &tag) { |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 277 | SLOG(VPN, 2) << __func__ << "(" << tag << ")"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 278 | string pin = driver_->args()->LookupString(flimflam::kOpenVPNPinProperty, ""); |
| 279 | if (pin.empty()) { |
Darin Petkov | 0440b9b | 2012-04-17 16:11:56 +0200 | [diff] [blame] | 280 | NOTIMPLEMENTED() << ": Missing PIN."; |
| 281 | driver_->Cleanup(Service::kStateFailure); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 282 | return; |
| 283 | } |
| 284 | SendPassword(tag, pin); |
| 285 | } |
| 286 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 287 | bool OpenVPNManagementServer::ProcessFailedPasswordMessage( |
| 288 | const string &message) { |
| 289 | if (!StartsWithASCII(message, ">PASSWORD:Verification Failed:", true)) { |
| 290 | return false; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 291 | } |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 292 | NOTIMPLEMENTED(); |
Darin Petkov | 0440b9b | 2012-04-17 16:11:56 +0200 | [diff] [blame] | 293 | driver_->Cleanup(Service::kStateFailure); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 294 | return true; |
| 295 | } |
| 296 | |
| 297 | // >STATE:* message support. State messages are of the form: |
| 298 | // >STATE:<date>,<state>,<detail>,<local-ip>,<remote-ip> |
| 299 | // where: |
| 300 | // <date> is the current time (since epoch) in seconds |
| 301 | // <state> is one of: |
| 302 | // INITIAL, CONNECTING, WAIT, AUTH, GET_CONFIG, ASSIGN_IP, ADD_ROUTES, |
| 303 | // CONNECTED, RECONNECTING, EXITING, RESOLVE, TCP_CONNECT |
| 304 | // <detail> is a free-form string giving details about the state change |
| 305 | // <local-ip> is a dotted-quad for the local IPv4 address (when available) |
| 306 | // <remote-ip> is a dotted-quad for the remote IPv4 address (when available) |
| 307 | bool OpenVPNManagementServer::ProcessStateMessage(const string &message) { |
| 308 | if (!StartsWithASCII(message, ">STATE:", true)) { |
| 309 | return false; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 310 | } |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 311 | LOG(INFO) << "Processing state message."; |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 312 | vector<string> details; |
| 313 | SplitString(message, ',', &details); |
| 314 | if (details.size() > 1) { |
| 315 | if (details[1] == "RECONNECTING") { |
| 316 | driver_->OnReconnecting(); |
| 317 | } |
| 318 | // The rest of the states are currently ignored. |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 319 | } |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 320 | return true; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 323 | bool OpenVPNManagementServer::ProcessHoldMessage(const string &message) { |
| 324 | if (!StartsWithASCII(message, ">HOLD:Waiting for hold release", true)) { |
| 325 | return false; |
| 326 | } |
| 327 | LOG(INFO) << "Processing hold message."; |
| 328 | hold_waiting_ = true; |
| 329 | if (hold_release_) { |
| 330 | ReleaseHold(); |
| 331 | } |
| 332 | return true; |
| 333 | } |
| 334 | |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 335 | // static |
| 336 | string OpenVPNManagementServer::EscapeToQuote(const string &str) { |
| 337 | string escaped; |
| 338 | for (string::const_iterator it = str.begin(); it != str.end(); ++it) { |
| 339 | if (*it == '\\' || *it == '"') { |
| 340 | escaped += '\\'; |
| 341 | } |
| 342 | escaped += *it; |
| 343 | } |
| 344 | return escaped; |
| 345 | } |
| 346 | |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 347 | void OpenVPNManagementServer::Send(const string &data) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 348 | SLOG(VPN, 2) << __func__; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 349 | ssize_t len = sockets_->Send(connected_socket_, data.data(), data.size(), 0); |
| 350 | PLOG_IF(ERROR, len < 0 || static_cast<size_t>(len) != data.size()) |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 351 | << "Send failed."; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void OpenVPNManagementServer::SendState(const string &state) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 355 | SLOG(VPN, 2) << __func__ << "(" << state << ")"; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 356 | Send(StringPrintf("state %s\n", state.c_str())); |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 359 | void OpenVPNManagementServer::SendUsername(const string &tag, |
| 360 | const string &username) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 361 | SLOG(VPN, 2) << __func__; |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 362 | Send(StringPrintf("username \"%s\" %s\n", tag.c_str(), username.c_str())); |
| 363 | } |
| 364 | |
| 365 | void OpenVPNManagementServer::SendPassword(const string &tag, |
| 366 | const string &password) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 367 | SLOG(VPN, 2) << __func__; |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 368 | Send(StringPrintf("password \"%s\" \"%s\"\n", |
| 369 | tag.c_str(), |
| 370 | EscapeToQuote(password).c_str())); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 373 | void OpenVPNManagementServer::SendHoldRelease() { |
| 374 | SLOG(VPN, 2) << __func__; |
| 375 | Send("hold release\n"); |
| 376 | } |
| 377 | |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 378 | } // namespace shill |