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] [noui]";
152 // This should be replaced with a command line parser if more options
156 int type = CRYPT_TYPE_DEFAULT;
157 int options = 4; // Optional parameters are at this offset
159 // Minimum 4 parameters
161 } else if (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace") ) {
162 // Second parameter must be wipe or inplace
165 // Third parameter must be valid type
166 type = getType(argv[3]);
169 } else if (type != CRYPT_TYPE_DEFAULT) {
176 // Too few parameters
178 } else if (argc == options) {
180 } else if (argc == options + 1) {
181 // One option, must be noui
182 if (!strcmp(argv[options], "noui")) {
194 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
198 dumpArgs(argc, argv, 4);
201 for (tries = 0; tries < 2; ++tries) {
203 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax,
206 } else if (type == CRYPT_TYPE_DEFAULT) {
207 rc = cryptfs_enable_default(argv[2], no_ui);
209 rc = cryptfs_enable(argv[2], type, argv[4], no_ui);
214 } else if (tries == 0) {
215 Process::killProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
218 } else if (!strcmp(argv[1], "enablefilecrypto")) {
219 const char* syntax = "Usage: cryptfs enablefilecrypto";
221 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
224 dumpArgs(argc, argv, -1);
225 rc = cryptfs_enable_file();
226 } else if (!strcmp(argv[1], "changepw")) {
227 const char* syntax = "Usage: cryptfs changepw "
228 "default|password|pin|pattern [[currentpasswd] newpasswd]";
229 const char* password;
230 const char* currentpassword = "";
234 else if (argc == 4) {
236 } else if (argc == 5) {
237 currentpassword = argv[3];
240 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
243 int type = getType(argv[2]);
245 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
248 SLOGD("cryptfs changepw %s {}", argv[2]);
249 rc = cryptfs_changepw(type, currentpassword, password);
250 } else if (!strcmp(argv[1], "verifypw")) {
252 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false);
255 SLOGD("cryptfs verifypw {}");
256 rc = cryptfs_verify_passwd(argv[2]);
257 } else if (!strcmp(argv[1], "getfield")) {
259 int valbuf_len = PROPERTY_VALUE_MAX;
262 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs getfield <fieldname>", false);
265 dumpArgs(argc, argv, -1);
267 // Increase the buffer size until it is big enough for the field value stored.
269 valbuf = (char*)malloc(valbuf_len);
270 if (valbuf == NULL) {
271 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", false);
274 rc = cryptfs_getfield(argv[2], valbuf, valbuf_len);
275 if (rc != CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL) {
281 if (rc == CRYPTO_GETFIELD_OK) {
282 cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false);
285 } else if (!strcmp(argv[1], "setfield")) {
287 cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs setfield <fieldname> <value>", false);
290 dumpArgs(argc, argv, -1);
291 rc = cryptfs_setfield(argv[2], argv[3]);
292 } else if (!strcmp(argv[1], "mountdefaultencrypted")) {
293 SLOGD("cryptfs mountdefaultencrypted");
294 dumpArgs(argc, argv, -1);
295 rc = cryptfs_mount_default_encrypted();
296 } else if (!strcmp(argv[1], "getpwtype")) {
297 SLOGD("cryptfs getpwtype");
298 dumpArgs(argc, argv, -1);
299 switch(cryptfs_get_password_type()) {
300 case CRYPT_TYPE_PASSWORD:
301 cli->sendMsg(ResponseCode::PasswordTypeResult, "password", false);
303 case CRYPT_TYPE_PATTERN:
304 cli->sendMsg(ResponseCode::PasswordTypeResult, "pattern", false);
307 cli->sendMsg(ResponseCode::PasswordTypeResult, "pin", false);
309 case CRYPT_TYPE_DEFAULT:
310 cli->sendMsg(ResponseCode::PasswordTypeResult, "default", false);
313 /** @TODO better error and make sure handled by callers */
314 cli->sendMsg(ResponseCode::OpFailedStorageNotFound, "Error", false);
317 } else if (!strcmp(argv[1], "getpw")) {
318 SLOGD("cryptfs getpw");
319 dumpArgs(argc, argv, -1);
320 const char* password = cryptfs_get_password();
323 int size = asprintf(&message, "{{sensitive}} %s", password);
325 cli->sendMsg(ResponseCode::CommandOkay, message, false);
326 memset(message, 0, size);
332 } else if (!strcmp(argv[1], "clearpw")) {
333 SLOGD("cryptfs clearpw");
334 dumpArgs(argc, argv, -1);
335 cryptfs_clear_password();
337 } else if (!strcmp(argv[1], "setusercryptopolicies")) {
339 cli->sendMsg(ResponseCode::CommandSyntaxError,
340 "Usage: cryptfs setusercryptopolicies <path>", false);
343 SLOGD("cryptfs setusercryptopolicies");
344 dumpArgs(argc, argv, -1);
345 rc = e4crypt_set_user_crypto_policies(argv[2]);
346 } else if (!strcmp(argv[1], "createnewuserdir")) {
348 cli->sendMsg(ResponseCode::CommandSyntaxError,
349 "Usage: cryptfs createnewuserdir <userHandle> <path>", false);
352 // ext4enc:TODO: send a CommandSyntaxError if argv[2] not an integer
353 SLOGD("cryptfs createnewuserdir");
354 dumpArgs(argc, argv, -1);
355 rc = e4crypt_create_new_user_dir(argv[2], argv[3]);
356 } else if (!strcmp(argv[1], "deleteuserkey")) {
358 cli->sendMsg(ResponseCode::CommandSyntaxError,
359 "Usage: cryptfs deleteuserkey <userHandle>", false);
362 // ext4enc:TODO: send a CommandSyntaxError if argv[2] not an integer
363 SLOGD("cryptfs deleteuserkey");
364 dumpArgs(argc, argv, -1);
365 rc = e4crypt_delete_user_key(argv[2]);
367 dumpArgs(argc, argv, -1);
368 cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false);
372 // Always report that the command succeeded and return the error code.
373 // The caller will check the return value to see what the error was.
375 snprintf(msg, sizeof(msg), "%d", rc);
376 cli->sendMsg(ResponseCode::CommandOkay, msg, false);