blob: 4b9a96de0c2d39b5e5a4e74f8e68966be5330391 [file] [log] [blame]
Andreea Costinas41e06442020-03-09 09:41:51 +01001// 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 Costinase45d54b2020-03-10 09:21:14 +01007#include <list>
8#include <map>
Andreea Costinas41e06442020-03-09 09:41:51 +01009#include <memory>
Andreea Costinas44cefa22020-03-09 09:07:39 +010010#include <string>
Andreea Costinase45d54b2020-03-10 09:21:14 +010011#include <vector>
Andreea Costinas41e06442020-03-09 09:41:51 +010012
13#include <base/callback_forward.h>
14#include <base/files/file_descriptor_watcher_posix.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010015#include <base/files/scoped_file.h>
16#include <base/memory/weak_ptr.h>
Andreea Costinas41e06442020-03-09 09:41:51 +010017#include <brillo/asynchronous_signal_handler.h>
Andreea Costinas44cefa22020-03-09 09:07:39 +010018#include <gtest/gtest_prod.h> // for FRIEND_TEST
Andreea Costinas41e06442020-03-09 09:41:51 +010019
Garrick Evans3388a032020-03-24 11:25:55 +090020namespace patchpanel {
Andreea Costinase45d54b2020-03-10 09:21:14 +010021class Socket;
22class SocketForwarder;
Garrick Evans3388a032020-03-24 11:25:55 +090023} // namespace patchpanel
Andreea Costinase45d54b2020-03-10 09:21:14 +010024
Andreea Costinas41e06442020-03-09 09:41:51 +010025namespace system_proxy {
26
Andreea Costinase45d54b2020-03-10 09:21:14 +010027using OnProxyResolvedCallback =
28 base::OnceCallback<void(const std::list<std::string>&)>;
Andreea Costinasbb2aa022020-06-13 00:03:23 +020029using OnAuthAcquiredCallback = base::OnceCallback<void(const std::string&)>;
Andreea Costinase45d54b2020-03-10 09:21:14 +010030
31class ProxyConnectJob;
32
Andreea Costinas41e06442020-03-09 09:41:51 +010033// ServerProxy listens for connections from the host (system services, ARC++
34// apps) and sets-up connections to the remote server.
Andreea Costinas44cefa22020-03-09 09:07:39 +010035// Note: System-proxy only supports proxying over IPv4 networks.
Andreea Costinas41e06442020-03-09 09:41:51 +010036class ServerProxy {
37 public:
38 explicit ServerProxy(base::OnceClosure quit_closure);
39 ServerProxy(const ServerProxy&) = delete;
40 ServerProxy& operator=(const ServerProxy&) = delete;
Andreea Costinas44cefa22020-03-09 09:07:39 +010041 virtual ~ServerProxy();
Andreea Costinas41e06442020-03-09 09:41:51 +010042
43 void Init();
44
Andreea Costinase45d54b2020-03-10 09:21:14 +010045 // 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 Costinasbb2aa022020-06-13 00:03:23 +020053 // 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 Costinase45d54b2020-03-10 09:21:14 +010063
Andreea Costinas44cefa22020-03-09 09:07:39 +010064 protected:
65 virtual int GetStdinPipe();
Andreea Costinas5862b102020-03-19 14:45:36 +010066 virtual int GetStdoutPipe();
Andreea Costinasb715eef2020-05-26 09:14:32 +020067 virtual void HandleStdinReadable();
Andreea Costinas44cefa22020-03-09 09:07:39 +010068
Andreea Costinas41e06442020-03-09 09:41:51 +010069 private:
Andreea Costinas44cefa22020-03-09 09:07:39 +010070 friend class ServerProxyTest;
71 FRIEND_TEST(ServerProxyTest, FetchCredentials);
72 FRIEND_TEST(ServerProxyTest, FetchListeningAddress);
Andreea Costinase45d54b2020-03-10 09:21:14 +010073 FRIEND_TEST(ServerProxyTest, HandleConnectRequest);
74 FRIEND_TEST(ServerProxyTest, HandlePendingJobs);
Andreea Costinas5862b102020-03-19 14:45:36 +010075 FRIEND_TEST(ServerProxyTest, SetupConnection);
Andreea Costinas833eb7c2020-06-12 11:09:15 +020076 FRIEND_TEST(ServerProxyTest, HandleCanceledJobWhilePendingProxyResolution);
Andreea Costinasdb2cbee2020-06-15 11:43:44 +020077 FRIEND_TEST(ServerProxyTest, HandlePendingAuthRequests);
78 FRIEND_TEST(ServerProxyTest, HandlePendingAuthRequestsCachedCredentials);
79 FRIEND_TEST(ServerProxyTest, HandlePendingAuthRequestsNoCredentials);
Andreea Costinas44cefa22020-03-09 09:07:39 +010080
Andreea Costinas41e06442020-03-09 09:41:51 +010081 bool HandleSignal(const struct signalfd_siginfo& siginfo);
82
Andreea Costinas44cefa22020-03-09 09:07:39 +010083 void CreateListeningSocket();
Andreea Costinase45d54b2020-03-10 09:21:14 +010084 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 Evans3388a032020-03-24 11:25:55 +090091 std::unique_ptr<patchpanel::SocketForwarder> fwd,
Andreea Costinase45d54b2020-03-10 09:21:14 +010092 ProxyConnectJob* connect_job);
Andreea Costinas44cefa22020-03-09 09:07:39 +010093
Andreea Costinas5862b102020-03-19 14:45:36 +010094 // 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 Costinasbb2aa022020-06-13 00:03:23 +0200100 void OnCredentialsReceived();
101
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200102 // Sets the environment variables for kerberos authentication.
103 void SetKerberosEnv(bool kerberos_enabled);
104
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200105 // 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 Costinas44cefa22020-03-09 09:07:39 +0100112 // The proxy listening address in network-byte order.
113 uint32_t listening_addr_ = 0;
114 int listening_port_;
115
Andreea Costinase45d54b2020-03-10 09:21:14 +0100116 // 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 Costinasdb2cbee2020-06-15 11:43:44 +0200118 // 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 Costinasbb2aa022020-06-13 00:03:23 +0200121 std::string system_credentials_;
Andreea Costinas5862b102020-03-19 14:45:36 +0100122
Garrick Evans3388a032020-03-24 11:25:55 +0900123 std::unique_ptr<patchpanel::Socket> listening_fd_;
Andreea Costinas44cefa22020-03-09 09:07:39 +0100124
Andreea Costinase45d54b2020-03-10 09:21:14 +0100125 // List of SocketForwarders that corresponds to the TCP tunnel between the
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200126 // local client and the remote proxy, forwarding data between the TCP
Andreea Costinase45d54b2020-03-10 09:21:14 +0100127 // 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 Evans3388a032020-03-24 11:25:55 +0900129 std::list<std::unique_ptr<patchpanel::SocketForwarder>> forwarders_;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100130
131 std::map<ProxyConnectJob*, std::unique_ptr<ProxyConnectJob>>
132 pending_connect_jobs_;
133
Andreea Costinas5862b102020-03-19 14:45:36 +0100134 // 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 Costinasdb2cbee2020-06-15 11:43:44 +0200140 // 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 Costinas41e06442020-03-09 09:41:51 +0100155 base::OnceClosure quit_closure_;
156 std::unique_ptr<base::FileDescriptorWatcher::Controller> stdin_watcher_;
Andreea Costinas44cefa22020-03-09 09:07:39 +0100157 std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_;
Andreea Costinas41e06442020-03-09 09:41:51 +0100158 brillo::AsynchronousSignalHandler signal_handler_;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100159
160 base::WeakPtrFactory<ServerProxy> weak_ptr_factory_;
Andreea Costinas41e06442020-03-09 09:41:51 +0100161};
162} // namespace system_proxy
163
164#endif // SYSTEM_PROXY_SERVER_PROXY_H_