OSDN Git Service

vold3: auto mount CDROM
[android-x86/system-vold.git] / CryptCommandListener.cpp
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdlib.h>
18 #include <sys/socket.h>
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <dirent.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <fs_mgr.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdint.h>
29 #include <inttypes.h>
30
31 #define LOG_TAG "VoldCryptCmdListener"
32
33 #include <base/stringprintf.h>
34 #include <cutils/fs.h>
35 #include <cutils/log.h>
36 #include <cutils/sockets.h>
37
38 #include <sysutils/SocketClient.h>
39 #include <private/android_filesystem_config.h>
40
41 #include "CryptCommandListener.h"
42 #include "Process.h"
43 #include "ResponseCode.h"
44 #include "cryptfs.h"
45 #include "Ext4Crypt.h"
46
47 #define DUMP_ARGS 0
48
49 CryptCommandListener::CryptCommandListener() :
50 FrameworkListener("cryptd", true) {
51     registerCmd(new CryptfsCmd());
52 }
53
54 #if DUMP_ARGS
55 void CryptCommandListener::dumpArgs(int argc, char **argv, int argObscure) {
56     char buffer[4096];
57     char *p = buffer;
58
59     memset(buffer, 0, sizeof(buffer));
60     int i;
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 {}
65         }
66         if (((p - buffer) + len) < (sizeof(buffer)-1)) {
67             if (i == argObscure) {
68                 *p++ = '{';
69                 *p++ = '}';
70                 *p++ = ' ';
71                 continue;
72             }
73             strcpy(p, argv[i]);
74             p+= strlen(argv[i]);
75             if (i != (argc -1)) {
76                 *p++ = ' ';
77             }
78         }
79     }
80     SLOGD("%s", buffer);
81 }
82 #else
83 void CryptCommandListener::dumpArgs(int /*argc*/, char ** /*argv*/, int /*argObscure*/) { }
84 #endif
85
86 int CryptCommandListener::sendGenericOkFail(SocketClient *cli, int cond) {
87     if (!cond) {
88         return cli->sendMsg(ResponseCode::CommandOkay, "Command succeeded", false);
89     } else {
90         return cli->sendMsg(ResponseCode::OperationFailed, "Command failed", false);
91     }
92 }
93
94 CryptCommandListener::CryptfsCmd::CryptfsCmd() :
95                  VoldCommand("cryptfs") {
96 }
97
98 static int getType(const char* type)
99 {
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;
108     } else {
109         return -1;
110     }
111 }
112
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);
117         return 0;
118     }
119
120     if (argc < 2) {
121         cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
122         return 0;
123     }
124
125     int rc = 0;
126
127     if (!strcmp(argv[1], "checkpw")) {
128         if (argc != 3) {
129             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs checkpw <passwd>", false);
130             return 0;
131         }
132         dumpArgs(argc, argv, 2);
133         rc = cryptfs_check_passwd(argv[2]);
134     } else if (!strcmp(argv[1], "restart")) {
135         if (argc != 2) {
136             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false);
137             return 0;
138         }
139         dumpArgs(argc, argv, -1);
140         rc = cryptfs_restart();
141     } else if (!strcmp(argv[1], "cryptocomplete")) {
142         if (argc != 2) {
143             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs cryptocomplete", false);
144             return 0;
145         }
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]";
151
152         // This should be replaced with a command line parser if more options
153         // are added
154         bool valid = true;
155         bool no_ui = false;
156         int type = CRYPT_TYPE_DEFAULT;
157         int options = 4; // Optional parameters are at this offset
158         if (argc < 4) {
159             // Minimum 4 parameters
160             valid = false;
161         } else if (strcmp(argv[2], "wipe") && strcmp(argv[2], "inplace") ) {
162             // Second parameter must be wipe or inplace
163             valid = false;
164         } else {
165             // Third parameter must be valid type
166             type = getType(argv[3]);
167             if (type == -1) {
168                 valid = false;
169             } else if (type != CRYPT_TYPE_DEFAULT) {
170                 options++;
171             }
172         }
173
174         if (valid) {
175             if(argc < options) {
176                 // Too few parameters
177                 valid = false;
178             } else if (argc == options) {
179                 // No more, done
180             } else if (argc == options + 1) {
181                 // One option, must be noui
182                 if (!strcmp(argv[options], "noui")) {
183                     no_ui = true;
184                 } else {
185                     valid = false;
186                 }
187             } else {
188                 // Too many options
189                 valid = false;
190             }
191         }
192
193         if (!valid ) {
194             cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
195             return 0;
196         }
197
198         dumpArgs(argc, argv, 4);
199
200         int tries;
201         for (tries = 0; tries < 2; ++tries) {
202             if (type == -1) {
203                 cli->sendMsg(ResponseCode::CommandSyntaxError, syntax,
204                              false);
205                 return 0;
206             } else if (type == CRYPT_TYPE_DEFAULT) {
207               rc = cryptfs_enable_default(argv[2], no_ui);
208             } else {
209                 rc = cryptfs_enable(argv[2], type, argv[4], no_ui);
210             }
211
212             if (rc == 0) {
213                 break;
214             } else if (tries == 0) {
215                 Process::killProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
216             }
217         }
218     } else if (!strcmp(argv[1], "enablefilecrypto")) {
219         const char* syntax = "Usage: cryptfs enablefilecrypto";
220         if (argc != 2) {
221             cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
222             return 0;
223         }
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 = "";
231         if (argc == 3) {
232             password = "";
233         }
234         else if (argc == 4) {
235             password = argv[3];
236         } else if (argc == 5) {
237             currentpassword = argv[3];
238             password = argv[4];
239         } else {
240             cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
241             return 0;
242         }
243         int type = getType(argv[2]);
244         if (type == -1) {
245             cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
246             return 0;
247         }
248         SLOGD("cryptfs changepw %s {}", argv[2]);
249         rc = cryptfs_changepw(type, currentpassword, password);
250     } else if (!strcmp(argv[1], "verifypw")) {
251         if (argc != 3) {
252             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false);
253             return 0;
254         }
255         SLOGD("cryptfs verifypw {}");
256         rc = cryptfs_verify_passwd(argv[2]);
257     } else if (!strcmp(argv[1], "getfield")) {
258         char *valbuf;
259         int valbuf_len = PROPERTY_VALUE_MAX;
260
261         if (argc != 3) {
262             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs getfield <fieldname>", false);
263             return 0;
264         }
265         dumpArgs(argc, argv, -1);
266
267         // Increase the buffer size until it is big enough for the field value stored.
268         while (1) {
269             valbuf = (char*)malloc(valbuf_len);
270             if (valbuf == NULL) {
271                 cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", false);
272                 return 0;
273             }
274             rc = cryptfs_getfield(argv[2], valbuf, valbuf_len);
275             if (rc != CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL) {
276                 break;
277             }
278             free(valbuf);
279             valbuf_len *= 2;
280         }
281         if (rc == CRYPTO_GETFIELD_OK) {
282             cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false);
283         }
284         free(valbuf);
285     } else if (!strcmp(argv[1], "setfield")) {
286         if (argc != 4) {
287             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs setfield <fieldname> <value>", false);
288             return 0;
289         }
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);
302             return 0;
303         case CRYPT_TYPE_PATTERN:
304             cli->sendMsg(ResponseCode::PasswordTypeResult, "pattern", false);
305             return 0;
306         case CRYPT_TYPE_PIN:
307             cli->sendMsg(ResponseCode::PasswordTypeResult, "pin", false);
308             return 0;
309         case CRYPT_TYPE_DEFAULT:
310             cli->sendMsg(ResponseCode::PasswordTypeResult, "default", false);
311             return 0;
312         default:
313           /** @TODO better error and make sure handled by callers */
314             cli->sendMsg(ResponseCode::OpFailedStorageNotFound, "Error", false);
315             return 0;
316         }
317     } else if (!strcmp(argv[1], "getpw")) {
318         SLOGD("cryptfs getpw");
319         dumpArgs(argc, argv, -1);
320         const char* password = cryptfs_get_password();
321         if (password) {
322             char* message = 0;
323             int size = asprintf(&message, "{{sensitive}} %s", password);
324             if (size != -1) {
325                 cli->sendMsg(ResponseCode::CommandOkay, message, false);
326                 memset(message, 0, size);
327                 free (message);
328                 return 0;
329             }
330         }
331         rc = -1;
332     } else if (!strcmp(argv[1], "clearpw")) {
333         SLOGD("cryptfs clearpw");
334         dumpArgs(argc, argv, -1);
335         cryptfs_clear_password();
336         rc = 0;
337     } else if (!strcmp(argv[1], "setusercryptopolicies")) {
338         if (argc != 3) {
339             cli->sendMsg(ResponseCode::CommandSyntaxError,
340                 "Usage: cryptfs setusercryptopolicies <path>", false);
341             return 0;
342         }
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")) {
347         if (argc != 4) {
348             cli->sendMsg(ResponseCode::CommandSyntaxError,
349                 "Usage: cryptfs createnewuserdir <userHandle> <path>", false);
350             return 0;
351         }
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")) {
357         if (argc != 3) {
358             cli->sendMsg(ResponseCode::CommandSyntaxError,
359                 "Usage: cryptfs deleteuserkey <userHandle>", false);
360             return 0;
361         }
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]);
366     } else {
367         dumpArgs(argc, argv, -1);
368         cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false);
369         return 0;
370     }
371
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.
374     char msg[255];
375     snprintf(msg, sizeof(msg), "%d", rc);
376     cli->sendMsg(ResponseCode::CommandOkay, msg, false);
377
378     return 0;
379 }