blob: e370b6fabd18653e126cc2ab894a2f5840ef3ca7 [file] [log] [blame]
[email protected]d2e6d592012-02-03 21:49:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]861c6c62009-04-20 16:50:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Lily Houghton582d4622018-01-22 22:43:405#include "net/proxy_resolution/proxy_config_service_linux.h"
[email protected]861c6c62009-04-20 16:50:566
[email protected]d7395e732009-08-28 23:13:437#include <errno.h>
[email protected]d7395e732009-08-28 23:13:438#include <limits.h>
[email protected]d7395e732009-08-28 23:13:439#include <sys/inotify.h>
10#include <unistd.h>
[email protected]861c6c62009-04-20 16:50:5611
[email protected]9bc8cff2010-04-03 01:05:3912#include <map>
Peter Boström08e7ed82021-04-19 17:49:5913#include <memory>
thestig0c412e852016-06-30 08:04:4014#include <utility>
[email protected]9bc8cff2010-04-03 01:05:3915
[email protected]6af889c2011-10-06 23:11:4116#include "base/bind.h"
gabf4f904e2017-05-10 20:55:0217#include "base/files/file_descriptor_watcher_posix.h"
[email protected]57999812013-02-24 05:40:5218#include "base/files/file_path.h"
thestigd8df0332014-09-04 06:33:2919#include "base/files/file_util.h"
[email protected]b9b4a572014-03-17 23:11:1220#include "base/files/scoped_file.h"
[email protected]861c6c62009-04-20 16:50:5621#include "base/logging.h"
Avi Drissman13fc8932015-12-20 04:40:4622#include "base/macros.h"
Eric Romancd032fb62018-05-18 21:40:1323#include "base/memory/ptr_util.h"
[email protected]3a29593d2011-04-11 10:07:5224#include "base/nix/xdg_util.h"
eroman0070d412017-06-22 22:18:2425#include "base/sequenced_task_runner.h"
[email protected]76722472012-05-24 08:26:4626#include "base/single_thread_task_runner.h"
[email protected]fc9be5802013-06-11 10:56:5127#include "base/strings/string_number_conversions.h"
[email protected]f4ebe772013-02-02 00:21:3928#include "base/strings/string_tokenizer.h"
[email protected]66e96c42013-06-28 15:20:3129#include "base/strings/string_util.h"
Gabriel Charette44db1422018-08-06 11:19:3330#include "base/task/post_task.h"
31#include "base/task/task_traits.h"
Gabriel Charette99f5df32021-03-19 19:55:5532#include "base/task/thread_pool.h"
[email protected]9a8c4022011-01-25 14:25:3333#include "base/threading/thread_restrictions.h"
[email protected]66e96c42013-06-28 15:20:3134#include "base/timer/timer.h"
Lily Houghton582d4622018-01-22 22:43:4035#include "net/base/proxy_server.h"
Eric Orth5ccc3f02021-09-23 00:01:5736#include "net/base/proxy_string_util.h"
[email protected]861c6c62009-04-20 16:50:5637
[email protected]3fc24f52012-11-30 21:22:3438#if defined(USE_GIO)
Tim Brown1c307cc2017-12-08 02:40:3839#include <gio/gio.h>
[email protected]3fc24f52012-11-30 21:22:3440#endif // defined(USE_GIO)
41
[email protected]861c6c62009-04-20 16:50:5642namespace net {
43
44namespace {
45
Shimi Zhang13eace252020-01-31 01:49:1946// This turns all rules with a hostname into wildcard matches, which will
47// match not just the indicated hostname but also any hostname that ends with
48// it.
49void RewriteRulesForSuffixMatching(ProxyBypassRules* out) {
50 // Prepend a wildcard (*) to any hostname based rules, provided it isn't an IP
51 // address.
52 for (size_t i = 0; i < out->rules().size(); ++i) {
53 if (!out->rules()[i]->IsHostnamePatternRule())
54 continue;
55
56 const SchemeHostPortMatcherHostnamePatternRule* prev_rule =
57 static_cast<const SchemeHostPortMatcherHostnamePatternRule*>(
58 out->rules()[i].get());
59 out->ReplaceRule(i, prev_rule->GenerateSuffixMatchingRule());
60 }
61}
62
[email protected]861c6c62009-04-20 16:50:5663// Given a proxy hostname from a setting, returns that hostname with
64// an appropriate proxy server scheme prefix.
65// scheme indicates the desired proxy scheme: usually http, with
66// socks 4 or 5 as special cases.
[email protected]87a102b2009-07-14 05:23:3067// TODO(arindam): Remove URI string manipulation by using MapUrlSchemeToProxy.
[email protected]861c6c62009-04-20 16:50:5668std::string FixupProxyHostScheme(ProxyServer::Scheme scheme,
69 std::string host) {
[email protected]e8c50812010-09-28 00:16:1770 if (scheme == ProxyServer::SCHEME_SOCKS5 &&
brettw3a2c6902015-07-06 19:43:2971 base::StartsWith(host, "socks4://",
72 base::CompareCase::INSENSITIVE_ASCII)) {
[email protected]e8c50812010-09-28 00:16:1773 // We default to socks 5, but if the user specifically set it to
74 // socks4://, then use that.
75 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:5676 }
77 // Strip the scheme if any.
78 std::string::size_type colon = host.find("://");
79 if (colon != std::string::npos)
80 host = host.substr(colon + 3);
81 // If a username and perhaps password are specified, give a warning.
82 std::string::size_type at_sign = host.find("@");
83 // Should this be supported?
84 if (at_sign != std::string::npos) {
[email protected]62749f182009-07-15 13:16:5485 // ProxyConfig does not support authentication parameters, but Chrome
86 // will prompt for the password later. Disregard the
87 // authentication parameters and continue with this hostname.
88 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
[email protected]861c6c62009-04-20 16:50:5689 host = host.substr(at_sign + 1);
90 }
91 // If this is a socks proxy, prepend a scheme so as to tell
92 // ProxyServer. This also allows ProxyServer to choose the right
93 // default port.
94 if (scheme == ProxyServer::SCHEME_SOCKS4)
95 host = "socks4://" + host;
96 else if (scheme == ProxyServer::SCHEME_SOCKS5)
97 host = "socks5://" + host;
[email protected]d7395e732009-08-28 23:13:4398 // If there is a trailing slash, remove it so |host| will parse correctly
99 // even if it includes a port number (since the slash is not numeric).
pkasting9022cb42016-02-05 00:08:56100 if (!host.empty() && host.back() == '/')
[email protected]d7395e732009-08-28 23:13:43101 host.resize(host.length() - 1);
[email protected]861c6c62009-04-20 16:50:56102 return host;
103}
104
Ramin Halavatica8d5252018-03-12 05:33:49105ProxyConfigWithAnnotation GetConfigOrDirect(
Anton Bikineev068d2912021-05-15 20:43:52106 const absl::optional<ProxyConfigWithAnnotation>& optional_config) {
Eric Roman750af4b12018-02-22 22:38:53107 if (optional_config)
108 return optional_config.value();
109
Ramin Halavatica8d5252018-03-12 05:33:49110 ProxyConfigWithAnnotation config = ProxyConfigWithAnnotation::CreateDirect();
Eric Roman750af4b12018-02-22 22:38:53111 return config;
112}
113
[email protected]861c6c62009-04-20 16:50:56114} // namespace
115
Chris Watkins3a13f632017-12-04 00:41:15116ProxyConfigServiceLinux::Delegate::~Delegate() = default;
[email protected]8e1845e12010-09-15 19:22:24117
[email protected]3e44697f2009-05-22 14:37:39118bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme(
thestig0c412e852016-06-30 08:04:40119 base::StringPiece variable,
120 ProxyServer::Scheme scheme,
[email protected]861c6c62009-04-20 16:50:56121 ProxyServer* result_server) {
122 std::string env_value;
thestig0c412e852016-06-30 08:04:40123 if (!env_var_getter_->GetVar(variable, &env_value))
124 return false;
125
126 if (env_value.empty())
127 return false;
128
129 env_value = FixupProxyHostScheme(scheme, env_value);
130 ProxyServer proxy_server =
Eric Orth5ccc3f02021-09-23 00:01:57131 ProxyUriToProxyServer(env_value, ProxyServer::SCHEME_HTTP);
thestig0c412e852016-06-30 08:04:40132 if (proxy_server.is_valid() && !proxy_server.is_direct()) {
133 *result_server = proxy_server;
134 return true;
[email protected]861c6c62009-04-20 16:50:56135 }
thestig0c412e852016-06-30 08:04:40136 LOG(ERROR) << "Failed to parse environment variable " << variable;
[email protected]861c6c62009-04-20 16:50:56137 return false;
138}
139
[email protected]3e44697f2009-05-22 14:37:39140bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVar(
thestig0c412e852016-06-30 08:04:40141 base::StringPiece variable,
142 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:56143 return GetProxyFromEnvVarForScheme(variable, ProxyServer::SCHEME_HTTP,
144 result_server);
145}
146
Anton Bikineev068d2912021-05-15 20:43:52147absl::optional<ProxyConfigWithAnnotation>
Eric Roman750af4b12018-02-22 22:38:53148ProxyConfigServiceLinux::Delegate::GetConfigFromEnv() {
Ramin Halavatica8d5252018-03-12 05:33:49149 ProxyConfig config;
Eric Roman750af4b12018-02-22 22:38:53150
[email protected]861c6c62009-04-20 16:50:56151 // Check for automatic configuration first, in
152 // "auto_proxy". Possibly only the "environment_proxy" firefox
153 // extension has ever used this, but it still sounds like a good
154 // idea.
155 std::string auto_proxy;
[email protected]3ba7e082010-08-07 02:57:59156 if (env_var_getter_->GetVar("auto_proxy", &auto_proxy)) {
[email protected]861c6c62009-04-20 16:50:56157 if (auto_proxy.empty()) {
158 // Defined and empty => autodetect
Ramin Halavatica8d5252018-03-12 05:33:49159 config.set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:56160 } else {
161 // specified autoconfig URL
Ramin Halavatica8d5252018-03-12 05:33:49162 config.set_pac_url(GURL(auto_proxy));
[email protected]861c6c62009-04-20 16:50:56163 }
Ramin Halavatica8d5252018-03-12 05:33:49164 return ProxyConfigWithAnnotation(
165 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:56166 }
167 // "all_proxy" is a shortcut to avoid defining {http,https,ftp}_proxy.
168 ProxyServer proxy_server;
169 if (GetProxyFromEnvVar("all_proxy", &proxy_server)) {
Ramin Halavatica8d5252018-03-12 05:33:49170 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
171 config.proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56172 } else {
173 bool have_http = GetProxyFromEnvVar("http_proxy", &proxy_server);
174 if (have_http)
Ramin Halavatica8d5252018-03-12 05:33:49175 config.proxy_rules().proxies_for_http.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56176 // It would be tempting to let http_proxy apply for all protocols
177 // if https_proxy and ftp_proxy are not defined. Googling turns up
178 // several documents that mention only http_proxy. But then the
179 // user really might not want to proxy https. And it doesn't seem
180 // like other apps do this. So we will refrain.
181 bool have_https = GetProxyFromEnvVar("https_proxy", &proxy_server);
182 if (have_https)
Ramin Halavatica8d5252018-03-12 05:33:49183 config.proxy_rules().proxies_for_https.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56184 bool have_ftp = GetProxyFromEnvVar("ftp_proxy", &proxy_server);
185 if (have_ftp)
Ramin Halavatica8d5252018-03-12 05:33:49186 config.proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56187 if (have_http || have_https || have_ftp) {
188 // mustn't change type unless some rules are actually set.
Ramin Halavatica8d5252018-03-12 05:33:49189 config.proxy_rules().type =
Lily Houghtone6b617e2018-01-19 20:13:07190 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
[email protected]861c6c62009-04-20 16:50:56191 }
192 }
Ramin Halavatica8d5252018-03-12 05:33:49193 if (config.proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56194 // If the above were not defined, try for socks.
[email protected]e8c50812010-09-28 00:16:17195 // For environment variables, we default to version 5, per the gnome
196 // documentation: http://library.gnome.org/devel/gnet/stable/gnet-socks.html
197 ProxyServer::Scheme scheme = ProxyServer::SCHEME_SOCKS5;
[email protected]861c6c62009-04-20 16:50:56198 std::string env_version;
[email protected]3ba7e082010-08-07 02:57:59199 if (env_var_getter_->GetVar("SOCKS_VERSION", &env_version)
[email protected]e8c50812010-09-28 00:16:17200 && env_version == "4")
201 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:56202 if (GetProxyFromEnvVarForScheme("SOCKS_SERVER", scheme, &proxy_server)) {
Ramin Halavatica8d5252018-03-12 05:33:49203 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
204 config.proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56205 }
206 }
207 // Look for the proxy bypass list.
208 std::string no_proxy;
[email protected]3ba7e082010-08-07 02:57:59209 env_var_getter_->GetVar("no_proxy", &no_proxy);
Ramin Halavatica8d5252018-03-12 05:33:49210 if (config.proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56211 // Having only "no_proxy" set, presumably to "*", makes it
212 // explicit that env vars do specify a configuration: having no
213 // rules specified only means the user explicitly asks for direct
214 // connections.
Ramin Halavatica8d5252018-03-12 05:33:49215 return !no_proxy.empty()
216 ? ProxyConfigWithAnnotation(
217 config, NetworkTrafficAnnotationTag(traffic_annotation_))
Anton Bikineev068d2912021-05-15 20:43:52218 : absl::optional<ProxyConfigWithAnnotation>();
[email protected]861c6c62009-04-20 16:50:56219 }
[email protected]7541206c2010-02-19 20:24:06220 // Note that this uses "suffix" matching. So a bypass of "google.com"
221 // is understood to mean a bypass of "*google.com".
Shimi Zhang13eace252020-01-31 01:49:19222 config.proxy_rules().bypass_rules.ParseFromString(no_proxy);
223 RewriteRulesForSuffixMatching(&config.proxy_rules().bypass_rules);
224
Ramin Halavatica8d5252018-03-12 05:33:49225 return ProxyConfigWithAnnotation(
226 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:56227}
228
229namespace {
230
[email protected]d7395e732009-08-28 23:13:43231const int kDebounceTimeoutMilliseconds = 250;
[email protected]3e44697f2009-05-22 14:37:39232
[email protected]8c20e3d2011-05-19 21:03:57233#if defined(USE_GIO)
Tim Brown1c307cc2017-12-08 02:40:38234const char kProxyGSettingsSchema[] = "org.gnome.system.proxy";
[email protected]2297bb22014-06-19 06:30:14235
[email protected]8c20e3d2011-05-19 21:03:57236// This setting getter uses gsettings, as used in most GNOME 3 desktops.
237class SettingGetterImplGSettings
238 : public ProxyConfigServiceLinux::SettingGetter {
239 public:
danakj8c3eb802015-09-24 07:53:00240 SettingGetterImplGSettings()
thestig0c412e852016-06-30 08:04:40241 : client_(nullptr),
242 http_client_(nullptr),
243 https_client_(nullptr),
244 ftp_client_(nullptr),
245 socks_client_(nullptr),
246 notify_delegate_(nullptr),
danakj8c3eb802015-09-24 07:53:00247 debounce_timer_(new base::OneShotTimer()) {}
[email protected]8c20e3d2011-05-19 21:03:57248
Peter Boström293b1342021-09-22 17:31:43249 SettingGetterImplGSettings(const SettingGetterImplGSettings&) = delete;
250 SettingGetterImplGSettings& operator=(const SettingGetterImplGSettings&) =
251 delete;
252
dcheng67be2b1f2014-10-27 21:47:29253 ~SettingGetterImplGSettings() override {
[email protected]8c20e3d2011-05-19 21:03:57254 // client_ should have been released before now, from
255 // Delegate::OnDestroy(), while running on the UI thread. However
256 // on exiting the process, it may happen that
257 // Delegate::OnDestroy() task is left pending on the glib loop
258 // after the loop was quit, and pending tasks may then be deleted
259 // without being run.
260 if (client_) {
Tim Brown2a19f3b2017-12-12 01:08:40261 // gsettings client was not cleaned up.
eroman0070d412017-06-22 22:18:24262 if (task_runner_->RunsTasksInCurrentSequence()) {
Mostyn Bramley-Moore699c5312018-05-01 10:48:09263 // We are on the UI thread so we can clean it safely.
[email protected]8c20e3d2011-05-19 21:03:57264 VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client";
265 ShutDown();
266 } else {
267 LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client";
thestig0c412e852016-06-30 08:04:40268 client_ = nullptr;
[email protected]8c20e3d2011-05-19 21:03:57269 }
270 }
271 DCHECK(!client_);
[email protected]8c20e3d2011-05-19 21:03:57272 }
273
Tim Brown1c307cc2017-12-08 02:40:38274 // CheckVersion() must be called *before* Init()!
275 bool CheckVersion(base::Environment* env);
[email protected]8c20e3d2011-05-19 21:03:57276
eroman0070d412017-06-22 22:18:24277 bool Init(const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner)
mostynbba063d6032014-10-09 11:01:13278 override {
eroman0070d412017-06-22 22:18:24279 DCHECK(glib_task_runner->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57280 DCHECK(!client_);
[email protected]90499482013-06-01 00:39:50281 DCHECK(!task_runner_.get());
[email protected]4cf80f0b2011-05-20 20:30:26282
Tim Brown1c307cc2017-12-08 02:40:38283 if (!g_settings_schema_source_lookup(g_settings_schema_source_get_default(),
284 kProxyGSettingsSchema, FALSE) ||
285 !(client_ = g_settings_new(kProxyGSettingsSchema))) {
[email protected]8c20e3d2011-05-19 21:03:57286 // It's not clear whether/when this can return NULL.
287 LOG(ERROR) << "Unable to create a gsettings client";
288 return false;
289 }
sergeyu3f923062014-09-05 01:39:40290 task_runner_ = glib_task_runner;
[email protected]8c20e3d2011-05-19 21:03:57291 // We assume these all work if the above call worked.
Tim Brown1c307cc2017-12-08 02:40:38292 http_client_ = g_settings_get_child(client_, "http");
293 https_client_ = g_settings_get_child(client_, "https");
294 ftp_client_ = g_settings_get_child(client_, "ftp");
295 socks_client_ = g_settings_get_child(client_, "socks");
[email protected]8c20e3d2011-05-19 21:03:57296 DCHECK(http_client_ && https_client_ && ftp_client_ && socks_client_);
297 return true;
298 }
299
dcheng67be2b1f2014-10-27 21:47:29300 void ShutDown() override {
[email protected]8c20e3d2011-05-19 21:03:57301 if (client_) {
eroman0070d412017-06-22 22:18:24302 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57303 // This also disables gsettings notifications.
304 g_object_unref(socks_client_);
305 g_object_unref(ftp_client_);
306 g_object_unref(https_client_);
307 g_object_unref(http_client_);
308 g_object_unref(client_);
309 // We only need to null client_ because it's the only one that we check.
thestig0c412e852016-06-30 08:04:40310 client_ = nullptr;
311 task_runner_ = nullptr;
[email protected]8c20e3d2011-05-19 21:03:57312 }
marshall8e5fe942015-03-06 19:22:40313 debounce_timer_.reset();
[email protected]8c20e3d2011-05-19 21:03:57314 }
315
dcheng67be2b1f2014-10-27 21:47:29316 bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13317 ProxyConfigServiceLinux::Delegate* delegate) override {
[email protected]8c20e3d2011-05-19 21:03:57318 DCHECK(client_);
eroman0070d412017-06-22 22:18:24319 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57320 notify_delegate_ = delegate;
321 // We could watch for the change-event signal instead of changed, but
322 // since we have to watch more than one object, we'd still have to
323 // debounce change notifications. This is conceptually simpler.
324 g_signal_connect(G_OBJECT(client_), "changed",
325 G_CALLBACK(OnGSettingsChangeNotification), this);
326 g_signal_connect(G_OBJECT(http_client_), "changed",
327 G_CALLBACK(OnGSettingsChangeNotification), this);
328 g_signal_connect(G_OBJECT(https_client_), "changed",
329 G_CALLBACK(OnGSettingsChangeNotification), this);
330 g_signal_connect(G_OBJECT(ftp_client_), "changed",
331 G_CALLBACK(OnGSettingsChangeNotification), this);
332 g_signal_connect(G_OBJECT(socks_client_), "changed",
333 G_CALLBACK(OnGSettingsChangeNotification), this);
334 // Simulate a change to avoid possibly losing updates before this point.
335 OnChangeNotification();
336 return true;
337 }
338
eroman0070d412017-06-22 22:18:24339 const scoped_refptr<base::SequencedTaskRunner>& GetNotificationTaskRunner()
dcheng67be2b1f2014-10-27 21:47:29340 override {
sergeyu3f923062014-09-05 01:39:40341 return task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57342 }
343
dcheng67be2b1f2014-10-27 21:47:29344 bool GetString(StringSetting key, std::string* result) override {
[email protected]8c20e3d2011-05-19 21:03:57345 DCHECK(client_);
346 switch (key) {
347 case PROXY_MODE:
348 return GetStringByPath(client_, "mode", result);
349 case PROXY_AUTOCONF_URL:
350 return GetStringByPath(client_, "autoconfig-url", result);
351 case PROXY_HTTP_HOST:
352 return GetStringByPath(http_client_, "host", result);
353 case PROXY_HTTPS_HOST:
354 return GetStringByPath(https_client_, "host", result);
355 case PROXY_FTP_HOST:
356 return GetStringByPath(ftp_client_, "host", result);
357 case PROXY_SOCKS_HOST:
358 return GetStringByPath(socks_client_, "host", result);
[email protected]8c20e3d2011-05-19 21:03:57359 }
[email protected]6b5fe742011-05-20 21:46:48360 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57361 }
dcheng67be2b1f2014-10-27 21:47:29362 bool GetBool(BoolSetting key, bool* result) override {
[email protected]8c20e3d2011-05-19 21:03:57363 DCHECK(client_);
364 switch (key) {
365 case PROXY_USE_HTTP_PROXY:
366 // Although there is an "enabled" boolean in http_client_, it is not set
367 // to true by the proxy config utility. We ignore it and return false.
368 return false;
369 case PROXY_USE_SAME_PROXY:
370 // Similarly, although there is a "use-same-proxy" boolean in client_,
371 // it is never set to false by the proxy config utility. We ignore it.
372 return false;
373 case PROXY_USE_AUTHENTICATION:
374 // There is also no way to set this in the proxy config utility, but it
375 // doesn't hurt us to get the actual setting (unlike the two above).
376 return GetBoolByPath(http_client_, "use-authentication", result);
[email protected]8c20e3d2011-05-19 21:03:57377 }
[email protected]6b5fe742011-05-20 21:46:48378 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57379 }
dcheng67be2b1f2014-10-27 21:47:29380 bool GetInt(IntSetting key, int* result) override {
[email protected]8c20e3d2011-05-19 21:03:57381 DCHECK(client_);
382 switch (key) {
383 case PROXY_HTTP_PORT:
384 return GetIntByPath(http_client_, "port", result);
385 case PROXY_HTTPS_PORT:
386 return GetIntByPath(https_client_, "port", result);
387 case PROXY_FTP_PORT:
388 return GetIntByPath(ftp_client_, "port", result);
389 case PROXY_SOCKS_PORT:
390 return GetIntByPath(socks_client_, "port", result);
[email protected]8c20e3d2011-05-19 21:03:57391 }
[email protected]6b5fe742011-05-20 21:46:48392 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57393 }
dcheng67be2b1f2014-10-27 21:47:29394 bool GetStringList(StringListSetting key,
395 std::vector<std::string>* result) override {
[email protected]8c20e3d2011-05-19 21:03:57396 DCHECK(client_);
397 switch (key) {
398 case PROXY_IGNORE_HOSTS:
399 return GetStringListByPath(client_, "ignore-hosts", result);
[email protected]8c20e3d2011-05-19 21:03:57400 }
[email protected]6b5fe742011-05-20 21:46:48401 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57402 }
403
dcheng67be2b1f2014-10-27 21:47:29404 bool BypassListIsReversed() override {
[email protected]8c20e3d2011-05-19 21:03:57405 // This is a KDE-specific setting.
406 return false;
407 }
408
Shimi Zhang13eace252020-01-31 01:49:19409 bool UseSuffixMatching() override { return false; }
[email protected]8c20e3d2011-05-19 21:03:57410
411 private:
thestig0c412e852016-06-30 08:04:40412 bool GetStringByPath(GSettings* client,
413 base::StringPiece key,
[email protected]8c20e3d2011-05-19 21:03:57414 std::string* result) {
eroman0070d412017-06-22 22:18:24415 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38416 gchar* value = g_settings_get_string(client, key.data());
[email protected]8c20e3d2011-05-19 21:03:57417 if (!value)
418 return false;
419 *result = value;
420 g_free(value);
421 return true;
422 }
thestig0c412e852016-06-30 08:04:40423 bool GetBoolByPath(GSettings* client, base::StringPiece key, bool* result) {
eroman0070d412017-06-22 22:18:24424 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38425 *result = static_cast<bool>(g_settings_get_boolean(client, key.data()));
[email protected]8c20e3d2011-05-19 21:03:57426 return true;
427 }
thestig0c412e852016-06-30 08:04:40428 bool GetIntByPath(GSettings* client, base::StringPiece key, int* result) {
eroman0070d412017-06-22 22:18:24429 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38430 *result = g_settings_get_int(client, key.data());
[email protected]8c20e3d2011-05-19 21:03:57431 return true;
432 }
thestig0c412e852016-06-30 08:04:40433 bool GetStringListByPath(GSettings* client,
434 base::StringPiece key,
[email protected]8c20e3d2011-05-19 21:03:57435 std::vector<std::string>* result) {
eroman0070d412017-06-22 22:18:24436 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38437 gchar** list = g_settings_get_strv(client, key.data());
[email protected]8c20e3d2011-05-19 21:03:57438 if (!list)
439 return false;
440 for (size_t i = 0; list[i]; ++i) {
441 result->push_back(static_cast<char*>(list[i]));
442 g_free(list[i]);
443 }
444 g_free(list);
445 return true;
446 }
447
448 // This is the callback from the debounce timer.
449 void OnDebouncedNotification() {
eroman0070d412017-06-22 22:18:24450 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57451 CHECK(notify_delegate_);
452 // Forward to a method on the proxy config service delegate object.
453 notify_delegate_->OnCheckProxyConfigSettings();
454 }
455
456 void OnChangeNotification() {
457 // We don't use Reset() because the timer may not yet be running.
458 // (In that case Stop() is a no-op.)
marshall8e5fe942015-03-06 19:22:40459 debounce_timer_->Stop();
460 debounce_timer_->Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57461 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
462 this, &SettingGetterImplGSettings::OnDebouncedNotification);
463 }
464
465 // gsettings notification callback, dispatched on the default glib main loop.
466 static void OnGSettingsChangeNotification(GSettings* client, gchar* key,
467 gpointer user_data) {
468 VLOG(1) << "gsettings change notification for key " << key;
469 // We don't track which key has changed, just that something did change.
470 SettingGetterImplGSettings* setting_getter =
471 reinterpret_cast<SettingGetterImplGSettings*>(user_data);
472 setting_getter->OnChangeNotification();
473 }
474
475 GSettings* client_;
476 GSettings* http_client_;
477 GSettings* https_client_;
478 GSettings* ftp_client_;
479 GSettings* socks_client_;
480 ProxyConfigServiceLinux::Delegate* notify_delegate_;
danakj8a98ca22016-04-16 02:47:36481 std::unique_ptr<base::OneShotTimer> debounce_timer_;
[email protected]8c20e3d2011-05-19 21:03:57482
[email protected]76722472012-05-24 08:26:46483 // Task runner for the thread that we make gsettings calls on. It should
[email protected]8c20e3d2011-05-19 21:03:57484 // be the UI thread and all our methods should be called on this
485 // thread. Only for assertions.
eroman0070d412017-06-22 22:18:24486 scoped_refptr<base::SequencedTaskRunner> task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57487};
488
Tim Brown1c307cc2017-12-08 02:40:38489bool SettingGetterImplGSettings::CheckVersion(
[email protected]8c20e3d2011-05-19 21:03:57490 base::Environment* env) {
Tim Brown1c307cc2017-12-08 02:40:38491 // CheckVersion() must be called *before* Init()!
[email protected]8c20e3d2011-05-19 21:03:57492 DCHECK(!client_);
493
thestig0c412e852016-06-30 08:04:40494 GSettings* client = nullptr;
Tim Brown1c307cc2017-12-08 02:40:38495 if (g_settings_schema_source_lookup(g_settings_schema_source_get_default(),
496 kProxyGSettingsSchema, FALSE)) {
497 client = g_settings_new(kProxyGSettingsSchema);
[email protected]4bbb72d2014-06-06 18:05:51498 }
499 if (!client) {
Tim Brown2a19f3b2017-12-12 01:08:40500 VLOG(1) << "Cannot create gsettings client.";
[email protected]8c20e3d2011-05-19 21:03:57501 return false;
502 }
503 g_object_unref(client);
504
[email protected]8c20e3d2011-05-19 21:03:57505 VLOG(1) << "All gsettings tests OK. Will get proxy config from gsettings.";
506 return true;
507}
508#endif // defined(USE_GIO)
509
eromane44498c2017-06-30 00:02:37510// Converts |value| from a decimal string to an int. If there was a failure
511// parsing, returns |default_value|.
512int StringToIntOrDefault(base::StringPiece value, int default_value) {
513 int result;
514 if (base::StringToInt(value, &result))
515 return result;
516 return default_value;
517}
518
Tim Brown2a19f3b2017-12-12 01:08:40519// This is the KDE version that reads kioslaverc and simulates gsettings.
[email protected]d7395e732009-08-28 23:13:43520// Doing this allows the main Delegate code, as well as the unit tests
521// for it, to stay the same - and the settings map fairly well besides.
gabf4f904e2017-05-10 20:55:02522class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter {
[email protected]d7395e732009-08-28 23:13:43523 public:
[email protected]573c0502011-05-17 22:19:50524 explicit SettingGetterImplKDE(base::Environment* env_var_getter)
marshall8e5fe942015-03-06 19:22:40525 : inotify_fd_(-1),
thestig0c412e852016-06-30 08:04:40526 notify_delegate_(nullptr),
danakj8c3eb802015-09-24 07:53:00527 debounce_timer_(new base::OneShotTimer()),
marshall8e5fe942015-03-06 19:22:40528 indirect_manual_(false),
529 auto_no_pac_(false),
530 reversed_bypass_list_(false),
531 env_var_getter_(env_var_getter),
thestig0c412e852016-06-30 08:04:40532 file_task_runner_(nullptr) {
[email protected]9a8c4022011-01-25 14:25:33533 // This has to be called on the UI thread (http://crbug.com/69057).
534 base::ThreadRestrictions::ScopedAllowIO allow_io;
535
[email protected]f18fde22010-05-18 23:49:54536 // Derive the location of the kde config dir from the environment.
[email protected]92d2dc82010-04-08 17:49:59537 std::string home;
[email protected]3ba7e082010-08-07 02:57:59538 if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) {
[email protected]2e8cfe22010-06-12 00:26:24539 // $KDEHOME is set. Use it unconditionally.
[email protected]6cdfd7f2013-02-08 20:40:15540 kde_config_dir_ = KDEHomeToConfigPath(base::FilePath(home));
[email protected]92d2dc82010-04-08 17:49:59541 } else {
[email protected]2e8cfe22010-06-12 00:26:24542 // $KDEHOME is unset. Try to figure out what to use. This seems to be
[email protected]92d2dc82010-04-08 17:49:59543 // the common case on most distributions.
[email protected]3ba7e082010-08-07 02:57:59544 if (!env_var_getter->GetVar(base::env_vars::kHome, &home))
[email protected]d7395e732009-08-28 23:13:43545 // User has no $HOME? Give up. Later we'll report the failure.
546 return;
[email protected]6b0349ef2010-10-16 04:56:06547 if (base::nix::GetDesktopEnvironment(env_var_getter) ==
548 base::nix::DESKTOP_ENVIRONMENT_KDE3) {
[email protected]92d2dc82010-04-08 17:49:59549 // KDE3 always uses .kde for its configuration.
[email protected]6cdfd7f2013-02-08 20:40:15550 base::FilePath kde_path = base::FilePath(home).Append(".kde");
[email protected]92d2dc82010-04-08 17:49:59551 kde_config_dir_ = KDEHomeToConfigPath(kde_path);
edward.baker53bec302015-10-02 16:57:49552 } else if (base::nix::GetDesktopEnvironment(env_var_getter) ==
553 base::nix::DESKTOP_ENVIRONMENT_KDE4) {
[email protected]92d2dc82010-04-08 17:49:59554 // Some distributions patch KDE4 to use .kde4 instead of .kde, so that
[email protected]fad9c8a52010-06-10 22:30:53555 // both can be installed side-by-side. Sadly they don't all do this, and
556 // they don't always do this: some distributions have started switching
557 // back as well. So if there is a .kde4 directory, check the timestamps
558 // of the config directories within and use the newest one.
[email protected]92d2dc82010-04-08 17:49:59559 // Note that we should currently be running in the UI thread, because in
Tim Brown2a19f3b2017-12-12 01:08:40560 // the gsettings version, that is the only thread that can access the
561 // proxy settings (a gsettings restriction). As noted below, the initial
562 // read of the proxy settings will be done in this thread anyway, so we
563 // check for .kde4 here in this thread as well.
[email protected]6cdfd7f2013-02-08 20:40:15564 base::FilePath kde3_path = base::FilePath(home).Append(".kde");
565 base::FilePath kde3_config = KDEHomeToConfigPath(kde3_path);
566 base::FilePath kde4_path = base::FilePath(home).Append(".kde4");
567 base::FilePath kde4_config = KDEHomeToConfigPath(kde4_path);
[email protected]fad9c8a52010-06-10 22:30:53568 bool use_kde4 = false;
[email protected]dcd16612013-07-15 20:18:09569 if (base::DirectoryExists(kde4_path)) {
[email protected]54124ed02014-01-07 10:06:58570 base::File::Info kde3_info;
571 base::File::Info kde4_info;
[email protected]9eae4e62013-12-04 20:56:49572 if (base::GetFileInfo(kde4_config, &kde4_info)) {
573 if (base::GetFileInfo(kde3_config, &kde3_info)) {
[email protected]fad9c8a52010-06-10 22:30:53574 use_kde4 = kde4_info.last_modified >= kde3_info.last_modified;
575 } else {
576 use_kde4 = true;
577 }
578 }
579 }
580 if (use_kde4) {
[email protected]92d2dc82010-04-08 17:49:59581 kde_config_dir_ = KDEHomeToConfigPath(kde4_path);
582 } else {
[email protected]fad9c8a52010-06-10 22:30:53583 kde_config_dir_ = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59584 }
edward.baker53bec302015-10-02 16:57:49585 } else {
586 // KDE 5 migrated to ~/.config for storing kioslaverc.
587 kde_config_dir_ = base::FilePath(home).Append(".config");
[email protected]92d2dc82010-04-08 17:49:59588 }
[email protected]d7395e732009-08-28 23:13:43589 }
[email protected]d7395e732009-08-28 23:13:43590 }
591
Peter Boström293b1342021-09-22 17:31:43592 SettingGetterImplKDE(const SettingGetterImplKDE&) = delete;
593 SettingGetterImplKDE& operator=(const SettingGetterImplKDE&) = delete;
594
dcheng67be2b1f2014-10-27 21:47:29595 ~SettingGetterImplKDE() override {
[email protected]d7395e732009-08-28 23:13:43596 // inotify_fd_ should have been closed before now, from
597 // Delegate::OnDestroy(), while running on the file thread. However
598 // on exiting the process, it may happen that Delegate::OnDestroy()
599 // task is left pending on the file loop after the loop was quit,
600 // and pending tasks may then be deleted without being run.
601 // Here in the KDE version, we can safely close the file descriptor
602 // anyway. (Not that it really matters; the process is exiting.)
603 if (inotify_fd_ >= 0)
[email protected]d3066142011-05-10 02:36:20604 ShutDown();
thestig0c412e852016-06-30 08:04:40605 DCHECK_LT(inotify_fd_, 0);
[email protected]d7395e732009-08-28 23:13:43606 }
607
eroman0070d412017-06-22 22:18:24608 bool Init(const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner)
mostynbba063d6032014-10-09 11:01:13609 override {
[email protected]9a8c4022011-01-25 14:25:33610 // This has to be called on the UI thread (http://crbug.com/69057).
611 base::ThreadRestrictions::ScopedAllowIO allow_io;
thestig0c412e852016-06-30 08:04:40612 DCHECK_LT(inotify_fd_, 0);
[email protected]d7395e732009-08-28 23:13:43613 inotify_fd_ = inotify_init();
614 if (inotify_fd_ < 0) {
[email protected]57b765672009-10-13 18:27:40615 PLOG(ERROR) << "inotify_init failed";
[email protected]d7395e732009-08-28 23:13:43616 return false;
617 }
tfarina89b4ae1c2015-12-16 18:59:18618 if (!base::SetNonBlocking(inotify_fd_)) {
619 PLOG(ERROR) << "base::SetNonBlocking failed";
[email protected]d7395e732009-08-28 23:13:43620 close(inotify_fd_);
621 inotify_fd_ = -1;
622 return false;
623 }
eroman0070d412017-06-22 22:18:24624
Gabriel Charette4049d422020-02-29 00:43:27625 constexpr base::TaskTraits kTraits = {base::TaskPriority::USER_VISIBLE,
626 base::MayBlock()};
627 file_task_runner_ = base::ThreadPool::CreateSequencedTaskRunner(kTraits);
eroman0070d412017-06-22 22:18:24628
sergeyu3f923062014-09-05 01:39:40629 // The initial read is done on the current thread, not
630 // |file_task_runner_|, since we will need to have it for
631 // SetUpAndFetchInitialConfig().
[email protected]d7395e732009-08-28 23:13:43632 UpdateCachedSettings();
633 return true;
634 }
635
dcheng67be2b1f2014-10-27 21:47:29636 void ShutDown() override {
[email protected]d7395e732009-08-28 23:13:43637 if (inotify_fd_ >= 0) {
638 ResetCachedSettings();
gabf4f904e2017-05-10 20:55:02639 inotify_watcher_.reset();
[email protected]d7395e732009-08-28 23:13:43640 close(inotify_fd_);
641 inotify_fd_ = -1;
642 }
marshall8e5fe942015-03-06 19:22:40643 debounce_timer_.reset();
[email protected]d7395e732009-08-28 23:13:43644 }
645
dcheng67be2b1f2014-10-27 21:47:29646 bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13647 ProxyConfigServiceLinux::Delegate* delegate) override {
thestig0c412e852016-06-30 08:04:40648 DCHECK_GE(inotify_fd_, 0);
eroman0070d412017-06-22 22:18:24649 DCHECK(file_task_runner_->RunsTasksInCurrentSequence());
[email protected]d7395e732009-08-28 23:13:43650 // We can't just watch the kioslaverc file directly, since KDE will write
651 // a new copy of it and then rename it whenever settings are changed and
652 // inotify watches inodes (so we'll be watching the old deleted file after
653 // the first change, and it will never change again). So, we watch the
654 // directory instead. We then act only on changes to the kioslaverc entry.
eroman6b0ca662017-06-22 00:16:36655 // TODO(eroman): What if the file is deleted? (handle with IN_DELETE).
[email protected]d7395e732009-08-28 23:13:43656 if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(),
sergeyu3f923062014-09-05 01:39:40657 IN_MODIFY | IN_MOVED_TO) < 0) {
[email protected]d7395e732009-08-28 23:13:43658 return false;
sergeyu3f923062014-09-05 01:39:40659 }
[email protected]d7395e732009-08-28 23:13:43660 notify_delegate_ = delegate;
gabf4f904e2017-05-10 20:55:02661 inotify_watcher_ = base::FileDescriptorWatcher::WatchReadable(
Anna Malovaae7007aa2020-03-09 16:48:48662 inotify_fd_,
663 base::BindRepeating(&SettingGetterImplKDE::OnChangeNotification,
664 base::Unretained(this)));
[email protected]d3066142011-05-10 02:36:20665 // Simulate a change to avoid possibly losing updates before this point.
666 OnChangeNotification();
667 return true;
[email protected]d7395e732009-08-28 23:13:43668 }
669
eroman0070d412017-06-22 22:18:24670 const scoped_refptr<base::SequencedTaskRunner>& GetNotificationTaskRunner()
dcheng67be2b1f2014-10-27 21:47:29671 override {
sergeyu3f923062014-09-05 01:39:40672 return file_task_runner_;
[email protected]d7395e732009-08-28 23:13:43673 }
674
dcheng67be2b1f2014-10-27 21:47:29675 bool GetString(StringSetting key, std::string* result) override {
jdoerrie22a91d8b92018-10-05 08:43:26676 auto it = string_table_.find(key);
[email protected]d7395e732009-08-28 23:13:43677 if (it == string_table_.end())
678 return false;
679 *result = it->second;
680 return true;
681 }
dcheng67be2b1f2014-10-27 21:47:29682 bool GetBool(BoolSetting key, bool* result) override {
[email protected]d7395e732009-08-28 23:13:43683 // We don't ever have any booleans.
684 return false;
685 }
dcheng67be2b1f2014-10-27 21:47:29686 bool GetInt(IntSetting key, int* result) override {
[email protected]d7395e732009-08-28 23:13:43687 // We don't ever have any integers. (See AddProxy() below about ports.)
688 return false;
689 }
dcheng67be2b1f2014-10-27 21:47:29690 bool GetStringList(StringListSetting key,
691 std::vector<std::string>* result) override {
jdoerrie22a91d8b92018-10-05 08:43:26692 auto it = strings_table_.find(key);
[email protected]d7395e732009-08-28 23:13:43693 if (it == strings_table_.end())
694 return false;
695 *result = it->second;
696 return true;
697 }
698
dcheng67be2b1f2014-10-27 21:47:29699 bool BypassListIsReversed() override { return reversed_bypass_list_; }
[email protected]a48bf4a2010-06-14 18:24:53700
Shimi Zhang13eace252020-01-31 01:49:19701 bool UseSuffixMatching() override { return true; }
[email protected]1a597192010-07-09 16:58:38702
[email protected]d7395e732009-08-28 23:13:43703 private:
704 void ResetCachedSettings() {
705 string_table_.clear();
706 strings_table_.clear();
707 indirect_manual_ = false;
708 auto_no_pac_ = false;
[email protected]a48bf4a2010-06-14 18:24:53709 reversed_bypass_list_ = false;
[email protected]d7395e732009-08-28 23:13:43710 }
711
[email protected]6cdfd7f2013-02-08 20:40:15712 base::FilePath KDEHomeToConfigPath(const base::FilePath& kde_home) {
[email protected]92d2dc82010-04-08 17:49:59713 return kde_home.Append("share").Append("config");
714 }
715
[email protected]6b5fe742011-05-20 21:46:48716 void AddProxy(StringSetting host_key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:43717 if (value.empty() || value.substr(0, 3) == "//:")
718 // No proxy.
719 return;
[email protected]4b90c202012-04-24 23:27:55720 size_t space = value.find(' ');
721 if (space != std::string::npos) {
722 // Newer versions of KDE use a space rather than a colon to separate the
723 // port number from the hostname. If we find this, we need to convert it.
724 std::string fixed = value;
725 fixed[space] = ':';
726 string_table_[host_key] = fixed;
727 } else {
728 // We don't need to parse the port number out; GetProxyFromSettings()
729 // would only append it right back again. So we just leave the port
730 // number right in the host string.
731 string_table_[host_key] = value;
732 }
[email protected]d7395e732009-08-28 23:13:43733 }
734
[email protected]6b5fe742011-05-20 21:46:48735 void AddHostList(StringListSetting key, const std::string& value) {
[email protected]f18fde22010-05-18 23:49:54736 std::vector<std::string> tokens;
[email protected]f4ebe772013-02-02 00:21:39737 base::StringTokenizer tk(value, ", ");
[email protected]f18fde22010-05-18 23:49:54738 while (tk.GetNext()) {
739 std::string token = tk.token();
740 if (!token.empty())
741 tokens.push_back(token);
742 }
743 strings_table_[key] = tokens;
744 }
745
[email protected]9a3d8d42009-09-03 17:01:46746 void AddKDESetting(const std::string& key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:43747 if (key == "ProxyType") {
748 const char* mode = "none";
749 indirect_manual_ = false;
750 auto_no_pac_ = false;
eromane44498c2017-06-30 00:02:37751 int int_value = StringToIntOrDefault(value, 0);
[email protected]e83326f2010-07-31 17:29:25752 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:43753 case 1: // Manual configuration.
754 mode = "manual";
755 break;
756 case 2: // PAC URL.
757 mode = "auto";
758 break;
759 case 3: // WPAD.
760 mode = "auto";
761 auto_no_pac_ = true;
762 break;
763 case 4: // Indirect manual via environment variables.
764 mode = "manual";
765 indirect_manual_ = true;
766 break;
eromane44498c2017-06-30 00:02:37767 default: // No proxy, or maybe kioslaverc syntax error.
768 break;
[email protected]d7395e732009-08-28 23:13:43769 }
[email protected]573c0502011-05-17 22:19:50770 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:43771 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:50772 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:43773 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:50774 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:43775 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:50776 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:43777 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:50778 AddProxy(PROXY_FTP_HOST, value);
[email protected]bfeb7232012-06-08 00:58:37779 } else if (key == "socksProxy") {
780 // Older versions of KDE configure SOCKS in a weird way involving
781 // LD_PRELOAD and a library that intercepts network calls to SOCKSify
782 // them. We don't support it. KDE 4.8 added a proper SOCKS setting.
783 AddProxy(PROXY_SOCKS_HOST, value);
[email protected]d7395e732009-08-28 23:13:43784 } else if (key == "ReversedException") {
785 // We count "true" or any nonzero number as true, otherwise false.
eromane44498c2017-06-30 00:02:37786 // A failure parsing the integer will also mean false.
787 reversed_bypass_list_ =
788 (value == "true" || StringToIntOrDefault(value, 0) != 0);
[email protected]d7395e732009-08-28 23:13:43789 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:50790 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:43791 } else if (key == "AuthMode") {
792 // Check for authentication, just so we can warn.
eromane44498c2017-06-30 00:02:37793 int mode = StringToIntOrDefault(value, 0);
[email protected]d7395e732009-08-28 23:13:43794 if (mode) {
795 // ProxyConfig does not support authentication parameters, but
796 // Chrome will prompt for the password later. So we ignore this.
797 LOG(WARNING) <<
798 "Proxy authentication parameters ignored, see bug 16709";
799 }
800 }
801 }
802
[email protected]6b5fe742011-05-20 21:46:48803 void ResolveIndirect(StringSetting key) {
jdoerrie22a91d8b92018-10-05 08:43:26804 auto it = string_table_.find(key);
[email protected]d7395e732009-08-28 23:13:43805 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:54806 std::string value;
[email protected]3ba7e082010-08-07 02:57:59807 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:43808 it->second = value;
[email protected]8425adc02010-04-18 17:45:31809 else
810 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:43811 }
812 }
813
[email protected]6b5fe742011-05-20 21:46:48814 void ResolveIndirectList(StringListSetting key) {
jdoerrie22a91d8b92018-10-05 08:43:26815 auto it = strings_table_.find(key);
[email protected]f18fde22010-05-18 23:49:54816 if (it != strings_table_.end()) {
817 std::string value;
818 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:59819 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:54820 AddHostList(key, value);
821 else
822 strings_table_.erase(it);
823 }
824 }
825
[email protected]d7395e732009-08-28 23:13:43826 // The settings in kioslaverc could occur in any order, but some affect
827 // others. Rather than read the whole file in and then query them in an
828 // order that allows us to handle that, we read the settings in whatever
829 // order they occur and do any necessary tweaking after we finish.
830 void ResolveModeEffects() {
831 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:50832 ResolveIndirect(PROXY_HTTP_HOST);
833 ResolveIndirect(PROXY_HTTPS_HOST);
834 ResolveIndirect(PROXY_FTP_HOST);
Maks Orlovichfee43b12021-06-17 21:10:08835 ResolveIndirect(PROXY_SOCKS_HOST);
[email protected]573c0502011-05-17 22:19:50836 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:43837 }
838 if (auto_no_pac_) {
839 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:50840 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:43841 }
[email protected]d7395e732009-08-28 23:13:43842 }
843
844 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
845 // each relevant name-value pair to the appropriate value table.
846 void UpdateCachedSettings() {
[email protected]6cdfd7f2013-02-08 20:40:15847 base::FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]b9b4a572014-03-17 23:11:12848 base::ScopedFILE input(base::OpenFile(kioslaverc, "r"));
[email protected]d7395e732009-08-28 23:13:43849 if (!input.get())
850 return;
851 ResetCachedSettings();
852 bool in_proxy_settings = false;
853 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:46854 char line[BUFFER_SIZE];
855 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:43856 while (fgets(line, sizeof(line), input.get())) {
857 // fgets() guarantees the line will be properly terminated.
858 size_t length = strlen(line);
859 if (!length)
860 continue;
861 // This should be true even with CRLF endings.
862 if (line[length - 1] != '\n') {
863 line_too_long = true;
864 continue;
865 }
866 if (line_too_long) {
867 // The previous line had no line ending, but this done does. This is
868 // the end of the line that was too long, so warn here and skip it.
869 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
870 line_too_long = false;
871 continue;
872 }
873 // Remove the LF at the end, and the CR if there is one.
874 line[--length] = '\0';
875 if (length && line[length - 1] == '\r')
876 line[--length] = '\0';
877 // Now parse the line.
878 if (line[0] == '[') {
879 // Switching sections. All we care about is whether this is
880 // the (a?) proxy settings section, for both KDE3 and KDE4.
881 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
882 } else if (in_proxy_settings) {
883 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:46884 char* split = strchr(line, '=');
885 // Skip this line if it does not contain an = sign.
886 if (!split)
[email protected]d7395e732009-08-28 23:13:43887 continue;
[email protected]9a3d8d42009-09-03 17:01:46888 // Split the line on the = and advance |split|.
889 *(split++) = 0;
890 std::string key = line;
891 std::string value = split;
[email protected]8af69c6c2014-03-03 19:05:31892 base::TrimWhitespaceASCII(key, base::TRIM_ALL, &key);
893 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value);
[email protected]9a3d8d42009-09-03 17:01:46894 // Skip this line if the key name is empty.
895 if (key.empty())
[email protected]d7395e732009-08-28 23:13:43896 continue;
897 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:46898 if (key[key.length() - 1] == ']') {
899 // Find the matching bracket.
900 length = key.rfind('[');
901 // Skip this line if the localization indicator is malformed.
902 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:43903 continue;
904 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:46905 key.resize(length);
906 // Remove any resulting trailing whitespace.
[email protected]8af69c6c2014-03-03 19:05:31907 base::TrimWhitespaceASCII(key, base::TRIM_TRAILING, &key);
[email protected]9a3d8d42009-09-03 17:01:46908 // Skip this line if the key name is now empty.
909 if (key.empty())
910 continue;
[email protected]d7395e732009-08-28 23:13:43911 }
[email protected]d7395e732009-08-28 23:13:43912 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:46913 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:43914 }
915 }
916 if (ferror(input.get()))
917 LOG(ERROR) << "error reading " << kioslaverc.value();
918 ResolveModeEffects();
919 }
920
921 // This is the callback from the debounce timer.
922 void OnDebouncedNotification() {
eroman0070d412017-06-22 22:18:24923 DCHECK(file_task_runner_->RunsTasksInCurrentSequence());
[email protected]b30a3f52010-10-16 01:05:46924 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:43925 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:14926 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:43927 // Forward to a method on the proxy config service delegate object.
928 notify_delegate_->OnCheckProxyConfigSettings();
929 }
930
931 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
932 // from the inotify file descriptor and starts up a debounce timer if
933 // an event for kioslaverc is seen.
934 void OnChangeNotification() {
[email protected]d2e6d592012-02-03 21:49:04935 DCHECK_GE(inotify_fd_, 0);
eroman0070d412017-06-22 22:18:24936 DCHECK(file_task_runner_->RunsTasksInCurrentSequence());
[email protected]d7395e732009-08-28 23:13:43937 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
938 bool kioslaverc_touched = false;
939 ssize_t r;
940 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
941 // inotify returns variable-length structures, which is why we have
942 // this strange-looking loop instead of iterating through an array.
943 char* event_ptr = event_buf;
944 while (event_ptr < event_buf + r) {
945 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
946 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:33947 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
948 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:43949 if (!strcmp(event->name, "kioslaverc"))
950 kioslaverc_touched = true;
951 // Advance the pointer just past the end of the filename.
952 event_ptr = event->name + event->len;
953 }
954 // We keep reading even if |kioslaverc_touched| is true to drain the
955 // inotify event queue.
956 }
957 if (!r)
958 // Instead of returning -1 and setting errno to EINVAL if there is not
959 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
960 // new behavior (EINVAL) so we can reuse the code below.
961 errno = EINVAL;
962 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:40963 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:43964 if (errno == EINVAL) {
965 // Our buffer is not large enough to read the next event. This should
966 // not happen (because its size is calculated to always be sufficiently
967 // large), but if it does we'd warn continuously since |inotify_fd_|
968 // would be forever ready to read. Close it and stop watching instead.
969 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
gabf4f904e2017-05-10 20:55:02970 inotify_watcher_.reset();
[email protected]d7395e732009-08-28 23:13:43971 close(inotify_fd_);
972 inotify_fd_ = -1;
973 }
974 }
975 if (kioslaverc_touched) {
eroman6b0ca662017-06-22 00:16:36976 LOG(ERROR) << "kioslaverc_touched";
[email protected]d7395e732009-08-28 23:13:43977 // We don't use Reset() because the timer may not yet be running.
978 // (In that case Stop() is a no-op.)
marshall8e5fe942015-03-06 19:22:40979 debounce_timer_->Stop();
980 debounce_timer_->Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
[email protected]d7395e732009-08-28 23:13:43981 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:50982 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:43983 }
984 }
985
[email protected]6b5fe742011-05-20 21:46:48986 typedef std::map<StringSetting, std::string> string_map_type;
987 typedef std::map<StringListSetting,
988 std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:43989
990 int inotify_fd_;
gabf4f904e2017-05-10 20:55:02991 std::unique_ptr<base::FileDescriptorWatcher::Controller> inotify_watcher_;
[email protected]d7395e732009-08-28 23:13:43992 ProxyConfigServiceLinux::Delegate* notify_delegate_;
danakj8a98ca22016-04-16 02:47:36993 std::unique_ptr<base::OneShotTimer> debounce_timer_;
[email protected]6cdfd7f2013-02-08 20:40:15994 base::FilePath kde_config_dir_;
[email protected]d7395e732009-08-28 23:13:43995 bool indirect_manual_;
996 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:53997 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:54998 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
999 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
1000 // same lifetime.
[email protected]76b90d312010-08-03 03:00:501001 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:431002
1003 // We cache these settings whenever we re-read the kioslaverc file.
1004 string_map_type string_table_;
1005 strings_map_type strings_table_;
1006
eroman0070d412017-06-22 22:18:241007 // Task runner for doing blocking file IO on, as well as handling inotify
1008 // events on.
1009 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
[email protected]861c6c62009-04-20 16:50:561010};
1011
1012} // namespace
1013
[email protected]573c0502011-05-17 22:19:501014bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
[email protected]6b5fe742011-05-20 21:46:481015 SettingGetter::StringSetting host_key,
[email protected]573c0502011-05-17 22:19:501016 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561017 std::string host;
[email protected]573c0502011-05-17 22:19:501018 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561019 // Unset or empty.
1020 return false;
1021 }
1022 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431023 int port = 0;
[email protected]6b5fe742011-05-20 21:46:481024 SettingGetter::IntSetting port_key =
[email protected]573c0502011-05-17 22:19:501025 SettingGetter::HostSettingToPortSetting(host_key);
1026 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561027 if (port != 0) {
1028 // If a port is set and non-zero:
Raul Tambre8c1981d2019-02-08 02:22:261029 host += ":" + base::NumberToString(port);
[email protected]861c6c62009-04-20 16:50:561030 }
[email protected]76960f3d2011-04-30 02:15:231031
Tim Brown2a19f3b2017-12-12 01:08:401032 // gsettings settings do not appear to distinguish between SOCKS version. We
[email protected]573c0502011-05-17 22:19:501033 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231034 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501035 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1036 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1037 host = FixupProxyHostScheme(scheme, host);
Eric Orth5ccc3f02021-09-23 00:01:571038 ProxyServer proxy_server =
1039 ProxyUriToProxyServer(host, ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561040 if (proxy_server.is_valid()) {
1041 *result_server = proxy_server;
1042 return true;
1043 }
1044 return false;
1045}
1046
Anton Bikineev068d2912021-05-15 20:43:521047absl::optional<ProxyConfigWithAnnotation>
Eric Roman750af4b12018-02-22 22:38:531048ProxyConfigServiceLinux::Delegate::GetConfigFromSettings() {
Ramin Halavatica8d5252018-03-12 05:33:491049 ProxyConfig config;
Eric Roman750af4b12018-02-22 22:38:531050
[email protected]861c6c62009-04-20 16:50:561051 std::string mode;
[email protected]573c0502011-05-17 22:19:501052 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
Tim Brown2a19f3b2017-12-12 01:08:401053 // We expect this to always be set, so if we don't see it then we probably
1054 // have a gsettings problem, and so we don't have a valid proxy config.
Anton Bikineev068d2912021-05-15 20:43:521055 return absl::nullopt;
[email protected]861c6c62009-04-20 16:50:561056 }
[email protected]3e44697f2009-05-22 14:37:391057 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561058 // Specifically specifies no proxy.
Ramin Halavatica8d5252018-03-12 05:33:491059 return ProxyConfigWithAnnotation(
1060 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]3e44697f2009-05-22 14:37:391061 }
[email protected]861c6c62009-04-20 16:50:561062
[email protected]3e44697f2009-05-22 14:37:391063 if (mode == "auto") {
[email protected]aa3ac2cc2012-06-19 00:28:041064 // Automatic proxy config.
[email protected]861c6c62009-04-20 16:50:561065 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501066 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1067 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561068 if (!pac_url_str.empty()) {
[email protected]aa3ac2cc2012-06-19 00:28:041069 // If the PAC URL is actually a file path, then put file:// in front.
1070 if (pac_url_str[0] == '/')
1071 pac_url_str = "file://" + pac_url_str;
[email protected]861c6c62009-04-20 16:50:561072 GURL pac_url(pac_url_str);
1073 if (!pac_url.is_valid())
Anton Bikineev068d2912021-05-15 20:43:521074 return absl::nullopt;
Ramin Halavatica8d5252018-03-12 05:33:491075 config.set_pac_url(pac_url);
1076 return ProxyConfigWithAnnotation(
1077 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561078 }
1079 }
Ramin Halavatica8d5252018-03-12 05:33:491080 config.set_auto_detect(true);
1081 return ProxyConfigWithAnnotation(
1082 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561083 }
1084
[email protected]3e44697f2009-05-22 14:37:391085 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561086 // Mode is unrecognized.
Anton Bikineev068d2912021-05-15 20:43:521087 return absl::nullopt;
[email protected]861c6c62009-04-20 16:50:561088 }
1089 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501090 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1091 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561092 && !use_http_proxy) {
1093 // Another master switch for some reason. If set to false, then no
1094 // proxy. But we don't panic if the key doesn't exist.
Ramin Halavatica8d5252018-03-12 05:33:491095 return ProxyConfigWithAnnotation(
1096 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561097 }
1098
1099 bool same_proxy = false;
1100 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501101 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561102 // case.
[email protected]573c0502011-05-17 22:19:501103 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1104 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561105
[email protected]76960f3d2011-04-30 02:15:231106 ProxyServer proxy_for_http;
1107 ProxyServer proxy_for_https;
1108 ProxyServer proxy_for_ftp;
1109 ProxyServer socks_proxy; // (socks)
1110
1111 // This counts how many of the above ProxyServers were defined and valid.
1112 size_t num_proxies_specified = 0;
1113
1114 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1115 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501116 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231117 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501118 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231119 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501120 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231121 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501122 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231123 num_proxies_specified++;
1124
1125 if (same_proxy) {
1126 if (proxy_for_http.is_valid()) {
1127 // Use the http proxy for all schemes.
Ramin Halavatica8d5252018-03-12 05:33:491128 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
1129 config.proxy_rules().single_proxies.SetSingleProxyServer(proxy_for_http);
[email protected]861c6c62009-04-20 16:50:561130 }
[email protected]76960f3d2011-04-30 02:15:231131 } else if (num_proxies_specified > 0) {
1132 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1133 // If the only proxy specified was for SOCKS, use it for all schemes.
Ramin Halavatica8d5252018-03-12 05:33:491134 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
1135 config.proxy_rules().single_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561136 } else {
[email protected]2189e092013-03-16 18:02:021137 // Otherwise use the indicated proxies per-scheme.
Ramin Halavatica8d5252018-03-12 05:33:491138 config.proxy_rules().type =
Lily Houghtone6b617e2018-01-19 20:13:071139 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
Ramin Halavatica8d5252018-03-12 05:33:491140 config.proxy_rules().proxies_for_http.SetSingleProxyServer(
1141 proxy_for_http);
1142 config.proxy_rules().proxies_for_https.SetSingleProxyServer(
1143 proxy_for_https);
1144 config.proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_for_ftp);
1145 config.proxy_rules().fallback_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561146 }
1147 }
1148
Ramin Halavatica8d5252018-03-12 05:33:491149 if (config.proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561150 // Manual mode but we couldn't parse any rules.
Anton Bikineev068d2912021-05-15 20:43:521151 return absl::nullopt;
[email protected]861c6c62009-04-20 16:50:561152 }
1153
1154 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431155 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501156 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1157 &use_auth);
[email protected]62749f182009-07-15 13:16:541158 if (use_auth) {
1159 // ProxyConfig does not support authentication parameters, but
1160 // Chrome will prompt for the password later. So we ignore
1161 // /system/http_proxy/*auth* settings.
1162 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1163 }
[email protected]861c6c62009-04-20 16:50:561164
1165 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061166 std::vector<std::string> ignore_hosts_list;
Ramin Halavatica8d5252018-03-12 05:33:491167 config.proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501168 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1169 &ignore_hosts_list)) {
Eric Romanda790f92018-11-07 19:17:151170 for (const auto& rule : ignore_hosts_list) {
Shimi Zhang13eace252020-01-31 01:49:191171 config.proxy_rules().bypass_rules.AddRuleFromString(rule);
[email protected]1a597192010-07-09 16:58:381172 }
[email protected]a8185d02010-06-11 00:19:501173 }
Shimi Zhang13eace252020-01-31 01:49:191174
1175 if (setting_getter_->UseSuffixMatching()) {
1176 RewriteRulesForSuffixMatching(&config.proxy_rules().bypass_rules);
1177 }
1178
[email protected]861c6c62009-04-20 16:50:561179 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381180 // bypass of local names in GNOME. In KDE, "<local>" is supported
1181 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561182
[email protected]a48bf4a2010-06-14 18:24:531183 // KDE allows one to reverse the bypass rules.
Ramin Halavatica8d5252018-03-12 05:33:491184 config.proxy_rules().reverse_bypass = setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531185
Ramin Halavatica8d5252018-03-12 05:33:491186 return ProxyConfigWithAnnotation(
1187 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561188}
1189
thestig0c412e852016-06-30 08:04:401190ProxyConfigServiceLinux::Delegate::Delegate(
Ramin Halavatica8d5252018-03-12 05:33:491191 std::unique_ptr<base::Environment> env_var_getter,
Anton Bikineev068d2912021-05-15 20:43:521192 absl::optional<std::unique_ptr<SettingGetter>> setting_getter,
1193 absl::optional<NetworkTrafficAnnotationTag> traffic_annotation)
Eric Romancd032fb62018-05-18 21:40:131194 : env_var_getter_(std::move(env_var_getter)) {
1195 if (traffic_annotation) {
1196 traffic_annotation_ =
1197 MutableNetworkTrafficAnnotationTag(traffic_annotation.value());
1198 }
1199
1200 if (setting_getter) {
1201 setting_getter_ = std::move(setting_getter.value());
1202 return;
1203 }
1204
[email protected]573c0502011-05-17 22:19:501205 // Figure out which SettingGetterImpl to use, if any.
thestig0c412e852016-06-30 08:04:401206 switch (base::nix::GetDesktopEnvironment(env_var_getter_.get())) {
Tim Brownd9bd4752017-12-14 20:26:341207 case base::nix::DESKTOP_ENVIRONMENT_CINNAMON:
[email protected]6b0349ef2010-10-16 04:56:061208 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
Tom Andersonac4d6f42017-10-13 20:14:201209 case base::nix::DESKTOP_ENVIRONMENT_PANTHEON:
[email protected]9e6c9bde2012-07-17 23:40:171210 case base::nix::DESKTOP_ENVIRONMENT_UNITY:
[email protected]8c20e3d2011-05-19 21:03:571211#if defined(USE_GIO)
1212 {
danakj8a98ca22016-04-16 02:47:361213 std::unique_ptr<SettingGetterImplGSettings> gs_getter(
1214 new SettingGetterImplGSettings());
1215 // We have to load symbols and check the GNOME version in use to decide
Tim Brown1c307cc2017-12-08 02:40:381216 // if we should use the gsettings getter. See CheckVersion().
1217 if (gs_getter->CheckVersion(env_var_getter_.get()))
inlinechan894515af2016-12-09 02:40:101218 setting_getter_ = std::move(gs_getter);
[email protected]8c20e3d2011-05-19 21:03:571219 }
1220#endif
[email protected]d7395e732009-08-28 23:13:431221 break;
[email protected]6b0349ef2010-10-16 04:56:061222 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1223 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
edward.baker53bec302015-10-02 16:57:491224 case base::nix::DESKTOP_ENVIRONMENT_KDE5:
Peter Boström08e7ed82021-04-19 17:49:591225 setting_getter_ =
1226 std::make_unique<SettingGetterImplKDE>(env_var_getter_.get());
[email protected]d7395e732009-08-28 23:13:431227 break;
[email protected]6b0349ef2010-10-16 04:56:061228 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1229 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431230 break;
1231 }
1232}
1233
[email protected]d3066142011-05-10 02:36:201234void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
sergeyu3f923062014-09-05 01:39:401235 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
Ramin Halavatica8d5252018-03-12 05:33:491236 const scoped_refptr<base::SequencedTaskRunner>& main_task_runner,
1237 const NetworkTrafficAnnotationTag& traffic_annotation) {
1238 traffic_annotation_ = MutableNetworkTrafficAnnotationTag(traffic_annotation);
1239
[email protected]3e44697f2009-05-22 14:37:391240 // We should be running on the default glib main loop thread right
Tim Brown2a19f3b2017-12-12 01:08:401241 // now. gsettings can only be accessed from this thread.
eroman0070d412017-06-22 22:18:241242 DCHECK(glib_task_runner->RunsTasksInCurrentSequence());
sergeyu3f923062014-09-05 01:39:401243 glib_task_runner_ = glib_task_runner;
Matt Menke75765062017-11-21 01:21:161244 main_task_runner_ = main_task_runner;
[email protected]3e44697f2009-05-22 14:37:391245
Matt Menke75765062017-11-21 01:21:161246 // If we are passed a NULL |main_task_runner|, then don't set up proxy
eroman0070d412017-06-22 22:18:241247 // setting change notifications. This should not be the usual case but is
1248 // intended to/ simplify test setups.
Matt Menke75765062017-11-21 01:21:161249 if (!main_task_runner_.get())
[email protected]b30a3f52010-10-16 01:05:461250 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391251
1252 // Fetch and cache the current proxy config. The config is left in
Matt Menke75765062017-11-21 01:21:161253 // cached_config_, where GetLatestProxyConfig() running on the main TaskRunner
[email protected]3e44697f2009-05-22 14:37:391254 // will expect to find it. This is safe to do because we return
1255 // before this ProxyConfigServiceLinux is passed on to
Nicolas Arciniegad2013f92020-02-07 23:00:561256 // the ConfiguredProxyResolutionService.
[email protected]d6cb85b2009-07-23 22:10:531257
1258 // Note: It would be nice to prioritize environment variables
Tim Brown2a19f3b2017-12-12 01:08:401259 // and only fall back to gsettings if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531260 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1261 // does so even if the proxy mode is set to auto, which would
1262 // mislead us.
1263
Anton Bikineev068d2912021-05-15 20:43:521264 cached_config_ = absl::nullopt;
Eric Roman750af4b12018-02-22 22:38:531265 if (setting_getter_ && setting_getter_->Init(glib_task_runner)) {
1266 cached_config_ = GetConfigFromSettings();
1267 }
1268 if (cached_config_) {
Ramin Halavatica8d5252018-03-12 05:33:491269 VLOG(1) << "Obtained proxy settings from annotation hash code "
1270 << cached_config_->traffic_annotation().unique_id_hash_code;
[email protected]d3066142011-05-10 02:36:201271
Tim Brown2a19f3b2017-12-12 01:08:401272 // If gsettings proxy mode is "none", meaning direct, then we take
[email protected]d3066142011-05-10 02:36:201273 // that to be a valid config and will not check environment
1274 // variables. The alternative would have been to look for a proxy
Eric Roman750af4b12018-02-22 22:38:531275 // wherever we can find one.
[email protected]d3066142011-05-10 02:36:201276
1277 // Keep a copy of the config for use from this thread for
1278 // comparison with updated settings when we get notifications.
1279 reference_config_ = cached_config_;
[email protected]d3066142011-05-10 02:36:201280
Matt Menke75765062017-11-21 01:21:161281 // We only set up notifications if we have the main and file loops
1282 // available. We do this after getting the initial configuration so that we
1283 // don't have to worry about cancelling it if the initial fetch above fails.
1284 // Note that setting up notifications has the side effect of simulating a
1285 // change, so that we won't lose any updates that may have happened after
1286 // the initial fetch and before setting up notifications. We'll detect the
1287 // common case of no changes in OnCheckProxyConfigSettings() (or sooner) and
1288 // ignore it.
1289 if (main_task_runner.get()) {
eroman0070d412017-06-22 22:18:241290 scoped_refptr<base::SequencedTaskRunner> required_loop =
[email protected]76722472012-05-24 08:26:461291 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241292 if (!required_loop.get() || required_loop->RunsTasksInCurrentSequence()) {
[email protected]d3066142011-05-10 02:36:201293 // In this case we are already on an acceptable thread.
1294 SetUpNotifications();
[email protected]d7395e732009-08-28 23:13:431295 } else {
[email protected]d3066142011-05-10 02:36:201296 // Post a task to set up notifications. We don't wait for success.
kylecharf4fe5172019-02-15 18:53:491297 required_loop->PostTask(
1298 FROM_HERE,
1299 base::BindOnce(
1300 &ProxyConfigServiceLinux::Delegate::SetUpNotifications, this));
[email protected]d6cb85b2009-07-23 22:10:531301 }
[email protected]d7395e732009-08-28 23:13:431302 }
[email protected]861c6c62009-04-20 16:50:561303 }
[email protected]d6cb85b2009-07-23 22:10:531304
Eric Roman750af4b12018-02-22 22:38:531305 if (!cached_config_) {
[email protected]d6cb85b2009-07-23 22:10:531306 // We fall back on environment variables.
[email protected]3e44697f2009-05-22 14:37:391307 //
[email protected]d3066142011-05-10 02:36:201308 // Consulting environment variables doesn't need to be done from the
1309 // default glib main loop, but it's a tiny enough amount of work.
Eric Roman750af4b12018-02-22 22:38:531310 cached_config_ = GetConfigFromEnv();
1311 if (cached_config_) {
[email protected]b30a3f52010-10-16 01:05:461312 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391313 }
[email protected]861c6c62009-04-20 16:50:561314 }
[email protected]3e44697f2009-05-22 14:37:391315}
1316
[email protected]573c0502011-05-17 22:19:501317// Depending on the SettingGetter in use, this method will be called
Tim Brown2a19f3b2017-12-12 01:08:401318// on either the UI thread (GSettings) or the file thread (KDE).
[email protected]d3066142011-05-10 02:36:201319void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
eroman0070d412017-06-22 22:18:241320 scoped_refptr<base::SequencedTaskRunner> required_loop =
[email protected]76722472012-05-24 08:26:461321 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241322 DCHECK(!required_loop.get() || required_loop->RunsTasksInCurrentSequence());
[email protected]573c0502011-05-17 22:19:501323 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201324 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1325}
1326
[email protected]119655002010-07-23 06:02:401327void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1328 observers_.AddObserver(observer);
1329}
1330
1331void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1332 observers_.RemoveObserver(observer);
1333}
1334
[email protected]3a29593d2011-04-11 10:07:521335ProxyConfigService::ConfigAvailability
Ramin Halavatica8d5252018-03-12 05:33:491336ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1337 ProxyConfigWithAnnotation* config) {
Matt Menke75765062017-11-21 01:21:161338 // This is called from the main TaskRunner.
1339 DCHECK(!main_task_runner_.get() ||
1340 main_task_runner_->RunsTasksInCurrentSequence());
[email protected]3e44697f2009-05-22 14:37:391341
1342 // Simply return the last proxy configuration that glib_default_loop
1343 // notified us of.
Eric Roman750af4b12018-02-22 22:38:531344 *config = GetConfigOrDirect(cached_config_);
[email protected]119655002010-07-23 06:02:401345
[email protected]3a29593d2011-04-11 10:07:521346 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401347 // going to be available since we initialized eagerly on the UI thread.
1348 // TODO(eroman): do lazy initialization instead, so we no longer need
1349 // to construct ProxyConfigServiceLinux on the UI thread.
1350 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521351 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391352}
1353
[email protected]573c0502011-05-17 22:19:501354// Depending on the SettingGetter in use, this method will be called
Tim Brown2a19f3b2017-12-12 01:08:401355// on either the UI thread (GSettings) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391356void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
eroman0070d412017-06-22 22:18:241357 scoped_refptr<base::SequencedTaskRunner> required_loop =
[email protected]76722472012-05-24 08:26:461358 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241359 DCHECK(!required_loop.get() || required_loop->RunsTasksInCurrentSequence());
Anton Bikineev068d2912021-05-15 20:43:521360 absl::optional<ProxyConfigWithAnnotation> new_config =
Ramin Halavatica8d5252018-03-12 05:33:491361 GetConfigFromSettings();
[email protected]3e44697f2009-05-22 14:37:391362
[email protected]119655002010-07-23 06:02:401363 // See if it is different from what we had before.
Eric Roman750af4b12018-02-22 22:38:531364 if (new_config.has_value() != reference_config_.has_value() ||
Eric Roman3e185842018-06-01 18:10:521365 (new_config.has_value() &&
1366 !new_config->value().Equals(reference_config_->value()))) {
Matt Menke75765062017-11-21 01:21:161367 // Post a task to the main TaskRunner with the new configuration, so it can
[email protected]3e44697f2009-05-22 14:37:391368 // update |cached_config_|.
Matt Menke75765062017-11-21 01:21:161369 main_task_runner_->PostTask(
1370 FROM_HERE,
kylecharf4fe5172019-02-15 18:53:491371 base::BindOnce(&ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1372 this, new_config));
[email protected]d1f9d472009-08-13 19:59:301373 // Update the thread-private copy in |reference_config_| as well.
1374 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201375 } else {
1376 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391377 }
1378}
1379
1380void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
Anton Bikineev068d2912021-05-15 20:43:521381 const absl::optional<ProxyConfigWithAnnotation>& new_config) {
Matt Menke75765062017-11-21 01:21:161382 DCHECK(main_task_runner_->RunsTasksInCurrentSequence());
[email protected]b30a3f52010-10-16 01:05:461383 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391384 cached_config_ = new_config;
Eric Roman750af4b12018-02-22 22:38:531385 for (auto& observer : observers_) {
1386 observer.OnProxyConfigChanged(GetConfigOrDirect(new_config),
1387 ProxyConfigService::CONFIG_VALID);
1388 }
[email protected]3e44697f2009-05-22 14:37:391389}
1390
1391void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
thestig0c412e852016-06-30 08:04:401392 if (!setting_getter_)
[email protected]d7395e732009-08-28 23:13:431393 return;
thestig0c412e852016-06-30 08:04:401394
eroman0070d412017-06-22 22:18:241395 scoped_refptr<base::SequencedTaskRunner> shutdown_loop =
[email protected]76722472012-05-24 08:26:461396 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241397 if (!shutdown_loop.get() || shutdown_loop->RunsTasksInCurrentSequence()) {
[email protected]3e44697f2009-05-22 14:37:391398 // Already on the right thread, call directly.
1399 // This is the case for the unittests.
1400 OnDestroy();
1401 } else {
[email protected]d7395e732009-08-28 23:13:431402 // Post to shutdown thread. Note that on browser shutdown, we may quit
1403 // this MessageLoop and exit the program before ever running this.
kylecharf4fe5172019-02-15 18:53:491404 shutdown_loop->PostTask(
1405 FROM_HERE,
1406 base::BindOnce(&ProxyConfigServiceLinux::Delegate::OnDestroy, this));
[email protected]3e44697f2009-05-22 14:37:391407 }
1408}
1409void ProxyConfigServiceLinux::Delegate::OnDestroy() {
eroman0070d412017-06-22 22:18:241410 scoped_refptr<base::SequencedTaskRunner> shutdown_loop =
[email protected]76722472012-05-24 08:26:461411 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241412 DCHECK(!shutdown_loop.get() || shutdown_loop->RunsTasksInCurrentSequence());
[email protected]573c0502011-05-17 22:19:501413 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391414}
1415
1416ProxyConfigServiceLinux::ProxyConfigServiceLinux()
Eric Romancd032fb62018-05-18 21:40:131417 : delegate_(new Delegate(base::Environment::Create(),
Anton Bikineev068d2912021-05-15 20:43:521418 absl::nullopt,
1419 absl::nullopt)) {}
[email protected]3e44697f2009-05-22 14:37:391420
[email protected]8e1845e12010-09-15 19:22:241421ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1422 delegate_->PostDestroyTask();
1423}
1424
[email protected]3e44697f2009-05-22 14:37:391425ProxyConfigServiceLinux::ProxyConfigServiceLinux(
Ramin Halavatica8d5252018-03-12 05:33:491426 std::unique_ptr<base::Environment> env_var_getter,
1427 const NetworkTrafficAnnotationTag& traffic_annotation)
Eric Romancd032fb62018-05-18 21:40:131428 : delegate_(new Delegate(std::move(env_var_getter),
Anton Bikineev068d2912021-05-15 20:43:521429 absl::nullopt,
Eric Romancd032fb62018-05-18 21:40:131430 traffic_annotation)) {}
[email protected]9a3d8d42009-09-03 17:01:461431
1432ProxyConfigServiceLinux::ProxyConfigServiceLinux(
thestig0c412e852016-06-30 08:04:401433 std::unique_ptr<base::Environment> env_var_getter,
Ramin Halavatica8d5252018-03-12 05:33:491434 SettingGetter* setting_getter,
1435 const NetworkTrafficAnnotationTag& traffic_annotation)
1436 : delegate_(new Delegate(std::move(env_var_getter),
Eric Romancd032fb62018-05-18 21:40:131437 base::WrapUnique(setting_getter),
Ramin Halavatica8d5252018-03-12 05:33:491438 traffic_annotation)) {}
[email protected]861c6c62009-04-20 16:50:561439
[email protected]e4be2dd2010-12-14 00:44:391440void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1441 delegate_->AddObserver(observer);
1442}
1443
1444void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1445 delegate_->RemoveObserver(observer);
1446}
1447
[email protected]3a29593d2011-04-11 10:07:521448ProxyConfigService::ConfigAvailability
Ramin Halavatica8d5252018-03-12 05:33:491449ProxyConfigServiceLinux::GetLatestProxyConfig(
1450 ProxyConfigWithAnnotation* config) {
[email protected]e4be2dd2010-12-14 00:44:391451 return delegate_->GetLatestProxyConfig(config);
1452}
1453
[email protected]861c6c62009-04-20 16:50:561454} // namespace net