CommandListener.cpp revision 707fd6c7cccc31c0ab0ec1a6ac8b6077c632fc35
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> 19#include <sys/types.h> 20#include <netinet/in.h> 21#include <arpa/inet.h> 22#include <dirent.h> 23#include <errno.h> 24#include <fcntl.h> 25#include <fs_mgr.h> 26#include <stdio.h> 27#include <string.h> 28 29#define LOG_TAG "VoldCmdListener" 30#include <cutils/log.h> 31 32#include <sysutils/SocketClient.h> 33#include <private/android_filesystem_config.h> 34 35#include "CommandListener.h" 36#include "VolumeManager.h" 37#include "ResponseCode.h" 38#include "Process.h" 39#include "Loop.h" 40#include "Devmapper.h" 41#include "cryptfs.h" 42#include "fstrim.h" 43 44#define DUMP_ARGS 0 45 46CommandListener::CommandListener() : 47 FrameworkListener("vold", true) { 48 registerCmd(new DumpCmd()); 49 registerCmd(new VolumeCmd()); 50 registerCmd(new AsecCmd()); 51 registerCmd(new ObbCmd()); 52 registerCmd(new StorageCmd()); 53 registerCmd(new CryptfsCmd()); 54 registerCmd(new FstrimCmd()); 55} 56 57#if DUMP_ARGS 58void CommandListener::dumpArgs(int argc, char **argv, int argObscure) { 59 char buffer[4096]; 60 char *p = buffer; 61 62 memset(buffer, 0, sizeof(buffer)); 63 int i; 64 for (i = 0; i < argc; i++) { 65 unsigned int len = strlen(argv[i]) + 1; // Account for space 66 if (i == argObscure) { 67 len += 2; // Account for {} 68 } 69 if (((p - buffer) + len) < (sizeof(buffer)-1)) { 70 if (i == argObscure) { 71 *p++ = '{'; 72 *p++ = '}'; 73 *p++ = ' '; 74 continue; 75 } 76 strcpy(p, argv[i]); 77 p+= strlen(argv[i]); 78 if (i != (argc -1)) { 79 *p++ = ' '; 80 } 81 } 82 } 83 SLOGD("%s", buffer); 84} 85#else 86void CommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { } 87#endif 88 89CommandListener::DumpCmd::DumpCmd() : 90 VoldCommand("dump") { 91} 92 93int CommandListener::DumpCmd::runCommand(SocketClient *cli, 94 int /*argc*/, char ** /*argv*/) { 95 cli->sendMsg(0, "Dumping loop status", false); 96 if (Loop::dumpState(cli)) { 97 cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true); 98 } 99 cli->sendMsg(0, "Dumping DM status", false); 100 if (Devmapper::dumpState(cli)) { 101 cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true); 102 } 103 cli->sendMsg(0, "Dumping mounted filesystems", false); 104 FILE *fp = fopen("/proc/mounts", "r"); 105 if (fp) { 106 char line[1024]; 107 while (fgets(line, sizeof(line), fp)) { 108 line[strlen(line)-1] = '\0'; 109 cli->sendMsg(0, line, false);; 110 } 111 fclose(fp); 112 } 113 114 cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false); 115 return 0; 116} 117 118CommandListener::VolumeCmd::VolumeCmd() : 119 VoldCommand("volume") { 120} 121 122int CommandListener::VolumeCmd::runCommand(SocketClient *cli, 123 int argc, char **argv) { 124 dumpArgs(argc, argv, -1); 125 126 if (argc < 2) { 127 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 128 return 0; 129 } 130 131 VolumeManager *vm = VolumeManager::Instance(); 132 int rc = 0; 133 134 if (!strcmp(argv[1], "list")) { 135 bool broadcast = argc >= 3 && !strcmp(argv[2], "broadcast"); 136 return vm->listVolumes(cli, broadcast); 137 } else if (!strcmp(argv[1], "debug")) { 138 if (argc != 3 || (argc == 3 && (strcmp(argv[2], "off") && strcmp(argv[2], "on")))) { 139 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume debug <off/on>", false); 140 return 0; 141 } 142 vm->setDebug(!strcmp(argv[2], "on") ? true : false); 143 } else if (!strcmp(argv[1], "mount")) { 144 if (argc != 3) { 145 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mount <path>", false); 146 return 0; 147 } 148 rc = vm->mountVolume(argv[2]); 149 } else if (!strcmp(argv[1], "unmount")) { 150 if (argc < 3 || argc > 4 || 151 ((argc == 4 && strcmp(argv[3], "force")) && 152 (argc == 4 && strcmp(argv[3], "force_and_revert")))) { 153 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume unmount <path> [force|force_and_revert]", false); 154 return 0; 155 } 156 157 bool force = false; 158 bool revert = false; 159 if (argc >= 4 && !strcmp(argv[3], "force")) { 160 force = true; 161 } else if (argc >= 4 && !strcmp(argv[3], "force_and_revert")) { 162 force = true; 163 revert = true; 164 } 165 rc = vm->unmountVolume(argv[2], force, revert); 166 } else if (!strcmp(argv[1], "format")) { 167 if (argc < 3 || argc > 4 || 168 (argc == 4 && strcmp(argv[3], "wipe"))) { 169 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume format <path> [wipe]", false); 170 return 0; 171 } 172 bool wipe = false; 173 if (argc >= 4 && !strcmp(argv[3], "wipe")) { 174 wipe = true; 175 } 176 rc = vm->formatVolume(argv[2], wipe); 177 } else if (!strcmp(argv[1], "share")) { 178 if (argc != 4) { 179 cli->sendMsg(ResponseCode::CommandSyntaxError, 180 "Usage: volume share <path> <method>", false); 181 return 0; 182 } 183 rc = vm->shareVolume(argv[2], argv[3]); 184 } else if (!strcmp(argv[1], "unshare")) { 185 if (argc != 4) { 186 cli->sendMsg(ResponseCode::CommandSyntaxError, 187 "Usage: volume unshare <path> <method>", false); 188 return 0; 189 } 190 rc = vm->unshareVolume(argv[2], argv[3]); 191 } else if (!strcmp(argv[1], "shared")) { 192 bool enabled = false; 193 if (argc != 4) { 194 cli->sendMsg(ResponseCode::CommandSyntaxError, 195 "Usage: volume shared <path> <method>", false); 196 return 0; 197 } 198 199 if (vm->shareEnabled(argv[2], argv[3], &enabled)) { 200 cli->sendMsg( 201 ResponseCode::OperationFailed, "Failed to determine share enable state", true); 202 } else { 203 cli->sendMsg(ResponseCode::ShareEnabledResult, 204 (enabled ? "Share enabled" : "Share disabled"), false); 205 } 206 return 0; 207 } else if (!strcmp(argv[1], "mkdirs")) { 208 if (argc != 3) { 209 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mkdirs <path>", false); 210 return 0; 211 } 212 rc = vm->mkdirs(argv[2]); 213 } else { 214 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume cmd", false); 215 } 216 217 if (!rc) { 218 cli->sendMsg(ResponseCode::CommandOkay, "volume operation succeeded", false); 219 } else { 220 rc = ResponseCode::convertFromErrno(); 221 cli->sendMsg(rc, "volume operation failed", true); 222 } 223 224 return 0; 225} 226 227CommandListener::StorageCmd::StorageCmd() : 228 VoldCommand("storage") { 229} 230 231int CommandListener::StorageCmd::runCommand(SocketClient *cli, 232 int argc, char **argv) { 233 /* Guarantied to be initialized by vold's main() before the CommandListener is active */ 234 extern struct fstab *fstab; 235 236 dumpArgs(argc, argv, -1); 237 238 if (argc < 2) { 239 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 240 return 0; 241 } 242 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 } 252 if (!strcmp(argv[1], "users")) { 253 DIR *dir; 254 struct dirent *de; 255 256 if (argc < 3) { 257 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument: user <mountpoint>", false); 258 return 0; 259 } 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 294CommandListener::AsecCmd::AsecCmd() : 295 VoldCommand("asec") { 296} 297 298void 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) + 307 fpathconf(dirfd(d), _PC_NAME_MAX) + 1; 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)); 327 strlcpy(id, dent->d_name, name_len - 4); 328 cli->sendMsg(ResponseCode::AsecListResult, id, false); 329 } 330 } 331 closedir(d); 332 333 free(dent); 334} 335 336int CommandListener::AsecCmd::runCommand(SocketClient *cli, 337 int argc, char **argv) { 338 if (argc < 2) { 339 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 340 return 0; 341 } 342 343 VolumeManager *vm = VolumeManager::Instance(); 344 int rc = 0; 345 346 if (!strcmp(argv[1], "list")) { 347 dumpArgs(argc, argv, -1); 348 349 listAsecsInDirectory(cli, Volume::SEC_ASECDIR_EXT); 350 listAsecsInDirectory(cli, Volume::SEC_ASECDIR_INT); 351 } else if (!strcmp(argv[1], "create")) { 352 dumpArgs(argc, argv, 5); 353 if (argc != 8) { 354 cli->sendMsg(ResponseCode::CommandSyntaxError, 355 "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> " 356 "<isExternal>", false); 357 return 0; 358 } 359 360 unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512; 361 const bool isExternal = (atoi(argv[7]) == 1); 362 rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal); 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]); 371 } else if (!strcmp(argv[1], "finalize")) { 372 dumpArgs(argc, argv, -1); 373 if (argc != 3) { 374 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false); 375 return 0; 376 } 377 rc = vm->finalizeAsec(argv[2]); 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]); 393 } else if (!strcmp(argv[1], "destroy")) { 394 dumpArgs(argc, argv, -1); 395 if (argc < 3) { 396 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false); 397 return 0; 398 } 399 bool force = false; 400 if (argc > 3 && !strcmp(argv[3], "force")) { 401 force = true; 402 } 403 rc = vm->destroyAsec(argv[2], force); 404 } else if (!strcmp(argv[1], "mount")) { 405 dumpArgs(argc, argv, 3); 406 if (argc != 6) { 407 cli->sendMsg(ResponseCode::CommandSyntaxError, 408 "Usage: asec mount <namespace-id> <key> <ownerUid> <ro|rw>", false); 409 return 0; 410 } 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); 416 } else if (!strcmp(argv[1], "unmount")) { 417 dumpArgs(argc, argv, -1); 418 if (argc < 3) { 419 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false); 420 return 0; 421 } 422 bool force = false; 423 if (argc > 3 && !strcmp(argv[3], "force")) { 424 force = true; 425 } 426 rc = vm->unmountAsec(argv[2], force); 427 } else if (!strcmp(argv[1], "rename")) { 428 dumpArgs(argc, argv, -1); 429 if (argc != 4) { 430 cli->sendMsg(ResponseCode::CommandSyntaxError, 431 "Usage: asec rename <old_id> <new_id>", false); 432 return 0; 433 } 434 rc = vm->renameAsec(argv[2], argv[3]); 435 } else if (!strcmp(argv[1], "path")) { 436 dumpArgs(argc, argv, -1); 437 if (argc != 3) { 438 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false); 439 return 0; 440 } 441 char path[255]; 442 443 if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) { 444 cli->sendMsg(ResponseCode::AsecPathResult, path, false); 445 return 0; 446 } 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 } 459 } else { 460 dumpArgs(argc, argv, -1); 461 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false); 462 } 463 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 471 return 0; 472} 473 474CommandListener::ObbCmd::ObbCmd() : 475 VoldCommand("obb") { 476} 477 478int CommandListener::ObbCmd::runCommand(SocketClient *cli, 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 488 if (!strcmp(argv[1], "list")) { 489 dumpArgs(argc, argv, -1); 490 491 rc = vm->listMountedObbs(cli); 492 } else if (!strcmp(argv[1], "mount")) { 493 dumpArgs(argc, argv, 3); 494 if (argc != 5) { 495 cli->sendMsg(ResponseCode::CommandSyntaxError, 496 "Usage: obb mount <filename> <key> <ownerGid>", false); 497 return 0; 498 } 499 rc = vm->mountObb(argv[2], argv[3], atoi(argv[4])); 500 } else if (!strcmp(argv[1], "unmount")) { 501 dumpArgs(argc, argv, -1); 502 if (argc < 3) { 503 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false); 504 return 0; 505 } 506 bool force = false; 507 if (argc > 3 && !strcmp(argv[3], "force")) { 508 force = true; 509 } 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 } 523 } else { 524 dumpArgs(argc, argv, -1); 525 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false); 526 } 527 528 if (!rc) { 529 cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false); 530 } else { 531 rc = ResponseCode::convertFromErrno(); 532 cli->sendMsg(rc, "obb operation failed", true); 533 } 534 535 return 0; 536} 537 538CommandListener::CryptfsCmd::CryptfsCmd() : 539 VoldCommand("cryptfs") { 540} 541 542static 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 557int CommandListener::CryptfsCmd::runCommand(SocketClient *cli, 558 int argc, char **argv) { 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 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 } 576 dumpArgs(argc, argv, 2); 577 rc = cryptfs_check_passwd(argv[2]); 578 } else if (!strcmp(argv[1], "restart")) { 579 if (argc != 2) { 580 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false); 581 return 0; 582 } 583 dumpArgs(argc, argv, -1); 584 rc = cryptfs_restart(); 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(); 592 } else if (!strcmp(argv[1], "enablecrypto")) { 593 const char* syntax = "Usage: cryptfs enablecrypto <wipe|inplace> " 594 "default|password|pin|pattern [passwd]"; 595 if ( (argc != 4 && argc != 5) 596 || (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace")) ) { 597 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); 598 return 0; 599 } 600 dumpArgs(argc, argv, 4); 601 602 int tries; 603 for (tries = 0; tries < 2; ++tries) { 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 } 615 616 if (rc == 0) { 617 break; 618 } else if (tries == 0) { 619 Process::killProcessesWithOpenFiles(DATA_MNT_POINT, 2); 620 } 621 } 622 } else if (!strcmp(argv[1], "enablefilecrypto")) { 623 const char* syntax = "Usage: cryptfs enablefilecrypto"; 624 if (argc != 2) { 625 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); 626 return 0; 627 } 628 dumpArgs(argc, argv, -1); 629 rc = cryptfs_enable_file(); 630 } else if (!strcmp(argv[1], "changepw")) { 631 const char* syntax = "Usage: cryptfs changepw " 632 "default|password|pin|pattern [newpasswd]"; 633 const char* password; 634 if (argc == 3) { 635 password = ""; 636 } else if (argc == 4) { 637 password = argv[3]; 638 } else { 639 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); 640 return 0; 641 } 642 int type = getType(argv[2]); 643 if (type == -1) { 644 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false); 645 return 0; 646 } 647 SLOGD("cryptfs changepw %s {}", argv[2]); 648 rc = cryptfs_changepw(type, password); 649 } else if (!strcmp(argv[1], "verifypw")) { 650 if (argc != 3) { 651 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false); 652 return 0; 653 } 654 SLOGD("cryptfs verifypw {}"); 655 rc = cryptfs_verify_passwd(argv[2]); 656 } else if (!strcmp(argv[1], "getfield")) { 657 char *valbuf; 658 int valbuf_len = PROPERTY_VALUE_MAX; 659 660 if (argc != 3) { 661 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs getfield <fieldname>", false); 662 return 0; 663 } 664 dumpArgs(argc, argv, -1); 665 666 // Increase the buffer size until it is big enough for the field value stored. 667 while (1) { 668 valbuf = (char*)malloc(valbuf_len); 669 if (valbuf == NULL) { 670 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", false); 671 return 0; 672 } 673 rc = cryptfs_getfield(argv[2], valbuf, valbuf_len); 674 if (rc != CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL) { 675 break; 676 } 677 free(valbuf); 678 valbuf_len *= 2; 679 } 680 if (rc == CRYPTO_GETFIELD_OK) { 681 cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false); 682 } 683 free(valbuf); 684 } else if (!strcmp(argv[1], "setfield")) { 685 if (argc != 4) { 686 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs setfield <fieldname> <value>", false); 687 return 0; 688 } 689 dumpArgs(argc, argv, -1); 690 rc = cryptfs_setfield(argv[2], argv[3]); 691 } else if (!strcmp(argv[1], "mountdefaultencrypted")) { 692 SLOGD("cryptfs mountdefaultencrypted"); 693 dumpArgs(argc, argv, -1); 694 rc = cryptfs_mount_default_encrypted(); 695 } else if (!strcmp(argv[1], "getpwtype")) { 696 SLOGD("cryptfs getpwtype"); 697 dumpArgs(argc, argv, -1); 698 switch(cryptfs_get_password_type()) { 699 case CRYPT_TYPE_PASSWORD: 700 cli->sendMsg(ResponseCode::PasswordTypeResult, "password", false); 701 return 0; 702 case CRYPT_TYPE_PATTERN: 703 cli->sendMsg(ResponseCode::PasswordTypeResult, "pattern", false); 704 return 0; 705 case CRYPT_TYPE_PIN: 706 cli->sendMsg(ResponseCode::PasswordTypeResult, "pin", false); 707 return 0; 708 case CRYPT_TYPE_DEFAULT: 709 cli->sendMsg(ResponseCode::PasswordTypeResult, "default", false); 710 return 0; 711 default: 712 /** @TODO better error and make sure handled by callers */ 713 cli->sendMsg(ResponseCode::OpFailedStorageNotFound, "Error", false); 714 return 0; 715 } 716 } else if (!strcmp(argv[1], "getpw")) { 717 SLOGD("cryptfs getpw"); 718 dumpArgs(argc, argv, -1); 719 const char* password = cryptfs_get_password(); 720 if (password) { 721 char* message = 0; 722 int size = asprintf(&message, "{{sensitive}} %s", password); 723 if (size != -1) { 724 cli->sendMsg(ResponseCode::CommandOkay, message, false); 725 memset(message, 0, size); 726 free (message); 727 return 0; 728 } 729 } 730 rc = -1; 731 } else if (!strcmp(argv[1], "clearpw")) { 732 SLOGD("cryptfs clearpw"); 733 dumpArgs(argc, argv, -1); 734 cryptfs_clear_password(); 735 rc = 0; 736 } else { 737 dumpArgs(argc, argv, -1); 738 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false); 739 return 0; 740 } 741 742 // Always report that the command succeeded and return the error code. 743 // The caller will check the return value to see what the error was. 744 char msg[255]; 745 snprintf(msg, sizeof(msg), "%d", rc); 746 cli->sendMsg(ResponseCode::CommandOkay, msg, false); 747 748 return 0; 749} 750 751CommandListener::FstrimCmd::FstrimCmd() : 752 VoldCommand("fstrim") { 753} 754int CommandListener::FstrimCmd::runCommand(SocketClient *cli, 755 int argc, char **argv) { 756 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) { 757 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run fstrim commands", false); 758 return 0; 759 } 760 761 if (argc < 2) { 762 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false); 763 return 0; 764 } 765 766 int rc = 0; 767 768 if (!strcmp(argv[1], "dotrim")) { 769 if (argc != 2) { 770 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dotrim", false); 771 return 0; 772 } 773 dumpArgs(argc, argv, -1); 774 rc = fstrim_filesystems(0); 775 } else if (!strcmp(argv[1], "dodtrim")) { 776 if (argc != 2) { 777 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: fstrim dodtrim", false); 778 return 0; 779 } 780 dumpArgs(argc, argv, -1); 781 rc = fstrim_filesystems(1); /* Do Deep Discard trim */ 782 } else { 783 dumpArgs(argc, argv, -1); 784 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown fstrim cmd", false); 785 } 786 787 // Always report that the command succeeded and return the error code. 788 // The caller will check the return value to see what the error was. 789 char msg[255]; 790 snprintf(msg, sizeof(msg), "%d", rc); 791 cli->sendMsg(ResponseCode::CommandOkay, msg, false); 792 793 return 0; 794} 795