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 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 9 | #include <base/logging.h> |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 10 | #include <base/string_util.h> |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
| 12 | |
| 13 | #include "shill/error.h" |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 14 | #include "shill/manager.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 15 | #include "shill/openvpn_driver.h" |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 16 | #include "shill/profile.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 17 | #include "shill/scope_logger.h" |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 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 | |
| 42 | VPNServiceRefPtr VPNProvider::GetService(const KeyValueStore &args, |
| 43 | Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 44 | SLOG(VPN, 2) << __func__; |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 45 | string type = args.LookupString(flimflam::kProviderTypeProperty, ""); |
| 46 | if (type.empty()) { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 47 | Error::PopulateAndLog( |
| 48 | error, Error::kNotSupported, "Missing VPN type property."); |
| 49 | return NULL; |
| 50 | } |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 51 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 52 | string storage_id = VPNService::CreateStorageIdentifier(args, error); |
| 53 | if (storage_id.empty()) { |
| 54 | return NULL; |
| 55 | } |
| 56 | |
Paul Stewart | 39964fa | 2012-04-04 09:50:25 -0700 | [diff] [blame] | 57 | // Find a service in the provider list which matches these parameters. |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 58 | VPNServiceRefPtr service = FindService(type, storage_id); |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 59 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 60 | if (service == NULL) { |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 61 | // Create a service, using the name and type arguments passed in. |
| 62 | string name = args.LookupString(flimflam::kProviderNameProperty, ""); |
| 63 | if (name.empty()) { |
| 64 | name = args.LookupString(flimflam::kNameProperty, ""); |
| 65 | } |
| 66 | service = CreateService(type, name, storage_id, error); |
| 67 | } |
| 68 | |
| 69 | if (service != NULL) { |
| 70 | // Configure the service using the the rest of the passed-in arguments. |
| 71 | service->Configure(args, error); |
Paul Stewart | 39964fa | 2012-04-04 09:50:25 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 74 | return service; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | bool VPNProvider::OnDeviceInfoAvailable(const string &link_name, |
| 78 | int interface_index) { |
| 79 | for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin(); |
| 80 | it != services_.end(); |
| 81 | ++it) { |
| 82 | if ((*it)->driver()->ClaimInterface(link_name, interface_index)) { |
| 83 | return true; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return false; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 90 | void VPNProvider::RemoveService(VPNServiceRefPtr service) { |
| 91 | vector<VPNServiceRefPtr>::iterator it; |
| 92 | it = std::find(services_.begin(), services_.end(), service); |
| 93 | if (it != services_.end()) { |
| 94 | services_.erase(it); |
| 95 | } |
| 96 | } |
| 97 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 98 | void VPNProvider::CreateServicesFromProfile(ProfileRefPtr profile) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 99 | SLOG(VPN, 2) << __func__; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 100 | const StoreInterface *storage = profile->GetConstStorage(); |
| 101 | set<string> groups = |
| 102 | storage->GetGroupsWithKey(flimflam::kProviderTypeProperty); |
| 103 | for (set<string>::iterator it = groups.begin(); it != groups.end(); ++it) { |
| 104 | if (!StartsWithASCII(*it, "vpn_", false)) { |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | string type; |
| 109 | if (!storage->GetString(*it, flimflam::kProviderTypeProperty, &type)) { |
| 110 | LOG(ERROR) << "Group " << *it << " is missing the " |
| 111 | << flimflam::kProviderTypeProperty << " property."; |
| 112 | continue; |
| 113 | } |
| 114 | |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 115 | string name; |
| 116 | if (!storage->GetString(*it, flimflam::kProviderNameProperty, &name) && |
| 117 | !storage->GetString(*it, flimflam::kNameProperty, &name)) { |
| 118 | LOG(ERROR) << "Group " << *it << " is missing the " |
| 119 | << flimflam::kProviderNameProperty << " property."; |
| 120 | continue; |
| 121 | } |
| 122 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 123 | VPNServiceRefPtr service = FindService(type, *it); |
| 124 | if (service != NULL) { |
| 125 | // If the service already exists, it does not need to be configured, |
| 126 | // since PushProfile would have already called ConfigureService on it. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 127 | SLOG(VPN, 2) << "Service already exists " << *it; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 128 | continue; |
| 129 | } |
| 130 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 131 | Error error; |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 132 | service = CreateService(type, name, *it, &error); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 133 | |
| 134 | if (service == NULL) { |
| 135 | LOG(ERROR) << "Could not create service for " << *it; |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | if (!profile->ConfigureService(service)) { |
| 140 | LOG(ERROR) << "Could not configure service for " << *it; |
| 141 | continue; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | VPNServiceRefPtr VPNProvider::CreateService(const string &type, |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 147 | const string &name, |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 148 | const string &storage_id, |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 149 | Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 150 | SLOG(VPN, 2) << __func__ << " type " << type << " name " << name |
| 151 | << " storage id " << storage_id; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 152 | scoped_ptr<VPNDriver> driver; |
| 153 | if (type == flimflam::kProviderOpenVpn) { |
| 154 | driver.reset(new OpenVPNDriver( |
| 155 | control_interface_, dispatcher_, metrics_, manager_, |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 156 | manager_->device_info(), manager_->glib())); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 157 | } else { |
| 158 | Error::PopulateAndLog( |
| 159 | error, Error::kNotSupported, "Unsupported VPN type: " + type); |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | VPNServiceRefPtr service = new VPNService( |
| 164 | control_interface_, dispatcher_, metrics_, manager_, driver.release()); |
| 165 | service->set_storage_id(storage_id); |
| 166 | service->InitDriverPropertyStore(); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 167 | if (!name.empty()) { |
| 168 | service->set_friendly_name(name); |
| 169 | } |
| 170 | services_.push_back(service); |
| 171 | manager_->RegisterService(service); |
| 172 | |
| 173 | return service; |
| 174 | } |
| 175 | |
| 176 | VPNServiceRefPtr VPNProvider::FindService(const std::string &type, |
| 177 | const std::string &storage_id) { |
| 178 | for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin(); |
| 179 | it != services_.end(); |
| 180 | ++it) { |
| 181 | if (type == (*it)->driver()->GetProviderType() && |
| 182 | storage_id == (*it)->GetStorageIdentifier()) { |
| 183 | return *it; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return NULL; |
| 188 | } |
| 189 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 190 | } // namespace shill |