]>
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 | 7 | #include <poll.h> |
a8fbdf54 | 8 | #include <signal.h> |
00843602 | 9 | #include <stdbool.h> |
7f4e0805 | 10 | #include <stddef.h> |
a8fbdf54 TA |
11 | #include <stdint.h> |
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
00843602 LP |
14 | #include <string.h> |
15 | #include <sys/inotify.h> | |
7f4e0805 | 16 | #include <sys/signalfd.h> |
00843602 | 17 | #include <sys/socket.h> |
a8fbdf54 TA |
18 | #include <sys/stat.h> |
19 | #include <sys/time.h> | |
20 | #include <sys/uio.h> | |
00843602 LP |
21 | #include <sys/un.h> |
22 | #include <termios.h> | |
23 | #include <unistd.h> | |
7f4e0805 | 24 | |
b5efdb8a | 25 | #include "alloc-util.h" |
3ffd4af2 LP |
26 | #include "ask-password-api.h" |
27 | #include "fd-util.h" | |
0d39fa9c | 28 | #include "fileio.h" |
f97b34a6 | 29 | #include "format-util.h" |
c004493c | 30 | #include "io-util.h" |
a8fbdf54 TA |
31 | #include "log.h" |
32 | #include "macro.h" | |
e287086b | 33 | #include "missing.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" |
affb60b1 | 43 | #include "umask-util.h" |
f3149d57 | 44 | #include "utf8.h" |
00843602 | 45 | #include "util.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) { | |
64 | _cleanup_free_ char *p = NULL; | |
65 | long m = 100, n; | |
66 | char **l; | |
67 | ||
68 | assert(ret); | |
69 | ||
70 | for (;;) { | |
71 | p = new(char, m); | |
72 | if (!p) | |
73 | return -ENOMEM; | |
74 | ||
75 | n = keyctl(KEYCTL_READ, (unsigned long) serial, (unsigned long) p, (unsigned long) m, 0); | |
76 | if (n < 0) | |
77 | return -errno; | |
78 | ||
79 | if (n < m) | |
80 | break; | |
81 | ||
2d26d8e0 | 82 | explicit_bzero(p, n); |
e287086b LP |
83 | free(p); |
84 | m *= 2; | |
85 | } | |
86 | ||
87 | l = strv_parse_nulstr(p, n); | |
88 | if (!l) | |
89 | return -ENOMEM; | |
90 | ||
2d26d8e0 | 91 | explicit_bzero(p, n); |
1602b008 | 92 | |
e287086b LP |
93 | *ret = l; |
94 | return 0; | |
95 | } | |
96 | ||
97 | static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **passwords) { | |
ab84f5b9 | 98 | _cleanup_strv_free_erase_ char **l = NULL; |
e287086b LP |
99 | _cleanup_free_ char *p = NULL; |
100 | key_serial_t serial; | |
101 | size_t n; | |
102 | int r; | |
103 | ||
104 | assert(keyname); | |
105 | assert(passwords); | |
106 | ||
107 | if (!(flags & ASK_PASSWORD_PUSH_CACHE)) | |
108 | return 0; | |
109 | ||
110 | r = lookup_key(keyname, &serial); | |
111 | if (r >= 0) { | |
112 | r = retrieve_key(serial, &l); | |
113 | if (r < 0) | |
114 | return r; | |
115 | } else if (r != -ENOKEY) | |
116 | return r; | |
117 | ||
118 | r = strv_extend_strv(&l, passwords, true); | |
119 | if (r <= 0) | |
120 | return r; | |
121 | ||
122 | r = strv_make_nulstr(l, &p, &n); | |
123 | if (r < 0) | |
124 | return r; | |
125 | ||
b60df13b | 126 | serial = add_key("user", keyname, p, n, KEY_SPEC_USER_KEYRING); |
2d26d8e0 | 127 | explicit_bzero(p, n); |
e287086b LP |
128 | if (serial == -1) |
129 | return -errno; | |
130 | ||
131 | if (keyctl(KEYCTL_SET_TIMEOUT, | |
132 | (unsigned long) serial, | |
133 | (unsigned long) DIV_ROUND_UP(KEYRING_TIMEOUT_USEC, USEC_PER_SEC), 0, 0) < 0) | |
134 | log_debug_errno(errno, "Failed to adjust timeout: %m"); | |
135 | ||
136 | log_debug("Added key to keyring as %" PRIi32 ".", serial); | |
137 | ||
138 | return 1; | |
139 | } | |
140 | ||
141 | static int add_to_keyring_and_log(const char *keyname, AskPasswordFlags flags, char **passwords) { | |
142 | int r; | |
143 | ||
144 | assert(keyname); | |
145 | assert(passwords); | |
146 | ||
147 | r = add_to_keyring(keyname, flags, passwords); | |
148 | if (r < 0) | |
149 | return log_debug_errno(r, "Failed to add password to keyring: %m"); | |
150 | ||
151 | return 0; | |
152 | } | |
153 | ||
154 | int ask_password_keyring(const char *keyname, AskPasswordFlags flags, char ***ret) { | |
155 | ||
156 | key_serial_t serial; | |
157 | int r; | |
158 | ||
159 | assert(keyname); | |
160 | assert(ret); | |
161 | ||
162 | if (!(flags & ASK_PASSWORD_ACCEPT_CACHED)) | |
163 | return -EUNATCH; | |
164 | ||
165 | r = lookup_key(keyname, &serial); | |
166 | if (r == -ENOSYS) /* when retrieving the distinction doesn't matter */ | |
167 | return -ENOKEY; | |
168 | if (r < 0) | |
169 | return r; | |
170 | ||
171 | return retrieve_key(serial, ret); | |
172 | } | |
173 | ||
58fc840b LP |
174 | static void backspace_chars(int ttyfd, size_t p) { |
175 | ||
176 | if (ttyfd < 0) | |
177 | return; | |
178 | ||
179 | while (p > 0) { | |
180 | p--; | |
181 | ||
182 | loop_write(ttyfd, "\b \b", 3, false); | |
183 | } | |
184 | } | |
185 | ||
fd6ac62c LP |
186 | static void backspace_string(int ttyfd, const char *str) { |
187 | size_t m; | |
188 | ||
189 | assert(str); | |
190 | ||
191 | if (ttyfd < 0) | |
192 | return; | |
193 | ||
f95dbcc2 | 194 | /* Backspaces through enough characters to entirely undo printing of the specified string. */ |
fd6ac62c LP |
195 | |
196 | m = utf8_n_codepoints(str); | |
197 | if (m == (size_t) -1) | |
198 | m = strlen(str); /* Not a valid UTF-8 string? If so, let's backspace the number of bytes output. Most | |
199 | * likely this happened because we are not in an UTF-8 locale, and in that case that | |
200 | * is the correct thing to do. And even if it's not, terminals tend to stop | |
201 | * backspacing at the leftmost column, hence backspacing too much should be mostly | |
202 | * OK. */ | |
203 | ||
204 | backspace_chars(ttyfd, m); | |
205 | } | |
206 | ||
7f4e0805 | 207 | int ask_password_tty( |
daa55720 | 208 | int ttyfd, |
7f4e0805 | 209 | const char *message, |
e287086b | 210 | const char *keyname, |
7f4e0805 | 211 | usec_t until, |
e287086b | 212 | AskPasswordFlags flags, |
7f4e0805 | 213 | const char *flag_file, |
e287086b | 214 | char **ret) { |
7f4e0805 | 215 | |
088dcd8e LP |
216 | enum { |
217 | POLL_TTY, | |
218 | POLL_INOTIFY, | |
219 | _POLL_MAX, | |
220 | }; | |
221 | ||
c2b32159 | 222 | bool reset_tty = false, dirty = false, use_color = false; |
daa55720 | 223 | _cleanup_close_ int cttyfd = -1, notify = -1; |
7f4e0805 | 224 | struct termios old_termios, new_termios; |
f3149d57 | 225 | char passphrase[LINE_MAX + 1] = {}, *x; |
088dcd8e | 226 | struct pollfd pollfd[_POLL_MAX]; |
f3149d57 | 227 | size_t p = 0, codepoint = 0; |
088dcd8e | 228 | int r; |
7f4e0805 | 229 | |
e287086b LP |
230 | assert(ret); |
231 | ||
232 | if (flags & ASK_PASSWORD_NO_TTY) | |
233 | return -EUNATCH; | |
234 | ||
235 | if (!message) | |
236 | message = "Password:"; | |
7f4e0805 LP |
237 | |
238 | if (flag_file) { | |
981e4cd3 | 239 | notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK); |
a497a296 LP |
240 | if (notify < 0) |
241 | return -errno; | |
242 | ||
243 | if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) | |
244 | return -errno; | |
7f4e0805 LP |
245 | } |
246 | ||
daa55720 LP |
247 | /* If the caller didn't specify a TTY, then use the controlling tty, if we can. */ |
248 | if (ttyfd < 0) | |
249 | ttyfd = cttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC); | |
7f4e0805 | 250 | |
daa55720 | 251 | if (ttyfd >= 0) { |
a497a296 LP |
252 | if (tcgetattr(ttyfd, &old_termios) < 0) |
253 | return -errno; | |
7f4e0805 | 254 | |
c2b32159 LP |
255 | if (flags & ASK_PASSWORD_CONSOLE_COLOR) |
256 | use_color = dev_console_colors_enabled(); | |
257 | else | |
258 | use_color = colors_enabled(); | |
259 | ||
260 | if (use_color) | |
ac7a9674 LP |
261 | (void) loop_write(ttyfd, ANSI_HIGHLIGHT, STRLEN(ANSI_HIGHLIGHT), false); |
262 | ||
263 | (void) loop_write(ttyfd, message, strlen(message), false); | |
264 | (void) loop_write(ttyfd, " ", 1, false); | |
265 | ||
c2b32159 | 266 | if (use_color) |
ac7a9674 | 267 | (void) loop_write(ttyfd, ANSI_NORMAL, STRLEN(ANSI_NORMAL), false); |
7f4e0805 LP |
268 | |
269 | new_termios = old_termios; | |
270 | new_termios.c_lflag &= ~(ICANON|ECHO); | |
271 | new_termios.c_cc[VMIN] = 1; | |
272 | new_termios.c_cc[VTIME] = 0; | |
273 | ||
274 | if (tcsetattr(ttyfd, TCSADRAIN, &new_termios) < 0) { | |
275 | r = -errno; | |
276 | goto finish; | |
277 | } | |
278 | ||
279 | reset_tty = true; | |
280 | } | |
281 | ||
70dee475 LP |
282 | pollfd[POLL_TTY] = (struct pollfd) { |
283 | .fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO, | |
284 | .events = POLLIN, | |
285 | }; | |
286 | pollfd[POLL_INOTIFY] = (struct pollfd) { | |
287 | .fd = notify, | |
288 | .events = POLLIN, | |
289 | }; | |
7f4e0805 LP |
290 | |
291 | for (;;) { | |
7f4e0805 LP |
292 | int sleep_for = -1, k; |
293 | ssize_t n; | |
fd6ac62c | 294 | char c; |
7f4e0805 LP |
295 | |
296 | if (until > 0) { | |
297 | usec_t y; | |
298 | ||
299 | y = now(CLOCK_MONOTONIC); | |
300 | ||
301 | if (y > until) { | |
7ded2e28 | 302 | r = -ETIME; |
7f4e0805 LP |
303 | goto finish; |
304 | } | |
305 | ||
c9eb4a00 | 306 | sleep_for = (int) DIV_ROUND_UP(until - y, USEC_PER_MSEC); |
7f4e0805 LP |
307 | } |
308 | ||
309 | if (flag_file) | |
310 | if (access(flag_file, F_OK) < 0) { | |
311 | r = -errno; | |
312 | goto finish; | |
313 | } | |
314 | ||
e287086b | 315 | k = poll(pollfd, notify >= 0 ? 2 : 1, sleep_for); |
981e4cd3 | 316 | if (k < 0) { |
7f4e0805 LP |
317 | if (errno == EINTR) |
318 | continue; | |
319 | ||
320 | r = -errno; | |
321 | goto finish; | |
322 | } else if (k == 0) { | |
ccc80078 | 323 | r = -ETIME; |
7f4e0805 LP |
324 | goto finish; |
325 | } | |
326 | ||
e287086b | 327 | if (notify >= 0 && pollfd[POLL_INOTIFY].revents != 0) |
665dfe93 | 328 | (void) flush_fd(notify); |
7f4e0805 LP |
329 | |
330 | if (pollfd[POLL_TTY].revents == 0) | |
331 | continue; | |
332 | ||
981e4cd3 LP |
333 | n = read(ttyfd >= 0 ? ttyfd : STDIN_FILENO, &c, 1); |
334 | if (n < 0) { | |
3742095b | 335 | if (IN_SET(errno, EINTR, EAGAIN)) |
7f4e0805 LP |
336 | continue; |
337 | ||
338 | r = -errno; | |
339 | goto finish; | |
340 | ||
fd6ac62c | 341 | } |
7f4e0805 | 342 | |
fd6ac62c LP |
343 | /* We treat EOF, newline and NUL byte all as valid end markers */ |
344 | if (n == 0 || c == '\n' || c == 0) | |
7f4e0805 | 345 | break; |
fd6ac62c LP |
346 | |
347 | if (c == 21) { /* C-u */ | |
7f4e0805 | 348 | |
e287086b | 349 | if (!(flags & ASK_PASSWORD_SILENT)) |
fd6ac62c LP |
350 | backspace_string(ttyfd, passphrase); |
351 | ||
352 | explicit_bzero(passphrase, sizeof(passphrase)); | |
353 | p = codepoint = 0; | |
7f4e0805 | 354 | |
4c701096 | 355 | } else if (IN_SET(c, '\b', 127)) { |
58fc840b LP |
356 | |
357 | if (p > 0) { | |
fd6ac62c | 358 | size_t q; |
58fc840b | 359 | |
e287086b | 360 | if (!(flags & ASK_PASSWORD_SILENT)) |
58fc840b LP |
361 | backspace_chars(ttyfd, 1); |
362 | ||
fd6ac62c LP |
363 | /* Remove a full UTF-8 codepoint from the end. For that, figure out where the last one |
364 | * begins */ | |
365 | q = 0; | |
366 | for (;;) { | |
367 | size_t z; | |
368 | ||
369 | z = utf8_encoded_valid_unichar(passphrase + q); | |
370 | if (z == 0) { | |
371 | q = (size_t) -1; /* Invalid UTF8! */ | |
372 | break; | |
373 | } | |
374 | ||
375 | if (q + z >= p) /* This one brings us over the edge */ | |
376 | break; | |
377 | ||
378 | q += z; | |
379 | } | |
380 | ||
381 | p = codepoint = q == (size_t) -1 ? p - 1 : q; | |
382 | explicit_bzero(passphrase + p, sizeof(passphrase) - p); | |
383 | ||
e287086b | 384 | } else if (!dirty && !(flags & ASK_PASSWORD_SILENT)) { |
441dfe09 | 385 | |
e287086b | 386 | flags |= ASK_PASSWORD_SILENT; |
441dfe09 | 387 | |
fd6ac62c LP |
388 | /* There are two ways to enter silent mode. Either by pressing backspace as first key |
389 | * (and only as first key), or ... */ | |
390 | ||
441dfe09 | 391 | if (ttyfd >= 0) |
ac7a9674 | 392 | (void) loop_write(ttyfd, "(no echo) ", 10, false); |
441dfe09 | 393 | |
58fc840b | 394 | } else if (ttyfd >= 0) |
ac7a9674 | 395 | (void) loop_write(ttyfd, "\a", 1, false); |
7f4e0805 | 396 | |
e287086b | 397 | } else if (c == '\t' && !(flags & ASK_PASSWORD_SILENT)) { |
58fc840b | 398 | |
fd6ac62c | 399 | backspace_string(ttyfd, passphrase); |
e287086b | 400 | flags |= ASK_PASSWORD_SILENT; |
58fc840b | 401 | |
441dfe09 LP |
402 | /* ... or by pressing TAB at any time. */ |
403 | ||
58fc840b | 404 | if (ttyfd >= 0) |
ac7a9674 | 405 | (void) loop_write(ttyfd, "(no echo) ", 10, false); |
036eeac5 | 406 | |
fd6ac62c LP |
407 | } else if (p >= sizeof(passphrase)-1) { |
408 | ||
409 | /* Reached the size limit */ | |
410 | if (ttyfd >= 0) | |
411 | (void) loop_write(ttyfd, "\a", 1, false); | |
412 | ||
413 | } else { | |
7f4e0805 LP |
414 | passphrase[p++] = c; |
415 | ||
f3149d57 | 416 | if (!(flags & ASK_PASSWORD_SILENT) && ttyfd >= 0) { |
fd6ac62c | 417 | /* Check if we got a complete UTF-8 character now. If so, let's output one '*'. */ |
f3149d57 ZJS |
418 | n = utf8_encoded_valid_unichar(passphrase + codepoint); |
419 | if (n >= 0) { | |
420 | codepoint = p; | |
ac7a9674 | 421 | (void) loop_write(ttyfd, (flags & ASK_PASSWORD_ECHO) ? &c : "*", 1, false); |
f3149d57 ZJS |
422 | } |
423 | } | |
441dfe09 LP |
424 | |
425 | dirty = true; | |
7f4e0805 | 426 | } |
1602b008 | 427 | |
fd6ac62c | 428 | /* Let's forget this char, just to not keep needlessly copies of key material around */ |
1602b008 | 429 | c = 'x'; |
7f4e0805 LP |
430 | } |
431 | ||
981e4cd3 | 432 | x = strndup(passphrase, p); |
fd6ac62c | 433 | explicit_bzero(passphrase, sizeof(passphrase)); |
981e4cd3 | 434 | if (!x) { |
7f4e0805 LP |
435 | r = -ENOMEM; |
436 | goto finish; | |
437 | } | |
438 | ||
e287086b LP |
439 | if (keyname) |
440 | (void) add_to_keyring_and_log(keyname, flags, STRV_MAKE(x)); | |
441 | ||
442 | *ret = x; | |
7f4e0805 LP |
443 | r = 0; |
444 | ||
445 | finish: | |
981e4cd3 | 446 | if (ttyfd >= 0 && reset_tty) { |
ac7a9674 LP |
447 | (void) loop_write(ttyfd, "\n", 1, false); |
448 | (void) tcsetattr(ttyfd, TCSADRAIN, &old_termios); | |
7f4e0805 LP |
449 | } |
450 | ||
451 | return r; | |
452 | } | |
453 | ||
15a3e96f LP |
454 | static int create_socket(char **ret) { |
455 | _cleanup_free_ char *path = NULL; | |
456 | union sockaddr_union sa = {}; | |
00843602 | 457 | _cleanup_close_ int fd = -1; |
15a3e96f | 458 | int salen; |
7f4e0805 | 459 | |
15a3e96f | 460 | assert(ret); |
7f4e0805 | 461 | |
e62d8c39 | 462 | fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
4a62c710 | 463 | if (fd < 0) |
00843602 | 464 | return -errno; |
7f4e0805 | 465 | |
15a3e96f LP |
466 | if (asprintf(&path, "/run/systemd/ask-password/sck.%" PRIx64, random_u64()) < 0) |
467 | return -ENOMEM; | |
468 | ||
469 | salen = sockaddr_un_set_path(&sa.un, path); | |
470 | if (salen < 0) | |
471 | return salen; | |
7f4e0805 | 472 | |
5c0d398d | 473 | RUN_WITH_UMASK(0177) { |
15a3e96f | 474 | if (bind(fd, &sa.sa, salen) < 0) |
00843602 | 475 | return -errno; |
5c0d398d | 476 | } |
1d6702e8 | 477 | |
6d5e65f6 | 478 | if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &const_int_one, sizeof(const_int_one)) < 0) |
00843602 | 479 | return -errno; |
7f4e0805 | 480 | |
15a3e96f LP |
481 | *ret = TAKE_PTR(path); |
482 | return TAKE_FD(fd); | |
7f4e0805 LP |
483 | } |
484 | ||
7f4e0805 LP |
485 | int ask_password_agent( |
486 | const char *message, | |
487 | const char *icon, | |
9fa1de96 | 488 | const char *id, |
e287086b | 489 | const char *keyname, |
7f4e0805 | 490 | usec_t until, |
e287086b LP |
491 | AskPasswordFlags flags, |
492 | char ***ret) { | |
7f4e0805 LP |
493 | |
494 | enum { | |
495 | FD_SOCKET, | |
496 | FD_SIGNAL, | |
497 | _FD_MAX | |
498 | }; | |
499 | ||
e287086b | 500 | _cleanup_close_ int socket_fd = -1, signal_fd = -1, fd = -1; |
2b583ce6 | 501 | char temp[] = "/run/systemd/ask-password/tmp.XXXXXX"; |
7f4e0805 | 502 | char final[sizeof(temp)] = ""; |
981e4cd3 | 503 | _cleanup_free_ char *socket_name = NULL; |
e287086b LP |
504 | _cleanup_strv_free_ char **l = NULL; |
505 | _cleanup_fclose_ FILE *f = NULL; | |
7f4e0805 | 506 | struct pollfd pollfd[_FD_MAX]; |
e287086b | 507 | sigset_t mask, oldmask; |
981e4cd3 | 508 | int r; |
7f4e0805 | 509 | |
e287086b | 510 | assert(ret); |
21bc923a | 511 | |
e287086b LP |
512 | if (flags & ASK_PASSWORD_NO_AGENT) |
513 | return -EUNATCH; | |
00843602 | 514 | |
72c0a2c2 LP |
515 | assert_se(sigemptyset(&mask) >= 0); |
516 | assert_se(sigset_add_many(&mask, SIGINT, SIGTERM, -1) >= 0); | |
517 | assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) >= 0); | |
f9b72cd8 | 518 | |
00843602 | 519 | (void) mkdir_p_label("/run/systemd/ask-password", 0755); |
7f4e0805 | 520 | |
646853bd | 521 | fd = mkostemp_safe(temp); |
1d6702e8 | 522 | if (fd < 0) { |
709f6e46 | 523 | r = fd; |
7f4e0805 LP |
524 | goto finish; |
525 | } | |
526 | ||
00843602 | 527 | (void) fchmod(fd, 0644); |
7f4e0805 | 528 | |
981e4cd3 LP |
529 | f = fdopen(fd, "w"); |
530 | if (!f) { | |
00843602 | 531 | r = -errno; |
7f4e0805 LP |
532 | goto finish; |
533 | } | |
534 | ||
535 | fd = -1; | |
536 | ||
981e4cd3 LP |
537 | signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); |
538 | if (signal_fd < 0) { | |
00843602 | 539 | r = -errno; |
7f4e0805 LP |
540 | goto finish; |
541 | } | |
542 | ||
981e4cd3 LP |
543 | socket_fd = create_socket(&socket_name); |
544 | if (socket_fd < 0) { | |
7f4e0805 LP |
545 | r = socket_fd; |
546 | goto finish; | |
547 | } | |
548 | ||
549 | fprintf(f, | |
550 | "[Ask]\n" | |
de0671ee | 551 | "PID="PID_FMT"\n" |
7f4e0805 | 552 | "Socket=%s\n" |
21bc923a | 553 | "AcceptCached=%i\n" |
64845bdc | 554 | "Echo=%i\n" |
de0671ee | 555 | "NotAfter="USEC_FMT"\n", |
df0ff127 | 556 | getpid_cached(), |
7f4e0805 | 557 | socket_name, |
e287086b LP |
558 | (flags & ASK_PASSWORD_ACCEPT_CACHED) ? 1 : 0, |
559 | (flags & ASK_PASSWORD_ECHO) ? 1 : 0, | |
de0671ee | 560 | until); |
7f4e0805 LP |
561 | |
562 | if (message) | |
563 | fprintf(f, "Message=%s\n", message); | |
564 | ||
565 | if (icon) | |
566 | fprintf(f, "Icon=%s\n", icon); | |
567 | ||
9fa1de96 DH |
568 | if (id) |
569 | fprintf(f, "Id=%s\n", id); | |
570 | ||
dacd6cee | 571 | r = fflush_and_check(f); |
00843602 | 572 | if (r < 0) |
7f4e0805 | 573 | goto finish; |
7f4e0805 LP |
574 | |
575 | memcpy(final, temp, sizeof(temp)); | |
576 | ||
577 | final[sizeof(final)-11] = 'a'; | |
578 | final[sizeof(final)-10] = 's'; | |
579 | final[sizeof(final)-9] = 'k'; | |
580 | ||
581 | if (rename(temp, final) < 0) { | |
00843602 | 582 | r = -errno; |
7f4e0805 LP |
583 | goto finish; |
584 | } | |
585 | ||
586 | zero(pollfd); | |
587 | pollfd[FD_SOCKET].fd = socket_fd; | |
588 | pollfd[FD_SOCKET].events = POLLIN; | |
589 | pollfd[FD_SIGNAL].fd = signal_fd; | |
590 | pollfd[FD_SIGNAL].events = POLLIN; | |
591 | ||
592 | for (;;) { | |
593 | char passphrase[LINE_MAX+1]; | |
594 | struct msghdr msghdr; | |
595 | struct iovec iovec; | |
596 | struct ucred *ucred; | |
597 | union { | |
598 | struct cmsghdr cmsghdr; | |
599 | uint8_t buf[CMSG_SPACE(sizeof(struct ucred))]; | |
600 | } control; | |
601 | ssize_t n; | |
602 | int k; | |
603 | usec_t t; | |
604 | ||
605 | t = now(CLOCK_MONOTONIC); | |
606 | ||
7dcda352 | 607 | if (until > 0 && until <= t) { |
7f4e0805 LP |
608 | r = -ETIME; |
609 | goto finish; | |
610 | } | |
611 | ||
981e4cd3 LP |
612 | k = poll(pollfd, _FD_MAX, until > 0 ? (int) ((until-t)/USEC_PER_MSEC) : -1); |
613 | if (k < 0) { | |
7f4e0805 LP |
614 | if (errno == EINTR) |
615 | continue; | |
616 | ||
00843602 | 617 | r = -errno; |
7f4e0805 LP |
618 | goto finish; |
619 | } | |
620 | ||
621 | if (k <= 0) { | |
7f4e0805 LP |
622 | r = -ETIME; |
623 | goto finish; | |
624 | } | |
625 | ||
21bc923a LP |
626 | if (pollfd[FD_SIGNAL].revents & POLLIN) { |
627 | r = -EINTR; | |
628 | goto finish; | |
629 | } | |
7f4e0805 LP |
630 | |
631 | if (pollfd[FD_SOCKET].revents != POLLIN) { | |
7f4e0805 LP |
632 | r = -EIO; |
633 | goto finish; | |
634 | } | |
635 | ||
636 | zero(iovec); | |
637 | iovec.iov_base = passphrase; | |
21bc923a | 638 | iovec.iov_len = sizeof(passphrase); |
7f4e0805 LP |
639 | |
640 | zero(control); | |
641 | zero(msghdr); | |
642 | msghdr.msg_iov = &iovec; | |
643 | msghdr.msg_iovlen = 1; | |
644 | msghdr.msg_control = &control; | |
645 | msghdr.msg_controllen = sizeof(control); | |
646 | ||
981e4cd3 LP |
647 | n = recvmsg(socket_fd, &msghdr, 0); |
648 | if (n < 0) { | |
3742095b | 649 | if (IN_SET(errno, EAGAIN, EINTR)) |
7f4e0805 LP |
650 | continue; |
651 | ||
00843602 | 652 | r = -errno; |
7f4e0805 LP |
653 | goto finish; |
654 | } | |
655 | ||
1c8da044 LP |
656 | cmsg_close_all(&msghdr); |
657 | ||
7f4e0805 | 658 | if (n <= 0) { |
00843602 | 659 | log_debug("Message too short"); |
7f4e0805 LP |
660 | continue; |
661 | } | |
662 | ||
663 | if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) || | |
664 | control.cmsghdr.cmsg_level != SOL_SOCKET || | |
665 | control.cmsghdr.cmsg_type != SCM_CREDENTIALS || | |
666 | control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) { | |
00843602 | 667 | log_debug("Received message without credentials. Ignoring."); |
7f4e0805 LP |
668 | continue; |
669 | } | |
670 | ||
671 | ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr); | |
672 | if (ucred->uid != 0) { | |
00843602 | 673 | log_debug("Got request from unprivileged user. Ignoring."); |
7f4e0805 LP |
674 | continue; |
675 | } | |
676 | ||
677 | if (passphrase[0] == '+') { | |
e287086b | 678 | /* An empty message refers to the empty password */ |
8254a475 LP |
679 | if (n == 1) |
680 | l = strv_new("", NULL); | |
681 | else | |
682 | l = strv_parse_nulstr(passphrase+1, n-1); | |
2d26d8e0 | 683 | explicit_bzero(passphrase, n); |
8254a475 | 684 | if (!l) { |
7f4e0805 LP |
685 | r = -ENOMEM; |
686 | goto finish; | |
687 | } | |
688 | ||
58629001 | 689 | if (strv_isempty(l)) { |
e287086b | 690 | l = strv_free(l); |
00843602 | 691 | log_debug("Invalid packet"); |
21bc923a LP |
692 | continue; |
693 | } | |
694 | ||
e287086b LP |
695 | break; |
696 | } | |
21bc923a | 697 | |
e287086b | 698 | if (passphrase[0] == '-') { |
7f4e0805 LP |
699 | r = -ECANCELED; |
700 | goto finish; | |
7f4e0805 LP |
701 | } |
702 | ||
e287086b | 703 | log_debug("Invalid packet"); |
7f4e0805 LP |
704 | } |
705 | ||
e287086b LP |
706 | if (keyname) |
707 | (void) add_to_keyring_and_log(keyname, flags, l); | |
708 | ||
ae2a15bc | 709 | *ret = TAKE_PTR(l); |
7f4e0805 LP |
710 | r = 0; |
711 | ||
712 | finish: | |
981e4cd3 | 713 | if (socket_name) |
00843602 | 714 | (void) unlink(socket_name); |
7f4e0805 | 715 | |
00843602 | 716 | (void) unlink(temp); |
7f4e0805 LP |
717 | |
718 | if (final[0]) | |
00843602 | 719 | (void) unlink(final); |
7f4e0805 | 720 | |
f9b72cd8 | 721 | assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0); |
7f4e0805 LP |
722 | return r; |
723 | } | |
260ab287 | 724 | |
00843602 LP |
725 | int ask_password_auto( |
726 | const char *message, | |
727 | const char *icon, | |
728 | const char *id, | |
e287086b | 729 | const char *keyname, |
00843602 | 730 | usec_t until, |
e287086b LP |
731 | AskPasswordFlags flags, |
732 | char ***ret) { | |
00843602 LP |
733 | |
734 | int r; | |
735 | ||
e287086b | 736 | assert(ret); |
21bc923a | 737 | |
e287086b LP |
738 | if ((flags & ASK_PASSWORD_ACCEPT_CACHED) && keyname) { |
739 | r = ask_password_keyring(keyname, flags, ret); | |
740 | if (r != -ENOKEY) | |
741 | return r; | |
742 | } | |
743 | ||
744 | if (!(flags & ASK_PASSWORD_NO_TTY) && isatty(STDIN_FILENO)) { | |
21bc923a LP |
745 | char *s = NULL, **l = NULL; |
746 | ||
daa55720 | 747 | r = ask_password_tty(-1, message, keyname, until, flags, NULL, &s); |
981e4cd3 | 748 | if (r < 0) |
21bc923a LP |
749 | return r; |
750 | ||
1602b008 LP |
751 | r = strv_push(&l, s); |
752 | if (r < 0) { | |
753 | string_erase(s); | |
754 | free(s); | |
00843602 | 755 | return -ENOMEM; |
1602b008 | 756 | } |
21bc923a | 757 | |
e287086b | 758 | *ret = l; |
00843602 LP |
759 | return 0; |
760 | } | |
761 | ||
e287086b LP |
762 | if (!(flags & ASK_PASSWORD_NO_AGENT)) |
763 | return ask_password_agent(message, icon, id, keyname, until, flags, ret); | |
764 | ||
765 | return -EUNATCH; | |
260ab287 | 766 | } |