-static int ask_agent(void) {
- char temp[] = "/dev/.systemd/ask-password/tmp.XXXXXX";
- char final[sizeof(temp)] = "";
- int fd = -1, r;
- FILE *f = NULL;
- char *socket_name = NULL;
- int socket_fd = -1, signal_fd;
- sigset_t mask;
- usec_t not_after;
-
- if ((fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY)) < 0) {
- log_error("Failed to create password file: %m");
- r = -errno;
- goto finish;
- }
-
- fchmod(fd, 0644);
-
- if (!(f = fdopen(fd, "w"))) {
- log_error("Failed to allocate FILE: %m");
- r = -errno;
- goto finish;
- }
-
- fd = -1;
-
- assert_se(sigemptyset(&mask) == 0);
- sigset_add_many(&mask, SIGINT, SIGTERM, -1);
- assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
-
- if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
- log_error("signalfd(): %m");
- r = -errno;
- goto finish;
- }
-
- if ((socket_fd = create_socket(&socket_name)) < 0) {
- r = socket_fd;
- goto finish;
- }
-
- not_after = now(CLOCK_MONOTONIC) + arg_timeout;
-
- fprintf(f,
- "[Ask]\n"
- "Socket=%s\n"
- "NotAfter=%llu\n",
- socket_name,
- (unsigned long long) not_after);
-
- if (arg_message)
- fprintf(f, "Message=%s\n", arg_message);
-
- if (arg_icon)
- fprintf(f, "Icon=%s\n", arg_icon);
-
- fflush(f);
-
- if (ferror(f)) {
- log_error("Failed to write query file: %m");
- r = -errno;
- goto finish;
- }
-
- memcpy(final, temp, sizeof(temp));
-
- final[sizeof(final)-11] = 'a';
- final[sizeof(final)-10] = 's';
- final[sizeof(final)-9] = 'k';
-
- if (rename(temp, final) < 0) {
- log_error("Failed to rename query file: %m");
- r = -errno;
- goto finish;
- }
-
- for (;;) {
- enum {
- FD_SOCKET,
- FD_SIGNAL,
- _FD_MAX
- };
-
- char passphrase[LINE_MAX+1];
- struct msghdr msghdr;
- struct iovec iovec;
- struct ucred *ucred;
- union {
- struct cmsghdr cmsghdr;
- uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
- } control;
- ssize_t n;
- struct pollfd pollfd[_FD_MAX];
- int k;
-
- zero(pollfd);
- pollfd[FD_SOCKET].fd = socket_fd;
- pollfd[FD_SOCKET].events = POLLIN;
- pollfd[FD_SIGNAL].fd = signal_fd;
- pollfd[FD_SIGNAL].events = POLLIN;
-
- if ((k = poll(pollfd, 2, arg_timeout/USEC_PER_MSEC)) < 0) {
-
- if (errno == EINTR)
- continue;
-
- log_error("poll() failed: %m");
- r = -errno;
- goto finish;
- }
-
- if (k <= 0) {
- log_notice("Timed out");
- r = -ETIME;
- goto finish;
- }
-
- if (pollfd[FD_SIGNAL].revents & POLLIN)
- break;
-
- if (pollfd[FD_SOCKET].revents != POLLIN) {
- log_error("Unexpected poll() event.");
- r = -EIO;
- goto finish;
- }
-
- zero(iovec);
- iovec.iov_base = passphrase;
- iovec.iov_len = sizeof(passphrase)-1;
-
- zero(control);
- zero(msghdr);
- msghdr.msg_iov = &iovec;
- msghdr.msg_iovlen = 1;
- msghdr.msg_control = &control;
- msghdr.msg_controllen = sizeof(control);
-
- if ((n = recvmsg(socket_fd, &msghdr, 0)) < 0) {
-
- if (errno == EAGAIN ||
- errno == EINTR)
- continue;
-
- log_error("recvmsg() failed: %m");
- r = -errno;
- goto finish;
- }
-
- if (n <= 0) {
- log_error("Message too short");
- continue;
- }
-
- if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
- control.cmsghdr.cmsg_level != SOL_SOCKET ||
- control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
- control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
- log_warning("Received message without credentials. Ignoring.");
- continue;
- }
-
- ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
- if (ucred->uid != 0) {
- log_warning("Got request from unprivileged user. Ignoring.");
- continue;
- }
-
- if (passphrase[0] == '+') {
- passphrase[n] = 0;
- fputs(passphrase+1, stdout);
- fflush(stdout);
- } else if (passphrase[0] == '-') {
- r = -ECANCELED;
- goto finish;
- } else {
- log_error("Invalid packet");
- continue;
- }
-
- break;
- }
-
- r = 0;
-
-finish:
- if (fd >= 0)
- close_nointr_nofail(fd);
-
- if (socket_fd >= 0)
- close_nointr_nofail(socket_fd);
-
- if (f)
- fclose(f);
-
- unlink(temp);
-
- if (final[0])
- unlink(final);
-
- return r;
-}
-
-static int ask_tty(void) {
- struct termios old_termios, new_termios;
- char passphrase[LINE_MAX];
- FILE *ttyf;
-
- if (!(ttyf = fopen("/dev/tty", "w"))) {
- log_error("Failed to open /dev/tty: %m");
- return -errno;
- }
-
- fputs("\x1B[1m", ttyf);
- fprintf(ttyf, "%s: ", arg_message);
- fputs("\x1B[0m", ttyf);
- fflush(ttyf);
-
- if (tcgetattr(STDIN_FILENO, &old_termios) >= 0) {
-
- new_termios = old_termios;
-
- new_termios.c_lflag &= ~(ICANON|ECHO);
- new_termios.c_cc[VMIN] = 1;
- new_termios.c_cc[VTIME] = 0;
-
- if (tcsetattr(STDIN_FILENO, TCSADRAIN, &new_termios) >= 0) {
- size_t p = 0;
- int r = 0;
-
- for (;;) {
- size_t k;
- char c;
-
- k = fread(&c, 1, 1, stdin);
-
- if (k <= 0) {
- r = -EIO;
- break;
- }
-
- if (c == '\n')
- break;
- else if (c == '\b' || c == 127) {
- if (p > 0) {
- p--;
- fputs("\b \b", ttyf);
- }
- } else {
- passphrase[p++] = c;
- fputc('*', ttyf);
- }
-
- fflush(ttyf);
- }
-
- fputc('\n', ttyf);
- fclose(ttyf);
- tcsetattr(STDIN_FILENO, TCSADRAIN, &old_termios);
-
- if (r < 0)
- return -EIO;
-
- passphrase[p] = 0;
-
- fputs(passphrase, stdout);
- fflush(stdout);
- return 0;
- }
-
- }
-
- fclose(ttyf);
-
- if (!fgets(passphrase, sizeof(passphrase), stdin)) {
- log_error("Failed to read password.");
- return -EIO;
- }
-
- truncate_nl(passphrase);
- fputs(passphrase, stdout);
- fflush(stdout);
-
- return 0;
-}
-