]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_key.c
v1.4.0: Split brain detection
[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              "  -k (session|user|user_session)\n"
18              "                         Keyring to add to (default: user)\n"
19              "  -h                     Display this help and exit\n"
20              "Report bugs to <linux-bcachefs@vger.kernel.org>");
21 }
22
23 int cmd_unlock(int argc, char *argv[])
24 {
25         const char *keyring = "user";
26         bool check = false;
27         int opt;
28
29         while ((opt = getopt(argc, argv, "ck:h")) != -1)
30                 switch (opt) {
31                 case 'c':
32                         check = true;
33                         break;
34                 case 'k':
35                         keyring = strdup(optarg);
36                         break;
37                 case 'h':
38                         unlock_usage();
39                         exit(EXIT_SUCCESS);
40                 }
41         args_shift(optind);
42
43         char *dev = arg_pop();
44         if (!dev)
45                 die("Please supply a device");
46
47         if (argc)
48                 die("Too many arguments");
49
50         struct bch_opts opts = bch2_opts_empty();
51
52         opt_set(opts, noexcl, true);
53         opt_set(opts, nochanges, true);
54
55         struct bch_sb_handle sb;
56         int ret = bch2_read_super(dev, &opts, &sb);
57         if (ret)
58                 die("Error opening %s: %s", dev, bch2_err_str(ret));
59
60         if (!bch2_sb_is_encrypted(sb.sb))
61                 die("%s is not encrypted", dev);
62
63         if (check)
64                 exit(EXIT_SUCCESS);
65
66         char *passphrase = read_passphrase("Enter passphrase: ");
67
68         bch2_add_key(sb.sb, "user", keyring, passphrase);
69
70         bch2_free_super(&sb);
71         memzero_explicit(passphrase, strlen(passphrase));
72         free(passphrase);
73         return 0;
74 }
75
76 int cmd_set_passphrase(int argc, char *argv[])
77 {
78         struct bch_opts opts = bch2_opts_empty();
79         struct bch_fs *c;
80
81         if (argc < 2)
82                 die("Please supply one or more devices");
83
84         opt_set(opts, nostart, true);
85
86         /*
87          * we use bch2_fs_open() here, instead of just reading the superblock,
88          * to make sure we're opening and updating every component device:
89          */
90
91         c = bch2_fs_open(argv + 1, argc - 1, opts);
92         if (IS_ERR(c))
93                 die("Error opening %s: %s", argv[1], bch2_err_str(PTR_ERR(c)));
94
95         struct bch_sb_field_crypt *crypt = bch2_sb_field_get(c->disk_sb.sb, crypt);
96         if (!crypt)
97                 die("Filesystem does not have encryption enabled");
98
99         struct bch_encrypted_key new_key;
100         new_key.magic = BCH_KEY_MAGIC;
101
102         int ret = bch2_decrypt_sb_key(c, crypt, &new_key.key);
103         if (ret)
104                 die("Error getting current key");
105
106         char *new_passphrase = read_passphrase_twice("Enter new passphrase: ");
107         struct bch_key passphrase_key = derive_passphrase(crypt, new_passphrase);
108
109         if (bch2_chacha_encrypt_key(&passphrase_key, __bch2_sb_key_nonce(c->disk_sb.sb),
110                                     &new_key, sizeof(new_key)))
111                 die("error encrypting key");
112         crypt->key = new_key;
113
114         bch2_revoke_key(c->disk_sb.sb);
115         bch2_write_super(c);
116         bch2_fs_stop(c);
117         return 0;
118 }
119
120 int cmd_remove_passphrase(int argc, char *argv[])
121 {
122         struct bch_opts opts = bch2_opts_empty();
123         struct bch_fs *c;
124
125         if (argc < 2)
126                 die("Please supply one or more devices");
127
128         opt_set(opts, nostart, true);
129         c = bch2_fs_open(argv + 1, argc - 1, opts);
130         if (IS_ERR(c))
131                 die("Error opening %s: %s", argv[1], bch2_err_str(PTR_ERR(c)));
132
133         struct bch_sb_field_crypt *crypt = bch2_sb_field_get(c->disk_sb.sb, crypt);
134         if (!crypt)
135                 die("Filesystem does not have encryption enabled");
136
137         struct bch_encrypted_key new_key;
138         new_key.magic = BCH_KEY_MAGIC;
139
140         int ret = bch2_decrypt_sb_key(c, crypt, &new_key.key);
141         if (ret)
142                 die("Error getting current key");
143
144         crypt->key = new_key;
145
146         bch2_write_super(c);
147         bch2_fs_stop(c);
148         return 0;
149 }