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