passphrase.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*-
  2. * Copyright (c) 2014 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer
  10. * in this position and unchanged.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /* $OpenBSD: readpassphrase.c,v 1.27 2019/01/25 00:19:25 millert Exp $ */
  27. /*
  28. * Copyright (c) 2000-2002, 2007, 2010
  29. * Todd C. Miller <[email protected]>
  30. *
  31. * Permission to use, copy, modify, and distribute this software for any
  32. * purpose with or without fee is hereby granted, provided that the above
  33. * copyright notice and this permission notice appear in all copies.
  34. *
  35. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  36. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  37. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  38. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  39. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  40. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  41. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  42. *
  43. * Sponsored in part by the Defense Advanced Research Projects
  44. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  45. * Materiel Command, USAF, under agreement number F39502-99-1-0512.
  46. */
  47. /* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
  48. #include "lafe_platform.h"
  49. __FBSDID("$FreeBSD$");
  50. #include <errno.h>
  51. #ifdef HAVE_STDLIB_H
  52. #include <stdlib.h>
  53. #endif
  54. #ifdef HAVE_UNISTD_H
  55. #include <unistd.h>
  56. #endif
  57. #ifdef HAVE_READPASSPHRASE_H
  58. #include <readpassphrase.h>
  59. #endif
  60. #include "err.h"
  61. #include "passphrase.h"
  62. #ifndef HAVE_READPASSPHRASE
  63. #define RPP_ECHO_OFF 0x00 /* Turn off echo (default). */
  64. #define RPP_ECHO_ON 0x01 /* Leave echo on. */
  65. #define RPP_REQUIRE_TTY 0x02 /* Fail if there is no tty. */
  66. #define RPP_FORCELOWER 0x04 /* Force input to lower case. */
  67. #define RPP_FORCEUPPER 0x08 /* Force input to upper case. */
  68. #define RPP_SEVENBIT 0x10 /* Strip the high bit from input. */
  69. #define RPP_STDIN 0x20 /* Read from stdin, not /dev/tty */
  70. #if defined(_WIN32) && !defined(__CYGWIN__)
  71. #include <windows.h>
  72. static char *
  73. readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
  74. {
  75. HANDLE hStdin, hStdout;
  76. DWORD mode, rbytes;
  77. BOOL success;
  78. (void)flags;
  79. hStdin = GetStdHandle(STD_INPUT_HANDLE);
  80. if (hStdin == INVALID_HANDLE_VALUE)
  81. return (NULL);
  82. hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  83. if (hStdout == INVALID_HANDLE_VALUE)
  84. return (NULL);
  85. success = GetConsoleMode(hStdin, &mode);
  86. if (!success)
  87. return (NULL);
  88. mode &= ~ENABLE_ECHO_INPUT;
  89. mode |= ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
  90. success = SetConsoleMode(hStdin, mode);
  91. if (!success)
  92. return (NULL);
  93. success = WriteFile(hStdout, prompt, (DWORD)strlen(prompt),
  94. NULL, NULL);
  95. if (!success)
  96. return (NULL);
  97. success = ReadFile(hStdin, buf, (DWORD)bufsiz - 1, &rbytes, NULL);
  98. if (!success)
  99. return (NULL);
  100. WriteFile(hStdout, "\r\n", 2, NULL, NULL);
  101. buf[rbytes] = '\0';
  102. /* Remove trailing carriage return(s). */
  103. if (rbytes > 2 && buf[rbytes - 2] == '\r' && buf[rbytes - 1] == '\n')
  104. buf[rbytes - 2] = '\0';
  105. return (buf);
  106. }
  107. #else /* _WIN32 && !__CYGWIN__ */
  108. #include <assert.h>
  109. #include <ctype.h>
  110. #include <fcntl.h>
  111. #ifdef HAVE_PATHS_H
  112. #include <paths.h>
  113. #endif
  114. #include <signal.h>
  115. #include <string.h>
  116. #include <termios.h>
  117. #include <unistd.h>
  118. #ifndef _PATH_TTY
  119. #define _PATH_TTY "/dev/tty"
  120. #endif
  121. #ifdef TCSASOFT
  122. # define _T_FLUSH (TCSAFLUSH|TCSASOFT)
  123. #else
  124. # define _T_FLUSH (TCSAFLUSH)
  125. #endif
  126. /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
  127. #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
  128. # define _POSIX_VDISABLE VDISABLE
  129. #endif
  130. #define M(a,b) (a > b ? a : b)
  131. #define MAX_SIGNO M(M(M(SIGALRM, SIGHUP), \
  132. M(SIGINT, SIGPIPE)), \
  133. M(M(SIGQUIT, SIGTERM), \
  134. M(M(SIGTSTP, SIGTTIN), SIGTTOU)))
  135. static volatile sig_atomic_t signo[MAX_SIGNO + 1];
  136. static void
  137. handler(int s)
  138. {
  139. assert(s <= MAX_SIGNO);
  140. signo[s] = 1;
  141. }
  142. static char *
  143. readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
  144. {
  145. ssize_t nr;
  146. int input, output, save_errno, i, need_restart;
  147. char ch, *p, *end;
  148. struct termios term, oterm;
  149. struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
  150. struct sigaction savetstp, savettin, savettou, savepipe;
  151. /* I suppose we could alloc on demand in this case (XXX). */
  152. if (bufsiz == 0) {
  153. errno = EINVAL;
  154. return(NULL);
  155. }
  156. restart:
  157. for (i = 0; i <= MAX_SIGNO; i++)
  158. signo[i] = 0;
  159. nr = -1;
  160. save_errno = 0;
  161. need_restart = 0;
  162. /*
  163. * Read and write to /dev/tty if available. If not, read from
  164. * stdin and write to stderr unless a tty is required.
  165. */
  166. if ((flags & RPP_STDIN) ||
  167. (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
  168. if (flags & RPP_REQUIRE_TTY) {
  169. errno = ENOTTY;
  170. return(NULL);
  171. }
  172. input = STDIN_FILENO;
  173. output = STDERR_FILENO;
  174. }
  175. /*
  176. * Turn off echo if possible.
  177. * If we are using a tty but are not the foreground pgrp this will
  178. * generate SIGTTOU, so do it *before* installing the signal handlers.
  179. */
  180. if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
  181. memcpy(&term, &oterm, sizeof(term));
  182. if (!(flags & RPP_ECHO_ON))
  183. term.c_lflag &= ~(ECHO | ECHONL);
  184. #ifdef VSTATUS
  185. if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
  186. term.c_cc[VSTATUS] = _POSIX_VDISABLE;
  187. #endif
  188. (void)tcsetattr(input, _T_FLUSH, &term);
  189. } else {
  190. memset(&term, 0, sizeof(term));
  191. term.c_lflag |= ECHO;
  192. memset(&oterm, 0, sizeof(oterm));
  193. oterm.c_lflag |= ECHO;
  194. }
  195. /*
  196. * Catch signals that would otherwise cause the user to end
  197. * up with echo turned off in the shell. Don't worry about
  198. * things like SIGXCPU and SIGVTALRM for now.
  199. */
  200. sigemptyset(&sa.sa_mask);
  201. sa.sa_flags = 0; /* don't restart system calls */
  202. sa.sa_handler = handler;
  203. /* Keep this list in sync with MAX_SIGNO! */
  204. (void)sigaction(SIGALRM, &sa, &savealrm);
  205. (void)sigaction(SIGHUP, &sa, &savehup);
  206. (void)sigaction(SIGINT, &sa, &saveint);
  207. (void)sigaction(SIGPIPE, &sa, &savepipe);
  208. (void)sigaction(SIGQUIT, &sa, &savequit);
  209. (void)sigaction(SIGTERM, &sa, &saveterm);
  210. (void)sigaction(SIGTSTP, &sa, &savetstp);
  211. (void)sigaction(SIGTTIN, &sa, &savettin);
  212. (void)sigaction(SIGTTOU, &sa, &savettou);
  213. if (!(flags & RPP_STDIN)) {
  214. int r = write(output, prompt, strlen(prompt));
  215. (void)r;
  216. }
  217. end = buf + bufsiz - 1;
  218. p = buf;
  219. while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
  220. if (p < end) {
  221. if ((flags & RPP_SEVENBIT))
  222. ch &= 0x7f;
  223. if (isalpha((unsigned char)ch)) {
  224. if ((flags & RPP_FORCELOWER))
  225. ch = (char)tolower((unsigned char)ch);
  226. if ((flags & RPP_FORCEUPPER))
  227. ch = (char)toupper((unsigned char)ch);
  228. }
  229. *p++ = ch;
  230. }
  231. }
  232. *p = '\0';
  233. save_errno = errno;
  234. if (!(term.c_lflag & ECHO)) {
  235. int r = write(output, "\n", 1);
  236. (void)r;
  237. }
  238. /* Restore old terminal settings and signals. */
  239. if (memcmp(&term, &oterm, sizeof(term)) != 0) {
  240. const int sigttou = signo[SIGTTOU];
  241. /* Ignore SIGTTOU generated when we are not the fg pgrp. */
  242. while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
  243. errno == EINTR && !signo[SIGTTOU])
  244. continue;
  245. signo[SIGTTOU] = sigttou;
  246. }
  247. (void)sigaction(SIGALRM, &savealrm, NULL);
  248. (void)sigaction(SIGHUP, &savehup, NULL);
  249. (void)sigaction(SIGINT, &saveint, NULL);
  250. (void)sigaction(SIGQUIT, &savequit, NULL);
  251. (void)sigaction(SIGPIPE, &savepipe, NULL);
  252. (void)sigaction(SIGTERM, &saveterm, NULL);
  253. (void)sigaction(SIGTSTP, &savetstp, NULL);
  254. (void)sigaction(SIGTTIN, &savettin, NULL);
  255. (void)sigaction(SIGTTOU, &savettou, NULL);
  256. if (input != STDIN_FILENO)
  257. (void)close(input);
  258. /*
  259. * If we were interrupted by a signal, resend it to ourselves
  260. * now that we have restored the signal handlers.
  261. */
  262. for (i = 0; i <= MAX_SIGNO; i++) {
  263. if (signo[i]) {
  264. kill(getpid(), i);
  265. switch (i) {
  266. case SIGTSTP:
  267. case SIGTTIN:
  268. case SIGTTOU:
  269. need_restart = 1;
  270. }
  271. }
  272. }
  273. if (need_restart)
  274. goto restart;
  275. if (save_errno)
  276. errno = save_errno;
  277. return(nr == -1 ? NULL : buf);
  278. }
  279. #endif /* _WIN32 && !__CYGWIN__ */
  280. #endif /* HAVE_READPASSPHRASE */
  281. char *
  282. lafe_readpassphrase(const char *prompt, char *buf, size_t bufsiz)
  283. {
  284. char *p;
  285. p = readpassphrase(prompt, buf, bufsiz, RPP_ECHO_OFF);
  286. if (p == NULL) {
  287. switch (errno) {
  288. case EINTR:
  289. break;
  290. default:
  291. lafe_errc(1, errno, "Couldn't read passphrase");
  292. break;
  293. }
  294. }
  295. return (p);
  296. }