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

Side by Side Diff: chromeos/network/policy_util.cc

Issue 23522004: Separate PolicyApplicator from ManagedNetworkConfigurationHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix clang compilation. Created 7 years, 3 months 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 | Annotate | Revision Log
« no previous file with comments | « chromeos/network/policy_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 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 "chromeos/network/policy_util.h"
6
7 #include "base/logging.h"
8 #include "chromeos/network/network_profile.h"
9 #include "chromeos/network/network_ui_data.h"
10 #include "chromeos/network/onc/onc_constants.h"
11 #include "chromeos/network/onc/onc_merger.h"
12 #include "chromeos/network/onc/onc_normalizer.h"
13 #include "chromeos/network/onc/onc_signature.h"
14 #include "chromeos/network/onc/onc_translator.h"
15 #include "chromeos/network/onc/onc_utils.h"
16 #include "chromeos/network/shill_property_util.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
18
19 namespace chromeos {
20
21 namespace policy_util {
22
23 namespace {
24
25 // This fake credential contains a random postfix which is extremly unlikely to
26 // be used by any user.
27 const char kFakeCredential[] = "FAKE_CREDENTIAL_VPaJDV9x";
28
29
30 // Removes all kFakeCredential values from sensitive fields (determined by
31 // onc::FieldIsCredential) of |onc_object|.
32 void RemoveFakeCredentials(
33 const onc::OncValueSignature& signature,
34 base::DictionaryValue* onc_object) {
35 base::DictionaryValue::Iterator it(*onc_object);
36 while (!it.IsAtEnd()) {
37 base::Value* value = NULL;
38 std::string field_name = it.key();
39 // We need the non-const entry to remove nested values but DictionaryValue
40 // has no non-const iterator.
41 onc_object->GetWithoutPathExpansion(field_name, &value);
42 // Advance before delete.
43 it.Advance();
44
45 // If |value| is a dictionary, recurse.
46 base::DictionaryValue* nested_object = NULL;
47 if (value->GetAsDictionary(&nested_object)) {
48 const onc::OncFieldSignature* field_signature =
49 onc::GetFieldSignature(signature, field_name);
50
51 RemoveFakeCredentials(*field_signature->value_signature,
52 nested_object);
53 continue;
54 }
55
56 // If |value| is a string, check if it is a fake credential.
57 std::string string_value;
58 if (value->GetAsString(&string_value) &&
59 onc::FieldIsCredential(signature, field_name)) {
60 if (string_value == kFakeCredential) {
61 // The value wasn't modified by the UI, thus we remove the field to keep
62 // the existing value that is stored in Shill.
63 onc_object->RemoveWithoutPathExpansion(field_name, NULL);
64 }
65 // Otherwise, the value is set and modified by the UI, thus we keep that
66 // value to overwrite whatever is stored in Shill.
67 }
68 }
69 }
70
71 // Returns true if |policy| matches |actual_network|, which must be part of a
72 // ONC NetworkConfiguration. This should be the only such matching function
73 // within Chrome. Shill does such matching in several functions for network
74 // identification. For compatibility, we currently should stick to Shill's
75 // matching behavior.
76 bool IsPolicyMatching(const base::DictionaryValue& policy,
77 const base::DictionaryValue& actual_network) {
78 std::string policy_type;
79 policy.GetStringWithoutPathExpansion(onc::network_config::kType,
80 &policy_type);
81 std::string network_type;
82 actual_network.GetStringWithoutPathExpansion(onc::network_config::kType,
83 &network_type);
84 if (policy_type != network_type)
85 return false;
86
87 if (network_type != onc::network_type::kWiFi)
88 return false;
89
90 const base::DictionaryValue* policy_wifi = NULL;
91 policy.GetDictionaryWithoutPathExpansion(onc::network_config::kWiFi,
92 &policy_wifi);
93 const base::DictionaryValue* actual_wifi = NULL;
94 actual_network.GetDictionaryWithoutPathExpansion(onc::network_config::kWiFi,
95 &actual_wifi);
96 if (!policy_wifi || !actual_wifi)
97 return false;
98
99 std::string policy_ssid;
100 policy_wifi->GetStringWithoutPathExpansion(onc::wifi::kSSID, &policy_ssid);
101 std::string actual_ssid;
102 actual_wifi->GetStringWithoutPathExpansion(onc::wifi::kSSID, &actual_ssid);
103 return (policy_ssid == actual_ssid);
104 }
105
106 } // namespace
107
108 scoped_ptr<base::DictionaryValue> CreateShillConfiguration(
109 const NetworkProfile& profile,
110 const std::string& guid,
111 const base::DictionaryValue* policy,
112 const base::DictionaryValue* settings) {
113 scoped_ptr<base::DictionaryValue> effective;
114 onc::ONCSource onc_source = onc::ONC_SOURCE_NONE;
115 if (policy) {
116 if (profile.type() == NetworkProfile::TYPE_SHARED) {
117 effective = onc::MergeSettingsAndPoliciesToEffective(
118 NULL, // no user policy
119 policy, // device policy
120 NULL, // no user settings
121 settings); // shared settings
122 onc_source = onc::ONC_SOURCE_DEVICE_POLICY;
123 } else if (profile.type() == NetworkProfile::TYPE_USER) {
124 effective = onc::MergeSettingsAndPoliciesToEffective(
125 policy, // user policy
126 NULL, // no device policy
127 settings, // user settings
128 NULL); // no shared settings
129 onc_source = onc::ONC_SOURCE_USER_POLICY;
130 } else {
131 NOTREACHED();
132 }
133 } else if (settings) {
134 effective.reset(settings->DeepCopy());
135 // TODO(pneubeck): change to source ONC_SOURCE_USER
136 onc_source = onc::ONC_SOURCE_NONE;
137 } else {
138 NOTREACHED();
139 onc_source = onc::ONC_SOURCE_NONE;
140 }
141
142 RemoveFakeCredentials(onc::kNetworkConfigurationSignature,
143 effective.get());
144
145 effective->SetStringWithoutPathExpansion(onc::network_config::kGUID, guid);
146
147 // Remove irrelevant fields.
148 onc::Normalizer normalizer(true /* remove recommended fields */);
149 effective = normalizer.NormalizeObject(&onc::kNetworkConfigurationSignature,
150 *effective);
151
152 scoped_ptr<base::DictionaryValue> shill_dictionary(
153 onc::TranslateONCObjectToShill(&onc::kNetworkConfigurationSignature,
154 *effective));
155
156 shill_dictionary->SetStringWithoutPathExpansion(flimflam::kProfileProperty,
157 profile.path);
158
159 scoped_ptr<NetworkUIData> ui_data;
160 if (policy)
161 ui_data = NetworkUIData::CreateFromONC(onc_source, *policy);
162 else
163 ui_data.reset(new NetworkUIData());
164
165 if (settings) {
166 // Shill doesn't know that sensitive data is contained in the UIData
167 // property and might write it into logs or other insecure places. Thus, we
168 // have to remove or mask credentials.
169 //
170 // Shill's GetProperties doesn't return credentials. Masking credentials
171 // instead of just removing them, allows remembering if a credential is set
172 // or not.
173 scoped_ptr<base::DictionaryValue> sanitized_settings(
174 onc::MaskCredentialsInOncObject(onc::kNetworkConfigurationSignature,
175 *settings,
176 kFakeCredential));
177 ui_data->set_user_settings(sanitized_settings.Pass());
178 }
179
180 shill_property_util::SetUIData(*ui_data, shill_dictionary.get());
181
182 VLOG(2) << "Created Shill properties: " << *shill_dictionary;
183
184 return shill_dictionary.Pass();
185 }
186
187 const base::DictionaryValue* FindMatchingPolicy(
188 const GuidToPolicyMap& policies,
189 const base::DictionaryValue& actual_network) {
190 for (GuidToPolicyMap::const_iterator it = policies.begin();
191 it != policies.end(); ++it) {
192 if (IsPolicyMatching(*it->second, actual_network))
193 return it->second;
194 }
195 return NULL;
196 }
197
198 } // namespace policy_util
199
200 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/policy_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698