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 | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 77 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 78 | bool HandleSignal(const struct signalfd_siginfo& siginfo); |
| 79 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 80 | void CreateListeningSocket(); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 81 | void OnConnectionAccept(); |
| 82 | |
| 83 | // Called by |ProxyConnectJob| after setting up the connection with the remote |
| 84 | // server via the remote proxy server. If the connection is successful, |fwd| |
| 85 | // corresponds to the tunnel between the client and the server that has |
| 86 | // started to forward data. In case of failure, |fwd| is empty. |
| 87 | void OnConnectionSetupFinished( |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 88 | std::unique_ptr<patchpanel::SocketForwarder> fwd, |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 89 | ProxyConnectJob* connect_job); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 90 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 91 | // Called when the proxy resolution result for |target_url| is received via |
| 92 | // the standard input (see |ResolveProxy| method). |proxy_servers| will always |
| 93 | // contain at least one entry, the direct proxy. |
| 94 | void OnProxyResolved(const std::string& target_url, |
| 95 | const std::list<std::string>& proxy_servers); |
| 96 | |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 97 | void OnCredentialsReceived(); |
| 98 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 99 | // Sets the environment variables for kerberos authentication. |
| 100 | void SetKerberosEnv(bool kerberos_enabled); |
| 101 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 102 | // The proxy listening address in network-byte order. |
| 103 | uint32_t listening_addr_ = 0; |
| 104 | int listening_port_; |
| 105 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 106 | // The user name and password to use for proxy authentication in the format |
| 107 | // compatible with libcurl's CURLOPT_USERPWD: both user name and password URL |
| 108 | // encoded and separated by colon. |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 109 | std::string system_credentials_; |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 110 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 111 | std::unique_ptr<patchpanel::Socket> listening_fd_; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 112 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 113 | // List of SocketForwarders that corresponds to the TCP tunnel between the |
| 114 | // local client and the remote proxy, forwarding data between the TCP |
| 115 | // connection initiated by the local client to the local proxy and the TCP |
| 116 | // connection initiated by the local proxy to the remote proxy. |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 117 | std::list<std::unique_ptr<patchpanel::SocketForwarder>> forwarders_; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 118 | |
| 119 | std::map<ProxyConnectJob*, std::unique_ptr<ProxyConnectJob>> |
| 120 | pending_connect_jobs_; |
| 121 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 122 | // Collection of ongoing proxy resolution requests. The key represents the |
| 123 | // target url to be resolved and it's mapped to a list of callbaks to pending |
| 124 | // connect jobs that are connecting to the same target url. |
| 125 | std::map<std::string, std::list<OnProxyResolvedCallback>> |
| 126 | pending_proxy_resolution_requests_; |
| 127 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 128 | base::OnceClosure quit_closure_; |
| 129 | std::unique_ptr<base::FileDescriptorWatcher::Controller> stdin_watcher_; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 130 | std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 131 | brillo::AsynchronousSignalHandler signal_handler_; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 132 | |
| 133 | base::WeakPtrFactory<ServerProxy> weak_ptr_factory_; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 134 | }; |
| 135 | } // namespace system_proxy |
| 136 | |
| 137 | #endif // SYSTEM_PROXY_SERVER_PROXY_H_ |