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>
41 static const char *arg_icon = NULL;
42 static const char *arg_message = NULL;
43 static bool arg_use_tty = true;
44 static usec_t arg_timeout = 60 * USEC_PER_SEC;
46 static int create_socket(char **name) {
50 struct sockaddr_un un;
57 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
58 log_error("socket() failed: %m");
63 sa.un.sun_family = AF_UNIX;
64 snprintf(sa.un.sun_path+1, sizeof(sa.un.sun_path)-1, "/org/freedesktop/systemd1/ask-password/%llu", random_ull());
66 if (bind(fd, &sa.sa, sizeof(sa_family_t) + 1 + strlen(sa.un.sun_path+1)) < 0) {
68 log_error("bind() failed: %m");
72 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
74 log_error("SO_PASSCRED failed: %m");
78 if (!(c = strdup(sa.un.sun_path+1))) {
80 log_error("Out of memory");
88 close_nointr_nofail(fd);
93 static int help(void) {
95 printf("%s [OPTIONS...] MESSAGE\n\n"
96 "Query the user for a system passphrase, via the TTY or an UI agent.\n\n"
97 " -h --help Show this help\n"
98 " --icon=NAME Icon name\n"
99 " --timeout=USEC Timeout in usec\n"
100 " --no-tty Ask question via agent even on TTY\n",
101 program_invocation_short_name);
106 static int parse_argv(int argc, char *argv[]) {
114 static const struct option options[] = {
115 { "help", no_argument, NULL, 'h' },
116 { "icon", required_argument, NULL, ARG_ICON },
117 { "timeout", required_argument, NULL, ARG_TIMEOUT },
118 { "no-tty", no_argument, NULL, ARG_NO_TTY },
127 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
140 if (parse_usec(optarg, &arg_timeout) < 0 || arg_timeout <= 0) {
141 log_error("Failed to parse --timeout parameter %s", optarg);
154 log_error("Unknown option code %c", c);
159 if (optind != argc-1) {
164 arg_message = argv[optind];
168 static int ask_agent(void) {
169 char temp[] = "/dev/.systemd/ask-password/tmp.XXXXXX";
170 char final[sizeof(temp)] = "";
173 char *socket_name = NULL;
174 int socket_fd = -1, signal_fd;
178 if ((fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY)) < 0) {
179 log_error("Failed to create password file: %m");
186 if (!(f = fdopen(fd, "w"))) {
187 log_error("Failed to allocate FILE: %m");
194 assert_se(sigemptyset(&mask) == 0);
195 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
196 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
198 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
199 log_error("signalfd(): %m");
204 if ((socket_fd = create_socket(&socket_name)) < 0) {
209 not_after = now(CLOCK_MONOTONIC) + arg_timeout;
216 (unsigned long long) not_after);
219 fprintf(f, "Message=%s\n", arg_message);
222 fprintf(f, "Icon=%s\n", arg_icon);
227 log_error("Failed to write query file: %m");
232 memcpy(final, temp, sizeof(temp));
234 final[sizeof(final)-11] = 'a';
235 final[sizeof(final)-10] = 's';
236 final[sizeof(final)-9] = 'k';
238 if (rename(temp, final) < 0) {
239 log_error("Failed to rename query file: %m");
251 char passphrase[LINE_MAX+1];
252 struct msghdr msghdr;
256 struct cmsghdr cmsghdr;
257 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
260 struct pollfd pollfd[_FD_MAX];
264 pollfd[FD_SOCKET].fd = socket_fd;
265 pollfd[FD_SOCKET].events = POLLIN;
266 pollfd[FD_SIGNAL].fd = signal_fd;
267 pollfd[FD_SIGNAL].events = POLLIN;
269 if ((k = poll(pollfd, 2, arg_timeout/USEC_PER_MSEC)) < 0) {
274 log_error("poll() failed: %m");
280 log_notice("Timed out");
285 if (pollfd[FD_SIGNAL].revents & POLLIN)
288 if (pollfd[FD_SOCKET].revents != POLLIN) {
289 log_error("Unexpected poll() event.");
295 iovec.iov_base = passphrase;
296 iovec.iov_len = sizeof(passphrase)-1;
300 msghdr.msg_iov = &iovec;
301 msghdr.msg_iovlen = 1;
302 msghdr.msg_control = &control;
303 msghdr.msg_controllen = sizeof(control);
305 if ((n = recvmsg(socket_fd, &msghdr, 0)) < 0) {
307 if (errno == EAGAIN ||
311 log_error("recvmsg() failed: %m");
317 log_error("Message too short");
321 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
322 control.cmsghdr.cmsg_level != SOL_SOCKET ||
323 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
324 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
325 log_warning("Received message without credentials. Ignoring.");
329 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
330 if (ucred->uid != 0) {
331 log_warning("Got request from unprivileged user. Ignoring.");
335 if (passphrase[0] == '+') {
337 fputs(passphrase+1, stdout);
339 } else if (passphrase[0] == '-') {
343 log_error("Invalid packet");
354 close_nointr_nofail(fd);
357 close_nointr_nofail(socket_fd);
370 static int ask_tty(void) {
371 struct termios old_termios, new_termios;
372 char passphrase[LINE_MAX];
375 if (!(ttyf = fopen("/dev/tty", "w"))) {
376 log_error("Failed to open /dev/tty: %m");
380 fputs("\x1B[1m", ttyf);
381 fprintf(ttyf, "%s: ", arg_message);
382 fputs("\x1B[0m", ttyf);
385 if (tcgetattr(STDIN_FILENO, &old_termios) >= 0) {
387 new_termios = old_termios;
389 new_termios.c_lflag &= ~(ICANON|ECHO);
390 new_termios.c_cc[VMIN] = 1;
391 new_termios.c_cc[VTIME] = 0;
393 if (tcsetattr(STDIN_FILENO, TCSADRAIN, &new_termios) >= 0) {
401 k = fread(&c, 1, 1, stdin);
410 else if (c == '\b' || c == 127) {
413 fputs("\b \b", ttyf);
425 tcsetattr(STDIN_FILENO, TCSADRAIN, &old_termios);
432 fputs(passphrase, stdout);
441 if (!fgets(passphrase, sizeof(passphrase), stdin)) {
442 log_error("Failed to read password.");
446 truncate_nl(passphrase);
447 fputs(passphrase, stdout);
453 int main(int argc, char *argv[]) {
456 log_parse_environment();
459 if ((r = parse_argv(argc, argv)) <= 0)
462 if (arg_use_tty && isatty(STDIN_FILENO))
468 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;