1f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat/*
2f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * Copyright (C) 2008 The Android Open Source Project
3f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat *
4f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * Licensed under the Apache License, Version 2.0 (the "License");
5f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * you may not use this file except in compliance with the License.
6f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * You may obtain a copy of the License at
7f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat *
8f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat *      http://www.apache.org/licenses/LICENSE-2.0
9f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat *
10f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * Unless required by applicable law or agreed to in writing, software
11f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * distributed under the License is distributed on an "AS IS" BASIS,
12f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * See the License for the specific language governing permissions and
14f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat * limitations under the License.
15f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat */
16f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
17f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <stdlib.h>
18f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <sys/socket.h>
19a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include <sys/types.h>
20f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <netinet/in.h>
21f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <arpa/inet.h>
22a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include <dirent.h>
23f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <errno.h>
242350c44ff39b4cb2940893964a05f778fc80a436San Mehat#include <fcntl.h>
2537dcda68d334f70e1f7f69a9817def65fe3ee717Olivier Bailly#include <string.h>
26f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
27d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat#define LOG_TAG "VoldCmdListener"
28f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <cutils/log.h>
29f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
30f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include <sysutils/SocketClient.h>
313ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall#include <private/android_filesystem_config.h>
32f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
33f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include "CommandListener.h"
34f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat#include "VolumeManager.h"
35a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat#include "ResponseCode.h"
36586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat#include "Process.h"
372350c44ff39b4cb2940893964a05f778fc80a436San Mehat#include "Xwarp.h"
38d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat#include "Loop.h"
39d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat#include "Devmapper.h"
408f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include "cryptfs.h"
41f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
42f1b736bc5605e92e917ab27f5abf3ba839be2270San MehatCommandListener::CommandListener() :
43149aa3eb65a8cb878781206b1476aae110e0e1fdRobert Greenwalt                 FrameworkListener("vold", true) {
44d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    registerCmd(new DumpCmd());
45eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    registerCmd(new VolumeCmd());
46eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    registerCmd(new AsecCmd());
47508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    registerCmd(new ObbCmd());
48586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat    registerCmd(new StorageCmd());
492350c44ff39b4cb2940893964a05f778fc80a436San Mehat    registerCmd(new XwarpCmd());
508f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    registerCmd(new CryptfsCmd());
51f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
52f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
53d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehatvoid CommandListener::dumpArgs(int argc, char **argv, int argObscure) {
54d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    char buffer[4096];
55d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    char *p = buffer;
56d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
57d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    memset(buffer, 0, sizeof(buffer));
58d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    int i;
59d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    for (i = 0; i < argc; i++) {
608f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        unsigned int len = strlen(argv[i]) + 1; // Account for space
61d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        if (i == argObscure) {
62d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            len += 2; // Account for {}
63d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        }
64d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        if (((p - buffer) + len) < (sizeof(buffer)-1)) {
65d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            if (i == argObscure) {
66d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                *p++ = '{';
67d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                *p++ = '}';
68d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                *p++ = ' ';
69d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                continue;
70d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            }
71d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            strcpy(p, argv[i]);
72d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            p+= strlen(argv[i]);
73d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            if (i != (argc -1)) {
74d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                *p++ = ' ';
75d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            }
76d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        }
77d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    }
7897ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat    SLOGD("%s", buffer);
79d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat}
80d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
81d9a4e358614a0c5f60cc76c0636ee4bb02004a32San MehatCommandListener::DumpCmd::DumpCmd() :
82d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                 VoldCommand("dump") {
83d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat}
84d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
85d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehatint CommandListener::DumpCmd::runCommand(SocketClient *cli,
86d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                                         int argc, char **argv) {
87d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    cli->sendMsg(0, "Dumping loop status", false);
88d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    if (Loop::dumpState(cli)) {
89d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        cli->sendMsg(ResponseCode::CommandOkay, "Loop dump failed", true);
90d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    }
91d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    cli->sendMsg(0, "Dumping DM status", false);
92d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    if (Devmapper::dumpState(cli)) {
93d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true);
94d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    }
9596597e8b840ef671fe5279f8bd64fb09a8b38d4cSan Mehat    cli->sendMsg(0, "Dumping mounted filesystems", false);
9696597e8b840ef671fe5279f8bd64fb09a8b38d4cSan Mehat    FILE *fp = fopen("/proc/mounts", "r");
9796597e8b840ef671fe5279f8bd64fb09a8b38d4cSan Mehat    if (fp) {
9896597e8b840ef671fe5279f8bd64fb09a8b38d4cSan Mehat        char line[1024];
9996597e8b840ef671fe5279f8bd64fb09a8b38d4cSan Mehat        while (fgets(line, sizeof(line), fp)) {
10096597e8b840ef671fe5279f8bd64fb09a8b38d4cSan Mehat            line[strlen(line)-1] = '\0';
10196597e8b840ef671fe5279f8bd64fb09a8b38d4cSan Mehat            cli->sendMsg(0, line, false);;
10296597e8b840ef671fe5279f8bd64fb09a8b38d4cSan Mehat        }
10396597e8b840ef671fe5279f8bd64fb09a8b38d4cSan Mehat        fclose(fp);
10496597e8b840ef671fe5279f8bd64fb09a8b38d4cSan Mehat    }
105d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
106d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    cli->sendMsg(ResponseCode::CommandOkay, "dump complete", false);
107d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    return 0;
108d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat}
109d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
110d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
111eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan MehatCommandListener::VolumeCmd::VolumeCmd() :
112eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat                 VoldCommand("volume") {
113f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat}
114f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
115eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehatint CommandListener::VolumeCmd::runCommand(SocketClient *cli,
116f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat                                                      int argc, char **argv) {
117d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    dumpArgs(argc, argv, -1);
118d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
119eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    if (argc < 2) {
120eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
121eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        return 0;
122eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    }
123f1b736bc5605e92e917ab27f5abf3ba839be2270San Mehat
124eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    VolumeManager *vm = VolumeManager::Instance();
125eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    int rc = 0;
126eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat
127eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    if (!strcmp(argv[1], "list")) {
128eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        return vm->listVolumes(cli);
129d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    } else if (!strcmp(argv[1], "debug")) {
13057df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        if (argc != 3 || (argc == 3 && (strcmp(argv[2], "off") && strcmp(argv[2], "on")))) {
13157df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume debug <off/on>", false);
13257df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            return 0;
13357df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        }
13457df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        vm->setDebug(!strcmp(argv[2], "on") ? true : false);
135eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "mount")) {
13657df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        if (argc != 3) {
13757df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mount <path>", false);
13857df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            return 0;
13957df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        }
140eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        rc = vm->mountVolume(argv[2]);
141eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "unmount")) {
1420b8b59719357fb80c330442787f7d5b1e332263bKen Sumrall        if (argc < 3 || argc > 4 ||
1430b8b59719357fb80c330442787f7d5b1e332263bKen Sumrall           ((argc == 4 && strcmp(argv[3], "force")) &&
1440b8b59719357fb80c330442787f7d5b1e332263bKen Sumrall            (argc == 4 && strcmp(argv[3], "force_and_revert")))) {
1450b8b59719357fb80c330442787f7d5b1e332263bKen Sumrall            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume unmount <path> [force|force_and_revert]", false);
14657df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            return 0;
14757df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        }
14857df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat
1494ba8948dc16463053e21cda5744f519a555080d0San Mehat        bool force = false;
1500b8b59719357fb80c330442787f7d5b1e332263bKen Sumrall        bool revert = false;
1514ba8948dc16463053e21cda5744f519a555080d0San Mehat        if (argc >= 4 && !strcmp(argv[3], "force")) {
1524ba8948dc16463053e21cda5744f519a555080d0San Mehat            force = true;
1530b8b59719357fb80c330442787f7d5b1e332263bKen Sumrall        } else if (argc >= 4 && !strcmp(argv[3], "force_and_revert")) {
1540b8b59719357fb80c330442787f7d5b1e332263bKen Sumrall            force = true;
1550b8b59719357fb80c330442787f7d5b1e332263bKen Sumrall            revert = true;
1564ba8948dc16463053e21cda5744f519a555080d0San Mehat        }
1570b8b59719357fb80c330442787f7d5b1e332263bKen Sumrall        rc = vm->unmountVolume(argv[2], force, revert);
158eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "format")) {
15957df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        if (argc != 3) {
16057df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume format <path>", false);
16157df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            return 0;
16257df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        }
163eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        rc = vm->formatVolume(argv[2]);
164eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "share")) {
16557df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        if (argc != 4) {
16657df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError,
16757df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat                    "Usage: volume share <path> <method>", false);
16857df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            return 0;
16957df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        }
170b9aed74b146beb7499ebc5775e8ae179d16900efSan Mehat        rc = vm->shareVolume(argv[2], argv[3]);
171eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "unshare")) {
17257df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        if (argc != 4) {
17357df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError,
17457df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat                    "Usage: volume unshare <path> <method>", false);
17557df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            return 0;
17657df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        }
177b9aed74b146beb7499ebc5775e8ae179d16900efSan Mehat        rc = vm->unshareVolume(argv[2], argv[3]);
178eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "shared")) {
179eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        bool enabled = false;
18057df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        if (argc != 4) {
18157df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError,
18257df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat                    "Usage: volume shared <path> <method>", false);
18357df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat            return 0;
18457df7bf33968d65c23f3d0dc9f30a8ce2625b1d0San Mehat        }
185eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat
1862b22552f9a3b077f9d0a3624ac6f9b8b332f8a7aSan Mehat        if (vm->shareEnabled(argv[2], argv[3], &enabled)) {
187eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            cli->sendMsg(
188eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat                    ResponseCode::OperationFailed, "Failed to determine share enable state", true);
189eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        } else {
190eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            cli->sendMsg(ResponseCode::ShareEnabledResult,
191eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat                    (enabled ? "Share enabled" : "Share disabled"), false);
192eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        }
193b9aed74b146beb7499ebc5775e8ae179d16900efSan Mehat        return 0;
19449e2bce5b74129c26a35e25d4693cbfe98c4688eSan Mehat    } else {
195eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown volume cmd", false);
19649e2bce5b74129c26a35e25d4693cbfe98c4688eSan Mehat    }
19749e2bce5b74129c26a35e25d4693cbfe98c4688eSan Mehat
198eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    if (!rc) {
199eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        cli->sendMsg(ResponseCode::CommandOkay, "volume operation succeeded", false);
200a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat    } else {
2018f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat        int erno = errno;
2028f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat        rc = ResponseCode::convertFromErrno();
203eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        cli->sendMsg(rc, "volume operation failed", true);
204a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat    }
205a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat
206a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat    return 0;
207a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat}
208a2677e4ad01f250b0765f04adf0acfa6627efc98San Mehat
209586536c60b773e3517531ad8a6cb0de6722c67fcSan MehatCommandListener::StorageCmd::StorageCmd() :
210586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat                 VoldCommand("storage") {
211586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat}
212586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat
213586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehatint CommandListener::StorageCmd::runCommand(SocketClient *cli,
214586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat                                                      int argc, char **argv) {
215d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    dumpArgs(argc, argv, -1);
216d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
217586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat    if (argc < 2) {
218586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
219586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat        return 0;
220586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat    }
221586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat
222586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat    if (!strcmp(argv[1], "users")) {
223586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat        DIR *dir;
224586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat        struct dirent *de;
225586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat
226586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat        if (!(dir = opendir("/proc"))) {
227586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat            cli->sendMsg(ResponseCode::OperationFailed, "Failed to open /proc", true);
228586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat            return 0;
229586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat        }
230586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat
231586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat        while ((de = readdir(dir))) {
232586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat            int pid = Process::getPid(de->d_name);
233586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat
234586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat            if (pid < 0) {
235586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat                continue;
236586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat            }
237586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat
238586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat            char processName[255];
239586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat            Process::getProcessName(pid, processName, sizeof(processName));
240586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat
241586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat            if (Process::checkFileDescriptorSymLinks(pid, argv[2]) ||
242586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat                Process::checkFileMaps(pid, argv[2]) ||
243586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat                Process::checkSymLink(pid, argv[2], "cwd") ||
244586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat                Process::checkSymLink(pid, argv[2], "root") ||
245586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat                Process::checkSymLink(pid, argv[2], "exe")) {
246586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat
247586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat                char msg[1024];
248586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat                snprintf(msg, sizeof(msg), "%d %s", pid, processName);
249586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat                cli->sendMsg(ResponseCode::StorageUsersListResult, msg, false);
250586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat            }
251586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat        }
252586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat        closedir(dir);
253586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat        cli->sendMsg(ResponseCode::CommandOkay, "Storage user list complete", false);
254586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat    } else {
255586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
256586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat    }
257586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat    return 0;
258586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat}
259586536c60b773e3517531ad8a6cb0de6722c67fcSan Mehat
260eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan MehatCommandListener::AsecCmd::AsecCmd() :
261eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat                 VoldCommand("asec") {
262048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat}
263048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat
264344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Rootvoid CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) {
265344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    DIR *d = opendir(directory);
266344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
267344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (!d) {
268344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true);
269344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return;
270344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
271344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
272344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    size_t dirent_len = offsetof(struct dirent, d_name) +
273344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            pathconf(directory, _PC_NAME_MAX) + 1;
274344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
275344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    struct dirent *dent = (struct dirent *) malloc(dirent_len);
276344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (dent == NULL) {
277344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true);
278344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return;
279344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
280344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
281344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    struct dirent *result;
282344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
283344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    while (!readdir_r(d, dent, &result) && result != NULL) {
284344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (dent->d_name[0] == '.')
285344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            continue;
286344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (dent->d_type != DT_REG)
287344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            continue;
288344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        size_t name_len = strlen(dent->d_name);
289344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (name_len > 5 && name_len < 260 &&
290344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                !strcmp(&dent->d_name[name_len - 5], ".asec")) {
291344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            char id[255];
292344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            memset(id, 0, sizeof(id));
2937b0bc8571465666d6cba79bda60b72a97f852c05Kenny Root            strlcpy(id, dent->d_name, name_len - 4);
294344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            cli->sendMsg(ResponseCode::AsecListResult, id, false);
295344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        }
296344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
297344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    closedir(d);
298344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
299344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    free(dent);
300344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root}
301344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
302eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehatint CommandListener::AsecCmd::runCommand(SocketClient *cli,
303eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat                                                      int argc, char **argv) {
304eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    if (argc < 2) {
305eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
306048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat        return 0;
307048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat    }
308048b0801fcd6fcfbb8fa812284c751181e4821b8San Mehat
309eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    VolumeManager *vm = VolumeManager::Instance();
310eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    int rc = 0;
311a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
312eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    if (!strcmp(argv[1], "list")) {
313d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        dumpArgs(argc, argv, -1);
314a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
315344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        listAsecsInDirectory(cli, Volume::SEC_ASECDIR_EXT);
316344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        listAsecsInDirectory(cli, Volume::SEC_ASECDIR_INT);
317eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "create")) {
318d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        dumpArgs(argc, argv, 5);
319344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (argc != 8) {
320eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError,
321344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                    "Usage: asec create <container-id> <size_mb> <fstype> <key> <ownerUid> "
322344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root                    "<isExternal>", false);
323eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            return 0;
324a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        }
325a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
326eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
327344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        const bool isExternal = (atoi(argv[7]) == 1);
328344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal);
329eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "finalize")) {
330d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        dumpArgs(argc, argv, -1);
331eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        if (argc != 3) {
332eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec finalize <container-id>", false);
333eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            return 0;
334eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        }
3358f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat        rc = vm->finalizeAsec(argv[2]);
336344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    } else if (!strcmp(argv[1], "fixperms")) {
337344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        dumpArgs(argc, argv, -1);
338344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if  (argc != 5) {
339344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
340344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            return 0;
341344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        }
342344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
343344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        char *endptr;
344344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        gid_t gid = (gid_t) strtoul(argv[3], &endptr, 10);
345344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        if (*endptr != '\0') {
346344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fixperms <container-id> <gid> <filename>", false);
347344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root            return 0;
348344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        }
349344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
350344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        rc = vm->fixupAsecPermissions(argv[2], gid, argv[4]);
351eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "destroy")) {
352d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        dumpArgs(argc, argv, -1);
3534ba8948dc16463053e21cda5744f519a555080d0San Mehat        if (argc < 3) {
3544ba8948dc16463053e21cda5744f519a555080d0San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec destroy <container-id> [force]", false);
355eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            return 0;
356eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        }
3574ba8948dc16463053e21cda5744f519a555080d0San Mehat        bool force = false;
3584ba8948dc16463053e21cda5744f519a555080d0San Mehat        if (argc > 3 && !strcmp(argv[3], "force")) {
3594ba8948dc16463053e21cda5744f519a555080d0San Mehat            force = true;
3604ba8948dc16463053e21cda5744f519a555080d0San Mehat        }
3618f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat        rc = vm->destroyAsec(argv[2], force);
362eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "mount")) {
363d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        dumpArgs(argc, argv, 3);
364eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        if (argc != 5) {
365eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError,
366eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat                    "Usage: asec mount <namespace-id> <key> <ownerUid>", false);
367eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            return 0;
368eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        }
3698f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat        rc = vm->mountAsec(argv[2], argv[3], atoi(argv[4]));
370eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "unmount")) {
371d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        dumpArgs(argc, argv, -1);
3724ba8948dc16463053e21cda5744f519a555080d0San Mehat        if (argc < 3) {
3734ba8948dc16463053e21cda5744f519a555080d0San Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec unmount <container-id> [force]", false);
374eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            return 0;
375eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        }
3764ba8948dc16463053e21cda5744f519a555080d0San Mehat        bool force = false;
3774ba8948dc16463053e21cda5744f519a555080d0San Mehat        if (argc > 3 && !strcmp(argv[3], "force")) {
3784ba8948dc16463053e21cda5744f519a555080d0San Mehat            force = true;
3794ba8948dc16463053e21cda5744f519a555080d0San Mehat        }
3808f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat        rc = vm->unmountAsec(argv[2], force);
381eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "rename")) {
382d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        dumpArgs(argc, argv, -1);
383eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        if (argc != 4) {
384eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError,
385eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat                    "Usage: asec rename <old_id> <new_id>", false);
386eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            return 0;
387eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        }
3888f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat        rc = vm->renameAsec(argv[2], argv[3]);
389eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat    } else if (!strcmp(argv[1], "path")) {
390d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        dumpArgs(argc, argv, -1);
391eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        if (argc != 3) {
392eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec path <container-id>", false);
393eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            return 0;
394eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        }
395eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        char path[255];
396a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
39788ac2c06539485942bf414efda2d39647fa1a415San Mehat        if (!(rc = vm->getAsecMountPath(argv[2], path, sizeof(path)))) {
398eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat            cli->sendMsg(ResponseCode::AsecPathResult, path, false);
39988ac2c06539485942bf414efda2d39647fa1a415San Mehat            return 0;
400eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        }
401736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn    } else if (!strcmp(argv[1], "fspath")) {
402736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn        dumpArgs(argc, argv, -1);
403736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn        if (argc != 3) {
404736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec fspath <container-id>", false);
405736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn            return 0;
406736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn        }
407736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn        char path[255];
408736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn
409736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn        if (!(rc = vm->getAsecFilesystemPath(argv[2], path, sizeof(path)))) {
410736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn            cli->sendMsg(ResponseCode::AsecPathResult, path, false);
411736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn            return 0;
412736910ca99a40b9add4353bf619e778c40938948Dianne Hackborn        }
413a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    } else {
414d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        dumpArgs(argc, argv, -1);
415eba65e9d438a05f1c5dfd0f8d31bc463a5d08eeeSan Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown asec cmd", false);
416a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
417a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
4188f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat    if (!rc) {
4198f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat        cli->sendMsg(ResponseCode::CommandOkay, "asec operation succeeded", false);
4208f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat    } else {
4218f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat        rc = ResponseCode::convertFromErrno();
4228f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat        cli->sendMsg(rc, "asec operation failed", true);
4238f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat    }
4248f2875b29780312f4edda3d831cc8a99e1648dd5San Mehat
425a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    return 0;
426a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat}
4272350c44ff39b4cb2940893964a05f778fc80a436San Mehat
428508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny RootCommandListener::ObbCmd::ObbCmd() :
429508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root                 VoldCommand("obb") {
430fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root}
431fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root
432508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Rootint CommandListener::ObbCmd::runCommand(SocketClient *cli,
433fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root                                                      int argc, char **argv) {
434fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    if (argc < 2) {
435fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
436fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        return 0;
437fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    }
438fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root
439fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    VolumeManager *vm = VolumeManager::Instance();
440fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    int rc = 0;
441fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root
442508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    if (!strcmp(argv[1], "list")) {
443508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        dumpArgs(argc, argv, -1);
444508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root
445508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        rc = vm->listMountedObbs(cli);
446508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    } else if (!strcmp(argv[1], "mount")) {
447fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root            dumpArgs(argc, argv, 3);
448fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root            if (argc != 5) {
449fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root                cli->sendMsg(ResponseCode::CommandSyntaxError,
4506947904a76b69a1db20a3ddd30c0bcd281922fdeJeff Sharkey                        "Usage: obb mount <filename> <key> <ownerGid>", false);
451fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root                return 0;
452fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root            }
453508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root            rc = vm->mountObb(argv[2], argv[3], atoi(argv[4]));
454fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    } else if (!strcmp(argv[1], "unmount")) {
455fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        dumpArgs(argc, argv, -1);
456fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        if (argc < 3) {
457508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb unmount <source file> [force]", false);
458fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root            return 0;
459fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        }
460fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        bool force = false;
461fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        if (argc > 3 && !strcmp(argv[3], "force")) {
462fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root            force = true;
463fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        }
464508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        rc = vm->unmountObb(argv[2], force);
465508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    } else if (!strcmp(argv[1], "path")) {
466508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        dumpArgs(argc, argv, -1);
467508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        if (argc != 3) {
468508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: obb path <source file>", false);
469508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root            return 0;
470508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        }
471508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        char path[255];
472508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root
473508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        if (!(rc = vm->getObbMountPath(argv[2], path, sizeof(path)))) {
474508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root            cli->sendMsg(ResponseCode::AsecPathResult, path, false);
475508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root            return 0;
476508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        }
477fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    } else {
478fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        dumpArgs(argc, argv, -1);
479508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown obb cmd", false);
480fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    }
481fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root
482fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    if (!rc) {
483508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        cli->sendMsg(ResponseCode::CommandOkay, "obb operation succeeded", false);
484fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    } else {
485fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root        rc = ResponseCode::convertFromErrno();
486508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        cli->sendMsg(rc, "obb operation failed", true);
487fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    }
488fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root
489fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root    return 0;
490fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root}
491fb7c4d5a8a1031cf0e493ff182dcf458e5fe8c77Kenny Root
4922350c44ff39b4cb2940893964a05f778fc80a436San MehatCommandListener::XwarpCmd::XwarpCmd() :
4932350c44ff39b4cb2940893964a05f778fc80a436San Mehat                 VoldCommand("xwarp") {
4942350c44ff39b4cb2940893964a05f778fc80a436San Mehat}
4952350c44ff39b4cb2940893964a05f778fc80a436San Mehat
4962350c44ff39b4cb2940893964a05f778fc80a436San Mehatint CommandListener::XwarpCmd::runCommand(SocketClient *cli,
4972350c44ff39b4cb2940893964a05f778fc80a436San Mehat                                                      int argc, char **argv) {
4982350c44ff39b4cb2940893964a05f778fc80a436San Mehat    if (argc < 2) {
4992350c44ff39b4cb2940893964a05f778fc80a436San Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
5002350c44ff39b4cb2940893964a05f778fc80a436San Mehat        return 0;
5012350c44ff39b4cb2940893964a05f778fc80a436San Mehat    }
5022350c44ff39b4cb2940893964a05f778fc80a436San Mehat
5032350c44ff39b4cb2940893964a05f778fc80a436San Mehat    if (!strcmp(argv[1], "enable")) {
5042350c44ff39b4cb2940893964a05f778fc80a436San Mehat        if (Xwarp::enable()) {
5052350c44ff39b4cb2940893964a05f778fc80a436San Mehat            cli->sendMsg(ResponseCode::OperationFailed, "Failed to enable xwarp", true);
5062350c44ff39b4cb2940893964a05f778fc80a436San Mehat            return 0;
5072350c44ff39b4cb2940893964a05f778fc80a436San Mehat        }
5082350c44ff39b4cb2940893964a05f778fc80a436San Mehat
5092350c44ff39b4cb2940893964a05f778fc80a436San Mehat        cli->sendMsg(ResponseCode::CommandOkay, "Xwarp mirroring started", false);
5102350c44ff39b4cb2940893964a05f778fc80a436San Mehat    } else if (!strcmp(argv[1], "disable")) {
5112350c44ff39b4cb2940893964a05f778fc80a436San Mehat        if (Xwarp::disable()) {
5122350c44ff39b4cb2940893964a05f778fc80a436San Mehat            cli->sendMsg(ResponseCode::OperationFailed, "Failed to disable xwarp", true);
5132350c44ff39b4cb2940893964a05f778fc80a436San Mehat            return 0;
5142350c44ff39b4cb2940893964a05f778fc80a436San Mehat        }
5152350c44ff39b4cb2940893964a05f778fc80a436San Mehat
5162350c44ff39b4cb2940893964a05f778fc80a436San Mehat        cli->sendMsg(ResponseCode::CommandOkay, "Xwarp disabled", false);
5172350c44ff39b4cb2940893964a05f778fc80a436San Mehat    } else if (!strcmp(argv[1], "status")) {
5182350c44ff39b4cb2940893964a05f778fc80a436San Mehat        char msg[255];
5192350c44ff39b4cb2940893964a05f778fc80a436San Mehat        bool r;
5202350c44ff39b4cb2940893964a05f778fc80a436San Mehat        unsigned mirrorPos, maxSize;
5212350c44ff39b4cb2940893964a05f778fc80a436San Mehat
5222350c44ff39b4cb2940893964a05f778fc80a436San Mehat        if (Xwarp::status(&r, &mirrorPos, &maxSize)) {
5232350c44ff39b4cb2940893964a05f778fc80a436San Mehat            cli->sendMsg(ResponseCode::OperationFailed, "Failed to get xwarp status", true);
5242350c44ff39b4cb2940893964a05f778fc80a436San Mehat            return 0;
5252350c44ff39b4cb2940893964a05f778fc80a436San Mehat        }
5262350c44ff39b4cb2940893964a05f778fc80a436San Mehat        snprintf(msg, sizeof(msg), "%s %u %u", (r ? "ready" : "not-ready"), mirrorPos, maxSize);
5272350c44ff39b4cb2940893964a05f778fc80a436San Mehat        cli->sendMsg(ResponseCode::XwarpStatusResult, msg, false);
5282350c44ff39b4cb2940893964a05f778fc80a436San Mehat    } else {
5292350c44ff39b4cb2940893964a05f778fc80a436San Mehat        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown storage cmd", false);
5302350c44ff39b4cb2940893964a05f778fc80a436San Mehat    }
5312350c44ff39b4cb2940893964a05f778fc80a436San Mehat
5322350c44ff39b4cb2940893964a05f778fc80a436San Mehat    return 0;
5332350c44ff39b4cb2940893964a05f778fc80a436San Mehat}
5348f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
5358f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken SumrallCommandListener::CryptfsCmd::CryptfsCmd() :
5368f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall                 VoldCommand("cryptfs") {
5378f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall}
5388f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
5398f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallint CommandListener::CryptfsCmd::runCommand(SocketClient *cli,
5408f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall                                                      int argc, char **argv) {
5413ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall    if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
5423ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall        cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run cryptfs commands", false);
5433ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall        return 0;
5443ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall    }
5453ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall
5468f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    if (argc < 2) {
5478f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
5488f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        return 0;
5498f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    }
5508f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
5518f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    int rc = 0;
5528f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
5538f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    if (!strcmp(argv[1], "checkpw")) {
5548f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        if (argc != 3) {
5558f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs checkpw <passwd>", false);
5568f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall            return 0;
5578f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        }
5588ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        dumpArgs(argc, argv, 2);
5598f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        rc = cryptfs_check_passwd(argv[2]);
5606864b7ec94a57b73c300457955d86dc604aeddf5Ken Sumrall    } else if (!strcmp(argv[1], "restart")) {
5616864b7ec94a57b73c300457955d86dc604aeddf5Ken Sumrall        if (argc != 2) {
5626864b7ec94a57b73c300457955d86dc604aeddf5Ken Sumrall            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false);
5636864b7ec94a57b73c300457955d86dc604aeddf5Ken Sumrall            return 0;
5646864b7ec94a57b73c300457955d86dc604aeddf5Ken Sumrall        }
5658ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        dumpArgs(argc, argv, -1);
5666864b7ec94a57b73c300457955d86dc604aeddf5Ken Sumrall        rc = cryptfs_restart();
5677f7dbaa2784c10fd2989fb303e5edfb8136d53dcKen Sumrall    } else if (!strcmp(argv[1], "cryptocomplete")) {
5687f7dbaa2784c10fd2989fb303e5edfb8136d53dcKen Sumrall        if (argc != 2) {
5697f7dbaa2784c10fd2989fb303e5edfb8136d53dcKen Sumrall            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs cryptocomplete", false);
5707f7dbaa2784c10fd2989fb303e5edfb8136d53dcKen Sumrall            return 0;
5717f7dbaa2784c10fd2989fb303e5edfb8136d53dcKen Sumrall        }
5727f7dbaa2784c10fd2989fb303e5edfb8136d53dcKen Sumrall        dumpArgs(argc, argv, -1);
5737f7dbaa2784c10fd2989fb303e5edfb8136d53dcKen Sumrall        rc = cryptfs_crypto_complete();
5748f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    } else if (!strcmp(argv[1], "enablecrypto")) {
5758f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        if ( (argc != 4) || (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace")) ) {
5768f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs enablecrypto <wipe|inplace> <passwd>", false);
5778f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall            return 0;
5788f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        }
5798ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        dumpArgs(argc, argv, 3);
5808f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        rc = cryptfs_enable(argv[2], argv[3]);
5818ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    } else if (!strcmp(argv[1], "changepw")) {
58270a4b3fd7a84a84bbe6e9d6d4ca3ee2098259fd9Jason parks        if (argc != 3) {
58370a4b3fd7a84a84bbe6e9d6d4ca3ee2098259fd9Jason parks            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs changepw <newpasswd>", false);
5848ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall            return 0;
5858ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        }
58670a4b3fd7a84a84bbe6e9d6d4ca3ee2098259fd9Jason parks        SLOGD("cryptfs changepw {}");
58770a4b3fd7a84a84bbe6e9d6d4ca3ee2098259fd9Jason parks        rc = cryptfs_changepw(argv[2]);
5883ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall    } else if (!strcmp(argv[1], "verifypw")) {
5893ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall        if (argc != 3) {
5903ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false);
5913ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall            return 0;
5923ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall        }
5933ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall        SLOGD("cryptfs verifypw {}");
5943ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrall        rc = cryptfs_verify_passwd(argv[2]);
5958f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    } else {
5968ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        dumpArgs(argc, argv, -1);
5978f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false);
5988f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    }
5998f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
6000167cb15935592deea9abbd6a8bbe904e27bd101Jason parks    // Always report that the command succeeded and return the error code.
6010167cb15935592deea9abbd6a8bbe904e27bd101Jason parks    // The caller will check the return value to see what the error was.
6020167cb15935592deea9abbd6a8bbe904e27bd101Jason parks    char msg[255];
6030167cb15935592deea9abbd6a8bbe904e27bd101Jason parks    snprintf(msg, sizeof(msg), "%d", rc);
6040167cb15935592deea9abbd6a8bbe904e27bd101Jason parks    cli->sendMsg(ResponseCode::CommandOkay, msg, false);
6058f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
6068f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    return 0;
6078f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall}
608