| OLD | NEW |
| (Empty) |
| 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 "chrome/browser/policy/enterprise_install_attributes.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "chrome/browser/chromeos/cros/cryptohome_library.h" | |
| 12 #include "chrome/browser/policy/proto/install_attributes.pb.h" | |
| 13 #include "google_apis/gaia/gaia_auth_util.h" | |
| 14 | |
| 15 namespace policy { | |
| 16 | |
| 17 namespace { | |
| 18 // Constants for the possible device modes that can be stored in the lockbox. | |
| 19 const char kConsumerDeviceMode[] = "consumer"; | |
| 20 const char kEnterpiseDeviceMode[] = "enterprise"; | |
| 21 const char kKioskDeviceMode[] = "kiosk"; | |
| 22 const char kUnknownDeviceMode[] = "unknown"; | |
| 23 | |
| 24 // Field names in the lockbox. | |
| 25 const char kAttrEnterpriseDeviceId[] = "enterprise.device_id"; | |
| 26 const char kAttrEnterpriseDomain[] = "enterprise.domain"; | |
| 27 const char kAttrEnterpriseMode[] = "enterprise.mode"; | |
| 28 const char kAttrEnterpriseOwned[] = "enterprise.owned"; | |
| 29 const char kAttrEnterpriseUser[] = "enterprise.user"; | |
| 30 | |
| 31 // Translates DeviceMode constants to strings used in the lockbox. | |
| 32 std::string GetDeviceModeString(DeviceMode mode) { | |
| 33 switch (mode) { | |
| 34 case DEVICE_MODE_CONSUMER: | |
| 35 return kConsumerDeviceMode; | |
| 36 case DEVICE_MODE_ENTERPRISE: | |
| 37 return kEnterpiseDeviceMode; | |
| 38 case DEVICE_MODE_KIOSK: | |
| 39 return kKioskDeviceMode; | |
| 40 case DEVICE_MODE_PENDING: | |
| 41 case DEVICE_MODE_NOT_SET: | |
| 42 break; | |
| 43 } | |
| 44 NOTREACHED() << "Invalid device mode: " << mode; | |
| 45 return kUnknownDeviceMode; | |
| 46 } | |
| 47 | |
| 48 // Translates strings used in the lockbox to DeviceMode values. | |
| 49 DeviceMode GetDeviceModeFromString( | |
| 50 const std::string& mode) { | |
| 51 if (mode == kConsumerDeviceMode) | |
| 52 return DEVICE_MODE_CONSUMER; | |
| 53 else if (mode == kEnterpiseDeviceMode) | |
| 54 return DEVICE_MODE_ENTERPRISE; | |
| 55 else if (mode == kKioskDeviceMode) | |
| 56 return DEVICE_MODE_KIOSK; | |
| 57 NOTREACHED() << "Unknown device mode string: " << mode; | |
| 58 return DEVICE_MODE_NOT_SET; | |
| 59 } | |
| 60 | |
| 61 bool ReadMapKey(const std::map<std::string, std::string>& map, | |
| 62 const std::string& key, | |
| 63 std::string* value) { | |
| 64 std::map<std::string, std::string>::const_iterator entry = map.find(key); | |
| 65 if (entry == map.end()) | |
| 66 return false; | |
| 67 | |
| 68 *value = entry->second; | |
| 69 return true; | |
| 70 } | |
| 71 | |
| 72 } // namespace | |
| 73 | |
| 74 // Cache file name. | |
| 75 const FilePath::CharType EnterpriseInstallAttributes::kCacheFilePath[] = | |
| 76 FILE_PATH_LITERAL("/var/run/lockbox/install_attributes.pb"); | |
| 77 | |
| 78 EnterpriseInstallAttributes::EnterpriseInstallAttributes( | |
| 79 chromeos::CryptohomeLibrary* cryptohome) | |
| 80 : cryptohome_(cryptohome), | |
| 81 device_locked_(false), | |
| 82 registration_mode_(DEVICE_MODE_PENDING) {} | |
| 83 | |
| 84 void EnterpriseInstallAttributes::ReadCacheFile(const FilePath& cache_file) { | |
| 85 if (device_locked_ || !file_util::PathExists(cache_file)) | |
| 86 return; | |
| 87 | |
| 88 device_locked_ = true; | |
| 89 | |
| 90 char buf[16384]; | |
| 91 int len = file_util::ReadFile(cache_file, buf, sizeof(buf)); | |
| 92 if (len == -1 || len >= static_cast<int>(sizeof(buf))) { | |
| 93 PLOG(ERROR) << "Failed to read " << cache_file.value(); | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 cryptohome::SerializedInstallAttributes install_attrs_proto; | |
| 98 if (!install_attrs_proto.ParseFromArray(buf, len)) { | |
| 99 LOG(ERROR) << "Failed to parse install attributes cache"; | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 google::protobuf::RepeatedPtrField< | |
| 104 const cryptohome::SerializedInstallAttributes::Attribute>::iterator entry; | |
| 105 std::map<std::string, std::string> attr_map; | |
| 106 for (entry = install_attrs_proto.attributes().begin(); | |
| 107 entry != install_attrs_proto.attributes().end(); | |
| 108 ++entry) { | |
| 109 // The protobuf values unfortunately contain terminating null characters, so | |
| 110 // we have to sanitize the value here. | |
| 111 attr_map.insert(std::make_pair(entry->name(), | |
| 112 std::string(entry->value().c_str()))); | |
| 113 } | |
| 114 | |
| 115 DecodeInstallAttributes(attr_map); | |
| 116 } | |
| 117 | |
| 118 void EnterpriseInstallAttributes::ReadImmutableAttributes() { | |
| 119 if (device_locked_) | |
| 120 return; | |
| 121 | |
| 122 if (cryptohome_ && cryptohome_->InstallAttributesIsReady()) { | |
| 123 registration_mode_ = DEVICE_MODE_NOT_SET; | |
| 124 if (!cryptohome_->InstallAttributesIsInvalid() && | |
| 125 !cryptohome_->InstallAttributesIsFirstInstall()) { | |
| 126 device_locked_ = true; | |
| 127 | |
| 128 static const char* kEnterpriseAttributes[] = { | |
| 129 kAttrEnterpriseDeviceId, | |
| 130 kAttrEnterpriseDomain, | |
| 131 kAttrEnterpriseMode, | |
| 132 kAttrEnterpriseOwned, | |
| 133 kAttrEnterpriseUser, | |
| 134 }; | |
| 135 std::map<std::string, std::string> attr_map; | |
| 136 for (size_t i = 0; i < arraysize(kEnterpriseAttributes); ++i) { | |
| 137 std::string value; | |
| 138 if (cryptohome_->InstallAttributesGet(kEnterpriseAttributes[i], &value)) | |
| 139 attr_map[kEnterpriseAttributes[i]] = value; | |
| 140 } | |
| 141 | |
| 142 DecodeInstallAttributes(attr_map); | |
| 143 } | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 EnterpriseInstallAttributes::LockResult EnterpriseInstallAttributes::LockDevice( | |
| 148 const std::string& user, | |
| 149 DeviceMode device_mode, | |
| 150 const std::string& device_id) { | |
| 151 CHECK_NE(device_mode, DEVICE_MODE_PENDING); | |
| 152 CHECK_NE(device_mode, DEVICE_MODE_NOT_SET); | |
| 153 | |
| 154 std::string domain = gaia::ExtractDomainName(user); | |
| 155 | |
| 156 // Check for existing lock first. | |
| 157 if (device_locked_) { | |
| 158 return !registration_domain_.empty() && domain == registration_domain_ ? | |
| 159 LOCK_SUCCESS : LOCK_WRONG_USER; | |
| 160 } | |
| 161 | |
| 162 if (!cryptohome_ || !cryptohome_->InstallAttributesIsReady()) | |
| 163 return LOCK_NOT_READY; | |
| 164 | |
| 165 // Clearing the TPM password seems to be always a good deal. | |
| 166 if (cryptohome_->TpmIsEnabled() && | |
| 167 !cryptohome_->TpmIsBeingOwned() && | |
| 168 cryptohome_->TpmIsOwned()) { | |
| 169 cryptohome_->TpmClearStoredPassword(); | |
| 170 } | |
| 171 | |
| 172 // Make sure we really have a working InstallAttrs. | |
| 173 if (cryptohome_->InstallAttributesIsInvalid()) { | |
| 174 LOG(ERROR) << "Install attributes invalid."; | |
| 175 return LOCK_BACKEND_ERROR; | |
| 176 } | |
| 177 | |
| 178 if (!cryptohome_->InstallAttributesIsFirstInstall()) | |
| 179 return LOCK_WRONG_USER; | |
| 180 | |
| 181 std::string mode = GetDeviceModeString(device_mode); | |
| 182 | |
| 183 // Set values in the InstallAttrs and lock it. | |
| 184 if (!cryptohome_->InstallAttributesSet(kAttrEnterpriseOwned, "true") || | |
| 185 !cryptohome_->InstallAttributesSet(kAttrEnterpriseUser, user) || | |
| 186 !cryptohome_->InstallAttributesSet(kAttrEnterpriseDomain, domain) || | |
| 187 !cryptohome_->InstallAttributesSet(kAttrEnterpriseMode, mode) || | |
| 188 !cryptohome_->InstallAttributesSet(kAttrEnterpriseDeviceId, device_id)) { | |
| 189 LOG(ERROR) << "Failed writing attributes"; | |
| 190 return LOCK_BACKEND_ERROR; | |
| 191 } | |
| 192 | |
| 193 if (!cryptohome_->InstallAttributesFinalize() || | |
| 194 cryptohome_->InstallAttributesIsFirstInstall()) { | |
| 195 LOG(ERROR) << "Failed locking."; | |
| 196 return LOCK_BACKEND_ERROR; | |
| 197 } | |
| 198 | |
| 199 ReadImmutableAttributes(); | |
| 200 if (GetRegistrationUser() != user) { | |
| 201 LOG(ERROR) << "Locked data doesn't match"; | |
| 202 return LOCK_BACKEND_ERROR; | |
| 203 } | |
| 204 | |
| 205 return LOCK_SUCCESS; | |
| 206 } | |
| 207 | |
| 208 bool EnterpriseInstallAttributes::IsEnterpriseDevice() { | |
| 209 return device_locked_ && !registration_user_.empty(); | |
| 210 } | |
| 211 | |
| 212 std::string EnterpriseInstallAttributes::GetRegistrationUser() { | |
| 213 if (!device_locked_) | |
| 214 return std::string(); | |
| 215 | |
| 216 return registration_user_; | |
| 217 } | |
| 218 | |
| 219 std::string EnterpriseInstallAttributes::GetDomain() { | |
| 220 if (!IsEnterpriseDevice()) | |
| 221 return std::string(); | |
| 222 | |
| 223 return registration_domain_; | |
| 224 } | |
| 225 | |
| 226 std::string EnterpriseInstallAttributes::GetDeviceId() { | |
| 227 if (!IsEnterpriseDevice()) | |
| 228 return std::string(); | |
| 229 | |
| 230 return registration_device_id_; | |
| 231 } | |
| 232 | |
| 233 DeviceMode EnterpriseInstallAttributes::GetMode() { | |
| 234 return registration_mode_; | |
| 235 } | |
| 236 | |
| 237 void EnterpriseInstallAttributes::DecodeInstallAttributes( | |
| 238 const std::map<std::string, std::string>& attr_map) { | |
| 239 std::string enterprise_owned; | |
| 240 std::string enterprise_user; | |
| 241 if (ReadMapKey(attr_map, kAttrEnterpriseOwned, &enterprise_owned) && | |
| 242 ReadMapKey(attr_map, kAttrEnterpriseUser, &enterprise_user) && | |
| 243 enterprise_owned == "true" && | |
| 244 !enterprise_user.empty()) { | |
| 245 registration_user_ = gaia::CanonicalizeEmail(enterprise_user); | |
| 246 | |
| 247 // Initialize the mode to the legacy enterprise mode here and update | |
| 248 // below if more information is present. | |
| 249 registration_mode_ = DEVICE_MODE_ENTERPRISE; | |
| 250 | |
| 251 // If we could extract basic setting we should try to extract the | |
| 252 // extended ones too. We try to set these to defaults as good as | |
| 253 // as possible if present, which could happen for device enrolled in | |
| 254 // pre 19 revisions of the code, before these new attributes were added. | |
| 255 if (ReadMapKey(attr_map, kAttrEnterpriseDomain, ®istration_domain_)) | |
| 256 registration_domain_ = gaia::CanonicalizeDomain(registration_domain_); | |
| 257 else | |
| 258 registration_domain_ = gaia::ExtractDomainName(registration_user_); | |
| 259 | |
| 260 ReadMapKey(attr_map, kAttrEnterpriseDeviceId, ®istration_device_id_); | |
| 261 | |
| 262 std::string mode; | |
| 263 if (ReadMapKey(attr_map, kAttrEnterpriseMode, &mode)) | |
| 264 registration_mode_ = GetDeviceModeFromString(mode); | |
| 265 } else if (enterprise_user.empty() && enterprise_owned != "true") { | |
| 266 // |registration_user_| is empty on consumer devices. | |
| 267 registration_mode_ = DEVICE_MODE_CONSUMER; | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 } // namespace policy | |
| OLD | NEW |