readpassphrase.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* $OpenBSD: readpassphrase.c,v 1.26 2016/10/18 12:47:18 millert Exp $ */
  2. /*
  3. * Copyright (c) 2000-2002, 2007, 2010
  4. * Todd C. Miller <Todd.Miller@courtesan.com>
  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. /* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
  23. #include "includes.h"
  24. #ifndef HAVE_READPASSPHRASE
  25. #include <termios.h>
  26. #include <signal.h>
  27. #include <ctype.h>
  28. #include <fcntl.h>
  29. #include <readpassphrase.h>
  30. #include <errno.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #ifndef TCSASOFT
  34. /* If we don't have TCSASOFT define it so that ORing it it below is a no-op. */
  35. # define TCSASOFT 0
  36. #endif
  37. /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
  38. #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
  39. # define _POSIX_VDISABLE VDISABLE
  40. #endif
  41. static volatile sig_atomic_t signo[_NSIG];
  42. static void handler(int);
  43. char *
  44. readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
  45. {
  46. ssize_t nr;
  47. int input, output, save_errno, i, need_restart;
  48. char ch, *p, *end;
  49. struct termios term, oterm;
  50. struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
  51. struct sigaction savetstp, savettin, savettou, savepipe;
  52. /* I suppose we could alloc on demand in this case (XXX). */
  53. if (bufsiz == 0) {
  54. errno = EINVAL;
  55. return(NULL);
  56. }
  57. restart:
  58. for (i = 0; i < _NSIG; i++)
  59. signo[i] = 0;
  60. nr = -1;
  61. save_errno = 0;
  62. need_restart = 0;
  63. /*
  64. * Read and write to /dev/tty if available. If not, read from
  65. * stdin and write to stderr unless a tty is required.
  66. */
  67. if ((flags & RPP_STDIN) ||
  68. (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
  69. if (flags & RPP_REQUIRE_TTY) {
  70. errno = ENOTTY;
  71. return(NULL);
  72. }
  73. input = STDIN_FILENO;
  74. output = STDERR_FILENO;
  75. }
  76. /*
  77. * Turn off echo if possible.
  78. * If we are using a tty but are not the foreground pgrp this will
  79. * generate SIGTTOU, so do it *before* installing the signal handlers.
  80. */
  81. if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
  82. memcpy(&term, &oterm, sizeof(term));
  83. if (!(flags & RPP_ECHO_ON))
  84. term.c_lflag &= ~(ECHO | ECHONL);
  85. #ifdef VSTATUS
  86. if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
  87. term.c_cc[VSTATUS] = _POSIX_VDISABLE;
  88. #endif
  89. (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
  90. } else {
  91. memset(&term, 0, sizeof(term));
  92. term.c_lflag |= ECHO;
  93. memset(&oterm, 0, sizeof(oterm));
  94. oterm.c_lflag |= ECHO;
  95. }
  96. /*
  97. * Catch signals that would otherwise cause the user to end
  98. * up with echo turned off in the shell. Don't worry about
  99. * things like SIGXCPU and SIGVTALRM for now.
  100. */
  101. sigemptyset(&sa.sa_mask);
  102. sa.sa_flags = 0; /* don't restart system calls */
  103. sa.sa_handler = handler;
  104. (void)sigaction(SIGALRM, &sa, &savealrm);
  105. (void)sigaction(SIGHUP, &sa, &savehup);
  106. (void)sigaction(SIGINT, &sa, &saveint);
  107. (void)sigaction(SIGPIPE, &sa, &savepipe);
  108. (void)sigaction(SIGQUIT, &sa, &savequit);
  109. (void)sigaction(SIGTERM, &sa, &saveterm);
  110. (void)sigaction(SIGTSTP, &sa, &savetstp);
  111. (void)sigaction(SIGTTIN, &sa, &savettin);
  112. (void)sigaction(SIGTTOU, &sa, &savettou);
  113. if (!(flags & RPP_STDIN))
  114. (void)write(output, prompt, strlen(prompt));
  115. end = buf + bufsiz - 1;
  116. p = buf;
  117. while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
  118. if (p < end) {
  119. if ((flags & RPP_SEVENBIT))
  120. ch &= 0x7f;
  121. if (isalpha((unsigned char)ch)) {
  122. if ((flags & RPP_FORCELOWER))
  123. ch = (char)tolower((unsigned char)ch);
  124. if ((flags & RPP_FORCEUPPER))
  125. ch = (char)toupper((unsigned char)ch);
  126. }
  127. *p++ = ch;
  128. }
  129. }
  130. *p = '\0';
  131. save_errno = errno;
  132. if (!(term.c_lflag & ECHO))
  133. (void)write(output, "\n", 1);
  134. /* Restore old terminal settings and signals. */
  135. if (memcmp(&term, &oterm, sizeof(term)) != 0) {
  136. const int sigttou = signo[SIGTTOU];
  137. /* Ignore SIGTTOU generated when we are not the fg pgrp. */
  138. while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
  139. errno == EINTR && !signo[SIGTTOU])
  140. continue;
  141. signo[SIGTTOU] = sigttou;
  142. }
  143. (void)sigaction(SIGALRM, &savealrm, NULL);
  144. (void)sigaction(SIGHUP, &savehup, NULL);
  145. (void)sigaction(SIGINT, &saveint, NULL);
  146. (void)sigaction(SIGQUIT, &savequit, NULL);
  147. (void)sigaction(SIGPIPE, &savepipe, NULL);
  148. (void)sigaction(SIGTERM, &saveterm, NULL);
  149. (void)sigaction(SIGTSTP, &savetstp, NULL);
  150. (void)sigaction(SIGTTIN, &savettin, NULL);
  151. (void)sigaction(SIGTTOU, &savettou, NULL);
  152. if (input != STDIN_FILENO)
  153. (void)close(input);
  154. /*
  155. * If we were interrupted by a signal, resend it to ourselves
  156. * now that we have restored the signal handlers.
  157. */
  158. for (i = 0; i < _NSIG; i++) {
  159. if (signo[i]) {
  160. kill(getpid(), i);
  161. switch (i) {
  162. case SIGTSTP:
  163. case SIGTTIN:
  164. case SIGTTOU:
  165. need_restart = 1;
  166. }
  167. }
  168. }
  169. if (need_restart)
  170. goto restart;
  171. if (save_errno)
  172. errno = save_errno;
  173. return(nr == -1 ? NULL : buf);
  174. }
  175. DEF_WEAK(readpassphrase);
  176. #if 0
  177. char *
  178. getpass(const char *prompt)
  179. {
  180. static char buf[_PASSWORD_LEN + 1];
  181. return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
  182. }
  183. #endif
  184. static void handler(int s)
  185. {
  186. signo[s] = 1;
  187. }
  188. #endif /* HAVE_READPASSPHRASE */