2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/socket.h>
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
31 #define LOG_TAG "VoldCryptCmdListener"
33 #include <base/stringprintf.h>
34 #include <cutils/fs.h>
35 #include <cutils/log.h>
36 #include <cutils/sockets.h>
38 #include <sysutils/SocketClient.h>
39 #include <private/android_filesystem_config.h>
41 #include "CryptCommandListener.h"
43 #include "ResponseCode.h"
45 #include "Ext4Crypt.h"
49 CryptCommandListener::CryptCommandListener() :
50 FrameworkListener("cryptd", true) {
51 registerCmd(new CryptfsCmd());
55 void CryptCommandListener::dumpArgs(int argc, char **argv, int argObscure) {
59 memset(buffer, 0, sizeof(buffer));
61 for (i = 0; i < argc; i++) {
62 unsigned int len = strlen(argv[i]) + 1; // Account for space
63 if (i == argObscure) {
64 len += 2; // Account for {}
66 if (((p - buffer) + len) < (sizeof(buffer)-1)) {
67 if (i == argObscure) {
83 void CryptCommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { }
86 int CryptCommandListener::sendGenericOkFail(SocketClient *cli, int cond) {
88 return cli->sendMsg(ResponseCode::CommandOkay, "Command succeeded", false);
90 return cli->sendMsg(ResponseCode::OperationFailed, "Command failed", false);
94 CryptCommandListener::CryptfsCmd::CryptfsCmd() :
95 VoldCommand("cryptfs") {
98 static int getType(const char* type)
100 if (!strcmp(type, "default")) {
101 return CRYPT_TYPE_DEFAULT;
102 } else if (!strcmp(type, "password")) {
103 return CRYPT_TYPE_PASSWORD;
104 } else if (!strcmp(type, "pin")) {
105 return CRYPT_TYPE_PIN;
106 } else if (!strcmp(type, "pattern")) {
107 return CRYPT_TYPE_PATTERN;
113 int CryptCommandListener::CryptfsCmd::runCommand(SocketClient *cli,
114 int argc, char **argv) {
115 if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
116 cli->sendMsg(ResponseCode::CommandNoPermission, "No permission to run cryptfs commands", false);
121 cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
127 if (!strcmp(argv[1], "checkpw")) {
129 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs checkpw <passwd>", false);
132 dumpArgs(argc, argv, 2);
133 rc = cryptfs_check_passwd(argv[2]);
134 } else if (!strcmp(argv[1], "restart")) {
136 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false);
139 dumpArgs(argc, argv, -1);
140 rc = cryptfs_restart();
141 } else if (!strcmp(argv[1], "cryptocomplete")) {
143 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs cryptocomplete", false);
146 dumpArgs(argc, argv, -1);
147 rc = cryptfs_crypto_complete();
148 } else if (!strcmp(argv[1], "enablecrypto")) {
149 const char* syntax = "Usage: cryptfs enablecrypto <wipe|inplace> "
150 "default|password|pin|pattern [passwd]";
151 if ( (argc != 4 && argc != 5)
152 || (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace")) ) {
153 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
156 dumpArgs(argc, argv, 4);
159 for (tries = 0; tries < 2; ++tries) {
160 int type = getType(argv[3]);
162 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax,
165 } else if (type == CRYPT_TYPE_DEFAULT) {
166 rc = cryptfs_enable_default(argv[2], /*allow_reboot*/false);
168 rc = cryptfs_enable(argv[2], type, argv[4],
169 /*allow_reboot*/false);
174 } else if (tries == 0) {
175 Process::killProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
178 } else if (!strcmp(argv[1], "enablefilecrypto")) {
179 const char* syntax = "Usage: cryptfs enablefilecrypto";
181 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
184 dumpArgs(argc, argv, -1);
185 rc = cryptfs_enable_file();
186 } else if (!strcmp(argv[1], "changepw")) {
187 const char* syntax = "Usage: cryptfs changepw "
188 "default|password|pin|pattern [newpasswd]";
189 const char* password;
192 } else if (argc == 4) {
195 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
198 int type = getType(argv[2]);
200 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
203 SLOGD("cryptfs changepw %s {}", argv[2]);
204 rc = cryptfs_changepw(type, password);
205 } else if (!strcmp(argv[1], "verifypw")) {
207 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false);
210 SLOGD("cryptfs verifypw {}");
211 rc = cryptfs_verify_passwd(argv[2]);
212 } else if (!strcmp(argv[1], "getfield")) {
214 int valbuf_len = PROPERTY_VALUE_MAX;
217 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs getfield <fieldname>", false);
220 dumpArgs(argc, argv, -1);
222 // Increase the buffer size until it is big enough for the field value stored.
224 valbuf = (char*)malloc(valbuf_len);
225 if (valbuf == NULL) {
226 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", false);
229 rc = cryptfs_getfield(argv[2], valbuf, valbuf_len);
230 if (rc != CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL) {
236 if (rc == CRYPTO_GETFIELD_OK) {
237 cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false);
240 } else if (!strcmp(argv[1], "setfield")) {
242 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs setfield <fieldname> <value>", false);
245 dumpArgs(argc, argv, -1);
246 rc = cryptfs_setfield(argv[2], argv[3]);
247 } else if (!strcmp(argv[1], "mountdefaultencrypted")) {
248 SLOGD("cryptfs mountdefaultencrypted");
249 dumpArgs(argc, argv, -1);
250 rc = cryptfs_mount_default_encrypted();
251 } else if (!strcmp(argv[1], "getpwtype")) {
252 SLOGD("cryptfs getpwtype");
253 dumpArgs(argc, argv, -1);
254 switch(cryptfs_get_password_type()) {
255 case CRYPT_TYPE_PASSWORD:
256 cli->sendMsg(ResponseCode::PasswordTypeResult, "password", false);
258 case CRYPT_TYPE_PATTERN:
259 cli->sendMsg(ResponseCode::PasswordTypeResult, "pattern", false);
262 cli->sendMsg(ResponseCode::PasswordTypeResult, "pin", false);
264 case CRYPT_TYPE_DEFAULT:
265 cli->sendMsg(ResponseCode::PasswordTypeResult, "default", false);
268 /** @TODO better error and make sure handled by callers */
269 cli->sendMsg(ResponseCode::OpFailedStorageNotFound, "Error", false);
272 } else if (!strcmp(argv[1], "getpw")) {
273 SLOGD("cryptfs getpw");
274 dumpArgs(argc, argv, -1);
275 const char* password = cryptfs_get_password();
278 int size = asprintf(&message, "{{sensitive}} %s", password);
280 cli->sendMsg(ResponseCode::CommandOkay, message, false);
281 memset(message, 0, size);
287 } else if (!strcmp(argv[1], "clearpw")) {
288 SLOGD("cryptfs clearpw");
289 dumpArgs(argc, argv, -1);
290 cryptfs_clear_password();
292 } else if (!strcmp(argv[1], "setusercryptopolicies")) {
294 cli->sendMsg(ResponseCode::CommandSyntaxError,
295 "Usage: cryptfs setusercryptopolicies <path>", false);
298 SLOGD("cryptfs setusercryptopolicies");
299 dumpArgs(argc, argv, -1);
300 rc = e4crypt_set_user_crypto_policies(argv[2]);
301 } else if (!strcmp(argv[1], "createnewuserdir")) {
303 cli->sendMsg(ResponseCode::CommandSyntaxError,
304 "Usage: cryptfs createnewuserdir <userHandle> <path>", false);
307 // ext4enc:TODO: send a CommandSyntaxError if argv[2] not an integer
308 SLOGD("cryptfs createnewuserdir");
309 dumpArgs(argc, argv, -1);
310 rc = e4crypt_create_new_user_dir(argv[2], argv[3]);
311 } else if (!strcmp(argv[1], "deleteuserkey")) {
313 cli->sendMsg(ResponseCode::CommandSyntaxError,
314 "Usage: cryptfs deleteuserkey <userHandle>", false);
317 // ext4enc:TODO: send a CommandSyntaxError if argv[2] not an integer
318 SLOGD("cryptfs deleteuserkey");
319 dumpArgs(argc, argv, -1);
320 rc = e4crypt_delete_user_key(argv[2]);
322 dumpArgs(argc, argv, -1);
323 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false);
327 // Always report that the command succeeded and return the error code.
328 // The caller will check the return value to see what the error was.
330 snprintf(msg, sizeof(msg), "%d", rc);
331 cli->sendMsg(ResponseCode::CommandOkay, msg, false);