blob: 530da540b919cc165ae559872c1cd605966e0a4c [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
20namespace system_proxy {
21
Andreea Costinas5862b102020-03-19 14:45:36 +010022class SystemProxyAdaptor;
23
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010024class SandboxedWorker {
25 public:
Andreea Costinas5862b102020-03-19 14:45:36 +010026 explicit SandboxedWorker(base::WeakPtr<SystemProxyAdaptor> adaptor);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010027 SandboxedWorker(const SandboxedWorker&) = delete;
28 SandboxedWorker& operator=(const SandboxedWorker&) = delete;
29 virtual ~SandboxedWorker() = default;
30
31 // Starts a sandboxed worker with pipes.
Andreea Costinasc9defae2020-04-22 10:28:35 +020032 virtual bool Start();
Andreea Costinas41e06442020-03-09 09:41:51 +010033 // Sends the username and password to the worker via communication pipes.
34 void SetUsernameAndPassword(const std::string& username,
35 const std::string& password);
Andreea Costinas922fbaf2020-05-28 11:55:22 +020036 // Sends the availability of kerberos auth to the worker via communication
37 // pipes.
38 bool SetKerberosEnabled(bool enabled,
39 const std::string& krb5_conf_path,
40 const std::string& krb5_ccache_path);
41
Andreea Costinas41e06442020-03-09 09:41:51 +010042 // Sends the listening address and port to the worker via communication
Andreea Costinasa89309d2020-05-08 15:51:12 +020043 // pipes and sets |local_proxy_host_and_port_|.
Andreea Costinasc9defae2020-04-22 10:28:35 +020044 bool SetListeningAddress(uint32_t addr, int port);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010045
46 // Terminates the child process by sending a SIGTERM signal.
47 virtual bool Stop();
48
49 virtual bool IsRunning();
50
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020051 void SetNetNamespaceLifelineFd(base::ScopedFD net_namespace_lifeline_fd);
52
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010053 pid_t pid() { return pid_; }
54
Andreea Costinasa89309d2020-05-08 15:51:12 +020055 // Returns the address of the local proxy as host:port.
56 virtual std::string local_proxy_host_and_port() {
57 return local_proxy_host_and_port_;
58 }
59
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010060 private:
Andreea Costinas41e06442020-03-09 09:41:51 +010061 friend class SystemProxyAdaptorTest;
62 FRIEND_TEST(SystemProxyAdaptorTest, SetSystemTrafficCredentials);
Andreea Costinas77b180e2020-05-12 15:17:32 +020063 FRIEND_TEST(SystemProxyAdaptorTest, SetAuthenticationDetails);
Andreea Costinas922fbaf2020-05-28 11:55:22 +020064 FRIEND_TEST(SystemProxyAdaptorTest, KerberosEnabled);
Andreea Costinasa89309d2020-05-08 15:51:12 +020065 FRIEND_TEST(SystemProxyAdaptorTest, ProxyResolutionFilter);
Andreea Costinas41e06442020-03-09 09:41:51 +010066
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010067 void OnMessageReceived();
68 void OnErrorReceived();
Andreea Costinas5862b102020-03-19 14:45:36 +010069 // Called when a proxy resolver job is resolved. |proxy_servers| is the
70 // ordered list of proxies returned by Chrome. In case of failure it will be
71 // the direct proxy.
72 void OnProxyResolved(const std::string& target_url,
73 bool success,
74 const std::vector<std::string>& proxy_servers);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010075
Andreea Costinasa89309d2020-05-08 15:51:12 +020076 std::string local_proxy_host_and_port_;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010077 bool is_being_terminated_ = false;
78 ScopedMinijail jail_;
79 base::ScopedFD stdin_pipe_;
80 base::ScopedFD stdout_pipe_;
81 base::ScopedFD stderr_pipe_;
82
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020083 // The fd will be released when the owning sandbox worker instance is
84 // destroyed. Closing this fd will signal to the patchpanel service to tear
85 // down the network namespace setup for the associated worker process.
86 base::ScopedFD net_namespace_lifeline_fd_;
87
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010088 std::unique_ptr<base::FileDescriptorWatcher::Controller> stdout_watcher_;
89 std::unique_ptr<base::FileDescriptorWatcher::Controller> stderr_watcher_;
90
Andreea Costinas5862b102020-03-19 14:45:36 +010091 // The adaptor that owns this worker.
92 base::WeakPtr<SystemProxyAdaptor> adaptor_;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010093 pid_t pid_;
Andreea Costinas5862b102020-03-19 14:45:36 +010094 base::WeakPtrFactory<SandboxedWorker> weak_ptr_factory_{this};
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010095};
96
97} // namespace system_proxy
98
99#endif // SYSTEM_PROXY_SANDBOXED_WORKER_H_