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