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