]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
00843602 | 2 | |
7f4e0805 LP |
3 | #include <errno.h> |
4 | #include <fcntl.h> | |
a8fbdf54 TA |
5 | #include <inttypes.h> |
6 | #include <limits.h> | |
00843602 | 7 | #include <stdbool.h> |
7f4e0805 | 8 | #include <stddef.h> |
a8fbdf54 TA |
9 | #include <stdint.h> |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
00843602 | 12 | #include <sys/inotify.h> |
7f4e0805 | 13 | #include <sys/signalfd.h> |
a8fbdf54 TA |
14 | #include <sys/stat.h> |
15 | #include <sys/time.h> | |
16 | #include <sys/uio.h> | |
00843602 LP |
17 | #include <sys/un.h> |
18 | #include <termios.h> | |
19 | #include <unistd.h> | |
7f4e0805 | 20 | |
b5efdb8a | 21 | #include "alloc-util.h" |
3ffd4af2 | 22 | #include "ask-password-api.h" |
28db6fbf | 23 | #include "constants.h" |
8806bb4b | 24 | #include "creds-util.h" |
3ffd4af2 | 25 | #include "fd-util.h" |
0d39fa9c | 26 | #include "fileio.h" |
f97b34a6 | 27 | #include "format-util.h" |
c7b7d74e | 28 | #include "fs-util.h" |
d8e32c47 | 29 | #include "glyph-util.h" |
c004493c | 30 | #include "io-util.h" |
cbae575e | 31 | #include "keyring-util.h" |
a8fbdf54 TA |
32 | #include "log.h" |
33 | #include "macro.h" | |
0a970718 | 34 | #include "memory-util.h" |
f5947a5e | 35 | #include "missing_syscall.h" |
35cd0ba5 | 36 | #include "mkdir-label.h" |
df0ff127 | 37 | #include "process-util.h" |
3df3e884 | 38 | #include "random-util.h" |
24882e06 | 39 | #include "signal-util.h" |
00843602 | 40 | #include "socket-util.h" |
07630cea | 41 | #include "string-util.h" |
00843602 LP |
42 | #include "strv.h" |
43 | #include "terminal-util.h" | |
a8fbdf54 | 44 | #include "time-util.h" |
e4de7287 | 45 | #include "tmpfile-util.h" |
affb60b1 | 46 | #include "umask-util.h" |
f3149d57 | 47 | #include "utf8.h" |
7f4e0805 | 48 | |
e287086b LP |
49 | #define KEYRING_TIMEOUT_USEC ((5 * USEC_PER_MINUTE) / 2) |
50 | ||
51 | static int lookup_key(const char *keyname, key_serial_t *ret) { | |
52 | key_serial_t serial; | |
53 | ||
54 | assert(keyname); | |
55 | assert(ret); | |
56 | ||
57 | serial = request_key("user", keyname, NULL, 0); | |
58 | if (serial == -1) | |
9c4615fb | 59 | return negative_errno(); |
e287086b LP |
60 | |
61 | *ret = serial; | |
62 | return 0; | |
63 | } | |
64 | ||
65 | static int retrieve_key(key_serial_t serial, char ***ret) { | |
cbae575e | 66 | _cleanup_(erase_and_freep) void *p = NULL; |
e287086b | 67 | char **l; |
cbae575e LP |
68 | size_t n; |
69 | int r; | |
e287086b LP |
70 | |
71 | assert(ret); | |
72 | ||
cbae575e LP |
73 | r = keyring_read(serial, &p, &n); |
74 | if (r < 0) | |
75 | return r; | |
76 | ||
77 | l = strv_parse_nulstr(p, n); | |
e287086b LP |
78 | if (!l) |
79 | return -ENOMEM; | |
80 | ||
81 | *ret = l; | |
82 | return 0; | |
83 | } | |
84 | ||
85 | static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **passwords) { | |
ab84f5b9 | 86 | _cleanup_strv_free_erase_ char **l = NULL; |
e693a932 | 87 | _cleanup_(erase_and_freep) char *p = NULL; |
e287086b LP |
88 | key_serial_t serial; |
89 | size_t n; | |
90 | int r; | |
91 | ||
92 | assert(keyname); | |
e287086b | 93 | |
9cb5bf91 | 94 | if (!FLAGS_SET(flags, ASK_PASSWORD_PUSH_CACHE)) |
e287086b | 95 | return 0; |
e013e10d LP |
96 | if (strv_isempty(passwords)) |
97 | return 0; | |
e287086b LP |
98 | |
99 | r = lookup_key(keyname, &serial); | |
100 | if (r >= 0) { | |
101 | r = retrieve_key(serial, &l); | |
102 | if (r < 0) | |
103 | return r; | |
104 | } else if (r != -ENOKEY) | |
105 | return r; | |
106 | ||
107 | r = strv_extend_strv(&l, passwords, true); | |
108 | if (r <= 0) | |
109 | return r; | |
110 | ||
111 | r = strv_make_nulstr(l, &p, &n); | |
112 | if (r < 0) | |
113 | return r; | |
114 | ||
b60df13b | 115 | serial = add_key("user", keyname, p, n, KEY_SPEC_USER_KEYRING); |
e287086b LP |
116 | if (serial == -1) |
117 | return -errno; | |
118 | ||
119 | if (keyctl(KEYCTL_SET_TIMEOUT, | |
120 | (unsigned long) serial, | |
121 | (unsigned long) DIV_ROUND_UP(KEYRING_TIMEOUT_USEC, USEC_PER_SEC), 0, 0) < 0) | |
e3ac53a2 | 122 | log_debug_errno(errno, "Failed to adjust kernel keyring key timeout: %m"); |
e287086b | 123 | |
c7b7d74e XF |
124 | /* Tell everyone to check the keyring */ |
125 | (void) touch("/run/systemd/ask-password"); | |
126 | ||
e3ac53a2 | 127 | log_debug("Added key to kernel keyring as %" PRIi32 ".", serial); |
e287086b LP |
128 | |
129 | return 1; | |
130 | } | |
131 | ||
132 | static int add_to_keyring_and_log(const char *keyname, AskPasswordFlags flags, char **passwords) { | |
133 | int r; | |
134 | ||
135 | assert(keyname); | |
e287086b LP |
136 | |
137 | r = add_to_keyring(keyname, flags, passwords); | |
138 | if (r < 0) | |
e3ac53a2 | 139 | return log_debug_errno(r, "Failed to add password to kernel keyring: %m"); |
e287086b LP |
140 | |
141 | return 0; | |
142 | } | |
143 | ||
8a111277 | 144 | static int ask_password_keyring(const char *keyname, AskPasswordFlags flags, char ***ret) { |
e287086b LP |
145 | |
146 | key_serial_t serial; | |
147 | int r; | |
148 | ||
149 | assert(keyname); | |
150 | assert(ret); | |
151 | ||
9cb5bf91 | 152 | if (!FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED)) |
e287086b LP |
153 | return -EUNATCH; |
154 | ||
155 | r = lookup_key(keyname, &serial); | |
16edfadc YW |
156 | if (r < 0) { |
157 | /* when retrieving the distinction between "kernel or container manager don't support | |
158 | * or allow this" and "no matching key known" doesn't matter. Note that we propagate | |
159 | * EACCESS here (even if EPERM not) since that is used if the keyring is available but | |
160 | * we lack access to the key. */ | |
161 | if (ERRNO_IS_NOT_SUPPORTED(r) || r == -EPERM) | |
162 | return -ENOKEY; | |
163 | ||
e287086b | 164 | return r; |
16edfadc | 165 | } |
e287086b LP |
166 | |
167 | return retrieve_key(serial, ret); | |
168 | } | |
169 | ||
8e9d1eec | 170 | static int backspace_chars(int ttyfd, size_t p) { |
58fc840b | 171 | if (ttyfd < 0) |
8e9d1eec | 172 | return 0; |
58fc840b | 173 | |
8e9d1eec ZJS |
174 | _cleanup_free_ char *buf = malloc_multiply(3, p); |
175 | if (!buf) | |
176 | return log_oom(); | |
58fc840b | 177 | |
8e9d1eec ZJS |
178 | for (size_t i = 0; i < p; i++) |
179 | memcpy(buf + 3 * i, "\b \b", 3); | |
58fc840b | 180 | |
8e9d1eec ZJS |
181 | return loop_write(ttyfd, buf, 3*p, false); |
182 | } | |
fd6ac62c | 183 | |
8e9d1eec | 184 | static int backspace_string(int ttyfd, const char *str) { |
fd6ac62c LP |
185 | assert(str); |
186 | ||
f95dbcc2 | 187 | /* Backspaces through enough characters to entirely undo printing of the specified string. */ |
fd6ac62c | 188 | |
8e9d1eec ZJS |
189 | if (ttyfd < 0) |
190 | return 0; | |
191 | ||
192 | size_t m = utf8_n_codepoints(str); | |
f5fbe71d | 193 | if (m == SIZE_MAX) |
8e9d1eec ZJS |
194 | m = strlen(str); /* Not a valid UTF-8 string? If so, let's backspace the number of bytes |
195 | * output. Most likely this happened because we are not in an UTF-8 locale, | |
196 | * and in that case that is the correct thing to do. And even if it's not, | |
197 | * terminals tend to stop backspacing at the leftmost column, hence | |
198 | * backspacing too much should be mostly OK. */ | |
fd6ac62c | 199 | |
8e9d1eec | 200 | return backspace_chars(ttyfd, m); |
fd6ac62c LP |
201 | } |
202 | ||
6b2faba7 FB |
203 | int ask_password_plymouth( |
204 | const char *message, | |
205 | usec_t until, | |
206 | AskPasswordFlags flags, | |
207 | const char *flag_file, | |
208 | char ***ret) { | |
209 | ||
210 | static const union sockaddr_union sa = PLYMOUTH_SOCKET; | |
211 | _cleanup_close_ int fd = -1, notify = -1; | |
212 | _cleanup_free_ char *packet = NULL; | |
213 | ssize_t k; | |
214 | int r, n; | |
215 | struct pollfd pollfd[2] = {}; | |
216 | char buffer[LINE_MAX]; | |
217 | size_t p = 0; | |
218 | enum { | |
219 | POLL_SOCKET, | |
220 | POLL_INOTIFY | |
221 | }; | |
222 | ||
223 | assert(ret); | |
224 | ||
66bff73b LP |
225 | if (!message) |
226 | message = "Password:"; | |
227 | ||
6b2faba7 FB |
228 | if (flag_file) { |
229 | notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK); | |
230 | if (notify < 0) | |
231 | return -errno; | |
232 | ||
233 | r = inotify_add_watch(notify, flag_file, IN_ATTRIB); /* for the link count */ | |
234 | if (r < 0) | |
235 | return -errno; | |
236 | } | |
237 | ||
238 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
239 | if (fd < 0) | |
240 | return -errno; | |
241 | ||
242 | r = connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); | |
243 | if (r < 0) | |
244 | return -errno; | |
245 | ||
9cb5bf91 | 246 | if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED)) { |
6b2faba7 FB |
247 | packet = strdup("c"); |
248 | n = 1; | |
249 | } else if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) | |
250 | packet = NULL; | |
251 | if (!packet) | |
252 | return -ENOMEM; | |
253 | ||
254 | r = loop_write(fd, packet, n + 1, true); | |
255 | if (r < 0) | |
256 | return r; | |
257 | ||
258 | pollfd[POLL_SOCKET].fd = fd; | |
259 | pollfd[POLL_SOCKET].events = POLLIN; | |
260 | pollfd[POLL_INOTIFY].fd = notify; | |
261 | pollfd[POLL_INOTIFY].events = POLLIN; | |
262 | ||
263 | for (;;) { | |
d9e2af0a | 264 | usec_t timeout; |
6b2faba7 | 265 | |
d9e2af0a YW |
266 | if (until > 0) |
267 | timeout = usec_sub_unsigned(until, now(CLOCK_MONOTONIC)); | |
268 | else | |
269 | timeout = USEC_INFINITY; | |
6b2faba7 FB |
270 | |
271 | if (flag_file && access(flag_file, F_OK) < 0) { | |
272 | r = -errno; | |
273 | goto finish; | |
274 | } | |
275 | ||
d9e2af0a YW |
276 | r = ppoll_usec(pollfd, notify >= 0 ? 2 : 1, timeout); |
277 | if (r == -EINTR) | |
278 | continue; | |
279 | if (r < 0) | |
6b2faba7 | 280 | goto finish; |
d9e2af0a | 281 | if (r == 0) { |
6b2faba7 FB |
282 | r = -ETIME; |
283 | goto finish; | |
284 | } | |
285 | ||
286 | if (notify >= 0 && pollfd[POLL_INOTIFY].revents != 0) | |
287 | (void) flush_fd(notify); | |
288 | ||
289 | if (pollfd[POLL_SOCKET].revents == 0) | |
290 | continue; | |
291 | ||
292 | k = read(fd, buffer + p, sizeof(buffer) - p); | |
293 | if (k < 0) { | |
8add30a0 | 294 | if (ERRNO_IS_TRANSIENT(errno)) |
6b2faba7 FB |
295 | continue; |
296 | ||
297 | r = -errno; | |
298 | goto finish; | |
8add30a0 YW |
299 | } |
300 | if (k == 0) { | |
6b2faba7 FB |
301 | r = -EIO; |
302 | goto finish; | |
303 | } | |
304 | ||
305 | p += k; | |
306 | ||
6b2faba7 FB |
307 | if (buffer[0] == 5) { |
308 | ||
9cb5bf91 | 309 | if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED)) { |
6b2faba7 FB |
310 | /* Hmm, first try with cached |
311 | * passwords failed, so let's retry | |
312 | * with a normal password request */ | |
313 | packet = mfree(packet); | |
314 | ||
315 | if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) { | |
316 | r = -ENOMEM; | |
317 | goto finish; | |
318 | } | |
319 | ||
320 | r = loop_write(fd, packet, n+1, true); | |
321 | if (r < 0) | |
322 | goto finish; | |
323 | ||
324 | flags &= ~ASK_PASSWORD_ACCEPT_CACHED; | |
325 | p = 0; | |
326 | continue; | |
327 | } | |
328 | ||
329 | /* No password, because UI not shown */ | |
330 | r = -ENOENT; | |
331 | goto finish; | |
332 | ||
333 | } else if (IN_SET(buffer[0], 2, 9)) { | |
334 | uint32_t size; | |
335 | char **l; | |
336 | ||
337 | /* One or more answers */ | |
338 | if (p < 5) | |
339 | continue; | |
340 | ||
341 | memcpy(&size, buffer+1, sizeof(size)); | |
342 | size = le32toh(size); | |
343 | if (size + 5 > sizeof(buffer)) { | |
344 | r = -EIO; | |
345 | goto finish; | |
346 | } | |
347 | ||
348 | if (p-5 < size) | |
349 | continue; | |
350 | ||
351 | l = strv_parse_nulstr(buffer + 5, size); | |
352 | if (!l) { | |
353 | r = -ENOMEM; | |
354 | goto finish; | |
355 | } | |
356 | ||
357 | *ret = l; | |
358 | break; | |
359 | ||
360 | } else { | |
361 | /* Unknown packet */ | |
362 | r = -EIO; | |
363 | goto finish; | |
364 | } | |
365 | } | |
366 | ||
367 | r = 0; | |
368 | ||
369 | finish: | |
370 | explicit_bzero_safe(buffer, sizeof(buffer)); | |
371 | return r; | |
372 | } | |
373 | ||
8aaf18e0 ZJS |
374 | #define NO_ECHO "(no echo) " |
375 | #define PRESS_TAB "(press TAB for no echo) " | |
376 | #define SKIPPED "(skipped)" | |
377 | ||
7f4e0805 | 378 | int ask_password_tty( |
daa55720 | 379 | int ttyfd, |
7f4e0805 | 380 | const char *message, |
e287086b | 381 | const char *keyname, |
7f4e0805 | 382 | usec_t until, |
e287086b | 383 | AskPasswordFlags flags, |
7f4e0805 | 384 | const char *flag_file, |
c7b7d74e | 385 | char ***ret) { |
7f4e0805 | 386 | |
088dcd8e LP |
387 | enum { |
388 | POLL_TTY, | |
389 | POLL_INOTIFY, | |
390 | _POLL_MAX, | |
391 | }; | |
392 | ||
8aaf18e0 | 393 | bool reset_tty = false, dirty = false, use_color = false, press_tab_visible = false; |
daa55720 | 394 | _cleanup_close_ int cttyfd = -1, notify = -1; |
7f4e0805 | 395 | struct termios old_termios, new_termios; |
f3149d57 | 396 | char passphrase[LINE_MAX + 1] = {}, *x; |
c7b7d74e | 397 | _cleanup_strv_free_erase_ char **l = NULL; |
088dcd8e | 398 | struct pollfd pollfd[_POLL_MAX]; |
f3149d57 | 399 | size_t p = 0, codepoint = 0; |
088dcd8e | 400 | int r; |
7f4e0805 | 401 | |
e287086b LP |
402 | assert(ret); |
403 | ||
9cb5bf91 | 404 | if (FLAGS_SET(flags, ASK_PASSWORD_NO_TTY)) |
e287086b LP |
405 | return -EUNATCH; |
406 | ||
407 | if (!message) | |
408 | message = "Password:"; | |
7f4e0805 | 409 | |
e390c34d | 410 | if (!FLAGS_SET(flags, ASK_PASSWORD_HIDE_EMOJI) && emoji_enabled()) |
52d199e3 LP |
411 | message = strjoina(special_glyph(SPECIAL_GLYPH_LOCK_AND_KEY), " ", message); |
412 | ||
9cb5bf91 | 413 | if (flag_file || (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && keyname)) { |
981e4cd3 | 414 | notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK); |
a497a296 LP |
415 | if (notify < 0) |
416 | return -errno; | |
c7b7d74e XF |
417 | } |
418 | if (flag_file) { | |
a497a296 LP |
419 | if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) |
420 | return -errno; | |
7f4e0805 | 421 | } |
9cb5bf91 | 422 | if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && keyname) { |
c7b7d74e XF |
423 | r = ask_password_keyring(keyname, flags, ret); |
424 | if (r >= 0) | |
425 | return 0; | |
426 | else if (r != -ENOKEY) | |
427 | return r; | |
428 | ||
429 | if (inotify_add_watch(notify, "/run/systemd/ask-password", IN_ATTRIB /* for mtime */) < 0) | |
430 | return -errno; | |
431 | } | |
7f4e0805 | 432 | |
daa55720 LP |
433 | /* If the caller didn't specify a TTY, then use the controlling tty, if we can. */ |
434 | if (ttyfd < 0) | |
435 | ttyfd = cttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC); | |
7f4e0805 | 436 | |
daa55720 | 437 | if (ttyfd >= 0) { |
a497a296 LP |
438 | if (tcgetattr(ttyfd, &old_termios) < 0) |
439 | return -errno; | |
7f4e0805 | 440 | |
9cb5bf91 | 441 | if (FLAGS_SET(flags, ASK_PASSWORD_CONSOLE_COLOR)) |
c2b32159 LP |
442 | use_color = dev_console_colors_enabled(); |
443 | else | |
444 | use_color = colors_enabled(); | |
445 | ||
446 | if (use_color) | |
ac7a9674 LP |
447 | (void) loop_write(ttyfd, ANSI_HIGHLIGHT, STRLEN(ANSI_HIGHLIGHT), false); |
448 | ||
449 | (void) loop_write(ttyfd, message, strlen(message), false); | |
450 | (void) loop_write(ttyfd, " ", 1, false); | |
451 | ||
9cb5bf91 | 452 | if (!FLAGS_SET(flags, ASK_PASSWORD_SILENT) && !FLAGS_SET(flags, ASK_PASSWORD_ECHO)) { |
8aaf18e0 | 453 | if (use_color) |
25e4608b | 454 | (void) loop_write(ttyfd, ansi_grey(), strlen(ansi_grey()), false); |
8aaf18e0 ZJS |
455 | (void) loop_write(ttyfd, PRESS_TAB, strlen(PRESS_TAB), false); |
456 | press_tab_visible = true; | |
457 | } | |
458 | ||
c2b32159 | 459 | if (use_color) |
ac7a9674 | 460 | (void) loop_write(ttyfd, ANSI_NORMAL, STRLEN(ANSI_NORMAL), false); |
7f4e0805 LP |
461 | |
462 | new_termios = old_termios; | |
463 | new_termios.c_lflag &= ~(ICANON|ECHO); | |
464 | new_termios.c_cc[VMIN] = 1; | |
465 | new_termios.c_cc[VTIME] = 0; | |
466 | ||
467 | if (tcsetattr(ttyfd, TCSADRAIN, &new_termios) < 0) { | |
468 | r = -errno; | |
469 | goto finish; | |
470 | } | |
471 | ||
472 | reset_tty = true; | |
473 | } | |
474 | ||
70dee475 LP |
475 | pollfd[POLL_TTY] = (struct pollfd) { |
476 | .fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO, | |
477 | .events = POLLIN, | |
478 | }; | |
479 | pollfd[POLL_INOTIFY] = (struct pollfd) { | |
480 | .fd = notify, | |
481 | .events = POLLIN, | |
482 | }; | |
7f4e0805 LP |
483 | |
484 | for (;;) { | |
e1ed99c8 | 485 | _cleanup_(erase_char) char c; |
d9e2af0a | 486 | usec_t timeout; |
7f4e0805 LP |
487 | ssize_t n; |
488 | ||
d9e2af0a YW |
489 | if (until > 0) |
490 | timeout = usec_sub_unsigned(until, now(CLOCK_MONOTONIC)); | |
491 | else | |
492 | timeout = USEC_INFINITY; | |
7f4e0805 LP |
493 | |
494 | if (flag_file) | |
495 | if (access(flag_file, F_OK) < 0) { | |
496 | r = -errno; | |
497 | goto finish; | |
498 | } | |
499 | ||
d9e2af0a YW |
500 | r = ppoll_usec(pollfd, notify >= 0 ? 2 : 1, timeout); |
501 | if (r == -EINTR) | |
502 | continue; | |
503 | if (r < 0) | |
7f4e0805 | 504 | goto finish; |
d9e2af0a | 505 | if (r == 0) { |
ccc80078 | 506 | r = -ETIME; |
7f4e0805 LP |
507 | goto finish; |
508 | } | |
509 | ||
1f00998c | 510 | if (notify >= 0 && pollfd[POLL_INOTIFY].revents != 0 && keyname) { |
665dfe93 | 511 | (void) flush_fd(notify); |
7f4e0805 | 512 | |
c7b7d74e XF |
513 | r = ask_password_keyring(keyname, flags, ret); |
514 | if (r >= 0) { | |
515 | r = 0; | |
516 | goto finish; | |
517 | } else if (r != -ENOKEY) | |
518 | goto finish; | |
519 | } | |
520 | ||
7f4e0805 LP |
521 | if (pollfd[POLL_TTY].revents == 0) |
522 | continue; | |
523 | ||
981e4cd3 LP |
524 | n = read(ttyfd >= 0 ? ttyfd : STDIN_FILENO, &c, 1); |
525 | if (n < 0) { | |
8add30a0 | 526 | if (ERRNO_IS_TRANSIENT(errno)) |
7f4e0805 LP |
527 | continue; |
528 | ||
529 | r = -errno; | |
530 | goto finish; | |
531 | ||
fd6ac62c | 532 | } |
7f4e0805 | 533 | |
8aaf18e0 ZJS |
534 | if (press_tab_visible) { |
535 | assert(ttyfd >= 0); | |
536 | backspace_chars(ttyfd, strlen(PRESS_TAB)); | |
537 | press_tab_visible = false; | |
538 | } | |
539 | ||
fd6ac62c LP |
540 | /* We treat EOF, newline and NUL byte all as valid end markers */ |
541 | if (n == 0 || c == '\n' || c == 0) | |
7f4e0805 | 542 | break; |
fd6ac62c | 543 | |
d325f244 FB |
544 | if (c == 4) { /* C-d also known as EOT */ |
545 | if (ttyfd >= 0) | |
8aaf18e0 | 546 | (void) loop_write(ttyfd, SKIPPED, strlen(SKIPPED), false); |
d325f244 FB |
547 | |
548 | goto skipped; | |
549 | } | |
550 | ||
fd6ac62c | 551 | if (c == 21) { /* C-u */ |
7f4e0805 | 552 | |
9cb5bf91 | 553 | if (!FLAGS_SET(flags, ASK_PASSWORD_SILENT)) |
8e9d1eec | 554 | (void) backspace_string(ttyfd, passphrase); |
fd6ac62c | 555 | |
87f54463 | 556 | explicit_bzero_safe(passphrase, sizeof(passphrase)); |
fd6ac62c | 557 | p = codepoint = 0; |
7f4e0805 | 558 | |
4c701096 | 559 | } else if (IN_SET(c, '\b', 127)) { |
58fc840b LP |
560 | |
561 | if (p > 0) { | |
fd6ac62c | 562 | size_t q; |
58fc840b | 563 | |
9cb5bf91 | 564 | if (!FLAGS_SET(flags, ASK_PASSWORD_SILENT)) |
8e9d1eec | 565 | (void) backspace_chars(ttyfd, 1); |
58fc840b | 566 | |
92e068b4 ZJS |
567 | /* Remove a full UTF-8 codepoint from the end. For that, figure out where the |
568 | * last one begins */ | |
fd6ac62c LP |
569 | q = 0; |
570 | for (;;) { | |
37ca78a3 | 571 | int z; |
fd6ac62c | 572 | |
f5fbe71d | 573 | z = utf8_encoded_valid_unichar(passphrase + q, SIZE_MAX); |
37ca78a3 | 574 | if (z <= 0) { |
f5fbe71d | 575 | q = SIZE_MAX; /* Invalid UTF8! */ |
fd6ac62c LP |
576 | break; |
577 | } | |
578 | ||
579 | if (q + z >= p) /* This one brings us over the edge */ | |
580 | break; | |
581 | ||
582 | q += z; | |
583 | } | |
584 | ||
f5fbe71d | 585 | p = codepoint = q == SIZE_MAX ? p - 1 : q; |
87f54463 | 586 | explicit_bzero_safe(passphrase + p, sizeof(passphrase) - p); |
fd6ac62c | 587 | |
9cb5bf91 | 588 | } else if (!dirty && !FLAGS_SET(flags, ASK_PASSWORD_SILENT)) { |
441dfe09 | 589 | |
e287086b | 590 | flags |= ASK_PASSWORD_SILENT; |
441dfe09 | 591 | |
92e068b4 ZJS |
592 | /* There are two ways to enter silent mode. Either by pressing backspace as |
593 | * first key (and only as first key), or ... */ | |
fd6ac62c | 594 | |
441dfe09 | 595 | if (ttyfd >= 0) |
8aaf18e0 | 596 | (void) loop_write(ttyfd, NO_ECHO, strlen(NO_ECHO), false); |
441dfe09 | 597 | |
58fc840b | 598 | } else if (ttyfd >= 0) |
ac7a9674 | 599 | (void) loop_write(ttyfd, "\a", 1, false); |
7f4e0805 | 600 | |
9cb5bf91 | 601 | } else if (c == '\t' && !FLAGS_SET(flags, ASK_PASSWORD_SILENT)) { |
58fc840b | 602 | |
8e9d1eec | 603 | (void) backspace_string(ttyfd, passphrase); |
e287086b | 604 | flags |= ASK_PASSWORD_SILENT; |
58fc840b | 605 | |
441dfe09 LP |
606 | /* ... or by pressing TAB at any time. */ |
607 | ||
58fc840b | 608 | if (ttyfd >= 0) |
8aaf18e0 | 609 | (void) loop_write(ttyfd, NO_ECHO, strlen(NO_ECHO), false); |
036eeac5 | 610 | |
fd6ac62c LP |
611 | } else if (p >= sizeof(passphrase)-1) { |
612 | ||
613 | /* Reached the size limit */ | |
614 | if (ttyfd >= 0) | |
615 | (void) loop_write(ttyfd, "\a", 1, false); | |
616 | ||
617 | } else { | |
7f4e0805 LP |
618 | passphrase[p++] = c; |
619 | ||
9cb5bf91 | 620 | if (!FLAGS_SET(flags, ASK_PASSWORD_SILENT) && ttyfd >= 0) { |
fd6ac62c | 621 | /* Check if we got a complete UTF-8 character now. If so, let's output one '*'. */ |
f5fbe71d | 622 | n = utf8_encoded_valid_unichar(passphrase + codepoint, SIZE_MAX); |
f3149d57 | 623 | if (n >= 0) { |
9cb5bf91 | 624 | if (FLAGS_SET(flags, ASK_PASSWORD_ECHO)) |
d26eef92 ZJS |
625 | (void) loop_write(ttyfd, passphrase + codepoint, n, false); |
626 | else | |
627 | (void) loop_write(ttyfd, "*", 1, false); | |
f3149d57 | 628 | codepoint = p; |
f3149d57 ZJS |
629 | } |
630 | } | |
441dfe09 LP |
631 | |
632 | dirty = true; | |
7f4e0805 LP |
633 | } |
634 | } | |
635 | ||
981e4cd3 | 636 | x = strndup(passphrase, p); |
87f54463 | 637 | explicit_bzero_safe(passphrase, sizeof(passphrase)); |
981e4cd3 | 638 | if (!x) { |
7f4e0805 LP |
639 | r = -ENOMEM; |
640 | goto finish; | |
641 | } | |
642 | ||
0e28c86f LP |
643 | r = strv_consume(&l, x); |
644 | if (r < 0) | |
c7b7d74e | 645 | goto finish; |
c7b7d74e | 646 | |
d325f244 | 647 | skipped: |
72c08a47 ZJS |
648 | if (strv_isempty(l)) |
649 | r = log_debug_errno(SYNTHETIC_ERRNO(ECANCELED), "Password query was cancelled."); | |
650 | else { | |
651 | if (keyname) | |
652 | (void) add_to_keyring_and_log(keyname, flags, l); | |
653 | ||
654 | *ret = TAKE_PTR(l); | |
655 | r = 0; | |
656 | } | |
7f4e0805 LP |
657 | |
658 | finish: | |
981e4cd3 | 659 | if (ttyfd >= 0 && reset_tty) { |
ac7a9674 LP |
660 | (void) loop_write(ttyfd, "\n", 1, false); |
661 | (void) tcsetattr(ttyfd, TCSADRAIN, &old_termios); | |
7f4e0805 LP |
662 | } |
663 | ||
664 | return r; | |
665 | } | |
666 | ||
15a3e96f LP |
667 | static int create_socket(char **ret) { |
668 | _cleanup_free_ char *path = NULL; | |
f36a9d59 ZJS |
669 | union sockaddr_union sa; |
670 | socklen_t sa_len; | |
00843602 | 671 | _cleanup_close_ int fd = -1; |
f36a9d59 | 672 | int r; |
7f4e0805 | 673 | |
15a3e96f | 674 | assert(ret); |
7f4e0805 | 675 | |
e62d8c39 | 676 | fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
4a62c710 | 677 | if (fd < 0) |
00843602 | 678 | return -errno; |
7f4e0805 | 679 | |
15a3e96f LP |
680 | if (asprintf(&path, "/run/systemd/ask-password/sck.%" PRIx64, random_u64()) < 0) |
681 | return -ENOMEM; | |
682 | ||
f36a9d59 ZJS |
683 | r = sockaddr_un_set_path(&sa.un, path); |
684 | if (r < 0) | |
685 | return r; | |
686 | sa_len = r; | |
7f4e0805 | 687 | |
2b43402c | 688 | RUN_WITH_UMASK(0177) |
f36a9d59 | 689 | if (bind(fd, &sa.sa, sa_len) < 0) |
00843602 | 690 | return -errno; |
1d6702e8 | 691 | |
2ff48e98 LP |
692 | r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true); |
693 | if (r < 0) | |
694 | return r; | |
7f4e0805 | 695 | |
15a3e96f LP |
696 | *ret = TAKE_PTR(path); |
697 | return TAKE_FD(fd); | |
7f4e0805 LP |
698 | } |
699 | ||
7f4e0805 LP |
700 | int ask_password_agent( |
701 | const char *message, | |
702 | const char *icon, | |
9fa1de96 | 703 | const char *id, |
e287086b | 704 | const char *keyname, |
7f4e0805 | 705 | usec_t until, |
e287086b LP |
706 | AskPasswordFlags flags, |
707 | char ***ret) { | |
7f4e0805 LP |
708 | |
709 | enum { | |
710 | FD_SOCKET, | |
711 | FD_SIGNAL, | |
c7b7d74e | 712 | FD_INOTIFY, |
7f4e0805 LP |
713 | _FD_MAX |
714 | }; | |
715 | ||
c7b7d74e | 716 | _cleanup_close_ int socket_fd = -1, signal_fd = -1, notify = -1, fd = -1; |
2b583ce6 | 717 | char temp[] = "/run/systemd/ask-password/tmp.XXXXXX"; |
7f4e0805 | 718 | char final[sizeof(temp)] = ""; |
981e4cd3 | 719 | _cleanup_free_ char *socket_name = NULL; |
c7b7d74e | 720 | _cleanup_strv_free_erase_ char **l = NULL; |
e287086b | 721 | _cleanup_fclose_ FILE *f = NULL; |
7f4e0805 | 722 | struct pollfd pollfd[_FD_MAX]; |
e287086b | 723 | sigset_t mask, oldmask; |
981e4cd3 | 724 | int r; |
7f4e0805 | 725 | |
e287086b | 726 | assert(ret); |
21bc923a | 727 | |
9cb5bf91 | 728 | if (FLAGS_SET(flags, ASK_PASSWORD_NO_AGENT)) |
e287086b | 729 | return -EUNATCH; |
00843602 | 730 | |
72c0a2c2 LP |
731 | assert_se(sigemptyset(&mask) >= 0); |
732 | assert_se(sigset_add_many(&mask, SIGINT, SIGTERM, -1) >= 0); | |
733 | assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) >= 0); | |
f9b72cd8 | 734 | |
00843602 | 735 | (void) mkdir_p_label("/run/systemd/ask-password", 0755); |
7f4e0805 | 736 | |
9cb5bf91 | 737 | if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && keyname) { |
c7b7d74e XF |
738 | r = ask_password_keyring(keyname, flags, ret); |
739 | if (r >= 0) { | |
740 | r = 0; | |
741 | goto finish; | |
742 | } else if (r != -ENOKEY) | |
743 | goto finish; | |
744 | ||
745 | notify = inotify_init1(IN_CLOEXEC | IN_NONBLOCK); | |
746 | if (notify < 0) { | |
747 | r = -errno; | |
748 | goto finish; | |
749 | } | |
750 | if (inotify_add_watch(notify, "/run/systemd/ask-password", IN_ATTRIB /* for mtime */) < 0) { | |
751 | r = -errno; | |
752 | goto finish; | |
753 | } | |
754 | } | |
755 | ||
646853bd | 756 | fd = mkostemp_safe(temp); |
1d6702e8 | 757 | if (fd < 0) { |
709f6e46 | 758 | r = fd; |
7f4e0805 LP |
759 | goto finish; |
760 | } | |
761 | ||
00843602 | 762 | (void) fchmod(fd, 0644); |
7f4e0805 | 763 | |
4fa744a3 | 764 | f = take_fdopen(&fd, "w"); |
981e4cd3 | 765 | if (!f) { |
00843602 | 766 | r = -errno; |
7f4e0805 LP |
767 | goto finish; |
768 | } | |
769 | ||
981e4cd3 LP |
770 | signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); |
771 | if (signal_fd < 0) { | |
00843602 | 772 | r = -errno; |
7f4e0805 LP |
773 | goto finish; |
774 | } | |
775 | ||
981e4cd3 LP |
776 | socket_fd = create_socket(&socket_name); |
777 | if (socket_fd < 0) { | |
7f4e0805 LP |
778 | r = socket_fd; |
779 | goto finish; | |
780 | } | |
781 | ||
782 | fprintf(f, | |
783 | "[Ask]\n" | |
de0671ee | 784 | "PID="PID_FMT"\n" |
7f4e0805 | 785 | "Socket=%s\n" |
21bc923a | 786 | "AcceptCached=%i\n" |
64845bdc | 787 | "Echo=%i\n" |
1fa94a31 SB |
788 | "NotAfter="USEC_FMT"\n" |
789 | "Silent=%i\n", | |
df0ff127 | 790 | getpid_cached(), |
7f4e0805 | 791 | socket_name, |
9cb5bf91 CH |
792 | FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED), |
793 | FLAGS_SET(flags, ASK_PASSWORD_ECHO), | |
1fa94a31 | 794 | until, |
9cb5bf91 | 795 | FLAGS_SET(flags, ASK_PASSWORD_SILENT)); |
7f4e0805 LP |
796 | |
797 | if (message) | |
798 | fprintf(f, "Message=%s\n", message); | |
799 | ||
800 | if (icon) | |
801 | fprintf(f, "Icon=%s\n", icon); | |
802 | ||
9fa1de96 DH |
803 | if (id) |
804 | fprintf(f, "Id=%s\n", id); | |
805 | ||
dacd6cee | 806 | r = fflush_and_check(f); |
00843602 | 807 | if (r < 0) |
7f4e0805 | 808 | goto finish; |
7f4e0805 LP |
809 | |
810 | memcpy(final, temp, sizeof(temp)); | |
811 | ||
812 | final[sizeof(final)-11] = 'a'; | |
813 | final[sizeof(final)-10] = 's'; | |
814 | final[sizeof(final)-9] = 'k'; | |
815 | ||
816 | if (rename(temp, final) < 0) { | |
00843602 | 817 | r = -errno; |
7f4e0805 LP |
818 | goto finish; |
819 | } | |
820 | ||
821 | zero(pollfd); | |
822 | pollfd[FD_SOCKET].fd = socket_fd; | |
823 | pollfd[FD_SOCKET].events = POLLIN; | |
824 | pollfd[FD_SIGNAL].fd = signal_fd; | |
825 | pollfd[FD_SIGNAL].events = POLLIN; | |
c7b7d74e XF |
826 | pollfd[FD_INOTIFY].fd = notify; |
827 | pollfd[FD_INOTIFY].events = POLLIN; | |
7f4e0805 LP |
828 | |
829 | for (;;) { | |
fb29cdbe | 830 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control; |
7f4e0805 | 831 | char passphrase[LINE_MAX+1]; |
7f4e0805 LP |
832 | struct iovec iovec; |
833 | struct ucred *ucred; | |
d9e2af0a | 834 | usec_t timeout; |
7f4e0805 | 835 | ssize_t n; |
7f4e0805 | 836 | |
d9e2af0a YW |
837 | if (until > 0) |
838 | timeout = usec_sub_unsigned(until, now(CLOCK_MONOTONIC)); | |
839 | else | |
840 | timeout = USEC_INFINITY; | |
7f4e0805 | 841 | |
d9e2af0a YW |
842 | r = ppoll_usec(pollfd, notify >= 0 ? _FD_MAX : _FD_MAX - 1, timeout); |
843 | if (r == -EINTR) | |
844 | continue; | |
845 | if (r < 0) | |
7f4e0805 | 846 | goto finish; |
d9e2af0a | 847 | if (r == 0) { |
7f4e0805 LP |
848 | r = -ETIME; |
849 | goto finish; | |
850 | } | |
851 | ||
21bc923a LP |
852 | if (pollfd[FD_SIGNAL].revents & POLLIN) { |
853 | r = -EINTR; | |
854 | goto finish; | |
855 | } | |
7f4e0805 | 856 | |
c7b7d74e XF |
857 | if (notify >= 0 && pollfd[FD_INOTIFY].revents != 0) { |
858 | (void) flush_fd(notify); | |
859 | ||
860 | r = ask_password_keyring(keyname, flags, ret); | |
861 | if (r >= 0) { | |
862 | r = 0; | |
863 | goto finish; | |
864 | } else if (r != -ENOKEY) | |
865 | goto finish; | |
866 | } | |
867 | ||
868 | if (pollfd[FD_SOCKET].revents == 0) | |
869 | continue; | |
870 | ||
7f4e0805 | 871 | if (pollfd[FD_SOCKET].revents != POLLIN) { |
7f4e0805 LP |
872 | r = -EIO; |
873 | goto finish; | |
874 | } | |
875 | ||
5cfa2c3d | 876 | iovec = IOVEC_MAKE(passphrase, sizeof(passphrase)); |
7f4e0805 | 877 | |
41ab8c67 LP |
878 | struct msghdr msghdr = { |
879 | .msg_iov = &iovec, | |
880 | .msg_iovlen = 1, | |
881 | .msg_control = &control, | |
882 | .msg_controllen = sizeof(control), | |
883 | }; | |
7f4e0805 | 884 | |
3691bcf3 | 885 | n = recvmsg_safe(socket_fd, &msghdr, 0); |
981e4cd3 | 886 | if (n < 0) { |
8add30a0 YW |
887 | if (ERRNO_IS_TRANSIENT(n)) |
888 | continue; | |
889 | if (n == -EXFULL) { | |
890 | log_debug("Got message with truncated control data, ignoring."); | |
891 | continue; | |
892 | } | |
893 | ||
3691bcf3 | 894 | r = (int) n; |
7f4e0805 LP |
895 | goto finish; |
896 | } | |
897 | ||
1c8da044 LP |
898 | cmsg_close_all(&msghdr); |
899 | ||
8add30a0 | 900 | if (n == 0) { |
00843602 | 901 | log_debug("Message too short"); |
7f4e0805 LP |
902 | continue; |
903 | } | |
904 | ||
371d72e0 LP |
905 | ucred = CMSG_FIND_DATA(&msghdr, SOL_SOCKET, SCM_CREDENTIALS, struct ucred); |
906 | if (!ucred) { | |
00843602 | 907 | log_debug("Received message without credentials. Ignoring."); |
7f4e0805 LP |
908 | continue; |
909 | } | |
910 | ||
7f4e0805 | 911 | if (ucred->uid != 0) { |
00843602 | 912 | log_debug("Got request from unprivileged user. Ignoring."); |
7f4e0805 LP |
913 | continue; |
914 | } | |
915 | ||
916 | if (passphrase[0] == '+') { | |
e287086b | 917 | /* An empty message refers to the empty password */ |
8254a475 | 918 | if (n == 1) |
bea1a013 | 919 | l = strv_new(""); |
8254a475 LP |
920 | else |
921 | l = strv_parse_nulstr(passphrase+1, n-1); | |
87f54463 | 922 | explicit_bzero_safe(passphrase, n); |
8254a475 | 923 | if (!l) { |
7f4e0805 LP |
924 | r = -ENOMEM; |
925 | goto finish; | |
926 | } | |
927 | ||
58629001 | 928 | if (strv_isempty(l)) { |
e287086b | 929 | l = strv_free(l); |
00843602 | 930 | log_debug("Invalid packet"); |
21bc923a LP |
931 | continue; |
932 | } | |
933 | ||
e287086b LP |
934 | break; |
935 | } | |
21bc923a | 936 | |
e287086b | 937 | if (passphrase[0] == '-') { |
7f4e0805 LP |
938 | r = -ECANCELED; |
939 | goto finish; | |
7f4e0805 LP |
940 | } |
941 | ||
e287086b | 942 | log_debug("Invalid packet"); |
7f4e0805 LP |
943 | } |
944 | ||
e287086b LP |
945 | if (keyname) |
946 | (void) add_to_keyring_and_log(keyname, flags, l); | |
947 | ||
ae2a15bc | 948 | *ret = TAKE_PTR(l); |
7f4e0805 LP |
949 | r = 0; |
950 | ||
951 | finish: | |
981e4cd3 | 952 | if (socket_name) |
00843602 | 953 | (void) unlink(socket_name); |
7f4e0805 | 954 | |
00843602 | 955 | (void) unlink(temp); |
7f4e0805 LP |
956 | |
957 | if (final[0]) | |
00843602 | 958 | (void) unlink(final); |
7f4e0805 | 959 | |
f9b72cd8 | 960 | assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0); |
7f4e0805 LP |
961 | return r; |
962 | } | |
260ab287 | 963 | |
8806bb4b LP |
964 | static int ask_password_credential(const char *credential_name, AskPasswordFlags flags, char ***ret) { |
965 | _cleanup_(erase_and_freep) char *buffer = NULL; | |
966 | size_t size; | |
967 | char **l; | |
968 | int r; | |
969 | ||
970 | assert(credential_name); | |
971 | assert(ret); | |
972 | ||
973 | r = read_credential(credential_name, (void**) &buffer, &size); | |
974 | if (IN_SET(r, -ENXIO, -ENOENT)) /* No credentials passed or this credential not defined? */ | |
975 | return -ENOKEY; | |
976 | ||
977 | l = strv_parse_nulstr(buffer, size); | |
978 | if (!l) | |
979 | return -ENOMEM; | |
980 | ||
981 | *ret = l; | |
982 | return 0; | |
983 | } | |
984 | ||
00843602 LP |
985 | int ask_password_auto( |
986 | const char *message, | |
987 | const char *icon, | |
8806bb4b LP |
988 | const char *id, /* id in "ask-password" protocol */ |
989 | const char *key_name, /* name in kernel keyring */ | |
990 | const char *credential_name, /* name in $CREDENTIALS_DIRECTORY directory */ | |
00843602 | 991 | usec_t until, |
e287086b LP |
992 | AskPasswordFlags flags, |
993 | char ***ret) { | |
00843602 LP |
994 | |
995 | int r; | |
996 | ||
e287086b | 997 | assert(ret); |
21bc923a | 998 | |
9cb5bf91 | 999 | if (!FLAGS_SET(flags, ASK_PASSWORD_NO_CREDENTIAL) && credential_name) { |
8806bb4b LP |
1000 | r = ask_password_credential(credential_name, flags, ret); |
1001 | if (r != -ENOKEY) | |
1002 | return r; | |
1003 | } | |
1004 | ||
9cb5bf91 | 1005 | if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && |
8806bb4b | 1006 | key_name && |
9cb5bf91 CH |
1007 | (FLAGS_SET(flags, ASK_PASSWORD_NO_TTY) || !isatty(STDIN_FILENO)) && |
1008 | FLAGS_SET(flags, ASK_PASSWORD_NO_AGENT)) { | |
8806bb4b | 1009 | r = ask_password_keyring(key_name, flags, ret); |
e287086b LP |
1010 | if (r != -ENOKEY) |
1011 | return r; | |
1012 | } | |
1013 | ||
9cb5bf91 | 1014 | if (!FLAGS_SET(flags, ASK_PASSWORD_NO_TTY) && isatty(STDIN_FILENO)) |
8806bb4b | 1015 | return ask_password_tty(-1, message, key_name, until, flags, NULL, ret); |
00843602 | 1016 | |
9cb5bf91 | 1017 | if (!FLAGS_SET(flags, ASK_PASSWORD_NO_AGENT)) |
8806bb4b | 1018 | return ask_password_agent(message, icon, id, key_name, until, flags, ret); |
e287086b LP |
1019 | |
1020 | return -EUNATCH; | |
260ab287 | 1021 | } |