passphrase.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #ifdef HAVE_SIGACTION
  150. struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
  151. struct sigaction savetstp, savettin, savettou, savepipe;
  152. #endif
  153. /* I suppose we could alloc on demand in this case (XXX). */
  154. if (bufsiz == 0) {
  155. errno = EINVAL;
  156. return(NULL);
  157. }
  158. restart:
  159. for (i = 0; i <= MAX_SIGNO; i++)
  160. signo[i] = 0;
  161. nr = -1;
  162. save_errno = 0;
  163. need_restart = 0;
  164. /*
  165. * Read and write to /dev/tty if available. If not, read from
  166. * stdin and write to stderr unless a tty is required.
  167. */
  168. if ((flags & RPP_STDIN) ||
  169. (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
  170. if (flags & RPP_REQUIRE_TTY) {
  171. errno = ENOTTY;
  172. return(NULL);
  173. }
  174. input = STDIN_FILENO;
  175. output = STDERR_FILENO;
  176. }
  177. /*
  178. * Turn off echo if possible.
  179. * If we are using a tty but are not the foreground pgrp this will
  180. * generate SIGTTOU, so do it *before* installing the signal handlers.
  181. */
  182. if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
  183. memcpy(&term, &oterm, sizeof(term));
  184. if (!(flags & RPP_ECHO_ON))
  185. term.c_lflag &= ~(ECHO | ECHONL);
  186. #ifdef VSTATUS
  187. if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
  188. term.c_cc[VSTATUS] = _POSIX_VDISABLE;
  189. #endif
  190. (void)tcsetattr(input, _T_FLUSH, &term);
  191. } else {
  192. memset(&term, 0, sizeof(term));
  193. term.c_lflag |= ECHO;
  194. memset(&oterm, 0, sizeof(oterm));
  195. oterm.c_lflag |= ECHO;
  196. }
  197. #ifdef HAVE_SIGACTION
  198. /*
  199. * Catch signals that would otherwise cause the user to end
  200. * up with echo turned off in the shell. Don't worry about
  201. * things like SIGXCPU and SIGVTALRM for now.
  202. */
  203. sigemptyset(&sa.sa_mask);
  204. sa.sa_flags = 0; /* don't restart system calls */
  205. sa.sa_handler = handler;
  206. /* Keep this list in sync with MAX_SIGNO! */
  207. (void)sigaction(SIGALRM, &sa, &savealrm);
  208. (void)sigaction(SIGHUP, &sa, &savehup);
  209. (void)sigaction(SIGINT, &sa, &saveint);
  210. (void)sigaction(SIGPIPE, &sa, &savepipe);
  211. (void)sigaction(SIGQUIT, &sa, &savequit);
  212. (void)sigaction(SIGTERM, &sa, &saveterm);
  213. (void)sigaction(SIGTSTP, &sa, &savetstp);
  214. (void)sigaction(SIGTTIN, &sa, &savettin);
  215. (void)sigaction(SIGTTOU, &sa, &savettou);
  216. #endif
  217. if (!(flags & RPP_STDIN)) {
  218. int r = write(output, prompt, strlen(prompt));
  219. (void)r;
  220. }
  221. end = buf + bufsiz - 1;
  222. p = buf;
  223. while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
  224. if (p < end) {
  225. if ((flags & RPP_SEVENBIT))
  226. ch &= 0x7f;
  227. if (isalpha((unsigned char)ch)) {
  228. if ((flags & RPP_FORCELOWER))
  229. ch = (char)tolower((unsigned char)ch);
  230. if ((flags & RPP_FORCEUPPER))
  231. ch = (char)toupper((unsigned char)ch);
  232. }
  233. *p++ = ch;
  234. }
  235. }
  236. *p = '\0';
  237. save_errno = errno;
  238. if (!(term.c_lflag & ECHO)) {
  239. int r = write(output, "\n", 1);
  240. (void)r;
  241. }
  242. /* Restore old terminal settings and signals. */
  243. if (memcmp(&term, &oterm, sizeof(term)) != 0) {
  244. const int sigttou = signo[SIGTTOU];
  245. /* Ignore SIGTTOU generated when we are not the fg pgrp. */
  246. while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
  247. errno == EINTR && !signo[SIGTTOU])
  248. continue;
  249. signo[SIGTTOU] = sigttou;
  250. }
  251. #ifdef HAVE_SIGACTION
  252. (void)sigaction(SIGALRM, &savealrm, NULL);
  253. (void)sigaction(SIGHUP, &savehup, NULL);
  254. (void)sigaction(SIGINT, &saveint, NULL);
  255. (void)sigaction(SIGQUIT, &savequit, NULL);
  256. (void)sigaction(SIGPIPE, &savepipe, NULL);
  257. (void)sigaction(SIGTERM, &saveterm, NULL);
  258. (void)sigaction(SIGTSTP, &savetstp, NULL);
  259. (void)sigaction(SIGTTIN, &savettin, NULL);
  260. (void)sigaction(SIGTTOU, &savettou, NULL);
  261. #endif
  262. if (input != STDIN_FILENO)
  263. (void)close(input);
  264. /*
  265. * If we were interrupted by a signal, resend it to ourselves
  266. * now that we have restored the signal handlers.
  267. */
  268. for (i = 0; i <= MAX_SIGNO; i++) {
  269. if (signo[i]) {
  270. kill(getpid(), i);
  271. switch (i) {
  272. case SIGTSTP:
  273. case SIGTTIN:
  274. case SIGTTOU:
  275. need_restart = 1;
  276. }
  277. }
  278. }
  279. if (need_restart)
  280. goto restart;
  281. if (save_errno)
  282. errno = save_errno;
  283. return(nr == -1 ? NULL : buf);
  284. }
  285. #endif /* _WIN32 && !__CYGWIN__ */
  286. #endif /* HAVE_READPASSPHRASE */
  287. char *
  288. lafe_readpassphrase(const char *prompt, char *buf, size_t bufsiz)
  289. {
  290. char *p;
  291. p = readpassphrase(prompt, buf, bufsiz, RPP_ECHO_OFF);
  292. if (p == NULL) {
  293. switch (errno) {
  294. case EINTR:
  295. break;
  296. default:
  297. lafe_errc(1, errno, "Couldn't read passphrase");
  298. break;
  299. }
  300. }
  301. return (p);
  302. }