File: rollback_onc_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (309 lines) | stat: -rw-r--r-- 10,444 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/net/rollback_network_config/rollback_onc_util.h"

#include "base/check.h"
#include "base/notreached.h"
#include "base/values.h"

#include "components/onc/onc_constants.h"

namespace ash::rollback_network_config {

namespace {

static const char* const kAugmentationKeys[] = {
    onc::kAugmentationActiveSetting,  onc::kAugmentationEffectiveSetting,
    onc::kAugmentationUserPolicy,     onc::kAugmentationDevicePolicy,
    onc::kAugmentationUserSetting,    onc::kAugmentationSharedSetting,
    onc::kAugmentationUserEditable,   onc::kAugmentationDeviceEditable,
    onc::kAugmentationActiveExtension};

const base::Value::Dict* OncGetWiFi(const base::Value::Dict& network) {
  const base::Value::Dict* wifi = network.FindDict(onc::network_config::kWiFi);
  DCHECK(wifi);
  return wifi;
}

base::Value::Dict* OncGetWiFi(base::Value::Dict* network) {
  return const_cast<base::Value::Dict*>(OncGetWiFi(*network));
}

const base::Value::Dict* OncGetEthernet(const base::Value::Dict& network) {
  const base::Value::Dict* ethernet =
      network.FindDict(onc::network_config::kEthernet);
  DCHECK(ethernet);
  return ethernet;
}

const base::Value::Dict* OncGetEap(const base::Value::Dict& network) {
  if (OncIsWiFi(network)) {
    const base::Value::Dict* wifi = OncGetWiFi(network);
    const base::Value::Dict* eap = wifi->FindDict(onc::wifi::kEAP);
    DCHECK(eap);
    return eap;
  }
  if (OncIsEthernet(network)) {
    const base::Value::Dict* ethernet = OncGetEthernet(network);
    const base::Value::Dict* eap = ethernet->FindDict(onc::ethernet::kEAP);
    DCHECK(eap);
    return eap;
  }
  NOTREACHED();
}

base::Value::Dict* OncGetEap(base::Value::Dict* network) {
  return const_cast<base::Value::Dict*>(OncGetEap(*network));
}

base::Value::Dict ManagedOncCreatePasswordDict(const base::Value::Dict& network,
                                               const std::string& password) {
  std::string source = onc::kAugmentationDevicePolicy;
  if (OncIsSourceDevice(network)) {
    source = onc::kAugmentationSharedSetting;
  }

  return base::Value::Dict()
      .Set(onc::kAugmentationActiveSetting, password)
      .Set(onc::kAugmentationEffectiveSetting, source)
      .Set(source, password);
}

}  // namespace

std::string GetStringValue(const base::Value::Dict& network,
                           const std::string& key) {
  const std::string* value = network.FindString(key);
  DCHECK(value);
  return *value;
}

bool GetBoolValue(const base::Value::Dict& network, const std::string& key) {
  std::optional<bool> value = network.FindBool(key);
  DCHECK(value);
  return *value;
}

void ManagedOncCollapseToActive(base::Value* network) {
  DCHECK(network);
  base::Value::Dict* network_dict = network->GetIfDict();
  if (!network_dict) {
    return;
  }

  // FYI: don't fail to notice the fact that this out value assigned to
  // `network` might not be a dictionary. That is why the argument to this
  // function is not of `base::Value::Dict` type.
  base::Value* active = network_dict->Find(onc::kAugmentationActiveSetting);
  if (active) {
    *network = active->Clone();
    return;
  }

  std::vector<std::string> empty_dictionaries;
  for (const auto property : *network_dict) {
    ManagedOncCollapseToActive(&property.second);
    if (property.second.is_dict() && property.second.GetDict().empty()) {
      empty_dictionaries.push_back(property.first);
    }
  }
  for (const std::string& key : empty_dictionaries) {
    network_dict->Remove(key);
  }
  for (const std::string& key : kAugmentationKeys) {
    network_dict->Remove(key);
  }
}

void ManagedOncCollapseToUiData(base::Value* network) {
  DCHECK(network);
  base::Value::Dict& network_dict = network->GetDict();

  // FYI: don't fail to notice the fact that this out value assigned to
  // `network` might not be a dictionary. That is why the argument to this
  // function is not of `base::Value::Dict` type.
  base::Value* shared = network_dict.Find(onc::kAugmentationSharedSetting);
  if (shared) {
    *network = shared->Clone();
    return;
  }

  std::vector<std::string> to_remove;
  for (const auto property : network_dict) {
    if (!property.second.is_dict()) {
      to_remove.push_back(property.first);
    } else {
      // The call below may change the type of `property.second`, that's why we
      // need to check again (see above).
      ManagedOncCollapseToUiData(&property.second);
      base::Value::Dict* property_value_dict = property.second.GetIfDict();
      if (property_value_dict && property_value_dict->empty()) {
        to_remove.push_back(property.first);
      }
    }
  }
  for (const std::string& key : to_remove) {
    network_dict.Remove(key);
  }
}

void ManagedOncSetEapPassword(base::Value::Dict* network,
                              const std::string& password) {
  base::Value::Dict* eap = OncGetEap(network);
  eap->Set(onc::eap::kPassword,
           ManagedOncCreatePasswordDict(*network, password));
}

void ManagedOncWiFiSetPskPassword(base::Value::Dict* network,
                                  const std::string& password) {
  base::Value::Dict* wifi = OncGetWiFi(network);
  wifi->Set(onc::wifi::kPassphrase,
            ManagedOncCreatePasswordDict(*network, password));
}

bool OncIsWiFi(const base::Value::Dict& network) {
  return GetStringValue(network, onc::network_config::kType) ==
         onc::network_type::kWiFi;
}

bool OncIsEthernet(const base::Value::Dict& network) {
  return GetStringValue(network, onc::network_config::kType) ==
         onc::network_type::kEthernet;
}

bool OncIsSourceDevicePolicy(const base::Value::Dict& network) {
  return GetStringValue(network, onc::network_config::kSource) ==
         onc::network_config::kSourceDevicePolicy;
}

bool OncIsSourceDevice(const base::Value::Dict& network) {
  return GetStringValue(network, onc::network_config::kSource) ==
         onc::network_config::kSourceDevice;
}

bool OncHasNoSecurity(const base::Value::Dict& network) {
  if (OncIsWiFi(network)) {
    return OncWiFiGetSecurity(network) == onc::wifi::kSecurityNone;
  }
  if (OncIsEthernet(network)) {
    return OncEthernetGetAuthentication(network) ==
           onc::ethernet::kAuthenticationNone;
  }
  return false;
}

bool OncIsEap(const base::Value::Dict& network) {
  if (OncIsWiFi(network)) {
    const std::string security_type = OncWiFiGetSecurity(network);
    return security_type == onc::wifi::kWEP_8021X ||
           security_type == onc::wifi::kWPA_EAP;
  }
  if (OncIsEthernet(network)) {
    const std::string authentication_type =
        OncEthernetGetAuthentication(network);
    return authentication_type == onc::ethernet::k8021X;
  }
  return false;
}

bool OncHasEapConfiguration(const base::Value::Dict& network) {
  if (OncIsWiFi(network)) {
    const base::Value::Dict* wifi = OncGetWiFi(network);
    return wifi->FindDict(onc::wifi::kEAP);
  }
  if (OncIsEthernet(network)) {
    const base::Value::Dict* ethernet = OncGetEthernet(network);
    return ethernet->FindDict(onc::ethernet::kEAP);
  }
  return false;
}

bool OncIsEapWithoutClientCertificate(const base::Value::Dict& network) {
  return OncIsEap(network) && !OncEapRequiresClientCertificate(network);
}

std::string OncGetEapIdentity(const base::Value::Dict& network) {
  const base::Value::Dict* eap = OncGetEap(network);
  return GetStringValue(*eap, onc::eap::kIdentity);
}

std::string OncGetEapInner(const base::Value::Dict& network) {
  const base::Value::Dict* eap = OncGetEap(network);
  return GetStringValue(*eap, onc::eap::kInner);
}

std::string OncGetEapOuter(const base::Value::Dict& network) {
  const base::Value::Dict* eap = OncGetEap(network);
  return GetStringValue(*eap, onc::eap::kOuter);
}

bool OncGetEapSaveCredentials(const base::Value::Dict& network) {
  const base::Value::Dict* eap = OncGetEap(network);
  return GetBoolValue(*eap, onc::eap::kSaveCredentials);
}

std::string OncGetEapPassword(const base::Value::Dict& network) {
  const base::Value::Dict* eap = OncGetEap(network);
  return GetStringValue(*eap, onc::eap::kPassword);
}

std::string OncGetEapClientCertType(const base::Value::Dict& network) {
  const base::Value::Dict* eap = OncGetEap(network);
  return GetStringValue(*eap, onc::client_cert::kClientCertType);
}

std::string OncGetEapClientCertPKCS11Id(const base::Value::Dict& network) {
  const base::Value::Dict* eap = OncGetEap(network);
  return GetStringValue(*eap, onc::client_cert::kClientCertPKCS11Id);
}

bool OncEapRequiresClientCertificate(const base::Value::Dict& network) {
  // TODO(crbug/1225560) There may be unexpected client cert fields, so we
  // cannot rely on them. Simply check for EAP-TLS for now, which is the only
  // type for which a user may configure a client cert in the UI.
  return OncGetEapOuter(network) == onc::eap::kEAP_TLS;
}

void OncSetEapPassword(base::Value::Dict* network,
                       const std::string& password) {
  base::Value::Dict* eap = OncGetEap(network);
  eap->Set(onc::eap::kPassword, password);
}

std::string OncWiFiGetSecurity(const base::Value::Dict& network) {
  const base::Value::Dict* wifi = OncGetWiFi(network);
  const std::string* security_type = wifi->FindString(onc::wifi::kSecurity);
  DCHECK(security_type);
  return *security_type;
}

std::string OncWiFiGetPassword(const base::Value::Dict& network) {
  const base::Value::Dict* wifi = OncGetWiFi(network);
  const std::string* password = wifi->FindString(onc::wifi::kPassphrase);
  DCHECK(password);
  return *password;
}

bool OncWiFiIsPsk(const base::Value::Dict& network) {
  const std::string security_type = OncWiFiGetSecurity(network);
  return security_type == onc::wifi::kWEP_PSK ||
         security_type == onc::wifi::kWPA_PSK ||
         security_type == onc::wifi::kWPA2_PSK;
}

void OncWiFiSetPskPassword(base::Value::Dict* network,
                           const std::string& password) {
  base::Value::Dict* wifi = OncGetWiFi(network);
  wifi->Set(onc::wifi::kPassphrase, password);
}

std::string OncEthernetGetAuthentication(const base::Value::Dict& network) {
  const std::string* type = network.FindDict(onc::network_config::kEthernet)
                                ->FindString(onc::ethernet::kAuthentication);
  DCHECK(type);
  return *type;
}

}  // namespace ash::rollback_network_config