File: device_name_policy_handler_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (230 lines) | stat: -rw-r--r-- 8,775 bytes parent folder | download | duplicates (6)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/policy/handlers/device_name_policy_handler_impl.h"

#include <string_view>

#include "ash/constants/ash_features.h"
#include "base/functional/bind.h"
#include "base/strings/string_util.h"
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/ash/policy/handlers/device_name_policy_handler_name_generator.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chromeos/ash/components/install_attributes/install_attributes.h"
#include "chromeos/ash/components/network/device_state.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "chromeos/ash/components/settings/cros_settings_provider.h"

namespace policy {

namespace {

// By default, device name policy should be kPolicyHostnameNotConfigurable for
// managed devices and kNoPolicy for unmanaged devices.
DeviceNamePolicyHandler::DeviceNamePolicy ComputeInitialPolicy() {
  if (ash::InstallAttributes::Get()->IsEnterpriseManaged()) {
    // We assume that the device name is not configurable unless/until we know
    // about any policies that are set.
    return DeviceNamePolicyHandler::DeviceNamePolicy::
        kPolicyHostnameNotConfigurable;
  }

  return DeviceNamePolicyHandler::DeviceNamePolicy::kNoPolicy;
}

}  // namespace

DeviceNamePolicyHandlerImpl::DeviceNamePolicyHandlerImpl(
    ash::CrosSettings* cros_settings)
    : DeviceNamePolicyHandlerImpl(
          cros_settings,
          ash::system::StatisticsProvider::GetInstance(),
          ash::NetworkHandler::Get()->network_state_handler()) {}

DeviceNamePolicyHandlerImpl::DeviceNamePolicyHandlerImpl(
    ash::CrosSettings* cros_settings,
    ash::system::StatisticsProvider* statistics_provider,
    ash::NetworkStateHandler* handler)
    : cros_settings_(cros_settings),
      statistics_provider_(statistics_provider),
      handler_(handler),
      device_name_policy_(ComputeInitialPolicy()) {
  template_policy_subscription_ = cros_settings_->AddSettingsObserver(
      ash::kDeviceHostnameTemplate,
      base::BindRepeating(
          &DeviceNamePolicyHandlerImpl::OnDeviceHostnamePropertyChanged,
          weak_factory_.GetWeakPtr()));
  configurable_policy_subscription_ = cros_settings_->AddSettingsObserver(
      ash::kDeviceHostnameUserConfigurable,
      base::BindRepeating(
          &DeviceNamePolicyHandlerImpl::OnDeviceHostnamePropertyChanged,
          weak_factory_.GetWeakPtr()));

  network_state_handler_observer_.Observe(
      ash::NetworkHandler::Get()->network_state_handler());

  // Fire it once so we're sure we get an invocation on startup.
  OnDeviceHostnamePropertyChanged();
}

DeviceNamePolicyHandlerImpl::~DeviceNamePolicyHandlerImpl() = default;

DeviceNamePolicyHandler::DeviceNamePolicy
DeviceNamePolicyHandlerImpl::GetDeviceNamePolicy() const {
  return device_name_policy_;
}

std::optional<std::string>
DeviceNamePolicyHandlerImpl::GetHostnameChosenByAdministrator() const {
  if (GetDeviceNamePolicy() == DeviceNamePolicy::kPolicyHostnameChosenByAdmin) {
    return hostname_;
  }
  return std::nullopt;
}

void DeviceNamePolicyHandlerImpl::DefaultNetworkChanged(
    const ash::NetworkState* network) {
  OnDeviceHostnamePropertyChanged();
}

void DeviceNamePolicyHandlerImpl::OnShuttingDown() {
  network_state_handler_observer_.Reset();
}

void DeviceNamePolicyHandlerImpl::OnDeviceHostnamePropertyChanged() {
  ash::CrosSettingsProvider::TrustedStatus status =
      cros_settings_->PrepareTrustedValues(base::BindOnce(
          &DeviceNamePolicyHandlerImpl::OnDeviceHostnamePropertyChanged,
          weak_factory_.GetWeakPtr()));
  if (status != ash::CrosSettingsProvider::TRUSTED)
    return;

  // Continue when machine statistics are loaded, to avoid blocking.
  statistics_provider_->ScheduleOnMachineStatisticsLoaded(base::BindOnce(
      &DeviceNamePolicyHandlerImpl::
          OnDeviceHostnamePropertyChangedAndMachineStatisticsLoaded,
      weak_factory_.GetWeakPtr()));
}

void DeviceNamePolicyHandlerImpl::
    OnDeviceHostnamePropertyChangedAndMachineStatisticsLoaded() {
  std::string hostname_template;
  DeviceNamePolicy policy = ComputePolicy(&hostname_template);

  std::string new_hostname;
  if (policy == DeviceNamePolicy::kPolicyHostnameChosenByAdmin) {
    new_hostname = GenerateHostname(hostname_template);
  }

  SetDeviceNamePolicy(policy, new_hostname);
}

DeviceNamePolicyHandler::DeviceNamePolicy
DeviceNamePolicyHandlerImpl::ComputePolicy(std::string* hostname_template_out) {
  if (cros_settings_->GetString(ash::kDeviceHostnameTemplate,
                                hostname_template_out)) {
    // Do not set an empty hostname (which would overwrite any custom hostname
    // set) if DeviceHostnameTemplate is not specified by policy.
    // No policy is set for administrator to choose hostname.
    return DeviceNamePolicy::kPolicyHostnameChosenByAdmin;
  }

  bool hostname_user_configurable;
  if (ash::features::IsHostnameSettingEnabled() &&
      cros_settings_->GetBoolean(ash::kDeviceHostnameUserConfigurable,
                                 &hostname_user_configurable)) {
    return hostname_user_configurable
               ? DeviceNamePolicy::kPolicyHostnameConfigurableByManagedUser
               : DeviceNamePolicy::kPolicyHostnameNotConfigurable;
  }

  // If no policies are set, device name policy should be
  // kPolicyHostnameNotConfigurable for managed devices and kNoPolicy for
  // unmanaged devices.
  if (ash::InstallAttributes::Get()->IsEnterpriseManaged())
    return DeviceNamePolicy::kPolicyHostnameNotConfigurable;

  return DeviceNamePolicy::kNoPolicy;
}

std::string DeviceNamePolicyHandlerImpl::GenerateHostname(
    const std::string& hostname_template) const {
  const std::string_view serial =
      statistics_provider_->GetMachineID().value_or(std::string_view());

  const std::string asset_id = g_browser_process->platform_part()
                                   ->browser_policy_connector_ash()
                                   ->GetDeviceAssetID();

  const std::string machine_name = g_browser_process->platform_part()
                                       ->browser_policy_connector_ash()
                                       ->GetMachineName();

  const std::string location = g_browser_process->platform_part()
                                   ->browser_policy_connector_ash()
                                   ->GetDeviceAnnotatedLocation();
  std::string mac = "MAC_unknown";
  const ash::NetworkState* network = handler_->DefaultNetwork();
  if (network) {
    const ash::DeviceState* device =
        handler_->GetDeviceState(network->device_path());
    if (device) {
      mac = device->mac_address();
      base::ReplaceSubstringsAfterOffset(&mac, 0, ":", "");
    }
  }

  return FormatHostname(hostname_template, asset_id, serial, mac, machine_name,
                        location);
}

void DeviceNamePolicyHandlerImpl::SetDeviceNamePolicy(
    DeviceNamePolicy policy,
    const std::string& new_hostname) {
  if (device_name_policy_ == policy && hostname_ == new_hostname)
    return;

  // If the hostname has changed, set it using NetworkStateHandler. Note that
  // this process is skipped when the hostname settings flag is enabled since
  // this is handled elsewhere. See https://crbug.com/126802.
  if (!ash::features::IsHostnameSettingEnabled() &&
      policy == DeviceNamePolicy::kPolicyHostnameChosenByAdmin &&
      hostname_ != new_hostname) {
    handler_->SetHostname(new_hostname);
  }

  device_name_policy_ = policy;
  hostname_ = new_hostname;
  NotifyHostnamePolicyChanged();
}

std::ostream& operator<<(
    std::ostream& stream,
    const DeviceNamePolicyHandlerImpl::DeviceNamePolicy& state) {
  switch (state) {
    case DeviceNamePolicyHandlerImpl::DeviceNamePolicy::kNoPolicy:
      stream << "[No policy]";
      break;
    case DeviceNamePolicyHandlerImpl::DeviceNamePolicy::
        kPolicyHostnameChosenByAdmin:
      stream << "[Admin chooses hostname template]";
      break;
    case DeviceNamePolicyHandlerImpl::DeviceNamePolicy::
        kPolicyHostnameConfigurableByManagedUser:
      stream << "[Managed user can choose hostname]";
      break;
    case DeviceNamePolicyHandlerImpl::DeviceNamePolicy::
        kPolicyHostnameNotConfigurable:
      stream << "[Managed user cannot choose hostname]";
      break;
  }
  return stream;
}

}  // namespace policy