Path: blob/main/contrib/libfido2/openbsd-compat/readpassphrase.c
39536 views
/* $OpenBSD: readpassphrase.c,v 1.26 2016/10/18 12:47:18 millert Exp $ */12/*3* Copyright (c) 2000-2002, 2007, 20104* Todd C. Miller <[email protected]>5*6* Permission to use, copy, modify, and distribute this software for any7* purpose with or without fee is hereby granted, provided that the above8* copyright notice and this permission notice appear in all copies.9*10* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES11* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR13* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF16* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17*18* Sponsored in part by the Defense Advanced Research Projects19* Agency (DARPA) and Air Force Research Laboratory, Air Force20* Materiel Command, USAF, under agreement number F39502-99-1-0512.21*/2223/* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */2425#include "openbsd-compat.h"2627#ifndef HAVE_READPASSPHRASE2829#include <termios.h>30#include <signal.h>31#include <ctype.h>32#include <fcntl.h>33#include <errno.h>34#include <string.h>35#ifdef HAVE_UNISTD_H36#include <unistd.h>37#endif38#include <paths.h>3940#ifndef _PATH_TTY41# define _PATH_TTY "/dev/tty"42#endif4344#ifndef TCSASOFT45/* If we don't have TCSASOFT define it so that ORing it it below is a no-op. */46# define TCSASOFT 047#endif4849/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */50#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)51# define _POSIX_VDISABLE VDISABLE52#endif5354static volatile sig_atomic_t signo[NSIG];5556static void handler(int);5758char *59readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)60{61ssize_t nr;62int input, output, save_errno, i, need_restart;63char ch, *p, *end;64struct termios term, oterm;65struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;66struct sigaction savetstp, savettin, savettou, savepipe;6768/* I suppose we could alloc on demand in this case (XXX). */69if (bufsiz == 0) {70errno = EINVAL;71return(NULL);72}7374restart:75for (i = 0; i < NSIG; i++)76signo[i] = 0;77need_restart = 0;78/*79* Read and write to /dev/tty if available. If not, read from80* stdin and write to stderr unless a tty is required.81*/82if ((flags & RPP_STDIN) ||83(input = output = open(_PATH_TTY, O_RDWR)) == -1) {84if (flags & RPP_REQUIRE_TTY) {85errno = ENOTTY;86return(NULL);87}88input = STDIN_FILENO;89output = STDERR_FILENO;90}9192/*93* Turn off echo if possible.94* If we are using a tty but are not the foreground pgrp this will95* generate SIGTTOU, so do it *before* installing the signal handlers.96*/97if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {98memcpy(&term, &oterm, sizeof(term));99if (!(flags & RPP_ECHO_ON))100term.c_lflag &= ~(ECHO | ECHONL);101#ifdef VSTATUS102if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)103term.c_cc[VSTATUS] = _POSIX_VDISABLE;104#endif105(void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);106} else {107memset(&term, 0, sizeof(term));108term.c_lflag |= ECHO;109memset(&oterm, 0, sizeof(oterm));110oterm.c_lflag |= ECHO;111}112113/*114* Catch signals that would otherwise cause the user to end115* up with echo turned off in the shell. Don't worry about116* things like SIGXCPU and SIGVTALRM for now.117*/118sigemptyset(&sa.sa_mask);119sa.sa_flags = 0; /* don't restart system calls */120sa.sa_handler = handler;121(void)sigaction(SIGALRM, &sa, &savealrm);122(void)sigaction(SIGHUP, &sa, &savehup);123(void)sigaction(SIGINT, &sa, &saveint);124(void)sigaction(SIGPIPE, &sa, &savepipe);125(void)sigaction(SIGQUIT, &sa, &savequit);126(void)sigaction(SIGTERM, &sa, &saveterm);127(void)sigaction(SIGTSTP, &sa, &savetstp);128(void)sigaction(SIGTTIN, &sa, &savettin);129(void)sigaction(SIGTTOU, &sa, &savettou);130131if (!(flags & RPP_STDIN))132(void)write(output, prompt, strlen(prompt));133end = buf + bufsiz - 1;134p = buf;135while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {136if (p < end) {137if ((flags & RPP_SEVENBIT))138ch &= 0x7f;139if (isalpha((unsigned char)ch)) {140if ((flags & RPP_FORCELOWER))141ch = (char)tolower((unsigned char)ch);142if ((flags & RPP_FORCEUPPER))143ch = (char)toupper((unsigned char)ch);144}145*p++ = ch;146}147}148*p = '\0';149save_errno = errno;150if (!(term.c_lflag & ECHO))151(void)write(output, "\n", 1);152153/* Restore old terminal settings and signals. */154if (memcmp(&term, &oterm, sizeof(term)) != 0) {155const int sigttou = signo[SIGTTOU];156157/* Ignore SIGTTOU generated when we are not the fg pgrp. */158while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&159errno == EINTR && !signo[SIGTTOU])160continue;161signo[SIGTTOU] = sigttou;162}163(void)sigaction(SIGALRM, &savealrm, NULL);164(void)sigaction(SIGHUP, &savehup, NULL);165(void)sigaction(SIGINT, &saveint, NULL);166(void)sigaction(SIGQUIT, &savequit, NULL);167(void)sigaction(SIGPIPE, &savepipe, NULL);168(void)sigaction(SIGTERM, &saveterm, NULL);169(void)sigaction(SIGTSTP, &savetstp, NULL);170(void)sigaction(SIGTTIN, &savettin, NULL);171(void)sigaction(SIGTTOU, &savettou, NULL);172if (input != STDIN_FILENO)173(void)close(input);174175/*176* If we were interrupted by a signal, resend it to ourselves177* now that we have restored the signal handlers.178*/179for (i = 0; i < NSIG; i++) {180if (signo[i]) {181kill(getpid(), i);182switch (i) {183case SIGTSTP:184case SIGTTIN:185case SIGTTOU:186need_restart = 1;187}188}189}190if (need_restart)191goto restart;192193if (save_errno)194errno = save_errno;195return(nr == -1 ? NULL : buf);196}197198#if 0199char *200getpass(const char *prompt)201{202static char buf[_PASSWORD_LEN + 1];203204return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));205}206#endif207208static void handler(int s)209{210211signo[s] = 1;212}213#endif /* HAVE_READPASSPHRASE */214215216