]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/tty-ask-password-agent.c
ask-password: support passwords without timeouts
[thirdparty/systemd.git] / src / tty-ask-password-agent.c
CommitLineData
ec863ba6
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <stdbool.h>
23#include <errno.h>
24#include <string.h>
25#include <sys/socket.h>
26#include <sys/un.h>
27#include <stddef.h>
28#include <sys/poll.h>
29#include <sys/inotify.h>
30#include <unistd.h>
31#include <getopt.h>
b9ba604e 32#include <sys/signalfd.h>
7af53310 33#include <fcntl.h>
ec863ba6
LP
34
35#include "util.h"
36#include "conf-parser.h"
37#include "utmp-wtmp.h"
e5ebf783 38#include "socket-util.h"
7f4e0805 39#include "ask-password-api.h"
21bc923a 40#include "strv.h"
ec863ba6
LP
41
42static enum {
43 ACTION_LIST,
44 ACTION_QUERY,
45 ACTION_WATCH,
46 ACTION_WALL
47} arg_action = ACTION_QUERY;
48
e5ebf783 49static bool arg_plymouth = false;
0cf84693 50static bool arg_console = false;
e5ebf783 51
21bc923a
LP
52static int ask_password_plymouth(
53 const char *message,
54 usec_t until,
55 const char *flag_file,
56 bool accept_cached,
57 char ***_passphrases) {
58
e5ebf783
LP
59 int fd = -1, notify = -1;
60 union sockaddr_union sa;
61 char *packet = NULL;
62 ssize_t k;
63 int r, n;
64 struct pollfd pollfd[2];
65 char buffer[LINE_MAX];
66 size_t p = 0;
67 enum {
68 POLL_SOCKET,
69 POLL_INOTIFY
70 };
71
21bc923a
LP
72 assert(_passphrases);
73
e5ebf783
LP
74 if (flag_file) {
75 if ((notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
76 r = -errno;
77 goto finish;
78 }
79
80 if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
81 r = -errno;
82 goto finish;
83 }
84 }
85
86 if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
87 r = -errno;
88 goto finish;
89 }
90
91 zero(sa);
92 sa.sa.sa_family = AF_UNIX;
96707269
LP
93 strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1);
94 if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
c0f9c7da 95 log_error("Failed to connect to Plymouth: %m");
e5ebf783
LP
96 r = -errno;
97 goto finish;
98 }
99
21bc923a
LP
100 if (accept_cached) {
101 packet = strdup("c");
102 n = 1;
103 } else
104 asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n);
105
106 if (!packet) {
e5ebf783
LP
107 r = -ENOMEM;
108 goto finish;
109 }
110
111 if ((k = loop_write(fd, packet, n+1, true)) != n+1) {
112 r = k < 0 ? (int) k : -EIO;
113 goto finish;
114 }
115
116 zero(pollfd);
117 pollfd[POLL_SOCKET].fd = fd;
118 pollfd[POLL_SOCKET].events = POLLIN;
119 pollfd[POLL_INOTIFY].fd = notify;
120 pollfd[POLL_INOTIFY].events = POLLIN;
121
122 for (;;) {
123 int sleep_for = -1, j;
124
125 if (until > 0) {
126 usec_t y;
127
128 y = now(CLOCK_MONOTONIC);
129
130 if (y > until) {
ccc80078 131 r = -ETIME;
e5ebf783
LP
132 goto finish;
133 }
134
135 sleep_for = (int) ((until - y) / USEC_PER_MSEC);
136 }
137
138 if (flag_file)
139 if (access(flag_file, F_OK) < 0) {
140 r = -errno;
141 goto finish;
142 }
143
144 if ((j = poll(pollfd, notify > 0 ? 2 : 1, sleep_for)) < 0) {
145
146 if (errno == EINTR)
147 continue;
148
149 r = -errno;
150 goto finish;
151 } else if (j == 0) {
ccc80078 152 r = -ETIME;
e5ebf783
LP
153 goto finish;
154 }
155
156 if (notify > 0 && pollfd[POLL_INOTIFY].revents != 0)
157 flush_fd(notify);
158
159 if (pollfd[POLL_SOCKET].revents == 0)
160 continue;
161
162 if ((k = read(fd, buffer + p, sizeof(buffer) - p)) <= 0) {
163 r = k < 0 ? -errno : -EIO;
164 goto finish;
165 }
166
167 p += k;
168
169 if (p < 1)
170 continue;
171
172 if (buffer[0] == 5) {
21bc923a
LP
173
174 if (accept_cached) {
175 /* Hmm, first try with cached
176 * passwords failed, so let's retry
177 * with a normal password request */
178 free(packet);
179 packet = NULL;
180
181 if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) {
182 r = -ENOMEM;
183 goto finish;
184 }
185
186 if ((k = loop_write(fd, packet, n+1, true)) != n+1) {
187 r = k < 0 ? (int) k : -EIO;
188 goto finish;
189 }
190
191 accept_cached = false;
192 p = 0;
193 continue;
194 }
195
e5ebf783
LP
196 /* No password, because UI not shown */
197 r = -ENOENT;
198 goto finish;
199
21bc923a 200 } else if (buffer[0] == 2 || buffer[0] == 9) {
e5ebf783 201 uint32_t size;
21bc923a 202 char **l;
e5ebf783 203
21bc923a 204 /* One ore more answers */
e5ebf783
LP
205 if (p < 5)
206 continue;
207
208 memcpy(&size, buffer+1, sizeof(size));
209 if (size+5 > sizeof(buffer)) {
210 r = -EIO;
211 goto finish;
212 }
213
214 if (p-5 < size)
215 continue;
216
21bc923a 217 if (!(l = strv_parse_nulstr(buffer + 5, size))) {
e5ebf783
LP
218 r = -ENOMEM;
219 goto finish;
220 }
221
21bc923a 222 *_passphrases = l;
e5ebf783 223 break;
21bc923a 224
e5ebf783
LP
225 } else {
226 /* Unknown packet */
227 r = -EIO;
228 goto finish;
229 }
230 }
231
232 r = 0;
233
234finish:
235 if (notify >= 0)
236 close_nointr_nofail(notify);
237
238 if (fd >= 0)
239 close_nointr_nofail(fd);
240
241 free(packet);
242
243 return r;
244}
245
0ddf1d3a 246static int parse_password(const char *filename, char **wall) {
ec863ba6
LP
247 char *socket_name = NULL, *message = NULL, *packet = NULL;
248 uint64_t not_after = 0;
249 unsigned pid = 0;
250 int socket_fd = -1;
21bc923a 251 bool accept_cached = false;
ec863ba6
LP
252
253 const ConfigItem items[] = {
2b583ce6
KS
254 { "Socket", config_parse_string, 0, &socket_name, "Ask" },
255 { "NotAfter", config_parse_uint64, 0, &not_after, "Ask" },
256 { "Message", config_parse_string, 0, &message, "Ask" },
257 { "PID", config_parse_unsigned, 0, &pid, "Ask" },
258 { "AcceptCached", config_parse_bool, 0, &accept_cached, "Ask" },
259 { NULL, NULL, 0, NULL, NULL }
ec863ba6
LP
260 };
261
262 FILE *f;
263 int r;
ec863ba6
LP
264
265 assert(filename);
266
267 if (!(f = fopen(filename, "re"))) {
268
269 if (errno == ENOENT)
270 return 0;
271
272 log_error("open(%s): %m", filename);
273 return -errno;
274 }
275
656ce8f7 276 if ((r = config_parse(filename, f, NULL, items, true, NULL)) < 0) {
ec863ba6
LP
277 log_error("Failed to parse password file %s: %s", filename, strerror(-r));
278 goto finish;
279 }
280
7dcda352 281 if (!socket_name) {
ec863ba6
LP
282 log_error("Invalid password file %s", filename);
283 r = -EBADMSG;
284 goto finish;
285 }
286
7dcda352
LP
287 if (not_after > 0) {
288 if (now(CLOCK_MONOTONIC) > not_after) {
289 r = 0;
290 goto finish;
291 }
ec863ba6
LP
292 }
293
294 if (arg_action == ACTION_LIST)
295 printf("'%s' (PID %u)\n", message, pid);
296 else if (arg_action == ACTION_WALL) {
0ddf1d3a 297 char *_wall;
ec863ba6 298
0ddf1d3a
LP
299 if (asprintf(&_wall,
300 "%s%sPassword entry required for \'%s\' (PID %u).\r\n"
9d3e691e 301 "Please enter password with the systemd-tty-ask-password-agent tool!",
0ddf1d3a
LP
302 *wall ? *wall : "",
303 *wall ? "\r\n\r\n" : "",
ec863ba6
LP
304 message,
305 pid) < 0) {
306 log_error("Out of memory");
307 r = -ENOMEM;
308 goto finish;
309 }
310
0ddf1d3a
LP
311 free(*wall);
312 *wall = _wall;
ec863ba6
LP
313 } else {
314 union {
315 struct sockaddr sa;
316 struct sockaddr_un un;
317 } sa;
3414abee 318 size_t packet_length = 0;
ec863ba6
LP
319
320 assert(arg_action == ACTION_QUERY ||
321 arg_action == ACTION_WATCH);
322
323 if (access(socket_name, W_OK) < 0) {
324
325 if (arg_action == ACTION_QUERY)
326 log_info("Not querying '%s' (PID %u), lacking privileges.", message, pid);
327
328 r = 0;
329 goto finish;
330 }
331
21bc923a 332 if (arg_plymouth) {
3414abee 333 char **passwords = NULL;
21bc923a
LP
334
335 if ((r = ask_password_plymouth(message, not_after, filename, accept_cached, &passwords)) >= 0) {
336 char **p;
337
338 packet_length = 1;
339 STRV_FOREACH(p, passwords)
340 packet_length += strlen(*p) + 1;
341
342 if (!(packet = new(char, packet_length)))
343 r = -ENOMEM;
344 else {
345 char *d;
346
347 packet[0] = '+';
348 d = packet+1;
349
350 STRV_FOREACH(p, passwords)
351 d = stpcpy(d, *p) + 1;
352 }
353 }
354
355 } else {
0cf84693 356 int tty_fd = -1;
21bc923a 357 char *password;
0cf84693
LP
358
359 if (arg_console)
360 if ((tty_fd = acquire_terminal("/dev/console", false, false, false)) < 0) {
361 r = tty_fd;
362 goto finish;
363 }
364
e5ebf783
LP
365 r = ask_password_tty(message, not_after, filename, &password);
366
0cf84693
LP
367 if (arg_console) {
368 close_nointr_nofail(tty_fd);
369 release_terminal();
370 }
21bc923a 371
d55f4f3f
LP
372 packet_length = 1+strlen(password)+1;
373 if (!(packet = new(char, packet_length)))
374 r = -ENOMEM;
375 else {
376 packet[0] = '+';
377 strcpy(packet+1, password);
378 }
21bc923a 379
d55f4f3f 380 free(password);
0cf84693
LP
381 }
382
446f0046
LP
383 if (r == -ETIME || r == -ENOENT) {
384 /* If the query went away, that's OK */
385 r = 0;
386 goto finish;
387 }
388
e5ebf783
LP
389 if (r < 0) {
390 log_error("Failed to query password: %s", strerror(-r));
ec863ba6
LP
391 goto finish;
392 }
393
ec863ba6
LP
394 if ((socket_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
395 log_error("socket(): %m");
396 r = -errno;
397 goto finish;
398 }
399
400 zero(sa);
401 sa.un.sun_family = AF_UNIX;
402 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
403
21bc923a 404 if (sendto(socket_fd, packet, packet_length, MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0) {
ec863ba6
LP
405 log_error("Failed to send: %m");
406 r = -errno;
407 goto finish;
408 }
409 }
410
411finish:
412 fclose(f);
413
414 if (socket_fd >= 0)
415 close_nointr_nofail(socket_fd);
416
417 free(packet);
418 free(socket_name);
419 free(message);
420
421 return r;
422}
423
0cf84693 424static int wall_tty_block(void) {
7af53310 425 char *p;
fc116c6a
LP
426 int fd, r;
427 dev_t devnr;
7af53310 428
fc116c6a
LP
429 if ((r = get_ctty_devnr(&devnr)) < 0)
430 return -r;
7af53310 431
2b583ce6 432 if (asprintf(&p, "/run/systemd/ask-password-block/%u:%u", major(devnr), minor(devnr)) < 0)
7af53310
LP
433 return -ENOMEM;
434
435 mkdir_parents(p, 0700);
436 mkfifo(p, 0600);
437
438 fd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
439 free(p);
440
441 if (fd < 0)
442 return -errno;
443
444 return fd;
445}
446
0cf84693 447static bool wall_tty_match(const char *path) {
fc116c6a 448 int fd, k;
7af53310 449 char *p;
fc116c6a
LP
450 struct stat st;
451
452 if (path_is_absolute(path))
453 k = lstat(path, &st);
454 else {
455 if (asprintf(&p, "/dev/%s", path) < 0)
456 return true;
457
458 k = lstat(p, &st);
459 free(p);
460 }
461
462 if (k < 0)
463 return true;
464
465 if (!S_ISCHR(st.st_mode))
466 return true;
7af53310
LP
467
468 /* We use named pipes to ensure that wall messages suggesting
469 * password entry are not printed over password prompts
470 * already shown. We use the fact here that opening a pipe in
471 * non-blocking mode for write-only will succeed only if
472 * there's some writer behind it. Using pipes has the
473 * advantage that the block will automatically go away if the
474 * process dies. */
475
2b583ce6 476 if (asprintf(&p, "/run/systemd/ask-password-block/%u:%u", major(st.st_rdev), minor(st.st_rdev)) < 0)
7af53310
LP
477 return true;
478
479 fd = open(p, O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
480 free(p);
481
482 if (fd < 0)
483 return true;
484
485 /* What, we managed to open the pipe? Then this tty is filtered. */
486 close_nointr_nofail(fd);
487 return false;
488}
489
ec863ba6
LP
490static int show_passwords(void) {
491 DIR *d;
492 struct dirent *de;
493 int r = 0;
494
2b583ce6 495 if (!(d = opendir("/run/systemd/ask-password"))) {
ec863ba6
LP
496 if (errno == ENOENT)
497 return 0;
498
499 log_error("opendir(): %m");
500 return -errno;
501 }
502
503 while ((de = readdir(d))) {
504 char *p;
505 int q;
0ddf1d3a 506 char *wall;
ec863ba6 507
1a6f4df6
LP
508 /* We only support /dev on tmpfs, hence we can rely on
509 * d_type to be reliable */
510
ec863ba6
LP
511 if (de->d_type != DT_REG)
512 continue;
513
514 if (ignore_file(de->d_name))
515 continue;
516
517 if (!startswith(de->d_name, "ask."))
518 continue;
519
2b583ce6 520 if (!(p = strappend("/run/systemd/ask-password/", de->d_name))) {
ec863ba6
LP
521 log_error("Out of memory");
522 r = -ENOMEM;
523 goto finish;
524 }
525
0ddf1d3a
LP
526 wall = NULL;
527 if ((q = parse_password(p, &wall)) < 0)
ec863ba6
LP
528 r = q;
529
530 free(p);
0ddf1d3a
LP
531
532 if (wall) {
0cf84693 533 utmp_wall(wall, wall_tty_match);
0ddf1d3a
LP
534 free(wall);
535 }
ec863ba6
LP
536 }
537
538finish:
539 if (d)
540 closedir(d);
541
542 return r;
543}
544
545static int watch_passwords(void) {
b9ba604e
LP
546 enum {
547 FD_INOTIFY,
548 FD_SIGNAL,
549 _FD_MAX
550 };
551
7af53310 552 int notify = -1, signal_fd = -1, tty_block_fd = -1;
b9ba604e
LP
553 struct pollfd pollfd[_FD_MAX];
554 sigset_t mask;
ec863ba6
LP
555 int r;
556
0cf84693 557 tty_block_fd = wall_tty_block();
7af53310 558
2b583ce6 559 mkdir_p("/run/systemd/ask-password", 0755);
ec863ba6
LP
560
561 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
562 r = -errno;
563 goto finish;
564 }
565
2b583ce6 566 if (inotify_add_watch(notify, "/run/systemd/ask-password", IN_CLOSE_WRITE|IN_MOVED_TO) < 0) {
ec863ba6
LP
567 r = -errno;
568 goto finish;
569 }
570
b9ba604e
LP
571 assert_se(sigemptyset(&mask) == 0);
572 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
573 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
574
575 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
576 log_error("signalfd(): %m");
577 r = -errno;
578 goto finish;
579 }
580
ec863ba6 581 zero(pollfd);
b9ba604e
LP
582 pollfd[FD_INOTIFY].fd = notify;
583 pollfd[FD_INOTIFY].events = POLLIN;
584 pollfd[FD_SIGNAL].fd = signal_fd;
585 pollfd[FD_SIGNAL].events = POLLIN;
ec863ba6
LP
586
587 for (;;) {
588 if ((r = show_passwords()) < 0)
0cf84693 589 log_error("Failed to show password: %s", strerror(-r));
ec863ba6 590
b9ba604e 591 if (poll(pollfd, _FD_MAX, -1) < 0) {
ec863ba6
LP
592
593 if (errno == EINTR)
594 continue;
595
596 r = -errno;
597 goto finish;
598 }
599
b9ba604e 600 if (pollfd[FD_INOTIFY].revents != 0)
ec863ba6 601 flush_fd(notify);
b9ba604e
LP
602
603 if (pollfd[FD_SIGNAL].revents != 0)
604 break;
ec863ba6
LP
605 }
606
607 r = 0;
608
609finish:
610 if (notify >= 0)
611 close_nointr_nofail(notify);
612
b9ba604e
LP
613 if (signal_fd >= 0)
614 close_nointr_nofail(signal_fd);
615
7af53310
LP
616 if (tty_block_fd >= 0)
617 close_nointr_nofail(tty_block_fd);
618
ec863ba6
LP
619 return r;
620}
621
622static int help(void) {
623
624 printf("%s [OPTIONS...]\n\n"
625 "Process system password requests.\n\n"
e5ebf783
LP
626 " -h --help Show this help\n"
627 " --list Show pending password requests\n"
628 " --query Process pending password requests\n"
35b8ca3a
HH
629 " --watch Continuously process password requests\n"
630 " --wall Continuously forward password requests to wall\n"
0cf84693
LP
631 " --plymouth Ask question with Plymouth instead of on TTY\n"
632 " --console Ask question on /dev/console instead of current TTY\n",
ec863ba6
LP
633 program_invocation_short_name);
634
635 return 0;
636}
637
638static int parse_argv(int argc, char *argv[]) {
639
640 enum {
641 ARG_LIST = 0x100,
642 ARG_QUERY,
643 ARG_WATCH,
644 ARG_WALL,
0cf84693
LP
645 ARG_PLYMOUTH,
646 ARG_CONSOLE
ec863ba6
LP
647 };
648
649 static const struct option options[] = {
e5ebf783
LP
650 { "help", no_argument, NULL, 'h' },
651 { "list", no_argument, NULL, ARG_LIST },
652 { "query", no_argument, NULL, ARG_QUERY },
653 { "watch", no_argument, NULL, ARG_WATCH },
654 { "wall", no_argument, NULL, ARG_WALL },
655 { "plymouth", no_argument, NULL, ARG_PLYMOUTH },
0cf84693 656 { "console", no_argument, NULL, ARG_CONSOLE },
e5ebf783 657 { NULL, 0, NULL, 0 }
ec863ba6
LP
658 };
659
660 int c;
661
662 assert(argc >= 0);
663 assert(argv);
664
665 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
666
667 switch (c) {
668
669 case 'h':
670 help();
671 return 0;
672
673 case ARG_LIST:
674 arg_action = ACTION_LIST;
675 break;
676
677 case ARG_QUERY:
678 arg_action = ACTION_QUERY;
679 break;
680
681 case ARG_WATCH:
682 arg_action = ACTION_WATCH;
683 break;
684
685 case ARG_WALL:
686 arg_action = ACTION_WALL;
687 break;
688
e5ebf783
LP
689 case ARG_PLYMOUTH:
690 arg_plymouth = true;
691 break;
692
0cf84693
LP
693 case ARG_CONSOLE:
694 arg_console = true;
695 break;
696
ec863ba6
LP
697 case '?':
698 return -EINVAL;
699
700 default:
701 log_error("Unknown option code %c", c);
702 return -EINVAL;
703 }
704 }
705
706 if (optind != argc) {
707 help();
708 return -EINVAL;
709 }
710
711 return 1;
712}
713
714int main(int argc, char *argv[]) {
715 int r;
716
717 log_parse_environment();
718 log_open();
719
720 if ((r = parse_argv(argc, argv)) <= 0)
721 goto finish;
722
0cf84693
LP
723 if (arg_console) {
724 setsid();
725 release_terminal();
726 }
727
ec863ba6
LP
728 if (arg_action == ACTION_WATCH ||
729 arg_action == ACTION_WALL)
730 r = watch_passwords();
731 else
732 r = show_passwords();
733
96707269
LP
734 if (r < 0)
735 log_error("Error: %s", strerror(-r));
736
ec863ba6
LP
737finish:
738 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
739}