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 | #ifndef SHILL_OPENVPN_DRIVER_ |
| 6 | #define SHILL_OPENVPN_DRIVER_ |
| 7 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 8 | #include <map> |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 12 | #include <base/file_path.h> |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 13 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 15 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 16 | #include "shill/glib.h" |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 17 | #include "shill/ipconfig.h" |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 18 | #include "shill/refptr_types.h" |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 19 | #include "shill/rpc_task.h" |
Darin Petkov | 3f9131c | 2012-03-20 11:37:32 +0100 | [diff] [blame] | 20 | #include "shill/service.h" |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 21 | #include "shill/sockets.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 22 | #include "shill/vpn_driver.h" |
| 23 | |
| 24 | namespace shill { |
| 25 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 26 | class ControlInterface; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 27 | class DeviceInfo; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 28 | class Error; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 29 | class EventDispatcher; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 30 | class Metrics; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 31 | class NSS; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 32 | class OpenVPNManagementServer; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 33 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 34 | class OpenVPNDriver : public VPNDriver, |
| 35 | public RPCTaskDelegate { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 36 | public: |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 37 | OpenVPNDriver(ControlInterface *control, |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 38 | EventDispatcher *dispatcher, |
| 39 | Metrics *metrics, |
| 40 | Manager *manager, |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 41 | DeviceInfo *device_info, |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 42 | GLib *glib); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 43 | virtual ~OpenVPNDriver(); |
| 44 | |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 45 | // Inherited from VPNDriver. |Connect| initiates the VPN connection by |
| 46 | // creating a tunnel device. When the device index becomes available, this |
| 47 | // instance is notified through |ClaimInterface| and resumes the connection |
| 48 | // process by setting up and spawning an external 'openvpn' process. IP |
| 49 | // configuration settings are passed back from the external process through |
| 50 | // the |Notify| RPC service method. |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 51 | virtual void Connect(const VPNServiceRefPtr &service, Error *error); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 52 | virtual bool ClaimInterface(const std::string &link_name, |
| 53 | int interface_index); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 54 | virtual void Disconnect(); |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 55 | virtual void OnConnectionDisconnected(); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 56 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 57 | virtual void OnReconnecting(); |
| 58 | |
Paul Stewart | 39964fa | 2012-04-04 09:50:25 -0700 | [diff] [blame] | 59 | virtual std::string GetProviderType() const; |
| 60 | |
Darin Petkov | 0440b9b | 2012-04-17 16:11:56 +0200 | [diff] [blame] | 61 | virtual void Cleanup(Service::ConnectState state); |
| 62 | |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 63 | // Returns true if an opton was appended. |
| 64 | bool AppendValueOption(const std::string &property, |
| 65 | const std::string &option, |
| 66 | std::vector<std::string> *options); |
| 67 | |
| 68 | // Returns true if a flag was appended. |
| 69 | bool AppendFlag(const std::string &property, |
| 70 | const std::string &option, |
| 71 | std::vector<std::string> *options); |
| 72 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 73 | private: |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 74 | friend class OpenVPNDriverTest; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 75 | FRIEND_TEST(OpenVPNDriverTest, ClaimInterface); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 76 | FRIEND_TEST(OpenVPNDriverTest, Cleanup); |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 77 | FRIEND_TEST(OpenVPNDriverTest, Connect); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 78 | FRIEND_TEST(OpenVPNDriverTest, ConnectTunnelFailure); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 79 | FRIEND_TEST(OpenVPNDriverTest, Disconnect); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 80 | FRIEND_TEST(OpenVPNDriverTest, GetRouteOptionEntry); |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 81 | FRIEND_TEST(OpenVPNDriverTest, InitEnvironment); |
Darin Petkov | 55771b7 | 2012-04-25 09:25:19 +0200 | [diff] [blame] | 82 | FRIEND_TEST(OpenVPNDriverTest, InitLoggingOptions); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 83 | FRIEND_TEST(OpenVPNDriverTest, InitManagementChannelOptions); |
| 84 | FRIEND_TEST(OpenVPNDriverTest, InitNSSOptions); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 85 | FRIEND_TEST(OpenVPNDriverTest, InitOptions); |
| 86 | FRIEND_TEST(OpenVPNDriverTest, InitOptionsNoHost); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 87 | FRIEND_TEST(OpenVPNDriverTest, InitPKCS11Options); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 88 | FRIEND_TEST(OpenVPNDriverTest, Notify); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 89 | FRIEND_TEST(OpenVPNDriverTest, NotifyFail); |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 90 | FRIEND_TEST(OpenVPNDriverTest, OnConnectionDisconnected); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 91 | FRIEND_TEST(OpenVPNDriverTest, OnOpenVPNDied); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 92 | FRIEND_TEST(OpenVPNDriverTest, OnReconnecting); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 93 | FRIEND_TEST(OpenVPNDriverTest, ParseForeignOption); |
| 94 | FRIEND_TEST(OpenVPNDriverTest, ParseForeignOptions); |
| 95 | FRIEND_TEST(OpenVPNDriverTest, ParseIPConfiguration); |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 96 | FRIEND_TEST(OpenVPNDriverTest, ParseLSBRelease); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 97 | FRIEND_TEST(OpenVPNDriverTest, ParseRouteOption); |
| 98 | FRIEND_TEST(OpenVPNDriverTest, SetRoutes); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 99 | FRIEND_TEST(OpenVPNDriverTest, SpawnOpenVPN); |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 100 | FRIEND_TEST(OpenVPNDriverTest, VerifyPaths); |
| 101 | |
| 102 | static const char kOpenVPNPath[]; |
| 103 | static const char kOpenVPNScript[]; |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 104 | static const Property kProperties[]; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 105 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 106 | static const char kLSBReleaseFile[]; |
| 107 | static const char kChromeOSReleaseName[]; |
| 108 | static const char kChromeOSReleaseVersion[]; |
| 109 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 110 | // The map is a sorted container that allows us to iterate through the options |
| 111 | // in order. |
| 112 | typedef std::map<int, std::string> ForeignOptions; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 113 | typedef std::map<int, IPConfig::Route> RouteOptions; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 114 | |
| 115 | static void ParseIPConfiguration( |
| 116 | const std::map<std::string, std::string> &configuration, |
| 117 | IPConfig::Properties *properties); |
| 118 | static void ParseForeignOptions(const ForeignOptions &options, |
| 119 | IPConfig::Properties *properties); |
| 120 | static void ParseForeignOption(const std::string &option, |
| 121 | IPConfig::Properties *properties); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 122 | static IPConfig::Route *GetRouteOptionEntry(const std::string &prefix, |
| 123 | const std::string &key, |
| 124 | RouteOptions *routes); |
| 125 | static void ParseRouteOption(const std::string &key, |
| 126 | const std::string &value, |
| 127 | RouteOptions *routes); |
| 128 | static void SetRoutes(const RouteOptions &routes, |
| 129 | IPConfig::Properties *properties); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 130 | |
| 131 | void InitOptions(std::vector<std::string> *options, Error *error); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 132 | bool InitNSSOptions(std::vector<std::string> *options, Error *error); |
| 133 | void InitPKCS11Options(std::vector<std::string> *options); |
| 134 | bool InitManagementChannelOptions( |
| 135 | std::vector<std::string> *options, Error *error); |
Darin Petkov | 55771b7 | 2012-04-25 09:25:19 +0200 | [diff] [blame] | 136 | void InitLoggingOptions(std::vector<std::string> *options); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 137 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 138 | void InitEnvironment(std::vector<std::string> *environment); |
| 139 | bool ParseLSBRelease(std::map<std::string, std::string> *lsb_release); |
| 140 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 141 | bool SpawnOpenVPN(); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 142 | |
| 143 | // Called when the openpvn process exits. |
| 144 | static void OnOpenVPNDied(GPid pid, gint status, gpointer data); |
| 145 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 146 | // Inherit from VPNDriver to add custom properties. |
| 147 | virtual KeyValueStore GetProvider(Error *error); |
| 148 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 149 | // Implements RPCTaskDelegate. |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 150 | virtual void GetLogin(std::string *user, std::string *password); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 151 | virtual void Notify(const std::string &reason, |
| 152 | const std::map<std::string, std::string> &dict); |
| 153 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 154 | ControlInterface *control_; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 155 | EventDispatcher *dispatcher_; |
| 156 | Metrics *metrics_; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 157 | DeviceInfo *device_info_; |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 158 | GLib *glib_; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 159 | Sockets sockets_; |
| 160 | scoped_ptr<OpenVPNManagementServer> management_server_; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 161 | NSS *nss_; |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 162 | FilePath lsb_release_file_; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 163 | |
| 164 | VPNServiceRefPtr service_; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 165 | scoped_ptr<RPCTask> rpc_task_; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 166 | std::string tunnel_interface_; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 167 | VPNRefPtr device_; |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 168 | FilePath tls_auth_file_; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 169 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 170 | // The PID of the spawned openvpn process. May be 0 if no process has been |
| 171 | // spawned yet or the process has died. |
| 172 | int pid_; |
| 173 | |
| 174 | // Child exit watch callback source tag. |
| 175 | unsigned int child_watch_tag_; |
| 176 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 177 | DISALLOW_COPY_AND_ASSIGN(OpenVPNDriver); |
| 178 | }; |
| 179 | |
| 180 | } // namespace shill |
| 181 | |
| 182 | #endif // SHILL_OPENVPN_DRIVER_ |