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