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/vpn_provider.h" |
| 6 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 9 | #include <base/string_util.h> |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 10 | #include <chromeos/dbus/service_constants.h> |
| 11 | |
| 12 | #include "shill/error.h" |
Darin Petkov | 9d1bbe7 | 2012-04-25 10:58:59 +0200 | [diff] [blame] | 13 | #include "shill/l2tp_ipsec_driver.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 14 | #include "shill/logging.h" |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 15 | #include "shill/manager.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 16 | #include "shill/openvpn_driver.h" |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 17 | #include "shill/profile.h" |
| 18 | #include "shill/store_interface.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 19 | #include "shill/vpn_service.h" |
| 20 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 21 | using std::set; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 22 | using std::string; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 23 | using std::vector; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 24 | |
| 25 | namespace shill { |
| 26 | |
| 27 | VPNProvider::VPNProvider(ControlInterface *control_interface, |
| 28 | EventDispatcher *dispatcher, |
| 29 | Metrics *metrics, |
| 30 | Manager *manager) |
| 31 | : control_interface_(control_interface), |
| 32 | dispatcher_(dispatcher), |
| 33 | metrics_(metrics), |
| 34 | manager_(manager) {} |
| 35 | |
| 36 | VPNProvider::~VPNProvider() {} |
| 37 | |
| 38 | void VPNProvider::Start() {} |
| 39 | |
| 40 | void VPNProvider::Stop() {} |
| 41 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 42 | // static |
| 43 | bool VPNProvider::GetServiceParametersFromArgs(const KeyValueStore &args, |
| 44 | string *type_ptr, |
| 45 | string *name_ptr, |
| 46 | string *host_ptr, |
| 47 | Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 48 | SLOG(VPN, 2) << __func__; |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame^] | 49 | string type = args.LookupString(kProviderTypeProperty, ""); |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 50 | if (type.empty()) { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 51 | Error::PopulateAndLog( |
| 52 | error, Error::kNotSupported, "Missing VPN type property."); |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 53 | return false; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 54 | } |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 55 | |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame^] | 56 | string host = args.LookupString(kProviderHostProperty, ""); |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 57 | if (host.empty()) { |
| 58 | Error::PopulateAndLog( |
| 59 | error, Error::kNotSupported, "Missing VPN host property."); |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | |
| 63 | *type_ptr = type, |
| 64 | *host_ptr = host, |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame^] | 65 | *name_ptr = args.LookupString(kNameProperty, ""); |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | ServiceRefPtr VPNProvider::GetService(const KeyValueStore &args, |
| 71 | Error *error) { |
| 72 | SLOG(VPN, 2) << __func__; |
| 73 | string type; |
| 74 | string name; |
| 75 | string host; |
| 76 | |
| 77 | if (!GetServiceParametersFromArgs(args, &type, &name, &host, error)) { |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 78 | return NULL; |
| 79 | } |
| 80 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 81 | string storage_id = VPNService::CreateStorageIdentifier(args, error); |
| 82 | if (storage_id.empty()) { |
| 83 | return NULL; |
| 84 | } |
| 85 | |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 86 | // Find a service in the provider list which matches these parameters. |
| 87 | VPNServiceRefPtr service = FindService(type, name, host); |
| 88 | if (service == NULL) { |
| 89 | service = CreateService(type, name, storage_id, error); |
| 90 | } |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 91 | return service; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 94 | ServiceRefPtr VPNProvider::FindSimilarService(const KeyValueStore &args, |
| 95 | Error *error) const { |
| 96 | SLOG(VPN, 2) << __func__; |
| 97 | string type; |
| 98 | string name; |
| 99 | string host; |
| 100 | |
| 101 | if (!GetServiceParametersFromArgs(args, &type, &name, &host, error)) { |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | // Find a service in the provider list which matches these parameters. |
| 106 | VPNServiceRefPtr service = FindService(type, name, host); |
| 107 | if (!service) { |
| 108 | error->Populate(Error::kNotFound, "Matching service was not found"); |
| 109 | } |
| 110 | |
| 111 | return service; |
| 112 | } |
| 113 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 114 | bool VPNProvider::OnDeviceInfoAvailable(const string &link_name, |
| 115 | int interface_index) { |
| 116 | for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin(); |
| 117 | it != services_.end(); |
| 118 | ++it) { |
| 119 | if ((*it)->driver()->ClaimInterface(link_name, interface_index)) { |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return false; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 127 | void VPNProvider::RemoveService(VPNServiceRefPtr service) { |
| 128 | vector<VPNServiceRefPtr>::iterator it; |
| 129 | it = std::find(services_.begin(), services_.end(), service); |
| 130 | if (it != services_.end()) { |
| 131 | services_.erase(it); |
| 132 | } |
| 133 | } |
| 134 | |
Paul Stewart | 0e51ad9 | 2013-07-26 14:42:55 -0700 | [diff] [blame] | 135 | void VPNProvider::CreateServicesFromProfile(const ProfileRefPtr &profile) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 136 | SLOG(VPN, 2) << __func__; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 137 | const StoreInterface *storage = profile->GetConstStorage(); |
| 138 | set<string> groups = |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame^] | 139 | storage->GetGroupsWithKey(kProviderTypeProperty); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 140 | for (set<string>::iterator it = groups.begin(); it != groups.end(); ++it) { |
| 141 | if (!StartsWithASCII(*it, "vpn_", false)) { |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | string type; |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame^] | 146 | if (!storage->GetString(*it, kProviderTypeProperty, &type)) { |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 147 | LOG(ERROR) << "Group " << *it << " is missing the " |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame^] | 148 | << kProviderTypeProperty << " property."; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 149 | continue; |
| 150 | } |
| 151 | |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 152 | string name; |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame^] | 153 | if (!storage->GetString(*it, kNameProperty, &name)) { |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 154 | LOG(ERROR) << "Group " << *it << " is missing the " |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame^] | 155 | << kNameProperty << " property."; |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 156 | continue; |
| 157 | } |
| 158 | |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 159 | string host; |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame^] | 160 | if (!storage->GetString(*it, kProviderHostProperty, &host)) { |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 161 | LOG(ERROR) << "Group " << *it << " is missing the " |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame^] | 162 | << kProviderHostProperty << " property."; |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 163 | continue; |
| 164 | } |
| 165 | |
| 166 | VPNServiceRefPtr service = FindService(type, name, host); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 167 | if (service != NULL) { |
| 168 | // If the service already exists, it does not need to be configured, |
| 169 | // since PushProfile would have already called ConfigureService on it. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 170 | SLOG(VPN, 2) << "Service already exists " << *it; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 171 | continue; |
| 172 | } |
| 173 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 174 | Error error; |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 175 | service = CreateService(type, name, *it, &error); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 176 | |
| 177 | if (service == NULL) { |
| 178 | LOG(ERROR) << "Could not create service for " << *it; |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | if (!profile->ConfigureService(service)) { |
| 183 | LOG(ERROR) << "Could not configure service for " << *it; |
| 184 | continue; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 189 | VPNServiceRefPtr VPNProvider::CreateServiceInner(const string &type, |
| 190 | const string &name, |
| 191 | const string &storage_id, |
| 192 | Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 193 | SLOG(VPN, 2) << __func__ << " type " << type << " name " << name |
| 194 | << " storage id " << storage_id; |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 195 | #if defined(DISABLE_VPN) |
| 196 | |
| 197 | Error::PopulateAndLog(error, Error::kNotSupported, "VPN is not supported."); |
| 198 | return NULL; |
| 199 | |
| 200 | #else |
| 201 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 202 | scoped_ptr<VPNDriver> driver; |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame^] | 203 | if (type == kProviderOpenVpn) { |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 204 | driver.reset(new OpenVPNDriver( |
| 205 | control_interface_, dispatcher_, metrics_, manager_, |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 206 | manager_->device_info(), manager_->glib())); |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame^] | 207 | } else if (type == kProviderL2tpIpsec) { |
Darin Petkov | 9d1bbe7 | 2012-04-25 10:58:59 +0200 | [diff] [blame] | 208 | driver.reset(new L2TPIPSecDriver( |
| 209 | control_interface_, dispatcher_, metrics_, manager_, |
| 210 | manager_->device_info(), manager_->glib())); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 211 | } else { |
| 212 | Error::PopulateAndLog( |
| 213 | error, Error::kNotSupported, "Unsupported VPN type: " + type); |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | VPNServiceRefPtr service = new VPNService( |
| 218 | control_interface_, dispatcher_, metrics_, manager_, driver.release()); |
| 219 | service->set_storage_id(storage_id); |
| 220 | service->InitDriverPropertyStore(); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 221 | if (!name.empty()) { |
| 222 | service->set_friendly_name(name); |
| 223 | } |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 224 | return service; |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 225 | |
| 226 | #endif // DISABLE_VPN |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 229 | VPNServiceRefPtr VPNProvider::CreateService(const string &type, |
| 230 | const string &name, |
| 231 | const string &storage_id, |
| 232 | Error *error) { |
| 233 | VPNServiceRefPtr service = CreateServiceInner(type, name, storage_id, error); |
| 234 | if (service) { |
| 235 | services_.push_back(service); |
| 236 | manager_->RegisterService(service); |
| 237 | } |
| 238 | |
| 239 | return service; |
| 240 | } |
| 241 | |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 242 | VPNServiceRefPtr VPNProvider::FindService(const string &type, |
| 243 | const string &name, |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 244 | const string &host) const { |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 245 | for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin(); |
| 246 | it != services_.end(); |
| 247 | ++it) { |
| 248 | if (type == (*it)->driver()->GetProviderType() && |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 249 | name == (*it)->friendly_name() && |
| 250 | host == (*it)->driver()->GetHost()) { |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 251 | return *it; |
| 252 | } |
| 253 | } |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 254 | return NULL; |
| 255 | } |
| 256 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 257 | ServiceRefPtr VPNProvider::CreateTemporaryService( |
| 258 | const KeyValueStore &args, Error *error) { |
| 259 | string type; |
| 260 | string name; |
| 261 | string host; |
| 262 | |
| 263 | if (!GetServiceParametersFromArgs(args, &type, &name, &host, error)) { |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | string storage_id = VPNService::CreateStorageIdentifier(args, error); |
| 268 | if (storage_id.empty()) { |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | return CreateServiceInner(type, name, storage_id, error); |
| 273 | } |
| 274 | |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 275 | bool VPNProvider::HasActiveService() const { |
| 276 | for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin(); |
| 277 | it != services_.end(); ++it) { |
| 278 | if ((*it)->IsConnecting() || (*it)->IsConnected()) { |
| 279 | return true; |
| 280 | } |
| 281 | } |
| 282 | return false; |
| 283 | } |
| 284 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 285 | } // namespace shill |