David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 1 | // Copyright 2014 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 "debugd/src/dev_features_tool.h" |
| 6 | |
| 7 | #include <functional> |
| 8 | |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 9 | #include <base/bind.h> |
| 10 | #include <base/callback.h> |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame^] | 11 | #include <base/check.h> |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
| 13 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 14 | #include "debugd/src/error_utils.h" |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 15 | #include "debugd/src/process_with_output.h" |
| 16 | |
| 17 | namespace debugd { |
| 18 | |
| 19 | namespace { |
| 20 | |
Ben Chan | 81905fb | 2017-02-08 22:03:11 -0800 | [diff] [blame] | 21 | using ArgList = ProcessWithOutput::ArgList; |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 22 | |
David Pursell | ab8412f | 2014-12-09 13:41:12 -0800 | [diff] [blame] | 23 | const char kDefaultRootPassword[] = "test0000"; |
| 24 | |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 25 | const char kDevFeaturesErrorString[] = "org.chromium.debugd.error.DevFeatures"; |
| 26 | const char kRootfsLockedErrorString[] = |
| 27 | "Rootfs verification must be removed first"; |
| 28 | |
| 29 | // Executes a helper process with the expectation that any message printed to |
| 30 | // stderr indicates a failure that should be passed back over the D-Bus. |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 31 | // Returns false if any errors launching the process occur. Returns true |
| 32 | // otherwise, and sets |exit_status| if it isn't null. |
| 33 | bool RunHelper(const std::string& command, |
| 34 | const ArgList& arguments, |
| 35 | bool requires_root, |
| 36 | const std::string* stdin, |
| 37 | int* exit_status, |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 38 | brillo::ErrorPtr* error) { |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 39 | std::string stderr; |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 40 | int result = |
| 41 | ProcessWithOutput::RunHelper(command, arguments, requires_root, stdin, |
| 42 | nullptr, // Don't need stdout. |
| 43 | &stderr, error); |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 44 | if (!stderr.empty()) { |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 45 | DEBUGD_ADD_ERROR(error, kDevFeaturesErrorString, stderr.c_str()); |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 46 | return false; |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 47 | } |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 48 | |
| 49 | if (exit_status) |
| 50 | *exit_status = result; |
| 51 | return true; |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 54 | bool RemoveRootfsVerificationQuery(int* exit_status, brillo::ErrorPtr* error) { |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 55 | return RunHelper("dev_features_rootfs_verification", ArgList{"-q"}, |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 56 | true, // requires root to check if / is writable by root. |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 57 | nullptr, // no stdin. |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 58 | exit_status, error); |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 61 | bool EnableBootFromUsbQuery(int* exit_status, brillo::ErrorPtr* error) { |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 62 | return RunHelper("dev_features_usb_boot", ArgList{"-q"}, |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 63 | true, // requires root for crossystem queries. |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 64 | nullptr, // no stdin. |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 65 | exit_status, error); |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 68 | bool ConfigureSshServerQuery(int* exit_status, brillo::ErrorPtr* error) { |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 69 | return RunHelper("dev_features_ssh", ArgList{"-q"}, |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 70 | true, // needs root to check for files in 700 folders. |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 71 | nullptr, // no stdin. |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 72 | exit_status, error); |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 75 | bool EnableChromeRemoteDebuggingQuery(int* exit_status, |
| 76 | brillo::ErrorPtr* error) { |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 77 | return RunHelper("dev_features_chrome_remote_debugging", ArgList{"-q"}, false, |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 78 | nullptr, // no stdin. |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 79 | exit_status, error); |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | bool SetUserPasswordQuery(const std::string& username, |
| 83 | bool system, |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 84 | int* exit_status, |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 85 | brillo::ErrorPtr* error) { |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 86 | ArgList args{"-q", "--user=" + username}; |
| 87 | if (system) |
| 88 | args.push_back("--system"); |
| 89 | |
| 90 | return RunHelper("dev_features_password", args, |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 91 | true, // requires root to read either password file. |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 92 | nullptr, // no stdin. |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 93 | exit_status, error); |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 94 | } |
| 95 | |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 96 | } // namespace |
| 97 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 98 | bool DevFeaturesTool::RemoveRootfsVerification(brillo::ErrorPtr* error) const { |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 99 | return RunHelper("dev_features_rootfs_verification", ArgList{}, |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 100 | true, // requires root for make_dev_ssd.sh script. |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 101 | nullptr, // no stdin. |
| 102 | nullptr, // exit status doesn't matter. |
| 103 | error); |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 106 | bool DevFeaturesTool::EnableBootFromUsb(brillo::ErrorPtr* error) const { |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 107 | return RunHelper("dev_features_usb_boot", ArgList{}, |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 108 | true, // requires root for enable_dev_usb_boot script. |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 109 | nullptr, // no stdin. |
| 110 | nullptr, // exit status doesn't matter. |
| 111 | error); |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 114 | bool DevFeaturesTool::ConfigureSshServer(brillo::ErrorPtr* error) const { |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 115 | // SSH server configuration requires writing to rootfs. |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 116 | int exit_status; |
| 117 | if (!RemoveRootfsVerificationQuery(&exit_status, error) || exit_status != 0) { |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 118 | DEBUGD_ADD_ERROR(error, kDevFeaturesErrorString, kRootfsLockedErrorString); |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 119 | return false; |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 120 | } |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 121 | |
| 122 | return RunHelper("dev_features_ssh", ArgList{}, |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 123 | true, // requires root to write to rootfs directories. |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 124 | nullptr, // no stdin. |
| 125 | nullptr, // exit status doesn't matter. |
| 126 | error); |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 129 | bool DevFeaturesTool::EnableChromeRemoteDebugging( |
| 130 | brillo::ErrorPtr* error) const { |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 131 | int exit_status; |
| 132 | if (!RemoveRootfsVerificationQuery(&exit_status, error) || exit_status != 0) { |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 133 | DEBUGD_ADD_ERROR(error, kDevFeaturesErrorString, kRootfsLockedErrorString); |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 134 | return false; |
Xiaohui Chen | a8bced8 | 2015-02-27 10:35:26 -0800 | [diff] [blame] | 135 | } |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 136 | |
| 137 | return RunHelper("dev_features_chrome_remote_debugging", ArgList{}, |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 138 | true, // requires root to write to rootfs directories. |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 139 | nullptr, // no stdin. |
| 140 | nullptr, // exit status doesn't matter. |
| 141 | error); |
Xiaohui Chen | a8bced8 | 2015-02-27 10:35:26 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 144 | bool DevFeaturesTool::SetUserPassword(const std::string& username, |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 145 | const std::string& password, |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 146 | brillo::ErrorPtr* error) const { |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 147 | ArgList args{"--user=" + username}; |
| 148 | |
| 149 | // Set the devmode password regardless of rootfs verification state. |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 150 | if (!RunHelper("dev_features_password", args, |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 151 | true, // requires root to write devmode password file. |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 152 | &password, // pipe the password through stdin. |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 153 | nullptr, // exit status doesn't matter. |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 154 | error)) { |
| 155 | return false; |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 156 | } |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 157 | |
| 158 | // If rootfs is locked, don't bother setting the system password. |
| 159 | int exit_status; |
| 160 | if (!RemoveRootfsVerificationQuery(&exit_status, error) || exit_status != 0) |
| 161 | return true; |
| 162 | |
| 163 | args.push_back("--system"); |
| 164 | return RunHelper("dev_features_password", args, |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 165 | true, // requires root to write system password file. |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 166 | &password, // pipe the password through stdin. |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 167 | nullptr, // exit status doesn't matter. |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 168 | error); |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 171 | bool DevFeaturesTool::EnableChromeDevFeatures(const std::string& root_password, |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 172 | brillo::ErrorPtr* error) const { |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 173 | if (!EnableBootFromUsb(error)) |
| 174 | return false; |
| 175 | |
| 176 | if (!ConfigureSshServer(error)) |
| 177 | return false; |
| 178 | |
| 179 | return SetUserPassword( |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 180 | "root", root_password.empty() ? kDefaultRootPassword : root_password, |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 181 | error); |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | namespace { |
| 185 | |
| 186 | struct Query { |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 187 | // The callback should launch the query program. If launching fails, return |
| 188 | // false and set the error. If it succeeds, put the exit status in the |
| 189 | // integer out-argument. |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 190 | using Function = base::Callback<bool(int*, brillo::ErrorPtr*)>; |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 191 | |
| 192 | Function function; |
| 193 | DevFeatureFlag flag; |
| 194 | }; |
| 195 | |
| 196 | } // namespace |
| 197 | |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 198 | bool DevFeaturesTool::QueryDevFeatures(int32_t* flags, |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 199 | brillo::ErrorPtr* error) const { |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 200 | DCHECK(flags); |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 201 | Query queries[] = { |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 202 | {base::Bind(&RemoveRootfsVerificationQuery), |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 203 | DEV_FEATURE_ROOTFS_VERIFICATION_REMOVED}, |
| 204 | {base::Bind(&EnableBootFromUsbQuery), DEV_FEATURE_BOOT_FROM_USB_ENABLED}, |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 205 | {base::Bind(&EnableChromeRemoteDebuggingQuery), |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 206 | DEV_FEATURE_CHROME_REMOTE_DEBUGGING_ENABLED}, |
| 207 | {base::Bind(&ConfigureSshServerQuery), DEV_FEATURE_SSH_SERVER_CONFIGURED}, |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 208 | {base::Bind(&SetUserPasswordQuery, "root", /* system = */ false), |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 209 | DEV_FEATURE_DEV_MODE_ROOT_PASSWORD_SET}, |
Eric Caruso | 75eda08 | 2017-04-25 11:37:26 -0700 | [diff] [blame] | 210 | {base::Bind(&SetUserPasswordQuery, "root", /* system = */ true), |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 211 | DEV_FEATURE_SYSTEM_ROOT_PASSWORD_SET}}; |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 212 | |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 213 | int32_t result_flags = 0; |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 214 | for (const auto& query : queries) { |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 215 | int exit_status; |
| 216 | if (!query.function.Run(&exit_status, error)) { |
| 217 | // D-Bus is only set up to handle a single error so exit as soon as we |
| 218 | // hit one. |
| 219 | return false; |
| 220 | } |
| 221 | if (exit_status == 0) |
| 222 | result_flags |= query.flag; |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 223 | } |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 224 | *flags = result_flags; |
| 225 | return true; |
David Pursell | e5ebdae | 2014-11-04 08:51:37 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | } // namespace debugd |