]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Merge lib/libc/gen/readpassphrase.c v 1.27 from OpenBSD
authorMartin Matuska <martin@matuska.org>
Sun, 9 Jun 2019 23:31:56 +0000 (01:31 +0200)
committerMartin Matuska <martin@matuska.org>
Sun, 9 Jun 2019 23:31:56 +0000 (01:31 +0200)
Fixes #1209

libarchive_fe/passphrase.c

index 8c38ad77782828ea93c3ae9ff7e07dee90966750..edf72d147182470d5d4373d34a61f5ccce09be67 100644 (file)
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
-/*     $OpenBSD: readpassphrase.c,v 1.22 2010/01/13 10:20:54 dtucker Exp $     */
+/*     $OpenBSD: readpassphrase.c,v 1.27 2019/01/25 00:19:25 millert Exp $     */
+
 /*
- * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2000-2002, 2007, 2010
+ *     Todd C. Miller <millert@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -198,6 +200,27 @@ restart:
                output = STDERR_FILENO;
        }
 
+       /*
+        * Turn off echo if possible.
+        * If we are using a tty but are not the foreground pgrp this will
+        * generate SIGTTOU, so do it *before* installing the signal handlers.
+        */
+       if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
+               memcpy(&term, &oterm, sizeof(term));
+               if (!(flags & RPP_ECHO_ON))
+                       term.c_lflag &= ~(ECHO | ECHONL);
+#ifdef VSTATUS
+               if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
+                       term.c_cc[VSTATUS] = _POSIX_VDISABLE;
+#endif
+               (void)tcsetattr(input, _T_FLUSH, &term);
+       } else {
+               memset(&term, 0, sizeof(term));
+               term.c_lflag |= ECHO;
+               memset(&oterm, 0, sizeof(oterm));
+               oterm.c_lflag |= ECHO;
+       }
+
        /*
         * Catch signals that would otherwise cause the user to end
         * up with echo turned off in the shell.  Don't worry about
@@ -217,57 +240,41 @@ restart:
        (void)sigaction(SIGTTIN, &sa, &savettin);
        (void)sigaction(SIGTTOU, &sa, &savettou);
 
-       /* Turn off echo if possible. */
-       if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
-               memcpy(&term, &oterm, sizeof(term));
-               if (!(flags & RPP_ECHO_ON))
-                       term.c_lflag &= ~(ECHO | ECHONL);
-#ifdef VSTATUS
-               if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
-                       term.c_cc[VSTATUS] = _POSIX_VDISABLE;
-#endif
-               (void)tcsetattr(input, _T_FLUSH, &term);
-       } else {
-               memset(&term, 0, sizeof(term));
-               term.c_lflag |= ECHO;
-               memset(&oterm, 0, sizeof(oterm));
-               oterm.c_lflag |= ECHO;
+       if (!(flags & RPP_STDIN)) {
+               int r = write(output, prompt, strlen(prompt));
+               (void)r;
        }
-
-       /* No I/O if we are already backgrounded. */
-       if (signo[SIGTTOU] != 1 && signo[SIGTTIN] != 1) {
-               if (!(flags & RPP_STDIN)) {
-                       int r = write(output, prompt, strlen(prompt));
-                       (void)r;
-               }
-               end = buf + bufsiz - 1;
-               p = buf;
-               while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
-                       if (p < end) {
-                               if ((flags & RPP_SEVENBIT))
-                                       ch &= 0x7f;
-                               if (isalpha((unsigned char)ch)) {
-                                       if ((flags & RPP_FORCELOWER))
-                                               ch = (char)tolower((unsigned char)ch);
-                                       if ((flags & RPP_FORCEUPPER))
-                                               ch = (char)toupper((unsigned char)ch);
-                               }
-                               *p++ = ch;
+       end = buf + bufsiz - 1;
+       p = buf;
+       while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
+               if (p < end) {
+                       if ((flags & RPP_SEVENBIT))
+                               ch &= 0x7f;
+                       if (isalpha((unsigned char)ch)) {
+                               if ((flags & RPP_FORCELOWER))
+                                       ch = (char)tolower((unsigned char)ch);
+                               if ((flags & RPP_FORCEUPPER))
+                                       ch = (char)toupper((unsigned char)ch);
                        }
-               }
-               *p = '\0';
-               save_errno = errno;
-               if (!(term.c_lflag & ECHO)) {
-                       int r = write(output, "\n", 1);
-                       (void)r;
+                       *p++ = ch;
                }
        }
+       *p = '\0';
+       save_errno = errno;
+       if (!(term.c_lflag & ECHO)) {
+               int r = write(output, "\n", 1);
+               (void)r;
+       }
 
        /* Restore old terminal settings and signals. */
        if (memcmp(&term, &oterm, sizeof(term)) != 0) {
+               const int sigttou = signo[SIGTTOU];
+
+               /* Ignore SIGTTOU generated when we are not the fg pgrp. */
                while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
-                   errno == EINTR)
+                   errno == EINTR && !signo[SIGTTOU])
                        continue;
+               signo[SIGTTOU] = sigttou;
        }
        (void)sigaction(SIGALRM, &savealrm, NULL);
        (void)sigaction(SIGHUP, &savehup, NULL);