]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_key.c
Add upstream files
[bcachefs-tools-debian] / cmd_key.c
1 #include <errno.h>
2 #include <unistd.h>
3 #include <uuid/uuid.h>
4
5 #include "cmds.h"
6 #include "libbcachefs/checksum.h"
7 #include "crypto.h"
8 #include "libbcachefs.h"
9
10 static void unlock_usage(void)
11 {
12         puts("bcachefs unlock - unlock an encrypted filesystem so it can be mounted\n"
13              "Usage: bcachefs unlock [OPTION] device\n"
14              "\n"
15              "Options:\n"
16              "  -c                     Check if a device is encrypted\n"
17              "  -h                     Display this help and exit\n"
18              "Report bugs to <linux-bcache@vger.kernel.org>");
19 }
20
21 int cmd_unlock(int argc, char *argv[])
22 {
23         bool check = false;
24         int opt;
25
26         while ((opt = getopt(argc, argv, "ch")) != -1)
27                 switch (opt) {
28                 case 'c':
29                         check = true;
30                         break;
31                 case 'h':
32                         unlock_usage();
33                         exit(EXIT_SUCCESS);
34                 }
35         args_shift(optind);
36
37         char *dev = arg_pop();
38         if (!dev)
39                 die("Please supply a device");
40
41         if (argc)
42                 die("Too many arguments");
43
44         struct bch_opts opts = bch2_opts_empty();
45
46         opt_set(opts, noexcl, true);
47         opt_set(opts, nochanges, true);
48
49         struct bch_sb_handle sb;
50         int ret = bch2_read_super(dev, &opts, &sb);
51         if (ret)
52                 die("Error opening %s: %s", dev, strerror(-ret));
53
54         if (!bch2_sb_is_encrypted(sb.sb))
55                 die("%s is not encrypted", dev);
56
57         if (check)
58                 exit(EXIT_SUCCESS);
59
60         char *passphrase = read_passphrase("Enter passphrase: ");
61
62         bch2_add_key(sb.sb, passphrase);
63
64         bch2_free_super(&sb);
65         memzero_explicit(passphrase, strlen(passphrase));
66         free(passphrase);
67         return 0;
68 }
69
70 int cmd_set_passphrase(int argc, char *argv[])
71 {
72         struct bch_opts opts = bch2_opts_empty();
73         struct bch_fs *c;
74
75         if (argc < 2)
76                 die("Please supply one or more devices");
77
78         opt_set(opts, nostart, true);
79
80         /*
81          * we use bch2_fs_open() here, instead of just reading the superblock,
82          * to make sure we're opening and updating every component device:
83          */
84
85         c = bch2_fs_open(argv + 1, argc - 1, opts);
86         if (IS_ERR(c))
87                 die("Error opening %s: %s", argv[1], strerror(-PTR_ERR(c)));
88
89         struct bch_sb_field_crypt *crypt = bch2_sb_get_crypt(c->disk_sb.sb);
90         if (!crypt)
91                 die("Filesystem does not have encryption enabled");
92
93         struct bch_encrypted_key new_key;
94         new_key.magic = BCH_KEY_MAGIC;
95
96         int ret = bch2_decrypt_sb_key(c, crypt, &new_key.key);
97         if (ret)
98                 die("Error getting current key");
99
100         char *new_passphrase = read_passphrase_twice("Enter new passphrase: ");
101         struct bch_key passphrase_key = derive_passphrase(crypt, new_passphrase);
102
103         if (bch2_chacha_encrypt_key(&passphrase_key, __bch2_sb_key_nonce(c->disk_sb.sb),
104                                     &new_key, sizeof(new_key)))
105                 die("error encrypting key");
106         crypt->key = new_key;
107
108         bch2_write_super(c);
109         bch2_fs_stop(c);
110         return 0;
111 }
112
113 int cmd_remove_passphrase(int argc, char *argv[])
114 {
115         struct bch_opts opts = bch2_opts_empty();
116         struct bch_fs *c;
117
118         if (argc < 2)
119                 die("Please supply one or more devices");
120
121         opt_set(opts, nostart, true);
122         c = bch2_fs_open(argv + 1, argc - 1, opts);
123         if (IS_ERR(c))
124                 die("Error opening %s: %s", argv[1], strerror(-PTR_ERR(c)));
125
126         struct bch_sb_field_crypt *crypt = bch2_sb_get_crypt(c->disk_sb.sb);
127         if (!crypt)
128                 die("Filesystem does not have encryption enabled");
129
130         struct bch_encrypted_key new_key;
131         new_key.magic = BCH_KEY_MAGIC;
132
133         int ret = bch2_decrypt_sb_key(c, crypt, &new_key.key);
134         if (ret)
135                 die("Error getting current key");
136
137         crypt->key = new_key;
138
139         bch2_write_super(c);
140         bch2_fs_stop(c);
141         return 0;
142 }