Path: blob/main/contrib/libarchive/libarchive_fe/passphrase.c
39507 views
/*-1* Copyright (c) 2014 Michihiro NAKAJIMA2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer9* in this position and unchanged.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/25/* $OpenBSD: readpassphrase.c,v 1.27 2019/01/25 00:19:25 millert Exp $ */2627/*28* Copyright (c) 2000-2002, 2007, 201029* Todd C. Miller <[email protected]>30*31* Permission to use, copy, modify, and distribute this software for any32* purpose with or without fee is hereby granted, provided that the above33* copyright notice and this permission notice appear in all copies.34*35* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES36* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF37* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR38* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES39* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN40* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF41* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.42*43* Sponsored in part by the Defense Advanced Research Projects44* Agency (DARPA) and Air Force Research Laboratory, Air Force45* Materiel Command, USAF, under agreement number F39502-99-1-0512.46*/4748/* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */495051#include "lafe_platform.h"52#include <errno.h>53#ifdef HAVE_STDLIB_H54#include <stdlib.h>55#endif56#ifdef HAVE_UNISTD_H57#include <unistd.h>58#endif59#ifdef HAVE_READPASSPHRASE_H60#include <readpassphrase.h>61#endif6263#include "err.h"64#include "passphrase.h"6566#ifndef HAVE_READPASSPHRASE6768#define RPP_ECHO_OFF 0x00 /* Turn off echo (default). */69#define RPP_ECHO_ON 0x01 /* Leave echo on. */70#define RPP_REQUIRE_TTY 0x02 /* Fail if there is no tty. */71#define RPP_FORCELOWER 0x04 /* Force input to lower case. */72#define RPP_FORCEUPPER 0x08 /* Force input to upper case. */73#define RPP_SEVENBIT 0x10 /* Strip the high bit from input. */74#define RPP_STDIN 0x20 /* Read from stdin, not /dev/tty */757677#if defined(_WIN32) && !defined(__CYGWIN__)78#include <string.h>79#include <windows.h>8081static char *82readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)83{84HANDLE hStdin, hStdout;85DWORD mode, rbytes;86BOOL success;8788(void)flags;8990hStdin = GetStdHandle(STD_INPUT_HANDLE);91if (hStdin == INVALID_HANDLE_VALUE)92return (NULL);93hStdout = GetStdHandle(STD_OUTPUT_HANDLE);94if (hStdout == INVALID_HANDLE_VALUE)95return (NULL);9697success = GetConsoleMode(hStdin, &mode);98if (!success)99return (NULL);100mode &= ~ENABLE_ECHO_INPUT;101mode |= ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;102success = SetConsoleMode(hStdin, mode);103if (!success)104return (NULL);105106success = WriteFile(hStdout, prompt, (DWORD)strlen(prompt),107NULL, NULL);108if (!success)109return (NULL);110success = ReadFile(hStdin, buf, (DWORD)bufsiz - 1, &rbytes, NULL);111if (!success)112return (NULL);113WriteFile(hStdout, "\r\n", 2, NULL, NULL);114buf[rbytes] = '\0';115/* Remove trailing carriage return(s). */116buf[strcspn(buf, "\r\n")] = '\0';117118return (buf);119}120121#elif defined(HAVE_TCGETATTR) && defined(HAVE_TCSETATTR)122123#include <assert.h>124#include <ctype.h>125#include <fcntl.h>126#ifdef HAVE_PATHS_H127#include <paths.h>128#endif129#include <signal.h>130#include <string.h>131#include <termios.h>132#include <unistd.h>133134#ifndef _PATH_TTY135#define _PATH_TTY "/dev/tty"136#endif137138#ifdef TCSASOFT139# define _T_FLUSH (TCSAFLUSH|TCSASOFT)140#else141# define _T_FLUSH (TCSAFLUSH)142#endif143144/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */145#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)146# define _POSIX_VDISABLE VDISABLE147#endif148149#define M(a,b) (a > b ? a : b)150#define MAX_SIGNO M(M(M(SIGALRM, SIGHUP), \151M(SIGINT, SIGPIPE)), \152M(M(SIGQUIT, SIGTERM), \153M(M(SIGTSTP, SIGTTIN), SIGTTOU)))154155static volatile sig_atomic_t signo[MAX_SIGNO + 1];156157static void158handler(int s)159{160assert(s <= MAX_SIGNO);161signo[s] = 1;162}163164static char *165readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)166{167ssize_t nr;168int input, output, save_errno, i, need_restart;169char ch, *p, *end;170struct termios term, oterm;171#ifdef HAVE_SIGACTION172struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;173struct sigaction savetstp, savettin, savettou, savepipe;174#endif175176/* I suppose we could alloc on demand in this case (XXX). */177if (bufsiz == 0) {178errno = EINVAL;179return(NULL);180}181182restart:183for (i = 0; i <= MAX_SIGNO; i++)184signo[i] = 0;185nr = -1;186save_errno = 0;187need_restart = 0;188/*189* Read and write to /dev/tty if available. If not, read from190* stdin and write to stderr unless a tty is required.191*/192if ((flags & RPP_STDIN) ||193(input = output = open(_PATH_TTY, O_RDWR)) == -1) {194if (flags & RPP_REQUIRE_TTY) {195errno = ENOTTY;196return(NULL);197}198input = STDIN_FILENO;199output = STDERR_FILENO;200}201202/*203* Turn off echo if possible.204* If we are using a tty but are not the foreground pgrp this will205* generate SIGTTOU, so do it *before* installing the signal handlers.206*/207if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {208memcpy(&term, &oterm, sizeof(term));209if (!(flags & RPP_ECHO_ON))210term.c_lflag &= ~(ECHO | ECHONL);211#ifdef VSTATUS212if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)213term.c_cc[VSTATUS] = _POSIX_VDISABLE;214#endif215(void)tcsetattr(input, _T_FLUSH, &term);216} else {217memset(&term, 0, sizeof(term));218term.c_lflag |= ECHO;219memset(&oterm, 0, sizeof(oterm));220oterm.c_lflag |= ECHO;221}222223#ifdef HAVE_SIGACTION224/*225* Catch signals that would otherwise cause the user to end226* up with echo turned off in the shell. Don't worry about227* things like SIGXCPU and SIGVTALRM for now.228*/229sigemptyset(&sa.sa_mask);230sa.sa_flags = 0; /* don't restart system calls */231sa.sa_handler = handler;232/* Keep this list in sync with MAX_SIGNO! */233(void)sigaction(SIGALRM, &sa, &savealrm);234(void)sigaction(SIGHUP, &sa, &savehup);235(void)sigaction(SIGINT, &sa, &saveint);236(void)sigaction(SIGPIPE, &sa, &savepipe);237(void)sigaction(SIGQUIT, &sa, &savequit);238(void)sigaction(SIGTERM, &sa, &saveterm);239(void)sigaction(SIGTSTP, &sa, &savetstp);240(void)sigaction(SIGTTIN, &sa, &savettin);241(void)sigaction(SIGTTOU, &sa, &savettou);242#endif243244if (!(flags & RPP_STDIN)) {245int r = write(output, prompt, strlen(prompt));246(void)r;247}248end = buf + bufsiz - 1;249p = buf;250while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {251if (p < end) {252if ((flags & RPP_SEVENBIT))253ch &= 0x7f;254if (isalpha((unsigned char)ch)) {255if ((flags & RPP_FORCELOWER))256ch = (char)tolower((unsigned char)ch);257if ((flags & RPP_FORCEUPPER))258ch = (char)toupper((unsigned char)ch);259}260*p++ = ch;261}262}263*p = '\0';264save_errno = errno;265if (!(term.c_lflag & ECHO)) {266int r = write(output, "\n", 1);267(void)r;268}269270/* Restore old terminal settings and signals. */271if (memcmp(&term, &oterm, sizeof(term)) != 0) {272const int sigttou = signo[SIGTTOU];273274/* Ignore SIGTTOU generated when we are not the fg pgrp. */275while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&276errno == EINTR && !signo[SIGTTOU])277continue;278signo[SIGTTOU] = sigttou;279}280#ifdef HAVE_SIGACTION281(void)sigaction(SIGALRM, &savealrm, NULL);282(void)sigaction(SIGHUP, &savehup, NULL);283(void)sigaction(SIGINT, &saveint, NULL);284(void)sigaction(SIGQUIT, &savequit, NULL);285(void)sigaction(SIGPIPE, &savepipe, NULL);286(void)sigaction(SIGTERM, &saveterm, NULL);287(void)sigaction(SIGTSTP, &savetstp, NULL);288(void)sigaction(SIGTTIN, &savettin, NULL);289(void)sigaction(SIGTTOU, &savettou, NULL);290#endif291if (input != STDIN_FILENO)292(void)close(input);293294/*295* If we were interrupted by a signal, resend it to ourselves296* now that we have restored the signal handlers.297*/298for (i = 0; i <= MAX_SIGNO; i++) {299if (signo[i]) {300kill(getpid(), i);301switch (i) {302case SIGTSTP:303case SIGTTIN:304case SIGTTOU:305need_restart = 1;306}307}308}309if (need_restart)310goto restart;311312if (save_errno)313errno = save_errno;314return(nr == -1 ? NULL : buf);315}316#else317static char *318readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)319{320return (NULL);321}322#endif323#endif /* HAVE_READPASSPHRASE */324325char *326lafe_readpassphrase(const char *prompt, char *buf, size_t bufsiz)327{328char *p;329330p = readpassphrase(prompt, buf, bufsiz, RPP_ECHO_OFF);331if (p == NULL) {332switch (errno) {333case EINTR:334break;335default:336lafe_errc(1, errno, "Couldn't read passphrase");337/* NOTREACHED */338}339}340return (p);341}342343344345