]>
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; |
2ff48e98 | 458 | int salen, r; |
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 | |
2ff48e98 LP |
478 | r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true); |
479 | if (r < 0) | |
480 | return r; | |
7f4e0805 | 481 | |
15a3e96f LP |
482 | *ret = TAKE_PTR(path); |
483 | return TAKE_FD(fd); | |
7f4e0805 LP |
484 | } |
485 | ||
7f4e0805 LP |
486 | int ask_password_agent( |
487 | const char *message, | |
488 | const char *icon, | |
9fa1de96 | 489 | const char *id, |
e287086b | 490 | const char *keyname, |
7f4e0805 | 491 | usec_t until, |
e287086b LP |
492 | AskPasswordFlags flags, |
493 | char ***ret) { | |
7f4e0805 LP |
494 | |
495 | enum { | |
496 | FD_SOCKET, | |
497 | FD_SIGNAL, | |
498 | _FD_MAX | |
499 | }; | |
500 | ||
e287086b | 501 | _cleanup_close_ int socket_fd = -1, signal_fd = -1, fd = -1; |
2b583ce6 | 502 | char temp[] = "/run/systemd/ask-password/tmp.XXXXXX"; |
7f4e0805 | 503 | char final[sizeof(temp)] = ""; |
981e4cd3 | 504 | _cleanup_free_ char *socket_name = NULL; |
e287086b LP |
505 | _cleanup_strv_free_ char **l = NULL; |
506 | _cleanup_fclose_ FILE *f = NULL; | |
7f4e0805 | 507 | struct pollfd pollfd[_FD_MAX]; |
e287086b | 508 | sigset_t mask, oldmask; |
981e4cd3 | 509 | int r; |
7f4e0805 | 510 | |
e287086b | 511 | assert(ret); |
21bc923a | 512 | |
e287086b LP |
513 | if (flags & ASK_PASSWORD_NO_AGENT) |
514 | return -EUNATCH; | |
00843602 | 515 | |
72c0a2c2 LP |
516 | assert_se(sigemptyset(&mask) >= 0); |
517 | assert_se(sigset_add_many(&mask, SIGINT, SIGTERM, -1) >= 0); | |
518 | assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) >= 0); | |
f9b72cd8 | 519 | |
00843602 | 520 | (void) mkdir_p_label("/run/systemd/ask-password", 0755); |
7f4e0805 | 521 | |
646853bd | 522 | fd = mkostemp_safe(temp); |
1d6702e8 | 523 | if (fd < 0) { |
709f6e46 | 524 | r = fd; |
7f4e0805 LP |
525 | goto finish; |
526 | } | |
527 | ||
00843602 | 528 | (void) fchmod(fd, 0644); |
7f4e0805 | 529 | |
981e4cd3 LP |
530 | f = fdopen(fd, "w"); |
531 | if (!f) { | |
00843602 | 532 | r = -errno; |
7f4e0805 LP |
533 | goto finish; |
534 | } | |
535 | ||
536 | fd = -1; | |
537 | ||
981e4cd3 LP |
538 | signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); |
539 | if (signal_fd < 0) { | |
00843602 | 540 | r = -errno; |
7f4e0805 LP |
541 | goto finish; |
542 | } | |
543 | ||
981e4cd3 LP |
544 | socket_fd = create_socket(&socket_name); |
545 | if (socket_fd < 0) { | |
7f4e0805 LP |
546 | r = socket_fd; |
547 | goto finish; | |
548 | } | |
549 | ||
550 | fprintf(f, | |
551 | "[Ask]\n" | |
de0671ee | 552 | "PID="PID_FMT"\n" |
7f4e0805 | 553 | "Socket=%s\n" |
21bc923a | 554 | "AcceptCached=%i\n" |
64845bdc | 555 | "Echo=%i\n" |
de0671ee | 556 | "NotAfter="USEC_FMT"\n", |
df0ff127 | 557 | getpid_cached(), |
7f4e0805 | 558 | socket_name, |
e287086b LP |
559 | (flags & ASK_PASSWORD_ACCEPT_CACHED) ? 1 : 0, |
560 | (flags & ASK_PASSWORD_ECHO) ? 1 : 0, | |
de0671ee | 561 | until); |
7f4e0805 LP |
562 | |
563 | if (message) | |
564 | fprintf(f, "Message=%s\n", message); | |
565 | ||
566 | if (icon) | |
567 | fprintf(f, "Icon=%s\n", icon); | |
568 | ||
9fa1de96 DH |
569 | if (id) |
570 | fprintf(f, "Id=%s\n", id); | |
571 | ||
dacd6cee | 572 | r = fflush_and_check(f); |
00843602 | 573 | if (r < 0) |
7f4e0805 | 574 | goto finish; |
7f4e0805 LP |
575 | |
576 | memcpy(final, temp, sizeof(temp)); | |
577 | ||
578 | final[sizeof(final)-11] = 'a'; | |
579 | final[sizeof(final)-10] = 's'; | |
580 | final[sizeof(final)-9] = 'k'; | |
581 | ||
582 | if (rename(temp, final) < 0) { | |
00843602 | 583 | r = -errno; |
7f4e0805 LP |
584 | goto finish; |
585 | } | |
586 | ||
587 | zero(pollfd); | |
588 | pollfd[FD_SOCKET].fd = socket_fd; | |
589 | pollfd[FD_SOCKET].events = POLLIN; | |
590 | pollfd[FD_SIGNAL].fd = signal_fd; | |
591 | pollfd[FD_SIGNAL].events = POLLIN; | |
592 | ||
593 | for (;;) { | |
594 | char passphrase[LINE_MAX+1]; | |
595 | struct msghdr msghdr; | |
596 | struct iovec iovec; | |
597 | struct ucred *ucred; | |
598 | union { | |
599 | struct cmsghdr cmsghdr; | |
600 | uint8_t buf[CMSG_SPACE(sizeof(struct ucred))]; | |
601 | } control; | |
602 | ssize_t n; | |
603 | int k; | |
604 | usec_t t; | |
605 | ||
606 | t = now(CLOCK_MONOTONIC); | |
607 | ||
7dcda352 | 608 | if (until > 0 && until <= t) { |
7f4e0805 LP |
609 | r = -ETIME; |
610 | goto finish; | |
611 | } | |
612 | ||
981e4cd3 LP |
613 | k = poll(pollfd, _FD_MAX, until > 0 ? (int) ((until-t)/USEC_PER_MSEC) : -1); |
614 | if (k < 0) { | |
7f4e0805 LP |
615 | if (errno == EINTR) |
616 | continue; | |
617 | ||
00843602 | 618 | r = -errno; |
7f4e0805 LP |
619 | goto finish; |
620 | } | |
621 | ||
622 | if (k <= 0) { | |
7f4e0805 LP |
623 | r = -ETIME; |
624 | goto finish; | |
625 | } | |
626 | ||
21bc923a LP |
627 | if (pollfd[FD_SIGNAL].revents & POLLIN) { |
628 | r = -EINTR; | |
629 | goto finish; | |
630 | } | |
7f4e0805 LP |
631 | |
632 | if (pollfd[FD_SOCKET].revents != POLLIN) { | |
7f4e0805 LP |
633 | r = -EIO; |
634 | goto finish; | |
635 | } | |
636 | ||
637 | zero(iovec); | |
638 | iovec.iov_base = passphrase; | |
21bc923a | 639 | iovec.iov_len = sizeof(passphrase); |
7f4e0805 LP |
640 | |
641 | zero(control); | |
642 | zero(msghdr); | |
643 | msghdr.msg_iov = &iovec; | |
644 | msghdr.msg_iovlen = 1; | |
645 | msghdr.msg_control = &control; | |
646 | msghdr.msg_controllen = sizeof(control); | |
647 | ||
981e4cd3 LP |
648 | n = recvmsg(socket_fd, &msghdr, 0); |
649 | if (n < 0) { | |
3742095b | 650 | if (IN_SET(errno, EAGAIN, EINTR)) |
7f4e0805 LP |
651 | continue; |
652 | ||
00843602 | 653 | r = -errno; |
7f4e0805 LP |
654 | goto finish; |
655 | } | |
656 | ||
1c8da044 LP |
657 | cmsg_close_all(&msghdr); |
658 | ||
7f4e0805 | 659 | if (n <= 0) { |
00843602 | 660 | log_debug("Message too short"); |
7f4e0805 LP |
661 | continue; |
662 | } | |
663 | ||
664 | if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) || | |
665 | control.cmsghdr.cmsg_level != SOL_SOCKET || | |
666 | control.cmsghdr.cmsg_type != SCM_CREDENTIALS || | |
667 | control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) { | |
00843602 | 668 | log_debug("Received message without credentials. Ignoring."); |
7f4e0805 LP |
669 | continue; |
670 | } | |
671 | ||
672 | ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr); | |
673 | if (ucred->uid != 0) { | |
00843602 | 674 | log_debug("Got request from unprivileged user. Ignoring."); |
7f4e0805 LP |
675 | continue; |
676 | } | |
677 | ||
678 | if (passphrase[0] == '+') { | |
e287086b | 679 | /* An empty message refers to the empty password */ |
8254a475 LP |
680 | if (n == 1) |
681 | l = strv_new("", NULL); | |
682 | else | |
683 | l = strv_parse_nulstr(passphrase+1, n-1); | |
2d26d8e0 | 684 | explicit_bzero(passphrase, n); |
8254a475 | 685 | if (!l) { |
7f4e0805 LP |
686 | r = -ENOMEM; |
687 | goto finish; | |
688 | } | |
689 | ||
58629001 | 690 | if (strv_isempty(l)) { |
e287086b | 691 | l = strv_free(l); |
00843602 | 692 | log_debug("Invalid packet"); |
21bc923a LP |
693 | continue; |
694 | } | |
695 | ||
e287086b LP |
696 | break; |
697 | } | |
21bc923a | 698 | |
e287086b | 699 | if (passphrase[0] == '-') { |
7f4e0805 LP |
700 | r = -ECANCELED; |
701 | goto finish; | |
7f4e0805 LP |
702 | } |
703 | ||
e287086b | 704 | log_debug("Invalid packet"); |
7f4e0805 LP |
705 | } |
706 | ||
e287086b LP |
707 | if (keyname) |
708 | (void) add_to_keyring_and_log(keyname, flags, l); | |
709 | ||
ae2a15bc | 710 | *ret = TAKE_PTR(l); |
7f4e0805 LP |
711 | r = 0; |
712 | ||
713 | finish: | |
981e4cd3 | 714 | if (socket_name) |
00843602 | 715 | (void) unlink(socket_name); |
7f4e0805 | 716 | |
00843602 | 717 | (void) unlink(temp); |
7f4e0805 LP |
718 | |
719 | if (final[0]) | |
00843602 | 720 | (void) unlink(final); |
7f4e0805 | 721 | |
f9b72cd8 | 722 | assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0); |
7f4e0805 LP |
723 | return r; |
724 | } | |
260ab287 | 725 | |
00843602 LP |
726 | int ask_password_auto( |
727 | const char *message, | |
728 | const char *icon, | |
729 | const char *id, | |
e287086b | 730 | const char *keyname, |
00843602 | 731 | usec_t until, |
e287086b LP |
732 | AskPasswordFlags flags, |
733 | char ***ret) { | |
00843602 LP |
734 | |
735 | int r; | |
736 | ||
e287086b | 737 | assert(ret); |
21bc923a | 738 | |
e287086b LP |
739 | if ((flags & ASK_PASSWORD_ACCEPT_CACHED) && keyname) { |
740 | r = ask_password_keyring(keyname, flags, ret); | |
741 | if (r != -ENOKEY) | |
742 | return r; | |
743 | } | |
744 | ||
745 | if (!(flags & ASK_PASSWORD_NO_TTY) && isatty(STDIN_FILENO)) { | |
21bc923a LP |
746 | char *s = NULL, **l = NULL; |
747 | ||
daa55720 | 748 | r = ask_password_tty(-1, message, keyname, until, flags, NULL, &s); |
981e4cd3 | 749 | if (r < 0) |
21bc923a LP |
750 | return r; |
751 | ||
1602b008 LP |
752 | r = strv_push(&l, s); |
753 | if (r < 0) { | |
754 | string_erase(s); | |
755 | free(s); | |
00843602 | 756 | return -ENOMEM; |
1602b008 | 757 | } |
21bc923a | 758 | |
e287086b | 759 | *ret = l; |
00843602 LP |
760 | return 0; |
761 | } | |
762 | ||
e287086b LP |
763 | if (!(flags & ASK_PASSWORD_NO_AGENT)) |
764 | return ask_password_agent(message, icon, id, keyname, until, flags, ret); | |
765 | ||
766 | return -EUNATCH; | |
260ab287 | 767 | } |