Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(537)

Side by Side Diff: l2tp_manager.cc

Issue 6508016: vpn-manager: Add l2tp/ipsec vpn manager (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/vpn-manager.git@master
Patch Set: respond to petkov Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « l2tp_manager.h ('k') | l2tp_manager_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "vpn-manager/l2tp_manager.h"
6
7 #include "base/file_util.h"
8 #include "base/logging.h"
9 #include "base/string_util.h"
10 #include "chromeos/process.h"
11 #include "gflags/gflags.h"
12
13 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
14 DEFINE_bool(length_bit, true, "length bit");
15 DEFINE_bool(require_chap, true, "require chap");
16 DEFINE_bool(refuse_pap, true, "refuse chap");
17 DEFINE_bool(require_authentication, true, "require authentication");
18 DEFINE_string(password, "", "password (insecure - use pppd plugin instead)");
19 DEFINE_bool(ppp_debug, true, "ppp debug");
20 DEFINE_int32(ppp_setup_timeout, 10, "timeout to setup ppp (seconds)");
21 DEFINE_string(pppd_plugin, "", "pppd plugin");
22 DEFINE_bool(usepeerdns, true, "usepeerdns - ask peer for DNS");
23 DEFINE_string(user, "", "user name");
24 #pragma GCC diagnostic error "-Wstrict-aliasing"
25
26 const char kL2tpConnectionName[] = "managed";
27 const char kPppInterfacePath[] = "/sys/class/net/ppp0";
28
29 using ::chromeos::ProcessImpl;
30
31 L2tpManager::L2tpManager()
32 : ServiceManager("l2tp"),
33 was_initiated_(false),
34 output_fd_(-1),
35 ppp_interface_path_(kPppInterfacePath),
36 l2tpd_(new ProcessImpl) {
37 }
38
39 bool L2tpManager::Initialize(const std::string& remote_address) {
40 remote_address_ = remote_address;
41 if (FLAGS_user.empty()) {
42 LOG(ERROR) << "l2tp layer requires user name";
43 return false;
44 }
45 if (!FLAGS_pppd_plugin.empty() &&
46 !file_util::PathExists(FilePath(FLAGS_pppd_plugin))) {
47 LOG(WARNING) << "pppd_plugin (" << FLAGS_pppd_plugin << ") does not exist";
48 }
49 if (!FLAGS_password.empty()) {
50 LOG(WARNING) << "Passing a password on the command-line is insecure";
51 }
52 return true;
53 }
54
55 static void AddString(std::string* config, const char* key,
56 const std::string& value) {
57 config->append(StringPrintf("%s = %s\n", key, value.c_str()));
58 }
59
60 static void AddBool(std::string* config, const char* key, bool value) {
61 config->append(StringPrintf("%s = %s\n", key, value ? "yes" : "no"));
62 }
63
64 std::string L2tpManager::FormatL2tpdConfiguration(
65 const std::string& ppp_config_path) {
66 std::string l2tpd_config;
67 l2tpd_config.append(StringPrintf("[lac %s]\n", kL2tpConnectionName));
68 AddString(&l2tpd_config, "lns", remote_address_);
69 AddBool(&l2tpd_config, "require chap", FLAGS_require_chap);
70 AddBool(&l2tpd_config, "refuse pap", FLAGS_refuse_pap);
71 AddBool(&l2tpd_config, "require authentication",
72 FLAGS_require_authentication);
73 AddString(&l2tpd_config, "name", FLAGS_user);
74 AddBool(&l2tpd_config, "ppp debug", FLAGS_ppp_debug);
75 AddString(&l2tpd_config, "pppoptfile", ppp_config_path);
76 AddBool(&l2tpd_config, "length bit", FLAGS_length_bit);
77 return l2tpd_config;
78 }
79
80 std::string L2tpManager::FormatPppdConfiguration() {
81 std::string pppd_config = StringPrintf(
82 "ipcp-accept-local\n"
83 "ipcp-accept-remote\n"
84 "refuse-eap\n"
85 "noccp\n"
86 "noauth\n"
87 "crtscts\n"
88 "idle 1800\n"
89 "mtu 1410\n"
90 "mru 1410\n"
91 "nodefaultroute\n"
92 "debug\n"
93 "lock\n"
94 "connect-delay 5000\n");
95 if (FLAGS_usepeerdns) {
96 pppd_config.append("usepeerdns\n");
97 }
98 if (!FLAGS_pppd_plugin.empty()) {
99 DLOG(INFO) << "Using pppd plugin " << FLAGS_pppd_plugin;
100 pppd_config.append(StringPrintf("plugin %s\n", FLAGS_pppd_plugin.c_str()));
101 }
102 return pppd_config;
103 }
104
105 bool L2tpManager::Initiate() {
106 std::string control_string;
107 control_string = StringPrintf("c %s", kL2tpConnectionName);
108 if (FLAGS_pppd_plugin.empty()) {
109 control_string.append(StringPrintf(" %s %s\n",
110 FLAGS_user.c_str(),
111 FLAGS_password.c_str()));
112 } else {
113 // otherwise the plugin must specify username and password.
114 control_string.append("\n");
115 }
116 if (!file_util::WriteFile(l2tpd_control_path_, control_string.c_str(),
117 control_string.size())) {
118 return false;
119 }
120 was_initiated_ = true;
121 return true;
122 }
123
124 bool L2tpManager::Terminate() {
125 std::string control_string = StringPrintf("d %s\n",
126 kL2tpConnectionName);
127 if (!file_util::WriteFile(l2tpd_control_path_, control_string.c_str(),
128 control_string.size())) {
129 return false;
130 }
131 return true;
132 }
133
134 bool L2tpManager::Start() {
135 FilePath pppd_config_path = temp_path()->Append("pppd.conf");
136 std::string l2tpd_config = FormatL2tpdConfiguration(pppd_config_path.value());
137 FilePath l2tpd_config_path = temp_path()->Append("l2tpd.conf");
138 if (!file_util::WriteFile(l2tpd_config_path, l2tpd_config.c_str(),
139 l2tpd_config.size())) {
140 LOG(ERROR) << "Unable to write l2tpd config to "
141 << l2tpd_config_path.value();
142 return false;
143 }
144 std::string pppd_config = FormatPppdConfiguration();
145 if (!file_util::WriteFile(pppd_config_path, pppd_config.c_str(),
146 pppd_config.size())) {
147 LOG(ERROR) << "Unable to write pppd config to " << pppd_config_path.value();
148 return false;
149 }
150 l2tpd_control_path_ = temp_path()->Append("l2tpd.control");
151 file_util::Delete(l2tpd_control_path_, false);
152
153 l2tpd_->Reset(0);
154 l2tpd_->AddArg(L2TPD);
155 l2tpd_->AddStringOption("-c", l2tpd_config_path.value());
156 l2tpd_->AddStringOption("-C", l2tpd_control_path_.value());
157 l2tpd_->AddArg("-D");
158 l2tpd_->RedirectUsingPipe(STDERR_FILENO, false);
159 l2tpd_->Start();
160 output_fd_ = l2tpd_->GetPipe(STDERR_FILENO);
161 start_ticks_ = base::TimeTicks::Now();
162 return true;
163 }
164
165 int L2tpManager::Poll() {
166 if (is_running()) return -1;
167 if (start_ticks_.is_null()) return -1;
168 if (!was_initiated_ && file_util::PathExists(l2tpd_control_path_)) {
169 if (!Initiate()) {
170 LOG(ERROR) << "Unable to initiate connection";
171 Terminate();
172 OnStopped(false);
173 return -1;
174 }
175 // With the connection initated, check if it's up in 1s.
176 return 1000;
177 }
178 if (was_initiated_ && file_util::PathExists(FilePath(ppp_interface_path_))) {
179 LOG(INFO) << "L2TP connection now up";
180 OnStarted();
181 return -1;
182 }
183 // Check for the ppp setup timeout. This includes the time
184 // to start pppd, it to set up its control file, l2tp connection
185 // setup, ppp connection setup. Authentication happens after
186 // the ppp device is created.
187 if (base::TimeTicks::Now() - start_ticks_ >
188 base::TimeDelta::FromSeconds(FLAGS_ppp_setup_timeout)) {
189 LOG(ERROR) << "PPP setup timed out";
190 // Cleanly terminate if the control file exists.
191 if (was_initiated_) Terminate();
192 OnStopped(false);
193 // Poll in 1 second in order to check if clean shutdown worked.
194 }
195 return 1000;
196 }
197
198 void L2tpManager::ProcessOutput() {
199 ServiceManager::WriteFdToSyslog(output_fd_, "", &partial_output_line_);
200 }
201
202 bool L2tpManager::IsChild(pid_t pid) {
203 return pid == l2tpd_->pid();
204 }
205
206 void L2tpManager::Stop() {
207 if (l2tpd_->pid()) {
208 LOG(INFO) << "Shutting down L2TP";
209 Terminate();
210 }
211 OnStopped(false);
212 }
OLDNEW
« no previous file with comments | « l2tp_manager.h ('k') | l2tp_manager_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698