1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
25 #include <sys/socket.h>
29 #include <sys/inotify.h>
32 #include <sys/signalfd.h>
36 #include "conf-parser.h"
37 #include "utmp-wtmp.h"
38 #include "socket-util.h"
39 #include "ask-password-api.h"
47 } arg_action = ACTION_QUERY;
49 static bool arg_plymouth = false;
50 static bool arg_console = false;
52 static int ask_password_plymouth(
55 const char *flag_file,
57 char ***_passphrases) {
59 int fd = -1, notify = -1;
60 union sockaddr_union sa;
64 struct pollfd pollfd[2];
65 char buffer[LINE_MAX];
75 if ((notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
80 if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
86 if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
92 sa.sa.sa_family = AF_UNIX;
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) {
95 log_error("Failed to connect to Plymouth: %m");
101 packet = strdup("c");
104 asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n);
111 if ((k = loop_write(fd, packet, n+1, true)) != n+1) {
112 r = k < 0 ? (int) k : -EIO;
117 pollfd[POLL_SOCKET].fd = fd;
118 pollfd[POLL_SOCKET].events = POLLIN;
119 pollfd[POLL_INOTIFY].fd = notify;
120 pollfd[POLL_INOTIFY].events = POLLIN;
123 int sleep_for = -1, j;
128 y = now(CLOCK_MONOTONIC);
135 sleep_for = (int) ((until - y) / USEC_PER_MSEC);
139 if (access(flag_file, F_OK) < 0) {
144 if ((j = poll(pollfd, notify > 0 ? 2 : 1, sleep_for)) < 0) {
156 if (notify > 0 && pollfd[POLL_INOTIFY].revents != 0)
159 if (pollfd[POLL_SOCKET].revents == 0)
162 if ((k = read(fd, buffer + p, sizeof(buffer) - p)) <= 0) {
163 r = k < 0 ? -errno : -EIO;
172 if (buffer[0] == 5) {
175 /* Hmm, first try with cached
176 * passwords failed, so let's retry
177 * with a normal password request */
181 if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) {
186 if ((k = loop_write(fd, packet, n+1, true)) != n+1) {
187 r = k < 0 ? (int) k : -EIO;
191 accept_cached = false;
196 /* No password, because UI not shown */
200 } else if (buffer[0] == 2 || buffer[0] == 9) {
204 /* One ore more answers */
208 memcpy(&size, buffer+1, sizeof(size));
209 if (size+5 > sizeof(buffer)) {
217 if (!(l = strv_parse_nulstr(buffer + 5, size))) {
236 close_nointr_nofail(notify);
239 close_nointr_nofail(fd);
246 static int parse_password(const char *filename, char **wall) {
247 char *socket_name = NULL, *message = NULL, *packet = NULL;
248 uint64_t not_after = 0;
251 bool accept_cached = false;
253 const ConfigItem items[] = {
254 { "Socket", config_parse_string, &socket_name, "Ask" },
255 { "NotAfter", config_parse_uint64, ¬_after, "Ask" },
256 { "Message", config_parse_string, &message, "Ask" },
257 { "PID", config_parse_unsigned, &pid, "Ask" },
258 { "AcceptCached", config_parse_bool, &accept_cached, "Ask" },
259 { NULL, NULL, NULL, NULL }
268 if (!(f = fopen(filename, "re"))) {
273 log_error("open(%s): %m", filename);
277 if ((r = config_parse(filename, f, NULL, items, true, NULL)) < 0) {
278 log_error("Failed to parse password file %s: %s", filename, strerror(-r));
282 if (!socket_name || not_after <= 0) {
283 log_error("Invalid password file %s", filename);
288 n = now(CLOCK_MONOTONIC);
294 if (arg_action == ACTION_LIST)
295 printf("'%s' (PID %u)\n", message, pid);
296 else if (arg_action == ACTION_WALL) {
300 "%s%sPassword entry required for \'%s\' (PID %u).\r\n"
301 "Please enter password with the systemd-tty-ask-password-agent tool!",
303 *wall ? "\r\n\r\n" : "",
306 log_error("Out of memory");
316 struct sockaddr_un un;
318 size_t packet_length = 0;
320 assert(arg_action == ACTION_QUERY ||
321 arg_action == ACTION_WATCH);
323 if (access(socket_name, W_OK) < 0) {
325 if (arg_action == ACTION_QUERY)
326 log_info("Not querying '%s' (PID %u), lacking privileges.", message, pid);
333 char **passwords = NULL;
335 if ((r = ask_password_plymouth(message, not_after, filename, accept_cached, &passwords)) >= 0) {
339 STRV_FOREACH(p, passwords)
340 packet_length += strlen(*p) + 1;
342 if (!(packet = new(char, packet_length)))
350 STRV_FOREACH(p, passwords)
351 d = stpcpy(d, *p) + 1;
360 if ((tty_fd = acquire_terminal("/dev/console", false, false, false)) < 0) {
365 r = ask_password_tty(message, not_after, filename, &password);
368 close_nointr_nofail(tty_fd);
372 asprintf(&packet, "+%s", password);
375 packet_length = strlen(packet);
378 if (r == -ETIME || r == -ENOENT) {
379 /* If the query went away, that's OK */
386 log_error("Failed to query password: %s", strerror(-r));
391 log_error("Out of memory");
396 if ((socket_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
397 log_error("socket(): %m");
403 sa.un.sun_family = AF_UNIX;
404 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
406 if (sendto(socket_fd, packet, packet_length, MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0) {
407 log_error("Failed to send: %m");
417 close_nointr_nofail(socket_fd);
426 static int wall_tty_block(void) {
431 if ((r = get_ctty_devnr(&devnr)) < 0)
434 if (asprintf(&p, "/dev/.run/systemd/ask-password-block/%u:%u", major(devnr), minor(devnr)) < 0)
437 mkdir_parents(p, 0700);
440 fd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
449 static bool wall_tty_match(const char *path) {
454 if (path_is_absolute(path))
455 k = lstat(path, &st);
457 if (asprintf(&p, "/dev/%s", path) < 0)
467 if (!S_ISCHR(st.st_mode))
470 /* We use named pipes to ensure that wall messages suggesting
471 * password entry are not printed over password prompts
472 * already shown. We use the fact here that opening a pipe in
473 * non-blocking mode for write-only will succeed only if
474 * there's some writer behind it. Using pipes has the
475 * advantage that the block will automatically go away if the
478 if (asprintf(&p, "/dev/.run/systemd/ask-password-block/%u:%u", major(st.st_rdev), minor(st.st_rdev)) < 0)
481 fd = open(p, O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
487 /* What, we managed to open the pipe? Then this tty is filtered. */
488 close_nointr_nofail(fd);
492 static int show_passwords(void) {
497 if (!(d = opendir("/dev/.run/systemd/ask-password"))) {
501 log_error("opendir(): %m");
505 while ((de = readdir(d))) {
510 /* We only support /dev on tmpfs, hence we can rely on
511 * d_type to be reliable */
513 if (de->d_type != DT_REG)
516 if (ignore_file(de->d_name))
519 if (!startswith(de->d_name, "ask."))
522 if (!(p = strappend("/dev/.run/systemd/ask-password/", de->d_name))) {
523 log_error("Out of memory");
529 if ((q = parse_password(p, &wall)) < 0)
535 utmp_wall(wall, wall_tty_match);
547 static int watch_passwords(void) {
554 int notify = -1, signal_fd = -1, tty_block_fd = -1;
555 struct pollfd pollfd[_FD_MAX];
559 tty_block_fd = wall_tty_block();
561 mkdir_p("/dev/.run/systemd/ask-password", 0755);
563 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
568 if (inotify_add_watch(notify, "/dev/.run/systemd/ask-password", IN_CLOSE_WRITE|IN_MOVED_TO) < 0) {
573 assert_se(sigemptyset(&mask) == 0);
574 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
575 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
577 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
578 log_error("signalfd(): %m");
584 pollfd[FD_INOTIFY].fd = notify;
585 pollfd[FD_INOTIFY].events = POLLIN;
586 pollfd[FD_SIGNAL].fd = signal_fd;
587 pollfd[FD_SIGNAL].events = POLLIN;
590 if ((r = show_passwords()) < 0)
591 log_error("Failed to show password: %s", strerror(-r));
593 if (poll(pollfd, _FD_MAX, -1) < 0) {
602 if (pollfd[FD_INOTIFY].revents != 0)
605 if (pollfd[FD_SIGNAL].revents != 0)
613 close_nointr_nofail(notify);
616 close_nointr_nofail(signal_fd);
618 if (tty_block_fd >= 0)
619 close_nointr_nofail(tty_block_fd);
624 static int help(void) {
626 printf("%s [OPTIONS...]\n\n"
627 "Process system password requests.\n\n"
628 " -h --help Show this help\n"
629 " --list Show pending password requests\n"
630 " --query Process pending password requests\n"
631 " --watch Continuously process password requests\n"
632 " --wall Continuously forward password requests to wall\n"
633 " --plymouth Ask question with Plymouth instead of on TTY\n"
634 " --console Ask question on /dev/console instead of current TTY\n",
635 program_invocation_short_name);
640 static int parse_argv(int argc, char *argv[]) {
651 static const struct option options[] = {
652 { "help", no_argument, NULL, 'h' },
653 { "list", no_argument, NULL, ARG_LIST },
654 { "query", no_argument, NULL, ARG_QUERY },
655 { "watch", no_argument, NULL, ARG_WATCH },
656 { "wall", no_argument, NULL, ARG_WALL },
657 { "plymouth", no_argument, NULL, ARG_PLYMOUTH },
658 { "console", no_argument, NULL, ARG_CONSOLE },
667 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
676 arg_action = ACTION_LIST;
680 arg_action = ACTION_QUERY;
684 arg_action = ACTION_WATCH;
688 arg_action = ACTION_WALL;
703 log_error("Unknown option code %c", c);
708 if (optind != argc) {
716 int main(int argc, char *argv[]) {
719 log_parse_environment();
722 if ((r = parse_argv(argc, argv)) <= 0)
730 if (arg_action == ACTION_WATCH ||
731 arg_action == ACTION_WALL)
732 r = watch_passwords();
734 r = show_passwords();
737 log_error("Error: %s", strerror(-r));
740 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;