Debian Codegraph @

src/cryptenroll/cryptenroll-password.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "ask-password-api.h"
5 #include "cryptenroll-password.h"
6 #include "cryptsetup-util.h"
7 #include "env-util.h"
8 #include "errno-util.h"
9 #include "escape.h"
10 #include "iovec-util.h"
11 #include "log.h"
12 #include "password-quality-util.h"
13 #include "string-util.h"
14 #include "strv.h"
15
16 int load_volume_key_password(
17 struct crypt_device *cd,
18 const char *cd_node,
19 void *ret_vk,
20 size_t *ret_vks) {
21
22 _cleanup_(erase_and_freep) char *envpw = NULL;
23 int r;
24
25 assert_se(cd);
26 assert_se(cd_node);
27 assert_se(ret_vk);
28 assert_se(ret_vks);
29
30 r = getenv_steal_erase("PASSWORD", &envpw);
31 if (r < 0)
32 return log_error_errno(r, "Failed to acquire password from environment: %m");
33 if (r > 0) {
34 r = sym_crypt_volume_key_get(
35 cd,
36 CRYPT_ANY_SLOT,
37 ret_vk,
38 ret_vks,
39 envpw,
40 strlen(envpw));
41 if (r < 0)
42 return log_error_errno(r, "Password from environment variable $PASSWORD did not work: %m");
43 } else {
44 AskPasswordFlags ask_password_flags = ASK_PASSWORD_PUSH_CACHE|ASK_PASSWORD_ACCEPT_CACHED;
45 _cleanup_free_ char *question = NULL, *id = NULL, *disk_path = NULL;
46 unsigned i = 5;
47
48 question = strjoin("Please enter current passphrase for disk ", cd_node, ":");
49 if (!question)
50 return log_oom();
51
52 disk_path = cescape(cd_node);
53 if (!disk_path)
54 return log_oom();
55
56 id = strjoin("cryptenroll:", disk_path);
57 if (!id)
58 return log_oom();
59
60 AskPasswordRequest req = {
61 .tty_fd = -EBADF,
62 .message = question,
63 .icon = "drive-harddisk",
64 .id = id,
65 .keyring = "cryptenroll",
66 .credential = "cryptenroll.passphrase",
67 .until = USEC_INFINITY,
68 .hup_fd = -EBADF,
69 };
70
71 for (;;) {
72 _cleanup_strv_free_erase_ char **passwords = NULL;
73
74 if (--i == 0)
75 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY),
76 "Too many attempts, giving up.");
77
78 r = ask_password_auto(&req, ask_password_flags, &passwords);
79 if (r < 0)
80 return log_error_errno(r, "Failed to query password: %m");
81
82 r = -EPERM;
83 STRV_FOREACH(p, passwords) {
84 r = sym_crypt_volume_key_get(
85 cd,
86 CRYPT_ANY_SLOT,
87 ret_vk,
88 ret_vks,
89 *p,
90 strlen(*p));
91 if (r >= 0)
92 break;
93 }
94 if (r >= 0)
95 break;
96
97 log_error_errno(r, "Password not correct, please try again: %m");
98 ask_password_flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
99 }
100 }
101
102 return r;
103 }
104
105 int enroll_password(
106 struct crypt_device *cd,
107 const struct iovec *volume_key) {
108
109 _cleanup_(erase_and_freep) char *new_password = NULL;
110 _cleanup_free_ char *error = NULL;
111 const char *node;
112 int r, keyslot;
113
114 assert(cd);
115 assert(iovec_is_set(volume_key));
116
117 assert_se(node = sym_crypt_get_device_name(cd));
118
119 r = getenv_steal_erase("NEWPASSWORD", &new_password);
120 if (r < 0)
121 return log_error_errno(r, "Failed to acquire password from environment: %m");
122 if (r == 0) {
123 _cleanup_free_ char *disk_path = NULL, *id = NULL;
124 unsigned i = 5;
125
126 assert_se(node = sym_crypt_get_device_name(cd));
127
128 (void) suggest_passwords();
129
130 disk_path = cescape(node);
131 if (!disk_path)
132 return log_oom();
133
134 id = strjoin("cryptenroll-new:", disk_path);
135 if (!id)
136 return log_oom();
137
138 AskPasswordRequest req = {
139 .tty_fd = -EBADF,
140 .icon = "drive-harddisk",
141 .id = id,
142 .keyring = "cryptenroll",
143 .credential = "cryptenroll.new-passphrase",
144 .until = USEC_INFINITY,
145 .hup_fd = -EBADF,
146 };
147
148 for (;;) {
149 _cleanup_strv_free_erase_ char **passwords = NULL, **passwords2 = NULL;
150 _cleanup_free_ char *question = NULL;
151
152 if (--i == 0)
153 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY),
154 "Too many attempts, giving up.");
155
156 question = strjoin("Please enter new passphrase for disk ", node, ":");
157 if (!question)
158 return log_oom();
159
160 req.message = question;
161
162 r = ask_password_auto(&req, /* flags= */ 0, &passwords);
163 if (r < 0)
164 return log_error_errno(r, "Failed to query password: %m");
165
166 assert(strv_length(passwords) == 1);
167
168 free(question);
169 question = strjoin("Please enter new passphrase for disk ", node, " (repeat):");
170 if (!question)
171 return log_oom();
172
173 req.message = question;
174
175 r = ask_password_auto(&req, /* flags= */ 0, &passwords2);
176 if (r < 0)
177 return log_error_errno(r, "Failed to query password: %m");
178
179 assert(strv_length(passwords2) == 1);
180
181 if (strv_equal(passwords, passwords2)) {
182 new_password = passwords2[0];
183 passwords2 = mfree(passwords2);
184 break;
185 }
186
187 log_error("Password didn't match, try again.");
188 }
189 }
190
191 r = check_password_quality(new_password, /* old= */ NULL, /* user= */ NULL, &error);
192 if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
193 log_warning("Password quality check is not supported, proceeding anyway.");
194 else if (r < 0)
195 return log_error_errno(r, "Failed to check password quality: %m");
196 else if (r == 0)
197 log_warning("Specified password does not pass quality checks (%s), proceeding anyway.", error);
198
199 keyslot = sym_crypt_keyslot_add_by_volume_key(
200 cd,
201 CRYPT_ANY_SLOT,
202 volume_key->iov_base,
203 volume_key->iov_len,
204 new_password,
205 strlen(new_password));
206 if (keyslot < 0)
207 return log_error_errno(keyslot, "Failed to add new password to %s: %m", node);
208
209 log_info("New password enrolled as key slot %i.", keyslot);
210 return keyslot;
211 }