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 #include "chrome/browser/policy/enterprise_install_attributes.h" | 5 #include "chrome/browser/policy/enterprise_install_attributes.h" |
6 | 6 |
7 #include "base/file_util.h" | |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "chrome/browser/chromeos/cros/cryptohome_library.h" | 9 #include "chrome/browser/chromeos/cros/cryptohome_library.h" |
10 #include "chrome/browser/policy/proto/install_attributes.pb.h" | |
9 #include "google_apis/gaia/gaia_auth_util.h" | 11 #include "google_apis/gaia/gaia_auth_util.h" |
10 | 12 |
11 namespace policy { | 13 namespace policy { |
12 | 14 |
13 namespace { | 15 namespace { |
14 // Constants for the possible device modes that can be stored in the lockbox. | 16 // Constants for the possible device modes that can be stored in the lockbox. |
15 const char kConsumerDeviceMode[] = "consumer"; | 17 const char kConsumerDeviceMode[] = "consumer"; |
16 const char kEnterpiseDeviceMode[] = "enterprise"; | 18 const char kEnterpiseDeviceMode[] = "enterprise"; |
17 const char kKioskDeviceMode[] = "kiosk"; | 19 const char kKioskDeviceMode[] = "kiosk"; |
18 const char kUnknownDeviceMode[] = "unknown"; | 20 const char kUnknownDeviceMode[] = "unknown"; |
(...skipping 28 matching lines...) Expand all Loading... | |
47 if (mode == kConsumerDeviceMode) | 49 if (mode == kConsumerDeviceMode) |
48 return DEVICE_MODE_CONSUMER; | 50 return DEVICE_MODE_CONSUMER; |
49 else if (mode == kEnterpiseDeviceMode) | 51 else if (mode == kEnterpiseDeviceMode) |
50 return DEVICE_MODE_ENTERPRISE; | 52 return DEVICE_MODE_ENTERPRISE; |
51 else if (mode == kKioskDeviceMode) | 53 else if (mode == kKioskDeviceMode) |
52 return DEVICE_MODE_KIOSK; | 54 return DEVICE_MODE_KIOSK; |
53 NOTREACHED() << "Unknown device mode string: " << mode; | 55 NOTREACHED() << "Unknown device mode string: " << mode; |
54 return DEVICE_MODE_NOT_SET; | 56 return DEVICE_MODE_NOT_SET; |
55 } | 57 } |
56 | 58 |
59 bool ReadMapKey(const std::map<std::string, std::string>& map, | |
60 const std::string& key, | |
61 std::string* value) { | |
62 std::map<std::string, std::string>::const_iterator entry = map.find(key); | |
63 if (entry == map.end()) | |
64 return false; | |
65 | |
66 *value = entry->second; | |
67 return true; | |
68 } | |
69 | |
57 } // namespace | 70 } // namespace |
58 | 71 |
72 // Cache file name. | |
73 const FilePath::CharType EnterpriseInstallAttributes::kCacheFilePath[] = | |
74 FILE_PATH_LITERAL("/var/run/lockbox/install_attributes.pb"); | |
75 | |
59 EnterpriseInstallAttributes::EnterpriseInstallAttributes( | 76 EnterpriseInstallAttributes::EnterpriseInstallAttributes( |
60 chromeos::CryptohomeLibrary* cryptohome) | 77 chromeos::CryptohomeLibrary* cryptohome) |
61 : cryptohome_(cryptohome), | 78 : cryptohome_(cryptohome), |
79 attrs_read_(false), | |
62 device_locked_(false), | 80 device_locked_(false), |
63 registration_mode_(DEVICE_MODE_PENDING) {} | 81 registration_mode_(DEVICE_MODE_PENDING) {} |
64 | 82 |
83 void EnterpriseInstallAttributes::ReadCacheFile(const FilePath& cache_file) { | |
84 attrs_read_ = true; | |
85 if (!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(), | |
Joao da Silva
2013/01/17 19:53:25
#include <utility>
Mattias Nissler (ping if slow)
2013/01/17 20:34:43
Done.
| |
112 std::string(entry->value().c_str()))); | |
113 } | |
114 | |
115 DecodeInstallAttributes(attr_map); | |
116 } | |
117 | |
65 EnterpriseInstallAttributes::LockResult EnterpriseInstallAttributes::LockDevice( | 118 EnterpriseInstallAttributes::LockResult EnterpriseInstallAttributes::LockDevice( |
66 const std::string& user, | 119 const std::string& user, |
67 DeviceMode device_mode, | 120 DeviceMode device_mode, |
68 const std::string& device_id) { | 121 const std::string& device_id) { |
69 CHECK_NE(device_mode, DEVICE_MODE_PENDING); | 122 CHECK_NE(device_mode, DEVICE_MODE_PENDING); |
70 CHECK_NE(device_mode, DEVICE_MODE_NOT_SET); | 123 CHECK_NE(device_mode, DEVICE_MODE_NOT_SET); |
71 | 124 |
72 std::string domain = gaia::ExtractDomainName(user); | 125 std::string domain = gaia::ExtractDomainName(user); |
73 | 126 |
74 // Check for existing lock first. | 127 // Check for existing lock first. |
(...skipping 27 matching lines...) Expand all Loading... | |
102 if (!cryptohome_->InstallAttributesSet(kAttrEnterpriseOwned, "true") || | 155 if (!cryptohome_->InstallAttributesSet(kAttrEnterpriseOwned, "true") || |
103 !cryptohome_->InstallAttributesSet(kAttrEnterpriseUser, user) || | 156 !cryptohome_->InstallAttributesSet(kAttrEnterpriseUser, user) || |
104 !cryptohome_->InstallAttributesSet(kAttrEnterpriseDomain, domain) || | 157 !cryptohome_->InstallAttributesSet(kAttrEnterpriseDomain, domain) || |
105 !cryptohome_->InstallAttributesSet(kAttrEnterpriseMode, mode) || | 158 !cryptohome_->InstallAttributesSet(kAttrEnterpriseMode, mode) || |
106 !cryptohome_->InstallAttributesSet(kAttrEnterpriseDeviceId, device_id)) { | 159 !cryptohome_->InstallAttributesSet(kAttrEnterpriseDeviceId, device_id)) { |
107 LOG(ERROR) << "Failed writing attributes"; | 160 LOG(ERROR) << "Failed writing attributes"; |
108 return LOCK_BACKEND_ERROR; | 161 return LOCK_BACKEND_ERROR; |
109 } | 162 } |
110 | 163 |
111 if (!cryptohome_->InstallAttributesFinalize() || | 164 if (!cryptohome_->InstallAttributesFinalize() || |
112 cryptohome_->InstallAttributesIsFirstInstall() || | 165 cryptohome_->InstallAttributesIsFirstInstall()) { |
113 GetRegistrationUser() != user) { | |
114 LOG(ERROR) << "Failed locking."; | 166 LOG(ERROR) << "Failed locking."; |
115 return LOCK_BACKEND_ERROR; | 167 return LOCK_BACKEND_ERROR; |
116 } | 168 } |
117 | 169 |
170 ReadImmutableAttributes(); | |
171 if (GetRegistrationUser() != user) { | |
172 LOG(ERROR) << "Locked data doesn't match"; | |
173 return LOCK_BACKEND_ERROR; | |
174 } | |
175 | |
118 return LOCK_SUCCESS; | 176 return LOCK_SUCCESS; |
119 } | 177 } |
120 | 178 |
121 bool EnterpriseInstallAttributes::IsEnterpriseDevice() { | 179 bool EnterpriseInstallAttributes::IsEnterpriseDevice() { |
122 ReadImmutableAttributes(); | |
123 return device_locked_ && !registration_user_.empty(); | 180 return device_locked_ && !registration_user_.empty(); |
Joao da Silva
2013/01/17 19:53:25
CHECK(attrs_read_)?
Mattias Nissler (ping if slow)
2013/01/17 20:34:43
I removed the entire attrs_read_ stuff, wasn't muc
| |
124 } | 181 } |
125 | 182 |
126 std::string EnterpriseInstallAttributes::GetRegistrationUser() { | 183 std::string EnterpriseInstallAttributes::GetRegistrationUser() { |
127 ReadImmutableAttributes(); | 184 CHECK(attrs_read_); |
128 | |
129 if (!device_locked_) | 185 if (!device_locked_) |
130 return std::string(); | 186 return std::string(); |
131 | 187 |
132 return registration_user_; | 188 return registration_user_; |
133 } | 189 } |
134 | 190 |
135 std::string EnterpriseInstallAttributes::GetDomain() { | 191 std::string EnterpriseInstallAttributes::GetDomain() { |
192 CHECK(attrs_read_); | |
136 if (!IsEnterpriseDevice()) | 193 if (!IsEnterpriseDevice()) |
137 return std::string(); | 194 return std::string(); |
138 | 195 |
139 return registration_domain_; | 196 return registration_domain_; |
140 } | 197 } |
141 | 198 |
142 std::string EnterpriseInstallAttributes::GetDeviceId() { | 199 std::string EnterpriseInstallAttributes::GetDeviceId() { |
200 CHECK(attrs_read_); | |
143 if (!IsEnterpriseDevice()) | 201 if (!IsEnterpriseDevice()) |
144 return std::string(); | 202 return std::string(); |
145 | 203 |
146 return registration_device_id_; | 204 return registration_device_id_; |
147 } | 205 } |
148 | 206 |
149 DeviceMode EnterpriseInstallAttributes::GetMode() { | 207 DeviceMode EnterpriseInstallAttributes::GetMode() { |
150 ReadImmutableAttributes(); | 208 CHECK(attrs_read_); |
151 return registration_mode_; | 209 return registration_mode_; |
152 } | 210 } |
153 | 211 |
154 void EnterpriseInstallAttributes::ReadImmutableAttributes() { | 212 void EnterpriseInstallAttributes::ReadImmutableAttributes() { |
155 if (device_locked_) | 213 if (device_locked_) |
156 return; | 214 return; |
157 | 215 |
158 if (cryptohome_ && cryptohome_->InstallAttributesIsReady()) { | 216 if (cryptohome_ && cryptohome_->InstallAttributesIsReady()) { |
159 registration_mode_ = DEVICE_MODE_NOT_SET; | 217 registration_mode_ = DEVICE_MODE_NOT_SET; |
160 if (!cryptohome_->InstallAttributesIsInvalid() && | 218 if (!cryptohome_->InstallAttributesIsInvalid() && |
161 !cryptohome_->InstallAttributesIsFirstInstall()) { | 219 !cryptohome_->InstallAttributesIsFirstInstall()) { |
220 attrs_read_ = true; | |
Joao da Silva
2013/01/17 19:53:25
It's possible that locking the device fails and th
Mattias Nissler (ping if slow)
2013/01/17 20:34:43
The flag was meant to catch reads before ReadCache
| |
162 device_locked_ = true; | 221 device_locked_ = true; |
163 std::string enterprise_owned; | |
164 std::string enterprise_user; | |
165 if (cryptohome_->InstallAttributesGet(kAttrEnterpriseOwned, | |
166 &enterprise_owned) && | |
167 cryptohome_->InstallAttributesGet(kAttrEnterpriseUser, | |
168 &enterprise_user) && | |
169 enterprise_owned == "true" && | |
170 !enterprise_user.empty()) { | |
171 registration_user_ = gaia::CanonicalizeEmail(enterprise_user); | |
172 | 222 |
173 // Initialize the mode to the legacy enterprise mode here and update | 223 static const char* kEnterpriseAttributes[] = { |
174 // below if more information is present. | 224 kAttrEnterpriseDeviceId, |
175 registration_mode_ = DEVICE_MODE_ENTERPRISE; | 225 kAttrEnterpriseDomain, |
226 kAttrEnterpriseMode, | |
227 kAttrEnterpriseOwned, | |
228 kAttrEnterpriseUser, | |
229 }; | |
230 std::map<std::string, std::string> attr_map; | |
231 for (size_t i = 0; i < arraysize(kEnterpriseAttributes); ++i) { | |
232 std::string value; | |
233 if (cryptohome_->InstallAttributesGet(kEnterpriseAttributes[i], &value)) | |
234 attr_map[kEnterpriseAttributes[i]] = value; | |
235 } | |
176 | 236 |
177 // If we could extract basic setting we should try to extract the | 237 DecodeInstallAttributes(attr_map); |
178 // extended ones too. We try to set these to defaults as good as | |
179 // as possible if present, which could happen for device enrolled in | |
180 // pre 19 revisions of the code, before these new attributes were added. | |
181 if (cryptohome_->InstallAttributesGet(kAttrEnterpriseDomain, | |
182 ®istration_domain_)) { | |
183 registration_domain_ = gaia::CanonicalizeDomain(registration_domain_); | |
184 } else { | |
185 registration_domain_ = gaia::ExtractDomainName(registration_user_); | |
186 } | |
187 if (!cryptohome_->InstallAttributesGet(kAttrEnterpriseDeviceId, | |
188 ®istration_device_id_)) { | |
189 registration_device_id_.clear(); | |
190 } | |
191 std::string mode; | |
192 if (cryptohome_->InstallAttributesGet(kAttrEnterpriseMode, &mode)) | |
193 registration_mode_ = GetDeviceModeFromString(mode); | |
194 } else if (enterprise_user.empty() && enterprise_owned != "true") { | |
195 // |registration_user_| is empty on consumer devices. | |
196 registration_mode_ = DEVICE_MODE_CONSUMER; | |
197 } | |
198 } | 238 } |
199 } | 239 } |
200 } | 240 } |
201 | 241 |
242 void EnterpriseInstallAttributes::DecodeInstallAttributes( | |
243 const std::map<std::string, std::string>& attr_map) { | |
244 std::string enterprise_owned; | |
245 std::string enterprise_user; | |
246 if (ReadMapKey(attr_map, kAttrEnterpriseOwned, &enterprise_owned) && | |
247 ReadMapKey(attr_map, kAttrEnterpriseUser, &enterprise_user) && | |
248 enterprise_owned == "true" && | |
249 !enterprise_user.empty()) { | |
250 registration_user_ = gaia::CanonicalizeEmail(enterprise_user); | |
251 | |
252 // Initialize the mode to the legacy enterprise mode here and update | |
253 // below if more information is present. | |
254 registration_mode_ = DEVICE_MODE_ENTERPRISE; | |
255 | |
256 // If we could extract basic setting we should try to extract the | |
257 // extended ones too. We try to set these to defaults as good as | |
258 // as possible if present, which could happen for device enrolled in | |
259 // pre 19 revisions of the code, before these new attributes were added. | |
260 if (ReadMapKey(attr_map, kAttrEnterpriseDomain, ®istration_domain_)) | |
261 registration_domain_ = gaia::CanonicalizeDomain(registration_domain_); | |
262 else | |
263 registration_domain_ = gaia::ExtractDomainName(registration_user_); | |
264 | |
265 ReadMapKey(attr_map, kAttrEnterpriseDeviceId, ®istration_device_id_); | |
266 | |
267 std::string mode; | |
268 if (ReadMapKey(attr_map, kAttrEnterpriseMode, &mode)) | |
269 registration_mode_ = GetDeviceModeFromString(mode); | |
270 } else if (enterprise_user.empty() && enterprise_owned != "true") { | |
271 // |registration_user_| is empty on consumer devices. | |
272 registration_mode_ = DEVICE_MODE_CONSUMER; | |
273 } | |
274 } | |
275 | |
202 } // namespace policy | 276 } // namespace policy |
OLD | NEW |