1 // Copyright (c) 2012 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 #ifndef LIBBRILLO_POLICY_DEVICE_POLICY_IMPL_H_
6 #define LIBBRILLO_POLICY_DEVICE_POLICY_IMPL_H_
7 
8 #include <memory>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
13 
14 #include <base/files/file_path.h>
15 #include <base/macros.h>
16 
17 #include "bindings/chrome_device_policy.pb.h"
18 #include "bindings/device_management_backend.pb.h"
19 #include "install_attributes/libinstallattributes.h"
20 #include "policy/device_policy.h"
21 
22 #pragma GCC visibility push(default)
23 
24 namespace policy {
25 
26 // This class holds device settings that are to be enforced across all users.
27 //
28 // Before serving it to the users this class verifies that the policy is valid
29 // against its signature and the owner's key and also that the policy files
30 // are owned by root.
31 class DevicePolicyImpl : public DevicePolicy {
32  public:
33   DevicePolicyImpl();
34   ~DevicePolicyImpl() override;
35 
get_device_policy()36   const enterprise_management::ChromeDeviceSettingsProto& get_device_policy()
37       const {
38     return device_policy_;
39   }
40 
41   // DevicePolicy overrides:
42   bool LoadPolicy() override;
43   bool IsEnterpriseEnrolled() const override;
44   bool GetPolicyRefreshRate(int* rate) const override;
45   bool GetUserWhitelist(
46       std::vector<std::string>* user_whitelist) const override;
47   bool GetGuestModeEnabled(bool* guest_mode_enabled) const override;
48   bool GetCameraEnabled(bool* camera_enabled) const override;
49   bool GetShowUserNames(bool* show_user_names) const override;
50   bool GetDataRoamingEnabled(bool* data_roaming_enabled) const override;
51   bool GetAllowNewUsers(bool* allow_new_users) const override;
52   bool GetMetricsEnabled(bool* metrics_enabled) const override;
53   bool GetReportVersionInfo(bool* report_version_info) const override;
54   bool GetReportActivityTimes(bool* report_activity_times) const override;
55   bool GetReportBootMode(bool* report_boot_mode) const override;
56   bool GetEphemeralUsersEnabled(bool* ephemeral_users_enabled) const override;
57   bool GetReleaseChannel(std::string* release_channel) const override;
58   bool GetReleaseChannelDelegated(
59       bool* release_channel_delegated) const override;
60   bool GetUpdateDisabled(bool* update_disabled) const override;
61   bool GetTargetVersionPrefix(
62       std::string* target_version_prefix) const override;
63   bool GetRollbackToTargetVersion(
64       int* rollback_to_target_version) const override;
65   bool GetRollbackAllowedMilestones(
66       int* rollback_allowed_milestones) const override;
67   bool GetScatterFactorInSeconds(
68       int64_t* scatter_factor_in_seconds) const override;
69   bool GetAllowedConnectionTypesForUpdate(
70       std::set<std::string>* connection_types) const override;
71   bool GetOpenNetworkConfiguration(
72       std::string* open_network_configuration) const override;
73   bool GetOwner(std::string* owner) const override;
74   bool GetHttpDownloadsEnabled(bool* http_downloads_enabled) const override;
75   bool GetAuP2PEnabled(bool* au_p2p_enabled) const override;
76   bool GetAllowKioskAppControlChromeVersion(
77       bool* allow_kiosk_app_control_chrome_version) const override;
78   bool GetUsbDetachableWhitelist(
79       std::vector<UsbDeviceId>* usb_whitelist) const override;
80   bool GetAutoLaunchedKioskAppId(std::string* app_id_out) const override;
81   bool IsEnterpriseManaged() const override;
82   bool GetSecondFactorAuthenticationMode(int* mode_out) const override;
83   bool GetDisallowedTimeIntervals(
84       std::vector<WeeklyTimeInterval>* intervals_out) const override;
85   bool GetDeviceUpdateStagingSchedule(
86       std::vector<DayPercentagePair> *staging_schedule_out) const override;
87   bool GetDeviceQuickFixBuildToken(
88       std::string* device_quick_fix_build_token) const override;
89   bool GetDeviceDirectoryApiId(
90       std::string* device_directory_api_out) const override;
91 
92   // Methods that can be used only for testing.
set_policy_data_for_testing(const enterprise_management::PolicyData & policy_data)93   void set_policy_data_for_testing(
94       const enterprise_management::PolicyData& policy_data) {
95     policy_data_ = policy_data;
96   }
set_verify_root_ownership_for_testing(bool verify_root_ownership)97   void set_verify_root_ownership_for_testing(bool verify_root_ownership) {
98     verify_root_ownership_ = verify_root_ownership;
99   }
set_install_attributes_for_testing(std::unique_ptr<InstallAttributesReader> install_attributes_reader)100   void set_install_attributes_for_testing(
101       std::unique_ptr<InstallAttributesReader> install_attributes_reader) {
102     install_attributes_reader_ = std::move(install_attributes_reader);
103   }
set_policy_for_testing(const enterprise_management::ChromeDeviceSettingsProto & device_policy)104   void set_policy_for_testing(
105       const enterprise_management::ChromeDeviceSettingsProto& device_policy) {
106     device_policy_ = device_policy;
107   }
set_policy_path_for_testing(const base::FilePath & policy_path)108   void set_policy_path_for_testing(const base::FilePath& policy_path) {
109     policy_path_ = policy_path;
110   }
set_key_file_path_for_testing(const base::FilePath & keyfile_path)111   void set_key_file_path_for_testing(const base::FilePath& keyfile_path) {
112     keyfile_path_ = keyfile_path;
113   }
set_verify_policy_for_testing(bool value)114   void set_verify_policy_for_testing(bool value) { verify_policy_ = value; }
115 
116  private:
117   // Verifies that both the policy file and the signature file exist and are
118   // owned by the root. Does nothing when |verify_root_ownership_| is set to
119   // false.
120   bool VerifyPolicyFile(const base::FilePath& policy_path);
121 
122   // Verifies that the policy signature is correct.
123   bool VerifyPolicySignature() override;
124 
125   // Loads policy off of disk from |policy_path| into |policy_|. Returns true if
126   // the |policy_path| is present on disk and loading it is successful.
127   bool LoadPolicyFromFile(const base::FilePath& policy_path);
128 
129   // Path of the default policy file, e.g. /path/to/policy. In order to make
130   // device policy more resilient against broken files, this class also tries to
131   // load indexed paths /path/to/policy.1, /path/to/policy.2 etc., see
132   // resilient_policy_utils.h.
133   base::FilePath policy_path_;
134   base::FilePath keyfile_path_;
135   std::unique_ptr<InstallAttributesReader> install_attributes_reader_;
136   enterprise_management::PolicyFetchResponse policy_;
137   enterprise_management::PolicyData policy_data_;
138   enterprise_management::ChromeDeviceSettingsProto device_policy_;
139 
140   // If true, verify that policy files are owned by root. True in production
141   // but can be set to false by tests.
142   bool verify_root_ownership_ = true;
143   // If false, all types of verification are disabled. True in production
144   // but can be set to false by tests.
145   bool verify_policy_ = true;
146 
147   DISALLOW_COPY_AND_ASSIGN(DevicePolicyImpl);
148 };
149 }  // namespace policy
150 
151 #pragma GCC visibility pop
152 
153 #endif  // LIBBRILLO_POLICY_DEVICE_POLICY_IMPL_H_
154