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"
42 const char *flag_file,
45 struct termios old_termios, new_termios;
46 char passphrase[LINE_MAX];
48 int r, ttyfd = -1, notify = -1;
49 struct pollfd pollfd[2];
50 bool reset_tty = false;
51 bool silent_mode = false;
61 if ((notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
66 if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
72 if ((ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC)) >= 0) {
74 if (tcgetattr(ttyfd, &old_termios) < 0) {
79 loop_write(ttyfd, "\x1B[1m", 4, false);
80 loop_write(ttyfd, message, strlen(message), false);
81 loop_write(ttyfd, " ", 1, false);
82 loop_write(ttyfd, "\x1B[0m", 4, false);
84 new_termios = old_termios;
85 new_termios.c_lflag &= ~(ICANON|ECHO);
86 new_termios.c_cc[VMIN] = 1;
87 new_termios.c_cc[VTIME] = 0;
89 if (tcsetattr(ttyfd, TCSADRAIN, &new_termios) < 0) {
99 pollfd[POLL_TTY].fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO;
100 pollfd[POLL_TTY].events = POLLIN;
101 pollfd[POLL_INOTIFY].fd = notify;
102 pollfd[POLL_INOTIFY].events = POLLIN;
106 int sleep_for = -1, k;
112 y = now(CLOCK_MONOTONIC);
119 sleep_for = (int) ((until - y) / USEC_PER_MSEC);
123 if (access(flag_file, F_OK) < 0) {
128 if ((k = poll(pollfd, notify > 0 ? 2 : 1, sleep_for)) < 0) {
140 if (notify > 0 && pollfd[POLL_INOTIFY].revents != 0)
143 if (pollfd[POLL_TTY].revents == 0)
146 if ((n = read(ttyfd >= 0 ? ttyfd : STDIN_FILENO, &c, 1)) < 0) {
148 if (errno == EINTR || errno == EAGAIN)
164 loop_write(ttyfd, "\b \b", 3, false);
167 } else if (c == '\b' || c == 127) {
168 if (p == 0 && !silent_mode) {
170 loop_write(ttyfd, "(no echo) ", 10, false);
175 loop_write(ttyfd, "\b \b", 3, false);
180 if (!silent_mode && ttyfd >= 0)
181 loop_write(ttyfd, "*", 1, false);
187 if (!(*_passphrase = strdup(passphrase))) {
196 close_nointr_nofail(notify);
201 loop_write(ttyfd, "\n", 1, false);
202 tcsetattr(ttyfd, TCSADRAIN, &old_termios);
205 close_nointr_nofail(ttyfd);
211 static int create_socket(char **name) {
215 struct sockaddr_un un;
222 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
223 log_error("socket() failed: %m");
228 sa.un.sun_family = AF_UNIX;
229 snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%llu", random_ull());
231 if (bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
233 log_error("bind() failed: %m");
237 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
239 log_error("SO_PASSCRED failed: %m");
243 if (!(c = strdup(sa.un.sun_path))) {
245 log_error("Out of memory");
253 close_nointr_nofail(fd);
258 int ask_password_agent(
263 char ***_passphrases) {
271 char temp[] = "/run/systemd/ask-password/tmp.XXXXXX";
272 char final[sizeof(temp)] = "";
275 char *socket_name = NULL;
276 int socket_fd = -1, signal_fd = -1;
277 sigset_t mask, oldmask;
278 struct pollfd pollfd[_FD_MAX];
280 assert(_passphrases);
282 assert_se(sigemptyset(&mask) == 0);
283 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
284 assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
286 mkdir_p("/run/systemd/ask-password", 0755);
288 if ((fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY)) < 0) {
289 log_error("Failed to create password file: %m");
296 if (!(f = fdopen(fd, "w"))) {
297 log_error("Failed to allocate FILE: %m");
304 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
305 log_error("signalfd(): %m");
310 if ((socket_fd = create_socket(&socket_name)) < 0) {
321 (unsigned long) getpid(),
323 accept_cached ? 1 : 0,
324 (unsigned long long) until);
327 fprintf(f, "Message=%s\n", message);
330 fprintf(f, "Icon=%s\n", icon);
335 log_error("Failed to write query file: %m");
340 memcpy(final, temp, sizeof(temp));
342 final[sizeof(final)-11] = 'a';
343 final[sizeof(final)-10] = 's';
344 final[sizeof(final)-9] = 'k';
346 if (rename(temp, final) < 0) {
347 log_error("Failed to rename query file: %m");
353 pollfd[FD_SOCKET].fd = socket_fd;
354 pollfd[FD_SOCKET].events = POLLIN;
355 pollfd[FD_SIGNAL].fd = signal_fd;
356 pollfd[FD_SIGNAL].events = POLLIN;
359 char passphrase[LINE_MAX+1];
360 struct msghdr msghdr;
364 struct cmsghdr cmsghdr;
365 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
371 t = now(CLOCK_MONOTONIC);
374 log_notice("Timed out");
379 if ((k = poll(pollfd, _FD_MAX, (until-t)/USEC_PER_MSEC)) < 0) {
384 log_error("poll() failed: %m");
390 log_notice("Timed out");
395 if (pollfd[FD_SIGNAL].revents & POLLIN) {
400 if (pollfd[FD_SOCKET].revents != POLLIN) {
401 log_error("Unexpected poll() event.");
407 iovec.iov_base = passphrase;
408 iovec.iov_len = sizeof(passphrase);
412 msghdr.msg_iov = &iovec;
413 msghdr.msg_iovlen = 1;
414 msghdr.msg_control = &control;
415 msghdr.msg_controllen = sizeof(control);
417 if ((n = recvmsg(socket_fd, &msghdr, 0)) < 0) {
419 if (errno == EAGAIN ||
423 log_error("recvmsg() failed: %m");
429 log_error("Message too short");
433 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
434 control.cmsghdr.cmsg_level != SOL_SOCKET ||
435 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
436 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
437 log_warning("Received message without credentials. Ignoring.");
441 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
442 if (ucred->uid != 0) {
443 log_warning("Got request from unprivileged user. Ignoring.");
447 if (passphrase[0] == '+') {
450 if (!(l = strv_parse_nulstr(passphrase+1, n-1))) {
455 if (strv_length(l) <= 0) {
457 log_error("Invalid packet");
463 } else if (passphrase[0] == '-') {
467 log_error("Invalid packet");
478 close_nointr_nofail(fd);
486 close_nointr_nofail(socket_fd);
489 close_nointr_nofail(signal_fd);
499 assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0);
504 int ask_password_auto(const char *message, const char *icon, usec_t until, bool accept_cached, char ***_passphrases) {
506 assert(_passphrases);
508 if (isatty(STDIN_FILENO)) {
510 char *s = NULL, **l = NULL;
512 if ((r = ask_password_tty(message, until, NULL, &s)) < 0)
515 l = strv_new(s, NULL);
525 return ask_password_agent(message, icon, until, accept_cached, _passphrases);