16b384f39SPeter Avalos /*-
26b384f39SPeter Avalos * Copyright (c) 2014 Michihiro NAKAJIMA
36b384f39SPeter Avalos * All rights reserved.
46b384f39SPeter Avalos *
56b384f39SPeter Avalos * Redistribution and use in source and binary forms, with or without
66b384f39SPeter Avalos * modification, are permitted provided that the following conditions
76b384f39SPeter Avalos * are met:
86b384f39SPeter Avalos * 1. Redistributions of source code must retain the above copyright
96b384f39SPeter Avalos * notice, this list of conditions and the following disclaimer
106b384f39SPeter Avalos * in this position and unchanged.
116b384f39SPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
126b384f39SPeter Avalos * notice, this list of conditions and the following disclaimer in the
136b384f39SPeter Avalos * documentation and/or other materials provided with the distribution.
146b384f39SPeter Avalos *
156b384f39SPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
166b384f39SPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
176b384f39SPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
186b384f39SPeter Avalos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
196b384f39SPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
206b384f39SPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
216b384f39SPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
226b384f39SPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
236b384f39SPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
246b384f39SPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
256b384f39SPeter Avalos */
26*085658deSDaniel Fojt /* $OpenBSD: readpassphrase.c,v 1.27 2019/01/25 00:19:25 millert Exp $ */
27*085658deSDaniel Fojt
286b384f39SPeter Avalos /*
29*085658deSDaniel Fojt * Copyright (c) 2000-2002, 2007, 2010
30*085658deSDaniel Fojt * Todd C. Miller <millert@openbsd.org>
316b384f39SPeter Avalos *
326b384f39SPeter Avalos * Permission to use, copy, modify, and distribute this software for any
336b384f39SPeter Avalos * purpose with or without fee is hereby granted, provided that the above
346b384f39SPeter Avalos * copyright notice and this permission notice appear in all copies.
356b384f39SPeter Avalos *
366b384f39SPeter Avalos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
376b384f39SPeter Avalos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
386b384f39SPeter Avalos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
396b384f39SPeter Avalos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
406b384f39SPeter Avalos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
416b384f39SPeter Avalos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
426b384f39SPeter Avalos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
436b384f39SPeter Avalos *
446b384f39SPeter Avalos * Sponsored in part by the Defense Advanced Research Projects
456b384f39SPeter Avalos * Agency (DARPA) and Air Force Research Laboratory, Air Force
466b384f39SPeter Avalos * Materiel Command, USAF, under agreement number F39502-99-1-0512.
476b384f39SPeter Avalos */
486b384f39SPeter Avalos
496b384f39SPeter Avalos /* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
506b384f39SPeter Avalos
516b384f39SPeter Avalos
526b384f39SPeter Avalos #include "lafe_platform.h"
536b384f39SPeter Avalos __FBSDID("$FreeBSD$");
546b384f39SPeter Avalos
556b384f39SPeter Avalos #include <errno.h>
566b384f39SPeter Avalos #ifdef HAVE_STDLIB_H
576b384f39SPeter Avalos #include <stdlib.h>
586b384f39SPeter Avalos #endif
596b384f39SPeter Avalos #ifdef HAVE_UNISTD_H
606b384f39SPeter Avalos #include <unistd.h>
616b384f39SPeter Avalos #endif
626b384f39SPeter Avalos #ifdef HAVE_READPASSPHRASE_H
636b384f39SPeter Avalos #include <readpassphrase.h>
646b384f39SPeter Avalos #endif
656b384f39SPeter Avalos
666b384f39SPeter Avalos #include "err.h"
676b384f39SPeter Avalos #include "passphrase.h"
686b384f39SPeter Avalos
696b384f39SPeter Avalos #ifndef HAVE_READPASSPHRASE
706b384f39SPeter Avalos
716b384f39SPeter Avalos #define RPP_ECHO_OFF 0x00 /* Turn off echo (default). */
726b384f39SPeter Avalos #define RPP_ECHO_ON 0x01 /* Leave echo on. */
736b384f39SPeter Avalos #define RPP_REQUIRE_TTY 0x02 /* Fail if there is no tty. */
746b384f39SPeter Avalos #define RPP_FORCELOWER 0x04 /* Force input to lower case. */
756b384f39SPeter Avalos #define RPP_FORCEUPPER 0x08 /* Force input to upper case. */
766b384f39SPeter Avalos #define RPP_SEVENBIT 0x10 /* Strip the high bit from input. */
776b384f39SPeter Avalos #define RPP_STDIN 0x20 /* Read from stdin, not /dev/tty */
786b384f39SPeter Avalos
796b384f39SPeter Avalos
806b384f39SPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
816b384f39SPeter Avalos #include <windows.h>
826b384f39SPeter Avalos
836b384f39SPeter Avalos static char *
readpassphrase(const char * prompt,char * buf,size_t bufsiz,int flags)846b384f39SPeter Avalos readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
856b384f39SPeter Avalos {
866b384f39SPeter Avalos HANDLE hStdin, hStdout;
876b384f39SPeter Avalos DWORD mode, rbytes;
886b384f39SPeter Avalos BOOL success;
896b384f39SPeter Avalos
906b384f39SPeter Avalos (void)flags;
916b384f39SPeter Avalos
926b384f39SPeter Avalos hStdin = GetStdHandle(STD_INPUT_HANDLE);
936b384f39SPeter Avalos if (hStdin == INVALID_HANDLE_VALUE)
946b384f39SPeter Avalos return (NULL);
956b384f39SPeter Avalos hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
966b384f39SPeter Avalos if (hStdout == INVALID_HANDLE_VALUE)
976b384f39SPeter Avalos return (NULL);
986b384f39SPeter Avalos
996b384f39SPeter Avalos success = GetConsoleMode(hStdin, &mode);
1006b384f39SPeter Avalos if (!success)
1016b384f39SPeter Avalos return (NULL);
1026b384f39SPeter Avalos mode &= ~ENABLE_ECHO_INPUT;
1036b384f39SPeter Avalos mode |= ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
1046b384f39SPeter Avalos success = SetConsoleMode(hStdin, mode);
1056b384f39SPeter Avalos if (!success)
1066b384f39SPeter Avalos return (NULL);
1076b384f39SPeter Avalos
1086b384f39SPeter Avalos success = WriteFile(hStdout, prompt, (DWORD)strlen(prompt),
1096b384f39SPeter Avalos NULL, NULL);
1106b384f39SPeter Avalos if (!success)
1116b384f39SPeter Avalos return (NULL);
1126b384f39SPeter Avalos success = ReadFile(hStdin, buf, (DWORD)bufsiz - 1, &rbytes, NULL);
1136b384f39SPeter Avalos if (!success)
1146b384f39SPeter Avalos return (NULL);
1156b384f39SPeter Avalos WriteFile(hStdout, "\r\n", 2, NULL, NULL);
1166b384f39SPeter Avalos buf[rbytes] = '\0';
1176b384f39SPeter Avalos /* Remove trailing carriage return(s). */
1186b384f39SPeter Avalos if (rbytes > 2 && buf[rbytes - 2] == '\r' && buf[rbytes - 1] == '\n')
1196b384f39SPeter Avalos buf[rbytes - 2] = '\0';
1206b384f39SPeter Avalos
1216b384f39SPeter Avalos return (buf);
1226b384f39SPeter Avalos }
1236b384f39SPeter Avalos
1246b384f39SPeter Avalos #else /* _WIN32 && !__CYGWIN__ */
1256b384f39SPeter Avalos
126e95abc47Szrj #include <assert.h>
1276b384f39SPeter Avalos #include <ctype.h>
1286b384f39SPeter Avalos #include <fcntl.h>
1296b384f39SPeter Avalos #ifdef HAVE_PATHS_H
1306b384f39SPeter Avalos #include <paths.h>
1316b384f39SPeter Avalos #endif
132e95abc47Szrj #include <signal.h>
1336b384f39SPeter Avalos #include <string.h>
134e95abc47Szrj #include <termios.h>
1356b384f39SPeter Avalos #include <unistd.h>
1366b384f39SPeter Avalos
137e95abc47Szrj #ifndef _PATH_TTY
138e95abc47Szrj #define _PATH_TTY "/dev/tty"
139e95abc47Szrj #endif
140e95abc47Szrj
1416b384f39SPeter Avalos #ifdef TCSASOFT
1426b384f39SPeter Avalos # define _T_FLUSH (TCSAFLUSH|TCSASOFT)
1436b384f39SPeter Avalos #else
1446b384f39SPeter Avalos # define _T_FLUSH (TCSAFLUSH)
1456b384f39SPeter Avalos #endif
1466b384f39SPeter Avalos
1476b384f39SPeter Avalos /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
1486b384f39SPeter Avalos #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
1496b384f39SPeter Avalos # define _POSIX_VDISABLE VDISABLE
1506b384f39SPeter Avalos #endif
1516b384f39SPeter Avalos
152e95abc47Szrj #define M(a,b) (a > b ? a : b)
153e95abc47Szrj #define MAX_SIGNO M(M(M(SIGALRM, SIGHUP), \
154e95abc47Szrj M(SIGINT, SIGPIPE)), \
155e95abc47Szrj M(M(SIGQUIT, SIGTERM), \
156e95abc47Szrj M(M(SIGTSTP, SIGTTIN), SIGTTOU)))
157e95abc47Szrj
158e95abc47Szrj static volatile sig_atomic_t signo[MAX_SIGNO + 1];
1596b384f39SPeter Avalos
1606b384f39SPeter Avalos static void
handler(int s)1616b384f39SPeter Avalos handler(int s)
1626b384f39SPeter Avalos {
163e95abc47Szrj assert(s <= MAX_SIGNO);
1646b384f39SPeter Avalos signo[s] = 1;
1656b384f39SPeter Avalos }
1666b384f39SPeter Avalos
1676b384f39SPeter Avalos static char *
readpassphrase(const char * prompt,char * buf,size_t bufsiz,int flags)1686b384f39SPeter Avalos readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
1696b384f39SPeter Avalos {
1706b384f39SPeter Avalos ssize_t nr;
1716b384f39SPeter Avalos int input, output, save_errno, i, need_restart;
1726b384f39SPeter Avalos char ch, *p, *end;
1736b384f39SPeter Avalos struct termios term, oterm;
1746b384f39SPeter Avalos struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
1756b384f39SPeter Avalos struct sigaction savetstp, savettin, savettou, savepipe;
1766b384f39SPeter Avalos
1776b384f39SPeter Avalos /* I suppose we could alloc on demand in this case (XXX). */
1786b384f39SPeter Avalos if (bufsiz == 0) {
1796b384f39SPeter Avalos errno = EINVAL;
1806b384f39SPeter Avalos return(NULL);
1816b384f39SPeter Avalos }
1826b384f39SPeter Avalos
1836b384f39SPeter Avalos restart:
184e95abc47Szrj for (i = 0; i <= MAX_SIGNO; i++)
1856b384f39SPeter Avalos signo[i] = 0;
1866b384f39SPeter Avalos nr = -1;
1876b384f39SPeter Avalos save_errno = 0;
1886b384f39SPeter Avalos need_restart = 0;
1896b384f39SPeter Avalos /*
1906b384f39SPeter Avalos * Read and write to /dev/tty if available. If not, read from
1916b384f39SPeter Avalos * stdin and write to stderr unless a tty is required.
1926b384f39SPeter Avalos */
1936b384f39SPeter Avalos if ((flags & RPP_STDIN) ||
1946b384f39SPeter Avalos (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
1956b384f39SPeter Avalos if (flags & RPP_REQUIRE_TTY) {
1966b384f39SPeter Avalos errno = ENOTTY;
1976b384f39SPeter Avalos return(NULL);
1986b384f39SPeter Avalos }
1996b384f39SPeter Avalos input = STDIN_FILENO;
2006b384f39SPeter Avalos output = STDERR_FILENO;
2016b384f39SPeter Avalos }
2026b384f39SPeter Avalos
2036b384f39SPeter Avalos /*
204*085658deSDaniel Fojt * Turn off echo if possible.
205*085658deSDaniel Fojt * If we are using a tty but are not the foreground pgrp this will
206*085658deSDaniel Fojt * generate SIGTTOU, so do it *before* installing the signal handlers.
207*085658deSDaniel Fojt */
208*085658deSDaniel Fojt if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
209*085658deSDaniel Fojt memcpy(&term, &oterm, sizeof(term));
210*085658deSDaniel Fojt if (!(flags & RPP_ECHO_ON))
211*085658deSDaniel Fojt term.c_lflag &= ~(ECHO | ECHONL);
212*085658deSDaniel Fojt #ifdef VSTATUS
213*085658deSDaniel Fojt if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
214*085658deSDaniel Fojt term.c_cc[VSTATUS] = _POSIX_VDISABLE;
215*085658deSDaniel Fojt #endif
216*085658deSDaniel Fojt (void)tcsetattr(input, _T_FLUSH, &term);
217*085658deSDaniel Fojt } else {
218*085658deSDaniel Fojt memset(&term, 0, sizeof(term));
219*085658deSDaniel Fojt term.c_lflag |= ECHO;
220*085658deSDaniel Fojt memset(&oterm, 0, sizeof(oterm));
221*085658deSDaniel Fojt oterm.c_lflag |= ECHO;
222*085658deSDaniel Fojt }
223*085658deSDaniel Fojt
224*085658deSDaniel Fojt /*
2256b384f39SPeter Avalos * Catch signals that would otherwise cause the user to end
2266b384f39SPeter Avalos * up with echo turned off in the shell. Don't worry about
2276b384f39SPeter Avalos * things like SIGXCPU and SIGVTALRM for now.
2286b384f39SPeter Avalos */
2296b384f39SPeter Avalos sigemptyset(&sa.sa_mask);
2306b384f39SPeter Avalos sa.sa_flags = 0; /* don't restart system calls */
2316b384f39SPeter Avalos sa.sa_handler = handler;
232e95abc47Szrj /* Keep this list in sync with MAX_SIGNO! */
2336b384f39SPeter Avalos (void)sigaction(SIGALRM, &sa, &savealrm);
2346b384f39SPeter Avalos (void)sigaction(SIGHUP, &sa, &savehup);
2356b384f39SPeter Avalos (void)sigaction(SIGINT, &sa, &saveint);
2366b384f39SPeter Avalos (void)sigaction(SIGPIPE, &sa, &savepipe);
2376b384f39SPeter Avalos (void)sigaction(SIGQUIT, &sa, &savequit);
2386b384f39SPeter Avalos (void)sigaction(SIGTERM, &sa, &saveterm);
2396b384f39SPeter Avalos (void)sigaction(SIGTSTP, &sa, &savetstp);
2406b384f39SPeter Avalos (void)sigaction(SIGTTIN, &sa, &savettin);
2416b384f39SPeter Avalos (void)sigaction(SIGTTOU, &sa, &savettou);
2426b384f39SPeter Avalos
2436b384f39SPeter Avalos if (!(flags & RPP_STDIN)) {
2446b384f39SPeter Avalos int r = write(output, prompt, strlen(prompt));
2456b384f39SPeter Avalos (void)r;
2466b384f39SPeter Avalos }
2476b384f39SPeter Avalos end = buf + bufsiz - 1;
2486b384f39SPeter Avalos p = buf;
2496b384f39SPeter Avalos while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
2506b384f39SPeter Avalos if (p < end) {
2516b384f39SPeter Avalos if ((flags & RPP_SEVENBIT))
2526b384f39SPeter Avalos ch &= 0x7f;
253e95abc47Szrj if (isalpha((unsigned char)ch)) {
2546b384f39SPeter Avalos if ((flags & RPP_FORCELOWER))
255e95abc47Szrj ch = (char)tolower((unsigned char)ch);
2566b384f39SPeter Avalos if ((flags & RPP_FORCEUPPER))
257e95abc47Szrj ch = (char)toupper((unsigned char)ch);
2586b384f39SPeter Avalos }
2596b384f39SPeter Avalos *p++ = ch;
2606b384f39SPeter Avalos }
2616b384f39SPeter Avalos }
2626b384f39SPeter Avalos *p = '\0';
2636b384f39SPeter Avalos save_errno = errno;
2646b384f39SPeter Avalos if (!(term.c_lflag & ECHO)) {
2656b384f39SPeter Avalos int r = write(output, "\n", 1);
2666b384f39SPeter Avalos (void)r;
2676b384f39SPeter Avalos }
2686b384f39SPeter Avalos
2696b384f39SPeter Avalos /* Restore old terminal settings and signals. */
2706b384f39SPeter Avalos if (memcmp(&term, &oterm, sizeof(term)) != 0) {
271*085658deSDaniel Fojt const int sigttou = signo[SIGTTOU];
272*085658deSDaniel Fojt
273*085658deSDaniel Fojt /* Ignore SIGTTOU generated when we are not the fg pgrp. */
2746b384f39SPeter Avalos while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
275*085658deSDaniel Fojt errno == EINTR && !signo[SIGTTOU])
2766b384f39SPeter Avalos continue;
277*085658deSDaniel Fojt signo[SIGTTOU] = sigttou;
2786b384f39SPeter Avalos }
2796b384f39SPeter Avalos (void)sigaction(SIGALRM, &savealrm, NULL);
2806b384f39SPeter Avalos (void)sigaction(SIGHUP, &savehup, NULL);
2816b384f39SPeter Avalos (void)sigaction(SIGINT, &saveint, NULL);
2826b384f39SPeter Avalos (void)sigaction(SIGQUIT, &savequit, NULL);
2836b384f39SPeter Avalos (void)sigaction(SIGPIPE, &savepipe, NULL);
2846b384f39SPeter Avalos (void)sigaction(SIGTERM, &saveterm, NULL);
2856b384f39SPeter Avalos (void)sigaction(SIGTSTP, &savetstp, NULL);
2866b384f39SPeter Avalos (void)sigaction(SIGTTIN, &savettin, NULL);
2876b384f39SPeter Avalos (void)sigaction(SIGTTOU, &savettou, NULL);
2886b384f39SPeter Avalos if (input != STDIN_FILENO)
2896b384f39SPeter Avalos (void)close(input);
2906b384f39SPeter Avalos
2916b384f39SPeter Avalos /*
2926b384f39SPeter Avalos * If we were interrupted by a signal, resend it to ourselves
2936b384f39SPeter Avalos * now that we have restored the signal handlers.
2946b384f39SPeter Avalos */
295e95abc47Szrj for (i = 0; i <= MAX_SIGNO; i++) {
2966b384f39SPeter Avalos if (signo[i]) {
2976b384f39SPeter Avalos kill(getpid(), i);
2986b384f39SPeter Avalos switch (i) {
2996b384f39SPeter Avalos case SIGTSTP:
3006b384f39SPeter Avalos case SIGTTIN:
3016b384f39SPeter Avalos case SIGTTOU:
3026b384f39SPeter Avalos need_restart = 1;
3036b384f39SPeter Avalos }
3046b384f39SPeter Avalos }
3056b384f39SPeter Avalos }
3066b384f39SPeter Avalos if (need_restart)
3076b384f39SPeter Avalos goto restart;
3086b384f39SPeter Avalos
3096b384f39SPeter Avalos if (save_errno)
3106b384f39SPeter Avalos errno = save_errno;
3116b384f39SPeter Avalos return(nr == -1 ? NULL : buf);
3126b384f39SPeter Avalos }
3136b384f39SPeter Avalos #endif /* _WIN32 && !__CYGWIN__ */
3146b384f39SPeter Avalos #endif /* HAVE_READPASSPHRASE */
3156b384f39SPeter Avalos
3166b384f39SPeter Avalos char *
lafe_readpassphrase(const char * prompt,char * buf,size_t bufsiz)3176b384f39SPeter Avalos lafe_readpassphrase(const char *prompt, char *buf, size_t bufsiz)
3186b384f39SPeter Avalos {
3196b384f39SPeter Avalos char *p;
3206b384f39SPeter Avalos
3216b384f39SPeter Avalos p = readpassphrase(prompt, buf, bufsiz, RPP_ECHO_OFF);
3226b384f39SPeter Avalos if (p == NULL) {
3236b384f39SPeter Avalos switch (errno) {
3246b384f39SPeter Avalos case EINTR:
3256b384f39SPeter Avalos break;
3266b384f39SPeter Avalos default:
3276b384f39SPeter Avalos lafe_errc(1, errno, "Couldn't read passphrase");
3286b384f39SPeter Avalos break;
3296b384f39SPeter Avalos }
3306b384f39SPeter Avalos }
3316b384f39SPeter Avalos return (p);
3326b384f39SPeter Avalos }
3336b384f39SPeter Avalos
334