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"
42 } arg_action = ACTION_QUERY;
44 static int parse_password(const char *filename) {
45 char *socket_name = NULL, *message = NULL, *packet = NULL;
46 uint64_t not_after = 0;
50 const ConfigItem items[] = {
51 { "Socket", config_parse_string, &socket_name, "Ask" },
52 { "NotAfter", config_parse_uint64, ¬_after, "Ask" },
53 { "Message", config_parse_string, &message, "Ask" },
54 { "PID", config_parse_unsigned, &pid, "Ask" },
63 if (!(f = fopen(filename, "re"))) {
68 log_error("open(%s): %m", filename);
72 if ((r = config_parse(filename, f, NULL, items, false, NULL)) < 0) {
73 log_error("Failed to parse password file %s: %s", filename, strerror(-r));
77 if (!socket_name || not_after <= 0) {
78 log_error("Invalid password file %s", filename);
83 n = now(CLOCK_MONOTONIC);
89 if (arg_action == ACTION_LIST)
90 printf("'%s' (PID %u)\n", message, pid);
91 else if (arg_action == ACTION_WALL) {
95 "Password entry required for \'%s\' (PID %u).\r\n"
96 "Please enter password with the systemd-tty-password-agent tool!",
99 log_error("Out of memory");
109 struct sockaddr_un un;
113 assert(arg_action == ACTION_QUERY ||
114 arg_action == ACTION_WATCH);
116 if (access(socket_name, W_OK) < 0) {
118 if (arg_action == ACTION_QUERY)
119 log_info("Not querying '%s' (PID %u), lacking privileges.", message, pid);
125 if ((r = ask_password_tty(message, not_after, filename, &password)) < 0) {
126 log_error("Failed to query passwords: %s", strerror(-r));
130 asprintf(&packet, "+%s", password);
134 log_error("Out of memory");
139 if ((socket_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
140 log_error("socket(): %m");
146 sa.un.sun_family = AF_UNIX;
147 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
149 if (sendto(socket_fd, packet, strlen(packet), MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0) {
150 log_error("Failed to send: %m");
160 close_nointr_nofail(socket_fd);
169 static int show_passwords(void) {
174 if (!(d = opendir("/dev/.systemd/ask-password"))) {
178 log_error("opendir(): %m");
182 while ((de = readdir(d))) {
186 if (de->d_type != DT_REG)
189 if (ignore_file(de->d_name))
192 if (!startswith(de->d_name, "ask."))
195 if (!(p = strappend("/dev/.systemd/ask-password/", de->d_name))) {
196 log_error("Out of memory");
201 if ((q = parse_password(p)) < 0)
214 static int watch_passwords(void) {
216 struct pollfd pollfd;
219 mkdir_p("/dev/.systemd/ask-password", 0755);
221 if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
226 if (inotify_add_watch(notify, "/dev/.systemd/ask-password", IN_CLOSE_WRITE|IN_MOVED_TO) < 0) {
233 pollfd.events = POLLIN;
236 if ((r = show_passwords()) < 0)
239 if (poll(&pollfd, 1, -1) < 0) {
248 if (pollfd.revents != 0)
256 close_nointr_nofail(notify);
261 static int help(void) {
263 printf("%s [OPTIONS...]\n\n"
264 "Process system password requests.\n\n"
265 " -h --help Show this help\n"
266 " --list Show pending password requests\n"
267 " --query Process pending password requests\n"
268 " --watch Continously process password requests\n"
269 " --wall Continously forward password requests to wall\n",
270 program_invocation_short_name);
275 static int parse_argv(int argc, char *argv[]) {
284 static const struct option options[] = {
285 { "help", no_argument, NULL, 'h' },
286 { "list", no_argument, NULL, ARG_LIST },
287 { "query", no_argument, NULL, ARG_QUERY },
288 { "watch", no_argument, NULL, ARG_WATCH },
289 { "wall", no_argument, NULL, ARG_WALL },
298 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
307 arg_action = ACTION_LIST;
311 arg_action = ACTION_QUERY;
315 arg_action = ACTION_WATCH;
319 arg_action = ACTION_WALL;
326 log_error("Unknown option code %c", c);
331 if (optind != argc) {
339 int main(int argc, char *argv[]) {
342 log_parse_environment();
345 if ((r = parse_argv(argc, argv)) <= 0)
348 if (arg_action == ACTION_WATCH ||
349 arg_action == ACTION_WALL)
350 r = watch_passwords();
352 r = show_passwords();
355 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;