onc_translation_tables.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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
22namespace {
23
24const FieldTranslationEntry eap_fields[] = {
25    { ::onc::eap::kAnonymousIdentity, shill::kEapAnonymousIdentityProperty},
26    { ::onc::eap::kIdentity, shill::kEapIdentityProperty},
27    // This field is converted during translation, see onc_translator_*.
28    // { ::onc::eap::kInner, shill::kEapPhase2AuthProperty },
29
30    // This field is converted during translation, see onc_translator_*.
31    // { ::onc::eap::kOuter, shill::kEapMethodProperty },
32    { ::onc::eap::kPassword, shill::kEapPasswordProperty},
33    { ::onc::eap::kSaveCredentials, shill::kSaveCredentialsProperty},
34    { ::onc::eap::kServerCAPEMs, shill::kEapCaCertPemProperty},
35    { ::onc::eap::kUseSystemCAs, shill::kEapUseSystemCasProperty},
36    {NULL}};
37
38const FieldTranslationEntry ipsec_fields[] = {
39    // Ignored by Shill, not necessary to synchronize.
40    // { ::onc::ipsec::kAuthenticationType, shill::kL2tpIpsecAuthenticationType
41    // },
42    { ::onc::ipsec::kGroup, shill::kL2tpIpsecTunnelGroupProperty},
43    // Ignored by Shill, not necessary to synchronize.
44    // { ::onc::ipsec::kIKEVersion, shill::kL2tpIpsecIkeVersion },
45    { ::onc::ipsec::kPSK, shill::kL2tpIpsecPskProperty},
46    { ::onc::vpn::kSaveCredentials, shill::kSaveCredentialsProperty},
47    { ::onc::ipsec::kServerCAPEMs, shill::kL2tpIpsecCaCertPemProperty},
48    {NULL}};
49
50const FieldTranslationEntry xauth_fields[] = {
51    { ::onc::vpn::kPassword, shill::kL2tpIpsecXauthPasswordProperty},
52    { ::onc::vpn::kUsername, shill::kL2tpIpsecXauthUserProperty},
53    {NULL}};
54
55const FieldTranslationEntry l2tp_fields[] = {
56    { ::onc::vpn::kPassword, shill::kL2tpIpsecPasswordProperty},
57    // We don't synchronize l2tp's SaveCredentials field for now, as Shill
58    // doesn't
59    // support separate settings for ipsec and l2tp.
60    // { ::onc::vpn::kSaveCredentials, &kBoolSignature },
61    { ::onc::vpn::kUsername, shill::kL2tpIpsecUserProperty}, {NULL}};
62
63const FieldTranslationEntry openvpn_fields[] = {
64    { ::onc::openvpn::kAuth, shill::kOpenVPNAuthProperty},
65    { ::onc::openvpn::kAuthNoCache, shill::kOpenVPNAuthNoCacheProperty},
66    { ::onc::openvpn::kAuthRetry, shill::kOpenVPNAuthRetryProperty},
67    { ::onc::openvpn::kCipher, shill::kOpenVPNCipherProperty},
68    { ::onc::openvpn::kCompLZO, shill::kOpenVPNCompLZOProperty},
69    { ::onc::openvpn::kCompNoAdapt, shill::kOpenVPNCompNoAdaptProperty},
70    { ::onc::openvpn::kIgnoreDefaultRoute,
71      shill::kOpenVPNIgnoreDefaultRouteProperty},
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
133// This must only contain Service properties and not Device properties.
134// For Device properties see kCellularDeviceTable.
135const FieldTranslationEntry cellular_fields[] = {
136    { ::onc::cellular::kActivateOverNonCellularNetwork,
137      shill::kActivateOverNonCellularNetworkProperty},
138    { ::onc::cellular::kActivationState, shill::kActivationStateProperty},
139    { ::onc::cellular::kNetworkTechnology, shill::kNetworkTechnologyProperty},
140    { ::onc::cellular::kRoamingState, shill::kRoamingStateProperty},
141    {NULL}};
142
143const FieldTranslationEntry network_fields[] = {
144    { ::onc::network_config::kGUID, shill::kGuidProperty},
145    { ::onc::network_config::kConnectable, shill::kConnectableProperty },
146    { ::onc::network_config::kErrorState, shill::kErrorProperty },
147
148    // Shill doesn't allow setting the name for non-VPN networks.
149    // Name is conditionally translated, see onc_translator_*.
150    // { ::onc::network_config::kName, shill::kNameProperty },
151
152    // Type is converted during translation, see onc_translator_*.
153    // { ::onc::network_config::kType, shill::kTypeProperty },
154
155    // These fields are converted during translation, see
156    // onc_translator_shill_to_onc.cc. They are only converted when going from
157    // Shill->ONC, and ignored otherwise.
158    // { ::onc::network_config::kConnectionState, shill::kStateProperty },
159    // { ::onc::network_config::kMacAddress, shill::kAddressProperty },
160    {NULL}};
161
162const FieldTranslationEntry ipconfig_fields[] = {
163    { ::onc::ipconfig::kIPAddress, shill::kAddressProperty},
164    { ::onc::ipconfig::kGateway, shill::kGatewayProperty},
165    { ::onc::ipconfig::kRoutingPrefix, shill::kPrefixlenProperty},
166    { ::onc::ipconfig::kNameServers, shill::kNameServersProperty},
167    // This field is converted during translation, see ShillToONCTranslator::
168    // TranslateIPConfig. It is only converted from Shill->ONC.
169    // { ::onc::ipconfig::kType, shill::kMethodProperty},
170    {NULL}};
171
172struct OncValueTranslationEntry {
173  const OncValueSignature* onc_signature;
174  const FieldTranslationEntry* field_translation_table;
175};
176
177const OncValueTranslationEntry onc_value_translation_table[] = {
178  { &kEAPSignature, eap_fields },
179  { &kIPsecSignature, ipsec_fields },
180  { &kL2TPSignature, l2tp_fields },
181  { &kXAUTHSignature, xauth_fields },
182  { &kOpenVPNSignature, openvpn_fields },
183  { &kVerifyX509Signature, verify_x509_fields },
184  { &kVPNSignature, vpn_fields },
185  { &kWiFiSignature, wifi_fields },
186  { &kWiFiWithStateSignature, wifi_fields },
187  { &kCellularApnSignature, cellular_apn_fields },
188  { &kCellularProviderSignature, cellular_provider_fields },
189  { &kCellularSignature, cellular_fields },
190  { &kCellularWithStateSignature, cellular_fields },
191  { &kNetworkWithStateSignature, network_fields },
192  { &kNetworkConfigurationSignature, network_fields },
193  { &kIPConfigSignature, ipconfig_fields },
194  { NULL }
195};
196
197struct NestedShillDictionaryEntry {
198  const OncValueSignature* onc_signature;
199  // NULL terminated list of Shill property keys.
200  const char* const* shill_property_path;
201};
202
203const char* cellular_apn_property_path_entries[] = {
204  shill::kCellularApnProperty,
205  NULL
206};
207
208const NestedShillDictionaryEntry nested_shill_dictionaries[] = {
209  { &kCellularApnSignature, cellular_apn_property_path_entries },
210  { NULL }
211};
212
213}  // namespace
214
215const StringTranslationEntry kNetworkTypeTable[] = {
216    // This mapping is ensured in the translation code.
217    //  { network_type::kEthernet, shill::kTypeEthernet },
218    //  { network_type::kEthernet, shill::kTypeEthernetEap },
219    { ::onc::network_type::kWiFi, shill::kTypeWifi},
220    { ::onc::network_type::kCellular, shill::kTypeCellular},
221    { ::onc::network_type::kVPN, shill::kTypeVPN},
222    {NULL}};
223
224const StringTranslationEntry kVPNTypeTable[] = {
225    { ::onc::vpn::kTypeL2TP_IPsec, shill::kProviderL2tpIpsec},
226    { ::onc::vpn::kOpenVPN, shill::kProviderOpenVpn}, {NULL}};
227
228// The first matching line is chosen.
229const StringTranslationEntry kWiFiSecurityTable[] = {
230    { ::onc::wifi::kNone, shill::kSecurityNone},
231    { ::onc::wifi::kWEP_PSK, shill::kSecurityWep},
232    { ::onc::wifi::kWPA_PSK, shill::kSecurityPsk},
233    { ::onc::wifi::kWPA_EAP, shill::kSecurity8021x},
234    { ::onc::wifi::kWPA_PSK, shill::kSecurityRsn},
235    { ::onc::wifi::kWPA_PSK, shill::kSecurityWpa},
236    {NULL}};
237
238const StringTranslationEntry kEAPOuterTable[] = {
239    { ::onc::eap::kPEAP, shill::kEapMethodPEAP},
240    { ::onc::eap::kEAP_TLS, shill::kEapMethodTLS},
241    { ::onc::eap::kEAP_TTLS, shill::kEapMethodTTLS},
242    { ::onc::eap::kLEAP, shill::kEapMethodLEAP},
243    {NULL}};
244
245// Translation of the EAP.Inner field in case of EAP.Outer == PEAP
246const StringTranslationEntry kEAP_PEAP_InnerTable[] = {
247    { ::onc::eap::kMD5, shill::kEapPhase2AuthPEAPMD5},
248    { ::onc::eap::kMSCHAPv2, shill::kEapPhase2AuthPEAPMSCHAPV2}, {NULL}};
249
250// Translation of the EAP.Inner field in case of EAP.Outer == TTLS
251const StringTranslationEntry kEAP_TTLS_InnerTable[] = {
252    { ::onc::eap::kMD5, shill::kEapPhase2AuthTTLSMD5},
253    { ::onc::eap::kMSCHAPv2, shill::kEapPhase2AuthTTLSMSCHAPV2},
254    { ::onc::eap::kPAP, shill::kEapPhase2AuthTTLSPAP},
255    {NULL}};
256
257// This must contain only Shill Device properties and no Service properties.
258// For Service properties see cellular_fields.
259const FieldTranslationEntry kCellularDeviceTable[] = {
260    { ::onc::cellular::kAllowRoaming, shill::kCellularAllowRoamingProperty},
261    { ::onc::cellular::kCarrier, shill::kCarrierProperty},
262    { ::onc::cellular::kESN, shill::kEsnProperty},
263    { ::onc::cellular::kFamily, shill::kTechnologyFamilyProperty},
264    { ::onc::cellular::kFirmwareRevision, shill::kFirmwareRevisionProperty},
265    { ::onc::cellular::kFoundNetworks, shill::kFoundNetworksProperty},
266    { ::onc::cellular::kHardwareRevision, shill::kHardwareRevisionProperty},
267    { ::onc::cellular::kHomeProvider, shill::kHomeProviderProperty},
268    { ::onc::cellular::kICCID, shill::kIccidProperty},
269    { ::onc::cellular::kIMEI, shill::kImeiProperty},
270    { ::onc::cellular::kIMSI, shill::kImsiProperty},
271    { ::onc::cellular::kManufacturer, shill::kManufacturerProperty},
272    { ::onc::cellular::kMDN, shill::kMdnProperty},
273    { ::onc::cellular::kMEID, shill::kMeidProperty},
274    { ::onc::cellular::kMIN, shill::kMinProperty},
275    { ::onc::cellular::kModelID, shill::kModelIDProperty},
276    { ::onc::cellular::kPRLVersion, shill::kPRLVersionProperty},
277    { ::onc::cellular::kProviderRequiresRoaming,
278      shill::kProviderRequiresRoamingProperty},
279    { ::onc::cellular::kSelectedNetwork, shill::kSelectedNetworkProperty},
280    { ::onc::cellular::kSIMLockEnabled, shill::kSIMLockEnabledProperty},
281    { ::onc::cellular::kSIMLockStatus, shill::kSIMLockStatusProperty},
282    { ::onc::cellular::kSIMLockType, shill::kSIMLockTypeProperty},
283    { ::onc::cellular::kSIMPresent, shill::kSIMPresentProperty},
284    { ::onc::cellular::kSupportedCarriers, shill::kSupportedCarriersProperty},
285    { ::onc::cellular::kSupportNetworkScan, shill::kSupportNetworkScanProperty},
286    {NULL}};
287
288const FieldTranslationEntry* GetFieldTranslationTable(
289    const OncValueSignature& onc_signature) {
290  for (const OncValueTranslationEntry* it = onc_value_translation_table;
291       it->onc_signature != NULL; ++it) {
292    if (it->onc_signature == &onc_signature)
293      return it->field_translation_table;
294  }
295  return NULL;
296}
297
298std::vector<std::string> GetPathToNestedShillDictionary(
299    const OncValueSignature& onc_signature) {
300  std::vector<std::string> shill_property_path;
301  for (const NestedShillDictionaryEntry* it = nested_shill_dictionaries;
302       it->onc_signature != NULL; ++it) {
303    if (it->onc_signature == &onc_signature) {
304      for (const char* const* key = it->shill_property_path; *key != NULL;
305           ++key) {
306        shill_property_path.push_back(std::string(*key));
307      }
308      break;
309    }
310  }
311  return shill_property_path;
312}
313
314bool GetShillPropertyName(const std::string& onc_field_name,
315                          const FieldTranslationEntry table[],
316                          std::string* shill_property_name) {
317  for (const FieldTranslationEntry* it = table;
318       it->onc_field_name != NULL; ++it) {
319    if (it->onc_field_name != onc_field_name)
320      continue;
321    *shill_property_name = it->shill_property_name;
322    return true;
323  }
324  return false;
325}
326
327bool TranslateStringToShill(const StringTranslationEntry table[],
328                            const std::string& onc_value,
329                            std::string* shill_value) {
330  for (int i = 0; table[i].onc_value != NULL; ++i) {
331    if (onc_value != table[i].onc_value)
332      continue;
333    *shill_value = table[i].shill_value;
334    return true;
335  }
336  LOG(ERROR) << "Value '" << onc_value << "' cannot be translated to Shill";
337  return false;
338}
339
340bool TranslateStringToONC(const StringTranslationEntry table[],
341                          const std::string& shill_value,
342                          std::string* onc_value) {
343  for (int i = 0; table[i].shill_value != NULL; ++i) {
344    if (shill_value != table[i].shill_value)
345      continue;
346    *onc_value = table[i].onc_value;
347    return true;
348  }
349  LOG(ERROR) << "Value '" << shill_value << "' cannot be translated to ONC";
350  return false;
351}
352
353}  // namespace onc
354}  // namespace chromeos
355