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