OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/policy/network_configuration_updater.h" | 5 #include "chrome/browser/chromeos/policy/network_configuration_updater.h" |
6 | 6 |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/command_line.h" | |
12 #include "base/logging.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "chrome/browser/chromeos/cros/network_library.h" | |
15 #include "chrome/browser/policy/policy_map.h" | |
16 #include "chrome/common/chrome_switches.h" | |
17 #include "chromeos/network/onc/onc_constants.h" | |
18 #include "chromeos/network/onc/onc_utils.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 #include "net/cert/cert_trust_anchor_provider.h" | |
21 #include "net/cert/x509_certificate.h" | |
22 #include "policy/policy_constants.h" | |
23 | |
24 using content::BrowserThread; | |
25 | |
26 namespace policy { | 7 namespace policy { |
27 | 8 |
28 namespace { | 9 NetworkConfigurationUpdater::NetworkConfigurationUpdater() { |
29 | |
30 // A simple implementation of net::CertTrustAnchorProvider that returns a list | |
31 // of certificates that can be set by the owner of this object. | |
32 class CrosTrustAnchorProvider : public net::CertTrustAnchorProvider { | |
33 public: | |
34 CrosTrustAnchorProvider() {} | |
35 virtual ~CrosTrustAnchorProvider() {} | |
36 | |
37 // CertTrustAnchorProvider overrides. | |
38 virtual const net::CertificateList& GetAdditionalTrustAnchors() OVERRIDE { | |
39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
40 return trust_anchors_; | |
41 } | |
42 | |
43 void SetTrustAnchors(scoped_ptr<net::CertificateList> trust_anchors) { | |
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
45 trust_anchors_.swap(*trust_anchors); | |
46 } | |
47 | |
48 private: | |
49 net::CertificateList trust_anchors_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(CrosTrustAnchorProvider); | |
52 }; | |
53 | |
54 } // namespace | |
55 | |
56 NetworkConfigurationUpdater::NetworkConfigurationUpdater( | |
57 PolicyService* policy_service, | |
58 chromeos::NetworkLibrary* network_library) | |
59 : policy_change_registrar_( | |
60 policy_service, PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())), | |
61 network_library_(network_library), | |
62 user_policy_initialized_(false), | |
63 allow_trusted_certificates_from_policy_(false), | |
64 policy_service_(policy_service), | |
65 cert_trust_provider_(new CrosTrustAnchorProvider()) { | |
66 DCHECK(network_library_); | |
67 policy_change_registrar_.Observe( | |
68 key::kDeviceOpenNetworkConfiguration, | |
69 base::Bind(&NetworkConfigurationUpdater::OnPolicyChanged, | |
70 base::Unretained(this), | |
71 chromeos::onc::ONC_SOURCE_DEVICE_POLICY)); | |
72 policy_change_registrar_.Observe( | |
73 key::kOpenNetworkConfiguration, | |
74 base::Bind(&NetworkConfigurationUpdater::OnPolicyChanged, | |
75 base::Unretained(this), | |
76 chromeos::onc::ONC_SOURCE_USER_POLICY)); | |
77 | |
78 network_library_->AddNetworkProfileObserver(this); | |
79 | |
80 // Apply the current policies immediately. | |
81 ApplyNetworkConfigurations(); | |
82 } | 10 } |
83 | 11 |
84 NetworkConfigurationUpdater::~NetworkConfigurationUpdater() { | 12 NetworkConfigurationUpdater::~NetworkConfigurationUpdater() { |
85 network_library_->RemoveNetworkProfileObserver(this); | |
86 bool posted = BrowserThread::DeleteSoon( | |
87 BrowserThread::IO, FROM_HERE, cert_trust_provider_); | |
88 if (!posted) | |
89 delete cert_trust_provider_; | |
90 } | |
91 | |
92 void NetworkConfigurationUpdater::OnProfileListChanged() { | |
93 VLOG(1) << "Network profile list changed, applying policies."; | |
94 ApplyNetworkConfigurations(); | |
95 } | |
96 | |
97 void NetworkConfigurationUpdater::OnUserPolicyInitialized() { | |
98 VLOG(1) << "User policy initialized, applying policies."; | |
99 user_policy_initialized_ = true; | |
100 ApplyNetworkConfigurations(); | |
101 } | |
102 | |
103 net::CertTrustAnchorProvider* | |
104 NetworkConfigurationUpdater::GetCertTrustAnchorProvider() { | |
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
106 return cert_trust_provider_; | |
107 } | |
108 | |
109 void NetworkConfigurationUpdater::OnPolicyChanged( | |
110 chromeos::onc::ONCSource onc_source, | |
111 const base::Value* previous, | |
112 const base::Value* current) { | |
113 VLOG(1) << "Policy for ONC source " | |
114 << chromeos::onc::GetSourceAsString(onc_source) << " changed."; | |
115 ApplyNetworkConfigurations(); | |
116 } | |
117 | |
118 void NetworkConfigurationUpdater::ApplyNetworkConfigurations() { | |
119 ApplyNetworkConfiguration(key::kDeviceOpenNetworkConfiguration, | |
120 chromeos::onc::ONC_SOURCE_DEVICE_POLICY); | |
121 if (user_policy_initialized_) { | |
122 ApplyNetworkConfiguration(key::kOpenNetworkConfiguration, | |
123 chromeos::onc::ONC_SOURCE_USER_POLICY); | |
124 } | |
125 } | |
126 | |
127 void NetworkConfigurationUpdater::ApplyNetworkConfiguration( | |
128 const std::string& policy_key, | |
129 chromeos::onc::ONCSource onc_source) { | |
130 VLOG(1) << "Apply policy for ONC source " | |
131 << chromeos::onc::GetSourceAsString(onc_source); | |
132 const PolicyMap& policies = policy_service_->GetPolicies( | |
133 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); | |
134 const base::Value* policy_value = policies.GetValue(policy_key); | |
135 | |
136 std::string new_network_config; | |
137 if (policy_value != NULL) { | |
138 // If the policy is not a string, we issue a warning, but still clear the | |
139 // network configuration. | |
140 if (!policy_value->GetAsString(&new_network_config)) { | |
141 LOG(WARNING) << "ONC policy for source " | |
142 << chromeos::onc::GetSourceAsString(onc_source) | |
143 << " is not a string value."; | |
144 } | |
145 } | |
146 | |
147 // An empty string is not a valid ONC and generates warnings and | |
148 // errors. Replace by a valid empty configuration. | |
149 if (new_network_config.empty()) | |
150 new_network_config = chromeos::onc::kEmptyUnencryptedConfiguration; | |
151 | |
152 scoped_ptr<net::CertificateList> web_trust_certs(new net::CertificateList()); | |
153 if (!network_library_->LoadOncNetworks(new_network_config, "", onc_source, | |
154 web_trust_certs.get())) { | |
155 LOG(ERROR) << "Errors occurred during the ONC policy application."; | |
156 } | |
157 | |
158 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
159 if (onc_source == chromeos::onc::ONC_SOURCE_USER_POLICY && | |
160 allow_trusted_certificates_from_policy_ && | |
161 command_line->HasSwitch(switches::kEnableWebTrustCerts)) { | |
162 BrowserThread::PostTask( | |
163 BrowserThread::IO, FROM_HERE, | |
164 base::Bind(&CrosTrustAnchorProvider::SetTrustAnchors, | |
165 base::Unretained(static_cast<CrosTrustAnchorProvider*>( | |
166 cert_trust_provider_)), | |
167 base::Passed(&web_trust_certs))); | |
168 } | |
169 } | 13 } |
170 | 14 |
171 } // namespace policy | 15 } // namespace policy |
OLD | NEW |