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