Andreea Costinas | c7d5ad0 | 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_SANDBOXED_WORKER_H_ |
| 5 | #define SYSTEM_PROXY_SANDBOXED_WORKER_H_ |
| 6 | |
| 7 | #include <array> |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 8 | #include <map> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <string> |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 11 | #include <vector> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 12 | |
| 13 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 14 | |
| 15 | #include <base/files/file_descriptor_watcher_posix.h> |
| 16 | #include <base/files/scoped_file.h> |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 17 | #include <base/memory/weak_ptr.h> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 18 | #include <chromeos/scoped_minijail.h> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 19 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 20 | #include "bindings/worker_common.pb.h" |
| 21 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 22 | namespace system_proxy { |
| 23 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 24 | class SystemProxyAdaptor; |
| 25 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 26 | class SandboxedWorker { |
| 27 | public: |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 28 | explicit SandboxedWorker(base::WeakPtr<SystemProxyAdaptor> adaptor); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 29 | SandboxedWorker(const SandboxedWorker&) = delete; |
| 30 | SandboxedWorker& operator=(const SandboxedWorker&) = delete; |
| 31 | virtual ~SandboxedWorker() = default; |
| 32 | |
| 33 | // Starts a sandboxed worker with pipes. |
Andreea Costinas | c9defae | 2020-04-22 10:28:35 +0200 | [diff] [blame] | 34 | virtual bool Start(); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 35 | // Sends the credentials which include username, password and protection |
| 36 | // space (optional) to the worker via communication pipes. |
| 37 | void SetCredentials(const worker::Credentials& credentials); |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 38 | // Sends the availability of kerberos auth to the worker via communication |
| 39 | // pipes. |
| 40 | bool SetKerberosEnabled(bool enabled, |
| 41 | const std::string& krb5_conf_path, |
| 42 | const std::string& krb5_ccache_path); |
| 43 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 44 | // Sends the listening address and port to the worker via communication |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 45 | // pipes and sets |local_proxy_host_and_port_|. |
Andreea Costinas | c9defae | 2020-04-22 10:28:35 +0200 | [diff] [blame] | 46 | bool SetListeningAddress(uint32_t addr, int port); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 47 | |
| 48 | // Terminates the child process by sending a SIGTERM signal. |
| 49 | virtual bool Stop(); |
| 50 | |
| 51 | virtual bool IsRunning(); |
| 52 | |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 53 | void SetNetNamespaceLifelineFd(base::ScopedFD net_namespace_lifeline_fd); |
| 54 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 55 | pid_t pid() { return pid_; } |
| 56 | |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 57 | // Returns the address of the local proxy as host:port. |
| 58 | virtual std::string local_proxy_host_and_port() { |
| 59 | return local_proxy_host_and_port_; |
| 60 | } |
| 61 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 62 | private: |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 63 | friend class SystemProxyAdaptorTest; |
| 64 | FRIEND_TEST(SystemProxyAdaptorTest, SetSystemTrafficCredentials); |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 65 | FRIEND_TEST(SystemProxyAdaptorTest, SetAuthenticationDetails); |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 66 | FRIEND_TEST(SystemProxyAdaptorTest, KerberosEnabled); |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 67 | FRIEND_TEST(SystemProxyAdaptorTest, ProxyResolutionFilter); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame^] | 68 | FRIEND_TEST(SystemProxyAdaptorTest, ProtectionSpaceAuthenticationRequired); |
| 69 | FRIEND_TEST(SystemProxyAdaptorTest, ProtectionSpaceNoCredentials); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 70 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 71 | void OnMessageReceived(); |
| 72 | void OnErrorReceived(); |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 73 | // Called when a proxy resolver job is resolved. |proxy_servers| is the |
| 74 | // ordered list of proxies returned by Chrome. In case of failure it will be |
| 75 | // the direct proxy. |
| 76 | void OnProxyResolved(const std::string& target_url, |
| 77 | bool success, |
| 78 | const std::vector<std::string>& proxy_servers); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 79 | |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 80 | std::string local_proxy_host_and_port_; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 81 | bool is_being_terminated_ = false; |
| 82 | ScopedMinijail jail_; |
| 83 | base::ScopedFD stdin_pipe_; |
| 84 | base::ScopedFD stdout_pipe_; |
| 85 | base::ScopedFD stderr_pipe_; |
| 86 | |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 87 | // The fd will be released when the owning sandbox worker instance is |
| 88 | // destroyed. Closing this fd will signal to the patchpanel service to tear |
| 89 | // down the network namespace setup for the associated worker process. |
| 90 | base::ScopedFD net_namespace_lifeline_fd_; |
| 91 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 92 | std::unique_ptr<base::FileDescriptorWatcher::Controller> stdout_watcher_; |
| 93 | std::unique_ptr<base::FileDescriptorWatcher::Controller> stderr_watcher_; |
| 94 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 95 | // The adaptor that owns this worker. |
| 96 | base::WeakPtr<SystemProxyAdaptor> adaptor_; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 97 | pid_t pid_; |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 98 | base::WeakPtrFactory<SandboxedWorker> weak_ptr_factory_{this}; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | } // namespace system_proxy |
| 102 | |
| 103 | #endif // SYSTEM_PROXY_SANDBOXED_WORKER_H_ |