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