blob: 7b74e4fc2309bd44ab1e33934b54bde84cfd5923 [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"
[email protected]861c6c62009-04-20 16:50:5636
[email protected]3fc24f52012-11-30 21:22:3437#if defined(USE_GIO)
Tim Brown1c307cc2017-12-08 02:40:3838#include <gio/gio.h>
[email protected]3fc24f52012-11-30 21:22:3439#endif // defined(USE_GIO)
40
[email protected]861c6c62009-04-20 16:50:5641namespace net {
42
43namespace {
44
Shimi Zhang13eace252020-01-31 01:49:1945// This turns all rules with a hostname into wildcard matches, which will
46// match not just the indicated hostname but also any hostname that ends with
47// it.
48void RewriteRulesForSuffixMatching(ProxyBypassRules* out) {
49 // Prepend a wildcard (*) to any hostname based rules, provided it isn't an IP
50 // address.
51 for (size_t i = 0; i < out->rules().size(); ++i) {
52 if (!out->rules()[i]->IsHostnamePatternRule())
53 continue;
54
55 const SchemeHostPortMatcherHostnamePatternRule* prev_rule =
56 static_cast<const SchemeHostPortMatcherHostnamePatternRule*>(
57 out->rules()[i].get());
58 out->ReplaceRule(i, prev_rule->GenerateSuffixMatchingRule());
59 }
60}
61
[email protected]861c6c62009-04-20 16:50:5662// Given a proxy hostname from a setting, returns that hostname with
63// an appropriate proxy server scheme prefix.
64// scheme indicates the desired proxy scheme: usually http, with
65// socks 4 or 5 as special cases.
[email protected]87a102b2009-07-14 05:23:3066// TODO(arindam): Remove URI string manipulation by using MapUrlSchemeToProxy.
[email protected]861c6c62009-04-20 16:50:5667std::string FixupProxyHostScheme(ProxyServer::Scheme scheme,
68 std::string host) {
[email protected]e8c50812010-09-28 00:16:1769 if (scheme == ProxyServer::SCHEME_SOCKS5 &&
brettw3a2c6902015-07-06 19:43:2970 base::StartsWith(host, "socks4://",
71 base::CompareCase::INSENSITIVE_ASCII)) {
[email protected]e8c50812010-09-28 00:16:1772 // We default to socks 5, but if the user specifically set it to
73 // socks4://, then use that.
74 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:5675 }
76 // Strip the scheme if any.
77 std::string::size_type colon = host.find("://");
78 if (colon != std::string::npos)
79 host = host.substr(colon + 3);
80 // If a username and perhaps password are specified, give a warning.
81 std::string::size_type at_sign = host.find("@");
82 // Should this be supported?
83 if (at_sign != std::string::npos) {
[email protected]62749f182009-07-15 13:16:5484 // ProxyConfig does not support authentication parameters, but Chrome
85 // will prompt for the password later. Disregard the
86 // authentication parameters and continue with this hostname.
87 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
[email protected]861c6c62009-04-20 16:50:5688 host = host.substr(at_sign + 1);
89 }
90 // If this is a socks proxy, prepend a scheme so as to tell
91 // ProxyServer. This also allows ProxyServer to choose the right
92 // default port.
93 if (scheme == ProxyServer::SCHEME_SOCKS4)
94 host = "socks4://" + host;
95 else if (scheme == ProxyServer::SCHEME_SOCKS5)
96 host = "socks5://" + host;
[email protected]d7395e732009-08-28 23:13:4397 // If there is a trailing slash, remove it so |host| will parse correctly
98 // even if it includes a port number (since the slash is not numeric).
pkasting9022cb42016-02-05 00:08:5699 if (!host.empty() && host.back() == '/')
[email protected]d7395e732009-08-28 23:13:43100 host.resize(host.length() - 1);
[email protected]861c6c62009-04-20 16:50:56101 return host;
102}
103
Ramin Halavatica8d5252018-03-12 05:33:49104ProxyConfigWithAnnotation GetConfigOrDirect(
Anton Bikineev068d2912021-05-15 20:43:52105 const absl::optional<ProxyConfigWithAnnotation>& optional_config) {
Eric Roman750af4b12018-02-22 22:38:53106 if (optional_config)
107 return optional_config.value();
108
Ramin Halavatica8d5252018-03-12 05:33:49109 ProxyConfigWithAnnotation config = ProxyConfigWithAnnotation::CreateDirect();
Eric Roman750af4b12018-02-22 22:38:53110 return config;
111}
112
[email protected]861c6c62009-04-20 16:50:56113} // namespace
114
Chris Watkins3a13f632017-12-04 00:41:15115ProxyConfigServiceLinux::Delegate::~Delegate() = default;
[email protected]8e1845e12010-09-15 19:22:24116
[email protected]3e44697f2009-05-22 14:37:39117bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme(
thestig0c412e852016-06-30 08:04:40118 base::StringPiece variable,
119 ProxyServer::Scheme scheme,
[email protected]861c6c62009-04-20 16:50:56120 ProxyServer* result_server) {
121 std::string env_value;
thestig0c412e852016-06-30 08:04:40122 if (!env_var_getter_->GetVar(variable, &env_value))
123 return false;
124
125 if (env_value.empty())
126 return false;
127
128 env_value = FixupProxyHostScheme(scheme, env_value);
129 ProxyServer proxy_server =
130 ProxyServer::FromURI(env_value, ProxyServer::SCHEME_HTTP);
131 if (proxy_server.is_valid() && !proxy_server.is_direct()) {
132 *result_server = proxy_server;
133 return true;
[email protected]861c6c62009-04-20 16:50:56134 }
thestig0c412e852016-06-30 08:04:40135 LOG(ERROR) << "Failed to parse environment variable " << variable;
[email protected]861c6c62009-04-20 16:50:56136 return false;
137}
138
[email protected]3e44697f2009-05-22 14:37:39139bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVar(
thestig0c412e852016-06-30 08:04:40140 base::StringPiece variable,
141 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:56142 return GetProxyFromEnvVarForScheme(variable, ProxyServer::SCHEME_HTTP,
143 result_server);
144}
145
Anton Bikineev068d2912021-05-15 20:43:52146absl::optional<ProxyConfigWithAnnotation>
Eric Roman750af4b12018-02-22 22:38:53147ProxyConfigServiceLinux::Delegate::GetConfigFromEnv() {
Ramin Halavatica8d5252018-03-12 05:33:49148 ProxyConfig config;
Eric Roman750af4b12018-02-22 22:38:53149
[email protected]861c6c62009-04-20 16:50:56150 // Check for automatic configuration first, in
151 // "auto_proxy". Possibly only the "environment_proxy" firefox
152 // extension has ever used this, but it still sounds like a good
153 // idea.
154 std::string auto_proxy;
[email protected]3ba7e082010-08-07 02:57:59155 if (env_var_getter_->GetVar("auto_proxy", &auto_proxy)) {
[email protected]861c6c62009-04-20 16:50:56156 if (auto_proxy.empty()) {
157 // Defined and empty => autodetect
Ramin Halavatica8d5252018-03-12 05:33:49158 config.set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:56159 } else {
160 // specified autoconfig URL
Ramin Halavatica8d5252018-03-12 05:33:49161 config.set_pac_url(GURL(auto_proxy));
[email protected]861c6c62009-04-20 16:50:56162 }
Ramin Halavatica8d5252018-03-12 05:33:49163 return ProxyConfigWithAnnotation(
164 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:56165 }
166 // "all_proxy" is a shortcut to avoid defining {http,https,ftp}_proxy.
167 ProxyServer proxy_server;
168 if (GetProxyFromEnvVar("all_proxy", &proxy_server)) {
Ramin Halavatica8d5252018-03-12 05:33:49169 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
170 config.proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56171 } else {
172 bool have_http = GetProxyFromEnvVar("http_proxy", &proxy_server);
173 if (have_http)
Ramin Halavatica8d5252018-03-12 05:33:49174 config.proxy_rules().proxies_for_http.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56175 // It would be tempting to let http_proxy apply for all protocols
176 // if https_proxy and ftp_proxy are not defined. Googling turns up
177 // several documents that mention only http_proxy. But then the
178 // user really might not want to proxy https. And it doesn't seem
179 // like other apps do this. So we will refrain.
180 bool have_https = GetProxyFromEnvVar("https_proxy", &proxy_server);
181 if (have_https)
Ramin Halavatica8d5252018-03-12 05:33:49182 config.proxy_rules().proxies_for_https.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56183 bool have_ftp = GetProxyFromEnvVar("ftp_proxy", &proxy_server);
184 if (have_ftp)
Ramin Halavatica8d5252018-03-12 05:33:49185 config.proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56186 if (have_http || have_https || have_ftp) {
187 // mustn't change type unless some rules are actually set.
Ramin Halavatica8d5252018-03-12 05:33:49188 config.proxy_rules().type =
Lily Houghtone6b617e2018-01-19 20:13:07189 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
[email protected]861c6c62009-04-20 16:50:56190 }
191 }
Ramin Halavatica8d5252018-03-12 05:33:49192 if (config.proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56193 // If the above were not defined, try for socks.
[email protected]e8c50812010-09-28 00:16:17194 // For environment variables, we default to version 5, per the gnome
195 // documentation: http://library.gnome.org/devel/gnet/stable/gnet-socks.html
196 ProxyServer::Scheme scheme = ProxyServer::SCHEME_SOCKS5;
[email protected]861c6c62009-04-20 16:50:56197 std::string env_version;
[email protected]3ba7e082010-08-07 02:57:59198 if (env_var_getter_->GetVar("SOCKS_VERSION", &env_version)
[email protected]e8c50812010-09-28 00:16:17199 && env_version == "4")
200 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:56201 if (GetProxyFromEnvVarForScheme("SOCKS_SERVER", scheme, &proxy_server)) {
Ramin Halavatica8d5252018-03-12 05:33:49202 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
203 config.proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56204 }
205 }
206 // Look for the proxy bypass list.
207 std::string no_proxy;
[email protected]3ba7e082010-08-07 02:57:59208 env_var_getter_->GetVar("no_proxy", &no_proxy);
Ramin Halavatica8d5252018-03-12 05:33:49209 if (config.proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56210 // Having only "no_proxy" set, presumably to "*", makes it
211 // explicit that env vars do specify a configuration: having no
212 // rules specified only means the user explicitly asks for direct
213 // connections.
Ramin Halavatica8d5252018-03-12 05:33:49214 return !no_proxy.empty()
215 ? ProxyConfigWithAnnotation(
216 config, NetworkTrafficAnnotationTag(traffic_annotation_))
Anton Bikineev068d2912021-05-15 20:43:52217 : absl::optional<ProxyConfigWithAnnotation>();
[email protected]861c6c62009-04-20 16:50:56218 }
[email protected]7541206c2010-02-19 20:24:06219 // Note that this uses "suffix" matching. So a bypass of "google.com"
220 // is understood to mean a bypass of "*google.com".
Shimi Zhang13eace252020-01-31 01:49:19221 config.proxy_rules().bypass_rules.ParseFromString(no_proxy);
222 RewriteRulesForSuffixMatching(&config.proxy_rules().bypass_rules);
223
Ramin Halavatica8d5252018-03-12 05:33:49224 return ProxyConfigWithAnnotation(
225 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:56226}
227
228namespace {
229
[email protected]d7395e732009-08-28 23:13:43230const int kDebounceTimeoutMilliseconds = 250;
[email protected]3e44697f2009-05-22 14:37:39231
[email protected]8c20e3d2011-05-19 21:03:57232#if defined(USE_GIO)
Tim Brown1c307cc2017-12-08 02:40:38233const char kProxyGSettingsSchema[] = "org.gnome.system.proxy";
[email protected]2297bb22014-06-19 06:30:14234
[email protected]8c20e3d2011-05-19 21:03:57235// This setting getter uses gsettings, as used in most GNOME 3 desktops.
236class SettingGetterImplGSettings
237 : public ProxyConfigServiceLinux::SettingGetter {
238 public:
danakj8c3eb802015-09-24 07:53:00239 SettingGetterImplGSettings()
thestig0c412e852016-06-30 08:04:40240 : client_(nullptr),
241 http_client_(nullptr),
242 https_client_(nullptr),
243 ftp_client_(nullptr),
244 socks_client_(nullptr),
245 notify_delegate_(nullptr),
danakj8c3eb802015-09-24 07:53:00246 debounce_timer_(new base::OneShotTimer()) {}
[email protected]8c20e3d2011-05-19 21:03:57247
Peter Boström293b1342021-09-22 17:31:43248 SettingGetterImplGSettings(const SettingGetterImplGSettings&) = delete;
249 SettingGetterImplGSettings& operator=(const SettingGetterImplGSettings&) =
250 delete;
251
dcheng67be2b1f2014-10-27 21:47:29252 ~SettingGetterImplGSettings() override {
[email protected]8c20e3d2011-05-19 21:03:57253 // client_ should have been released before now, from
254 // Delegate::OnDestroy(), while running on the UI thread. However
255 // on exiting the process, it may happen that
256 // Delegate::OnDestroy() task is left pending on the glib loop
257 // after the loop was quit, and pending tasks may then be deleted
258 // without being run.
259 if (client_) {
Tim Brown2a19f3b2017-12-12 01:08:40260 // gsettings client was not cleaned up.
eroman0070d412017-06-22 22:18:24261 if (task_runner_->RunsTasksInCurrentSequence()) {
Mostyn Bramley-Moore699c5312018-05-01 10:48:09262 // We are on the UI thread so we can clean it safely.
[email protected]8c20e3d2011-05-19 21:03:57263 VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client";
264 ShutDown();
265 } else {
266 LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client";
thestig0c412e852016-06-30 08:04:40267 client_ = nullptr;
[email protected]8c20e3d2011-05-19 21:03:57268 }
269 }
270 DCHECK(!client_);
[email protected]8c20e3d2011-05-19 21:03:57271 }
272
Tim Brown1c307cc2017-12-08 02:40:38273 // CheckVersion() must be called *before* Init()!
274 bool CheckVersion(base::Environment* env);
[email protected]8c20e3d2011-05-19 21:03:57275
eroman0070d412017-06-22 22:18:24276 bool Init(const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner)
mostynbba063d6032014-10-09 11:01:13277 override {
eroman0070d412017-06-22 22:18:24278 DCHECK(glib_task_runner->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57279 DCHECK(!client_);
[email protected]90499482013-06-01 00:39:50280 DCHECK(!task_runner_.get());
[email protected]4cf80f0b2011-05-20 20:30:26281
Tim Brown1c307cc2017-12-08 02:40:38282 if (!g_settings_schema_source_lookup(g_settings_schema_source_get_default(),
283 kProxyGSettingsSchema, FALSE) ||
284 !(client_ = g_settings_new(kProxyGSettingsSchema))) {
[email protected]8c20e3d2011-05-19 21:03:57285 // It's not clear whether/when this can return NULL.
286 LOG(ERROR) << "Unable to create a gsettings client";
287 return false;
288 }
sergeyu3f923062014-09-05 01:39:40289 task_runner_ = glib_task_runner;
[email protected]8c20e3d2011-05-19 21:03:57290 // We assume these all work if the above call worked.
Tim Brown1c307cc2017-12-08 02:40:38291 http_client_ = g_settings_get_child(client_, "http");
292 https_client_ = g_settings_get_child(client_, "https");
293 ftp_client_ = g_settings_get_child(client_, "ftp");
294 socks_client_ = g_settings_get_child(client_, "socks");
[email protected]8c20e3d2011-05-19 21:03:57295 DCHECK(http_client_ && https_client_ && ftp_client_ && socks_client_);
296 return true;
297 }
298
dcheng67be2b1f2014-10-27 21:47:29299 void ShutDown() override {
[email protected]8c20e3d2011-05-19 21:03:57300 if (client_) {
eroman0070d412017-06-22 22:18:24301 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57302 // This also disables gsettings notifications.
303 g_object_unref(socks_client_);
304 g_object_unref(ftp_client_);
305 g_object_unref(https_client_);
306 g_object_unref(http_client_);
307 g_object_unref(client_);
308 // We only need to null client_ because it's the only one that we check.
thestig0c412e852016-06-30 08:04:40309 client_ = nullptr;
310 task_runner_ = nullptr;
[email protected]8c20e3d2011-05-19 21:03:57311 }
marshall8e5fe942015-03-06 19:22:40312 debounce_timer_.reset();
[email protected]8c20e3d2011-05-19 21:03:57313 }
314
dcheng67be2b1f2014-10-27 21:47:29315 bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13316 ProxyConfigServiceLinux::Delegate* delegate) override {
[email protected]8c20e3d2011-05-19 21:03:57317 DCHECK(client_);
eroman0070d412017-06-22 22:18:24318 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57319 notify_delegate_ = delegate;
320 // We could watch for the change-event signal instead of changed, but
321 // since we have to watch more than one object, we'd still have to
322 // debounce change notifications. This is conceptually simpler.
323 g_signal_connect(G_OBJECT(client_), "changed",
324 G_CALLBACK(OnGSettingsChangeNotification), this);
325 g_signal_connect(G_OBJECT(http_client_), "changed",
326 G_CALLBACK(OnGSettingsChangeNotification), this);
327 g_signal_connect(G_OBJECT(https_client_), "changed",
328 G_CALLBACK(OnGSettingsChangeNotification), this);
329 g_signal_connect(G_OBJECT(ftp_client_), "changed",
330 G_CALLBACK(OnGSettingsChangeNotification), this);
331 g_signal_connect(G_OBJECT(socks_client_), "changed",
332 G_CALLBACK(OnGSettingsChangeNotification), this);
333 // Simulate a change to avoid possibly losing updates before this point.
334 OnChangeNotification();
335 return true;
336 }
337
eroman0070d412017-06-22 22:18:24338 const scoped_refptr<base::SequencedTaskRunner>& GetNotificationTaskRunner()
dcheng67be2b1f2014-10-27 21:47:29339 override {
sergeyu3f923062014-09-05 01:39:40340 return task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57341 }
342
dcheng67be2b1f2014-10-27 21:47:29343 bool GetString(StringSetting key, std::string* result) override {
[email protected]8c20e3d2011-05-19 21:03:57344 DCHECK(client_);
345 switch (key) {
346 case PROXY_MODE:
347 return GetStringByPath(client_, "mode", result);
348 case PROXY_AUTOCONF_URL:
349 return GetStringByPath(client_, "autoconfig-url", result);
350 case PROXY_HTTP_HOST:
351 return GetStringByPath(http_client_, "host", result);
352 case PROXY_HTTPS_HOST:
353 return GetStringByPath(https_client_, "host", result);
354 case PROXY_FTP_HOST:
355 return GetStringByPath(ftp_client_, "host", result);
356 case PROXY_SOCKS_HOST:
357 return GetStringByPath(socks_client_, "host", result);
[email protected]8c20e3d2011-05-19 21:03:57358 }
[email protected]6b5fe742011-05-20 21:46:48359 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57360 }
dcheng67be2b1f2014-10-27 21:47:29361 bool GetBool(BoolSetting key, bool* result) override {
[email protected]8c20e3d2011-05-19 21:03:57362 DCHECK(client_);
363 switch (key) {
364 case PROXY_USE_HTTP_PROXY:
365 // Although there is an "enabled" boolean in http_client_, it is not set
366 // to true by the proxy config utility. We ignore it and return false.
367 return false;
368 case PROXY_USE_SAME_PROXY:
369 // Similarly, although there is a "use-same-proxy" boolean in client_,
370 // it is never set to false by the proxy config utility. We ignore it.
371 return false;
372 case PROXY_USE_AUTHENTICATION:
373 // There is also no way to set this in the proxy config utility, but it
374 // doesn't hurt us to get the actual setting (unlike the two above).
375 return GetBoolByPath(http_client_, "use-authentication", result);
[email protected]8c20e3d2011-05-19 21:03:57376 }
[email protected]6b5fe742011-05-20 21:46:48377 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57378 }
dcheng67be2b1f2014-10-27 21:47:29379 bool GetInt(IntSetting key, int* result) override {
[email protected]8c20e3d2011-05-19 21:03:57380 DCHECK(client_);
381 switch (key) {
382 case PROXY_HTTP_PORT:
383 return GetIntByPath(http_client_, "port", result);
384 case PROXY_HTTPS_PORT:
385 return GetIntByPath(https_client_, "port", result);
386 case PROXY_FTP_PORT:
387 return GetIntByPath(ftp_client_, "port", result);
388 case PROXY_SOCKS_PORT:
389 return GetIntByPath(socks_client_, "port", result);
[email protected]8c20e3d2011-05-19 21:03:57390 }
[email protected]6b5fe742011-05-20 21:46:48391 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57392 }
dcheng67be2b1f2014-10-27 21:47:29393 bool GetStringList(StringListSetting key,
394 std::vector<std::string>* result) override {
[email protected]8c20e3d2011-05-19 21:03:57395 DCHECK(client_);
396 switch (key) {
397 case PROXY_IGNORE_HOSTS:
398 return GetStringListByPath(client_, "ignore-hosts", result);
[email protected]8c20e3d2011-05-19 21:03:57399 }
[email protected]6b5fe742011-05-20 21:46:48400 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57401 }
402
dcheng67be2b1f2014-10-27 21:47:29403 bool BypassListIsReversed() override {
[email protected]8c20e3d2011-05-19 21:03:57404 // This is a KDE-specific setting.
405 return false;
406 }
407
Shimi Zhang13eace252020-01-31 01:49:19408 bool UseSuffixMatching() override { return false; }
[email protected]8c20e3d2011-05-19 21:03:57409
410 private:
thestig0c412e852016-06-30 08:04:40411 bool GetStringByPath(GSettings* client,
412 base::StringPiece key,
[email protected]8c20e3d2011-05-19 21:03:57413 std::string* result) {
eroman0070d412017-06-22 22:18:24414 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38415 gchar* value = g_settings_get_string(client, key.data());
[email protected]8c20e3d2011-05-19 21:03:57416 if (!value)
417 return false;
418 *result = value;
419 g_free(value);
420 return true;
421 }
thestig0c412e852016-06-30 08:04:40422 bool GetBoolByPath(GSettings* client, base::StringPiece key, bool* result) {
eroman0070d412017-06-22 22:18:24423 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38424 *result = static_cast<bool>(g_settings_get_boolean(client, key.data()));
[email protected]8c20e3d2011-05-19 21:03:57425 return true;
426 }
thestig0c412e852016-06-30 08:04:40427 bool GetIntByPath(GSettings* client, base::StringPiece key, int* result) {
eroman0070d412017-06-22 22:18:24428 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38429 *result = g_settings_get_int(client, key.data());
[email protected]8c20e3d2011-05-19 21:03:57430 return true;
431 }
thestig0c412e852016-06-30 08:04:40432 bool GetStringListByPath(GSettings* client,
433 base::StringPiece key,
[email protected]8c20e3d2011-05-19 21:03:57434 std::vector<std::string>* result) {
eroman0070d412017-06-22 22:18:24435 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38436 gchar** list = g_settings_get_strv(client, key.data());
[email protected]8c20e3d2011-05-19 21:03:57437 if (!list)
438 return false;
439 for (size_t i = 0; list[i]; ++i) {
440 result->push_back(static_cast<char*>(list[i]));
441 g_free(list[i]);
442 }
443 g_free(list);
444 return true;
445 }
446
447 // This is the callback from the debounce timer.
448 void OnDebouncedNotification() {
eroman0070d412017-06-22 22:18:24449 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57450 CHECK(notify_delegate_);
451 // Forward to a method on the proxy config service delegate object.
452 notify_delegate_->OnCheckProxyConfigSettings();
453 }
454
455 void OnChangeNotification() {
456 // We don't use Reset() because the timer may not yet be running.
457 // (In that case Stop() is a no-op.)
marshall8e5fe942015-03-06 19:22:40458 debounce_timer_->Stop();
459 debounce_timer_->Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57460 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
461 this, &SettingGetterImplGSettings::OnDebouncedNotification);
462 }
463
464 // gsettings notification callback, dispatched on the default glib main loop.
465 static void OnGSettingsChangeNotification(GSettings* client, gchar* key,
466 gpointer user_data) {
467 VLOG(1) << "gsettings change notification for key " << key;
468 // We don't track which key has changed, just that something did change.
469 SettingGetterImplGSettings* setting_getter =
470 reinterpret_cast<SettingGetterImplGSettings*>(user_data);
471 setting_getter->OnChangeNotification();
472 }
473
474 GSettings* client_;
475 GSettings* http_client_;
476 GSettings* https_client_;
477 GSettings* ftp_client_;
478 GSettings* socks_client_;
479 ProxyConfigServiceLinux::Delegate* notify_delegate_;
danakj8a98ca22016-04-16 02:47:36480 std::unique_ptr<base::OneShotTimer> debounce_timer_;
[email protected]8c20e3d2011-05-19 21:03:57481
[email protected]76722472012-05-24 08:26:46482 // Task runner for the thread that we make gsettings calls on. It should
[email protected]8c20e3d2011-05-19 21:03:57483 // be the UI thread and all our methods should be called on this
484 // thread. Only for assertions.
eroman0070d412017-06-22 22:18:24485 scoped_refptr<base::SequencedTaskRunner> task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57486};
487
Tim Brown1c307cc2017-12-08 02:40:38488bool SettingGetterImplGSettings::CheckVersion(
[email protected]8c20e3d2011-05-19 21:03:57489 base::Environment* env) {
Tim Brown1c307cc2017-12-08 02:40:38490 // CheckVersion() must be called *before* Init()!
[email protected]8c20e3d2011-05-19 21:03:57491 DCHECK(!client_);
492
thestig0c412e852016-06-30 08:04:40493 GSettings* client = nullptr;
Tim Brown1c307cc2017-12-08 02:40:38494 if (g_settings_schema_source_lookup(g_settings_schema_source_get_default(),
495 kProxyGSettingsSchema, FALSE)) {
496 client = g_settings_new(kProxyGSettingsSchema);
[email protected]4bbb72d2014-06-06 18:05:51497 }
498 if (!client) {
Tim Brown2a19f3b2017-12-12 01:08:40499 VLOG(1) << "Cannot create gsettings client.";
[email protected]8c20e3d2011-05-19 21:03:57500 return false;
501 }
502 g_object_unref(client);
503
[email protected]8c20e3d2011-05-19 21:03:57504 VLOG(1) << "All gsettings tests OK. Will get proxy config from gsettings.";
505 return true;
506}
507#endif // defined(USE_GIO)
508
eromane44498c2017-06-30 00:02:37509// Converts |value| from a decimal string to an int. If there was a failure
510// parsing, returns |default_value|.
511int StringToIntOrDefault(base::StringPiece value, int default_value) {
512 int result;
513 if (base::StringToInt(value, &result))
514 return result;
515 return default_value;
516}
517
Tim Brown2a19f3b2017-12-12 01:08:40518// This is the KDE version that reads kioslaverc and simulates gsettings.
[email protected]d7395e732009-08-28 23:13:43519// Doing this allows the main Delegate code, as well as the unit tests
520// for it, to stay the same - and the settings map fairly well besides.
gabf4f904e2017-05-10 20:55:02521class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter {
[email protected]d7395e732009-08-28 23:13:43522 public:
[email protected]573c0502011-05-17 22:19:50523 explicit SettingGetterImplKDE(base::Environment* env_var_getter)
marshall8e5fe942015-03-06 19:22:40524 : inotify_fd_(-1),
thestig0c412e852016-06-30 08:04:40525 notify_delegate_(nullptr),
danakj8c3eb802015-09-24 07:53:00526 debounce_timer_(new base::OneShotTimer()),
marshall8e5fe942015-03-06 19:22:40527 indirect_manual_(false),
528 auto_no_pac_(false),
529 reversed_bypass_list_(false),
530 env_var_getter_(env_var_getter),
thestig0c412e852016-06-30 08:04:40531 file_task_runner_(nullptr) {
[email protected]9a8c4022011-01-25 14:25:33532 // This has to be called on the UI thread (http://crbug.com/69057).
533 base::ThreadRestrictions::ScopedAllowIO allow_io;
534
[email protected]f18fde22010-05-18 23:49:54535 // Derive the location of the kde config dir from the environment.
[email protected]92d2dc82010-04-08 17:49:59536 std::string home;
[email protected]3ba7e082010-08-07 02:57:59537 if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) {
[email protected]2e8cfe22010-06-12 00:26:24538 // $KDEHOME is set. Use it unconditionally.
[email protected]6cdfd7f2013-02-08 20:40:15539 kde_config_dir_ = KDEHomeToConfigPath(base::FilePath(home));
[email protected]92d2dc82010-04-08 17:49:59540 } else {
[email protected]2e8cfe22010-06-12 00:26:24541 // $KDEHOME is unset. Try to figure out what to use. This seems to be
[email protected]92d2dc82010-04-08 17:49:59542 // the common case on most distributions.
[email protected]3ba7e082010-08-07 02:57:59543 if (!env_var_getter->GetVar(base::env_vars::kHome, &home))
[email protected]d7395e732009-08-28 23:13:43544 // User has no $HOME? Give up. Later we'll report the failure.
545 return;
[email protected]6b0349ef2010-10-16 04:56:06546 if (base::nix::GetDesktopEnvironment(env_var_getter) ==
547 base::nix::DESKTOP_ENVIRONMENT_KDE3) {
[email protected]92d2dc82010-04-08 17:49:59548 // KDE3 always uses .kde for its configuration.
[email protected]6cdfd7f2013-02-08 20:40:15549 base::FilePath kde_path = base::FilePath(home).Append(".kde");
[email protected]92d2dc82010-04-08 17:49:59550 kde_config_dir_ = KDEHomeToConfigPath(kde_path);
edward.baker53bec302015-10-02 16:57:49551 } else if (base::nix::GetDesktopEnvironment(env_var_getter) ==
552 base::nix::DESKTOP_ENVIRONMENT_KDE4) {
[email protected]92d2dc82010-04-08 17:49:59553 // Some distributions patch KDE4 to use .kde4 instead of .kde, so that
[email protected]fad9c8a52010-06-10 22:30:53554 // both can be installed side-by-side. Sadly they don't all do this, and
555 // they don't always do this: some distributions have started switching
556 // back as well. So if there is a .kde4 directory, check the timestamps
557 // of the config directories within and use the newest one.
[email protected]92d2dc82010-04-08 17:49:59558 // Note that we should currently be running in the UI thread, because in
Tim Brown2a19f3b2017-12-12 01:08:40559 // the gsettings version, that is the only thread that can access the
560 // proxy settings (a gsettings restriction). As noted below, the initial
561 // read of the proxy settings will be done in this thread anyway, so we
562 // check for .kde4 here in this thread as well.
[email protected]6cdfd7f2013-02-08 20:40:15563 base::FilePath kde3_path = base::FilePath(home).Append(".kde");
564 base::FilePath kde3_config = KDEHomeToConfigPath(kde3_path);
565 base::FilePath kde4_path = base::FilePath(home).Append(".kde4");
566 base::FilePath kde4_config = KDEHomeToConfigPath(kde4_path);
[email protected]fad9c8a52010-06-10 22:30:53567 bool use_kde4 = false;
[email protected]dcd16612013-07-15 20:18:09568 if (base::DirectoryExists(kde4_path)) {
[email protected]54124ed02014-01-07 10:06:58569 base::File::Info kde3_info;
570 base::File::Info kde4_info;
[email protected]9eae4e62013-12-04 20:56:49571 if (base::GetFileInfo(kde4_config, &kde4_info)) {
572 if (base::GetFileInfo(kde3_config, &kde3_info)) {
[email protected]fad9c8a52010-06-10 22:30:53573 use_kde4 = kde4_info.last_modified >= kde3_info.last_modified;
574 } else {
575 use_kde4 = true;
576 }
577 }
578 }
579 if (use_kde4) {
[email protected]92d2dc82010-04-08 17:49:59580 kde_config_dir_ = KDEHomeToConfigPath(kde4_path);
581 } else {
[email protected]fad9c8a52010-06-10 22:30:53582 kde_config_dir_ = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59583 }
edward.baker53bec302015-10-02 16:57:49584 } else {
585 // KDE 5 migrated to ~/.config for storing kioslaverc.
586 kde_config_dir_ = base::FilePath(home).Append(".config");
[email protected]92d2dc82010-04-08 17:49:59587 }
[email protected]d7395e732009-08-28 23:13:43588 }
[email protected]d7395e732009-08-28 23:13:43589 }
590
Peter Boström293b1342021-09-22 17:31:43591 SettingGetterImplKDE(const SettingGetterImplKDE&) = delete;
592 SettingGetterImplKDE& operator=(const SettingGetterImplKDE&) = delete;
593
dcheng67be2b1f2014-10-27 21:47:29594 ~SettingGetterImplKDE() override {
[email protected]d7395e732009-08-28 23:13:43595 // inotify_fd_ should have been closed before now, from
596 // Delegate::OnDestroy(), while running on the file thread. However
597 // on exiting the process, it may happen that Delegate::OnDestroy()
598 // task is left pending on the file loop after the loop was quit,
599 // and pending tasks may then be deleted without being run.
600 // Here in the KDE version, we can safely close the file descriptor
601 // anyway. (Not that it really matters; the process is exiting.)
602 if (inotify_fd_ >= 0)
[email protected]d3066142011-05-10 02:36:20603 ShutDown();
thestig0c412e852016-06-30 08:04:40604 DCHECK_LT(inotify_fd_, 0);
[email protected]d7395e732009-08-28 23:13:43605 }
606
eroman0070d412017-06-22 22:18:24607 bool Init(const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner)
mostynbba063d6032014-10-09 11:01:13608 override {
[email protected]9a8c4022011-01-25 14:25:33609 // This has to be called on the UI thread (http://crbug.com/69057).
610 base::ThreadRestrictions::ScopedAllowIO allow_io;
thestig0c412e852016-06-30 08:04:40611 DCHECK_LT(inotify_fd_, 0);
[email protected]d7395e732009-08-28 23:13:43612 inotify_fd_ = inotify_init();
613 if (inotify_fd_ < 0) {
[email protected]57b765672009-10-13 18:27:40614 PLOG(ERROR) << "inotify_init failed";
[email protected]d7395e732009-08-28 23:13:43615 return false;
616 }
tfarina89b4ae1c2015-12-16 18:59:18617 if (!base::SetNonBlocking(inotify_fd_)) {
618 PLOG(ERROR) << "base::SetNonBlocking failed";
[email protected]d7395e732009-08-28 23:13:43619 close(inotify_fd_);
620 inotify_fd_ = -1;
621 return false;
622 }
eroman0070d412017-06-22 22:18:24623
Gabriel Charette4049d422020-02-29 00:43:27624 constexpr base::TaskTraits kTraits = {base::TaskPriority::USER_VISIBLE,
625 base::MayBlock()};
626 file_task_runner_ = base::ThreadPool::CreateSequencedTaskRunner(kTraits);
eroman0070d412017-06-22 22:18:24627
sergeyu3f923062014-09-05 01:39:40628 // The initial read is done on the current thread, not
629 // |file_task_runner_|, since we will need to have it for
630 // SetUpAndFetchInitialConfig().
[email protected]d7395e732009-08-28 23:13:43631 UpdateCachedSettings();
632 return true;
633 }
634
dcheng67be2b1f2014-10-27 21:47:29635 void ShutDown() override {
[email protected]d7395e732009-08-28 23:13:43636 if (inotify_fd_ >= 0) {
637 ResetCachedSettings();
gabf4f904e2017-05-10 20:55:02638 inotify_watcher_.reset();
[email protected]d7395e732009-08-28 23:13:43639 close(inotify_fd_);
640 inotify_fd_ = -1;
641 }
marshall8e5fe942015-03-06 19:22:40642 debounce_timer_.reset();
[email protected]d7395e732009-08-28 23:13:43643 }
644
dcheng67be2b1f2014-10-27 21:47:29645 bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13646 ProxyConfigServiceLinux::Delegate* delegate) override {
thestig0c412e852016-06-30 08:04:40647 DCHECK_GE(inotify_fd_, 0);
eroman0070d412017-06-22 22:18:24648 DCHECK(file_task_runner_->RunsTasksInCurrentSequence());
[email protected]d7395e732009-08-28 23:13:43649 // We can't just watch the kioslaverc file directly, since KDE will write
650 // a new copy of it and then rename it whenever settings are changed and
651 // inotify watches inodes (so we'll be watching the old deleted file after
652 // the first change, and it will never change again). So, we watch the
653 // directory instead. We then act only on changes to the kioslaverc entry.
eroman6b0ca662017-06-22 00:16:36654 // TODO(eroman): What if the file is deleted? (handle with IN_DELETE).
[email protected]d7395e732009-08-28 23:13:43655 if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(),
sergeyu3f923062014-09-05 01:39:40656 IN_MODIFY | IN_MOVED_TO) < 0) {
[email protected]d7395e732009-08-28 23:13:43657 return false;
sergeyu3f923062014-09-05 01:39:40658 }
[email protected]d7395e732009-08-28 23:13:43659 notify_delegate_ = delegate;
gabf4f904e2017-05-10 20:55:02660 inotify_watcher_ = base::FileDescriptorWatcher::WatchReadable(
Anna Malovaae7007aa2020-03-09 16:48:48661 inotify_fd_,
662 base::BindRepeating(&SettingGetterImplKDE::OnChangeNotification,
663 base::Unretained(this)));
[email protected]d3066142011-05-10 02:36:20664 // Simulate a change to avoid possibly losing updates before this point.
665 OnChangeNotification();
666 return true;
[email protected]d7395e732009-08-28 23:13:43667 }
668
eroman0070d412017-06-22 22:18:24669 const scoped_refptr<base::SequencedTaskRunner>& GetNotificationTaskRunner()
dcheng67be2b1f2014-10-27 21:47:29670 override {
sergeyu3f923062014-09-05 01:39:40671 return file_task_runner_;
[email protected]d7395e732009-08-28 23:13:43672 }
673
dcheng67be2b1f2014-10-27 21:47:29674 bool GetString(StringSetting key, std::string* result) override {
jdoerrie22a91d8b92018-10-05 08:43:26675 auto it = string_table_.find(key);
[email protected]d7395e732009-08-28 23:13:43676 if (it == string_table_.end())
677 return false;
678 *result = it->second;
679 return true;
680 }
dcheng67be2b1f2014-10-27 21:47:29681 bool GetBool(BoolSetting key, bool* result) override {
[email protected]d7395e732009-08-28 23:13:43682 // We don't ever have any booleans.
683 return false;
684 }
dcheng67be2b1f2014-10-27 21:47:29685 bool GetInt(IntSetting key, int* result) override {
[email protected]d7395e732009-08-28 23:13:43686 // We don't ever have any integers. (See AddProxy() below about ports.)
687 return false;
688 }
dcheng67be2b1f2014-10-27 21:47:29689 bool GetStringList(StringListSetting key,
690 std::vector<std::string>* result) override {
jdoerrie22a91d8b92018-10-05 08:43:26691 auto it = strings_table_.find(key);
[email protected]d7395e732009-08-28 23:13:43692 if (it == strings_table_.end())
693 return false;
694 *result = it->second;
695 return true;
696 }
697
dcheng67be2b1f2014-10-27 21:47:29698 bool BypassListIsReversed() override { return reversed_bypass_list_; }
[email protected]a48bf4a2010-06-14 18:24:53699
Shimi Zhang13eace252020-01-31 01:49:19700 bool UseSuffixMatching() override { return true; }
[email protected]1a597192010-07-09 16:58:38701
[email protected]d7395e732009-08-28 23:13:43702 private:
703 void ResetCachedSettings() {
704 string_table_.clear();
705 strings_table_.clear();
706 indirect_manual_ = false;
707 auto_no_pac_ = false;
[email protected]a48bf4a2010-06-14 18:24:53708 reversed_bypass_list_ = false;
[email protected]d7395e732009-08-28 23:13:43709 }
710
[email protected]6cdfd7f2013-02-08 20:40:15711 base::FilePath KDEHomeToConfigPath(const base::FilePath& kde_home) {
[email protected]92d2dc82010-04-08 17:49:59712 return kde_home.Append("share").Append("config");
713 }
714
[email protected]6b5fe742011-05-20 21:46:48715 void AddProxy(StringSetting host_key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:43716 if (value.empty() || value.substr(0, 3) == "//:")
717 // No proxy.
718 return;
[email protected]4b90c202012-04-24 23:27:55719 size_t space = value.find(' ');
720 if (space != std::string::npos) {
721 // Newer versions of KDE use a space rather than a colon to separate the
722 // port number from the hostname. If we find this, we need to convert it.
723 std::string fixed = value;
724 fixed[space] = ':';
725 string_table_[host_key] = fixed;
726 } else {
727 // We don't need to parse the port number out; GetProxyFromSettings()
728 // would only append it right back again. So we just leave the port
729 // number right in the host string.
730 string_table_[host_key] = value;
731 }
[email protected]d7395e732009-08-28 23:13:43732 }
733
[email protected]6b5fe742011-05-20 21:46:48734 void AddHostList(StringListSetting key, const std::string& value) {
[email protected]f18fde22010-05-18 23:49:54735 std::vector<std::string> tokens;
[email protected]f4ebe772013-02-02 00:21:39736 base::StringTokenizer tk(value, ", ");
[email protected]f18fde22010-05-18 23:49:54737 while (tk.GetNext()) {
738 std::string token = tk.token();
739 if (!token.empty())
740 tokens.push_back(token);
741 }
742 strings_table_[key] = tokens;
743 }
744
[email protected]9a3d8d42009-09-03 17:01:46745 void AddKDESetting(const std::string& key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:43746 if (key == "ProxyType") {
747 const char* mode = "none";
748 indirect_manual_ = false;
749 auto_no_pac_ = false;
eromane44498c2017-06-30 00:02:37750 int int_value = StringToIntOrDefault(value, 0);
[email protected]e83326f2010-07-31 17:29:25751 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:43752 case 1: // Manual configuration.
753 mode = "manual";
754 break;
755 case 2: // PAC URL.
756 mode = "auto";
757 break;
758 case 3: // WPAD.
759 mode = "auto";
760 auto_no_pac_ = true;
761 break;
762 case 4: // Indirect manual via environment variables.
763 mode = "manual";
764 indirect_manual_ = true;
765 break;
eromane44498c2017-06-30 00:02:37766 default: // No proxy, or maybe kioslaverc syntax error.
767 break;
[email protected]d7395e732009-08-28 23:13:43768 }
[email protected]573c0502011-05-17 22:19:50769 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:43770 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:50771 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:43772 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:50773 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:43774 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:50775 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:43776 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:50777 AddProxy(PROXY_FTP_HOST, value);
[email protected]bfeb7232012-06-08 00:58:37778 } else if (key == "socksProxy") {
779 // Older versions of KDE configure SOCKS in a weird way involving
780 // LD_PRELOAD and a library that intercepts network calls to SOCKSify
781 // them. We don't support it. KDE 4.8 added a proper SOCKS setting.
782 AddProxy(PROXY_SOCKS_HOST, value);
[email protected]d7395e732009-08-28 23:13:43783 } else if (key == "ReversedException") {
784 // We count "true" or any nonzero number as true, otherwise false.
eromane44498c2017-06-30 00:02:37785 // A failure parsing the integer will also mean false.
786 reversed_bypass_list_ =
787 (value == "true" || StringToIntOrDefault(value, 0) != 0);
[email protected]d7395e732009-08-28 23:13:43788 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:50789 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:43790 } else if (key == "AuthMode") {
791 // Check for authentication, just so we can warn.
eromane44498c2017-06-30 00:02:37792 int mode = StringToIntOrDefault(value, 0);
[email protected]d7395e732009-08-28 23:13:43793 if (mode) {
794 // ProxyConfig does not support authentication parameters, but
795 // Chrome will prompt for the password later. So we ignore this.
796 LOG(WARNING) <<
797 "Proxy authentication parameters ignored, see bug 16709";
798 }
799 }
800 }
801
[email protected]6b5fe742011-05-20 21:46:48802 void ResolveIndirect(StringSetting key) {
jdoerrie22a91d8b92018-10-05 08:43:26803 auto it = string_table_.find(key);
[email protected]d7395e732009-08-28 23:13:43804 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:54805 std::string value;
[email protected]3ba7e082010-08-07 02:57:59806 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:43807 it->second = value;
[email protected]8425adc02010-04-18 17:45:31808 else
809 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:43810 }
811 }
812
[email protected]6b5fe742011-05-20 21:46:48813 void ResolveIndirectList(StringListSetting key) {
jdoerrie22a91d8b92018-10-05 08:43:26814 auto it = strings_table_.find(key);
[email protected]f18fde22010-05-18 23:49:54815 if (it != strings_table_.end()) {
816 std::string value;
817 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:59818 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:54819 AddHostList(key, value);
820 else
821 strings_table_.erase(it);
822 }
823 }
824
[email protected]d7395e732009-08-28 23:13:43825 // The settings in kioslaverc could occur in any order, but some affect
826 // others. Rather than read the whole file in and then query them in an
827 // order that allows us to handle that, we read the settings in whatever
828 // order they occur and do any necessary tweaking after we finish.
829 void ResolveModeEffects() {
830 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:50831 ResolveIndirect(PROXY_HTTP_HOST);
832 ResolveIndirect(PROXY_HTTPS_HOST);
833 ResolveIndirect(PROXY_FTP_HOST);
Maks Orlovichfee43b12021-06-17 21:10:08834 ResolveIndirect(PROXY_SOCKS_HOST);
[email protected]573c0502011-05-17 22:19:50835 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:43836 }
837 if (auto_no_pac_) {
838 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:50839 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:43840 }
[email protected]d7395e732009-08-28 23:13:43841 }
842
843 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
844 // each relevant name-value pair to the appropriate value table.
845 void UpdateCachedSettings() {
[email protected]6cdfd7f2013-02-08 20:40:15846 base::FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]b9b4a572014-03-17 23:11:12847 base::ScopedFILE input(base::OpenFile(kioslaverc, "r"));
[email protected]d7395e732009-08-28 23:13:43848 if (!input.get())
849 return;
850 ResetCachedSettings();
851 bool in_proxy_settings = false;
852 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:46853 char line[BUFFER_SIZE];
854 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:43855 while (fgets(line, sizeof(line), input.get())) {
856 // fgets() guarantees the line will be properly terminated.
857 size_t length = strlen(line);
858 if (!length)
859 continue;
860 // This should be true even with CRLF endings.
861 if (line[length - 1] != '\n') {
862 line_too_long = true;
863 continue;
864 }
865 if (line_too_long) {
866 // The previous line had no line ending, but this done does. This is
867 // the end of the line that was too long, so warn here and skip it.
868 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
869 line_too_long = false;
870 continue;
871 }
872 // Remove the LF at the end, and the CR if there is one.
873 line[--length] = '\0';
874 if (length && line[length - 1] == '\r')
875 line[--length] = '\0';
876 // Now parse the line.
877 if (line[0] == '[') {
878 // Switching sections. All we care about is whether this is
879 // the (a?) proxy settings section, for both KDE3 and KDE4.
880 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
881 } else if (in_proxy_settings) {
882 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:46883 char* split = strchr(line, '=');
884 // Skip this line if it does not contain an = sign.
885 if (!split)
[email protected]d7395e732009-08-28 23:13:43886 continue;
[email protected]9a3d8d42009-09-03 17:01:46887 // Split the line on the = and advance |split|.
888 *(split++) = 0;
889 std::string key = line;
890 std::string value = split;
[email protected]8af69c6c2014-03-03 19:05:31891 base::TrimWhitespaceASCII(key, base::TRIM_ALL, &key);
892 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value);
[email protected]9a3d8d42009-09-03 17:01:46893 // Skip this line if the key name is empty.
894 if (key.empty())
[email protected]d7395e732009-08-28 23:13:43895 continue;
896 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:46897 if (key[key.length() - 1] == ']') {
898 // Find the matching bracket.
899 length = key.rfind('[');
900 // Skip this line if the localization indicator is malformed.
901 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:43902 continue;
903 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:46904 key.resize(length);
905 // Remove any resulting trailing whitespace.
[email protected]8af69c6c2014-03-03 19:05:31906 base::TrimWhitespaceASCII(key, base::TRIM_TRAILING, &key);
[email protected]9a3d8d42009-09-03 17:01:46907 // Skip this line if the key name is now empty.
908 if (key.empty())
909 continue;
[email protected]d7395e732009-08-28 23:13:43910 }
[email protected]d7395e732009-08-28 23:13:43911 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:46912 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:43913 }
914 }
915 if (ferror(input.get()))
916 LOG(ERROR) << "error reading " << kioslaverc.value();
917 ResolveModeEffects();
918 }
919
920 // This is the callback from the debounce timer.
921 void OnDebouncedNotification() {
eroman0070d412017-06-22 22:18:24922 DCHECK(file_task_runner_->RunsTasksInCurrentSequence());
[email protected]b30a3f52010-10-16 01:05:46923 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:43924 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:14925 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:43926 // Forward to a method on the proxy config service delegate object.
927 notify_delegate_->OnCheckProxyConfigSettings();
928 }
929
930 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
931 // from the inotify file descriptor and starts up a debounce timer if
932 // an event for kioslaverc is seen.
933 void OnChangeNotification() {
[email protected]d2e6d592012-02-03 21:49:04934 DCHECK_GE(inotify_fd_, 0);
eroman0070d412017-06-22 22:18:24935 DCHECK(file_task_runner_->RunsTasksInCurrentSequence());
[email protected]d7395e732009-08-28 23:13:43936 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
937 bool kioslaverc_touched = false;
938 ssize_t r;
939 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
940 // inotify returns variable-length structures, which is why we have
941 // this strange-looking loop instead of iterating through an array.
942 char* event_ptr = event_buf;
943 while (event_ptr < event_buf + r) {
944 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
945 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:33946 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
947 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:43948 if (!strcmp(event->name, "kioslaverc"))
949 kioslaverc_touched = true;
950 // Advance the pointer just past the end of the filename.
951 event_ptr = event->name + event->len;
952 }
953 // We keep reading even if |kioslaverc_touched| is true to drain the
954 // inotify event queue.
955 }
956 if (!r)
957 // Instead of returning -1 and setting errno to EINVAL if there is not
958 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
959 // new behavior (EINVAL) so we can reuse the code below.
960 errno = EINVAL;
961 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:40962 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:43963 if (errno == EINVAL) {
964 // Our buffer is not large enough to read the next event. This should
965 // not happen (because its size is calculated to always be sufficiently
966 // large), but if it does we'd warn continuously since |inotify_fd_|
967 // would be forever ready to read. Close it and stop watching instead.
968 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
gabf4f904e2017-05-10 20:55:02969 inotify_watcher_.reset();
[email protected]d7395e732009-08-28 23:13:43970 close(inotify_fd_);
971 inotify_fd_ = -1;
972 }
973 }
974 if (kioslaverc_touched) {
eroman6b0ca662017-06-22 00:16:36975 LOG(ERROR) << "kioslaverc_touched";
[email protected]d7395e732009-08-28 23:13:43976 // We don't use Reset() because the timer may not yet be running.
977 // (In that case Stop() is a no-op.)
marshall8e5fe942015-03-06 19:22:40978 debounce_timer_->Stop();
979 debounce_timer_->Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
[email protected]d7395e732009-08-28 23:13:43980 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:50981 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:43982 }
983 }
984
[email protected]6b5fe742011-05-20 21:46:48985 typedef std::map<StringSetting, std::string> string_map_type;
986 typedef std::map<StringListSetting,
987 std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:43988
989 int inotify_fd_;
gabf4f904e2017-05-10 20:55:02990 std::unique_ptr<base::FileDescriptorWatcher::Controller> inotify_watcher_;
[email protected]d7395e732009-08-28 23:13:43991 ProxyConfigServiceLinux::Delegate* notify_delegate_;
danakj8a98ca22016-04-16 02:47:36992 std::unique_ptr<base::OneShotTimer> debounce_timer_;
[email protected]6cdfd7f2013-02-08 20:40:15993 base::FilePath kde_config_dir_;
[email protected]d7395e732009-08-28 23:13:43994 bool indirect_manual_;
995 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:53996 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:54997 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
998 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
999 // same lifetime.
[email protected]76b90d312010-08-03 03:00:501000 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:431001
1002 // We cache these settings whenever we re-read the kioslaverc file.
1003 string_map_type string_table_;
1004 strings_map_type strings_table_;
1005
eroman0070d412017-06-22 22:18:241006 // Task runner for doing blocking file IO on, as well as handling inotify
1007 // events on.
1008 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
[email protected]861c6c62009-04-20 16:50:561009};
1010
1011} // namespace
1012
[email protected]573c0502011-05-17 22:19:501013bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
[email protected]6b5fe742011-05-20 21:46:481014 SettingGetter::StringSetting host_key,
[email protected]573c0502011-05-17 22:19:501015 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561016 std::string host;
[email protected]573c0502011-05-17 22:19:501017 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561018 // Unset or empty.
1019 return false;
1020 }
1021 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431022 int port = 0;
[email protected]6b5fe742011-05-20 21:46:481023 SettingGetter::IntSetting port_key =
[email protected]573c0502011-05-17 22:19:501024 SettingGetter::HostSettingToPortSetting(host_key);
1025 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561026 if (port != 0) {
1027 // If a port is set and non-zero:
Raul Tambre8c1981d2019-02-08 02:22:261028 host += ":" + base::NumberToString(port);
[email protected]861c6c62009-04-20 16:50:561029 }
[email protected]76960f3d2011-04-30 02:15:231030
Tim Brown2a19f3b2017-12-12 01:08:401031 // gsettings settings do not appear to distinguish between SOCKS version. We
[email protected]573c0502011-05-17 22:19:501032 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231033 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501034 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1035 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1036 host = FixupProxyHostScheme(scheme, host);
[email protected]87a102b2009-07-14 05:23:301037 ProxyServer proxy_server = ProxyServer::FromURI(host,
1038 ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561039 if (proxy_server.is_valid()) {
1040 *result_server = proxy_server;
1041 return true;
1042 }
1043 return false;
1044}
1045
Anton Bikineev068d2912021-05-15 20:43:521046absl::optional<ProxyConfigWithAnnotation>
Eric Roman750af4b12018-02-22 22:38:531047ProxyConfigServiceLinux::Delegate::GetConfigFromSettings() {
Ramin Halavatica8d5252018-03-12 05:33:491048 ProxyConfig config;
Eric Roman750af4b12018-02-22 22:38:531049
[email protected]861c6c62009-04-20 16:50:561050 std::string mode;
[email protected]573c0502011-05-17 22:19:501051 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
Tim Brown2a19f3b2017-12-12 01:08:401052 // We expect this to always be set, so if we don't see it then we probably
1053 // have a gsettings problem, and so we don't have a valid proxy config.
Anton Bikineev068d2912021-05-15 20:43:521054 return absl::nullopt;
[email protected]861c6c62009-04-20 16:50:561055 }
[email protected]3e44697f2009-05-22 14:37:391056 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561057 // Specifically specifies no proxy.
Ramin Halavatica8d5252018-03-12 05:33:491058 return ProxyConfigWithAnnotation(
1059 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]3e44697f2009-05-22 14:37:391060 }
[email protected]861c6c62009-04-20 16:50:561061
[email protected]3e44697f2009-05-22 14:37:391062 if (mode == "auto") {
[email protected]aa3ac2cc2012-06-19 00:28:041063 // Automatic proxy config.
[email protected]861c6c62009-04-20 16:50:561064 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501065 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1066 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561067 if (!pac_url_str.empty()) {
[email protected]aa3ac2cc2012-06-19 00:28:041068 // If the PAC URL is actually a file path, then put file:// in front.
1069 if (pac_url_str[0] == '/')
1070 pac_url_str = "file://" + pac_url_str;
[email protected]861c6c62009-04-20 16:50:561071 GURL pac_url(pac_url_str);
1072 if (!pac_url.is_valid())
Anton Bikineev068d2912021-05-15 20:43:521073 return absl::nullopt;
Ramin Halavatica8d5252018-03-12 05:33:491074 config.set_pac_url(pac_url);
1075 return ProxyConfigWithAnnotation(
1076 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561077 }
1078 }
Ramin Halavatica8d5252018-03-12 05:33:491079 config.set_auto_detect(true);
1080 return ProxyConfigWithAnnotation(
1081 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561082 }
1083
[email protected]3e44697f2009-05-22 14:37:391084 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561085 // Mode is unrecognized.
Anton Bikineev068d2912021-05-15 20:43:521086 return absl::nullopt;
[email protected]861c6c62009-04-20 16:50:561087 }
1088 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501089 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1090 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561091 && !use_http_proxy) {
1092 // Another master switch for some reason. If set to false, then no
1093 // proxy. But we don't panic if the key doesn't exist.
Ramin Halavatica8d5252018-03-12 05:33:491094 return ProxyConfigWithAnnotation(
1095 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561096 }
1097
1098 bool same_proxy = false;
1099 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501100 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561101 // case.
[email protected]573c0502011-05-17 22:19:501102 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1103 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561104
[email protected]76960f3d2011-04-30 02:15:231105 ProxyServer proxy_for_http;
1106 ProxyServer proxy_for_https;
1107 ProxyServer proxy_for_ftp;
1108 ProxyServer socks_proxy; // (socks)
1109
1110 // This counts how many of the above ProxyServers were defined and valid.
1111 size_t num_proxies_specified = 0;
1112
1113 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1114 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501115 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231116 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501117 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231118 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501119 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231120 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501121 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231122 num_proxies_specified++;
1123
1124 if (same_proxy) {
1125 if (proxy_for_http.is_valid()) {
1126 // Use the http proxy for all schemes.
Ramin Halavatica8d5252018-03-12 05:33:491127 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
1128 config.proxy_rules().single_proxies.SetSingleProxyServer(proxy_for_http);
[email protected]861c6c62009-04-20 16:50:561129 }
[email protected]76960f3d2011-04-30 02:15:231130 } else if (num_proxies_specified > 0) {
1131 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1132 // If the only proxy specified was for SOCKS, use it for all schemes.
Ramin Halavatica8d5252018-03-12 05:33:491133 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
1134 config.proxy_rules().single_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561135 } else {
[email protected]2189e092013-03-16 18:02:021136 // Otherwise use the indicated proxies per-scheme.
Ramin Halavatica8d5252018-03-12 05:33:491137 config.proxy_rules().type =
Lily Houghtone6b617e2018-01-19 20:13:071138 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
Ramin Halavatica8d5252018-03-12 05:33:491139 config.proxy_rules().proxies_for_http.SetSingleProxyServer(
1140 proxy_for_http);
1141 config.proxy_rules().proxies_for_https.SetSingleProxyServer(
1142 proxy_for_https);
1143 config.proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_for_ftp);
1144 config.proxy_rules().fallback_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561145 }
1146 }
1147
Ramin Halavatica8d5252018-03-12 05:33:491148 if (config.proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561149 // Manual mode but we couldn't parse any rules.
Anton Bikineev068d2912021-05-15 20:43:521150 return absl::nullopt;
[email protected]861c6c62009-04-20 16:50:561151 }
1152
1153 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431154 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501155 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1156 &use_auth);
[email protected]62749f182009-07-15 13:16:541157 if (use_auth) {
1158 // ProxyConfig does not support authentication parameters, but
1159 // Chrome will prompt for the password later. So we ignore
1160 // /system/http_proxy/*auth* settings.
1161 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1162 }
[email protected]861c6c62009-04-20 16:50:561163
1164 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061165 std::vector<std::string> ignore_hosts_list;
Ramin Halavatica8d5252018-03-12 05:33:491166 config.proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501167 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1168 &ignore_hosts_list)) {
Eric Romanda790f92018-11-07 19:17:151169 for (const auto& rule : ignore_hosts_list) {
Shimi Zhang13eace252020-01-31 01:49:191170 config.proxy_rules().bypass_rules.AddRuleFromString(rule);
[email protected]1a597192010-07-09 16:58:381171 }
[email protected]a8185d02010-06-11 00:19:501172 }
Shimi Zhang13eace252020-01-31 01:49:191173
1174 if (setting_getter_->UseSuffixMatching()) {
1175 RewriteRulesForSuffixMatching(&config.proxy_rules().bypass_rules);
1176 }
1177
[email protected]861c6c62009-04-20 16:50:561178 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381179 // bypass of local names in GNOME. In KDE, "<local>" is supported
1180 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561181
[email protected]a48bf4a2010-06-14 18:24:531182 // KDE allows one to reverse the bypass rules.
Ramin Halavatica8d5252018-03-12 05:33:491183 config.proxy_rules().reverse_bypass = setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531184
Ramin Halavatica8d5252018-03-12 05:33:491185 return ProxyConfigWithAnnotation(
1186 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561187}
1188
thestig0c412e852016-06-30 08:04:401189ProxyConfigServiceLinux::Delegate::Delegate(
Ramin Halavatica8d5252018-03-12 05:33:491190 std::unique_ptr<base::Environment> env_var_getter,
Anton Bikineev068d2912021-05-15 20:43:521191 absl::optional<std::unique_ptr<SettingGetter>> setting_getter,
1192 absl::optional<NetworkTrafficAnnotationTag> traffic_annotation)
Eric Romancd032fb62018-05-18 21:40:131193 : env_var_getter_(std::move(env_var_getter)) {
1194 if (traffic_annotation) {
1195 traffic_annotation_ =
1196 MutableNetworkTrafficAnnotationTag(traffic_annotation.value());
1197 }
1198
1199 if (setting_getter) {
1200 setting_getter_ = std::move(setting_getter.value());
1201 return;
1202 }
1203
[email protected]573c0502011-05-17 22:19:501204 // Figure out which SettingGetterImpl to use, if any.
thestig0c412e852016-06-30 08:04:401205 switch (base::nix::GetDesktopEnvironment(env_var_getter_.get())) {
Tim Brownd9bd4752017-12-14 20:26:341206 case base::nix::DESKTOP_ENVIRONMENT_CINNAMON:
[email protected]6b0349ef2010-10-16 04:56:061207 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
Tom Andersonac4d6f42017-10-13 20:14:201208 case base::nix::DESKTOP_ENVIRONMENT_PANTHEON:
[email protected]9e6c9bde2012-07-17 23:40:171209 case base::nix::DESKTOP_ENVIRONMENT_UNITY:
[email protected]8c20e3d2011-05-19 21:03:571210#if defined(USE_GIO)
1211 {
danakj8a98ca22016-04-16 02:47:361212 std::unique_ptr<SettingGetterImplGSettings> gs_getter(
1213 new SettingGetterImplGSettings());
1214 // We have to load symbols and check the GNOME version in use to decide
Tim Brown1c307cc2017-12-08 02:40:381215 // if we should use the gsettings getter. See CheckVersion().
1216 if (gs_getter->CheckVersion(env_var_getter_.get()))
inlinechan894515af2016-12-09 02:40:101217 setting_getter_ = std::move(gs_getter);
[email protected]8c20e3d2011-05-19 21:03:571218 }
1219#endif
[email protected]d7395e732009-08-28 23:13:431220 break;
[email protected]6b0349ef2010-10-16 04:56:061221 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1222 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
edward.baker53bec302015-10-02 16:57:491223 case base::nix::DESKTOP_ENVIRONMENT_KDE5:
Peter Boström08e7ed82021-04-19 17:49:591224 setting_getter_ =
1225 std::make_unique<SettingGetterImplKDE>(env_var_getter_.get());
[email protected]d7395e732009-08-28 23:13:431226 break;
[email protected]6b0349ef2010-10-16 04:56:061227 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1228 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431229 break;
1230 }
1231}
1232
[email protected]d3066142011-05-10 02:36:201233void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
sergeyu3f923062014-09-05 01:39:401234 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
Ramin Halavatica8d5252018-03-12 05:33:491235 const scoped_refptr<base::SequencedTaskRunner>& main_task_runner,
1236 const NetworkTrafficAnnotationTag& traffic_annotation) {
1237 traffic_annotation_ = MutableNetworkTrafficAnnotationTag(traffic_annotation);
1238
[email protected]3e44697f2009-05-22 14:37:391239 // We should be running on the default glib main loop thread right
Tim Brown2a19f3b2017-12-12 01:08:401240 // now. gsettings can only be accessed from this thread.
eroman0070d412017-06-22 22:18:241241 DCHECK(glib_task_runner->RunsTasksInCurrentSequence());
sergeyu3f923062014-09-05 01:39:401242 glib_task_runner_ = glib_task_runner;
Matt Menke75765062017-11-21 01:21:161243 main_task_runner_ = main_task_runner;
[email protected]3e44697f2009-05-22 14:37:391244
Matt Menke75765062017-11-21 01:21:161245 // If we are passed a NULL |main_task_runner|, then don't set up proxy
eroman0070d412017-06-22 22:18:241246 // setting change notifications. This should not be the usual case but is
1247 // intended to/ simplify test setups.
Matt Menke75765062017-11-21 01:21:161248 if (!main_task_runner_.get())
[email protected]b30a3f52010-10-16 01:05:461249 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391250
1251 // Fetch and cache the current proxy config. The config is left in
Matt Menke75765062017-11-21 01:21:161252 // cached_config_, where GetLatestProxyConfig() running on the main TaskRunner
[email protected]3e44697f2009-05-22 14:37:391253 // will expect to find it. This is safe to do because we return
1254 // before this ProxyConfigServiceLinux is passed on to
Nicolas Arciniegad2013f92020-02-07 23:00:561255 // the ConfiguredProxyResolutionService.
[email protected]d6cb85b2009-07-23 22:10:531256
1257 // Note: It would be nice to prioritize environment variables
Tim Brown2a19f3b2017-12-12 01:08:401258 // and only fall back to gsettings if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531259 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1260 // does so even if the proxy mode is set to auto, which would
1261 // mislead us.
1262
Anton Bikineev068d2912021-05-15 20:43:521263 cached_config_ = absl::nullopt;
Eric Roman750af4b12018-02-22 22:38:531264 if (setting_getter_ && setting_getter_->Init(glib_task_runner)) {
1265 cached_config_ = GetConfigFromSettings();
1266 }
1267 if (cached_config_) {
Ramin Halavatica8d5252018-03-12 05:33:491268 VLOG(1) << "Obtained proxy settings from annotation hash code "
1269 << cached_config_->traffic_annotation().unique_id_hash_code;
[email protected]d3066142011-05-10 02:36:201270
Tim Brown2a19f3b2017-12-12 01:08:401271 // If gsettings proxy mode is "none", meaning direct, then we take
[email protected]d3066142011-05-10 02:36:201272 // that to be a valid config and will not check environment
1273 // variables. The alternative would have been to look for a proxy
Eric Roman750af4b12018-02-22 22:38:531274 // wherever we can find one.
[email protected]d3066142011-05-10 02:36:201275
1276 // Keep a copy of the config for use from this thread for
1277 // comparison with updated settings when we get notifications.
1278 reference_config_ = cached_config_;
[email protected]d3066142011-05-10 02:36:201279
Matt Menke75765062017-11-21 01:21:161280 // We only set up notifications if we have the main and file loops
1281 // available. We do this after getting the initial configuration so that we
1282 // don't have to worry about cancelling it if the initial fetch above fails.
1283 // Note that setting up notifications has the side effect of simulating a
1284 // change, so that we won't lose any updates that may have happened after
1285 // the initial fetch and before setting up notifications. We'll detect the
1286 // common case of no changes in OnCheckProxyConfigSettings() (or sooner) and
1287 // ignore it.
1288 if (main_task_runner.get()) {
eroman0070d412017-06-22 22:18:241289 scoped_refptr<base::SequencedTaskRunner> required_loop =
[email protected]76722472012-05-24 08:26:461290 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241291 if (!required_loop.get() || required_loop->RunsTasksInCurrentSequence()) {
[email protected]d3066142011-05-10 02:36:201292 // In this case we are already on an acceptable thread.
1293 SetUpNotifications();
[email protected]d7395e732009-08-28 23:13:431294 } else {
[email protected]d3066142011-05-10 02:36:201295 // Post a task to set up notifications. We don't wait for success.
kylecharf4fe5172019-02-15 18:53:491296 required_loop->PostTask(
1297 FROM_HERE,
1298 base::BindOnce(
1299 &ProxyConfigServiceLinux::Delegate::SetUpNotifications, this));
[email protected]d6cb85b2009-07-23 22:10:531300 }
[email protected]d7395e732009-08-28 23:13:431301 }
[email protected]861c6c62009-04-20 16:50:561302 }
[email protected]d6cb85b2009-07-23 22:10:531303
Eric Roman750af4b12018-02-22 22:38:531304 if (!cached_config_) {
[email protected]d6cb85b2009-07-23 22:10:531305 // We fall back on environment variables.
[email protected]3e44697f2009-05-22 14:37:391306 //
[email protected]d3066142011-05-10 02:36:201307 // Consulting environment variables doesn't need to be done from the
1308 // default glib main loop, but it's a tiny enough amount of work.
Eric Roman750af4b12018-02-22 22:38:531309 cached_config_ = GetConfigFromEnv();
1310 if (cached_config_) {
[email protected]b30a3f52010-10-16 01:05:461311 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391312 }
[email protected]861c6c62009-04-20 16:50:561313 }
[email protected]3e44697f2009-05-22 14:37:391314}
1315
[email protected]573c0502011-05-17 22:19:501316// Depending on the SettingGetter in use, this method will be called
Tim Brown2a19f3b2017-12-12 01:08:401317// on either the UI thread (GSettings) or the file thread (KDE).
[email protected]d3066142011-05-10 02:36:201318void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
eroman0070d412017-06-22 22:18:241319 scoped_refptr<base::SequencedTaskRunner> required_loop =
[email protected]76722472012-05-24 08:26:461320 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241321 DCHECK(!required_loop.get() || required_loop->RunsTasksInCurrentSequence());
[email protected]573c0502011-05-17 22:19:501322 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201323 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1324}
1325
[email protected]119655002010-07-23 06:02:401326void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1327 observers_.AddObserver(observer);
1328}
1329
1330void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1331 observers_.RemoveObserver(observer);
1332}
1333
[email protected]3a29593d2011-04-11 10:07:521334ProxyConfigService::ConfigAvailability
Ramin Halavatica8d5252018-03-12 05:33:491335ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1336 ProxyConfigWithAnnotation* config) {
Matt Menke75765062017-11-21 01:21:161337 // This is called from the main TaskRunner.
1338 DCHECK(!main_task_runner_.get() ||
1339 main_task_runner_->RunsTasksInCurrentSequence());
[email protected]3e44697f2009-05-22 14:37:391340
1341 // Simply return the last proxy configuration that glib_default_loop
1342 // notified us of.
Eric Roman750af4b12018-02-22 22:38:531343 *config = GetConfigOrDirect(cached_config_);
[email protected]119655002010-07-23 06:02:401344
[email protected]3a29593d2011-04-11 10:07:521345 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401346 // going to be available since we initialized eagerly on the UI thread.
1347 // TODO(eroman): do lazy initialization instead, so we no longer need
1348 // to construct ProxyConfigServiceLinux on the UI thread.
1349 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521350 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391351}
1352
[email protected]573c0502011-05-17 22:19:501353// Depending on the SettingGetter in use, this method will be called
Tim Brown2a19f3b2017-12-12 01:08:401354// on either the UI thread (GSettings) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391355void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
eroman0070d412017-06-22 22:18:241356 scoped_refptr<base::SequencedTaskRunner> required_loop =
[email protected]76722472012-05-24 08:26:461357 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241358 DCHECK(!required_loop.get() || required_loop->RunsTasksInCurrentSequence());
Anton Bikineev068d2912021-05-15 20:43:521359 absl::optional<ProxyConfigWithAnnotation> new_config =
Ramin Halavatica8d5252018-03-12 05:33:491360 GetConfigFromSettings();
[email protected]3e44697f2009-05-22 14:37:391361
[email protected]119655002010-07-23 06:02:401362 // See if it is different from what we had before.
Eric Roman750af4b12018-02-22 22:38:531363 if (new_config.has_value() != reference_config_.has_value() ||
Eric Roman3e185842018-06-01 18:10:521364 (new_config.has_value() &&
1365 !new_config->value().Equals(reference_config_->value()))) {
Matt Menke75765062017-11-21 01:21:161366 // Post a task to the main TaskRunner with the new configuration, so it can
[email protected]3e44697f2009-05-22 14:37:391367 // update |cached_config_|.
Matt Menke75765062017-11-21 01:21:161368 main_task_runner_->PostTask(
1369 FROM_HERE,
kylecharf4fe5172019-02-15 18:53:491370 base::BindOnce(&ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1371 this, new_config));
[email protected]d1f9d472009-08-13 19:59:301372 // Update the thread-private copy in |reference_config_| as well.
1373 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201374 } else {
1375 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391376 }
1377}
1378
1379void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
Anton Bikineev068d2912021-05-15 20:43:521380 const absl::optional<ProxyConfigWithAnnotation>& new_config) {
Matt Menke75765062017-11-21 01:21:161381 DCHECK(main_task_runner_->RunsTasksInCurrentSequence());
[email protected]b30a3f52010-10-16 01:05:461382 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391383 cached_config_ = new_config;
Eric Roman750af4b12018-02-22 22:38:531384 for (auto& observer : observers_) {
1385 observer.OnProxyConfigChanged(GetConfigOrDirect(new_config),
1386 ProxyConfigService::CONFIG_VALID);
1387 }
[email protected]3e44697f2009-05-22 14:37:391388}
1389
1390void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
thestig0c412e852016-06-30 08:04:401391 if (!setting_getter_)
[email protected]d7395e732009-08-28 23:13:431392 return;
thestig0c412e852016-06-30 08:04:401393
eroman0070d412017-06-22 22:18:241394 scoped_refptr<base::SequencedTaskRunner> shutdown_loop =
[email protected]76722472012-05-24 08:26:461395 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241396 if (!shutdown_loop.get() || shutdown_loop->RunsTasksInCurrentSequence()) {
[email protected]3e44697f2009-05-22 14:37:391397 // Already on the right thread, call directly.
1398 // This is the case for the unittests.
1399 OnDestroy();
1400 } else {
[email protected]d7395e732009-08-28 23:13:431401 // Post to shutdown thread. Note that on browser shutdown, we may quit
1402 // this MessageLoop and exit the program before ever running this.
kylecharf4fe5172019-02-15 18:53:491403 shutdown_loop->PostTask(
1404 FROM_HERE,
1405 base::BindOnce(&ProxyConfigServiceLinux::Delegate::OnDestroy, this));
[email protected]3e44697f2009-05-22 14:37:391406 }
1407}
1408void ProxyConfigServiceLinux::Delegate::OnDestroy() {
eroman0070d412017-06-22 22:18:241409 scoped_refptr<base::SequencedTaskRunner> shutdown_loop =
[email protected]76722472012-05-24 08:26:461410 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241411 DCHECK(!shutdown_loop.get() || shutdown_loop->RunsTasksInCurrentSequence());
[email protected]573c0502011-05-17 22:19:501412 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391413}
1414
1415ProxyConfigServiceLinux::ProxyConfigServiceLinux()
Eric Romancd032fb62018-05-18 21:40:131416 : delegate_(new Delegate(base::Environment::Create(),
Anton Bikineev068d2912021-05-15 20:43:521417 absl::nullopt,
1418 absl::nullopt)) {}
[email protected]3e44697f2009-05-22 14:37:391419
[email protected]8e1845e12010-09-15 19:22:241420ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1421 delegate_->PostDestroyTask();
1422}
1423
[email protected]3e44697f2009-05-22 14:37:391424ProxyConfigServiceLinux::ProxyConfigServiceLinux(
Ramin Halavatica8d5252018-03-12 05:33:491425 std::unique_ptr<base::Environment> env_var_getter,
1426 const NetworkTrafficAnnotationTag& traffic_annotation)
Eric Romancd032fb62018-05-18 21:40:131427 : delegate_(new Delegate(std::move(env_var_getter),
Anton Bikineev068d2912021-05-15 20:43:521428 absl::nullopt,
Eric Romancd032fb62018-05-18 21:40:131429 traffic_annotation)) {}
[email protected]9a3d8d42009-09-03 17:01:461430
1431ProxyConfigServiceLinux::ProxyConfigServiceLinux(
thestig0c412e852016-06-30 08:04:401432 std::unique_ptr<base::Environment> env_var_getter,
Ramin Halavatica8d5252018-03-12 05:33:491433 SettingGetter* setting_getter,
1434 const NetworkTrafficAnnotationTag& traffic_annotation)
1435 : delegate_(new Delegate(std::move(env_var_getter),
Eric Romancd032fb62018-05-18 21:40:131436 base::WrapUnique(setting_getter),
Ramin Halavatica8d5252018-03-12 05:33:491437 traffic_annotation)) {}
[email protected]861c6c62009-04-20 16:50:561438
[email protected]e4be2dd2010-12-14 00:44:391439void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1440 delegate_->AddObserver(observer);
1441}
1442
1443void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1444 delegate_->RemoveObserver(observer);
1445}
1446
[email protected]3a29593d2011-04-11 10:07:521447ProxyConfigService::ConfigAvailability
Ramin Halavatica8d5252018-03-12 05:33:491448ProxyConfigServiceLinux::GetLatestProxyConfig(
1449 ProxyConfigWithAnnotation* config) {
[email protected]e4be2dd2010-12-14 00:44:391450 return delegate_->GetLatestProxyConfig(config);
1451}
1452
[email protected]861c6c62009-04-20 16:50:561453} // namespace net