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