blob: e4b9e7b642b47a6932d0e43a3ec9c0d35052d41b [file] [log] [blame]
Andreea Costinasc7d5ad02020-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_SANDBOXED_WORKER_H_
5#define SYSTEM_PROXY_SANDBOXED_WORKER_H_
6
7#include <array>
Andreea Costinas5862b102020-03-19 14:45:36 +01008#include <map>
Andreea Costinasc7d5ad02020-03-09 09:41:51 +01009#include <memory>
10#include <string>
Andreea Costinas5862b102020-03-19 14:45:36 +010011#include <vector>
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010012
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 Costinas5862b102020-03-19 14:45:36 +010017#include <base/memory/weak_ptr.h>
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010018#include <chromeos/scoped_minijail.h>
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010019
Andreea Costinasdb2cbee2020-06-15 11:43:44 +020020#include "bindings/worker_common.pb.h"
21
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010022namespace system_proxy {
23
Andreea Costinas5862b102020-03-19 14:45:36 +010024class SystemProxyAdaptor;
25
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010026class SandboxedWorker {
27 public:
Andreea Costinas5862b102020-03-19 14:45:36 +010028 explicit SandboxedWorker(base::WeakPtr<SystemProxyAdaptor> adaptor);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010029 SandboxedWorker(const SandboxedWorker&) = delete;
30 SandboxedWorker& operator=(const SandboxedWorker&) = delete;
31 virtual ~SandboxedWorker() = default;
32
33 // Starts a sandboxed worker with pipes.
Andreea Costinasc9defae2020-04-22 10:28:35 +020034 virtual bool Start();
Andreea Costinasdb2cbee2020-06-15 11:43:44 +020035 // 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 Costinas922fbaf2020-05-28 11:55:22 +020038 // 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 Costinas41e06442020-03-09 09:41:51 +010044 // Sends the listening address and port to the worker via communication
Andreea Costinasa89309d2020-05-08 15:51:12 +020045 // pipes and sets |local_proxy_host_and_port_|.
Andreea Costinasc9defae2020-04-22 10:28:35 +020046 bool SetListeningAddress(uint32_t addr, int port);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010047
Andreea Costinase9c73592020-07-17 15:27:54 +020048 // Sends a request to clear the user credentials to the worker via
49 // communication pipes.
50 bool ClearUserCredentials();
51
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010052 // Terminates the child process by sending a SIGTERM signal.
53 virtual bool Stop();
54
55 virtual bool IsRunning();
56
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020057 void SetNetNamespaceLifelineFd(base::ScopedFD net_namespace_lifeline_fd);
58
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010059 pid_t pid() { return pid_; }
60
Andreea Costinasa89309d2020-05-08 15:51:12 +020061 // Returns the address of the local proxy as host:port.
62 virtual std::string local_proxy_host_and_port() {
63 return local_proxy_host_and_port_;
64 }
65
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010066 private:
Andreea Costinas41e06442020-03-09 09:41:51 +010067 friend class SystemProxyAdaptorTest;
Andreea Costinas77b180e2020-05-12 15:17:32 +020068 FRIEND_TEST(SystemProxyAdaptorTest, SetAuthenticationDetails);
Andreea Costinas922fbaf2020-05-28 11:55:22 +020069 FRIEND_TEST(SystemProxyAdaptorTest, KerberosEnabled);
Andreea Costinasa89309d2020-05-08 15:51:12 +020070 FRIEND_TEST(SystemProxyAdaptorTest, ProxyResolutionFilter);
Andreea Costinasdb2cbee2020-06-15 11:43:44 +020071 FRIEND_TEST(SystemProxyAdaptorTest, ProtectionSpaceAuthenticationRequired);
72 FRIEND_TEST(SystemProxyAdaptorTest, ProtectionSpaceNoCredentials);
Andreea Costinase9c73592020-07-17 15:27:54 +020073 FRIEND_TEST(SystemProxyAdaptorTest, ClearUserCredentials);
Andreea Costinas41e06442020-03-09 09:41:51 +010074
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010075 void OnMessageReceived();
76 void OnErrorReceived();
Andreea Costinas5862b102020-03-19 14:45:36 +010077 // Called when a proxy resolver job is resolved. |proxy_servers| is the
78 // ordered list of proxies returned by Chrome. In case of failure it will be
79 // the direct proxy.
80 void OnProxyResolved(const std::string& target_url,
81 bool success,
82 const std::vector<std::string>& proxy_servers);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010083
Andreea Costinasa89309d2020-05-08 15:51:12 +020084 std::string local_proxy_host_and_port_;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010085 bool is_being_terminated_ = false;
86 ScopedMinijail jail_;
87 base::ScopedFD stdin_pipe_;
88 base::ScopedFD stdout_pipe_;
89 base::ScopedFD stderr_pipe_;
90
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020091 // The fd will be released when the owning sandbox worker instance is
92 // destroyed. Closing this fd will signal to the patchpanel service to tear
93 // down the network namespace setup for the associated worker process.
94 base::ScopedFD net_namespace_lifeline_fd_;
95
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010096 std::unique_ptr<base::FileDescriptorWatcher::Controller> stdout_watcher_;
97 std::unique_ptr<base::FileDescriptorWatcher::Controller> stderr_watcher_;
98
Andreea Costinas5862b102020-03-19 14:45:36 +010099 // The adaptor that owns this worker.
100 base::WeakPtr<SystemProxyAdaptor> adaptor_;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100101 pid_t pid_;
Andreea Costinas5862b102020-03-19 14:45:36 +0100102 base::WeakPtrFactory<SandboxedWorker> weak_ptr_factory_{this};
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100103};
104
105} // namespace system_proxy
106
107#endif // SYSTEM_PROXY_SANDBOXED_WORKER_H_