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