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 // The implementation of TranslateONCObjectToShill is structured in two parts: | 5 // The implementation of TranslateONCObjectToShill is structured in two parts: |
6 // - The recursion through the existing ONC hierarchy | 6 // - The recursion through the existing ONC hierarchy |
7 // see TranslateONCHierarchy | 7 // see TranslateONCHierarchy |
8 // - The local translation of an object depending on the associated signature | 8 // - The local translation of an object depending on the associated signature |
9 // see LocalTranslator::TranslateFields | 9 // see LocalTranslator::TranslateFields |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... |
33 return make_scoped_ptr(base::Value::CreateStringValue(str)); | 33 return make_scoped_ptr(base::Value::CreateStringValue(str)); |
34 } | 34 } |
35 | 35 |
36 // This class is responsible to translate the local fields of the given | 36 // This class is responsible to translate the local fields of the given |
37 // |onc_object| according to |onc_signature| into |shill_dictionary|. This | 37 // |onc_object| according to |onc_signature| into |shill_dictionary|. This |
38 // translation should consider (if possible) only fields of this ONC object and | 38 // translation should consider (if possible) only fields of this ONC object and |
39 // not nested objects because recursion is handled by the calling function | 39 // not nested objects because recursion is handled by the calling function |
40 // TranslateONCHierarchy. | 40 // TranslateONCHierarchy. |
41 class LocalTranslator { | 41 class LocalTranslator { |
42 public: | 42 public: |
43 LocalTranslator(const OncValueSignature& onc_signature, | 43 LocalTranslator( |
44 const base::DictionaryValue& onc_object, | 44 const OncValueSignature& onc_signature, |
45 base::DictionaryValue* shill_dictionary) | 45 const base::DictionaryValue& onc_object, |
| 46 base::DictionaryValue* shill_dictionary, |
| 47 const base::Callback<std::string(const std::string&)>& fingerprint_to_pem) |
46 : onc_signature_(&onc_signature), | 48 : onc_signature_(&onc_signature), |
47 onc_object_(&onc_object), | 49 onc_object_(&onc_object), |
48 shill_dictionary_(shill_dictionary) { | 50 shill_dictionary_(shill_dictionary), |
| 51 fingerprint_to_pem_(fingerprint_to_pem) { |
49 field_translation_table_ = GetFieldTranslationTable(onc_signature); | 52 field_translation_table_ = GetFieldTranslationTable(onc_signature); |
50 } | 53 } |
51 | 54 |
52 void TranslateFields(); | 55 void TranslateFields(); |
53 | 56 |
54 private: | 57 private: |
55 void TranslateOpenVPN(); | 58 void TranslateOpenVPN(); |
| 59 void TranslateIPsec(); |
56 void TranslateVPN(); | 60 void TranslateVPN(); |
57 void TranslateWiFi(); | 61 void TranslateWiFi(); |
58 void TranslateEAP(); | 62 void TranslateEAP(); |
59 void TranslateNetworkConfiguration(); | 63 void TranslateNetworkConfiguration(); |
60 | 64 |
61 // Copies all entries from |onc_object_| to |shill_dictionary_| for which a | 65 // Copies all entries from |onc_object_| to |shill_dictionary_| for which a |
62 // translation (shill_property_name) is defined by |onc_signature_|. | 66 // translation (shill_property_name) is defined by |onc_signature_|. |
63 void CopyFieldsAccordingToSignature(); | 67 void CopyFieldsAccordingToSignature(); |
64 | 68 |
65 // Adds |value| to |shill_dictionary| at the field shill_property_name given | 69 // Adds |value| to |shill_dictionary| at the field shill_property_name given |
66 // by the associated signature. Takes ownership of |value|. Does nothing if | 70 // by the associated signature. Takes ownership of |value|. Does nothing if |
67 // |value| is NULL or the property name cannot be read from the signature. | 71 // |value| is NULL or the property name cannot be read from the signature. |
68 void AddValueAccordingToSignature(const std::string& onc_field_name, | 72 void AddValueAccordingToSignature(const std::string& onc_field_name, |
69 scoped_ptr<base::Value> value); | 73 scoped_ptr<base::Value> value); |
70 | 74 |
71 // If existent, translates the entry at |onc_field_name| in |onc_object_| | 75 // If existent, translates the entry at |onc_field_name| in |onc_object_| |
72 // using |table|. It is an error if no matching table entry is found. Writes | 76 // using |table|. It is an error if no matching table entry is found. Writes |
73 // the result as entry at |shill_property_name| in |shill_dictionary_|. | 77 // the result as entry at |shill_property_name| in |shill_dictionary_|. |
74 void TranslateWithTableAndSet(const std::string& onc_field_name, | 78 void TranslateWithTableAndSet(const std::string& onc_field_name, |
75 const StringTranslationEntry table[], | 79 const StringTranslationEntry table[], |
76 const std::string& shill_property_name); | 80 const std::string& shill_property_name); |
77 | 81 |
78 const OncValueSignature* onc_signature_; | 82 const OncValueSignature* onc_signature_; |
79 const FieldTranslationEntry* field_translation_table_; | 83 const FieldTranslationEntry* field_translation_table_; |
80 const base::DictionaryValue* onc_object_; | 84 const base::DictionaryValue* onc_object_; |
81 base::DictionaryValue* shill_dictionary_; | 85 base::DictionaryValue* shill_dictionary_; |
| 86 base::Callback<std::string(const std::string&)> fingerprint_to_pem_; |
82 | 87 |
83 DISALLOW_COPY_AND_ASSIGN(LocalTranslator); | 88 DISALLOW_COPY_AND_ASSIGN(LocalTranslator); |
84 }; | 89 }; |
85 | 90 |
86 void LocalTranslator::TranslateFields() { | 91 void LocalTranslator::TranslateFields() { |
87 if (onc_signature_ == &kNetworkConfigurationSignature) | 92 if (onc_signature_ == &kNetworkConfigurationSignature) |
88 TranslateNetworkConfiguration(); | 93 TranslateNetworkConfiguration(); |
89 else if (onc_signature_ == &kVPNSignature) | 94 else if (onc_signature_ == &kVPNSignature) |
90 TranslateVPN(); | 95 TranslateVPN(); |
91 else if (onc_signature_ == &kOpenVPNSignature) | 96 else if (onc_signature_ == &kOpenVPNSignature) |
92 TranslateOpenVPN(); | 97 TranslateOpenVPN(); |
| 98 else if (onc_signature_ == &kIPsecSignature) |
| 99 TranslateIPsec(); |
93 else if (onc_signature_ == &kWiFiSignature) | 100 else if (onc_signature_ == &kWiFiSignature) |
94 TranslateWiFi(); | 101 TranslateWiFi(); |
95 else if (onc_signature_ == &kEAPSignature) | 102 else if (onc_signature_ == &kEAPSignature) |
96 TranslateEAP(); | 103 TranslateEAP(); |
97 else | 104 else |
98 CopyFieldsAccordingToSignature(); | 105 CopyFieldsAccordingToSignature(); |
99 } | 106 } |
100 | 107 |
101 void LocalTranslator::TranslateOpenVPN() { | 108 void LocalTranslator::TranslateOpenVPN() { |
102 // Shill supports only one RemoteCertKU but ONC a list. | 109 // Shill supports only one RemoteCertKU but ONC a list. |
103 // Copy only the first entry if existing. | 110 // Copy only the first entry if existing. |
104 const base::ListValue* certKUs = NULL; | 111 const base::ListValue* cert_kus = NULL; |
105 std::string certKU; | 112 std::string cert_ku; |
106 if (onc_object_->GetListWithoutPathExpansion(vpn::kRemoteCertKU, &certKUs) && | 113 if (onc_object_->GetListWithoutPathExpansion(vpn::kRemoteCertKU, &cert_kus) && |
107 certKUs->GetString(0, &certKU)) { | 114 cert_kus->GetString(0, &cert_ku)) { |
108 shill_dictionary_->SetStringWithoutPathExpansion( | 115 shill_dictionary_->SetStringWithoutPathExpansion( |
109 flimflam::kOpenVPNRemoteCertKUProperty, certKU); | 116 flimflam::kOpenVPNRemoteCertKUProperty, cert_ku); |
| 117 } |
| 118 |
| 119 std::string ca_cert_fingerprint; |
| 120 if (onc_object_->GetStringWithoutPathExpansion(vpn::kServerCAFingerprint, |
| 121 &ca_cert_fingerprint)) { |
| 122 std::string ca_cert_pem = fingerprint_to_pem_.Run(ca_cert_fingerprint); |
| 123 if (!ca_cert_pem.empty()) { |
| 124 shill_dictionary_->SetStringWithoutPathExpansion( |
| 125 shill::kOpenVPNCaCertPemProperty, ca_cert_pem); |
| 126 } |
110 } | 127 } |
111 | 128 |
112 for (base::DictionaryValue::Iterator it(*onc_object_); !it.IsAtEnd(); | 129 for (base::DictionaryValue::Iterator it(*onc_object_); !it.IsAtEnd(); |
113 it.Advance()) { | 130 it.Advance()) { |
114 scoped_ptr<base::Value> translated; | 131 scoped_ptr<base::Value> translated; |
115 if (it.key() == vpn::kSaveCredentials || it.key() == vpn::kRemoteCertKU) { | 132 if (it.key() == vpn::kSaveCredentials || it.key() == vpn::kRemoteCertKU) { |
116 translated.reset(it.value().DeepCopy()); | 133 translated.reset(it.value().DeepCopy()); |
117 } else { | 134 } else { |
118 // Shill wants all Provider/VPN fields to be strings. | 135 // Shill wants all Provider/VPN fields to be strings. |
119 translated = ConvertValueToString(it.value()); | 136 translated = ConvertValueToString(it.value()); |
120 } | 137 } |
121 AddValueAccordingToSignature(it.key(), translated.Pass()); | 138 AddValueAccordingToSignature(it.key(), translated.Pass()); |
122 } | 139 } |
123 } | 140 } |
124 | 141 |
| 142 void LocalTranslator::TranslateIPsec() { |
| 143 std::string ca_cert_fingerprint; |
| 144 if (onc_object_->GetStringWithoutPathExpansion(vpn::kServerCAFingerprint, |
| 145 &ca_cert_fingerprint)) { |
| 146 std::string ca_cert_pem = fingerprint_to_pem_.Run(ca_cert_fingerprint); |
| 147 if (!ca_cert_pem.empty()) { |
| 148 shill_dictionary_->SetStringWithoutPathExpansion( |
| 149 shill::kL2tpIpsecCaCertPemProperty, ca_cert_pem); |
| 150 } |
| 151 } |
| 152 CopyFieldsAccordingToSignature(); |
| 153 } |
| 154 |
125 void LocalTranslator::TranslateVPN() { | 155 void LocalTranslator::TranslateVPN() { |
126 std::string type; | 156 std::string type; |
127 onc_object_->GetStringWithoutPathExpansion(vpn::kType, &type); | 157 onc_object_->GetStringWithoutPathExpansion(vpn::kType, &type); |
128 TranslateWithTableAndSet(type, kVPNTypeTable, | 158 TranslateWithTableAndSet(type, kVPNTypeTable, |
129 flimflam::kProviderTypeProperty); | 159 flimflam::kProviderTypeProperty); |
130 | 160 |
131 CopyFieldsAccordingToSignature(); | 161 CopyFieldsAccordingToSignature(); |
132 } | 162 } |
133 | 163 |
134 void LocalTranslator::TranslateWiFi() { | 164 void LocalTranslator::TranslateWiFi() { |
(...skipping 20 matching lines...) Expand all Loading... |
155 // ONC's Inner == "Automatic" translates to omitting the Phase2 property in | 185 // ONC's Inner == "Automatic" translates to omitting the Phase2 property in |
156 // Shill. | 186 // Shill. |
157 onc_object_->GetStringWithoutPathExpansion(eap::kInner, &inner); | 187 onc_object_->GetStringWithoutPathExpansion(eap::kInner, &inner); |
158 if (inner != eap::kAutomatic) { | 188 if (inner != eap::kAutomatic) { |
159 const StringTranslationEntry* table = | 189 const StringTranslationEntry* table = |
160 outer == eap::kPEAP ? kEAP_PEAP_InnerTable : kEAP_TTLS_InnerTable; | 190 outer == eap::kPEAP ? kEAP_PEAP_InnerTable : kEAP_TTLS_InnerTable; |
161 TranslateWithTableAndSet(inner, table, flimflam::kEapPhase2AuthProperty); | 191 TranslateWithTableAndSet(inner, table, flimflam::kEapPhase2AuthProperty); |
162 } | 192 } |
163 } | 193 } |
164 | 194 |
| 195 std::string ca_cert_fingerprint; |
| 196 if (onc_object_->GetStringWithoutPathExpansion(eap::kServerCAFingerprint, |
| 197 &ca_cert_fingerprint)) { |
| 198 std::string ca_cert_pem = fingerprint_to_pem_.Run(ca_cert_fingerprint); |
| 199 if (!ca_cert_pem.empty()) { |
| 200 shill_dictionary_->SetStringWithoutPathExpansion( |
| 201 shill::kEapCaCertPemProperty, ca_cert_pem); |
| 202 } |
| 203 } |
| 204 |
165 CopyFieldsAccordingToSignature(); | 205 CopyFieldsAccordingToSignature(); |
166 } | 206 } |
167 | 207 |
168 void LocalTranslator::TranslateNetworkConfiguration() { | 208 void LocalTranslator::TranslateNetworkConfiguration() { |
169 std::string type; | 209 std::string type; |
170 onc_object_->GetStringWithoutPathExpansion(network_config::kType, &type); | 210 onc_object_->GetStringWithoutPathExpansion(network_config::kType, &type); |
171 TranslateWithTableAndSet(type, kNetworkTypeTable, flimflam::kTypeProperty); | 211 TranslateWithTableAndSet(type, kNetworkTypeTable, flimflam::kTypeProperty); |
172 | 212 |
173 // Shill doesn't allow setting the name for non-VPN networks. | 213 // Shill doesn't allow setting the name for non-VPN networks. |
174 if (type == network_type::kVPN) { | 214 if (type == network_type::kVPN) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 // occurs, we should check here. Otherwise the failure will only show up much | 259 // occurs, we should check here. Otherwise the failure will only show up much |
220 // later in Shill. | 260 // later in Shill. |
221 LOG(ERROR) << "Value '" << onc_value | 261 LOG(ERROR) << "Value '" << onc_value |
222 << "' cannot be translated to Shill property " | 262 << "' cannot be translated to Shill property " |
223 << shill_property_name; | 263 << shill_property_name; |
224 } | 264 } |
225 | 265 |
226 // Iterates recursively over |onc_object| and its |signature|. At each object | 266 // Iterates recursively over |onc_object| and its |signature|. At each object |
227 // applies the local translation using LocalTranslator::TranslateFields. The | 267 // applies the local translation using LocalTranslator::TranslateFields. The |
228 // results are written to |shill_dictionary|. | 268 // results are written to |shill_dictionary|. |
229 void TranslateONCHierarchy(const OncValueSignature& signature, | 269 void TranslateONCHierarchy( |
230 const base::DictionaryValue& onc_object, | 270 const OncValueSignature& signature, |
231 base::DictionaryValue* shill_dictionary) { | 271 const base::DictionaryValue& onc_object, |
| 272 const base::Callback<std::string(const std::string&)>& fingerprint_to_pem, |
| 273 base::DictionaryValue* shill_dictionary) { |
232 // Translates fields of |onc_object| and writes them to |shill_dictionary_|. | 274 // Translates fields of |onc_object| and writes them to |shill_dictionary_|. |
233 LocalTranslator translator(signature, onc_object, shill_dictionary); | 275 LocalTranslator translator(signature, onc_object, shill_dictionary, |
| 276 fingerprint_to_pem); |
234 translator.TranslateFields(); | 277 translator.TranslateFields(); |
235 | 278 |
236 // Recurse into nested objects. | 279 // Recurse into nested objects. |
237 for (base::DictionaryValue::Iterator it(onc_object); !it.IsAtEnd(); | 280 for (base::DictionaryValue::Iterator it(onc_object); !it.IsAtEnd(); |
238 it.Advance()) { | 281 it.Advance()) { |
239 const base::DictionaryValue* inner_object = NULL; | 282 const base::DictionaryValue* inner_object = NULL; |
240 if (!it.value().GetAsDictionary(&inner_object)) | 283 if (!it.value().GetAsDictionary(&inner_object)) |
241 continue; | 284 continue; |
242 | 285 |
243 const OncFieldSignature* field_signature = | 286 const OncFieldSignature* field_signature = |
244 GetFieldSignature(signature, it.key()); | 287 GetFieldSignature(signature, it.key()); |
245 | 288 |
246 TranslateONCHierarchy(*field_signature->value_signature, *inner_object, | 289 TranslateONCHierarchy(*field_signature->value_signature, *inner_object, |
247 shill_dictionary); | 290 fingerprint_to_pem, shill_dictionary); |
248 } | 291 } |
249 } | 292 } |
250 | 293 |
251 } // namespace | 294 } // namespace |
252 | 295 |
253 scoped_ptr<base::DictionaryValue> TranslateONCObjectToShill( | 296 scoped_ptr<base::DictionaryValue> TranslateONCObjectToShill( |
254 const OncValueSignature* onc_signature, | 297 const OncValueSignature* onc_signature, |
255 const base::DictionaryValue& onc_object) { | 298 const base::DictionaryValue& onc_object, |
| 299 const base::Callback<std::string(const std::string&)>& fingerprint_to_pem) { |
256 CHECK(onc_signature != NULL); | 300 CHECK(onc_signature != NULL); |
257 scoped_ptr<base::DictionaryValue> shill_dictionary(new base::DictionaryValue); | 301 scoped_ptr<base::DictionaryValue> shill_dictionary(new base::DictionaryValue); |
258 TranslateONCHierarchy(*onc_signature, onc_object, shill_dictionary.get()); | 302 TranslateONCHierarchy(*onc_signature, onc_object, fingerprint_to_pem, |
| 303 shill_dictionary.get()); |
259 return shill_dictionary.Pass(); | 304 return shill_dictionary.Pass(); |
260 } | 305 } |
261 | 306 |
262 } // namespace onc | 307 } // namespace onc |
263 } // namespace chromeos | 308 } // namespace chromeos |
OLD | NEW |