readpassphrase.c 5.6 KB

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