blob: 5387cc929fb7d5ef3d198e8fe1e9603ead2e78fa [file] [log] [blame]
Darin Petkov33af05c2012-02-28 10:10:30 +01001// 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 Stewart65512e12012-03-26 18:01:08 -07007#include <algorithm>
8
Darin Petkov33af05c2012-02-28 10:10:30 +01009#include <base/logging.h>
Paul Stewart66815332012-04-09 18:09:36 -070010#include <base/string_util.h>
Darin Petkov33af05c2012-02-28 10:10:30 +010011#include <chromeos/dbus/service_constants.h>
12
13#include "shill/error.h"
Paul Stewartca6abd42012-03-01 15:45:29 -080014#include "shill/manager.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010015#include "shill/openvpn_driver.h"
Paul Stewart66815332012-04-09 18:09:36 -070016#include "shill/profile.h"
17#include "shill/store_interface.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010018#include "shill/vpn_service.h"
19
Paul Stewart66815332012-04-09 18:09:36 -070020using std::set;
Darin Petkov33af05c2012-02-28 10:10:30 +010021using std::string;
Paul Stewartca6abd42012-03-01 15:45:29 -080022using std::vector;
Darin Petkov33af05c2012-02-28 10:10:30 +010023
24namespace shill {
25
26VPNProvider::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
35VPNProvider::~VPNProvider() {}
36
37void VPNProvider::Start() {}
38
39void VPNProvider::Stop() {}
40
41VPNServiceRefPtr VPNProvider::GetService(const KeyValueStore &args,
42 Error *error) {
43 VLOG(2) << __func__;
Darin Petkov7f060332012-03-14 11:46:47 +010044 string type = args.LookupString(flimflam::kProviderTypeProperty, "");
45 if (type.empty()) {
Darin Petkov33af05c2012-02-28 10:10:30 +010046 Error::PopulateAndLog(
47 error, Error::kNotSupported, "Missing VPN type property.");
48 return NULL;
49 }
Paul Stewartca6abd42012-03-01 15:45:29 -080050
Darin Petkov02867712012-03-12 14:25:05 +010051 string storage_id = VPNService::CreateStorageIdentifier(args, error);
52 if (storage_id.empty()) {
53 return NULL;
54 }
55
Paul Stewart39964fa2012-04-04 09:50:25 -070056 // Find a service in the provider list which matches these parameters.
Paul Stewart66815332012-04-09 18:09:36 -070057 VPNServiceRefPtr service = FindService(type, storage_id);
58 if (service == NULL) {
59 service = CreateService(type, storage_id, args, error);
Paul Stewart39964fa2012-04-04 09:50:25 -070060 }
61
Darin Petkov79d74c92012-03-07 17:20:32 +010062 return service;
Paul Stewartca6abd42012-03-01 15:45:29 -080063}
64
65bool 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 Petkov33af05c2012-02-28 10:10:30 +010076}
77
Paul Stewart65512e12012-03-26 18:01:08 -070078void 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 Stewart66815332012-04-09 18:09:36 -070086void 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
128VPNServiceRefPtr 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
157VPNServiceRefPtr 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 Petkov33af05c2012-02-28 10:10:30 +0100171} // namespace shill