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"
46 } arg_action = ACTION_QUERY;
48 static bool arg_plymouth = false;
50 static int ask_password_plymouth(const char *message, usec_t until, const char *flag_file, char **_passphrase) {
51 int fd = -1, notify = -1;
52 union sockaddr_union sa;
56 struct pollfd pollfd[2];
57 char buffer[LINE_MAX];
65 if ((notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
70 if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
76 if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
82 sa.sa.sa_family = AF_UNIX;
83 strncpy(sa.un.sun_path+1, "/ply-boot-protocol", sizeof(sa.un.sun_path)-1);
85 if (connect(fd, &sa.sa, sizeof(sa.un)) < 0) {
90 if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) {
95 if ((k = loop_write(fd, packet, n+1, true)) != n+1) {
96 r = k < 0 ? (int) k : -EIO;
101 pollfd[POLL_SOCKET].fd = fd;
102 pollfd[POLL_SOCKET].events = POLLIN;
103 pollfd[POLL_INOTIFY].fd = notify;
104 pollfd[POLL_INOTIFY].events = POLLIN;
107 int sleep_for = -1, j;
112 y = now(CLOCK_MONOTONIC);
119 sleep_for = (int) ((until - y) / USEC_PER_MSEC);
123 if (access(flag_file, F_OK) < 0) {
128 if ((j = poll(pollfd, notify > 0 ? 2 : 1, sleep_for)) < 0) {
140 if (notify > 0 && pollfd[POLL_INOTIFY].revents != 0)
143 if (pollfd[POLL_SOCKET].revents == 0)
146 if ((k = read(fd, buffer + p, sizeof(buffer) - p)) <= 0) {
147 r = k < 0 ? -errno : -EIO;
156 if (buffer[0] == 5) {
157 /* No password, because UI not shown */
161 } else if (buffer[0] == 2) {
169 memcpy(&size, buffer+1, sizeof(size));
170 if (size+5 > sizeof(buffer)) {
178 if (!(s = strndup(buffer + 5, size))) {
196 close_nointr_nofail(notify);
199 close_nointr_nofail(fd);
206 static int parse_password(const char *filename, char **wall) {
207 char *socket_name = NULL, *message = NULL, *packet = NULL;
208 uint64_t not_after = 0;
212 const ConfigItem items[] = {
213 { "Socket", config_parse_string, &socket_name, "Ask" },
214 { "NotAfter", config_parse_uint64, ¬_after, "Ask" },
215 { "Message", config_parse_string, &message, "Ask" },
216 { "PID", config_parse_unsigned, &pid, "Ask" },
217 { NULL, NULL, NULL, NULL }
226 if (!(f = fopen(filename, "re"))) {
231 log_error("open(%s): %m", filename);
235 if ((r = config_parse(filename, f, NULL, items, true, NULL)) < 0) {
236 log_error("Failed to parse password file %s: %s", filename, strerror(-r));
240 if (!socket_name || not_after <= 0) {
241 log_error("Invalid password file %s", filename);
246 n = now(CLOCK_MONOTONIC);
252 if (arg_action == ACTION_LIST)
253 printf("'%s' (PID %u)\n", message, pid);
254 else if (arg_action == ACTION_WALL) {
258 "%s%sPassword entry required for \'%s\' (PID %u).\r\n"
259 "Please enter password with the systemd-tty-ask-password-agent tool!",
261 *wall ? "\r\n\r\n" : "",
264 log_error("Out of memory");
274 struct sockaddr_un un;
278 assert(arg_action == ACTION_QUERY ||
279 arg_action == ACTION_WATCH);
281 if (access(socket_name, W_OK) < 0) {
283 if (arg_action == ACTION_QUERY)
284 log_info("Not querying '%s' (PID %u), lacking privileges.", message, pid);
291 r = ask_password_plymouth(message, not_after, filename, &password);
293 r = ask_password_tty(message, not_after, filename, &password);
296 log_error("Failed to query password: %s", strerror(-r));
300 asprintf(&packet, "+%s", password);
304 log_error("Out of memory");
309 if ((socket_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
310 log_error("socket(): %m");
316 sa.un.sun_family = AF_UNIX;
317 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
319 if (sendto(socket_fd, packet, strlen(packet), MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0) {
320 log_error("Failed to send: %m");
330 close_nointr_nofail(socket_fd);
339 static int tty_block(void) {
344 if (!(t = ttyname(STDIN_FILENO)))
347 if (asprintf(&p, "/dev/.systemd/ask-password-block/%s", file_name_from_path(t)) < 0)
350 mkdir_parents(p, 0700);
353 fd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
362 static bool tty_match(const char *path) {
366 /* We use named pipes to ensure that wall messages suggesting
367 * password entry are not printed over password prompts
368 * already shown. We use the fact here that opening a pipe in
369 * non-blocking mode for write-only will succeed only if
370 * there's some writer behind it. Using pipes has the
371 * advantage that the block will automatically go away if the
374 if (asprintf(&p, "/dev/.systemd/ask-password-block/%s", file_name_from_path(path)) < 0)
377 fd = open(p, O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
383 /* What, we managed to open the pipe? Then this tty is filtered. */
384 close_nointr_nofail(fd);
388 static int show_passwords(void) {
393 if (!(d = opendir("/dev/.systemd/ask-password"))) {
397 log_error("opendir(): %m");
401 while ((de = readdir(d))) {
406 if (de->d_type != DT_REG)
409 if (ignore_file(de->d_name))
412 if (!startswith(de->d_name, "ask."))
415 if (!(p = strappend("/dev/.systemd/ask-password/", de->d_name))) {
416 log_error("Out of memory");
422 if ((q = parse_password(p, &wall)) < 0)
428 utmp_wall(wall, tty_match);
440 static int watch_passwords(void) {
447 int notify = -1, signal_fd = -1, tty_block_fd = -1;
448 struct pollfd pollfd[_FD_MAX];
452 tty_block_fd = tty_block();
454 mkdir_p("/dev/.systemd/ask-password", 0755);
456 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
461 if (inotify_add_watch(notify, "/dev/.systemd/ask-password", IN_CLOSE_WRITE|IN_MOVED_TO) < 0) {
466 assert_se(sigemptyset(&mask) == 0);
467 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
468 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
470 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
471 log_error("signalfd(): %m");
477 pollfd[FD_INOTIFY].fd = notify;
478 pollfd[FD_INOTIFY].events = POLLIN;
479 pollfd[FD_SIGNAL].fd = signal_fd;
480 pollfd[FD_SIGNAL].events = POLLIN;
483 if ((r = show_passwords()) < 0)
486 if (poll(pollfd, _FD_MAX, -1) < 0) {
495 if (pollfd[FD_INOTIFY].revents != 0)
498 if (pollfd[FD_SIGNAL].revents != 0)
506 close_nointr_nofail(notify);
509 close_nointr_nofail(signal_fd);
511 if (tty_block_fd >= 0)
512 close_nointr_nofail(tty_block_fd);
517 static int help(void) {
519 printf("%s [OPTIONS...]\n\n"
520 "Process system password requests.\n\n"
521 " -h --help Show this help\n"
522 " --list Show pending password requests\n"
523 " --query Process pending password requests\n"
524 " --watch Continously process password requests\n"
525 " --wall Continously forward password requests to wall\n"
526 " --plymouth Ask question with Plymouth instead of on TTY\n",
527 program_invocation_short_name);
532 static int parse_argv(int argc, char *argv[]) {
542 static const struct option options[] = {
543 { "help", no_argument, NULL, 'h' },
544 { "list", no_argument, NULL, ARG_LIST },
545 { "query", no_argument, NULL, ARG_QUERY },
546 { "watch", no_argument, NULL, ARG_WATCH },
547 { "wall", no_argument, NULL, ARG_WALL },
548 { "plymouth", no_argument, NULL, ARG_PLYMOUTH },
557 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
566 arg_action = ACTION_LIST;
570 arg_action = ACTION_QUERY;
574 arg_action = ACTION_WATCH;
578 arg_action = ACTION_WALL;
589 log_error("Unknown option code %c", c);
594 if (optind != argc) {
602 int main(int argc, char *argv[]) {
605 log_parse_environment();
608 if ((r = parse_argv(argc, argv)) <= 0)
611 if (arg_action == ACTION_WATCH ||
612 arg_action == ACTION_WALL)
613 r = watch_passwords();
615 r = show_passwords();
618 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;