OLD | NEW |
(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 <poll.h> |
| 6 #include <signal.h> |
| 7 #include <stddef.h> |
| 8 #include <sys/wait.h> |
| 9 |
| 10 #include <string> |
| 11 |
| 12 #include "base/command_line.h" |
| 13 #include "base/logging.h" |
| 14 #include "chromeos/process.h" |
| 15 #include "chromeos/syslog_logging.h" |
| 16 #include "gflags/gflags.h" |
| 17 #include "vpn-manager/ipsec_manager.h" |
| 18 #include "vpn-manager/l2tp_manager.h" |
| 19 |
| 20 #pragma GCC diagnostic ignored "-Wstrict-aliasing" |
| 21 DEFINE_string(client_cert_file, "", "File with IPsec client certificate"); |
| 22 DEFINE_string(client_key_file, "", "File with IPsec client private key"); |
| 23 DEFINE_string(psk_file, "", "File with IPsec pre-shared key"); |
| 24 DEFINE_string(remote, "", "VPN server address"); |
| 25 DEFINE_string(server_ca_file, "", "File with IPsec server CA"); |
| 26 #pragma GCC diagnostic error "-Wstrict-aliasing" |
| 27 |
| 28 // True if a signal has requested termination. |
| 29 static bool s_terminate_request = false; |
| 30 |
| 31 void HandleSignal(int sig_num) { |
| 32 LOG(INFO) << "Caught signal " << sig_num; |
| 33 switch(sig_num) { |
| 34 case SIGTERM: |
| 35 case SIGINT: |
| 36 s_terminate_request = true; |
| 37 break; |
| 38 case SIGALRM: |
| 39 break; |
| 40 } |
| 41 } |
| 42 |
| 43 static void InstallSignalHandlers() { |
| 44 struct sigaction sa; |
| 45 memset(&sa, 0, sizeof(sa)); |
| 46 sa.sa_handler = HandleSignal; |
| 47 sigaction(SIGTERM, &sa, NULL); |
| 48 sigaction(SIGINT, &sa, NULL); |
| 49 sigaction(SIGALRM, &sa, NULL); |
| 50 } |
| 51 |
| 52 static void LockDownUmask() { |
| 53 // Only user and group may access configuration files we create. |
| 54 umask(S_IWGRP | S_IROTH | S_IWOTH); |
| 55 } |
| 56 |
| 57 // Run the main event loop. The events to handle are: |
| 58 // 1) timeout from poll |
| 59 // 2) caught signal |
| 60 // 3) stdout/err of child process ready |
| 61 // 4) child process dies |
| 62 static void RunEventLoop(IpsecManager* ipsec, L2tpManager* l2tp) { |
| 63 do { |
| 64 int status; |
| 65 int ipsec_poll_timeout = ipsec->Poll(); |
| 66 int l2tp_poll_timeout = l2tp->Poll(); |
| 67 int poll_timeout = (ipsec_poll_timeout > l2tp_poll_timeout) ? |
| 68 ipsec_poll_timeout : l2tp_poll_timeout; |
| 69 |
| 70 const int poll_input_count = 2; |
| 71 struct pollfd poll_inputs[poll_input_count] = { |
| 72 { ipsec->output_fd(), POLLIN }, // ipsec output |
| 73 { l2tp->output_fd(), POLLIN } // l2tp output |
| 74 }; |
| 75 int poll_result = poll(poll_inputs, poll_input_count, poll_timeout); |
| 76 CHECK(poll_result >= 0 || errno == EINTR) << "Unexpected poll result"; |
| 77 |
| 78 // Check if there are any child processes to be reaped without |
| 79 // blocking. |
| 80 pid_t pid = waitpid(-1, &status, WNOHANG); |
| 81 if (pid > 0 && (ipsec->IsChild(pid) || l2tp->IsChild(pid))) { |
| 82 LOG(WARNING) << "Child process " << pid << " stopped early"; |
| 83 s_terminate_request = true; |
| 84 } |
| 85 |
| 86 if (poll_inputs[0].revents & POLLIN) |
| 87 ipsec->ProcessOutput(); |
| 88 if (poll_inputs[1].revents & POLLIN) |
| 89 l2tp->ProcessOutput(); |
| 90 } while(!ipsec->was_stopped() && !s_terminate_request); |
| 91 } |
| 92 |
| 93 int main(int argc, char* argv[]) { |
| 94 CommandLine::Init(argc, argv); |
| 95 google::ParseCommandLineFlags(&argc, &argv, true); |
| 96 int log_flags = chromeos::kLogToSyslog; |
| 97 if (isatty(STDOUT_FILENO)) log_flags |= chromeos::kLogToStderr; |
| 98 chromeos::InitLog(log_flags); |
| 99 chromeos::OpenLog("l2tpipsec_vpn", true); |
| 100 IpsecManager ipsec; |
| 101 L2tpManager l2tp; |
| 102 |
| 103 LockDownUmask(); |
| 104 |
| 105 ServiceManager::InitializeDirectories(); |
| 106 |
| 107 if (!ipsec.Initialize(1, |
| 108 FLAGS_remote, |
| 109 FLAGS_psk_file, |
| 110 FLAGS_server_ca_file, |
| 111 FLAGS_client_key_file, |
| 112 FLAGS_client_cert_file)) { |
| 113 return 1; |
| 114 } |
| 115 if (!l2tp.Initialize(FLAGS_remote)) { |
| 116 return 1; |
| 117 } |
| 118 ServiceManager::SetLayerOrder(&ipsec, &l2tp); |
| 119 |
| 120 InstallSignalHandlers(); |
| 121 CHECK(ipsec.Start()) << "Unable to start IPsec layer"; |
| 122 |
| 123 RunEventLoop(&ipsec, &l2tp); |
| 124 |
| 125 LOG(INFO) << "Shutting down..."; |
| 126 l2tp.Stop(); |
| 127 // TODO(kmixter): use error codes to distinguish login failed vs |
| 128 // connect failed. |
| 129 return 0; |
| 130 } |
OLD | NEW |