readpassphrase.c 5.7 KB

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