OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/preg_parser_win.h" | 5 #include "chrome/browser/policy/preg_parser_win.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 | 9 |
10 #include <windows.h> | 10 #include <windows.h> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/files/memory_mapped_file.h" | 14 #include "base/files/memory_mapped_file.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/string16.h" | 17 #include "base/string16.h" |
18 #include "base/string_util.h" | 18 #include "base/string_util.h" |
19 #include "base/sys_byteorder.h" | 19 #include "base/sys_byteorder.h" |
20 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
21 #include "base/values.h" | 21 #include "base/values.h" |
| 22 #include "chrome/browser/policy/policy_load_status.h" |
22 | 23 |
23 namespace policy { | 24 namespace policy { |
24 namespace preg_parser { | 25 namespace preg_parser { |
25 | 26 |
26 // The magic header in PReg files: ASCII "PReg" + version (0x0001). | 27 // The magic header in PReg files: ASCII "PReg" + version (0x0001). |
27 const char kPolicyRegistryFileHeader[] = "PReg\x01\x00\x00\x00"; | 28 const char kPolicyRegistryFileHeader[] = "PReg\x01\x00\x00\x00"; |
28 | 29 |
29 // Maximum PReg file size we're willing to accept. | 30 // Maximum PReg file size we're willing to accept. |
30 const int64 kMaxPRegFileSize = 1024 * 1024 * 16; | 31 const int64 kMaxPRegFileSize = 1024 * 1024 * 16; |
31 | 32 |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 } else if (StartsWithASCII(action_trigger, kActionTriggerSecureKey, true) || | 208 } else if (StartsWithASCII(action_trigger, kActionTriggerSecureKey, true) || |
208 StartsWithASCII(action_trigger, kActionTriggerSoft, true)) { | 209 StartsWithASCII(action_trigger, kActionTriggerSoft, true)) { |
209 // Doesn't affect values. | 210 // Doesn't affect values. |
210 } else { | 211 } else { |
211 LOG(ERROR) << "Bad action trigger " << value_name; | 212 LOG(ERROR) << "Bad action trigger " << value_name; |
212 } | 213 } |
213 } | 214 } |
214 | 215 |
215 bool ReadFile(const base::FilePath& file_path, | 216 bool ReadFile(const base::FilePath& file_path, |
216 const string16& root, | 217 const string16& root, |
217 base::DictionaryValue* dict) { | 218 base::DictionaryValue* dict, |
| 219 PolicyLoadStatusSample* status) { |
218 base::MemoryMappedFile mapped_file; | 220 base::MemoryMappedFile mapped_file; |
219 if (!mapped_file.Initialize(file_path) || !mapped_file.IsValid()) { | 221 if (!mapped_file.Initialize(file_path) || !mapped_file.IsValid()) { |
220 PLOG(ERROR) << "Failed to map " << file_path.value(); | 222 PLOG(ERROR) << "Failed to map " << file_path.value(); |
| 223 status->Add(POLICY_LOAD_STATUS_READ_ERROR); |
221 return false; | 224 return false; |
222 } | 225 } |
223 | 226 |
224 if (mapped_file.length() > kMaxPRegFileSize) { | 227 if (mapped_file.length() > kMaxPRegFileSize) { |
225 LOG(ERROR) << "PReg file " << file_path.value() << " too large: " | 228 LOG(ERROR) << "PReg file " << file_path.value() << " too large: " |
226 << mapped_file.length(); | 229 << mapped_file.length(); |
| 230 status->Add(POLICY_LOAD_STATUS_TOO_BIG); |
227 return false; | 231 return false; |
228 } | 232 } |
229 | 233 |
230 // Check the header. | 234 // Check the header. |
231 const int kHeaderSize = arraysize(kPolicyRegistryFileHeader) - 1; | 235 const int kHeaderSize = arraysize(kPolicyRegistryFileHeader) - 1; |
232 if (mapped_file.length() < kHeaderSize || | 236 if (mapped_file.length() < kHeaderSize || |
233 memcmp(kPolicyRegistryFileHeader, mapped_file.data(), kHeaderSize) != 0) { | 237 memcmp(kPolicyRegistryFileHeader, mapped_file.data(), kHeaderSize) != 0) { |
234 LOG(ERROR) << "Bad policy file " << file_path.value(); | 238 LOG(ERROR) << "Bad policy file " << file_path.value(); |
| 239 status->Add(POLICY_LOAD_STATUS_PARSE_ERROR); |
235 return false; | 240 return false; |
236 } | 241 } |
237 | 242 |
238 // Parse file contents, which is UCS-2 and little-endian. The latter I | 243 // Parse file contents, which is UCS-2 and little-endian. The latter I |
239 // couldn't find documentation on, but the example I saw were all | 244 // couldn't find documentation on, but the example I saw were all |
240 // little-endian. It'd be interesting to check on big-endian hardware. | 245 // little-endian. It'd be interesting to check on big-endian hardware. |
241 const uint8* cursor = mapped_file.data() + kHeaderSize; | 246 const uint8* cursor = mapped_file.data() + kHeaderSize; |
242 const uint8* end = mapped_file.data() + mapped_file.length(); | 247 const uint8* end = mapped_file.data() + mapped_file.length(); |
243 while (true) { | 248 while (true) { |
244 if (cursor == end) | 249 if (cursor == end) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 if (current != kDelimBracketClose) | 293 if (current != kDelimBracketClose) |
289 break; | 294 break; |
290 | 295 |
291 // Process the record if it is within the |root| subtree. | 296 // Process the record if it is within the |root| subtree. |
292 if (StartsWith(key_name, root, false)) | 297 if (StartsWith(key_name, root, false)) |
293 HandleRecord(key_name.substr(root.size()), value, type, data, dict); | 298 HandleRecord(key_name.substr(root.size()), value, type, data, dict); |
294 } | 299 } |
295 | 300 |
296 LOG(ERROR) << "Error parsing " << file_path.value() << " at offset " | 301 LOG(ERROR) << "Error parsing " << file_path.value() << " at offset " |
297 << reinterpret_cast<const uint8*>(cursor - 1) - mapped_file.data(); | 302 << reinterpret_cast<const uint8*>(cursor - 1) - mapped_file.data(); |
| 303 status->Add(POLICY_LOAD_STATUS_PARSE_ERROR); |
298 return false; | 304 return false; |
299 } | 305 } |
300 | 306 |
301 } // namespace preg_parser | 307 } // namespace preg_parser |
302 } // namespace policy | 308 } // namespace policy |
OLD | NEW |