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