readpassphrase.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* $OpenBSD: readpassphrase.c,v 1.27 2019/01/25 00:19:25 millert Exp $ */
  2. /*
  3. * Copyright (c) 2000-2002, 2007, 2010
  4. * Todd C. Miller <millert@openbsd.org>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. *
  18. * Sponsored in part by the Defense Advanced Research Projects
  19. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  20. * Materiel Command, USAF, under agreement number F39502-99-1-0512.
  21. */
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <paths.h>
  26. #include <pwd.h>
  27. #include <signal.h>
  28. #include <string.h>
  29. #include <termios.h>
  30. #include <unistd.h>
  31. #include <readpassphrase.h>
  32. #ifndef TCSASOFT
  33. /* If we don't have TCSASOFT define it so that ORing it it below is a no-op. */
  34. # define TCSASOFT 0
  35. #endif
  36. /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
  37. #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
  38. # define _POSIX_VDISABLE VDISABLE
  39. #endif
  40. static volatile sig_atomic_t signo[_NSIG];
  41. static void handler(int);
  42. char *
  43. readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
  44. {
  45. ssize_t nr;
  46. int input, output, save_errno, i, need_restart;
  47. char ch, *p, *end;
  48. struct termios term, oterm;
  49. struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
  50. struct sigaction savetstp, savettin, savettou, savepipe;
  51. /* I suppose we could alloc on demand in this case (XXX). */
  52. if (bufsiz == 0) {
  53. errno = EINVAL;
  54. return(NULL);
  55. }
  56. restart:
  57. for (i = 0; i < _NSIG; i++)
  58. signo[i] = 0;
  59. nr = -1;
  60. save_errno = 0;
  61. need_restart = 0;
  62. /*
  63. * Read and write to /dev/tty if available. If not, read from
  64. * stdin and write to stderr unless a tty is required.
  65. */
  66. if ((flags & RPP_STDIN) ||
  67. (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
  68. if (flags & RPP_REQUIRE_TTY) {
  69. errno = ENOTTY;
  70. return(NULL);
  71. }
  72. input = STDIN_FILENO;
  73. output = STDERR_FILENO;
  74. }
  75. /*
  76. * Turn off echo if possible.
  77. * If we are using a tty but are not the foreground pgrp this will
  78. * generate SIGTTOU, so do it *before* installing the signal handlers.
  79. */
  80. if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
  81. memcpy(&term, &oterm, sizeof(term));
  82. if (!(flags & RPP_ECHO_ON))
  83. term.c_lflag &= ~(ECHO | ECHONL);
  84. (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
  85. } else {
  86. memset(&term, 0, sizeof(term));
  87. term.c_lflag |= ECHO;
  88. memset(&oterm, 0, sizeof(oterm));
  89. oterm.c_lflag |= ECHO;
  90. }
  91. /*
  92. * Catch signals that would otherwise cause the user to end
  93. * up with echo turned off in the shell. Don't worry about
  94. * things like SIGXCPU and SIGVTALRM for now.
  95. */
  96. sigemptyset(&sa.sa_mask);
  97. sa.sa_flags = 0; /* don't restart system calls */
  98. sa.sa_handler = handler;
  99. (void)sigaction(SIGALRM, &sa, &savealrm);
  100. (void)sigaction(SIGHUP, &sa, &savehup);
  101. (void)sigaction(SIGINT, &sa, &saveint);
  102. (void)sigaction(SIGPIPE, &sa, &savepipe);
  103. (void)sigaction(SIGQUIT, &sa, &savequit);
  104. (void)sigaction(SIGTERM, &sa, &saveterm);
  105. (void)sigaction(SIGTSTP, &sa, &savetstp);
  106. (void)sigaction(SIGTTIN, &sa, &savettin);
  107. (void)sigaction(SIGTTOU, &sa, &savettou);
  108. if (!(flags & RPP_STDIN))
  109. (void)write(output, prompt, strlen(prompt));
  110. end = buf + bufsiz - 1;
  111. p = buf;
  112. while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
  113. if (p < end) {
  114. if ((flags & RPP_SEVENBIT))
  115. ch &= 0x7f;
  116. if (isalpha((unsigned char)ch)) {
  117. if ((flags & RPP_FORCELOWER))
  118. ch = (char)tolower((unsigned char)ch);
  119. if ((flags & RPP_FORCEUPPER))
  120. ch = (char)toupper((unsigned char)ch);
  121. }
  122. *p++ = ch;
  123. }
  124. }
  125. *p = '\0';
  126. save_errno = errno;
  127. if (!(term.c_lflag & ECHO))
  128. (void)write(output, "\n", 1);
  129. /* Restore old terminal settings and signals. */
  130. if (memcmp(&term, &oterm, sizeof(term)) != 0) {
  131. const int sigttou = signo[SIGTTOU];
  132. /* Ignore SIGTTOU generated when we are not the fg pgrp. */
  133. while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
  134. errno == EINTR && !signo[SIGTTOU])
  135. continue;
  136. signo[SIGTTOU] = sigttou;
  137. }
  138. (void)sigaction(SIGALRM, &savealrm, NULL);
  139. (void)sigaction(SIGHUP, &savehup, NULL);
  140. (void)sigaction(SIGINT, &saveint, NULL);
  141. (void)sigaction(SIGQUIT, &savequit, NULL);
  142. (void)sigaction(SIGPIPE, &savepipe, NULL);
  143. (void)sigaction(SIGTERM, &saveterm, NULL);
  144. (void)sigaction(SIGTSTP, &savetstp, NULL);
  145. (void)sigaction(SIGTTIN, &savettin, NULL);
  146. (void)sigaction(SIGTTOU, &savettou, NULL);
  147. if (input != STDIN_FILENO)
  148. (void)close(input);
  149. /*
  150. * If we were interrupted by a signal, resend it to ourselves
  151. * now that we have restored the signal handlers.
  152. */
  153. for (i = 0; i < _NSIG; i++) {
  154. if (signo[i]) {
  155. kill(getpid(), i);
  156. switch (i) {
  157. case SIGTSTP:
  158. case SIGTTIN:
  159. case SIGTTOU:
  160. need_restart = 1;
  161. }
  162. }
  163. }
  164. if (need_restart)
  165. goto restart;
  166. if (save_errno)
  167. errno = save_errno;
  168. return(nr == -1 ? NULL : buf);
  169. }
  170. static void handler(int s)
  171. {
  172. signo[s] = 1;
  173. }