Darin Petkov | 33af05c | 2012-02-28 10:10:30 +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_driver.h" |
| 6 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 7 | #include <arpa/inet.h> |
| 8 | |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 9 | #include <base/file_util.h> |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 10 | #include <base/string_number_conversions.h> |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 11 | #include <base/string_split.h> |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 12 | #include <base/string_util.h> |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
| 14 | |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 15 | #include "shill/certificate_file.h" |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 16 | #include "shill/connection.h" |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 17 | #include "shill/device_info.h" |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 18 | #include "shill/dhcp_config.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 19 | #include "shill/error.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 20 | #include "shill/logging.h" |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 21 | #include "shill/manager.h" |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 22 | #include "shill/nss.h" |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 23 | #include "shill/openvpn_management_server.h" |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 24 | #include "shill/process_killer.h" |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 25 | #include "shill/rpc_task.h" |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 26 | #include "shill/sockets.h" |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 27 | #include "shill/virtual_device.h" |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 28 | #include "shill/vpn_service.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 29 | |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 30 | using base::Closure; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 31 | using base::FilePath; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 32 | using base::SplitString; |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 33 | using base::Unretained; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 34 | using base::WeakPtr; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 35 | using std::map; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 36 | using std::string; |
| 37 | using std::vector; |
| 38 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 39 | namespace shill { |
| 40 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 41 | namespace { |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 42 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 43 | const char kOpenVPNForeignOptionPrefix[] = "foreign_option_"; |
| 44 | const char kOpenVPNIfconfigBroadcast[] = "ifconfig_broadcast"; |
| 45 | const char kOpenVPNIfconfigLocal[] = "ifconfig_local"; |
| 46 | const char kOpenVPNIfconfigNetmask[] = "ifconfig_netmask"; |
| 47 | const char kOpenVPNIfconfigRemote[] = "ifconfig_remote"; |
| 48 | const char kOpenVPNRouteOptionPrefix[] = "route_"; |
| 49 | const char kOpenVPNRouteVPNGateway[] = "route_vpn_gateway"; |
| 50 | const char kOpenVPNTrustedIP[] = "trusted_ip"; |
| 51 | const char kOpenVPNTunMTU[] = "tun_mtu"; |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 52 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 53 | const char kDefaultPKCS11Provider[] = "libchaps.so"; |
| 54 | |
Paul Stewart | 4698c1a | 2013-05-16 15:42:19 -0700 | [diff] [blame] | 55 | // Some configurations pass the netmask in the ifconfig_remote property. |
| 56 | // This is due to some servers not explicitly indicating that they are using |
| 57 | // a "broadcast mode" network instead of peer-to-peer. See |
| 58 | // http://crbug.com/241264 for an example of this issue. |
| 59 | const char kSuspectedNetmaskPrefix[] = "255."; |
| 60 | |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 61 | } // namespace |
| 62 | |
| 63 | // static |
Darin Petkov | c418b4b | 2012-10-05 11:42:52 +0200 | [diff] [blame] | 64 | const char OpenVPNDriver::kDefaultCACertificates[] = |
| 65 | "/etc/ssl/certs/ca-certificates.crt"; |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 66 | // static |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 67 | const char OpenVPNDriver::kOpenVPNPath[] = "/usr/sbin/openvpn"; |
| 68 | // static |
Darin Petkov | 728faa9 | 2012-10-12 11:25:47 +0200 | [diff] [blame] | 69 | const char OpenVPNDriver::kOpenVPNScript[] = SHIMDIR "/openvpn-script"; |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 70 | // static |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 71 | const VPNDriver::Property OpenVPNDriver::kProperties[] = { |
Darin Petkov | 1847d79 | 2012-04-17 11:33:06 +0200 | [diff] [blame] | 72 | { flimflam::kOpenVPNAuthNoCacheProperty, 0 }, |
| 73 | { flimflam::kOpenVPNAuthProperty, 0 }, |
| 74 | { flimflam::kOpenVPNAuthRetryProperty, 0 }, |
| 75 | { flimflam::kOpenVPNAuthUserPassProperty, 0 }, |
| 76 | { flimflam::kOpenVPNCaCertNSSProperty, 0 }, |
| 77 | { flimflam::kOpenVPNCaCertProperty, 0 }, |
| 78 | { flimflam::kOpenVPNCipherProperty, 0 }, |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 79 | { flimflam::kOpenVPNClientCertIdProperty, Property::kCredential }, |
Darin Petkov | 1847d79 | 2012-04-17 11:33:06 +0200 | [diff] [blame] | 80 | { flimflam::kOpenVPNCompLZOProperty, 0 }, |
| 81 | { flimflam::kOpenVPNCompNoAdaptProperty, 0 }, |
| 82 | { flimflam::kOpenVPNKeyDirectionProperty, 0 }, |
| 83 | { flimflam::kOpenVPNNsCertTypeProperty, 0 }, |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 84 | { flimflam::kOpenVPNOTPProperty, |
Darin Petkov | 0223655 | 2012-06-11 13:15:19 +0200 | [diff] [blame] | 85 | Property::kEphemeral | Property::kCredential | Property::kWriteOnly }, |
| 86 | { flimflam::kOpenVPNPasswordProperty, |
| 87 | Property::kCredential | Property::kWriteOnly }, |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 88 | { flimflam::kOpenVPNPinProperty, Property::kCredential }, |
Darin Petkov | 1847d79 | 2012-04-17 11:33:06 +0200 | [diff] [blame] | 89 | { flimflam::kOpenVPNPortProperty, 0 }, |
| 90 | { flimflam::kOpenVPNProtoProperty, 0 }, |
| 91 | { flimflam::kOpenVPNProviderProperty, 0 }, |
| 92 | { flimflam::kOpenVPNPushPeerInfoProperty, 0 }, |
| 93 | { flimflam::kOpenVPNRemoteCertEKUProperty, 0 }, |
| 94 | { flimflam::kOpenVPNRemoteCertKUProperty, 0 }, |
| 95 | { flimflam::kOpenVPNRemoteCertTLSProperty, 0 }, |
| 96 | { flimflam::kOpenVPNRenegSecProperty, 0 }, |
| 97 | { flimflam::kOpenVPNServerPollTimeoutProperty, 0 }, |
| 98 | { flimflam::kOpenVPNShaperProperty, 0 }, |
| 99 | { flimflam::kOpenVPNStaticChallengeProperty, 0 }, |
| 100 | { flimflam::kOpenVPNTLSAuthContentsProperty, 0 }, |
| 101 | { flimflam::kOpenVPNTLSRemoteProperty, 0 }, |
| 102 | { flimflam::kOpenVPNUserProperty, 0 }, |
| 103 | { flimflam::kProviderHostProperty, 0 }, |
Darin Petkov | 1847d79 | 2012-04-17 11:33:06 +0200 | [diff] [blame] | 104 | { flimflam::kProviderTypeProperty, 0 }, |
Paul Stewart | 0f9c930 | 2013-06-14 15:41:28 -0700 | [diff] [blame] | 105 | { kOpenVPNCaCertPemProperty, Property::kArray }, |
Darin Petkov | 1847d79 | 2012-04-17 11:33:06 +0200 | [diff] [blame] | 106 | { kOpenVPNCertProperty, 0 }, |
Paul Stewart | f3b6d57 | 2013-06-17 09:35:33 -0700 | [diff] [blame] | 107 | { kOpenVPNExtraCertPemProperty, Property::kArray }, |
Darin Petkov | 1847d79 | 2012-04-17 11:33:06 +0200 | [diff] [blame] | 108 | { kOpenVPNKeyProperty, 0 }, |
| 109 | { kOpenVPNPingExitProperty, 0 }, |
| 110 | { kOpenVPNPingProperty, 0 }, |
| 111 | { kOpenVPNPingRestartProperty, 0 }, |
| 112 | { kOpenVPNTLSAuthProperty, 0 }, |
| 113 | { kOpenVPNVerbProperty, 0 }, |
| 114 | { kVPNMTUProperty, 0 }, |
Paul Stewart | 2280799 | 2012-04-11 08:48:31 -0700 | [diff] [blame] | 115 | |
| 116 | // Provided only for compatibility. crosbug.com/29286 |
Darin Petkov | 1847d79 | 2012-04-17 11:33:06 +0200 | [diff] [blame] | 117 | { flimflam::kOpenVPNMgmtEnableProperty, 0 }, |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 118 | }; |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 119 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 120 | const char OpenVPNDriver::kLSBReleaseFile[] = "/etc/lsb-release"; |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 121 | const char OpenVPNDriver::kChromeOSReleaseName[] = "CHROMEOS_RELEASE_NAME"; |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 122 | const char OpenVPNDriver::kChromeOSReleaseVersion[] = |
| 123 | "CHROMEOS_RELEASE_VERSION"; |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 124 | const int OpenVPNDriver::kReconnectOfflineTimeoutSeconds = 2 * 60; |
| 125 | const int OpenVPNDriver::kReconnectTLSErrorTimeoutSeconds = 20; |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 126 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 127 | OpenVPNDriver::OpenVPNDriver(ControlInterface *control, |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 128 | EventDispatcher *dispatcher, |
| 129 | Metrics *metrics, |
| 130 | Manager *manager, |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 131 | DeviceInfo *device_info, |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 132 | GLib *glib) |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 133 | : VPNDriver(dispatcher, manager, kProperties, arraysize(kProperties)), |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 134 | control_(control), |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 135 | metrics_(metrics), |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 136 | device_info_(device_info), |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 137 | glib_(glib), |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 138 | management_server_(new OpenVPNManagementServer(this, glib)), |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 139 | nss_(NSS::GetInstance()), |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 140 | certificate_file_(new CertificateFile(glib)), |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 141 | process_killer_(ProcessKiller::GetInstance()), |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 142 | lsb_release_file_(kLSBReleaseFile), |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 143 | pid_(0), |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 144 | child_watch_tag_(0), |
| 145 | default_service_callback_tag_(0) {} |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 146 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 147 | OpenVPNDriver::~OpenVPNDriver() { |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 148 | IdleService(); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 151 | void OpenVPNDriver::IdleService() { |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 152 | Cleanup(Service::kStateIdle, |
| 153 | Service::kFailureUnknown, |
| 154 | Service::kErrorDetailsNone); |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 157 | void OpenVPNDriver::FailService(Service::ConnectFailure failure, |
| 158 | const string &error_details) { |
| 159 | Cleanup(Service::kStateFailure, failure, error_details); |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void OpenVPNDriver::Cleanup(Service::ConnectState state, |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 163 | Service::ConnectFailure failure, |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 164 | const string &error_details) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 165 | SLOG(VPN, 2) << __func__ << "(" << Service::ConnectStateToString(state) |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 166 | << ", " << error_details << ")"; |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 167 | StopConnectTimeout(); |
Darin Petkov | 84a9553 | 2013-03-13 12:24:45 +0100 | [diff] [blame] | 168 | if (child_watch_tag_) { |
| 169 | glib_->SourceRemove(child_watch_tag_); |
| 170 | child_watch_tag_ = 0; |
| 171 | } |
| 172 | // Disconnecting the management interface will terminate the openvpn |
| 173 | // process. Ensure this is handled robustly by first removing the child watch |
| 174 | // above and then terminating and reaping the process through ProcessKiller. |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 175 | management_server_->Stop(); |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 176 | if (!tls_auth_file_.empty()) { |
| 177 | file_util::Delete(tls_auth_file_, false); |
| 178 | tls_auth_file_.clear(); |
| 179 | } |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 180 | if (default_service_callback_tag_) { |
| 181 | manager()->DeregisterDefaultServiceCallback(default_service_callback_tag_); |
| 182 | default_service_callback_tag_ = 0; |
| 183 | } |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 184 | rpc_task_.reset(); |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 185 | int interface_index = -1; |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 186 | if (device_) { |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 187 | interface_index = device_->interface_index(); |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 188 | device_->DropConnection(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 189 | device_->SetEnabled(false); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 190 | device_ = NULL; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 191 | } |
| 192 | if (pid_) { |
| 193 | Closure callback; |
| 194 | if (interface_index >= 0) { |
| 195 | callback = |
| 196 | Bind(&DeleteInterface, device_info_->AsWeakPtr(), interface_index); |
| 197 | interface_index = -1; |
| 198 | } |
| 199 | process_killer_->Kill(pid_, callback); |
| 200 | pid_ = 0; |
| 201 | } |
| 202 | if (interface_index >= 0) { |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 203 | device_info_->DeleteInterface(interface_index); |
| 204 | } |
| 205 | tunnel_interface_.clear(); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 206 | if (service_) { |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 207 | if (state == Service::kStateFailure) { |
| 208 | service_->SetErrorDetails(error_details); |
| 209 | service_->SetFailure(failure); |
| 210 | } else { |
| 211 | service_->SetState(state); |
| 212 | } |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 213 | service_ = NULL; |
| 214 | } |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 215 | ip_properties_ = IPConfig::Properties(); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | bool OpenVPNDriver::SpawnOpenVPN() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 219 | SLOG(VPN, 2) << __func__ << "(" << tunnel_interface_ << ")"; |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 220 | |
| 221 | vector<string> options; |
| 222 | Error error; |
| 223 | InitOptions(&options, &error); |
| 224 | if (error.IsFailure()) { |
| 225 | return false; |
| 226 | } |
Darin Petkov | 728faa9 | 2012-10-12 11:25:47 +0200 | [diff] [blame] | 227 | LOG(INFO) << "OpenVPN process options: " << JoinString(options, ' '); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 228 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 229 | // TODO(quiche): This should be migrated to use ExternalTask. |
| 230 | // (crbug.com/246263). |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 231 | vector<char *> process_args; |
| 232 | process_args.push_back(const_cast<char *>(kOpenVPNPath)); |
| 233 | for (vector<string>::const_iterator it = options.begin(); |
| 234 | it != options.end(); ++it) { |
| 235 | process_args.push_back(const_cast<char *>(it->c_str())); |
| 236 | } |
| 237 | process_args.push_back(NULL); |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 238 | |
| 239 | vector<string> environment; |
| 240 | InitEnvironment(&environment); |
| 241 | |
| 242 | vector<char *> process_env; |
| 243 | for (vector<string>::const_iterator it = environment.begin(); |
| 244 | it != environment.end(); ++it) { |
| 245 | process_env.push_back(const_cast<char *>(it->c_str())); |
| 246 | } |
| 247 | process_env.push_back(NULL); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 248 | |
| 249 | CHECK(!pid_); |
Darin Petkov | 68710d7 | 2013-02-13 14:22:56 +0100 | [diff] [blame] | 250 | if (!glib_->SpawnAsync(NULL, |
| 251 | process_args.data(), |
| 252 | process_env.data(), |
| 253 | G_SPAWN_DO_NOT_REAP_CHILD, |
| 254 | NULL, |
| 255 | NULL, |
| 256 | &pid_, |
| 257 | NULL)) { |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 258 | LOG(ERROR) << "Unable to spawn: " << kOpenVPNPath; |
| 259 | return false; |
| 260 | } |
| 261 | CHECK(!child_watch_tag_); |
| 262 | child_watch_tag_ = glib_->ChildWatchAdd(pid_, OnOpenVPNDied, this); |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | // static |
| 267 | void OpenVPNDriver::OnOpenVPNDied(GPid pid, gint status, gpointer data) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 268 | SLOG(VPN, 2) << __func__ << "(" << pid << ", " << status << ")"; |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 269 | OpenVPNDriver *me = reinterpret_cast<OpenVPNDriver *>(data); |
| 270 | me->child_watch_tag_ = 0; |
| 271 | CHECK_EQ(pid, me->pid_); |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 272 | me->pid_ = 0; |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 273 | me->FailService(Service::kFailureInternal, Service::kErrorDetailsNone); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 274 | // TODO(petkov): Figure if we need to restart the connection. |
| 275 | } |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 276 | |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 277 | // static |
Darin Petkov | 5dbd261 | 2012-06-07 16:22:16 +0200 | [diff] [blame] | 278 | void OpenVPNDriver::DeleteInterface(const WeakPtr<DeviceInfo> &device_info, |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 279 | int interface_index) { |
| 280 | if (device_info) { |
| 281 | LOG(INFO) << "Deleting interface " << interface_index; |
| 282 | device_info->DeleteInterface(interface_index); |
| 283 | } |
| 284 | } |
| 285 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 286 | bool OpenVPNDriver::ClaimInterface(const string &link_name, |
| 287 | int interface_index) { |
| 288 | if (link_name != tunnel_interface_) { |
| 289 | return false; |
| 290 | } |
| 291 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 292 | SLOG(VPN, 2) << "Claiming " << link_name << " for OpenVPN tunnel"; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 293 | |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 294 | CHECK(!device_); |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 295 | device_ = new VirtualDevice(control_, dispatcher(), metrics_, manager(), |
| 296 | link_name, interface_index, Technology::kVPN); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 297 | device_->SetEnabled(true); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 298 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 299 | rpc_task_.reset(new RPCTask(control_, this)); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 300 | if (SpawnOpenVPN()) { |
Darin Petkov | 68710d7 | 2013-02-13 14:22:56 +0100 | [diff] [blame] | 301 | default_service_callback_tag_ = |
| 302 | manager()->RegisterDefaultServiceCallback( |
| 303 | Bind(&OpenVPNDriver::OnDefaultServiceChanged, Unretained(this))); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 304 | } else { |
| 305 | FailService(Service::kFailureInternal, Service::kErrorDetailsNone); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 306 | } |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 307 | return true; |
| 308 | } |
| 309 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 310 | void OpenVPNDriver::GetLogin(string */*user*/, string */*password*/) { |
| 311 | NOTREACHED(); |
| 312 | } |
| 313 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 314 | void OpenVPNDriver::Notify(const string &reason, |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 315 | const map<string, string> &dict) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 316 | LOG(INFO) << "IP configuration received: " << reason; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 317 | if (reason != "up") { |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 318 | device_->DropConnection(); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 319 | return; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 320 | } |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 321 | // On restart/reconnect, update the existing IP configuration. |
| 322 | ParseIPConfiguration(dict, &ip_properties_); |
| 323 | device_->SelectService(service_); |
| 324 | device_->UpdateIPConfig(ip_properties_); |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 325 | ReportConnectionMetrics(); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 326 | StopConnectTimeout(); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // static |
| 330 | void OpenVPNDriver::ParseIPConfiguration( |
| 331 | const map<string, string> &configuration, |
| 332 | IPConfig::Properties *properties) { |
| 333 | ForeignOptions foreign_options; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 334 | RouteOptions routes; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 335 | properties->address_family = IPAddress::kFamilyIPv4; |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 336 | if (!properties->subnet_prefix) { |
| 337 | properties->subnet_prefix = |
| 338 | IPAddress::GetMaxPrefixLength(properties->address_family); |
| 339 | } |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 340 | for (map<string, string>::const_iterator it = configuration.begin(); |
| 341 | it != configuration.end(); ++it) { |
| 342 | const string &key = it->first; |
| 343 | const string &value = it->second; |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 344 | SLOG(VPN, 2) << "Processing: " << key << " -> " << value; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 345 | if (LowerCaseEqualsASCII(key, kOpenVPNIfconfigLocal)) { |
| 346 | properties->address = value; |
| 347 | } else if (LowerCaseEqualsASCII(key, kOpenVPNIfconfigBroadcast)) { |
| 348 | properties->broadcast_address = value; |
| 349 | } else if (LowerCaseEqualsASCII(key, kOpenVPNIfconfigNetmask)) { |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 350 | properties->subnet_prefix = |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 351 | IPAddress::GetPrefixLengthFromMask(properties->address_family, value); |
| 352 | } else if (LowerCaseEqualsASCII(key, kOpenVPNIfconfigRemote)) { |
Paul Stewart | 4698c1a | 2013-05-16 15:42:19 -0700 | [diff] [blame] | 353 | if (StartsWithASCII(value, kSuspectedNetmaskPrefix, false)) { |
| 354 | LOG(WARNING) << "Option " << key << " value " << value |
| 355 | << " looks more like a netmask than a peer address; " |
| 356 | << "assuming it is the former."; |
| 357 | // In this situation, the "peer_address" value will be left |
| 358 | // unset and Connection::UpdateFromIPConfig() will treat the |
| 359 | // interface as if it were a broadcast-style network. The |
| 360 | // kernel will, automatically set the peer address equal to |
| 361 | // the local address. |
| 362 | properties->subnet_prefix = |
| 363 | IPAddress::GetPrefixLengthFromMask(properties->address_family, |
| 364 | value); |
| 365 | } else { |
| 366 | properties->peer_address = value; |
| 367 | } |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 368 | } else if (LowerCaseEqualsASCII(key, kOpenVPNRouteVPNGateway)) { |
| 369 | properties->gateway = value; |
| 370 | } else if (LowerCaseEqualsASCII(key, kOpenVPNTrustedIP)) { |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 371 | properties->trusted_ip = value; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 372 | } else if (LowerCaseEqualsASCII(key, kOpenVPNTunMTU)) { |
| 373 | int mtu = 0; |
| 374 | if (base::StringToInt(value, &mtu) && mtu >= DHCPConfig::kMinMTU) { |
| 375 | properties->mtu = mtu; |
| 376 | } else { |
| 377 | LOG(ERROR) << "MTU " << value << " ignored."; |
| 378 | } |
| 379 | } else if (StartsWithASCII(key, kOpenVPNForeignOptionPrefix, false)) { |
| 380 | const string suffix = key.substr(strlen(kOpenVPNForeignOptionPrefix)); |
| 381 | int order = 0; |
| 382 | if (base::StringToInt(suffix, &order)) { |
| 383 | foreign_options[order] = value; |
| 384 | } else { |
| 385 | LOG(ERROR) << "Ignored unexpected foreign option suffix: " << suffix; |
| 386 | } |
| 387 | } else if (StartsWithASCII(key, kOpenVPNRouteOptionPrefix, false)) { |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 388 | ParseRouteOption(key.substr(strlen(kOpenVPNRouteOptionPrefix)), |
| 389 | value, &routes); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 390 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 391 | SLOG(VPN, 2) << "Key ignored."; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 392 | } |
| 393 | } |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 394 | ParseForeignOptions(foreign_options, properties); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 395 | SetRoutes(routes, properties); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | // static |
| 399 | void OpenVPNDriver::ParseForeignOptions(const ForeignOptions &options, |
| 400 | IPConfig::Properties *properties) { |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 401 | vector<string> domain_search; |
| 402 | vector<string> dns_servers; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 403 | for (ForeignOptions::const_iterator it = options.begin(); |
| 404 | it != options.end(); ++it) { |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 405 | ParseForeignOption(it->second, &domain_search, &dns_servers); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 406 | } |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 407 | if (!domain_search.empty()) { |
| 408 | properties->domain_search.swap(domain_search); |
| 409 | } |
| 410 | LOG_IF(WARNING, properties->domain_search.empty()) |
| 411 | << "No search domains provided."; |
| 412 | if (!dns_servers.empty()) { |
| 413 | properties->dns_servers.swap(dns_servers); |
| 414 | } |
| 415 | LOG_IF(WARNING, properties->dns_servers.empty()) |
| 416 | << "No DNS servers provided."; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | // static |
| 420 | void OpenVPNDriver::ParseForeignOption(const string &option, |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 421 | vector<string> *domain_search, |
| 422 | vector<string> *dns_servers) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 423 | SLOG(VPN, 2) << __func__ << "(" << option << ")"; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 424 | vector<string> tokens; |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 425 | SplitString(option, ' ', &tokens); |
| 426 | if (tokens.size() != 3 || !LowerCaseEqualsASCII(tokens[0], "dhcp-option")) { |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 427 | return; |
| 428 | } |
| 429 | if (LowerCaseEqualsASCII(tokens[1], "domain")) { |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 430 | domain_search->push_back(tokens[2]); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 431 | } else if (LowerCaseEqualsASCII(tokens[1], "dns")) { |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 432 | dns_servers->push_back(tokens[2]); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 436 | // static |
| 437 | IPConfig::Route *OpenVPNDriver::GetRouteOptionEntry( |
| 438 | const string &prefix, const string &key, RouteOptions *routes) { |
| 439 | int order = 0; |
| 440 | if (!StartsWithASCII(key, prefix, false) || |
| 441 | !base::StringToInt(key.substr(prefix.size()), &order)) { |
| 442 | return NULL; |
| 443 | } |
| 444 | return &(*routes)[order]; |
| 445 | } |
| 446 | |
| 447 | // static |
| 448 | void OpenVPNDriver::ParseRouteOption( |
| 449 | const string &key, const string &value, RouteOptions *routes) { |
| 450 | IPConfig::Route *route = GetRouteOptionEntry("network_", key, routes); |
| 451 | if (route) { |
| 452 | route->host = value; |
| 453 | return; |
| 454 | } |
| 455 | route = GetRouteOptionEntry("netmask_", key, routes); |
| 456 | if (route) { |
| 457 | route->netmask = value; |
| 458 | return; |
| 459 | } |
| 460 | route = GetRouteOptionEntry("gateway_", key, routes); |
| 461 | if (route) { |
| 462 | route->gateway = value; |
| 463 | return; |
| 464 | } |
| 465 | LOG(WARNING) << "Unknown route option ignored: " << key; |
| 466 | } |
| 467 | |
| 468 | // static |
| 469 | void OpenVPNDriver::SetRoutes(const RouteOptions &routes, |
| 470 | IPConfig::Properties *properties) { |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 471 | vector<IPConfig::Route> new_routes; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 472 | for (RouteOptions::const_iterator it = routes.begin(); |
| 473 | it != routes.end(); ++it) { |
| 474 | const IPConfig::Route &route = it->second; |
| 475 | if (route.host.empty() || route.netmask.empty() || route.gateway.empty()) { |
| 476 | LOG(WARNING) << "Ignoring incomplete route: " << it->first; |
| 477 | continue; |
| 478 | } |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 479 | new_routes.push_back(route); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 480 | } |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 481 | if (!new_routes.empty()) { |
| 482 | properties->routes.swap(new_routes); |
| 483 | } |
| 484 | LOG_IF(WARNING, properties->routes.empty()) << "No routes provided."; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Darin Petkov | 4b94484 | 2012-09-21 10:48:48 +0200 | [diff] [blame] | 487 | // static |
| 488 | bool OpenVPNDriver::SplitPortFromHost( |
| 489 | const string &host, string *name, string *port) { |
| 490 | vector<string> tokens; |
| 491 | SplitString(host, ':', &tokens); |
| 492 | int port_number = 0; |
| 493 | if (tokens.size() != 2 || tokens[0].empty() || tokens[1].empty() || |
| 494 | !IsAsciiDigit(tokens[1][0]) || |
| 495 | !base::StringToInt(tokens[1], &port_number) || port_number > kuint16max) { |
| 496 | return false; |
| 497 | } |
| 498 | *name = tokens[0]; |
| 499 | *port = tokens[1]; |
| 500 | return true; |
| 501 | } |
| 502 | |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 503 | void OpenVPNDriver::Connect(const VPNServiceRefPtr &service, Error *error) { |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 504 | StartConnectTimeout(kDefaultConnectTimeoutSeconds); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 505 | service_ = service; |
| 506 | service_->SetState(Service::kStateConfiguring); |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 507 | if (!device_info_->CreateTunnelInterface(&tunnel_interface_)) { |
| 508 | Error::PopulateAndLog( |
| 509 | error, Error::kInternalError, "Could not create tunnel interface."); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 510 | FailService(Service::kFailureInternal, Service::kErrorDetailsNone); |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 511 | } |
| 512 | // Wait for the ClaimInterface callback to continue the connection process. |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 515 | void OpenVPNDriver::InitOptions(vector<string> *options, Error *error) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 516 | string vpnhost = args()->LookupString(flimflam::kProviderHostProperty, ""); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 517 | if (vpnhost.empty()) { |
| 518 | Error::PopulateAndLog( |
| 519 | error, Error::kInvalidArguments, "VPN host not specified."); |
| 520 | return; |
| 521 | } |
| 522 | options->push_back("--client"); |
| 523 | options->push_back("--tls-client"); |
Darin Petkov | 4b94484 | 2012-09-21 10:48:48 +0200 | [diff] [blame] | 524 | |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 525 | options->push_back("--remote"); |
Darin Petkov | 4b94484 | 2012-09-21 10:48:48 +0200 | [diff] [blame] | 526 | string host_name, host_port; |
| 527 | if (SplitPortFromHost(vpnhost, &host_name, &host_port)) { |
| 528 | DCHECK(!host_name.empty()); |
| 529 | DCHECK(!host_port.empty()); |
| 530 | options->push_back(host_name); |
| 531 | options->push_back(host_port); |
| 532 | } else { |
| 533 | options->push_back(vpnhost); |
| 534 | } |
| 535 | |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 536 | options->push_back("--nobind"); |
| 537 | options->push_back("--persist-key"); |
| 538 | options->push_back("--persist-tun"); |
| 539 | |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 540 | CHECK(!tunnel_interface_.empty()); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 541 | options->push_back("--dev"); |
| 542 | options->push_back(tunnel_interface_); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 543 | options->push_back("--dev-type"); |
| 544 | options->push_back("tun"); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 545 | |
Darin Petkov | 55771b7 | 2012-04-25 09:25:19 +0200 | [diff] [blame] | 546 | InitLoggingOptions(options); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 547 | |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 548 | AppendValueOption(kVPNMTUProperty, "--mtu", options); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 549 | AppendValueOption(flimflam::kOpenVPNProtoProperty, "--proto", options); |
| 550 | AppendValueOption(flimflam::kOpenVPNPortProperty, "--port", options); |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 551 | AppendValueOption(kOpenVPNTLSAuthProperty, "--tls-auth", options); |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 552 | { |
| 553 | string contents = |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 554 | args()->LookupString(flimflam::kOpenVPNTLSAuthContentsProperty, ""); |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 555 | if (!contents.empty()) { |
| 556 | if (!file_util::CreateTemporaryFile(&tls_auth_file_) || |
| 557 | file_util::WriteFile( |
| 558 | tls_auth_file_, contents.data(), contents.size()) != |
| 559 | static_cast<int>(contents.size())) { |
| 560 | Error::PopulateAndLog( |
| 561 | error, Error::kInternalError, "Unable to setup tls-auth file."); |
| 562 | return; |
| 563 | } |
| 564 | options->push_back("--tls-auth"); |
| 565 | options->push_back(tls_auth_file_.value()); |
| 566 | } |
| 567 | } |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 568 | AppendValueOption( |
| 569 | flimflam::kOpenVPNTLSRemoteProperty, "--tls-remote", options); |
| 570 | AppendValueOption(flimflam::kOpenVPNCipherProperty, "--cipher", options); |
| 571 | AppendValueOption(flimflam::kOpenVPNAuthProperty, "--auth", options); |
| 572 | AppendFlag(flimflam::kOpenVPNAuthNoCacheProperty, "--auth-nocache", options); |
| 573 | AppendValueOption( |
| 574 | flimflam::kOpenVPNAuthRetryProperty, "--auth-retry", options); |
| 575 | AppendFlag(flimflam::kOpenVPNCompLZOProperty, "--comp-lzo", options); |
| 576 | AppendFlag(flimflam::kOpenVPNCompNoAdaptProperty, "--comp-noadapt", options); |
| 577 | AppendFlag( |
| 578 | flimflam::kOpenVPNPushPeerInfoProperty, "--push-peer-info", options); |
| 579 | AppendValueOption(flimflam::kOpenVPNRenegSecProperty, "--reneg-sec", options); |
| 580 | AppendValueOption(flimflam::kOpenVPNShaperProperty, "--shaper", options); |
| 581 | AppendValueOption(flimflam::kOpenVPNServerPollTimeoutProperty, |
| 582 | "--server-poll-timeout", options); |
| 583 | |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 584 | if (!InitCAOptions(options, error)) { |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 585 | return; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 586 | } |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 587 | |
| 588 | // Client-side ping support. |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 589 | AppendValueOption(kOpenVPNPingProperty, "--ping", options); |
| 590 | AppendValueOption(kOpenVPNPingExitProperty, "--ping-exit", options); |
| 591 | AppendValueOption(kOpenVPNPingRestartProperty, "--ping-restart", options); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 592 | |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 593 | AppendValueOption( |
| 594 | flimflam::kOpenVPNNsCertTypeProperty, "--ns-cert-type", options); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 595 | |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 596 | InitClientAuthOptions(options); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 597 | InitPKCS11Options(options); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 598 | |
| 599 | // TLS suport. |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 600 | string remote_cert_tls = |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 601 | args()->LookupString(flimflam::kOpenVPNRemoteCertTLSProperty, ""); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 602 | if (remote_cert_tls.empty()) { |
| 603 | remote_cert_tls = "server"; |
| 604 | } |
| 605 | if (remote_cert_tls != "none") { |
| 606 | options->push_back("--remote-cert-tls"); |
| 607 | options->push_back(remote_cert_tls); |
| 608 | } |
| 609 | |
| 610 | // This is an undocumented command line argument that works like a .cfg file |
| 611 | // entry. TODO(sleffler): Maybe roll this into --tls-auth? |
| 612 | AppendValueOption( |
| 613 | flimflam::kOpenVPNKeyDirectionProperty, "--key-direction", options); |
| 614 | // TODO(sleffler): Support more than one eku parameter. |
| 615 | AppendValueOption( |
| 616 | flimflam::kOpenVPNRemoteCertEKUProperty, "--remote-cert-eku", options); |
| 617 | AppendValueOption( |
| 618 | flimflam::kOpenVPNRemoteCertKUProperty, "--remote-cert-ku", options); |
| 619 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 620 | if (!InitManagementChannelOptions(options, error)) { |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 621 | return; |
| 622 | } |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 623 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 624 | // Setup openvpn-script options and RPC information required to send back |
| 625 | // Layer 3 configuration. |
| 626 | options->push_back("--setenv"); |
Darin Petkov | 728faa9 | 2012-10-12 11:25:47 +0200 | [diff] [blame] | 627 | options->push_back(kRPCTaskServiceVariable); |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 628 | options->push_back(rpc_task_->GetRpcConnectionIdentifier()); |
| 629 | options->push_back("--setenv"); |
Darin Petkov | 728faa9 | 2012-10-12 11:25:47 +0200 | [diff] [blame] | 630 | options->push_back(kRPCTaskPathVariable); |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 631 | options->push_back(rpc_task_->GetRpcIdentifier()); |
| 632 | options->push_back("--script-security"); |
| 633 | options->push_back("2"); |
| 634 | options->push_back("--up"); |
| 635 | options->push_back(kOpenVPNScript); |
| 636 | options->push_back("--up-restart"); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 637 | |
| 638 | // Disable openvpn handling since we do route+ifconfig work. |
| 639 | options->push_back("--route-noexec"); |
| 640 | options->push_back("--ifconfig-noexec"); |
| 641 | |
| 642 | // Drop root privileges on connection and enable callback scripts to send |
| 643 | // notify messages. |
| 644 | options->push_back("--user"); |
| 645 | options->push_back("openvpn"); |
| 646 | options->push_back("--group"); |
| 647 | options->push_back("openvpn"); |
| 648 | } |
| 649 | |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 650 | bool OpenVPNDriver::InitCAOptions(vector<string> *options, Error *error) { |
Darin Petkov | c418b4b | 2012-10-05 11:42:52 +0200 | [diff] [blame] | 651 | options->push_back("--ca"); |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 652 | string ca_cert = |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 653 | args()->LookupString(flimflam::kOpenVPNCaCertProperty, ""); |
| 654 | string ca_cert_nss = |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 655 | args()->LookupString(flimflam::kOpenVPNCaCertNSSProperty, ""); |
Paul Stewart | 0f9c930 | 2013-06-14 15:41:28 -0700 | [diff] [blame] | 656 | vector<string> ca_cert_pem; |
| 657 | if (args()->ContainsStrings(kOpenVPNCaCertPemProperty)) { |
| 658 | ca_cert_pem = args()->GetStrings(kOpenVPNCaCertPemProperty); |
| 659 | } |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 660 | |
| 661 | int num_ca_cert_types = 0; |
| 662 | if (!ca_cert.empty()) |
| 663 | num_ca_cert_types++; |
| 664 | if (!ca_cert_nss.empty()) |
| 665 | num_ca_cert_types++; |
| 666 | if (!ca_cert_pem.empty()) |
| 667 | num_ca_cert_types++; |
| 668 | if (num_ca_cert_types == 0) { |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 669 | // Use default CAs if no CA certificate is provided. |
Darin Petkov | c418b4b | 2012-10-05 11:42:52 +0200 | [diff] [blame] | 670 | options->push_back(kDefaultCACertificates); |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 671 | return true; |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 672 | } else if (num_ca_cert_types > 1) { |
| 673 | Error::PopulateAndLog( |
| 674 | error, Error::kInvalidArguments, |
| 675 | "Can't specify more than one of CACert, CACertNSS and CACertPEM."); |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 676 | return false; |
| 677 | } |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 678 | string cert_file; |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 679 | if (!ca_cert_nss.empty()) { |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 680 | DCHECK(ca_cert.empty() && ca_cert_pem.empty()); |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 681 | const string &vpnhost = args()->GetString(flimflam::kProviderHostProperty); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 682 | vector<char> id(vpnhost.begin(), vpnhost.end()); |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 683 | FilePath certfile = nss_->GetPEMCertfile(ca_cert_nss, id); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 684 | if (certfile.empty()) { |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 685 | Error::PopulateAndLog( |
| 686 | error, |
| 687 | Error::kInvalidArguments, |
| 688 | "Unable to extract NSS CA certificate: " + ca_cert_nss); |
| 689 | return false; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 690 | } |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 691 | options->push_back(certfile.value()); |
| 692 | return true; |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 693 | } else if (!ca_cert_pem.empty()) { |
| 694 | DCHECK(ca_cert.empty() && ca_cert_nss.empty()); |
Paul Stewart | 0f9c930 | 2013-06-14 15:41:28 -0700 | [diff] [blame] | 695 | FilePath certfile = certificate_file_->CreatePEMFromStrings(ca_cert_pem); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 696 | if (certfile.empty()) { |
| 697 | Error::PopulateAndLog( |
| 698 | error, |
| 699 | Error::kInvalidArguments, |
Paul Stewart | 0f9c930 | 2013-06-14 15:41:28 -0700 | [diff] [blame] | 700 | "Unable to extract PEM CA certificates."); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 701 | return false; |
| 702 | } |
| 703 | options->push_back(certfile.value()); |
| 704 | return true; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 705 | } |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 706 | DCHECK(!ca_cert.empty() && ca_cert_nss.empty() && ca_cert_pem.empty()); |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 707 | options->push_back(ca_cert); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 708 | return true; |
| 709 | } |
| 710 | |
| 711 | void OpenVPNDriver::InitPKCS11Options(vector<string> *options) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 712 | string id = args()->LookupString(flimflam::kOpenVPNClientCertIdProperty, ""); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 713 | if (!id.empty()) { |
| 714 | string provider = |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 715 | args()->LookupString(flimflam::kOpenVPNProviderProperty, ""); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 716 | if (provider.empty()) { |
| 717 | provider = kDefaultPKCS11Provider; |
| 718 | } |
| 719 | options->push_back("--pkcs11-providers"); |
| 720 | options->push_back(provider); |
| 721 | options->push_back("--pkcs11-id"); |
| 722 | options->push_back(id); |
| 723 | } |
| 724 | } |
| 725 | |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 726 | void OpenVPNDriver::InitClientAuthOptions(vector<string> *options) { |
| 727 | bool has_cert = AppendValueOption(kOpenVPNCertProperty, "--cert", options); |
| 728 | bool has_key = AppendValueOption(kOpenVPNKeyProperty, "--key", options); |
| 729 | // If the AuthUserPass property is set, or the User property is non-empty, or |
| 730 | // there's neither a key, nor a cert available, specify user-password client |
| 731 | // authentication. |
| 732 | if (args()->ContainsString(flimflam::kOpenVPNAuthUserPassProperty) || |
| 733 | !args()->LookupString(flimflam::kOpenVPNUserProperty, "").empty() || |
| 734 | (!has_cert && !has_key)) { |
| 735 | options->push_back("--auth-user-pass"); |
| 736 | } |
| 737 | } |
| 738 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 739 | bool OpenVPNDriver::InitManagementChannelOptions( |
| 740 | vector<string> *options, Error *error) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 741 | if (!management_server_->Start(dispatcher(), &sockets_, options)) { |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 742 | Error::PopulateAndLog( |
| 743 | error, Error::kInternalError, "Unable to setup management channel."); |
| 744 | return false; |
| 745 | } |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 746 | // If there's a connected default service already, allow the openvpn client to |
| 747 | // establish connection as soon as it's started. Otherwise, hold the client |
| 748 | // until an underlying service connects and OnDefaultServiceChanged is |
| 749 | // invoked. |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 750 | if (manager()->IsOnline()) { |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 751 | management_server_->ReleaseHold(); |
| 752 | } |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 753 | return true; |
| 754 | } |
| 755 | |
Darin Petkov | 55771b7 | 2012-04-25 09:25:19 +0200 | [diff] [blame] | 756 | void OpenVPNDriver::InitLoggingOptions(vector<string> *options) { |
| 757 | options->push_back("--syslog"); |
| 758 | |
| 759 | string verb = args()->LookupString(kOpenVPNVerbProperty, ""); |
| 760 | if (verb.empty() && SLOG_IS_ON(VPN, 0)) { |
| 761 | verb = "3"; |
| 762 | } |
| 763 | if (!verb.empty()) { |
| 764 | options->push_back("--verb"); |
| 765 | options->push_back(verb); |
| 766 | } |
| 767 | } |
| 768 | |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 769 | bool OpenVPNDriver::AppendValueOption( |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 770 | const string &property, const string &option, vector<string> *options) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 771 | string value = args()->LookupString(property, ""); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 772 | if (!value.empty()) { |
| 773 | options->push_back(option); |
| 774 | options->push_back(value); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 775 | return true; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 776 | } |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 777 | return false; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 778 | } |
| 779 | |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 780 | bool OpenVPNDriver::AppendFlag( |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 781 | const string &property, const string &option, vector<string> *options) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 782 | if (args()->ContainsString(property)) { |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 783 | options->push_back(option); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 784 | return true; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 785 | } |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 786 | return false; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 787 | } |
| 788 | |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 789 | void OpenVPNDriver::Disconnect() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 790 | SLOG(VPN, 2) << __func__; |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 791 | IdleService(); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 792 | } |
| 793 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 794 | void OpenVPNDriver::OnConnectionDisconnected() { |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 795 | LOG(INFO) << "Underlying connection disconnected."; |
| 796 | // Restart the OpenVPN client forcing a reconnect attempt. |
| 797 | management_server_->Restart(); |
| 798 | // Indicate reconnect state right away to drop the VPN connection and start |
| 799 | // the connect timeout. This ensures that any miscommunication between shill |
| 800 | // and openvpn will not lead to a permanently stale connectivity state. Note |
| 801 | // that a subsequent invocation of OnReconnecting due to a RECONNECTING |
| 802 | // message will essentially be a no-op. |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 803 | OnReconnecting(kReconnectReasonOffline); |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | void OpenVPNDriver::OnConnectTimeout() { |
| 807 | VPNDriver::OnConnectTimeout(); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 808 | Service::ConnectFailure failure = |
| 809 | management_server_->state() == OpenVPNManagementServer::kStateResolve ? |
| 810 | Service::kFailureDNSLookup : Service::kFailureConnect; |
| 811 | FailService(failure, Service::kErrorDetailsNone); |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 814 | void OpenVPNDriver::OnReconnecting(ReconnectReason reason) { |
| 815 | LOG(INFO) << __func__ << "(" << reason << ")"; |
| 816 | int timeout_seconds = GetReconnectTimeoutSeconds(reason); |
| 817 | if (reason == kReconnectReasonTLSError && |
| 818 | timeout_seconds < connect_timeout_seconds()) { |
| 819 | // Reconnect due to TLS error happens during connect so we need to cancel |
| 820 | // the original connect timeout first and then reduce the time limit. |
| 821 | StopConnectTimeout(); |
| 822 | } |
| 823 | StartConnectTimeout(timeout_seconds); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 824 | // On restart/reconnect, drop the VPN connection, if any. The openvpn client |
| 825 | // might be in hold state if the VPN connection was previously established |
| 826 | // successfully. The hold will be released by OnDefaultServiceChanged when a |
| 827 | // new default service connects. This ensures that the client will use a fully |
| 828 | // functional underlying connection to reconnect. |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 829 | if (device_) { |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 830 | device_->DropConnection(); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 831 | } |
| 832 | if (service_) { |
| 833 | service_->SetState(Service::kStateAssociating); |
| 834 | } |
| 835 | } |
| 836 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 837 | // static |
| 838 | int OpenVPNDriver::GetReconnectTimeoutSeconds(ReconnectReason reason) { |
| 839 | switch (reason) { |
| 840 | case kReconnectReasonOffline: |
| 841 | return kReconnectOfflineTimeoutSeconds; |
| 842 | case kReconnectReasonTLSError: |
| 843 | return kReconnectTLSErrorTimeoutSeconds; |
| 844 | default: |
| 845 | break; |
| 846 | } |
| 847 | return kDefaultConnectTimeoutSeconds; |
| 848 | } |
| 849 | |
Paul Stewart | 39964fa | 2012-04-04 09:50:25 -0700 | [diff] [blame] | 850 | string OpenVPNDriver::GetProviderType() const { |
| 851 | return flimflam::kProviderOpenVpn; |
| 852 | } |
| 853 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 854 | KeyValueStore OpenVPNDriver::GetProvider(Error *error) { |
| 855 | SLOG(VPN, 2) << __func__; |
| 856 | KeyValueStore props = VPNDriver::GetProvider(error); |
| 857 | props.SetBool(flimflam::kPassphraseRequiredProperty, |
| 858 | args()->LookupString( |
| 859 | flimflam::kOpenVPNPasswordProperty, "").empty()); |
| 860 | return props; |
| 861 | } |
| 862 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 863 | // TODO(petkov): Consider refactoring lsb-release parsing out into a shared |
| 864 | // singleton if it's used outside OpenVPN. |
| 865 | bool OpenVPNDriver::ParseLSBRelease(map<string, string> *lsb_release) { |
| 866 | SLOG(VPN, 2) << __func__ << "(" << lsb_release_file_.value() << ")"; |
| 867 | string contents; |
| 868 | if (!file_util::ReadFileToString(lsb_release_file_, &contents)) { |
| 869 | LOG(ERROR) << "Unable to read the lsb-release file: " |
| 870 | << lsb_release_file_.value(); |
| 871 | return false; |
| 872 | } |
| 873 | vector<string> lines; |
| 874 | SplitString(contents, '\n', &lines); |
| 875 | for (vector<string>::const_iterator it = lines.begin(); |
| 876 | it != lines.end(); ++it) { |
| 877 | size_t assign = it->find('='); |
| 878 | if (assign == string::npos) { |
| 879 | continue; |
| 880 | } |
| 881 | (*lsb_release)[it->substr(0, assign)] = it->substr(assign + 1); |
| 882 | } |
| 883 | return true; |
| 884 | } |
| 885 | |
| 886 | void OpenVPNDriver::InitEnvironment(vector<string> *environment) { |
| 887 | // Adds the platform name and version to the environment so that openvpn can |
| 888 | // send them to the server when OpenVPN.PushPeerInfo is set. |
| 889 | map<string, string> lsb_release; |
| 890 | ParseLSBRelease(&lsb_release); |
| 891 | string platform_name = lsb_release[kChromeOSReleaseName]; |
| 892 | if (!platform_name.empty()) { |
| 893 | environment->push_back("IV_PLAT=" + platform_name); |
| 894 | } |
| 895 | string platform_version = lsb_release[kChromeOSReleaseVersion]; |
| 896 | if (!platform_version.empty()) { |
| 897 | environment->push_back("IV_PLAT_REL=" + platform_version); |
| 898 | } |
| 899 | } |
| 900 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 901 | void OpenVPNDriver::OnDefaultServiceChanged(const ServiceRefPtr &service) { |
| 902 | SLOG(VPN, 2) << __func__ |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 903 | << "(" << (service ? service->unique_name() : "-") << ")"; |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 904 | // Allow the openvpn client to connect/reconnect only over a connected |
| 905 | // underlying default service. If there's no default connected service, hold |
| 906 | // the openvpn client until an underlying connection is established. If the |
| 907 | // default service is our VPN service, hold the openvpn client on reconnect so |
| 908 | // that the VPN connection can be torn down fully before a new connection |
| 909 | // attempt is made over the underlying service. |
| 910 | if (service && service != service_ && service->IsConnected()) { |
| 911 | management_server_->ReleaseHold(); |
| 912 | } else { |
| 913 | management_server_->Hold(); |
| 914 | } |
| 915 | } |
| 916 | |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 917 | void OpenVPNDriver::ReportConnectionMetrics() { |
| 918 | metrics_->SendEnumToUMA( |
| 919 | Metrics::kMetricVpnDriver, |
| 920 | Metrics::kVpnDriverOpenVpn, |
| 921 | Metrics::kMetricVpnDriverMax); |
| 922 | |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 923 | if (args()->LookupString(flimflam::kOpenVPNCaCertNSSProperty, "") != "" || |
| 924 | args()->LookupString(flimflam::kOpenVPNCaCertProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 925 | metrics_->SendEnumToUMA( |
| 926 | Metrics::kMetricVpnRemoteAuthenticationType, |
| 927 | Metrics::kVpnRemoteAuthenticationTypeOpenVpnCertificate, |
| 928 | Metrics::kMetricVpnRemoteAuthenticationTypeMax); |
| 929 | } else { |
| 930 | metrics_->SendEnumToUMA( |
| 931 | Metrics::kMetricVpnRemoteAuthenticationType, |
| 932 | Metrics::kVpnRemoteAuthenticationTypeOpenVpnDefault, |
| 933 | Metrics::kMetricVpnRemoteAuthenticationTypeMax); |
| 934 | } |
| 935 | |
| 936 | bool has_user_authentication = false; |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 937 | if (args()->LookupString(flimflam::kOpenVPNOTPProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 938 | metrics_->SendEnumToUMA( |
| 939 | Metrics::kMetricVpnUserAuthenticationType, |
| 940 | Metrics::kVpnUserAuthenticationTypeOpenVpnUsernamePasswordOtp, |
| 941 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 942 | has_user_authentication = true; |
| 943 | } |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 944 | if (args()->LookupString(flimflam::kOpenVPNAuthUserPassProperty, "") != ""|| |
| 945 | args()->LookupString(flimflam::kOpenVPNUserProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 946 | metrics_->SendEnumToUMA( |
| 947 | Metrics::kMetricVpnUserAuthenticationType, |
| 948 | Metrics::kVpnUserAuthenticationTypeOpenVpnUsernamePassword, |
| 949 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 950 | has_user_authentication = true; |
| 951 | } |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 952 | if (args()->LookupString(flimflam::kOpenVPNClientCertIdProperty, "") != "" || |
| 953 | args()->LookupString(kOpenVPNCertProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 954 | metrics_->SendEnumToUMA( |
| 955 | Metrics::kMetricVpnUserAuthenticationType, |
| 956 | Metrics::kVpnUserAuthenticationTypeOpenVpnCertificate, |
| 957 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 958 | has_user_authentication = true; |
| 959 | } |
| 960 | if (!has_user_authentication) { |
| 961 | metrics_->SendEnumToUMA( |
| 962 | Metrics::kMetricVpnUserAuthenticationType, |
| 963 | Metrics::kVpnUserAuthenticationTypeOpenVpnNone, |
| 964 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 965 | } |
| 966 | } |
| 967 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 968 | } // namespace shill |