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/>.
22 #include <sys/socket.h>
24 #include <sys/types.h>
32 #include <sys/signalfd.h>
42 static const char *arg_icon = NULL;
43 static const char *arg_message = NULL;
44 static bool arg_use_tty = true;
45 static usec_t arg_timeout = 60 * USEC_PER_SEC;
47 static int create_socket(char **name) {
51 struct sockaddr_un un;
58 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
59 log_error("socket() failed: %m");
64 sa.un.sun_family = AF_UNIX;
65 snprintf(sa.un.sun_path+1, sizeof(sa.un.sun_path)-1, "/org/freedesktop/systemd1/ask-password/%llu", random_ull());
67 if (bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
69 log_error("bind() failed: %m");
73 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
75 log_error("SO_PASSCRED failed: %m");
79 if (!(c = strdup(sa.un.sun_path+1))) {
81 log_error("Out of memory");
89 close_nointr_nofail(fd);
94 static int help(void) {
96 printf("%s [OPTIONS...] MESSAGE\n\n"
97 "Query the user for a system passphrase, via the TTY or an UI agent.\n\n"
98 " -h --help Show this help\n"
99 " --icon=NAME Icon name\n"
100 " --timeout=USEC Timeout in usec\n"
101 " --no-tty Ask question via agent even on TTY\n",
102 program_invocation_short_name);
107 static int parse_argv(int argc, char *argv[]) {
115 static const struct option options[] = {
116 { "help", no_argument, NULL, 'h' },
117 { "icon", required_argument, NULL, ARG_ICON },
118 { "timeout", required_argument, NULL, ARG_TIMEOUT },
119 { "no-tty", no_argument, NULL, ARG_NO_TTY },
128 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
141 if (parse_usec(optarg, &arg_timeout) < 0 || arg_timeout <= 0) {
142 log_error("Failed to parse --timeout parameter %s", optarg);
155 log_error("Unknown option code %c", c);
160 if (optind != argc-1) {
165 arg_message = argv[optind];
169 static int ask_agent(void) {
170 char temp[] = "/dev/.systemd/ask-password/tmp.XXXXXX";
171 char final[sizeof(temp)] = "";
174 char *socket_name = NULL;
175 int socket_fd = -1, signal_fd;
179 if ((fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY)) < 0) {
180 log_error("Failed to create password file: %m");
187 if (!(f = fdopen(fd, "w"))) {
188 log_error("Failed to allocate FILE: %m");
195 assert_se(sigemptyset(&mask) == 0);
196 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
197 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
199 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
200 log_error("signalfd(): %m");
205 if ((socket_fd = create_socket(&socket_name)) < 0) {
210 not_after = now(CLOCK_MONOTONIC) + arg_timeout;
217 (unsigned long long) not_after);
220 fprintf(f, "Message=%s\n", arg_message);
223 fprintf(f, "Icon=%s\n", arg_icon);
228 log_error("Failed to write query file: %m");
233 memcpy(final, temp, sizeof(temp));
235 final[sizeof(final)-11] = 'a';
236 final[sizeof(final)-10] = 's';
237 final[sizeof(final)-9] = 'k';
239 if (rename(temp, final) < 0) {
240 log_error("Failed to rename query file: %m");
252 char passphrase[LINE_MAX+1];
253 struct msghdr msghdr;
257 struct cmsghdr cmsghdr;
258 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
261 struct pollfd pollfd[_FD_MAX];
265 pollfd[FD_SOCKET].fd = socket_fd;
266 pollfd[FD_SOCKET].events = POLLIN;
267 pollfd[FD_SIGNAL].fd = signal_fd;
268 pollfd[FD_SIGNAL].events = POLLIN;
270 if ((k = poll(pollfd, 2, arg_timeout/USEC_PER_MSEC)) < 0) {
275 log_error("poll() failed: %m");
281 log_notice("Timed out");
286 if (pollfd[FD_SIGNAL].revents & POLLIN)
289 if (pollfd[FD_SOCKET].revents != POLLIN) {
290 log_error("Unexpected poll() event.");
296 iovec.iov_base = passphrase;
297 iovec.iov_len = sizeof(passphrase)-1;
301 msghdr.msg_iov = &iovec;
302 msghdr.msg_iovlen = 1;
303 msghdr.msg_control = &control;
304 msghdr.msg_controllen = sizeof(control);
306 if ((n = recvmsg(socket_fd, &msghdr, 0)) < 0) {
308 if (errno == EAGAIN ||
312 log_error("recvmsg() failed: %m");
318 log_error("Message too short");
322 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
323 control.cmsghdr.cmsg_level != SOL_SOCKET ||
324 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
325 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
326 log_warning("Received message without credentials. Ignoring.");
330 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
331 if (ucred->uid != 0) {
332 log_warning("Got request from unprivileged user. Ignoring.");
336 if (passphrase[0] == '+') {
338 fputs(passphrase+1, stdout);
340 } else if (passphrase[0] == '-') {
344 log_error("Invalid packet");
355 close_nointr_nofail(fd);
358 close_nointr_nofail(socket_fd);
371 static int ask_tty(void) {
372 struct termios old_termios, new_termios;
373 char passphrase[LINE_MAX];
376 if (!(ttyf = fopen("/dev/tty", "w"))) {
377 log_error("Failed to open /dev/tty: %m");
381 fputs("\x1B[1m", ttyf);
382 fprintf(ttyf, "%s: ", arg_message);
383 fputs("\x1B[0m", ttyf);
386 if (tcgetattr(STDIN_FILENO, &old_termios) >= 0) {
388 new_termios = old_termios;
390 new_termios.c_lflag &= ~(ICANON|ECHO);
391 new_termios.c_cc[VMIN] = 1;
392 new_termios.c_cc[VTIME] = 0;
394 if (tcsetattr(STDIN_FILENO, TCSADRAIN, &new_termios) >= 0) {
402 k = fread(&c, 1, 1, stdin);
411 else if (c == '\b' || c == 127) {
414 fputs("\b \b", ttyf);
426 tcsetattr(STDIN_FILENO, TCSADRAIN, &old_termios);
433 fputs(passphrase, stdout);
442 if (!fgets(passphrase, sizeof(passphrase), stdin)) {
443 log_error("Failed to read password.");
447 truncate_nl(passphrase);
448 fputs(passphrase, stdout);
454 int main(int argc, char *argv[]) {
457 log_parse_environment();
460 if ((r = parse_argv(argc, argv)) <= 0)
463 if (arg_use_tty && isatty(STDIN_FILENO))
469 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;