Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 1 | // Copyright 2020 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 | #ifndef SYSTEM_PROXY_SERVER_PROXY_H_ |
| 5 | #define SYSTEM_PROXY_SERVER_PROXY_H_ |
| 6 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 7 | #include <list> |
| 8 | #include <map> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 9 | #include <memory> |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 10 | #include <string> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 11 | #include <vector> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 12 | |
| 13 | #include <base/callback_forward.h> |
| 14 | #include <base/files/file_descriptor_watcher_posix.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 15 | #include <base/files/scoped_file.h> |
| 16 | #include <base/memory/weak_ptr.h> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 17 | #include <brillo/asynchronous_signal_handler.h> |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 18 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 19 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 20 | namespace patchpanel { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 21 | class Socket; |
| 22 | class SocketForwarder; |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 23 | } // namespace patchpanel |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 24 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 25 | namespace system_proxy { |
| 26 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 27 | using OnProxyResolvedCallback = |
| 28 | base::OnceCallback<void(const std::list<std::string>&)>; |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 29 | using OnAuthAcquiredCallback = base::OnceCallback<void(const std::string&)>; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 30 | |
| 31 | class ProxyConnectJob; |
| 32 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 33 | // ServerProxy listens for connections from the host (system services, ARC++ |
| 34 | // apps) and sets-up connections to the remote server. |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 35 | // Note: System-proxy only supports proxying over IPv4 networks. |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 36 | class ServerProxy { |
| 37 | public: |
| 38 | explicit ServerProxy(base::OnceClosure quit_closure); |
| 39 | ServerProxy(const ServerProxy&) = delete; |
| 40 | ServerProxy& operator=(const ServerProxy&) = delete; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 41 | virtual ~ServerProxy(); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 42 | |
| 43 | void Init(); |
| 44 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 45 | // Creates a proxy resolution request that is forwarded to the parent process |
| 46 | // trough the standard output. When the request is resolved, the parent |
| 47 | // process will send the result trough the standard input. |
| 48 | // |callback| will be called when the proxy is resolved, with the list of |
| 49 | // proxy servers as parameter ,or in case of failure, with a list containing |
| 50 | // only the direct proxy. |
| 51 | void ResolveProxy(const std::string& target_url, |
| 52 | OnProxyResolvedCallback callback); |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 53 | // Creates an authentication required request that is forwarded to the parent |
| 54 | // process trough the standard output. When the request is resolved, the |
| 55 | // parent process will send the result trough the standard input. |callback| |
| 56 | // will be called when the credentials associated to the protection space |
| 57 | // given by the input parameters, or empty strings in case of failure or |
| 58 | // missing credentials. |
| 59 | void AuthenticationRequired(const std::string& proxy_url, |
| 60 | const std::string& scheme, |
| 61 | const std::string& realm, |
| 62 | OnAuthAcquiredCallback callback); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 63 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 64 | protected: |
| 65 | virtual int GetStdinPipe(); |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 66 | virtual int GetStdoutPipe(); |
Andreea Costinas | b715eef | 2020-05-26 09:14:32 +0200 | [diff] [blame] | 67 | virtual void HandleStdinReadable(); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 68 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 69 | private: |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 70 | friend class ServerProxyTest; |
| 71 | FRIEND_TEST(ServerProxyTest, FetchCredentials); |
| 72 | FRIEND_TEST(ServerProxyTest, FetchListeningAddress); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 73 | FRIEND_TEST(ServerProxyTest, HandleConnectRequest); |
| 74 | FRIEND_TEST(ServerProxyTest, HandlePendingJobs); |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 75 | FRIEND_TEST(ServerProxyTest, SetupConnection); |
Andreea Costinas | 833eb7c | 2020-06-12 11:09:15 +0200 | [diff] [blame] | 76 | FRIEND_TEST(ServerProxyTest, HandleCanceledJobWhilePendingProxyResolution); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 77 | FRIEND_TEST(ServerProxyTest, HandlePendingAuthRequests); |
| 78 | FRIEND_TEST(ServerProxyTest, HandlePendingAuthRequestsCachedCredentials); |
| 79 | FRIEND_TEST(ServerProxyTest, HandlePendingAuthRequestsNoCredentials); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 80 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 81 | bool HandleSignal(const struct signalfd_siginfo& siginfo); |
| 82 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 83 | void CreateListeningSocket(); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 84 | void OnConnectionAccept(); |
| 85 | |
| 86 | // Called by |ProxyConnectJob| after setting up the connection with the remote |
| 87 | // server via the remote proxy server. If the connection is successful, |fwd| |
| 88 | // corresponds to the tunnel between the client and the server that has |
| 89 | // started to forward data. In case of failure, |fwd| is empty. |
| 90 | void OnConnectionSetupFinished( |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 91 | std::unique_ptr<patchpanel::SocketForwarder> fwd, |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 92 | ProxyConnectJob* connect_job); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 93 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 94 | // Called when the proxy resolution result for |target_url| is received via |
| 95 | // the standard input (see |ResolveProxy| method). |proxy_servers| will always |
| 96 | // contain at least one entry, the direct proxy. |
| 97 | void OnProxyResolved(const std::string& target_url, |
| 98 | const std::list<std::string>& proxy_servers); |
| 99 | |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 100 | void OnCredentialsReceived(); |
| 101 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 102 | // Sets the environment variables for kerberos authentication. |
| 103 | void SetKerberosEnv(bool kerberos_enabled); |
| 104 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 105 | // Notifies proxy connect jobs which are pending authentication that |
| 106 | // credentials were provided for the protection space identified by |
| 107 | // |auth_credentials_key|. Called when the parent process sends credentials |
| 108 | // along with the associated protection space via the standard input. |
| 109 | void AuthCredentialsProvided(const std::string& auth_credentials_key, |
| 110 | const std::string& credentials); |
| 111 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 112 | // The proxy listening address in network-byte order. |
| 113 | uint32_t listening_addr_ = 0; |
| 114 | int listening_port_; |
| 115 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 116 | // The user name and password to use for proxy authentication in the format |
| 117 | // compatible with libcurl's CURLOPT_USERPWD: both user name and password URL |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 118 | // encoded and separated by colon. Only set for system traffic. If set, the |
| 119 | // credentials will be applied to any connection, regardless of the remote |
| 120 | // proxy it's connecting to or the challenge response. |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 121 | std::string system_credentials_; |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 122 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 123 | std::unique_ptr<patchpanel::Socket> listening_fd_; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 124 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 125 | // List of SocketForwarders that corresponds to the TCP tunnel between the |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 126 | // local client and the remote proxy, forwarding data between the TCP |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 127 | // connection initiated by the local client to the local proxy and the TCP |
| 128 | // connection initiated by the local proxy to the remote proxy. |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 129 | std::list<std::unique_ptr<patchpanel::SocketForwarder>> forwarders_; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 130 | |
| 131 | std::map<ProxyConnectJob*, std::unique_ptr<ProxyConnectJob>> |
| 132 | pending_connect_jobs_; |
| 133 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 134 | // Collection of ongoing proxy resolution requests. The key represents the |
| 135 | // target url to be resolved and it's mapped to a list of callbaks to pending |
| 136 | // connect jobs that are connecting to the same target url. |
| 137 | std::map<std::string, std::list<OnProxyResolvedCallback>> |
| 138 | pending_proxy_resolution_requests_; |
| 139 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 140 | // Collection of ongoing authentication requests. The key represents the |
| 141 | // ProtectionSpace proto message (proxy url, scheme and realm) associated with |
| 142 | // the request, serialized as a string. The value is a list of callbaks to |
| 143 | // pending connect jobs that are awaiting authentication and have received a |
| 144 | // challenge with the same scheme and realm from the same proxy server. |
| 145 | std::map<std::string, std::list<OnAuthAcquiredCallback>> |
| 146 | pending_auth_required_requests_; |
| 147 | |
| 148 | // Stores HTTP authentication identities acquired from the user and challenge |
| 149 | // info. The credentials are mapped by the protection space (origin, realm, |
| 150 | // scheme) and can only be used in response to challenges corresponding to |
| 151 | // this specific triple, as opposed to |system_credentials_| which, if set, |
| 152 | // can be used for any protection space. |
| 153 | std::map<std::string, std::string> auth_cache_; |
| 154 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 155 | base::OnceClosure quit_closure_; |
| 156 | std::unique_ptr<base::FileDescriptorWatcher::Controller> stdin_watcher_; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 157 | std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 158 | brillo::AsynchronousSignalHandler signal_handler_; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 159 | |
| 160 | base::WeakPtrFactory<ServerProxy> weak_ptr_factory_; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 161 | }; |
| 162 | } // namespace system_proxy |
| 163 | |
| 164 | #endif // SYSTEM_PROXY_SERVER_PROXY_H_ |