Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: chrome/browser/chromeos/net/onc_utils.cc

Issue 2442313003: Move some proxy config code out of src/chrome (Closed)
Patch Set: Fix DEPS Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/net/onc_utils.h"
6
7 #include "base/bind_helpers.h"
8 #include "base/json/json_writer.h"
9 #include "base/logging.h"
10 #include "base/macros.h"
11 #include "base/values.h"
12 #include "chrome/common/pref_names.h"
13 #include "chromeos/network/managed_network_configuration_handler.h"
14 #include "chromeos/network/network_configuration_handler.h"
15 #include "chromeos/network/network_handler.h"
16 #include "chromeos/network/network_profile.h"
17 #include "chromeos/network/network_profile_handler.h"
18 #include "chromeos/network/network_state.h"
19 #include "chromeos/network/network_state_handler.h"
20 #include "chromeos/network/network_ui_data.h"
21 #include "chromeos/network/onc/onc_normalizer.h"
22 #include "chromeos/network/onc/onc_signature.h"
23 #include "chromeos/network/onc/onc_translator.h"
24 #include "chromeos/network/onc/onc_utils.h"
25 #include "components/onc/onc_pref_names.h"
26 #include "components/prefs/pref_service.h"
27 #include "components/user_manager/user.h"
28 #include "components/user_manager/user_manager.h"
29 #include "third_party/cros_system_api/dbus/service_constants.h"
30 #include "url/gurl.h"
31
32 namespace chromeos {
33 namespace onc {
34
35 namespace {
36
37 // This class defines which string placeholders of ONC are replaced by which
38 // user attribute.
39 class UserStringSubstitution : public chromeos::onc::StringSubstitution {
40 public:
41 explicit UserStringSubstitution(const user_manager::User* user)
42 : user_(user) {}
43 ~UserStringSubstitution() override {}
44
45 bool GetSubstitute(const std::string& placeholder,
46 std::string* substitute) const override {
47 if (placeholder == ::onc::substitutes::kLoginIDField)
48 *substitute = user_->GetAccountName(false);
49 else if (placeholder == ::onc::substitutes::kEmailField)
50 *substitute = user_->email();
51 else
52 return false;
53 return true;
54 }
55
56 private:
57 const user_manager::User* user_;
58
59 DISALLOW_COPY_AND_ASSIGN(UserStringSubstitution);
60 };
61
62 } // namespace
63
64 void ExpandStringPlaceholdersInNetworksForUser(
65 const user_manager::User* user,
66 base::ListValue* network_configs) {
67 if (!user) {
68 // In tests no user may be logged in. It's not harmful if we just don't
69 // expand the strings.
70 return;
71 }
72 UserStringSubstitution substitution(user);
73 chromeos::onc::ExpandStringsInNetworks(substitution, network_configs);
74 }
75
76 void ImportNetworksForUser(const user_manager::User* user,
77 const base::ListValue& network_configs,
78 std::string* error) {
79 error->clear();
80
81 std::unique_ptr<base::ListValue> expanded_networks(
82 network_configs.DeepCopy());
83 ExpandStringPlaceholdersInNetworksForUser(user, expanded_networks.get());
84
85 const NetworkProfile* profile =
86 NetworkHandler::Get()->network_profile_handler()->GetProfileForUserhash(
87 user->username_hash());
88 if (!profile) {
89 *error = "User profile doesn't exist.";
90 return;
91 }
92
93 bool ethernet_not_found = false;
94 for (base::ListValue::const_iterator it = expanded_networks->begin();
95 it != expanded_networks->end();
96 ++it) {
97 const base::DictionaryValue* network = NULL;
98 (*it)->GetAsDictionary(&network);
99 DCHECK(network);
100
101 // Remove irrelevant fields.
102 onc::Normalizer normalizer(true /* remove recommended fields */);
103 std::unique_ptr<base::DictionaryValue> normalized_network =
104 normalizer.NormalizeObject(&onc::kNetworkConfigurationSignature,
105 *network);
106
107 // TODO(pneubeck): Use ONC and ManagedNetworkConfigurationHandler instead.
108 // crbug.com/457936
109 std::unique_ptr<base::DictionaryValue> shill_dict =
110 onc::TranslateONCObjectToShill(&onc::kNetworkConfigurationSignature,
111 *normalized_network);
112
113 std::unique_ptr<NetworkUIData> ui_data(
114 NetworkUIData::CreateFromONC(::onc::ONC_SOURCE_USER_IMPORT));
115 base::DictionaryValue ui_data_dict;
116 ui_data->FillDictionary(&ui_data_dict);
117 std::string ui_data_json;
118 base::JSONWriter::Write(ui_data_dict, &ui_data_json);
119 shill_dict->SetStringWithoutPathExpansion(shill::kUIDataProperty,
120 ui_data_json);
121
122 shill_dict->SetStringWithoutPathExpansion(shill::kProfileProperty,
123 profile->path);
124
125 std::string type;
126 shill_dict->GetStringWithoutPathExpansion(shill::kTypeProperty, &type);
127 NetworkConfigurationHandler* config_handler =
128 NetworkHandler::Get()->network_configuration_handler();
129 if (NetworkTypePattern::Ethernet().MatchesType(type)) {
130 // Ethernet has to be configured using an existing Ethernet service.
131 const NetworkState* ethernet =
132 NetworkHandler::Get()->network_state_handler()->FirstNetworkByType(
133 NetworkTypePattern::Ethernet());
134 if (ethernet) {
135 config_handler->SetShillProperties(
136 ethernet->path(), *shill_dict,
137 NetworkConfigurationObserver::SOURCE_USER_ACTION, base::Closure(),
138 network_handler::ErrorCallback());
139 } else {
140 ethernet_not_found = true;
141 }
142
143 } else {
144 config_handler->CreateShillConfiguration(
145 *shill_dict, NetworkConfigurationObserver::SOURCE_USER_ACTION,
146 network_handler::ServiceResultCallback(),
147 network_handler::ErrorCallback());
148 }
149 }
150
151 if (ethernet_not_found)
152 *error = "No Ethernet available to configure.";
153 }
154
155 const base::DictionaryValue* FindPolicyForActiveUser(
156 const std::string& guid,
157 ::onc::ONCSource* onc_source) {
158 const user_manager::User* user =
159 user_manager::UserManager::Get()->GetActiveUser();
160 std::string username_hash = user ? user->username_hash() : std::string();
161 return NetworkHandler::Get()->managed_network_configuration_handler()->
162 FindPolicyByGUID(username_hash, guid, onc_source);
163 }
164
165 const base::DictionaryValue* GetGlobalConfigFromPolicy(bool for_active_user) {
166 std::string username_hash;
167 if (for_active_user) {
168 const user_manager::User* user =
169 user_manager::UserManager::Get()->GetActiveUser();
170 if (!user) {
171 LOG(ERROR) << "No user logged in yet.";
172 return NULL;
173 }
174 username_hash = user->username_hash();
175 }
176 return NetworkHandler::Get()->managed_network_configuration_handler()->
177 GetGlobalConfigFromPolicy(username_hash);
178 }
179
180 bool PolicyAllowsOnlyPolicyNetworksToAutoconnect(bool for_active_user) {
181 const base::DictionaryValue* global_config =
182 GetGlobalConfigFromPolicy(for_active_user);
183 if (!global_config)
184 return false; // By default, all networks are allowed to autoconnect.
185
186 bool only_policy_autoconnect = false;
187 global_config->GetBooleanWithoutPathExpansion(
188 ::onc::global_network_config::kAllowOnlyPolicyNetworksToAutoconnect,
189 &only_policy_autoconnect);
190 return only_policy_autoconnect;
191 }
192
193 namespace {
194
195 const base::DictionaryValue* GetNetworkConfigByGUID(
196 const base::ListValue& network_configs,
197 const std::string& guid) {
198 for (base::ListValue::const_iterator it = network_configs.begin();
199 it != network_configs.end(); ++it) {
200 const base::DictionaryValue* network = NULL;
201 (*it)->GetAsDictionary(&network);
202 DCHECK(network);
203
204 std::string current_guid;
205 network->GetStringWithoutPathExpansion(::onc::network_config::kGUID,
206 &current_guid);
207 if (current_guid == guid)
208 return network;
209 }
210 return NULL;
211 }
212
213 const base::DictionaryValue* GetNetworkConfigForEthernetWithoutEAP(
214 const base::ListValue& network_configs) {
215 VLOG(2) << "Search for ethernet policy without EAP.";
216 for (base::ListValue::const_iterator it = network_configs.begin();
217 it != network_configs.end(); ++it) {
218 const base::DictionaryValue* network = NULL;
219 (*it)->GetAsDictionary(&network);
220 DCHECK(network);
221
222 std::string type;
223 network->GetStringWithoutPathExpansion(::onc::network_config::kType, &type);
224 if (type != ::onc::network_type::kEthernet)
225 continue;
226
227 const base::DictionaryValue* ethernet = NULL;
228 network->GetDictionaryWithoutPathExpansion(::onc::network_config::kEthernet,
229 &ethernet);
230
231 std::string auth;
232 ethernet->GetStringWithoutPathExpansion(::onc::ethernet::kAuthentication,
233 &auth);
234 if (auth == ::onc::ethernet::kAuthenticationNone)
235 return network;
236 }
237 return NULL;
238 }
239
240 const base::DictionaryValue* GetNetworkConfigForNetworkFromOnc(
241 const base::ListValue& network_configs,
242 const NetworkState& network) {
243 // In all cases except Ethernet, we use the GUID of |network|.
244 if (!network.Matches(NetworkTypePattern::Ethernet()))
245 return GetNetworkConfigByGUID(network_configs, network.guid());
246
247 // Ethernet is always shared and thus cannot store a GUID per user. Thus we
248 // search for any Ethernet policy intead of a matching GUID.
249 // EthernetEAP service contains only the EAP parameters and stores the GUID of
250 // the respective ONC policy. The EthernetEAP service itself is however never
251 // in state "connected". An EthernetEAP policy must be applied, if an Ethernet
252 // service is connected using the EAP parameters.
253 const NetworkState* ethernet_eap = NULL;
254 if (NetworkHandler::IsInitialized()) {
255 ethernet_eap =
256 NetworkHandler::Get()->network_state_handler()->GetEAPForEthernet(
257 network.path());
258 }
259
260 // The GUID associated with the EthernetEAP service refers to the ONC policy
261 // with "Authentication: 8021X".
262 if (ethernet_eap)
263 return GetNetworkConfigByGUID(network_configs, ethernet_eap->guid());
264
265 // Otherwise, EAP is not used and instead the Ethernet policy with
266 // "Authentication: None" applies.
267 return GetNetworkConfigForEthernetWithoutEAP(network_configs);
268 }
269
270 const base::DictionaryValue* GetPolicyForNetworkFromPref(
271 const PrefService* pref_service,
272 const char* pref_name,
273 const NetworkState& network) {
274 if (!pref_service) {
275 VLOG(2) << "No pref service";
276 return NULL;
277 }
278
279 const PrefService::Preference* preference =
280 pref_service->FindPreference(pref_name);
281 if (!preference) {
282 VLOG(2) << "No preference " << pref_name;
283 // The preference may not exist in tests.
284 return NULL;
285 }
286
287 // User prefs are not stored in this Preference yet but only the policy.
288 //
289 // The policy server incorrectly configures the OpenNetworkConfiguration user
290 // policy as Recommended. To work around that, we handle the Recommended and
291 // the Mandatory value in the same way.
292 // TODO(pneubeck): Remove this workaround, once the server is fixed. See
293 // http://crbug.com/280553 .
294 if (preference->IsDefaultValue()) {
295 VLOG(2) << "Preference has no recommended or mandatory value.";
296 // No policy set.
297 return NULL;
298 }
299 VLOG(2) << "Preference with policy found.";
300 const base::Value* onc_policy_value = preference->GetValue();
301 DCHECK(onc_policy_value);
302
303 const base::ListValue* onc_policy = NULL;
304 onc_policy_value->GetAsList(&onc_policy);
305 DCHECK(onc_policy);
306
307 return GetNetworkConfigForNetworkFromOnc(*onc_policy, network);
308 }
309
310 } // namespace
311
312 const base::DictionaryValue* GetPolicyForNetwork(
313 const PrefService* profile_prefs,
314 const PrefService* local_state_prefs,
315 const NetworkState& network,
316 ::onc::ONCSource* onc_source) {
317 VLOG(2) << "GetPolicyForNetwork: " << network.path();
318 *onc_source = ::onc::ONC_SOURCE_NONE;
319
320 const base::DictionaryValue* network_policy = GetPolicyForNetworkFromPref(
321 profile_prefs, ::onc::prefs::kOpenNetworkConfiguration, network);
322 if (network_policy) {
323 VLOG(1) << "Network " << network.path() << " is managed by user policy.";
324 *onc_source = ::onc::ONC_SOURCE_USER_POLICY;
325 return network_policy;
326 }
327 network_policy = GetPolicyForNetworkFromPref(
328 local_state_prefs, ::onc::prefs::kDeviceOpenNetworkConfiguration,
329 network);
330 if (network_policy) {
331 VLOG(1) << "Network " << network.path() << " is managed by device policy.";
332 *onc_source = ::onc::ONC_SOURCE_DEVICE_POLICY;
333 return network_policy;
334 }
335 VLOG(2) << "Network " << network.path() << " is unmanaged.";
336 return NULL;
337 }
338
339 bool HasPolicyForNetwork(const PrefService* profile_prefs,
340 const PrefService* local_state_prefs,
341 const NetworkState& network) {
342 ::onc::ONCSource ignored_onc_source;
343 const base::DictionaryValue* policy = onc::GetPolicyForNetwork(
344 profile_prefs, local_state_prefs, network, &ignored_onc_source);
345 return policy != NULL;
346 }
347
348 } // namespace onc
349 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/net/onc_utils.h ('k') | chrome/browser/chromeos/net/proxy_config_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698