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