X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fask-password-api.c;h=f57105c132e22ec70a529e2f3bcde46a75792501;hp=e4ee8adb4f51aa3a3bcf8c03a94a0dc440f24673;hb=1d6702e8d3877c0bebf3ac817dc45ff72f5ecfa9;hpb=bd1db33fb9ef740272fa4bcbdb034b134ea5e911 diff --git a/src/ask-password-api.c b/src/ask-password-api.c index e4ee8adb4..f57105c13 100644 --- a/src/ask-password-api.c +++ b/src/ask-password-api.c @@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License along with systemd; If not, see . ***/ - +#include #include #include #include @@ -32,9 +32,22 @@ #include #include "util.h" +#include "strv.h" #include "ask-password-api.h" +static void backspace_chars(int ttyfd, size_t p) { + + if (ttyfd < 0) + return; + + while (p > 0) { + p--; + + loop_write(ttyfd, "\b \b", 3, false); + } +} + int ask_password_tty( const char *message, usec_t until, @@ -47,6 +60,8 @@ int ask_password_tty( int r, ttyfd = -1, notify = -1; struct pollfd pollfd[2]; bool reset_tty = false; + bool silent_mode = false; + bool dirty = false; enum { POLL_TTY, POLL_INOTIFY @@ -110,7 +125,7 @@ int ask_password_tty( y = now(CLOCK_MONOTONIC); if (y > until) { - r = -ETIMEDOUT; + r = -ETIME; goto finish; } @@ -131,7 +146,7 @@ int ask_password_tty( r = -errno; goto finish; } else if (k == 0) { - r = -ETIMEDOUT; + r = -ETIME; goto finish; } @@ -154,33 +169,53 @@ int ask_password_tty( if (c == '\n') break; - else if (c == 21) { + else if (c == 21) { /* C-u */ - while (p > 0) { - p--; - - if (ttyfd >= 0) - loop_write(ttyfd, "\b \b", 3, false); - } + if (!silent_mode) + backspace_chars(ttyfd, p); + p = 0; } else if (c == '\b' || c == 127) { + if (p > 0) { + + if (!silent_mode) + backspace_chars(ttyfd, 1); + p--; + } else if (!dirty && !silent_mode) { + silent_mode = true; + + /* There are two ways to enter silent + * mode. Either by pressing backspace + * as first key (and only as first key), + * or ... */ if (ttyfd >= 0) - loop_write(ttyfd, "\b \b", 3, false); - } + loop_write(ttyfd, "(no echo) ", 10, false); + + } else if (ttyfd >= 0) + loop_write(ttyfd, "\a", 1, false); + + } else if (c == '\t' && !silent_mode) { + + backspace_chars(ttyfd, p); + silent_mode = true; + + /* ... or by pressing TAB at any time. */ + + if (ttyfd >= 0) + loop_write(ttyfd, "(no echo) ", 10, false); } else { passphrase[p++] = c; - if (ttyfd >= 0) + if (!silent_mode && ttyfd >= 0) loop_write(ttyfd, "*", 1, false); + + dirty = true; } } - if (ttyfd >= 0) - loop_write(ttyfd, "\n", 1, false); - passphrase[p] = 0; if (!(*_passphrase = strdup(passphrase))) { @@ -195,8 +230,11 @@ finish: close_nointr_nofail(notify); if (ttyfd >= 0) { - if (reset_tty) + + if (reset_tty) { + loop_write(ttyfd, "\n", 1, false); tcsetattr(ttyfd, TCSADRAIN, &old_termios); + } close_nointr_nofail(ttyfd); } @@ -212,6 +250,7 @@ static int create_socket(char **name) { } sa; int one = 1, r; char *c; + mode_t u; assert(name); @@ -222,9 +261,13 @@ static int create_socket(char **name) { zero(sa); sa.un.sun_family = AF_UNIX; - snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/dev/.systemd/ask-password/sck.%llu", random_ull()); + snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%llu", random_ull()); + + u = umask(0177); + r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)); + umask(u); - if (bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) { + if (r < 0) { r = -errno; log_error("bind() failed: %m"); goto fail; @@ -251,12 +294,12 @@ fail: return r; } - int ask_password_agent( const char *message, const char *icon, usec_t until, - char **_passphrase) { + bool accept_cached, + char ***_passphrases) { enum { FD_SOCKET, @@ -264,18 +307,29 @@ int ask_password_agent( _FD_MAX }; - char temp[] = "/dev/.systemd/ask-password/tmp.XXXXXX"; + char temp[] = "/run/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 = -1; - sigset_t mask; + sigset_t mask, oldmask; struct pollfd pollfd[_FD_MAX]; + mode_t u; + + assert(_passphrases); + + assert_se(sigemptyset(&mask) == 0); + sigset_add_many(&mask, SIGINT, SIGTERM, -1); + assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0); + + mkdir_p("/run/systemd/ask-password", 0755); - mkdir_p("/dev/.systemd/ask-password", 0755); + u = umask(0022); + fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY); + umask(u); - if ((fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY)) < 0) { + if (fd < 0) { log_error("Failed to create password file: %m"); r = -errno; goto finish; @@ -291,10 +345,6 @@ int ask_password_agent( 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; @@ -310,9 +360,11 @@ int ask_password_agent( "[Ask]\n" "PID=%lu\n" "Socket=%s\n" + "AcceptCached=%i\n" "NotAfter=%llu\n", (unsigned long) getpid(), socket_name, + accept_cached ? 1 : 0, (unsigned long long) until); if (message) @@ -362,13 +414,13 @@ int ask_password_agent( t = now(CLOCK_MONOTONIC); - if (until <= t) { + if (until > 0 && until <= t) { log_notice("Timed out"); r = -ETIME; goto finish; } - if ((k = poll(pollfd, _FD_MAX, until-t/USEC_PER_MSEC)) < 0) { + if ((k = poll(pollfd, _FD_MAX, until > 0 ? (int) ((until-t)/USEC_PER_MSEC) : -1)) < 0) { if (errno == EINTR) continue; @@ -384,8 +436,10 @@ int ask_password_agent( goto finish; } - if (pollfd[FD_SIGNAL].revents & POLLIN) - break; + if (pollfd[FD_SIGNAL].revents & POLLIN) { + r = -EINTR; + goto finish; + } if (pollfd[FD_SOCKET].revents != POLLIN) { log_error("Unexpected poll() event."); @@ -395,7 +449,7 @@ int ask_password_agent( zero(iovec); iovec.iov_base = passphrase; - iovec.iov_len = sizeof(passphrase)-1; + iovec.iov_len = sizeof(passphrase); zero(control); zero(msghdr); @@ -435,13 +489,27 @@ int ask_password_agent( } if (passphrase[0] == '+') { - passphrase[n] = 0; + char **l; + + if (n == 1) + l = strv_new("", NULL); + else + l = strv_parse_nulstr(passphrase+1, n-1); + /* An empty message refers to the empty password */ - if (!(*_passphrase = strdup(passphrase+1))) { + if (!l) { r = -ENOMEM; goto finish; } + if (strv_length(l) <= 0) { + strv_free(l); + log_error("Invalid packet"); + continue; + } + + *_passphrases = l; + } else if (passphrase[0] == '-') { r = -ECANCELED; goto finish; @@ -478,5 +546,31 @@ finish: if (final[0]) unlink(final); + assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0); + return r; } + +int ask_password_auto(const char *message, const char *icon, usec_t until, bool accept_cached, char ***_passphrases) { + assert(message); + assert(_passphrases); + + if (isatty(STDIN_FILENO)) { + int r; + char *s = NULL, **l = NULL; + + if ((r = ask_password_tty(message, until, NULL, &s)) < 0) + return r; + + l = strv_new(s, NULL); + free(s); + + if (!l) + return -ENOMEM; + + *_passphrases = l; + return r; + + } else + return ask_password_agent(message, icon, until, accept_cached, _passphrases); +}