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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// The implementation of TranslateONCObjectToShill is structured in two parts:
// - The recursion through the existing ONC hierarchy
// see TranslateONCHierarchy
// - The local translation of an object depending on the associated signature
// see LocalTranslator::TranslateFields
#include "chromeos/network/onc/onc_translator.h"
#include <string>
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chromeos/network/onc/onc_signature.h"
#include "chromeos/network/onc/onc_translation_tables.h"
#include "chromeos/network/shill_property_util.h"
#include "components/onc/onc_constants.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace chromeos {
namespace onc {
namespace {
scoped_ptr<base::StringValue> ConvertValueToString(const base::Value& value) {
std::string str;
if (!value.GetAsString(&str))
base::JSONWriter::Write(&value, &str);
return make_scoped_ptr(new base::StringValue(str));
}
// This class is responsible to translate the local fields of the given
// |onc_object| according to |onc_signature| into |shill_dictionary|. This
// translation should consider (if possible) only fields of this ONC object and
// not nested objects because recursion is handled by the calling function
// TranslateONCHierarchy.
class LocalTranslator {
public:
LocalTranslator(const OncValueSignature& onc_signature,
const base::DictionaryValue& onc_object,
base::DictionaryValue* shill_dictionary)
: onc_signature_(&onc_signature),
onc_object_(&onc_object),
shill_dictionary_(shill_dictionary) {
field_translation_table_ = GetFieldTranslationTable(onc_signature);
}
void TranslateFields();
private:
void TranslateEthernet();
void TranslateOpenVPN();
void TranslateIPsec();
void TranslateVPN();
void TranslateWiFi();
void TranslateEAP();
void TranslateNetworkConfiguration();
// Copies all entries from |onc_object_| to |shill_dictionary_| for which a
// translation (shill_property_name) is defined by the translation table for
// |onc_signature_|.
void CopyFieldsAccordingToSignature();
// If existent, copies the value of field |onc_field_name| from |onc_object_|
// to the property |shill_property_name| in |shill_dictionary_|.
void CopyFieldFromONCToShill(const std::string& onc_field_name,
const std::string& shill_property_name);
// Adds |value| to |shill_dictionary| at the field shill_property_name given
// by the associated signature. Takes ownership of |value|. Does nothing if
// |value| is NULL or the property name cannot be read from the signature.
void AddValueAccordingToSignature(const std::string& onc_field_name,
scoped_ptr<base::Value> value);
// Translates the value |onc_value| using |table|. It is an error if no
// matching table entry is found. Writes the result as entry at
// |shill_property_name| in |shill_dictionary_|.
void TranslateWithTableAndSet(const std::string& onc_value,
const StringTranslationEntry table[],
const std::string& shill_property_name);
const OncValueSignature* onc_signature_;
const FieldTranslationEntry* field_translation_table_;
const base::DictionaryValue* onc_object_;
base::DictionaryValue* shill_dictionary_;
DISALLOW_COPY_AND_ASSIGN(LocalTranslator);
};
void LocalTranslator::TranslateFields() {
if (onc_signature_ == &kNetworkConfigurationSignature)
TranslateNetworkConfiguration();
else if (onc_signature_ == &kEthernetSignature)
TranslateEthernet();
else if (onc_signature_ == &kVPNSignature)
TranslateVPN();
else if (onc_signature_ == &kOpenVPNSignature)
TranslateOpenVPN();
else if (onc_signature_ == &kIPsecSignature)
TranslateIPsec();
else if (onc_signature_ == &kWiFiSignature)
TranslateWiFi();
else if (onc_signature_ == &kEAPSignature)
TranslateEAP();
else
CopyFieldsAccordingToSignature();
}
void LocalTranslator::TranslateEthernet() {
std::string authentication;
onc_object_->GetStringWithoutPathExpansion(::onc::ethernet::kAuthentication,
&authentication);
const char* shill_type = shill::kTypeEthernet;
if (authentication == ::onc::ethernet::k8021X)
shill_type = shill::kTypeEthernetEap;
shill_dictionary_->SetStringWithoutPathExpansion(shill::kTypeProperty,
shill_type);
CopyFieldsAccordingToSignature();
}
void LocalTranslator::TranslateOpenVPN() {
// SaveCredentials needs special handling when translating from Shill -> ONC
// so handle it explicitly here.
CopyFieldFromONCToShill(::onc::vpn::kSaveCredentials,
shill::kSaveCredentialsProperty);
std::string user_auth_type;
onc_object_->GetStringWithoutPathExpansion(
::onc::openvpn::kUserAuthenticationType, &user_auth_type);
// The default behavior (if user_auth_type is empty) is to use both password
// and OTP in a static challenge and only the password otherwise. As long as
// Shill doe not know about the exact user authentication type, this is
// identical to kPasswordAndOTP.
if (user_auth_type.empty())
user_auth_type = ::onc::openvpn_user_auth_type::kPasswordAndOTP;
if (user_auth_type == ::onc::openvpn_user_auth_type::kPassword ||
user_auth_type == ::onc::openvpn_user_auth_type::kPasswordAndOTP) {
CopyFieldFromONCToShill(::onc::openvpn::kPassword,
shill::kOpenVPNPasswordProperty);
}
if (user_auth_type == ::onc::openvpn_user_auth_type::kPasswordAndOTP)
CopyFieldFromONCToShill(::onc::openvpn::kOTP, shill::kOpenVPNOTPProperty);
if (user_auth_type == ::onc::openvpn_user_auth_type::kOTP)
CopyFieldFromONCToShill(::onc::openvpn::kOTP, shill::kOpenVPNTokenProperty);
// Shill supports only one RemoteCertKU but ONC a list.
// Copy only the first entry if existing.
const base::ListValue* cert_kus = NULL;
std::string cert_ku;
if (onc_object_->GetListWithoutPathExpansion(::onc::openvpn::kRemoteCertKU,
&cert_kus) &&
cert_kus->GetString(0, &cert_ku)) {
shill_dictionary_->SetStringWithoutPathExpansion(
shill::kOpenVPNRemoteCertKUProperty, cert_ku);
}
for (base::DictionaryValue::Iterator it(*onc_object_); !it.IsAtEnd();
it.Advance()) {
scoped_ptr<base::Value> translated;
if (it.key() == ::onc::openvpn::kRemoteCertKU ||
it.key() == ::onc::openvpn::kServerCAPEMs) {
translated.reset(it.value().DeepCopy());
} else {
// Shill wants all Provider/VPN fields to be strings.
translated = ConvertValueToString(it.value());
}
AddValueAccordingToSignature(it.key(), translated.Pass());
}
}
void LocalTranslator::TranslateIPsec() {
CopyFieldsAccordingToSignature();
// SaveCredentials needs special handling when translating from Shill -> ONC
// so handle it explicitly here.
CopyFieldFromONCToShill(::onc::vpn::kSaveCredentials,
shill::kSaveCredentialsProperty);
}
void LocalTranslator::TranslateVPN() {
CopyFieldFromONCToShill(::onc::vpn::kHost, shill::kProviderHostProperty);
std::string type;
if (onc_object_->GetStringWithoutPathExpansion(::onc::vpn::kType, &type))
TranslateWithTableAndSet(type, kVPNTypeTable, shill::kProviderTypeProperty);
CopyFieldsAccordingToSignature();
}
void LocalTranslator::TranslateWiFi() {
std::string security;
onc_object_->GetStringWithoutPathExpansion(::onc::wifi::kSecurity, &security);
TranslateWithTableAndSet(security, kWiFiSecurityTable,
shill::kSecurityProperty);
std::string ssid;
onc_object_->GetStringWithoutPathExpansion(::onc::wifi::kSSID, &ssid);
if (!ssid.empty())
shill_property_util::SetSSID(ssid, shill_dictionary_);
// We currently only support managed and no adhoc networks.
shill_dictionary_->SetStringWithoutPathExpansion(shill::kModeProperty,
shill::kModeManaged);
bool allow_gateway_arp_polling;
if (onc_object_->GetBooleanWithoutPathExpansion(
::onc::wifi::kAllowGatewayARPPolling, &allow_gateway_arp_polling)) {
shill_dictionary_->SetBooleanWithoutPathExpansion(
shill::kLinkMonitorDisableProperty, !allow_gateway_arp_polling);
}
CopyFieldsAccordingToSignature();
}
void LocalTranslator::TranslateEAP() {
std::string outer;
onc_object_->GetStringWithoutPathExpansion(::onc::eap::kOuter, &outer);
TranslateWithTableAndSet(outer, kEAPOuterTable, shill::kEapMethodProperty);
// Translate the inner protocol only for outer tunneling protocols.
if (outer == ::onc::eap::kPEAP || outer == ::onc::eap::kEAP_TTLS) {
// In ONC the Inner protocol defaults to "Automatic".
std::string inner = ::onc::eap::kAutomatic;
// ONC's Inner == "Automatic" translates to omitting the Phase2 property in
// Shill.
onc_object_->GetStringWithoutPathExpansion(::onc::eap::kInner, &inner);
if (inner != ::onc::eap::kAutomatic) {
const StringTranslationEntry* table =
outer == ::onc::eap::kPEAP ? kEAP_PEAP_InnerTable :
kEAP_TTLS_InnerTable;
TranslateWithTableAndSet(inner, table, shill::kEapPhase2AuthProperty);
}
}
CopyFieldsAccordingToSignature();
}
void LocalTranslator::TranslateNetworkConfiguration() {
std::string type;
onc_object_->GetStringWithoutPathExpansion(::onc::network_config::kType,
&type);
// Set the type except for Ethernet which is set in TranslateEthernet.
if (type != ::onc::network_type::kEthernet)
TranslateWithTableAndSet(type, kNetworkTypeTable, shill::kTypeProperty);
// Shill doesn't allow setting the name for non-VPN networks.
if (type == ::onc::network_type::kVPN)
CopyFieldFromONCToShill(::onc::network_config::kName, shill::kNameProperty);
CopyFieldsAccordingToSignature();
}
void LocalTranslator::CopyFieldsAccordingToSignature() {
for (base::DictionaryValue::Iterator it(*onc_object_); !it.IsAtEnd();
it.Advance()) {
AddValueAccordingToSignature(it.key(),
make_scoped_ptr(it.value().DeepCopy()));
}
}
void LocalTranslator::CopyFieldFromONCToShill(
const std::string& onc_field_name,
const std::string& shill_property_name) {
const base::Value* value = NULL;
if (!onc_object_->GetWithoutPathExpansion(onc_field_name, &value))
return;
const OncFieldSignature* field_signature =
GetFieldSignature(*onc_signature_, onc_field_name);
if (field_signature) {
base::Value::Type expected_type =
field_signature->value_signature->onc_type;
if (value->GetType() != expected_type) {
LOG(ERROR) << "Found field " << onc_field_name << " of type "
<< value->GetType() << " but expected type " << expected_type;
return;
}
} else {
LOG(ERROR)
<< "Attempt to translate a field that is not part of the ONC format.";
return;
}
shill_dictionary_->SetWithoutPathExpansion(shill_property_name,
value->DeepCopy());
}
void LocalTranslator::AddValueAccordingToSignature(
const std::string& onc_name,
scoped_ptr<base::Value> value) {
if (!value || !field_translation_table_)
return;
std::string shill_property_name;
if (!GetShillPropertyName(
onc_name, field_translation_table_, &shill_property_name)) {
return;
}
shill_dictionary_->SetWithoutPathExpansion(shill_property_name,
value.release());
}
void LocalTranslator::TranslateWithTableAndSet(
const std::string& onc_value,
const StringTranslationEntry table[],
const std::string& shill_property_name) {
std::string shill_value;
if (TranslateStringToShill(table, onc_value, &shill_value)) {
shill_dictionary_->SetStringWithoutPathExpansion(shill_property_name,
shill_value);
return;
}
// As we previously validate ONC, this case should never occur. If it still
// occurs, we should check here. Otherwise the failure will only show up much
// later in Shill.
LOG(ERROR) << "Value '" << onc_value
<< "' cannot be translated to Shill property "
<< shill_property_name;
}
// Iterates recursively over |onc_object| and its |signature|. At each object
// applies the local translation using LocalTranslator::TranslateFields. The
// results are written to |shill_dictionary|.
void TranslateONCHierarchy(const OncValueSignature& signature,
const base::DictionaryValue& onc_object,
base::DictionaryValue* shill_dictionary) {
base::DictionaryValue* target_shill_dictionary = shill_dictionary;
std::vector<std::string> path_to_shill_dictionary =
GetPathToNestedShillDictionary(signature);
for (std::vector<std::string>::const_iterator it =
path_to_shill_dictionary.begin();
it != path_to_shill_dictionary.end();
++it) {
base::DictionaryValue* nested_shill_dict = NULL;
target_shill_dictionary->GetDictionaryWithoutPathExpansion(
*it, &nested_shill_dict);
if (!nested_shill_dict)
nested_shill_dict = new base::DictionaryValue;
target_shill_dictionary->SetWithoutPathExpansion(*it, nested_shill_dict);
target_shill_dictionary = nested_shill_dict;
}
// Translates fields of |onc_object| and writes them to
// |target_shill_dictionary_| nested in |shill_dictionary|.
LocalTranslator translator(signature, onc_object, target_shill_dictionary);
translator.TranslateFields();
// Recurse into nested objects.
for (base::DictionaryValue::Iterator it(onc_object); !it.IsAtEnd();
it.Advance()) {
const base::DictionaryValue* inner_object = NULL;
if (!it.value().GetAsDictionary(&inner_object))
continue;
const OncFieldSignature* field_signature =
GetFieldSignature(signature, it.key());
TranslateONCHierarchy(*field_signature->value_signature, *inner_object,
shill_dictionary);
}
}
} // namespace
scoped_ptr<base::DictionaryValue> TranslateONCObjectToShill(
const OncValueSignature* onc_signature,
const base::DictionaryValue& onc_object) {
CHECK(onc_signature != NULL);
scoped_ptr<base::DictionaryValue> shill_dictionary(new base::DictionaryValue);
TranslateONCHierarchy(*onc_signature, onc_object, shill_dictionary.get());
return shill_dictionary.Pass();
}
} // namespace onc
} // namespace chromeos
|