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); |
| 58 | if (service == NULL) { |
| 59 | service = CreateService(type, storage_id, args, error); |
Paul Stewart | 39964fa | 2012-04-04 09:50:25 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 62 | return service; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool VPNProvider::OnDeviceInfoAvailable(const string &link_name, |
| 66 | int interface_index) { |
| 67 | for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin(); |
| 68 | it != services_.end(); |
| 69 | ++it) { |
| 70 | if ((*it)->driver()->ClaimInterface(link_name, interface_index)) { |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return false; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 78 | void VPNProvider::RemoveService(VPNServiceRefPtr service) { |
| 79 | vector<VPNServiceRefPtr>::iterator it; |
| 80 | it = std::find(services_.begin(), services_.end(), service); |
| 81 | if (it != services_.end()) { |
| 82 | services_.erase(it); |
| 83 | } |
| 84 | } |
| 85 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame^] | 86 | void VPNProvider::CreateServicesFromProfile(ProfileRefPtr profile) { |
| 87 | const StoreInterface *storage = profile->GetConstStorage(); |
| 88 | set<string> groups = |
| 89 | storage->GetGroupsWithKey(flimflam::kProviderTypeProperty); |
| 90 | for (set<string>::iterator it = groups.begin(); it != groups.end(); ++it) { |
| 91 | if (!StartsWithASCII(*it, "vpn_", false)) { |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | string type; |
| 96 | if (!storage->GetString(*it, flimflam::kProviderTypeProperty, &type)) { |
| 97 | LOG(ERROR) << "Group " << *it << " is missing the " |
| 98 | << flimflam::kProviderTypeProperty << " property."; |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | VPNServiceRefPtr service = FindService(type, *it); |
| 103 | if (service != NULL) { |
| 104 | // If the service already exists, it does not need to be configured, |
| 105 | // since PushProfile would have already called ConfigureService on it. |
| 106 | VLOG(2) << "Service already exists " << *it; |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | // Create a service with an empty |args| parameter. We will populate |
| 111 | // the service with properties using ConfigureService() below. |
| 112 | KeyValueStore args; |
| 113 | Error error; |
| 114 | service = CreateService(type, *it, args, &error); |
| 115 | |
| 116 | if (service == NULL) { |
| 117 | LOG(ERROR) << "Could not create service for " << *it; |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | if (!profile->ConfigureService(service)) { |
| 122 | LOG(ERROR) << "Could not configure service for " << *it; |
| 123 | continue; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | VPNServiceRefPtr VPNProvider::CreateService(const string &type, |
| 129 | const string &storage_id, |
| 130 | const KeyValueStore &args, |
| 131 | Error *error) { |
| 132 | scoped_ptr<VPNDriver> driver; |
| 133 | if (type == flimflam::kProviderOpenVpn) { |
| 134 | driver.reset(new OpenVPNDriver( |
| 135 | control_interface_, dispatcher_, metrics_, manager_, |
| 136 | manager_->device_info(), manager_->glib(), args)); |
| 137 | } else { |
| 138 | Error::PopulateAndLog( |
| 139 | error, Error::kNotSupported, "Unsupported VPN type: " + type); |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | VPNServiceRefPtr service = new VPNService( |
| 144 | control_interface_, dispatcher_, metrics_, manager_, driver.release()); |
| 145 | service->set_storage_id(storage_id); |
| 146 | service->InitDriverPropertyStore(); |
| 147 | string name = args.LookupString(flimflam::kProviderNameProperty, ""); |
| 148 | if (!name.empty()) { |
| 149 | service->set_friendly_name(name); |
| 150 | } |
| 151 | services_.push_back(service); |
| 152 | manager_->RegisterService(service); |
| 153 | |
| 154 | return service; |
| 155 | } |
| 156 | |
| 157 | VPNServiceRefPtr VPNProvider::FindService(const std::string &type, |
| 158 | const std::string &storage_id) { |
| 159 | for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin(); |
| 160 | it != services_.end(); |
| 161 | ++it) { |
| 162 | if (type == (*it)->driver()->GetProviderType() && |
| 163 | storage_id == (*it)->GetStorageIdentifier()) { |
| 164 | return *it; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return NULL; |
| 169 | } |
| 170 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 171 | } // namespace shill |