1 // Copyright 2019 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef _GNU_SOURCE
6 #define _GNU_SOURCE
7 #endif
8
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <linux/fs.h>
12 #include <stdio.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <sys/ioctl.h>
16 #include <unistd.h>
17
18 extern char* program_invocation_short_name;
19
main(int argc,char ** argv)20 int main(int argc, char** argv) {
21 if (argc != 2) {
22 printf("Usage: %s <path_to_directory>\n", program_invocation_short_name);
23 return 1;
24 }
25
26 int dir = open(argv[1], O_DIRECTORY | O_CLOEXEC);
27 if (dir < 0) {
28 perror("Failed to open directory");
29 return 1;
30 }
31
32 struct fscrypt_policy policy;
33 int ret = ioctl(dir, FS_IOC_GET_ENCRYPTION_POLICY, &policy);
34 if (ret < 0) {
35 perror("FS_IOC_GET_ENCRYPTION_POLICY failed");
36 return 1;
37 }
38
39 printf("File system encryption policy:\n");
40 printf("\tversion = %#x\n", policy.version);
41 printf("\tcontents_encryption_mode = %#x\n", policy.contents_encryption_mode);
42 printf("\tfilenames_encryption_mode = %#x\n",
43 policy.filenames_encryption_mode);
44 printf("\tflags = %#x\n", policy.flags);
45 printf("\tmaster_key_descriptor = 0x");
46 for (int i = 0; i < FS_KEY_DESCRIPTOR_SIZE; ++i) {
47 printf("%x", policy.master_key_descriptor[i]);
48 }
49 printf("\n");
50
51 ret = ioctl(dir, FS_IOC_SET_ENCRYPTION_POLICY, &policy);
52 if (ret < 0) {
53 perror("FS_IOC_SET_ENCRYPTION_POLICY failed");
54 return 1;
55 }
56
57 return 0;
58 }
59