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>
34 #include "conf-parser.h"
35 #include "utmp-wtmp.h"
36 #include "socket-util.h"
43 } arg_action = ACTION_QUERY;
45 static bool arg_plymouth = false;
47 static int ask_password_plymouth(const char *message, usec_t until, const char *flag_file, char **_passphrase) {
48 int fd = -1, notify = -1;
49 union sockaddr_union sa;
53 struct pollfd pollfd[2];
54 char buffer[LINE_MAX];
62 if ((notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
67 if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
73 if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
79 sa.sa.sa_family = AF_UNIX;
80 strncpy(sa.un.sun_path+1, "/ply-boot-protocol", sizeof(sa.un.sun_path)-1);
82 if (connect(fd, &sa.sa, sizeof(sa.un)) < 0) {
87 if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) {
92 if ((k = loop_write(fd, packet, n+1, true)) != n+1) {
93 r = k < 0 ? (int) k : -EIO;
98 pollfd[POLL_SOCKET].fd = fd;
99 pollfd[POLL_SOCKET].events = POLLIN;
100 pollfd[POLL_INOTIFY].fd = notify;
101 pollfd[POLL_INOTIFY].events = POLLIN;
104 int sleep_for = -1, j;
109 y = now(CLOCK_MONOTONIC);
116 sleep_for = (int) ((until - y) / USEC_PER_MSEC);
120 if (access(flag_file, F_OK) < 0) {
125 if ((j = poll(pollfd, notify > 0 ? 2 : 1, sleep_for)) < 0) {
137 if (notify > 0 && pollfd[POLL_INOTIFY].revents != 0)
140 if (pollfd[POLL_SOCKET].revents == 0)
143 if ((k = read(fd, buffer + p, sizeof(buffer) - p)) <= 0) {
144 r = k < 0 ? -errno : -EIO;
153 if (buffer[0] == 5) {
154 /* No password, because UI not shown */
158 } else if (buffer[0] == 2) {
166 memcpy(&size, buffer+1, sizeof(size));
167 if (size+5 > sizeof(buffer)) {
175 if (!(s = strndup(buffer + 5, size))) {
193 close_nointr_nofail(notify);
196 close_nointr_nofail(fd);
203 static int parse_password(const char *filename, char **wall) {
204 char *socket_name = NULL, *message = NULL, *packet = NULL;
205 uint64_t not_after = 0;
209 const ConfigItem items[] = {
210 { "Socket", config_parse_string, &socket_name, "Ask" },
211 { "NotAfter", config_parse_uint64, ¬_after, "Ask" },
212 { "Message", config_parse_string, &message, "Ask" },
213 { "PID", config_parse_unsigned, &pid, "Ask" },
222 if (!(f = fopen(filename, "re"))) {
227 log_error("open(%s): %m", filename);
231 if ((r = config_parse(filename, f, NULL, items, false, NULL)) < 0) {
232 log_error("Failed to parse password file %s: %s", filename, strerror(-r));
236 if (!socket_name || not_after <= 0) {
237 log_error("Invalid password file %s", filename);
242 n = now(CLOCK_MONOTONIC);
248 if (arg_action == ACTION_LIST)
249 printf("'%s' (PID %u)\n", message, pid);
250 else if (arg_action == ACTION_WALL) {
254 "%s%sPassword entry required for \'%s\' (PID %u).\r\n"
255 "Please enter password with the systemd-tty-password-agent tool!",
257 *wall ? "\r\n\r\n" : "",
260 log_error("Out of memory");
270 struct sockaddr_un un;
274 assert(arg_action == ACTION_QUERY ||
275 arg_action == ACTION_WATCH);
277 if (access(socket_name, W_OK) < 0) {
279 if (arg_action == ACTION_QUERY)
280 log_info("Not querying '%s' (PID %u), lacking privileges.", message, pid);
287 r = ask_password_plymouth(message, not_after, filename, &password);
289 r = ask_password_tty(message, not_after, filename, &password);
292 log_error("Failed to query password: %s", strerror(-r));
296 asprintf(&packet, "+%s", password);
300 log_error("Out of memory");
305 if ((socket_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
306 log_error("socket(): %m");
312 sa.un.sun_family = AF_UNIX;
313 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
315 if (sendto(socket_fd, packet, strlen(packet), MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0) {
316 log_error("Failed to send: %m");
326 close_nointr_nofail(socket_fd);
335 static int show_passwords(void) {
340 if (!(d = opendir("/dev/.systemd/ask-password"))) {
344 log_error("opendir(): %m");
348 while ((de = readdir(d))) {
353 if (de->d_type != DT_REG)
356 if (ignore_file(de->d_name))
359 if (!startswith(de->d_name, "ask."))
362 if (!(p = strappend("/dev/.systemd/ask-password/", de->d_name))) {
363 log_error("Out of memory");
369 if ((q = parse_password(p, &wall)) < 0)
387 static int watch_passwords(void) {
389 struct pollfd pollfd;
392 mkdir_p("/dev/.systemd/ask-password", 0755);
394 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
399 if (inotify_add_watch(notify, "/dev/.systemd/ask-password", IN_CLOSE_WRITE|IN_MOVED_TO) < 0) {
406 pollfd.events = POLLIN;
409 if ((r = show_passwords()) < 0)
412 if (poll(&pollfd, 1, -1) < 0) {
421 if (pollfd.revents != 0)
429 close_nointr_nofail(notify);
434 static int help(void) {
436 printf("%s [OPTIONS...]\n\n"
437 "Process system password requests.\n\n"
438 " -h --help Show this help\n"
439 " --list Show pending password requests\n"
440 " --query Process pending password requests\n"
441 " --watch Continously process password requests\n"
442 " --wall Continously forward password requests to wall\n"
443 " --plymouth Ask question with Plymouth instead of on TTY\n",
444 program_invocation_short_name);
449 static int parse_argv(int argc, char *argv[]) {
459 static const struct option options[] = {
460 { "help", no_argument, NULL, 'h' },
461 { "list", no_argument, NULL, ARG_LIST },
462 { "query", no_argument, NULL, ARG_QUERY },
463 { "watch", no_argument, NULL, ARG_WATCH },
464 { "wall", no_argument, NULL, ARG_WALL },
465 { "plymouth", no_argument, NULL, ARG_PLYMOUTH },
474 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
483 arg_action = ACTION_LIST;
487 arg_action = ACTION_QUERY;
491 arg_action = ACTION_WATCH;
495 arg_action = ACTION_WALL;
506 log_error("Unknown option code %c", c);
511 if (optind != argc) {
519 int main(int argc, char *argv[]) {
522 log_parse_environment();
525 if ((r = parse_argv(argc, argv)) <= 0)
528 if (arg_action == ACTION_WATCH ||
529 arg_action == ACTION_WALL)
530 r = watch_passwords();
532 r = show_passwords();
535 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;