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>
35 #include "conf-parser.h"
36 #include "utmp-wtmp.h"
37 #include "socket-util.h"
38 #include "ask-password-api.h"
45 } arg_action = ACTION_QUERY;
47 static bool arg_plymouth = false;
49 static int ask_password_plymouth(const char *message, usec_t until, const char *flag_file, char **_passphrase) {
50 int fd = -1, notify = -1;
51 union sockaddr_union sa;
55 struct pollfd pollfd[2];
56 char buffer[LINE_MAX];
64 if ((notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
69 if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
75 if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
81 sa.sa.sa_family = AF_UNIX;
82 strncpy(sa.un.sun_path+1, "/ply-boot-protocol", sizeof(sa.un.sun_path)-1);
84 if (connect(fd, &sa.sa, sizeof(sa.un)) < 0) {
89 if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) {
94 if ((k = loop_write(fd, packet, n+1, true)) != n+1) {
95 r = k < 0 ? (int) k : -EIO;
100 pollfd[POLL_SOCKET].fd = fd;
101 pollfd[POLL_SOCKET].events = POLLIN;
102 pollfd[POLL_INOTIFY].fd = notify;
103 pollfd[POLL_INOTIFY].events = POLLIN;
106 int sleep_for = -1, j;
111 y = now(CLOCK_MONOTONIC);
118 sleep_for = (int) ((until - y) / USEC_PER_MSEC);
122 if (access(flag_file, F_OK) < 0) {
127 if ((j = poll(pollfd, notify > 0 ? 2 : 1, sleep_for)) < 0) {
139 if (notify > 0 && pollfd[POLL_INOTIFY].revents != 0)
142 if (pollfd[POLL_SOCKET].revents == 0)
145 if ((k = read(fd, buffer + p, sizeof(buffer) - p)) <= 0) {
146 r = k < 0 ? -errno : -EIO;
155 if (buffer[0] == 5) {
156 /* No password, because UI not shown */
160 } else if (buffer[0] == 2) {
168 memcpy(&size, buffer+1, sizeof(size));
169 if (size+5 > sizeof(buffer)) {
177 if (!(s = strndup(buffer + 5, size))) {
195 close_nointr_nofail(notify);
198 close_nointr_nofail(fd);
205 static int parse_password(const char *filename, char **wall) {
206 char *socket_name = NULL, *message = NULL, *packet = NULL;
207 uint64_t not_after = 0;
211 const ConfigItem items[] = {
212 { "Socket", config_parse_string, &socket_name, "Ask" },
213 { "NotAfter", config_parse_uint64, ¬_after, "Ask" },
214 { "Message", config_parse_string, &message, "Ask" },
215 { "PID", config_parse_unsigned, &pid, "Ask" },
224 if (!(f = fopen(filename, "re"))) {
229 log_error("open(%s): %m", filename);
233 if ((r = config_parse(filename, f, NULL, items, false, NULL)) < 0) {
234 log_error("Failed to parse password file %s: %s", filename, strerror(-r));
238 if (!socket_name || not_after <= 0) {
239 log_error("Invalid password file %s", filename);
244 n = now(CLOCK_MONOTONIC);
250 if (arg_action == ACTION_LIST)
251 printf("'%s' (PID %u)\n", message, pid);
252 else if (arg_action == ACTION_WALL) {
256 "%s%sPassword entry required for \'%s\' (PID %u).\r\n"
257 "Please enter password with the systemd-tty-password-agent tool!",
259 *wall ? "\r\n\r\n" : "",
262 log_error("Out of memory");
272 struct sockaddr_un un;
276 assert(arg_action == ACTION_QUERY ||
277 arg_action == ACTION_WATCH);
279 if (access(socket_name, W_OK) < 0) {
281 if (arg_action == ACTION_QUERY)
282 log_info("Not querying '%s' (PID %u), lacking privileges.", message, pid);
289 r = ask_password_plymouth(message, not_after, filename, &password);
291 r = ask_password_tty(message, not_after, filename, &password);
294 log_error("Failed to query password: %s", strerror(-r));
298 asprintf(&packet, "+%s", password);
302 log_error("Out of memory");
307 if ((socket_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
308 log_error("socket(): %m");
314 sa.un.sun_family = AF_UNIX;
315 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
317 if (sendto(socket_fd, packet, strlen(packet), MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0) {
318 log_error("Failed to send: %m");
328 close_nointr_nofail(socket_fd);
337 static int show_passwords(void) {
342 if (!(d = opendir("/dev/.systemd/ask-password"))) {
346 log_error("opendir(): %m");
350 while ((de = readdir(d))) {
355 if (de->d_type != DT_REG)
358 if (ignore_file(de->d_name))
361 if (!startswith(de->d_name, "ask."))
364 if (!(p = strappend("/dev/.systemd/ask-password/", de->d_name))) {
365 log_error("Out of memory");
371 if ((q = parse_password(p, &wall)) < 0)
389 static int watch_passwords(void) {
396 int notify = -1, signal_fd = -1;
397 struct pollfd pollfd[_FD_MAX];
401 mkdir_p("/dev/.systemd/ask-password", 0755);
403 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
408 if (inotify_add_watch(notify, "/dev/.systemd/ask-password", IN_CLOSE_WRITE|IN_MOVED_TO) < 0) {
413 assert_se(sigemptyset(&mask) == 0);
414 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
415 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
417 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
418 log_error("signalfd(): %m");
424 pollfd[FD_INOTIFY].fd = notify;
425 pollfd[FD_INOTIFY].events = POLLIN;
426 pollfd[FD_SIGNAL].fd = signal_fd;
427 pollfd[FD_SIGNAL].events = POLLIN;
430 if ((r = show_passwords()) < 0)
433 if (poll(pollfd, _FD_MAX, -1) < 0) {
442 if (pollfd[FD_INOTIFY].revents != 0)
445 if (pollfd[FD_SIGNAL].revents != 0)
453 close_nointr_nofail(notify);
456 close_nointr_nofail(signal_fd);
461 static int help(void) {
463 printf("%s [OPTIONS...]\n\n"
464 "Process system password requests.\n\n"
465 " -h --help Show this help\n"
466 " --list Show pending password requests\n"
467 " --query Process pending password requests\n"
468 " --watch Continously process password requests\n"
469 " --wall Continously forward password requests to wall\n"
470 " --plymouth Ask question with Plymouth instead of on TTY\n",
471 program_invocation_short_name);
476 static int parse_argv(int argc, char *argv[]) {
486 static const struct option options[] = {
487 { "help", no_argument, NULL, 'h' },
488 { "list", no_argument, NULL, ARG_LIST },
489 { "query", no_argument, NULL, ARG_QUERY },
490 { "watch", no_argument, NULL, ARG_WATCH },
491 { "wall", no_argument, NULL, ARG_WALL },
492 { "plymouth", no_argument, NULL, ARG_PLYMOUTH },
501 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
510 arg_action = ACTION_LIST;
514 arg_action = ACTION_QUERY;
518 arg_action = ACTION_WATCH;
522 arg_action = ACTION_WALL;
533 log_error("Unknown option code %c", c);
538 if (optind != argc) {
546 int main(int argc, char *argv[]) {
549 log_parse_environment();
552 if ((r = parse_argv(argc, argv)) <= 0)
555 if (arg_action == ACTION_WATCH ||
556 arg_action == ACTION_WALL)
557 r = watch_passwords();
559 r = show_passwords();
562 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;