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/inotify.h>
28 #include <sys/socket.h>
32 #include <sys/signalfd.h>
37 #include "ask-password-api.h"
39 static void backspace_chars(int ttyfd, size_t p) {
47 loop_write(ttyfd, "\b \b", 3, false);
54 const char *flag_file,
57 struct termios old_termios, new_termios;
58 char passphrase[LINE_MAX];
60 int r, ttyfd = -1, notify = -1;
61 struct pollfd pollfd[2];
62 bool reset_tty = false;
63 bool silent_mode = false;
74 if ((notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
79 if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
85 if ((ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC)) >= 0) {
87 if (tcgetattr(ttyfd, &old_termios) < 0) {
92 loop_write(ttyfd, "\x1B[1m", 4, false);
93 loop_write(ttyfd, message, strlen(message), false);
94 loop_write(ttyfd, " ", 1, false);
95 loop_write(ttyfd, "\x1B[0m", 4, false);
97 new_termios = old_termios;
98 new_termios.c_lflag &= ~(ICANON|ECHO);
99 new_termios.c_cc[VMIN] = 1;
100 new_termios.c_cc[VTIME] = 0;
102 if (tcsetattr(ttyfd, TCSADRAIN, &new_termios) < 0) {
112 pollfd[POLL_TTY].fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO;
113 pollfd[POLL_TTY].events = POLLIN;
114 pollfd[POLL_INOTIFY].fd = notify;
115 pollfd[POLL_INOTIFY].events = POLLIN;
119 int sleep_for = -1, k;
125 y = now(CLOCK_MONOTONIC);
132 sleep_for = (int) ((until - y) / USEC_PER_MSEC);
136 if (access(flag_file, F_OK) < 0) {
141 if ((k = poll(pollfd, notify > 0 ? 2 : 1, sleep_for)) < 0) {
153 if (notify > 0 && pollfd[POLL_INOTIFY].revents != 0)
156 if (pollfd[POLL_TTY].revents == 0)
159 if ((n = read(ttyfd >= 0 ? ttyfd : STDIN_FILENO, &c, 1)) < 0) {
161 if (errno == EINTR || errno == EAGAIN)
172 else if (c == 21) { /* C-u */
175 backspace_chars(ttyfd, p);
178 } else if (c == '\b' || c == 127) {
183 backspace_chars(ttyfd, 1);
186 } else if (!dirty && !silent_mode) {
190 /* There are two ways to enter silent
191 * mode. Either by pressing backspace
192 * as first key (and only as first key),
195 loop_write(ttyfd, "(no echo) ", 10, false);
197 } else if (ttyfd >= 0)
198 loop_write(ttyfd, "\a", 1, false);
200 } else if (c == '\t' && !silent_mode) {
202 backspace_chars(ttyfd, p);
205 /* ... or by pressing TAB at any time. */
208 loop_write(ttyfd, "(no echo) ", 10, false);
212 if (!silent_mode && ttyfd >= 0)
213 loop_write(ttyfd, "*", 1, false);
221 if (!(*_passphrase = strdup(passphrase))) {
230 close_nointr_nofail(notify);
235 loop_write(ttyfd, "\n", 1, false);
236 tcsetattr(ttyfd, TCSADRAIN, &old_termios);
239 close_nointr_nofail(ttyfd);
245 static int create_socket(char **name) {
249 struct sockaddr_un un;
256 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
257 log_error("socket() failed: %m");
262 sa.un.sun_family = AF_UNIX;
263 snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%llu", random_ull());
265 if (bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
267 log_error("bind() failed: %m");
271 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
273 log_error("SO_PASSCRED failed: %m");
277 if (!(c = strdup(sa.un.sun_path))) {
279 log_error("Out of memory");
287 close_nointr_nofail(fd);
292 int ask_password_agent(
297 char ***_passphrases) {
305 char temp[] = "/run/systemd/ask-password/tmp.XXXXXX";
306 char final[sizeof(temp)] = "";
309 char *socket_name = NULL;
310 int socket_fd = -1, signal_fd = -1;
311 sigset_t mask, oldmask;
312 struct pollfd pollfd[_FD_MAX];
314 assert(_passphrases);
316 assert_se(sigemptyset(&mask) == 0);
317 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
318 assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
320 mkdir_p("/run/systemd/ask-password", 0755);
322 if ((fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY)) < 0) {
323 log_error("Failed to create password file: %m");
330 if (!(f = fdopen(fd, "w"))) {
331 log_error("Failed to allocate FILE: %m");
338 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
339 log_error("signalfd(): %m");
344 if ((socket_fd = create_socket(&socket_name)) < 0) {
355 (unsigned long) getpid(),
357 accept_cached ? 1 : 0,
358 (unsigned long long) until);
361 fprintf(f, "Message=%s\n", message);
364 fprintf(f, "Icon=%s\n", icon);
369 log_error("Failed to write query file: %m");
374 memcpy(final, temp, sizeof(temp));
376 final[sizeof(final)-11] = 'a';
377 final[sizeof(final)-10] = 's';
378 final[sizeof(final)-9] = 'k';
380 if (rename(temp, final) < 0) {
381 log_error("Failed to rename query file: %m");
387 pollfd[FD_SOCKET].fd = socket_fd;
388 pollfd[FD_SOCKET].events = POLLIN;
389 pollfd[FD_SIGNAL].fd = signal_fd;
390 pollfd[FD_SIGNAL].events = POLLIN;
393 char passphrase[LINE_MAX+1];
394 struct msghdr msghdr;
398 struct cmsghdr cmsghdr;
399 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
405 t = now(CLOCK_MONOTONIC);
407 if (until > 0 && until <= t) {
408 log_notice("Timed out");
413 if ((k = poll(pollfd, _FD_MAX, until > 0 ? (int) ((until-t)/USEC_PER_MSEC) : -1)) < 0) {
418 log_error("poll() failed: %m");
424 log_notice("Timed out");
429 if (pollfd[FD_SIGNAL].revents & POLLIN) {
434 if (pollfd[FD_SOCKET].revents != POLLIN) {
435 log_error("Unexpected poll() event.");
441 iovec.iov_base = passphrase;
442 iovec.iov_len = sizeof(passphrase);
446 msghdr.msg_iov = &iovec;
447 msghdr.msg_iovlen = 1;
448 msghdr.msg_control = &control;
449 msghdr.msg_controllen = sizeof(control);
451 if ((n = recvmsg(socket_fd, &msghdr, 0)) < 0) {
453 if (errno == EAGAIN ||
457 log_error("recvmsg() failed: %m");
463 log_error("Message too short");
467 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
468 control.cmsghdr.cmsg_level != SOL_SOCKET ||
469 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
470 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
471 log_warning("Received message without credentials. Ignoring.");
475 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
476 if (ucred->uid != 0) {
477 log_warning("Got request from unprivileged user. Ignoring.");
481 if (passphrase[0] == '+') {
485 l = strv_new("", NULL);
487 l = strv_parse_nulstr(passphrase+1, n-1);
488 /* An empty message refers to the empty password */
495 if (strv_length(l) <= 0) {
497 log_error("Invalid packet");
503 } else if (passphrase[0] == '-') {
507 log_error("Invalid packet");
518 close_nointr_nofail(fd);
526 close_nointr_nofail(socket_fd);
529 close_nointr_nofail(signal_fd);
539 assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0);
544 int ask_password_auto(const char *message, const char *icon, usec_t until, bool accept_cached, char ***_passphrases) {
546 assert(_passphrases);
548 if (isatty(STDIN_FILENO)) {
550 char *s = NULL, **l = NULL;
552 if ((r = ask_password_tty(message, until, NULL, &s)) < 0)
555 l = strv_new(s, NULL);
565 return ask_password_agent(message, icon, until, accept_cached, _passphrases);