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