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