]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/ask-password-api.c
man: move non-target units together (#6934)
[thirdparty/systemd.git] / src / shared / ask-password-api.c
CommitLineData
7f4e0805
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
7f4e0805
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
7f4e0805 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
7f4e0805
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
00843602 19
7f4e0805
LP
20#include <errno.h>
21#include <fcntl.h>
a8fbdf54
TA
22#include <inttypes.h>
23#include <limits.h>
00843602 24#include <poll.h>
a8fbdf54 25#include <signal.h>
00843602 26#include <stdbool.h>
7f4e0805 27#include <stddef.h>
a8fbdf54
TA
28#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
00843602
LP
31#include <string.h>
32#include <sys/inotify.h>
7f4e0805 33#include <sys/signalfd.h>
00843602 34#include <sys/socket.h>
a8fbdf54
TA
35#include <sys/stat.h>
36#include <sys/time.h>
37#include <sys/uio.h>
00843602
LP
38#include <sys/un.h>
39#include <termios.h>
40#include <unistd.h>
7f4e0805 41
b5efdb8a 42#include "alloc-util.h"
3ffd4af2
LP
43#include "ask-password-api.h"
44#include "fd-util.h"
0d39fa9c 45#include "fileio.h"
f97b34a6 46#include "format-util.h"
c004493c 47#include "io-util.h"
a8fbdf54
TA
48#include "log.h"
49#include "macro.h"
e287086b 50#include "missing.h"
49e942b2 51#include "mkdir.h"
df0ff127 52#include "process-util.h"
3df3e884 53#include "random-util.h"
24882e06 54#include "signal-util.h"
00843602 55#include "socket-util.h"
07630cea 56#include "string-util.h"
00843602
LP
57#include "strv.h"
58#include "terminal-util.h"
a8fbdf54 59#include "time-util.h"
affb60b1 60#include "umask-util.h"
f3149d57 61#include "utf8.h"
00843602 62#include "util.h"
7f4e0805 63
e287086b
LP
64#define KEYRING_TIMEOUT_USEC ((5 * USEC_PER_MINUTE) / 2)
65
66static int lookup_key(const char *keyname, key_serial_t *ret) {
67 key_serial_t serial;
68
69 assert(keyname);
70 assert(ret);
71
72 serial = request_key("user", keyname, NULL, 0);
73 if (serial == -1)
9c4615fb 74 return negative_errno();
e287086b
LP
75
76 *ret = serial;
77 return 0;
78}
79
80static int retrieve_key(key_serial_t serial, char ***ret) {
81 _cleanup_free_ char *p = NULL;
82 long m = 100, n;
83 char **l;
84
85 assert(ret);
86
87 for (;;) {
88 p = new(char, m);
89 if (!p)
90 return -ENOMEM;
91
92 n = keyctl(KEYCTL_READ, (unsigned long) serial, (unsigned long) p, (unsigned long) m, 0);
93 if (n < 0)
94 return -errno;
95
96 if (n < m)
97 break;
98
2d26d8e0 99 explicit_bzero(p, n);
e287086b
LP
100 free(p);
101 m *= 2;
102 }
103
104 l = strv_parse_nulstr(p, n);
105 if (!l)
106 return -ENOMEM;
107
2d26d8e0 108 explicit_bzero(p, n);
1602b008 109
e287086b
LP
110 *ret = l;
111 return 0;
112}
113
114static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **passwords) {
ab84f5b9 115 _cleanup_strv_free_erase_ char **l = NULL;
e287086b
LP
116 _cleanup_free_ char *p = NULL;
117 key_serial_t serial;
118 size_t n;
119 int r;
120
121 assert(keyname);
122 assert(passwords);
123
124 if (!(flags & ASK_PASSWORD_PUSH_CACHE))
125 return 0;
126
127 r = lookup_key(keyname, &serial);
128 if (r >= 0) {
129 r = retrieve_key(serial, &l);
130 if (r < 0)
131 return r;
132 } else if (r != -ENOKEY)
133 return r;
134
135 r = strv_extend_strv(&l, passwords, true);
136 if (r <= 0)
137 return r;
138
139 r = strv_make_nulstr(l, &p, &n);
140 if (r < 0)
141 return r;
142
b60df13b 143 serial = add_key("user", keyname, p, n, KEY_SPEC_USER_KEYRING);
2d26d8e0 144 explicit_bzero(p, n);
e287086b
LP
145 if (serial == -1)
146 return -errno;
147
148 if (keyctl(KEYCTL_SET_TIMEOUT,
149 (unsigned long) serial,
150 (unsigned long) DIV_ROUND_UP(KEYRING_TIMEOUT_USEC, USEC_PER_SEC), 0, 0) < 0)
151 log_debug_errno(errno, "Failed to adjust timeout: %m");
152
153 log_debug("Added key to keyring as %" PRIi32 ".", serial);
154
155 return 1;
156}
157
158static int add_to_keyring_and_log(const char *keyname, AskPasswordFlags flags, char **passwords) {
159 int r;
160
161 assert(keyname);
162 assert(passwords);
163
164 r = add_to_keyring(keyname, flags, passwords);
165 if (r < 0)
166 return log_debug_errno(r, "Failed to add password to keyring: %m");
167
168 return 0;
169}
170
171int ask_password_keyring(const char *keyname, AskPasswordFlags flags, char ***ret) {
172
173 key_serial_t serial;
174 int r;
175
176 assert(keyname);
177 assert(ret);
178
179 if (!(flags & ASK_PASSWORD_ACCEPT_CACHED))
180 return -EUNATCH;
181
182 r = lookup_key(keyname, &serial);
183 if (r == -ENOSYS) /* when retrieving the distinction doesn't matter */
184 return -ENOKEY;
185 if (r < 0)
186 return r;
187
188 return retrieve_key(serial, ret);
189}
190
58fc840b
LP
191static void backspace_chars(int ttyfd, size_t p) {
192
193 if (ttyfd < 0)
194 return;
195
196 while (p > 0) {
197 p--;
198
199 loop_write(ttyfd, "\b \b", 3, false);
200 }
201}
202
7f4e0805
LP
203int ask_password_tty(
204 const char *message,
e287086b 205 const char *keyname,
7f4e0805 206 usec_t until,
e287086b 207 AskPasswordFlags flags,
7f4e0805 208 const char *flag_file,
e287086b 209 char **ret) {
7f4e0805
LP
210
211 struct termios old_termios, new_termios;
f3149d57
ZJS
212 char passphrase[LINE_MAX + 1] = {}, *x;
213 size_t p = 0, codepoint = 0;
981e4cd3
LP
214 int r;
215 _cleanup_close_ int ttyfd = -1, notify = -1;
7f4e0805
LP
216 struct pollfd pollfd[2];
217 bool reset_tty = false;
441dfe09 218 bool dirty = false;
7f4e0805
LP
219 enum {
220 POLL_TTY,
221 POLL_INOTIFY
222 };
223
e287086b
LP
224 assert(ret);
225
226 if (flags & ASK_PASSWORD_NO_TTY)
227 return -EUNATCH;
228
229 if (!message)
230 message = "Password:";
7f4e0805
LP
231
232 if (flag_file) {
981e4cd3
LP
233 notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
234 if (notify < 0) {
7f4e0805
LP
235 r = -errno;
236 goto finish;
237 }
238
239 if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
240 r = -errno;
241 goto finish;
242 }
243 }
244
981e4cd3
LP
245 ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC);
246 if (ttyfd >= 0) {
7f4e0805
LP
247
248 if (tcgetattr(ttyfd, &old_termios) < 0) {
249 r = -errno;
250 goto finish;
251 }
252
7565bb98
LP
253 if (colors_enabled())
254 loop_write(ttyfd, ANSI_HIGHLIGHT, strlen(ANSI_HIGHLIGHT), false);
7f4e0805 255 loop_write(ttyfd, message, strlen(message), false);
bd1db33f 256 loop_write(ttyfd, " ", 1, false);
7565bb98
LP
257 if (colors_enabled())
258 loop_write(ttyfd, ANSI_NORMAL, strlen(ANSI_NORMAL), false);
7f4e0805
LP
259
260 new_termios = old_termios;
261 new_termios.c_lflag &= ~(ICANON|ECHO);
262 new_termios.c_cc[VMIN] = 1;
263 new_termios.c_cc[VTIME] = 0;
264
265 if (tcsetattr(ttyfd, TCSADRAIN, &new_termios) < 0) {
266 r = -errno;
267 goto finish;
268 }
269
270 reset_tty = true;
271 }
272
273 zero(pollfd);
7f4e0805
LP
274 pollfd[POLL_TTY].fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO;
275 pollfd[POLL_TTY].events = POLLIN;
276 pollfd[POLL_INOTIFY].fd = notify;
277 pollfd[POLL_INOTIFY].events = POLLIN;
278
279 for (;;) {
280 char c;
281 int sleep_for = -1, k;
282 ssize_t n;
283
284 if (until > 0) {
285 usec_t y;
286
287 y = now(CLOCK_MONOTONIC);
288
289 if (y > until) {
7ded2e28 290 r = -ETIME;
7f4e0805
LP
291 goto finish;
292 }
293
294 sleep_for = (int) ((until - y) / USEC_PER_MSEC);
295 }
296
297 if (flag_file)
298 if (access(flag_file, F_OK) < 0) {
299 r = -errno;
300 goto finish;
301 }
302
e287086b 303 k = poll(pollfd, notify >= 0 ? 2 : 1, sleep_for);
981e4cd3 304 if (k < 0) {
7f4e0805
LP
305 if (errno == EINTR)
306 continue;
307
308 r = -errno;
309 goto finish;
310 } else if (k == 0) {
ccc80078 311 r = -ETIME;
7f4e0805
LP
312 goto finish;
313 }
314
e287086b 315 if (notify >= 0 && pollfd[POLL_INOTIFY].revents != 0)
7f4e0805
LP
316 flush_fd(notify);
317
318 if (pollfd[POLL_TTY].revents == 0)
319 continue;
320
981e4cd3
LP
321 n = read(ttyfd >= 0 ? ttyfd : STDIN_FILENO, &c, 1);
322 if (n < 0) {
7f4e0805
LP
323 if (errno == EINTR || errno == EAGAIN)
324 continue;
325
326 r = -errno;
327 goto finish;
328
329 } else if (n == 0)
330 break;
331
332 if (c == '\n')
333 break;
58fc840b 334 else if (c == 21) { /* C-u */
7f4e0805 335
e287086b 336 if (!(flags & ASK_PASSWORD_SILENT))
58fc840b
LP
337 backspace_chars(ttyfd, p);
338 p = 0;
7f4e0805
LP
339
340 } else if (c == '\b' || c == 127) {
58fc840b
LP
341
342 if (p > 0) {
343
e287086b 344 if (!(flags & ASK_PASSWORD_SILENT))
58fc840b
LP
345 backspace_chars(ttyfd, 1);
346
7f4e0805 347 p--;
e287086b 348 } else if (!dirty && !(flags & ASK_PASSWORD_SILENT)) {
441dfe09 349
e287086b 350 flags |= ASK_PASSWORD_SILENT;
441dfe09
LP
351
352 /* There are two ways to enter silent
353 * mode. Either by pressing backspace
e287086b
LP
354 * as first key (and only as first
355 * key), or ... */
441dfe09
LP
356 if (ttyfd >= 0)
357 loop_write(ttyfd, "(no echo) ", 10, false);
358
58fc840b
LP
359 } else if (ttyfd >= 0)
360 loop_write(ttyfd, "\a", 1, false);
7f4e0805 361
e287086b 362 } else if (c == '\t' && !(flags & ASK_PASSWORD_SILENT)) {
58fc840b
LP
363
364 backspace_chars(ttyfd, p);
e287086b 365 flags |= ASK_PASSWORD_SILENT;
58fc840b 366
441dfe09
LP
367 /* ... or by pressing TAB at any time. */
368
58fc840b
LP
369 if (ttyfd >= 0)
370 loop_write(ttyfd, "(no echo) ", 10, false);
7f4e0805 371 } else {
036eeac5
LP
372 if (p >= sizeof(passphrase)-1) {
373 loop_write(ttyfd, "\a", 1, false);
374 continue;
375 }
376
7f4e0805
LP
377 passphrase[p++] = c;
378
f3149d57
ZJS
379 if (!(flags & ASK_PASSWORD_SILENT) && ttyfd >= 0) {
380 n = utf8_encoded_valid_unichar(passphrase + codepoint);
381 if (n >= 0) {
382 codepoint = p;
383 loop_write(ttyfd, (flags & ASK_PASSWORD_ECHO) ? &c : "*", 1, false);
384 }
385 }
441dfe09
LP
386
387 dirty = true;
7f4e0805 388 }
1602b008
LP
389
390 c = 'x';
7f4e0805
LP
391 }
392
981e4cd3 393 x = strndup(passphrase, p);
2d26d8e0 394 explicit_bzero(passphrase, p);
981e4cd3 395 if (!x) {
7f4e0805
LP
396 r = -ENOMEM;
397 goto finish;
398 }
399
e287086b
LP
400 if (keyname)
401 (void) add_to_keyring_and_log(keyname, flags, STRV_MAKE(x));
402
403 *ret = x;
7f4e0805
LP
404 r = 0;
405
406finish:
981e4cd3
LP
407 if (ttyfd >= 0 && reset_tty) {
408 loop_write(ttyfd, "\n", 1, false);
409 tcsetattr(ttyfd, TCSADRAIN, &old_termios);
7f4e0805
LP
410 }
411
412 return r;
413}
414
415static int create_socket(char **name) {
00843602 416 union sockaddr_union sa = {
b92bea5d
ZJS
417 .un.sun_family = AF_UNIX,
418 };
00843602
LP
419 _cleanup_close_ int fd = -1;
420 static const int one = 1;
7f4e0805 421 char *c;
00843602 422 int r;
7f4e0805
LP
423
424 assert(name);
425
e62d8c39 426 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
4a62c710 427 if (fd < 0)
00843602 428 return -errno;
7f4e0805 429
9bf3b535 430 snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%" PRIx64, random_u64());
7f4e0805 431
5c0d398d 432 RUN_WITH_UMASK(0177) {
fc2fffe7 433 if (bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0)
00843602 434 return -errno;
5c0d398d 435 }
1d6702e8 436
00843602
LP
437 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
438 return -errno;
7f4e0805 439
e62d8c39 440 c = strdup(sa.un.sun_path);
00843602
LP
441 if (!c)
442 return -ENOMEM;
7f4e0805
LP
443
444 *name = c;
7f4e0805 445
00843602
LP
446 r = fd;
447 fd = -1;
7f4e0805
LP
448
449 return r;
450}
451
7f4e0805
LP
452int ask_password_agent(
453 const char *message,
454 const char *icon,
9fa1de96 455 const char *id,
e287086b 456 const char *keyname,
7f4e0805 457 usec_t until,
e287086b
LP
458 AskPasswordFlags flags,
459 char ***ret) {
7f4e0805
LP
460
461 enum {
462 FD_SOCKET,
463 FD_SIGNAL,
464 _FD_MAX
465 };
466
e287086b 467 _cleanup_close_ int socket_fd = -1, signal_fd = -1, fd = -1;
2b583ce6 468 char temp[] = "/run/systemd/ask-password/tmp.XXXXXX";
7f4e0805 469 char final[sizeof(temp)] = "";
981e4cd3 470 _cleanup_free_ char *socket_name = NULL;
e287086b
LP
471 _cleanup_strv_free_ char **l = NULL;
472 _cleanup_fclose_ FILE *f = NULL;
7f4e0805 473 struct pollfd pollfd[_FD_MAX];
e287086b 474 sigset_t mask, oldmask;
981e4cd3 475 int r;
7f4e0805 476
e287086b 477 assert(ret);
21bc923a 478
e287086b
LP
479 if (flags & ASK_PASSWORD_NO_AGENT)
480 return -EUNATCH;
00843602 481
72c0a2c2
LP
482 assert_se(sigemptyset(&mask) >= 0);
483 assert_se(sigset_add_many(&mask, SIGINT, SIGTERM, -1) >= 0);
484 assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) >= 0);
f9b72cd8 485
00843602 486 (void) mkdir_p_label("/run/systemd/ask-password", 0755);
7f4e0805 487
646853bd 488 fd = mkostemp_safe(temp);
1d6702e8 489 if (fd < 0) {
709f6e46 490 r = fd;
7f4e0805
LP
491 goto finish;
492 }
493
00843602 494 (void) fchmod(fd, 0644);
7f4e0805 495
981e4cd3
LP
496 f = fdopen(fd, "w");
497 if (!f) {
00843602 498 r = -errno;
7f4e0805
LP
499 goto finish;
500 }
501
502 fd = -1;
503
981e4cd3
LP
504 signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
505 if (signal_fd < 0) {
00843602 506 r = -errno;
7f4e0805
LP
507 goto finish;
508 }
509
981e4cd3
LP
510 socket_fd = create_socket(&socket_name);
511 if (socket_fd < 0) {
7f4e0805
LP
512 r = socket_fd;
513 goto finish;
514 }
515
516 fprintf(f,
517 "[Ask]\n"
de0671ee 518 "PID="PID_FMT"\n"
7f4e0805 519 "Socket=%s\n"
21bc923a 520 "AcceptCached=%i\n"
64845bdc 521 "Echo=%i\n"
de0671ee 522 "NotAfter="USEC_FMT"\n",
df0ff127 523 getpid_cached(),
7f4e0805 524 socket_name,
e287086b
LP
525 (flags & ASK_PASSWORD_ACCEPT_CACHED) ? 1 : 0,
526 (flags & ASK_PASSWORD_ECHO) ? 1 : 0,
de0671ee 527 until);
7f4e0805
LP
528
529 if (message)
530 fprintf(f, "Message=%s\n", message);
531
532 if (icon)
533 fprintf(f, "Icon=%s\n", icon);
534
9fa1de96
DH
535 if (id)
536 fprintf(f, "Id=%s\n", id);
537
dacd6cee 538 r = fflush_and_check(f);
00843602 539 if (r < 0)
7f4e0805 540 goto finish;
7f4e0805
LP
541
542 memcpy(final, temp, sizeof(temp));
543
544 final[sizeof(final)-11] = 'a';
545 final[sizeof(final)-10] = 's';
546 final[sizeof(final)-9] = 'k';
547
548 if (rename(temp, final) < 0) {
00843602 549 r = -errno;
7f4e0805
LP
550 goto finish;
551 }
552
553 zero(pollfd);
554 pollfd[FD_SOCKET].fd = socket_fd;
555 pollfd[FD_SOCKET].events = POLLIN;
556 pollfd[FD_SIGNAL].fd = signal_fd;
557 pollfd[FD_SIGNAL].events = POLLIN;
558
559 for (;;) {
560 char passphrase[LINE_MAX+1];
561 struct msghdr msghdr;
562 struct iovec iovec;
563 struct ucred *ucred;
564 union {
565 struct cmsghdr cmsghdr;
566 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
567 } control;
568 ssize_t n;
569 int k;
570 usec_t t;
571
572 t = now(CLOCK_MONOTONIC);
573
7dcda352 574 if (until > 0 && until <= t) {
7f4e0805
LP
575 r = -ETIME;
576 goto finish;
577 }
578
981e4cd3
LP
579 k = poll(pollfd, _FD_MAX, until > 0 ? (int) ((until-t)/USEC_PER_MSEC) : -1);
580 if (k < 0) {
7f4e0805
LP
581 if (errno == EINTR)
582 continue;
583
00843602 584 r = -errno;
7f4e0805
LP
585 goto finish;
586 }
587
588 if (k <= 0) {
7f4e0805
LP
589 r = -ETIME;
590 goto finish;
591 }
592
21bc923a
LP
593 if (pollfd[FD_SIGNAL].revents & POLLIN) {
594 r = -EINTR;
595 goto finish;
596 }
7f4e0805
LP
597
598 if (pollfd[FD_SOCKET].revents != POLLIN) {
7f4e0805
LP
599 r = -EIO;
600 goto finish;
601 }
602
603 zero(iovec);
604 iovec.iov_base = passphrase;
21bc923a 605 iovec.iov_len = sizeof(passphrase);
7f4e0805
LP
606
607 zero(control);
608 zero(msghdr);
609 msghdr.msg_iov = &iovec;
610 msghdr.msg_iovlen = 1;
611 msghdr.msg_control = &control;
612 msghdr.msg_controllen = sizeof(control);
613
981e4cd3
LP
614 n = recvmsg(socket_fd, &msghdr, 0);
615 if (n < 0) {
7f4e0805
LP
616 if (errno == EAGAIN ||
617 errno == EINTR)
618 continue;
619
00843602 620 r = -errno;
7f4e0805
LP
621 goto finish;
622 }
623
1c8da044
LP
624 cmsg_close_all(&msghdr);
625
7f4e0805 626 if (n <= 0) {
00843602 627 log_debug("Message too short");
7f4e0805
LP
628 continue;
629 }
630
631 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
632 control.cmsghdr.cmsg_level != SOL_SOCKET ||
633 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
634 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
00843602 635 log_debug("Received message without credentials. Ignoring.");
7f4e0805
LP
636 continue;
637 }
638
639 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
640 if (ucred->uid != 0) {
00843602 641 log_debug("Got request from unprivileged user. Ignoring.");
7f4e0805
LP
642 continue;
643 }
644
645 if (passphrase[0] == '+') {
e287086b 646 /* An empty message refers to the empty password */
8254a475
LP
647 if (n == 1)
648 l = strv_new("", NULL);
649 else
650 l = strv_parse_nulstr(passphrase+1, n-1);
2d26d8e0 651 explicit_bzero(passphrase, n);
8254a475 652 if (!l) {
7f4e0805
LP
653 r = -ENOMEM;
654 goto finish;
655 }
656
21bc923a 657 if (strv_length(l) <= 0) {
e287086b 658 l = strv_free(l);
00843602 659 log_debug("Invalid packet");
21bc923a
LP
660 continue;
661 }
662
e287086b
LP
663 break;
664 }
21bc923a 665
e287086b 666 if (passphrase[0] == '-') {
7f4e0805
LP
667 r = -ECANCELED;
668 goto finish;
7f4e0805
LP
669 }
670
e287086b 671 log_debug("Invalid packet");
7f4e0805
LP
672 }
673
e287086b
LP
674 if (keyname)
675 (void) add_to_keyring_and_log(keyname, flags, l);
676
677 *ret = l;
678 l = NULL;
7f4e0805
LP
679 r = 0;
680
681finish:
981e4cd3 682 if (socket_name)
00843602 683 (void) unlink(socket_name);
7f4e0805 684
00843602 685 (void) unlink(temp);
7f4e0805
LP
686
687 if (final[0])
00843602 688 (void) unlink(final);
7f4e0805 689
f9b72cd8 690 assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0);
7f4e0805
LP
691 return r;
692}
260ab287 693
00843602
LP
694int ask_password_auto(
695 const char *message,
696 const char *icon,
697 const char *id,
e287086b 698 const char *keyname,
00843602 699 usec_t until,
e287086b
LP
700 AskPasswordFlags flags,
701 char ***ret) {
00843602
LP
702
703 int r;
704
e287086b 705 assert(ret);
21bc923a 706
e287086b
LP
707 if ((flags & ASK_PASSWORD_ACCEPT_CACHED) && keyname) {
708 r = ask_password_keyring(keyname, flags, ret);
709 if (r != -ENOKEY)
710 return r;
711 }
712
713 if (!(flags & ASK_PASSWORD_NO_TTY) && isatty(STDIN_FILENO)) {
21bc923a
LP
714 char *s = NULL, **l = NULL;
715
e287086b 716 r = ask_password_tty(message, keyname, until, flags, NULL, &s);
981e4cd3 717 if (r < 0)
21bc923a
LP
718 return r;
719
1602b008
LP
720 r = strv_push(&l, s);
721 if (r < 0) {
722 string_erase(s);
723 free(s);
00843602 724 return -ENOMEM;
1602b008 725 }
21bc923a 726
e287086b 727 *ret = l;
00843602
LP
728 return 0;
729 }
730
e287086b
LP
731 if (!(flags & ASK_PASSWORD_NO_AGENT))
732 return ask_password_agent(message, icon, id, keyname, until, flags, ret);
733
734 return -EUNATCH;
260ab287 735}