ronnie
2022-10-23 4bf14332546635f50a1bf7f3df4c0a8e29643280
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2016 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "libinstallattributes.h"
 
#include <base/files/file_util.h>
#include <base/logging.h>
 
#include "bindings/install_attributes.pb.h"
 
namespace {
 
// Written by cryptohome or by lockbox-cache after signature verification and
// thus guaranteed to be unadulterated.
const char kInstallAttributesPath[] = "/run/lockbox/install_attributes.pb";
 
}  // namespace
 
// The source of truth for these constants is Chromium
// //chrome/browser/chromeos/settings/install_attributes.cc.
const char InstallAttributesReader::kAttrMode[] = "enterprise.mode";
const char InstallAttributesReader::kDeviceModeConsumer[] = "consumer";
const char InstallAttributesReader::kDeviceModeEnterprise[] = "enterprise";
const char InstallAttributesReader::kDeviceModeEnterpriseAD[] = "enterprise_ad";
const char InstallAttributesReader::kDeviceModeLegacyRetail[] = "kiosk";
const char InstallAttributesReader::kDeviceModeConsumerKiosk[] =
    "consumer_kiosk";
 
InstallAttributesReader::InstallAttributesReader()
    : install_attributes_path_(kInstallAttributesPath) {
}
 
InstallAttributesReader::~InstallAttributesReader() {
}
 
const std::string& InstallAttributesReader::GetAttribute(
    const std::string& key) {
  // By its very nature of immutable attributes, once read successfully the
  // attributes can never change and thus never need reloading.
  if (!initialized_) {
    TryToLoad();
  }
 
  const auto entry = attributes_.find(key);
  if (entry == attributes_.end()) {
    return empty_string_;
  }
  return entry->second;
}
 
bool InstallAttributesReader::IsLocked() {
  if (!initialized_) {
    TryToLoad();
  }
  return initialized_;
}
 
void InstallAttributesReader::TryToLoad() {
  std::string contents;
  if (!base::ReadFileToString(install_attributes_path_, &contents)) {
    // May fail during OOBE or early in the boot process.
    return;
  }
 
  // Parse errors are unrecoverable (lockbox does atomic write), thus mark as
  // inititialized already before checking for parse errors.
  initialized_ = true;
 
  cryptohome::SerializedInstallAttributes install_attributes;
  if (!install_attributes.ParseFromString(contents)) {
    LOG(ERROR) << "Can't parse install attributes.";
    return;
  }
 
  for (int i = 0; i < install_attributes.attributes_size(); ++i) {
    const cryptohome::SerializedInstallAttributes_Attribute& attribute =
        install_attributes.attributes(i);
    // Cast value to C string and back to remove trailing zero.
    attributes_[attribute.name()] = std::string(attribute.value().c_str());
  }
}