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