San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <sys/socket.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 19 | #include <sys/types.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 20 | #include <netinet/in.h> |
| 21 | #include <arpa/inet.h> |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 22 | #include <dirent.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 23 | #include <errno.h> |
San Mehat | 2350c44 | 2010-03-02 13:16:50 -0800 | [diff] [blame] | 24 | #include <fcntl.h> |
Mohamad Ayyash | 7929aa7 | 2014-03-10 15:55:33 -0700 | [diff] [blame] | 25 | #include <fs_mgr.h> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
Olivier Bailly | 37dcda6 | 2010-11-16 10:41:53 -0800 | [diff] [blame] | 27 | #include <string.h> |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame^] | 28 | #include <stdint.h> |
| 29 | #include <inttypes.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 30 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 31 | #define LOG_TAG "VoldCmdListener" |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame^] | 32 | |
| 33 | #include <base/stringprintf.h> |
| 34 | #include <cutils/fs.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 35 | #include <cutils/log.h> |
| 36 | |
| 37 | #include <sysutils/SocketClient.h> |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 39 | |
| 40 | #include "CommandListener.h" |
| 41 | #include "VolumeManager.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 42 | #include "VolumeBase.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 43 | #include "ResponseCode.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 44 | #include "Process.h" |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 45 | #include "Loop.h" |
| 46 | #include "Devmapper.h" |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 47 | #include "cryptfs.h" |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 48 | #include "fstrim.h" |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 49 | #include "MoveTask.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 50 | |
Dianne Hackborn | 3fd60b4 | 2012-11-27 16:00:04 -0800 | [diff] [blame] | 51 | #define DUMP_ARGS 0 |
| 52 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 53 | CommandListener::CommandListener() : |
Robert Greenwalt | 149aa3e | 2012-02-16 14:43:03 -0800 | [diff] [blame] | 54 | FrameworkListener("vold", true) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 55 | registerCmd(new DumpCmd()); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 56 | registerCmd(new VolumeCmd()); |
| 57 | registerCmd(new AsecCmd()); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 58 | registerCmd(new ObbCmd()); |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 59 | registerCmd(new StorageCmd()); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 60 | registerCmd(new CryptfsCmd()); |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 61 | registerCmd(new FstrimCmd()); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Dianne Hackborn | 3fd60b4 | 2012-11-27 16:00:04 -0800 | [diff] [blame] | 64 | #if DUMP_ARGS |
Mark Salyzyn | 3e97127 | 2014-01-21 13:27:04 -0800 | [diff] [blame] | 65 | void CommandListener::dumpArgs(int argc, char **argv, int argObscure) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 66 | char buffer[4096]; |
| 67 | char *p = buffer; |
| 68 | |
| 69 | memset(buffer, 0, sizeof(buffer)); |
| 70 | int i; |
| 71 | for (i = 0; i < argc; i++) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 72 | unsigned int len = strlen(argv[i]) + 1; // Account for space |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 73 | if (i == argObscure) { |
| 74 | len += 2; // Account for {} |
| 75 | } |
| 76 | if (((p - buffer) + len) < (sizeof(buffer)-1)) { |
| 77 | if (i == argObscure) { |
| 78 | *p++ = '{'; |
| 79 | *p++ = '}'; |
| 80 | *p++ = ' '; |
| 81 | continue; |
| 82 | } |
| 83 | strcpy(p, argv[i]); |
| 84 | p+= strlen(argv[i]); |
| 85 | if (i != (argc -1)) { |
| 86 | *p++ = ' '; |
| 87 | } |
| 88 | } |
| 89 | } |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 90 | SLOGD("%s", buffer); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 91 | } |
Mark Salyzyn | 3e97127 | 2014-01-21 13:27:04 -0800 | [diff] [blame] | 92 | #else |
| 93 | void CommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { } |
| 94 | #endif |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 95 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 96 | int CommandListener::sendGenericOkFail(SocketClient *cli, int cond) { |
| 97 | if (!cond) { |
| 98 | return cli->sendMsg(ResponseCode::CommandOkay, "Command succeeded", false); |
| 99 | } else { |
| 100 | return cli->sendMsg(ResponseCode::OperationFailed, "Command failed", false); |
| 101 | } |
| 102 | } |
| 103 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 104 | CommandListener::DumpCmd::DumpCmd() : |
| 105 | VoldCommand("dump") { |
| 106 | } |
| 107 | |
| 108 | int CommandListener::DumpCmd::runCommand(SocketClient *cli, |
Mark Salyzyn | 3e97127 | 2014-01-21 13:27:04 -0800 | [diff] [blame] | 109 | int /*argc*/, char ** /*argv*/) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 110 | cli->sendMsg(0, "Dumping loop status", false); |
| 111 | if (Loop::dumpState(cli)) { |
| 112 | cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true); |
| 113 | } |
| 114 | cli->sendMsg(0, "Dumping DM status", false); |
| 115 | if (Devmapper::dumpState(cli)) { |
| 116 | cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true); |
| 117 | } |
San Mehat | 96597e8 | 2010-03-17 09:50:54 -0700 | [diff] [blame] | 118 | cli->sendMsg(0, "Dumping mounted filesystems", false); |
| 119 | FILE *fp = fopen("/proc/mounts", "r"); |
| 120 | if (fp) { |
| 121 | char line[1024]; |
| 122 | while (fgets(line, sizeof(line), fp)) { |
| 123 | line[strlen(line)-1] = '\0'; |
| 124 | cli->sendMsg(0, line, false);; |
| 125 | } |
| 126 | fclose(fp); |
| 127 | } |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 128 | |
| 129 | cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false); |
| 130 | return 0; |
| 131 | } |
| 132 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 133 | CommandListener::VolumeCmd::VolumeCmd() : |
| 134 | VoldCommand("volume") { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 135 | } |
| 136 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 137 | int CommandListener::VolumeCmd::runCommand(SocketClient *cli, |
Mohamad Ayyash | 7929aa7 | 2014-03-10 15:55:33 -0700 | [diff] [blame] | 138 | int argc, char **argv) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 139 | dumpArgs(argc, argv, -1); |
| 140 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 141 | if (argc < 2) { |
| 142 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); |
| 143 | return 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 144 | } |
| 145 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 146 | VolumeManager *vm = VolumeManager::Instance(); |
Jeff Sharkey | c8e04c5 | 2015-04-21 12:14:17 -0700 | [diff] [blame] | 147 | std::lock_guard<std::mutex> lock(vm->getLock()); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 148 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 149 | // TODO: tease out methods not directly related to volumes |
| 150 | |
| 151 | std::string cmd(argv[1]); |
| 152 | if (cmd == "reset") { |
| 153 | return sendGenericOkFail(cli, vm->reset()); |
| 154 | |
| 155 | } else if (cmd == "shutdown") { |
| 156 | return sendGenericOkFail(cli, vm->shutdown()); |
| 157 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 158 | } else if (cmd == "debug") { |
| 159 | return sendGenericOkFail(cli, vm->setDebug(true)); |
| 160 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 161 | } else if (cmd == "partition" && argc > 3) { |
| 162 | // partition [diskId] [public|private|mixed] [ratio] |
| 163 | std::string id(argv[2]); |
| 164 | auto disk = vm->findDisk(id); |
| 165 | if (disk == nullptr) { |
| 166 | return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown disk", false); |
San Mehat | 57df7bf | 2010-03-14 13:41:27 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 169 | std::string type(argv[3]); |
| 170 | if (type == "public") { |
| 171 | return sendGenericOkFail(cli, disk->partitionPublic()); |
| 172 | } else if (type == "private") { |
| 173 | return sendGenericOkFail(cli, disk->partitionPrivate()); |
| 174 | } else if (type == "mixed") { |
| 175 | if (argc < 4) { |
| 176 | return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false); |
| 177 | } |
| 178 | int frac = atoi(argv[4]); |
| 179 | return sendGenericOkFail(cli, disk->partitionMixed(frac)); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 180 | } else { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 181 | return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 182 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 183 | |
| 184 | } else if (cmd == "mkdirs" && argc > 2) { |
| 185 | // mkdirs [path] |
| 186 | return sendGenericOkFail(cli, vm->mkdirs(argv[2])); |
| 187 | |
| 188 | } else if (cmd == "start_user" && argc > 2) { |
| 189 | // start_user [user] |
| 190 | return sendGenericOkFail(cli, vm->startUser(atoi(argv[2]))); |
| 191 | |
| 192 | } else if (cmd == "cleanup_user" && argc > 2) { |
| 193 | // cleanup_user [user] |
| 194 | return sendGenericOkFail(cli, vm->cleanupUser(atoi(argv[2]))); |
| 195 | |
| 196 | } else if (cmd == "mount" && argc > 2) { |
| 197 | // mount [volId] [flags] [user] |
| 198 | std::string id(argv[2]); |
| 199 | auto vol = vm->findVolume(id); |
| 200 | if (vol == nullptr) { |
| 201 | return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false); |
Jeff Sharkey | 71ebe15 | 2013-09-17 17:24:38 -0700 | [diff] [blame] | 202 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 203 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 204 | int mountFlags = (argc > 3) ? atoi(argv[3]) : 0; |
| 205 | userid_t mountUserId = (argc > 4) ? atoi(argv[4]) : -1; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 206 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 207 | vol->setMountFlags(mountFlags); |
| 208 | vol->setMountUserId(mountUserId); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 209 | |
Jeff Sharkey | 1bfb375 | 2015-04-29 15:22:23 -0700 | [diff] [blame] | 210 | int res = vol->mount(); |
| 211 | if (mountFlags & android::vold::VolumeBase::MountFlags::kPrimary) { |
| 212 | vm->setPrimary(vol); |
| 213 | } |
| 214 | return sendGenericOkFail(cli, res); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 215 | |
| 216 | } else if (cmd == "unmount" && argc > 2) { |
| 217 | // unmount [volId] |
| 218 | std::string id(argv[2]); |
| 219 | auto vol = vm->findVolume(id); |
| 220 | if (vol == nullptr) { |
| 221 | return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false); |
| 222 | } |
| 223 | |
| 224 | return sendGenericOkFail(cli, vol->unmount()); |
| 225 | |
| 226 | } else if (cmd == "format" && argc > 2) { |
| 227 | // format [volId] |
| 228 | std::string id(argv[2]); |
| 229 | auto vol = vm->findVolume(id); |
| 230 | if (vol == nullptr) { |
| 231 | return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false); |
| 232 | } |
| 233 | |
| 234 | return sendGenericOkFail(cli, vol->format()); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 235 | |
| 236 | } else if (cmd == "move_storage" && argc > 3) { |
| 237 | // move_storage [fromVolId] [toVolId] |
| 238 | auto fromVol = vm->findVolume(std::string(argv[2])); |
| 239 | auto toVol = vm->findVolume(std::string(argv[3])); |
| 240 | if (fromVol == nullptr || toVol == nullptr) { |
| 241 | return cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume", false); |
| 242 | } |
| 243 | |
| 244 | (new android::vold::MoveTask(fromVol, toVol))->start(); |
| 245 | return sendGenericOkFail(cli, 0); |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame^] | 246 | |
| 247 | } else if (cmd == "benchmark" && argc > 2) { |
| 248 | // benchmark [volId] |
| 249 | std::string id(argv[2]); |
| 250 | nsecs_t res = vm->benchmarkVolume(id); |
| 251 | return cli->sendMsg(ResponseCode::CommandOkay, |
| 252 | android::base::StringPrintf("%" PRId64, res).c_str(), false); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 255 | return cli->sendMsg(ResponseCode::CommandSyntaxError, nullptr, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 256 | } |
| 257 | |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 258 | CommandListener::StorageCmd::StorageCmd() : |
| 259 | VoldCommand("storage") { |
| 260 | } |
| 261 | |
| 262 | int CommandListener::StorageCmd::runCommand(SocketClient *cli, |
| 263 | int argc, char **argv) { |
Mohamad Ayyash | 7929aa7 | 2014-03-10 15:55:33 -0700 | [diff] [blame] | 264 | /* Guarantied to be initialized by vold's main() before the CommandListener is active */ |
| 265 | extern struct fstab *fstab; |
| 266 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 267 | dumpArgs(argc, argv, -1); |
| 268 | |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 269 | if (argc < 2) { |
| 270 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); |
| 271 | return 0; |
| 272 | } |
| 273 | |
Mohamad Ayyash | 7929aa7 | 2014-03-10 15:55:33 -0700 | [diff] [blame] | 274 | if (!strcmp(argv[1], "mountall")) { |
| 275 | if (argc != 2) { |
| 276 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: mountall", false); |
| 277 | return 0; |
| 278 | } |
| 279 | fs_mgr_mount_all(fstab); |
| 280 | cli->sendMsg(ResponseCode::CommandOkay, "Mountall ran successfully", false); |
| 281 | return 0; |
| 282 | } |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 283 | if (!strcmp(argv[1], "users")) { |
| 284 | DIR *dir; |
| 285 | struct dirent *de; |
| 286 | |
JP Abgrall | edf7adf | 2014-03-12 10:41:05 -0700 | [diff] [blame] | 287 | if (argc < 3) { |
| 288 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument: user <mountpoint>", false); |
| 289 | return 0; |
| 290 | } |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 291 | if (!(dir = opendir("/proc"))) { |
| 292 | cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | while ((de = readdir(dir))) { |
| 297 | int pid = Process::getPid(de->d_name); |
| 298 | |
| 299 | if (pid < 0) { |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | char processName[255]; |
| 304 | Process::getProcessName(pid, processName, sizeof(processName)); |
| 305 | |
| 306 | if (Process::checkFileDescriptorSymLinks(pid, argv[2]) || |
| 307 | Process::checkFileMaps(pid, argv[2]) || |
| 308 | Process::checkSymLink(pid, argv[2], "cwd") || |
| 309 | Process::checkSymLink(pid, argv[2], "root") || |
| 310 | Process::checkSymLink(pid, argv[2], "exe")) { |
| 311 | |
| 312 | char msg[1024]; |
| 313 | snprintf(msg, sizeof(msg), "%d %s", pid, processName); |
| 314 | cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false); |
| 315 | } |
| 316 | } |
| 317 | closedir(dir); |
| 318 | cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false); |
| 319 | } else { |
| 320 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false); |
| 321 | } |
| 322 | return 0; |
| 323 | } |
| 324 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 325 | CommandListener::AsecCmd::AsecCmd() : |
| 326 | VoldCommand("asec") { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 329 | void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) { |
| 330 | DIR *d = opendir(directory); |
| 331 | |
| 332 | if (!d) { |
| 333 | cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | size_t dirent_len = offsetof(struct dirent, d_name) + |
Elliott Hughes | 8c480f7 | 2012-10-26 16:57:19 -0700 | [diff] [blame] | 338 | fpathconf(dirfd(d), _PC_NAME_MAX) + 1; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 339 | |
| 340 | struct dirent *dent = (struct dirent *) malloc(dirent_len); |
| 341 | if (dent == NULL) { |
| 342 | cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | struct dirent *result; |
| 347 | |
| 348 | while (!readdir_r(d, dent, &result) && result != NULL) { |
| 349 | if (dent->d_name[0] == '.') |
| 350 | continue; |
| 351 | if (dent->d_type != DT_REG) |
| 352 | continue; |
| 353 | size_t name_len = strlen(dent->d_name); |
| 354 | if (name_len > 5 && name_len < 260 && |
| 355 | !strcmp(&dent->d_name[name_len - 5], ".asec")) { |
| 356 | char id[255]; |
| 357 | memset(id, 0, sizeof(id)); |
Kenny Root | 7b0bc85 | 2012-04-27 15:33:58 -0700 | [diff] [blame] | 358 | strlcpy(id, dent->d_name, name_len - 4); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 359 | cli->sendMsg(ResponseCode::AsecListResult, id, false); |
| 360 | } |
| 361 | } |
| 362 | closedir(d); |
| 363 | |
| 364 | free(dent); |
| 365 | } |
| 366 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 367 | int CommandListener::AsecCmd::runCommand(SocketClient *cli, |
| 368 | int argc, char **argv) { |
| 369 | if (argc < 2) { |
| 370 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 374 | VolumeManager *vm = VolumeManager::Instance(); |
| 375 | int rc = 0; |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 376 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 377 | if (!strcmp(argv[1], "list")) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 378 | dumpArgs(argc, argv, -1); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 379 | |
Jeff Sharkey | 9f18fe7 | 2015-04-01 23:32:18 -0700 | [diff] [blame] | 380 | listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_EXT); |
| 381 | listAsecsInDirectory(cli, VolumeManager::SEC_ASECDIR_INT); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 382 | } else if (!strcmp(argv[1], "create")) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 383 | dumpArgs(argc, argv, 5); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 384 | if (argc != 8) { |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 385 | cli->sendMsg(ResponseCode::CommandSyntaxError, |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 386 | "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> " |
| 387 | "<isExternal>", false); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 392 | const bool isExternal = (atoi(argv[7]) == 1); |
| 393 | rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 394 | } else if (!strcmp(argv[1], "resize")) { |
| 395 | dumpArgs(argc, argv, -1); |
| 396 | if (argc != 5) { |
| 397 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", false); |
| 398 | return 0; |
| 399 | } |
| 400 | unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; |
| 401 | rc = vm->resizeAsec(argv[2], numSectors, argv[4]); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 402 | } else if (!strcmp(argv[1], "finalize")) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 403 | dumpArgs(argc, argv, -1); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 404 | if (argc != 3) { |
| 405 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false); |
| 406 | return 0; |
| 407 | } |
San Mehat | 8f2875b | 2010-02-18 11:40:49 -0800 | [diff] [blame] | 408 | rc = vm->finalizeAsec(argv[2]); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 409 | } else if (!strcmp(argv[1], "fixperms")) { |
| 410 | dumpArgs(argc, argv, -1); |
| 411 | if (argc != 5) { |
| 412 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false); |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | char *endptr; |
| 417 | gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10); |
| 418 | if (*endptr != '\0') { |
| 419 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 424 | } else if (!strcmp(argv[1], "destroy")) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 425 | dumpArgs(argc, argv, -1); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 426 | if (argc < 3) { |
| 427 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 428 | return 0; |
| 429 | } |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 430 | bool force = false; |
| 431 | if (argc > 3 && !strcmp(argv[3], "force")) { |
| 432 | force = true; |
| 433 | } |
San Mehat | 8f2875b | 2010-02-18 11:40:49 -0800 | [diff] [blame] | 434 | rc = vm->destroyAsec(argv[2], force); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 435 | } else if (!strcmp(argv[1], "mount")) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 436 | dumpArgs(argc, argv, 3); |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 437 | if (argc != 6) { |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 438 | cli->sendMsg(ResponseCode::CommandSyntaxError, |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 439 | "Usage: asec mount <namespace-id> <key> <ownerUid> <ro|rw>", false); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 440 | return 0; |
| 441 | } |
Jeff Sharkey | 43ed123 | 2014-08-22 12:29:05 -0700 | [diff] [blame] | 442 | bool readOnly = true; |
| 443 | if (!strcmp(argv[5], "rw")) { |
| 444 | readOnly = false; |
| 445 | } |
| 446 | rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]), readOnly); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 447 | } else if (!strcmp(argv[1], "unmount")) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 448 | dumpArgs(argc, argv, -1); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 449 | if (argc < 3) { |
| 450 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 451 | return 0; |
| 452 | } |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 453 | bool force = false; |
| 454 | if (argc > 3 && !strcmp(argv[3], "force")) { |
| 455 | force = true; |
| 456 | } |
San Mehat | 8f2875b | 2010-02-18 11:40:49 -0800 | [diff] [blame] | 457 | rc = vm->unmountAsec(argv[2], force); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 458 | } else if (!strcmp(argv[1], "rename")) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 459 | dumpArgs(argc, argv, -1); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 460 | if (argc != 4) { |
| 461 | cli->sendMsg(ResponseCode::CommandSyntaxError, |
| 462 | "Usage: asec rename <old_id> <new_id>", false); |
| 463 | return 0; |
| 464 | } |
San Mehat | 8f2875b | 2010-02-18 11:40:49 -0800 | [diff] [blame] | 465 | rc = vm->renameAsec(argv[2], argv[3]); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 466 | } else if (!strcmp(argv[1], "path")) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 467 | dumpArgs(argc, argv, -1); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 468 | if (argc != 3) { |
| 469 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false); |
| 470 | return 0; |
| 471 | } |
| 472 | char path[255]; |
| 473 | |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 474 | if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) { |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 475 | cli->sendMsg(ResponseCode::AsecPathResult, path, false); |
San Mehat | 88ac2c0 | 2010-03-23 11:15:58 -0700 | [diff] [blame] | 476 | return 0; |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 477 | } |
Dianne Hackborn | 736910c | 2011-06-27 13:37:07 -0700 | [diff] [blame] | 478 | } else if (!strcmp(argv[1], "fspath")) { |
| 479 | dumpArgs(argc, argv, -1); |
| 480 | if (argc != 3) { |
| 481 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false); |
| 482 | return 0; |
| 483 | } |
| 484 | char path[255]; |
| 485 | |
| 486 | if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) { |
| 487 | cli->sendMsg(ResponseCode::AsecPathResult, path, false); |
| 488 | return 0; |
| 489 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 490 | } else { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 491 | dumpArgs(argc, argv, -1); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 492 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 493 | } |
| 494 | |
San Mehat | 8f2875b | 2010-02-18 11:40:49 -0800 | [diff] [blame] | 495 | if (!rc) { |
| 496 | cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false); |
| 497 | } else { |
| 498 | rc = ResponseCode::convertFromErrno(); |
| 499 | cli->sendMsg(rc, "asec operation failed", true); |
| 500 | } |
| 501 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 502 | return 0; |
| 503 | } |
San Mehat | 2350c44 | 2010-03-02 13:16:50 -0800 | [diff] [blame] | 504 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 505 | CommandListener::ObbCmd::ObbCmd() : |
| 506 | VoldCommand("obb") { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 509 | int CommandListener::ObbCmd::runCommand(SocketClient *cli, |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 510 | int argc, char **argv) { |
| 511 | if (argc < 2) { |
| 512 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | VolumeManager *vm = VolumeManager::Instance(); |
| 517 | int rc = 0; |
| 518 | |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 519 | if (!strcmp(argv[1], "list")) { |
| 520 | dumpArgs(argc, argv, -1); |
| 521 | |
| 522 | rc = vm->listMountedObbs(cli); |
| 523 | } else if (!strcmp(argv[1], "mount")) { |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 524 | dumpArgs(argc, argv, 3); |
| 525 | if (argc != 5) { |
| 526 | cli->sendMsg(ResponseCode::CommandSyntaxError, |
Jeff Sharkey | 6947904 | 2012-09-25 16:14:57 -0700 | [diff] [blame] | 527 | "Usage: obb mount <filename> <key> <ownerGid>", false); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 528 | return 0; |
| 529 | } |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 530 | rc = vm->mountObb(argv[2], argv[3], atoi(argv[4])); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 531 | } else if (!strcmp(argv[1], "unmount")) { |
| 532 | dumpArgs(argc, argv, -1); |
| 533 | if (argc < 3) { |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 534 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 535 | return 0; |
| 536 | } |
| 537 | bool force = false; |
| 538 | if (argc > 3 && !strcmp(argv[3], "force")) { |
| 539 | force = true; |
| 540 | } |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 541 | rc = vm->unmountObb(argv[2], force); |
| 542 | } else if (!strcmp(argv[1], "path")) { |
| 543 | dumpArgs(argc, argv, -1); |
| 544 | if (argc != 3) { |
| 545 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false); |
| 546 | return 0; |
| 547 | } |
| 548 | char path[255]; |
| 549 | |
| 550 | if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) { |
| 551 | cli->sendMsg(ResponseCode::AsecPathResult, path, false); |
| 552 | return 0; |
| 553 | } |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 554 | } else { |
| 555 | dumpArgs(argc, argv, -1); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 556 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | if (!rc) { |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 560 | cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 561 | } else { |
| 562 | rc = ResponseCode::convertFromErrno(); |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 563 | cli->sendMsg(rc, "obb operation failed", true); |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 569 | CommandListener::CryptfsCmd::CryptfsCmd() : |
| 570 | VoldCommand("cryptfs") { |
| 571 | } |
| 572 | |
Paul Lawrence | 45f1053 | 2014-04-04 18:11:56 +0000 | [diff] [blame] | 573 | static int getType(const char* type) |
| 574 | { |
| 575 | if (!strcmp(type, "default")) { |
| 576 | return CRYPT_TYPE_DEFAULT; |
| 577 | } else if (!strcmp(type, "password")) { |
| 578 | return CRYPT_TYPE_PASSWORD; |
| 579 | } else if (!strcmp(type, "pin")) { |
| 580 | return CRYPT_TYPE_PIN; |
| 581 | } else if (!strcmp(type, "pattern")) { |
| 582 | return CRYPT_TYPE_PATTERN; |
| 583 | } else { |
| 584 | return -1; |
| 585 | } |
| 586 | } |
| 587 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 588 | int CommandListener::CryptfsCmd::runCommand(SocketClient *cli, |
| 589 | int argc, char **argv) { |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 590 | if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) { |
| 591 | cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run cryptfs commands", false); |
| 592 | return 0; |
| 593 | } |
| 594 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 595 | if (argc < 2) { |
| 596 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | int rc = 0; |
| 601 | |
| 602 | if (!strcmp(argv[1], "checkpw")) { |
| 603 | if (argc != 3) { |
| 604 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs checkpw <passwd>", false); |
| 605 | return 0; |
| 606 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 607 | dumpArgs(argc, argv, 2); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 608 | rc = cryptfs_check_passwd(argv[2]); |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 609 | } else if (!strcmp(argv[1], "restart")) { |
| 610 | if (argc != 2) { |
| 611 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false); |
| 612 | return 0; |
| 613 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 614 | dumpArgs(argc, argv, -1); |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 615 | rc = cryptfs_restart(); |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 616 | } else if (!strcmp(argv[1], "cryptocomplete")) { |
| 617 | if (argc != 2) { |
| 618 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs cryptocomplete", false); |
| 619 | return 0; |
| 620 | } |
| 621 | dumpArgs(argc, argv, -1); |
| 622 | rc = cryptfs_crypto_complete(); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 623 | } else if (!strcmp(argv[1], "enablecrypto")) { |
Paul Lawrence | 45f1053 | 2014-04-04 18:11:56 +0000 | [diff] [blame] | 624 | const char* syntax = "Usage: cryptfs enablecrypto <wipe|inplace> " |
| 625 | "default|password|pin|pattern [passwd]"; |
| 626 | if ( (argc != 4 && argc != 5) |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 627 | || (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace")) ) { |
Paul Lawrence | 45f1053 | 2014-04-04 18:11:56 +0000 | [diff] [blame] | 628 | cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 629 | return 0; |
| 630 | } |
Paul Lawrence | 45f1053 | 2014-04-04 18:11:56 +0000 | [diff] [blame] | 631 | dumpArgs(argc, argv, 4); |
JP Abgrall | 502dc74 | 2013-11-01 13:06:20 -0700 | [diff] [blame] | 632 | |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 633 | int tries; |
| 634 | for (tries = 0; tries < 2; ++tries) { |
Paul Lawrence | 45f1053 | 2014-04-04 18:11:56 +0000 | [diff] [blame] | 635 | int type = getType(argv[3]); |
| 636 | if (type == -1) { |
| 637 | cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, |
| 638 | false); |
| 639 | return 0; |
| 640 | } else if (type == CRYPT_TYPE_DEFAULT) { |
| 641 | rc = cryptfs_enable_default(argv[2], /*allow_reboot*/false); |
| 642 | } else { |
| 643 | rc = cryptfs_enable(argv[2], type, argv[4], |
| 644 | /*allow_reboot*/false); |
| 645 | } |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 646 | |
| 647 | if (rc == 0) { |
| 648 | break; |
| 649 | } else if (tries == 0) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 650 | Process::killProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL); |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 651 | } |
| 652 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 653 | } else if (!strcmp(argv[1], "changepw")) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 654 | const char* syntax = "Usage: cryptfs changepw " |
| 655 | "default|password|pin|pattern [newpasswd]"; |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 656 | const char* password; |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 657 | if (argc == 3) { |
| 658 | password = ""; |
| 659 | } else if (argc == 4) { |
| 660 | password = argv[3]; |
| 661 | } else { |
| 662 | cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 663 | return 0; |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 664 | } |
Paul Lawrence | 45f1053 | 2014-04-04 18:11:56 +0000 | [diff] [blame] | 665 | int type = getType(argv[2]); |
| 666 | if (type == -1) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 667 | cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); |
| 668 | return 0; |
| 669 | } |
| 670 | SLOGD("cryptfs changepw %s {}", argv[2]); |
| 671 | rc = cryptfs_changepw(type, password); |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 672 | } else if (!strcmp(argv[1], "verifypw")) { |
| 673 | if (argc != 3) { |
| 674 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false); |
| 675 | return 0; |
| 676 | } |
| 677 | SLOGD("cryptfs verifypw {}"); |
| 678 | rc = cryptfs_verify_passwd(argv[2]); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 679 | } else if (!strcmp(argv[1], "getfield")) { |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 680 | char *valbuf; |
| 681 | int valbuf_len = PROPERTY_VALUE_MAX; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 682 | |
| 683 | if (argc != 3) { |
| 684 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs getfield <fieldname>", false); |
| 685 | return 0; |
| 686 | } |
| 687 | dumpArgs(argc, argv, -1); |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 688 | |
| 689 | // Increase the buffer size until it is big enough for the field value stored. |
| 690 | while (1) { |
| 691 | valbuf = (char*)malloc(valbuf_len); |
| 692 | if (valbuf == NULL) { |
| 693 | cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", false); |
| 694 | return 0; |
| 695 | } |
| 696 | rc = cryptfs_getfield(argv[2], valbuf, valbuf_len); |
| 697 | if (rc != CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL) { |
| 698 | break; |
| 699 | } |
| 700 | free(valbuf); |
| 701 | valbuf_len *= 2; |
| 702 | } |
| 703 | if (rc == CRYPTO_GETFIELD_OK) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 704 | cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false); |
| 705 | } |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 706 | free(valbuf); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 707 | } else if (!strcmp(argv[1], "setfield")) { |
| 708 | if (argc != 4) { |
| 709 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs setfield <fieldname> <value>", false); |
| 710 | return 0; |
| 711 | } |
| 712 | dumpArgs(argc, argv, -1); |
| 713 | rc = cryptfs_setfield(argv[2], argv[3]); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 714 | } else if (!strcmp(argv[1], "mountdefaultencrypted")) { |
| 715 | SLOGD("cryptfs mountdefaultencrypted"); |
| 716 | dumpArgs(argc, argv, -1); |
| 717 | rc = cryptfs_mount_default_encrypted(); |
| 718 | } else if (!strcmp(argv[1], "getpwtype")) { |
| 719 | SLOGD("cryptfs getpwtype"); |
| 720 | dumpArgs(argc, argv, -1); |
| 721 | switch(cryptfs_get_password_type()) { |
| 722 | case CRYPT_TYPE_PASSWORD: |
| 723 | cli->sendMsg(ResponseCode::PasswordTypeResult, "password", false); |
| 724 | return 0; |
| 725 | case CRYPT_TYPE_PATTERN: |
| 726 | cli->sendMsg(ResponseCode::PasswordTypeResult, "pattern", false); |
| 727 | return 0; |
| 728 | case CRYPT_TYPE_PIN: |
| 729 | cli->sendMsg(ResponseCode::PasswordTypeResult, "pin", false); |
| 730 | return 0; |
| 731 | case CRYPT_TYPE_DEFAULT: |
| 732 | cli->sendMsg(ResponseCode::PasswordTypeResult, "default", false); |
| 733 | return 0; |
| 734 | default: |
| 735 | /** @TODO better error and make sure handled by callers */ |
| 736 | cli->sendMsg(ResponseCode::OpFailedStorageNotFound, "Error", false); |
| 737 | return 0; |
| 738 | } |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 739 | } else if (!strcmp(argv[1], "getpw")) { |
| 740 | SLOGD("cryptfs getpw"); |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 741 | dumpArgs(argc, argv, -1); |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 742 | const char* password = cryptfs_get_password(); |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 743 | if (password) { |
Paul Lawrence | b25302e | 2014-11-11 12:26:09 -0800 | [diff] [blame] | 744 | char* message = 0; |
| 745 | int size = asprintf(&message, "{{sensitive}} %s", password); |
| 746 | if (size != -1) { |
| 747 | cli->sendMsg(ResponseCode::CommandOkay, message, false); |
| 748 | memset(message, 0, size); |
| 749 | free (message); |
| 750 | return 0; |
| 751 | } |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 752 | } |
| 753 | rc = -1; |
| 754 | } else if (!strcmp(argv[1], "clearpw")) { |
| 755 | SLOGD("cryptfs clearpw"); |
| 756 | dumpArgs(argc, argv, -1); |
| 757 | cryptfs_clear_password(); |
| 758 | rc = 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 759 | } else { |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 760 | dumpArgs(argc, argv, -1); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 761 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 762 | return 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 763 | } |
| 764 | |
Jason parks | 0167cb1 | 2011-01-20 18:30:39 -0600 | [diff] [blame] | 765 | // Always report that the command succeeded and return the error code. |
| 766 | // The caller will check the return value to see what the error was. |
| 767 | char msg[255]; |
| 768 | snprintf(msg, sizeof(msg), "%d", rc); |
| 769 | cli->sendMsg(ResponseCode::CommandOkay, msg, false); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 770 | |
| 771 | return 0; |
| 772 | } |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 773 | |
| 774 | CommandListener::FstrimCmd::FstrimCmd() : |
| 775 | VoldCommand("fstrim") { |
| 776 | } |
| 777 | int CommandListener::FstrimCmd::runCommand(SocketClient *cli, |
| 778 | int argc, char **argv) { |
| 779 | if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) { |
| 780 | cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false); |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | if (argc < 2) { |
| 785 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | int rc = 0; |
| 790 | |
| 791 | if (!strcmp(argv[1], "dotrim")) { |
| 792 | if (argc != 2) { |
| 793 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dotrim", false); |
| 794 | return 0; |
| 795 | } |
| 796 | dumpArgs(argc, argv, -1); |
JP Abgrall | 422bdb7 | 2014-07-29 13:24:56 -0700 | [diff] [blame] | 797 | rc = fstrim_filesystems(0); |
| 798 | } else if (!strcmp(argv[1], "dodtrim")) { |
| 799 | if (argc != 2) { |
| 800 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dodtrim", false); |
| 801 | return 0; |
| 802 | } |
| 803 | dumpArgs(argc, argv, -1); |
| 804 | rc = fstrim_filesystems(1); /* Do Deep Discard trim */ |
Ken Sumrall | b87937c | 2013-03-19 21:46:39 -0700 | [diff] [blame] | 805 | } else { |
| 806 | dumpArgs(argc, argv, -1); |
| 807 | cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fstrim cmd", false); |
| 808 | } |
| 809 | |
| 810 | // Always report that the command succeeded and return the error code. |
| 811 | // The caller will check the return value to see what the error was. |
| 812 | char msg[255]; |
| 813 | snprintf(msg, sizeof(msg), "%d", rc); |
| 814 | cli->sendMsg(ResponseCode::CommandOkay, msg, false); |
| 815 | |
| 816 | return 0; |
| 817 | } |