blob: c3ca2705351d12bd68e18cae9ab6627096a609c6 [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 Costinase9c73592020-07-17 15:27:54 +020080 FRIEND_TEST(ServerProxyTest, ClearUserCredentials);
Andreea Costinas44cefa22020-03-09 09:07:39 +010081
Andreea Costinas41e06442020-03-09 09:41:51 +010082 bool HandleSignal(const struct signalfd_siginfo& siginfo);
83
Andreea Costinas44cefa22020-03-09 09:07:39 +010084 void CreateListeningSocket();
Andreea Costinase45d54b2020-03-10 09:21:14 +010085 void OnConnectionAccept();
86
87 // Called by |ProxyConnectJob| after setting up the connection with the remote
88 // server via the remote proxy server. If the connection is successful, |fwd|
89 // corresponds to the tunnel between the client and the server that has
90 // started to forward data. In case of failure, |fwd| is empty.
91 void OnConnectionSetupFinished(
Garrick Evans3388a032020-03-24 11:25:55 +090092 std::unique_ptr<patchpanel::SocketForwarder> fwd,
Andreea Costinase45d54b2020-03-10 09:21:14 +010093 ProxyConnectJob* connect_job);
Andreea Costinas44cefa22020-03-09 09:07:39 +010094
Andreea Costinas5862b102020-03-19 14:45:36 +010095 // Called when the proxy resolution result for |target_url| is received via
96 // the standard input (see |ResolveProxy| method). |proxy_servers| will always
97 // contain at least one entry, the direct proxy.
98 void OnProxyResolved(const std::string& target_url,
99 const std::list<std::string>& proxy_servers);
100
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200101 void OnCredentialsReceived();
102
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200103 // Sets the environment variables for kerberos authentication.
104 void SetKerberosEnv(bool kerberos_enabled);
105
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200106 // Notifies proxy connect jobs which are pending authentication that
107 // credentials were provided for the protection space identified by
108 // |auth_credentials_key|. Called when the parent process sends credentials
109 // along with the associated protection space via the standard input.
110 void AuthCredentialsProvided(const std::string& auth_credentials_key,
111 const std::string& credentials);
112
Andreea Costinas44cefa22020-03-09 09:07:39 +0100113 // The proxy listening address in network-byte order.
114 uint32_t listening_addr_ = 0;
115 int listening_port_;
116
Andreea Costinase45d54b2020-03-10 09:21:14 +0100117 // The user name and password to use for proxy authentication in the format
118 // compatible with libcurl's CURLOPT_USERPWD: both user name and password URL
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200119 // encoded and separated by colon. Only set for system traffic. If set, the
120 // credentials will be applied to any connection, regardless of the remote
121 // proxy it's connecting to or the challenge response.
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200122 std::string system_credentials_;
Andreea Costinas5862b102020-03-19 14:45:36 +0100123
Garrick Evans3388a032020-03-24 11:25:55 +0900124 std::unique_ptr<patchpanel::Socket> listening_fd_;
Andreea Costinas44cefa22020-03-09 09:07:39 +0100125
Andreea Costinase45d54b2020-03-10 09:21:14 +0100126 // List of SocketForwarders that corresponds to the TCP tunnel between the
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200127 // local client and the remote proxy, forwarding data between the TCP
Andreea Costinase45d54b2020-03-10 09:21:14 +0100128 // connection initiated by the local client to the local proxy and the TCP
129 // connection initiated by the local proxy to the remote proxy.
Garrick Evans3388a032020-03-24 11:25:55 +0900130 std::list<std::unique_ptr<patchpanel::SocketForwarder>> forwarders_;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100131
132 std::map<ProxyConnectJob*, std::unique_ptr<ProxyConnectJob>>
133 pending_connect_jobs_;
134
Andreea Costinas5862b102020-03-19 14:45:36 +0100135 // Collection of ongoing proxy resolution requests. The key represents the
136 // target url to be resolved and it's mapped to a list of callbaks to pending
137 // connect jobs that are connecting to the same target url.
138 std::map<std::string, std::list<OnProxyResolvedCallback>>
139 pending_proxy_resolution_requests_;
140
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200141 // Collection of ongoing authentication requests. The key represents the
142 // ProtectionSpace proto message (proxy url, scheme and realm) associated with
143 // the request, serialized as a string. The value is a list of callbaks to
144 // pending connect jobs that are awaiting authentication and have received a
145 // challenge with the same scheme and realm from the same proxy server.
146 std::map<std::string, std::list<OnAuthAcquiredCallback>>
147 pending_auth_required_requests_;
148
149 // Stores HTTP authentication identities acquired from the user and challenge
150 // info. The credentials are mapped by the protection space (origin, realm,
151 // scheme) and can only be used in response to challenges corresponding to
152 // this specific triple, as opposed to |system_credentials_| which, if set,
153 // can be used for any protection space.
154 std::map<std::string, std::string> auth_cache_;
155
Andreea Costinas41e06442020-03-09 09:41:51 +0100156 base::OnceClosure quit_closure_;
157 std::unique_ptr<base::FileDescriptorWatcher::Controller> stdin_watcher_;
Andreea Costinas44cefa22020-03-09 09:07:39 +0100158 std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_;
Andreea Costinas41e06442020-03-09 09:41:51 +0100159 brillo::AsynchronousSignalHandler signal_handler_;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100160
161 base::WeakPtrFactory<ServerProxy> weak_ptr_factory_;
Andreea Costinas41e06442020-03-09 09:41:51 +0100162};
163} // namespace system_proxy
164
165#endif // SYSTEM_PROXY_SERVER_PROXY_H_