- add sources.
[platform/framework/web/crosswalk.git] / src / chromeos / network / onc / onc_translation_tables.cc
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 "chromeos/network/onc/onc_translation_tables.h"
6
7 #include <cstddef>
8
9 #include "base/logging.h"
10 #include "components/onc/onc_constants.h"
11 #include "third_party/cros_system_api/dbus/service_constants.h"
12
13 namespace chromeos {
14 namespace onc {
15
16 // CertificatePattern is converted with function CreateUIData(...) to UIData
17 // stored in Shill.
18 //
19 // Proxy settings are converted to Shill by function
20 // ConvertOncProxySettingsToProxyConfig(...).
21 //
22 // Translation of IPConfig objects is not supported, yet.
23
24 namespace {
25
26 const FieldTranslationEntry eap_fields[] = {
27     { ::onc::eap::kAnonymousIdentity, shill::kEapAnonymousIdentityProperty},
28     { ::onc::eap::kIdentity, shill::kEapIdentityProperty},
29     // This field is converted during translation, see onc_translator_*.
30     // { ::onc::eap::kInner, shill::kEapPhase2AuthProperty },
31
32     // This field is converted during translation, see onc_translator_*.
33     // { ::onc::eap::kOuter, shill::kEapMethodProperty },
34     { ::onc::eap::kPassword, shill::kEapPasswordProperty},
35     { ::onc::eap::kSaveCredentials, shill::kSaveCredentialsProperty},
36     { ::onc::eap::kServerCAPEMs, shill::kEapCaCertPemProperty},
37     { ::onc::eap::kUseSystemCAs, shill::kEapUseSystemCasProperty},
38     {NULL}};
39
40 const FieldTranslationEntry ipsec_fields[] = {
41     // Ignored by Shill, not necessary to synchronize.
42     // { ::onc::ipsec::kAuthenticationType, shill::kL2tpIpsecAuthenticationType
43     // },
44     { ::onc::ipsec::kGroup, shill::kL2tpIpsecTunnelGroupProperty},
45     // Ignored by Shill, not necessary to synchronize.
46     // { ::onc::ipsec::kIKEVersion, shill::kL2tpIpsecIkeVersion },
47     { ::onc::ipsec::kPSK, shill::kL2tpIpsecPskProperty},
48     { ::onc::vpn::kSaveCredentials, shill::kSaveCredentialsProperty},
49     { ::onc::ipsec::kServerCAPEMs, shill::kL2tpIpsecCaCertPemProperty},
50     {NULL}};
51
52 const FieldTranslationEntry l2tp_fields[] = {
53     { ::onc::vpn::kPassword, shill::kL2tpIpsecPasswordProperty},
54     // We don't synchronize l2tp's SaveCredentials field for now, as Shill
55     // doesn't
56     // support separate settings for ipsec and l2tp.
57     // { ::onc::vpn::kSaveCredentials, &kBoolSignature },
58     { ::onc::vpn::kUsername, shill::kL2tpIpsecUserProperty}, {NULL}};
59
60 const FieldTranslationEntry openvpn_fields[] = {
61     { ::onc::openvpn::kAuth, shill::kOpenVPNAuthProperty},
62     { ::onc::openvpn::kAuthNoCache, shill::kOpenVPNAuthNoCacheProperty},
63     { ::onc::openvpn::kAuthRetry, shill::kOpenVPNAuthRetryProperty},
64     { ::onc::openvpn::kCipher, shill::kOpenVPNCipherProperty},
65     { ::onc::openvpn::kCompLZO, shill::kOpenVPNCompLZOProperty},
66     { ::onc::openvpn::kCompNoAdapt, shill::kOpenVPNCompNoAdaptProperty},
67     { ::onc::openvpn::kKeyDirection, shill::kOpenVPNKeyDirectionProperty},
68     { ::onc::openvpn::kNsCertType, shill::kOpenVPNNsCertTypeProperty},
69     { ::onc::vpn::kPassword, shill::kOpenVPNPasswordProperty},
70     { ::onc::openvpn::kPort, shill::kOpenVPNPortProperty},
71     { ::onc::openvpn::kProto, shill::kOpenVPNProtoProperty},
72     { ::onc::openvpn::kPushPeerInfo, shill::kOpenVPNPushPeerInfoProperty},
73     { ::onc::openvpn::kRemoteCertEKU, shill::kOpenVPNRemoteCertEKUProperty},
74     // This field is converted during translation, see onc_translator_*.
75     // { ::onc::openvpn::kRemoteCertKU, shill::kOpenVPNRemoteCertKUProperty },
76     { ::onc::openvpn::kRemoteCertTLS, shill::kOpenVPNRemoteCertTLSProperty},
77     { ::onc::openvpn::kRenegSec, shill::kOpenVPNRenegSecProperty},
78     { ::onc::vpn::kSaveCredentials, shill::kSaveCredentialsProperty},
79     { ::onc::openvpn::kServerCAPEMs, shill::kOpenVPNCaCertPemProperty},
80     { ::onc::openvpn::kServerPollTimeout,
81       shill::kOpenVPNServerPollTimeoutProperty},
82     { ::onc::openvpn::kShaper, shill::kOpenVPNShaperProperty},
83     { ::onc::openvpn::kStaticChallenge, shill::kOpenVPNStaticChallengeProperty},
84     { ::onc::openvpn::kTLSAuthContents, shill::kOpenVPNTLSAuthContentsProperty},
85     { ::onc::openvpn::kTLSRemote, shill::kOpenVPNTLSRemoteProperty},
86     { ::onc::vpn::kUsername, shill::kOpenVPNUserProperty}, {NULL}};
87
88 const FieldTranslationEntry vpn_fields[] = {
89     { ::onc::vpn::kAutoConnect, shill::kAutoConnectProperty},
90     { ::onc::vpn::kHost, shill::kProviderHostProperty},
91     // This field is converted during translation, see onc_translator_*.
92     // { ::onc::vpn::kType, shill::kProviderTypeProperty },
93     {NULL}};
94
95 const FieldTranslationEntry wifi_fields[] = {
96     { ::onc::wifi::kAutoConnect, shill::kAutoConnectProperty},
97     { ::onc::wifi::kBSSID, shill::kWifiBSsid},
98     { ::onc::wifi::kFrequency, shill::kWifiFrequency},
99     { ::onc::wifi::kFrequencyList, shill::kWifiFrequencyListProperty},
100     { ::onc::wifi::kHiddenSSID, shill::kWifiHiddenSsid},
101     { ::onc::wifi::kPassphrase, shill::kPassphraseProperty},
102     // This field is converted during translation, see onc_translator_*.
103     // { ::onc::wifi::kSSID, shill::kWifiHexSsid},
104     // This field is converted during translation, see onc_translator_*.
105     // { ::onc::wifi::kSecurity, shill::kSecurityProperty },
106     { ::onc::wifi::kSignalStrength, shill::kSignalStrengthProperty},
107     {NULL}};
108
109 const FieldTranslationEntry cellular_apn_fields[] = {
110     { ::onc::cellular_apn::kName, shill::kApnProperty},
111     { ::onc::cellular_apn::kUsername, shill::kApnUsernameProperty},
112     { ::onc::cellular_apn::kPassword, shill::kApnPasswordProperty},
113     {NULL}};
114
115 const FieldTranslationEntry cellular_provider_fields[] = {
116     { ::onc::cellular_provider::kCode, shill::kOperatorCodeKey},
117     { ::onc::cellular_provider::kCountry, shill::kOperatorCountryKey},
118     { ::onc::cellular_provider::kName, shill::kOperatorNameKey},
119     {NULL}};
120
121 const FieldTranslationEntry cellular_fields[] = {
122     { ::onc::cellular::kActivateOverNonCellularNetwork,
123       shill::kActivateOverNonCellularNetworkProperty},
124     { ::onc::cellular::kActivationState, shill::kActivationStateProperty},
125     { ::onc::cellular::kAllowRoaming, shill::kCellularAllowRoamingProperty},
126     { ::onc::cellular::kCarrier, shill::kCarrierProperty},
127     { ::onc::cellular::kESN, shill::kEsnProperty},
128     { ::onc::cellular::kFamily, shill::kTechnologyFamilyProperty},
129     { ::onc::cellular::kFirmwareRevision, shill::kFirmwareRevisionProperty},
130     { ::onc::cellular::kFoundNetworks, shill::kFoundNetworksProperty},
131     { ::onc::cellular::kHardwareRevision, shill::kHardwareRevisionProperty},
132     { ::onc::cellular::kICCID, shill::kIccidProperty},
133     { ::onc::cellular::kIMEI, shill::kImeiProperty},
134     { ::onc::cellular::kIMSI, shill::kImsiProperty},
135     { ::onc::cellular::kManufacturer, shill::kManufacturerProperty},
136     { ::onc::cellular::kMDN, shill::kMdnProperty},
137     { ::onc::cellular::kMEID, shill::kMeidProperty},
138     { ::onc::cellular::kMIN, shill::kMinProperty},
139     { ::onc::cellular::kModelID, shill::kModelIDProperty},
140     { ::onc::cellular::kNetworkTechnology, shill::kNetworkTechnologyProperty},
141     { ::onc::cellular::kPRLVersion, shill::kPRLVersionProperty},
142     { ::onc::cellular::kProviderRequiresRoaming,
143       shill::kProviderRequiresRoamingProperty},
144     { ::onc::cellular::kRoamingState, shill::kRoamingStateProperty},
145     { ::onc::cellular::kSelectedNetwork, shill::kSelectedNetworkProperty},
146     { ::onc::cellular::kSIMLockStatus, shill::kSIMLockStatusProperty},
147     { ::onc::cellular::kSIMPresent, shill::kSIMPresentProperty},
148     { ::onc::cellular::kSupportedCarriers, shill::kSupportedCarriersProperty},
149     { ::onc::cellular::kSupportNetworkScan, shill::kSupportNetworkScanProperty},
150     {NULL}};
151
152 const FieldTranslationEntry network_fields[] = {
153     // Shill doesn't allow setting the name for non-VPN networks.
154     // This field is conditionally translated, see onc_translator_*.
155     // { ::onc::network_config::kName, shill::kNameProperty },
156     { ::onc::network_config::kGUID, shill::kGuidProperty},
157     // This field is converted during translation, see onc_translator_*.
158     // { ::onc::network_config::kType, shill::kTypeProperty },
159
160     // This field is converted during translation, see
161     // onc_translator_shill_to_onc.cc. It is only converted when going from
162     // Shill->ONC, and ignored otherwise.
163     // { ::onc::network_config::kConnectionState, shill::kStateProperty },
164     {NULL}};
165
166 struct OncValueTranslationEntry {
167   const OncValueSignature* onc_signature;
168   const FieldTranslationEntry* field_translation_table;
169 };
170
171 const OncValueTranslationEntry onc_value_translation_table[] = {
172   { &kEAPSignature, eap_fields },
173   { &kIPsecSignature, ipsec_fields },
174   { &kL2TPSignature, l2tp_fields },
175   { &kOpenVPNSignature, openvpn_fields },
176   { &kVPNSignature, vpn_fields },
177   { &kWiFiSignature, wifi_fields },
178   { &kWiFiWithStateSignature, wifi_fields },
179   { &kCellularApnSignature, cellular_apn_fields },
180   { &kCellularProviderSignature, cellular_provider_fields },
181   { &kCellularSignature, cellular_fields },
182   { &kCellularWithStateSignature, cellular_fields },
183   { &kNetworkWithStateSignature, network_fields },
184   { &kNetworkConfigurationSignature, network_fields },
185   { NULL }
186 };
187
188 struct NestedShillDictionaryEntry {
189   const OncValueSignature* onc_signature;
190   // NULL terminated list of Shill property keys.
191   const char* const* shill_property_path;
192 };
193
194 const char* cellular_apn_property_path_entries[] = {
195   shill::kCellularApnProperty,
196   NULL
197 };
198
199 const NestedShillDictionaryEntry nested_shill_dictionaries[] = {
200   { &kCellularApnSignature, cellular_apn_property_path_entries },
201   { NULL }
202 };
203
204 }  // namespace
205
206 const StringTranslationEntry kNetworkTypeTable[] = {
207     // This mapping is ensured in the translation code.
208     //  { network_type::kEthernet, shill::kTypeEthernet },
209     //  { network_type::kEthernet, shill::kTypeEthernetEap },
210     { ::onc::network_type::kWiFi, shill::kTypeWifi},
211     { ::onc::network_type::kCellular, shill::kTypeCellular},
212     { ::onc::network_type::kVPN, shill::kTypeVPN},
213     {NULL}};
214
215 const StringTranslationEntry kVPNTypeTable[] = {
216     { ::onc::vpn::kTypeL2TP_IPsec, shill::kProviderL2tpIpsec},
217     { ::onc::vpn::kOpenVPN, shill::kProviderOpenVpn}, {NULL}};
218
219 // The first matching line is chosen.
220 const StringTranslationEntry kWiFiSecurityTable[] = {
221     { ::onc::wifi::kNone, shill::kSecurityNone},
222     { ::onc::wifi::kWEP_PSK, shill::kSecurityWep},
223     { ::onc::wifi::kWPA_PSK, shill::kSecurityPsk},
224     { ::onc::wifi::kWPA_EAP, shill::kSecurity8021x},
225     { ::onc::wifi::kWPA_PSK, shill::kSecurityRsn},
226     { ::onc::wifi::kWPA_PSK, shill::kSecurityWpa},
227     {NULL}};
228
229 const StringTranslationEntry kEAPOuterTable[] = {
230     { ::onc::eap::kPEAP, shill::kEapMethodPEAP},
231     { ::onc::eap::kEAP_TLS, shill::kEapMethodTLS},
232     { ::onc::eap::kEAP_TTLS, shill::kEapMethodTTLS},
233     { ::onc::eap::kLEAP, shill::kEapMethodLEAP},
234     {NULL}};
235
236 // Translation of the EAP.Inner field in case of EAP.Outer == PEAP
237 const StringTranslationEntry kEAP_PEAP_InnerTable[] = {
238     { ::onc::eap::kMD5, shill::kEapPhase2AuthPEAPMD5},
239     { ::onc::eap::kMSCHAPv2, shill::kEapPhase2AuthPEAPMSCHAPV2}, {NULL}};
240
241 // Translation of the EAP.Inner field in case of EAP.Outer == TTLS
242 const StringTranslationEntry kEAP_TTLS_InnerTable[] = {
243     { ::onc::eap::kMD5, shill::kEapPhase2AuthTTLSMD5},
244     { ::onc::eap::kMSCHAPv2, shill::kEapPhase2AuthTTLSMSCHAPV2},
245     { ::onc::eap::kPAP, shill::kEapPhase2AuthTTLSPAP},
246     {NULL}};
247
248 const FieldTranslationEntry* GetFieldTranslationTable(
249     const OncValueSignature& onc_signature) {
250   for (const OncValueTranslationEntry* it = onc_value_translation_table;
251        it->onc_signature != NULL; ++it) {
252     if (it->onc_signature == &onc_signature)
253       return it->field_translation_table;
254   }
255   return NULL;
256 }
257
258 std::vector<std::string> GetPathToNestedShillDictionary(
259     const OncValueSignature& onc_signature) {
260   std::vector<std::string> shill_property_path;
261   for (const NestedShillDictionaryEntry* it = nested_shill_dictionaries;
262        it->onc_signature != NULL; ++it) {
263     if (it->onc_signature == &onc_signature) {
264       for (const char* const* key = it->shill_property_path; *key != NULL;
265            ++key) {
266         shill_property_path.push_back(std::string(*key));
267       }
268       break;
269     }
270   }
271   return shill_property_path;
272 }
273
274 bool GetShillPropertyName(const std::string& onc_field_name,
275                           const FieldTranslationEntry table[],
276                           std::string* shill_property_name) {
277   for (const FieldTranslationEntry* it = table;
278        it->onc_field_name != NULL; ++it) {
279     if (it->onc_field_name != onc_field_name)
280       continue;
281     *shill_property_name = it->shill_property_name;
282     return true;
283   }
284   return false;
285 }
286
287 bool TranslateStringToShill(const StringTranslationEntry table[],
288                             const std::string& onc_value,
289                             std::string* shill_value) {
290   for (int i = 0; table[i].onc_value != NULL; ++i) {
291     if (onc_value != table[i].onc_value)
292       continue;
293     *shill_value = table[i].shill_value;
294     return true;
295   }
296   LOG(ERROR) << "Value '" << onc_value << "' cannot be translated to Shill";
297   return false;
298 }
299
300 bool TranslateStringToONC(const StringTranslationEntry table[],
301                           const std::string& shill_value,
302                           std::string* onc_value) {
303   for (int i = 0; table[i].shill_value != NULL; ++i) {
304     if (shill_value != table[i].shill_value)
305       continue;
306     *onc_value = table[i].onc_value;
307     return true;
308   }
309   LOG(ERROR) << "Value '" << shill_value << "' cannot be translated to ONC";
310   return false;
311 }
312
313 }  // namespace onc
314 }  // namespace chromeos