X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fsystemd.git;a=blobdiff_plain;f=src%2Fshared%2Fask-password-api.c;h=a0c76eddbe7ab4c6bc877003d0dbca31efe2cd79;hp=c96dbf877a672f4b265d0634658f0f0c1de30578;hb=53e1b683907c2f12330f00feb9630150196f064d;hpb=0d0696812980d8f41c54739ad73716a233a2fa7c diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c index c96dbf877a6..a0c76eddbe7 100644 --- a/src/shared/ask-password-api.c +++ b/src/shared/ask-password-api.c @@ -1,5 +1,4 @@ -/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ - +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. @@ -21,13 +20,22 @@ #include #include +#include +#include #include +#include #include #include +#include +#include +#include #include #include #include #include +#include +#include +#include #include #include #include @@ -36,17 +44,22 @@ #include "ask-password-api.h" #include "fd-util.h" #include "fileio.h" -#include "formats-util.h" +#include "format-util.h" #include "io-util.h" +#include "log.h" +#include "macro.h" #include "missing.h" #include "mkdir.h" +#include "process-util.h" #include "random-util.h" #include "signal-util.h" #include "socket-util.h" #include "string-util.h" #include "strv.h" #include "terminal-util.h" +#include "time-util.h" #include "umask-util.h" +#include "utf8.h" #include "util.h" #define KEYRING_TIMEOUT_USEC ((5 * USEC_PER_MINUTE) / 2) @@ -59,7 +72,7 @@ static int lookup_key(const char *keyname, key_serial_t *ret) { serial = request_key("user", keyname, NULL, 0); if (serial == -1) - return -errno; + return negative_errno(); *ret = serial; return 0; @@ -84,7 +97,7 @@ static int retrieve_key(key_serial_t serial, char ***ret) { if (n < m) break; - memory_erase(p, n); + explicit_bzero(p, n); free(p); m *= 2; } @@ -93,7 +106,7 @@ static int retrieve_key(key_serial_t serial, char ***ret) { if (!l) return -ENOMEM; - memory_erase(p, n); + explicit_bzero(p, n); *ret = l; return 0; @@ -128,12 +141,8 @@ static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **pa if (r < 0) return r; - /* Truncate trailing NUL */ - assert(n > 0); - assert(p[n-1] == 0); - - serial = add_key("user", keyname, p, n-1, KEY_SPEC_USER_KEYRING); - memory_erase(p, n); + serial = add_key("user", keyname, p, n, KEY_SPEC_USER_KEYRING); + explicit_bzero(p, n); if (serial == -1) return -errno; @@ -201,8 +210,8 @@ int ask_password_tty( char **ret) { struct termios old_termios, new_termios; - char passphrase[LINE_MAX], *x; - size_t p = 0; + char passphrase[LINE_MAX + 1] = {}, *x; + size_t p = 0, codepoint = 0; int r; _cleanup_close_ int ttyfd = -1, notify = -1; struct pollfd pollfd[2]; @@ -242,10 +251,12 @@ int ask_password_tty( goto finish; } - loop_write(ttyfd, ANSI_HIGHLIGHT, strlen(ANSI_HIGHLIGHT), false); + if (colors_enabled()) + loop_write(ttyfd, ANSI_HIGHLIGHT, strlen(ANSI_HIGHLIGHT), false); loop_write(ttyfd, message, strlen(message), false); loop_write(ttyfd, " ", 1, false); - loop_write(ttyfd, ANSI_NORMAL, strlen(ANSI_NORMAL), false); + if (colors_enabled()) + loop_write(ttyfd, ANSI_NORMAL, strlen(ANSI_NORMAL), false); new_termios = old_termios; new_termios.c_lflag &= ~(ICANON|ECHO); @@ -310,7 +321,7 @@ int ask_password_tty( n = read(ttyfd >= 0 ? ttyfd : STDIN_FILENO, &c, 1); if (n < 0) { - if (errno == EINTR || errno == EAGAIN) + if (IN_SET(errno, EINTR, EAGAIN)) continue; r = -errno; @@ -327,7 +338,7 @@ int ask_password_tty( backspace_chars(ttyfd, p); p = 0; - } else if (c == '\b' || c == 127) { + } else if (IN_SET(c, '\b', 127)) { if (p > 0) { @@ -366,8 +377,13 @@ int ask_password_tty( passphrase[p++] = c; - if (!(flags & ASK_PASSWORD_SILENT) && ttyfd >= 0) - loop_write(ttyfd, (flags & ASK_PASSWORD_ECHO) ? &c : "*", 1, false); + if (!(flags & ASK_PASSWORD_SILENT) && ttyfd >= 0) { + n = utf8_encoded_valid_unichar(passphrase + codepoint); + if (n >= 0) { + codepoint = p; + loop_write(ttyfd, (flags & ASK_PASSWORD_ECHO) ? &c : "*", 1, false); + } + } dirty = true; } @@ -376,7 +392,7 @@ int ask_password_tty( } x = strndup(passphrase, p); - memory_erase(passphrase, p); + explicit_bzero(passphrase, p); if (!x) { r = -ENOMEM; goto finish; @@ -415,7 +431,7 @@ static int create_socket(char **name) { snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%" PRIx64, random_u64()); RUN_WITH_UMASK(0177) { - if (bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) + if (bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) return -errno; } @@ -470,9 +486,9 @@ int ask_password_agent( (void) mkdir_p_label("/run/systemd/ask-password", 0755); - fd = mkostemp_safe(temp, O_WRONLY|O_CLOEXEC); + fd = mkostemp_safe(temp); if (fd < 0) { - r = -errno; + r = fd; goto finish; } @@ -505,7 +521,7 @@ int ask_password_agent( "AcceptCached=%i\n" "Echo=%i\n" "NotAfter="USEC_FMT"\n", - getpid(), + getpid_cached(), socket_name, (flags & ASK_PASSWORD_ACCEPT_CACHED) ? 1 : 0, (flags & ASK_PASSWORD_ECHO) ? 1 : 0, @@ -598,8 +614,7 @@ int ask_password_agent( n = recvmsg(socket_fd, &msghdr, 0); if (n < 0) { - if (errno == EAGAIN || - errno == EINTR) + if (IN_SET(errno, EAGAIN, EINTR)) continue; r = -errno; @@ -633,7 +648,7 @@ int ask_password_agent( l = strv_new("", NULL); else l = strv_parse_nulstr(passphrase+1, n-1); - memory_erase(passphrase, n); + explicit_bzero(passphrase, n); if (!l) { r = -ENOMEM; goto finish;