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 | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 143 | void OpenVPNManagementServer::Restart() { |
| 144 | LOG(INFO) << "Restart."; |
| 145 | SendSignal("SIGUSR1"); |
| 146 | } |
| 147 | |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 148 | void OpenVPNManagementServer::OnReady(int fd) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 149 | SLOG(VPN, 2) << __func__ << "(" << fd << ")"; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 150 | connected_socket_ = sockets_->Accept(fd, NULL, NULL); |
| 151 | if (connected_socket_ < 0) { |
| 152 | PLOG(ERROR) << "Connected socket accept failed."; |
| 153 | return; |
| 154 | } |
| 155 | ready_handler_.reset(); |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 156 | // TODO(petkov): Create an error callback. crosbug.com/37427 |
| 157 | input_handler_.reset(dispatcher_->CreateInputHandler( |
| 158 | connected_socket_, input_callback_, IOHandler::ErrorCallback())); |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 159 | SendState("on"); |
| 160 | } |
| 161 | |
| 162 | void OpenVPNManagementServer::OnInput(InputData *data) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 163 | SLOG(VPN, 2) << __func__ << "(" << data->len << ")"; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 164 | vector<string> messages; |
| 165 | SplitString( |
| 166 | string(reinterpret_cast<char *>(data->buf), data->len), '\n', &messages); |
| 167 | for (vector<string>::const_iterator it = messages.begin(); |
Darin Petkov | e08084d | 2012-06-11 13:19:35 +0200 | [diff] [blame] | 168 | it != messages.end() && IsStarted(); ++it) { |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 169 | ProcessMessage(*it); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void OpenVPNManagementServer::ProcessMessage(const string &message) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 174 | SLOG(VPN, 2) << __func__ << "(" << message << ")"; |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 175 | if (message.empty()) { |
| 176 | return; |
| 177 | } |
| 178 | if (!ProcessInfoMessage(message) && |
| 179 | !ProcessNeedPasswordMessage(message) && |
| 180 | !ProcessFailedPasswordMessage(message) && |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 181 | !ProcessStateMessage(message) && |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 182 | !ProcessHoldMessage(message) && |
| 183 | !ProcessSuccessMessage(message)) { |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 184 | LOG(WARNING) << "OpenVPN management message ignored: " << message; |
| 185 | } |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | bool OpenVPNManagementServer::ProcessInfoMessage(const string &message) { |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 189 | if (!StartsWithASCII(message, ">INFO:", true)) { |
| 190 | return false; |
| 191 | } |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 192 | LOG(INFO) << message; |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 193 | return true; |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | bool OpenVPNManagementServer::ProcessNeedPasswordMessage( |
| 197 | const string &message) { |
| 198 | if (!StartsWithASCII(message, ">PASSWORD:Need ", true)) { |
| 199 | return false; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 200 | } |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 201 | LOG(INFO) << "Processing need-password message."; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 202 | string tag = ParseNeedPasswordTag(message); |
| 203 | if (tag == "Auth") { |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 204 | if (message.find("SC:") != string::npos) { |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 205 | PerformStaticChallenge(tag); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 206 | } else { |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 207 | PerformAuthentication(tag); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 208 | } |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 209 | } else if (StartsWithASCII(tag, "User-Specific TPM Token", true)) { |
| 210 | SupplyTPMToken(tag); |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 211 | } else { |
| 212 | NOTIMPLEMENTED() << ": Unsupported need-password message: " << message; |
| 213 | driver_->Cleanup(Service::kStateFailure); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 214 | } |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 215 | return true; |
| 216 | } |
| 217 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 218 | // static |
| 219 | string OpenVPNManagementServer::ParseNeedPasswordTag(const string &message) { |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 220 | SLOG(VPN, 2) << __func__ << "(" << message << ")"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 221 | size_t start = message.find('\''); |
| 222 | if (start == string::npos) { |
| 223 | return string(); |
| 224 | } |
| 225 | size_t end = message.find('\'', start + 1); |
| 226 | if (end == string::npos) { |
| 227 | return string(); |
| 228 | } |
| 229 | return message.substr(start + 1, end - start - 1); |
| 230 | } |
| 231 | |
| 232 | void OpenVPNManagementServer::PerformStaticChallenge(const string &tag) { |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 233 | LOG(INFO) << "Perform static challenge: " << tag; |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 234 | string user = |
| 235 | driver_->args()->LookupString(flimflam::kOpenVPNUserProperty, ""); |
| 236 | string password = |
| 237 | driver_->args()->LookupString(flimflam::kOpenVPNPasswordProperty, ""); |
| 238 | string otp = |
| 239 | driver_->args()->LookupString(flimflam::kOpenVPNOTPProperty, ""); |
| 240 | if (user.empty() || password.empty() || otp.empty()) { |
Darin Petkov | e08084d | 2012-06-11 13:19:35 +0200 | [diff] [blame] | 241 | NOTIMPLEMENTED() << ": Missing credentials:" |
| 242 | << (user.empty() ? " no-user" : "") |
| 243 | << (password.empty() ? " no-password" : "") |
| 244 | << (otp.empty() ? " no-otp" : ""); |
Darin Petkov | 0440b9b | 2012-04-17 16:11:56 +0200 | [diff] [blame] | 245 | driver_->Cleanup(Service::kStateFailure); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 246 | return; |
| 247 | } |
| 248 | gchar *b64_password = |
| 249 | glib_->Base64Encode(reinterpret_cast<const guchar *>(password.data()), |
| 250 | password.size()); |
| 251 | gchar *b64_otp = |
| 252 | glib_->Base64Encode(reinterpret_cast<const guchar *>(otp.data()), |
| 253 | otp.size()); |
| 254 | if (!b64_password || !b64_otp) { |
| 255 | LOG(ERROR) << "Unable to base64-encode credentials."; |
| 256 | return; |
| 257 | } |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 258 | SendUsername(tag, user); |
| 259 | SendPassword(tag, StringPrintf("SCRV1:%s:%s", b64_password, b64_otp)); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 260 | glib_->Free(b64_otp); |
| 261 | glib_->Free(b64_password); |
| 262 | // Don't reuse OTP. |
| 263 | driver_->args()->RemoveString(flimflam::kOpenVPNOTPProperty); |
| 264 | } |
| 265 | |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 266 | void OpenVPNManagementServer::PerformAuthentication(const string &tag) { |
| 267 | LOG(INFO) << "Perform authentication: " << tag; |
| 268 | string user = |
| 269 | driver_->args()->LookupString(flimflam::kOpenVPNUserProperty, ""); |
| 270 | string password = |
| 271 | driver_->args()->LookupString(flimflam::kOpenVPNPasswordProperty, ""); |
| 272 | if (user.empty() || password.empty()) { |
| 273 | NOTIMPLEMENTED() << ": Missing credentials:" |
| 274 | << (user.empty() ? " no-user" : "") |
| 275 | << (password.empty() ? " no-password" : ""); |
| 276 | driver_->Cleanup(Service::kStateFailure); |
| 277 | return; |
| 278 | } |
| 279 | SendUsername(tag, user); |
| 280 | SendPassword(tag, password); |
| 281 | } |
| 282 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 283 | void OpenVPNManagementServer::SupplyTPMToken(const string &tag) { |
Darin Petkov | 92e6561 | 2012-06-10 12:52:10 +0200 | [diff] [blame] | 284 | SLOG(VPN, 2) << __func__ << "(" << tag << ")"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 285 | string pin = driver_->args()->LookupString(flimflam::kOpenVPNPinProperty, ""); |
| 286 | if (pin.empty()) { |
Darin Petkov | 0440b9b | 2012-04-17 16:11:56 +0200 | [diff] [blame] | 287 | NOTIMPLEMENTED() << ": Missing PIN."; |
| 288 | driver_->Cleanup(Service::kStateFailure); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 289 | return; |
| 290 | } |
| 291 | SendPassword(tag, pin); |
| 292 | } |
| 293 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 294 | bool OpenVPNManagementServer::ProcessFailedPasswordMessage( |
| 295 | const string &message) { |
| 296 | if (!StartsWithASCII(message, ">PASSWORD:Verification Failed:", true)) { |
| 297 | return false; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 298 | } |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 299 | NOTIMPLEMENTED(); |
Darin Petkov | 0440b9b | 2012-04-17 16:11:56 +0200 | [diff] [blame] | 300 | driver_->Cleanup(Service::kStateFailure); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | |
| 304 | // >STATE:* message support. State messages are of the form: |
| 305 | // >STATE:<date>,<state>,<detail>,<local-ip>,<remote-ip> |
| 306 | // where: |
| 307 | // <date> is the current time (since epoch) in seconds |
| 308 | // <state> is one of: |
| 309 | // INITIAL, CONNECTING, WAIT, AUTH, GET_CONFIG, ASSIGN_IP, ADD_ROUTES, |
| 310 | // CONNECTED, RECONNECTING, EXITING, RESOLVE, TCP_CONNECT |
| 311 | // <detail> is a free-form string giving details about the state change |
| 312 | // <local-ip> is a dotted-quad for the local IPv4 address (when available) |
| 313 | // <remote-ip> is a dotted-quad for the remote IPv4 address (when available) |
| 314 | bool OpenVPNManagementServer::ProcessStateMessage(const string &message) { |
| 315 | if (!StartsWithASCII(message, ">STATE:", true)) { |
| 316 | return false; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 317 | } |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 318 | vector<string> details; |
| 319 | SplitString(message, ',', &details); |
| 320 | if (details.size() > 1) { |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 321 | LOG(INFO) << "Processing state message: " << details[1]; |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 322 | if (details[1] == "RECONNECTING") { |
| 323 | driver_->OnReconnecting(); |
| 324 | } |
| 325 | // The rest of the states are currently ignored. |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 326 | } |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 327 | return true; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 330 | bool OpenVPNManagementServer::ProcessHoldMessage(const string &message) { |
| 331 | if (!StartsWithASCII(message, ">HOLD:Waiting for hold release", true)) { |
| 332 | return false; |
| 333 | } |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 334 | LOG(INFO) << "Client waiting for hold release."; |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 335 | hold_waiting_ = true; |
| 336 | if (hold_release_) { |
| 337 | ReleaseHold(); |
| 338 | } |
| 339 | return true; |
| 340 | } |
| 341 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 342 | bool OpenVPNManagementServer::ProcessSuccessMessage(const string &message) { |
| 343 | if (!StartsWithASCII(message, "SUCCESS: ", true)) { |
| 344 | return false; |
| 345 | } |
| 346 | LOG(INFO) << message; |
| 347 | return true; |
| 348 | } |
| 349 | |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 350 | // static |
| 351 | string OpenVPNManagementServer::EscapeToQuote(const string &str) { |
| 352 | string escaped; |
| 353 | for (string::const_iterator it = str.begin(); it != str.end(); ++it) { |
| 354 | if (*it == '\\' || *it == '"') { |
| 355 | escaped += '\\'; |
| 356 | } |
| 357 | escaped += *it; |
| 358 | } |
| 359 | return escaped; |
| 360 | } |
| 361 | |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 362 | void OpenVPNManagementServer::Send(const string &data) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 363 | SLOG(VPN, 2) << __func__; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 364 | ssize_t len = sockets_->Send(connected_socket_, data.data(), data.size(), 0); |
| 365 | PLOG_IF(ERROR, len < 0 || static_cast<size_t>(len) != data.size()) |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 366 | << "Send failed."; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void OpenVPNManagementServer::SendState(const string &state) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 370 | SLOG(VPN, 2) << __func__ << "(" << state << ")"; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 371 | Send(StringPrintf("state %s\n", state.c_str())); |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 372 | } |
| 373 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 374 | void OpenVPNManagementServer::SendUsername(const string &tag, |
| 375 | const string &username) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 376 | SLOG(VPN, 2) << __func__; |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 377 | Send(StringPrintf("username \"%s\" %s\n", tag.c_str(), username.c_str())); |
| 378 | } |
| 379 | |
| 380 | void OpenVPNManagementServer::SendPassword(const string &tag, |
| 381 | const string &password) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 382 | SLOG(VPN, 2) << __func__; |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 383 | Send(StringPrintf("password \"%s\" \"%s\"\n", |
| 384 | tag.c_str(), |
| 385 | EscapeToQuote(password).c_str())); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 386 | } |
| 387 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 388 | void OpenVPNManagementServer::SendSignal(const string &signal) { |
| 389 | SLOG(VPN, 2) << __func__ << "(" << signal << ")"; |
| 390 | Send(StringPrintf("signal %s\n", signal.c_str())); |
| 391 | } |
| 392 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 393 | void OpenVPNManagementServer::SendHoldRelease() { |
| 394 | SLOG(VPN, 2) << __func__; |
| 395 | Send("hold release\n"); |
| 396 | } |
| 397 | |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 398 | } // namespace shill |