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