readpassphrase.c (98937) | readpassphrase.c (106121) |
---|---|
1/* $OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $ */ | 1/* $OpenBSD: readpassphrase.c,v 1.14 2002/06/28 01:43:58 millert Exp $ */ |
2 3/* | 2 3/* |
4 * Copyright (c) 2000 Todd C. Miller | 4 * Copyright (c) 2000-2002 Todd C. Miller <Todd.Miller@courtesan.com> |
5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright --- 10 unchanged lines hidden (view full) --- 23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#if defined(LIBC_SCCS) && !defined(lint) | 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright --- 10 unchanged lines hidden (view full) --- 23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#if defined(LIBC_SCCS) && !defined(lint) |
31static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $"; | 31static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.14 2002/06/28 01:43:58 millert Exp $"; |
32#endif /* LIBC_SCCS and not lint */ 33 34#include "includes.h" 35 36#ifndef HAVE_READPASSPHRASE 37 38#include <termios.h> 39#include <readpassphrase.h> --- 15 unchanged lines hidden (view full) --- 55 56char * 57readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) 58{ 59 ssize_t nr; 60 int input, output, save_errno; 61 char ch, *p, *end; 62 struct termios term, oterm; | 32#endif /* LIBC_SCCS and not lint */ 33 34#include "includes.h" 35 36#ifndef HAVE_READPASSPHRASE 37 38#include <termios.h> 39#include <readpassphrase.h> --- 15 unchanged lines hidden (view full) --- 55 56char * 57readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) 58{ 59 ssize_t nr; 60 int input, output, save_errno; 61 char ch, *p, *end; 62 struct termios term, oterm; |
63 struct sigaction sa, saveint, savehup, savequit, saveterm; 64 struct sigaction savetstp, savettin, savettou; | 63 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm; 64 struct sigaction savetstp, savettin, savettou, savepipe; |
65 66 /* I suppose we could alloc on demand in this case (XXX). */ 67 if (bufsiz == 0) { 68 errno = EINVAL; 69 return(NULL); 70 } 71 72restart: | 65 66 /* I suppose we could alloc on demand in this case (XXX). */ 67 if (bufsiz == 0) { 68 errno = EINVAL; 69 return(NULL); 70 } 71 72restart: |
73 signo = 0; |
|
73 /* 74 * Read and write to /dev/tty if available. If not, read from 75 * stdin and write to stderr unless a tty is required. 76 */ | 74 /* 75 * Read and write to /dev/tty if available. If not, read from 76 * stdin and write to stderr unless a tty is required. 77 */ |
77 if ((input = output = open(_PATH_TTY, O_RDWR)) == -1) { | 78 if ((flags & RPP_STDIN) || 79 (input = output = open(_PATH_TTY, O_RDWR)) == -1) { |
78 if (flags & RPP_REQUIRE_TTY) { 79 errno = ENOTTY; 80 return(NULL); 81 } 82 input = STDIN_FILENO; 83 output = STDERR_FILENO; 84 } 85 86 /* 87 * Catch signals that would otherwise cause the user to end 88 * up with echo turned off in the shell. Don't worry about | 80 if (flags & RPP_REQUIRE_TTY) { 81 errno = ENOTTY; 82 return(NULL); 83 } 84 input = STDIN_FILENO; 85 output = STDERR_FILENO; 86 } 87 88 /* 89 * Catch signals that would otherwise cause the user to end 90 * up with echo turned off in the shell. Don't worry about |
89 * things like SIGALRM and SIGPIPE for now. | 91 * things like SIGXCPU and SIGVTALRM for now. |
90 */ 91 sigemptyset(&sa.sa_mask); 92 sa.sa_flags = 0; /* don't restart system calls */ 93 sa.sa_handler = handler; | 92 */ 93 sigemptyset(&sa.sa_mask); 94 sa.sa_flags = 0; /* don't restart system calls */ 95 sa.sa_handler = handler; |
94 (void)sigaction(SIGINT, &sa, &saveint); | 96 (void)sigaction(SIGALRM, &sa, &savealrm); |
95 (void)sigaction(SIGHUP, &sa, &savehup); | 97 (void)sigaction(SIGHUP, &sa, &savehup); |
98 (void)sigaction(SIGINT, &sa, &saveint); 99 (void)sigaction(SIGPIPE, &sa, &savepipe); |
|
96 (void)sigaction(SIGQUIT, &sa, &savequit); 97 (void)sigaction(SIGTERM, &sa, &saveterm); 98 (void)sigaction(SIGTSTP, &sa, &savetstp); 99 (void)sigaction(SIGTTIN, &sa, &savettin); 100 (void)sigaction(SIGTTOU, &sa, &savettou); 101 102 /* Turn off echo if possible. */ | 100 (void)sigaction(SIGQUIT, &sa, &savequit); 101 (void)sigaction(SIGTERM, &sa, &saveterm); 102 (void)sigaction(SIGTSTP, &sa, &savetstp); 103 (void)sigaction(SIGTTIN, &sa, &savettin); 104 (void)sigaction(SIGTTOU, &sa, &savettou); 105 106 /* Turn off echo if possible. */ |
103 if (tcgetattr(input, &oterm) == 0) { | 107 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) { |
104 memcpy(&term, &oterm, sizeof(term)); 105 if (!(flags & RPP_ECHO_ON)) 106 term.c_lflag &= ~(ECHO | ECHONL); 107#ifdef VSTATUS 108 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) 109 term.c_cc[VSTATUS] = _POSIX_VDISABLE; 110#endif 111 (void)tcsetattr(input, _T_FLUSH, &term); 112 } else { 113 memset(&term, 0, sizeof(term)); | 108 memcpy(&term, &oterm, sizeof(term)); 109 if (!(flags & RPP_ECHO_ON)) 110 term.c_lflag &= ~(ECHO | ECHONL); 111#ifdef VSTATUS 112 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) 113 term.c_cc[VSTATUS] = _POSIX_VDISABLE; 114#endif 115 (void)tcsetattr(input, _T_FLUSH, &term); 116 } else { 117 memset(&term, 0, sizeof(term)); |
118 term.c_lflag |= ECHO; |
|
114 memset(&oterm, 0, sizeof(oterm)); | 119 memset(&oterm, 0, sizeof(oterm)); |
120 oterm.c_lflag |= ECHO; |
|
115 } 116 | 121 } 122 |
117 (void)write(output, prompt, strlen(prompt)); | 123 if (!(flags & RPP_STDIN)) 124 (void)write(output, prompt, strlen(prompt)); |
118 end = buf + bufsiz - 1; 119 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) { 120 if (p < end) { 121 if ((flags & RPP_SEVENBIT)) 122 ch &= 0x7f; 123 if (isalpha(ch)) { 124 if ((flags & RPP_FORCELOWER)) 125 ch = tolower(ch); --- 6 unchanged lines hidden (view full) --- 132 *p = '\0'; 133 save_errno = errno; 134 if (!(term.c_lflag & ECHO)) 135 (void)write(output, "\n", 1); 136 137 /* Restore old terminal settings and signals. */ 138 if (memcmp(&term, &oterm, sizeof(term)) != 0) 139 (void)tcsetattr(input, _T_FLUSH, &oterm); | 125 end = buf + bufsiz - 1; 126 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) { 127 if (p < end) { 128 if ((flags & RPP_SEVENBIT)) 129 ch &= 0x7f; 130 if (isalpha(ch)) { 131 if ((flags & RPP_FORCELOWER)) 132 ch = tolower(ch); --- 6 unchanged lines hidden (view full) --- 139 *p = '\0'; 140 save_errno = errno; 141 if (!(term.c_lflag & ECHO)) 142 (void)write(output, "\n", 1); 143 144 /* Restore old terminal settings and signals. */ 145 if (memcmp(&term, &oterm, sizeof(term)) != 0) 146 (void)tcsetattr(input, _T_FLUSH, &oterm); |
140 (void)sigaction(SIGINT, &saveint, NULL); | 147 (void)sigaction(SIGALRM, &savealrm, NULL); |
141 (void)sigaction(SIGHUP, &savehup, NULL); | 148 (void)sigaction(SIGHUP, &savehup, NULL); |
149 (void)sigaction(SIGINT, &saveint, NULL); |
|
142 (void)sigaction(SIGQUIT, &savequit, NULL); | 150 (void)sigaction(SIGQUIT, &savequit, NULL); |
151 (void)sigaction(SIGPIPE, &savepipe, NULL); |
|
143 (void)sigaction(SIGTERM, &saveterm, NULL); 144 (void)sigaction(SIGTSTP, &savetstp, NULL); 145 (void)sigaction(SIGTTIN, &savettin, NULL); | 152 (void)sigaction(SIGTERM, &saveterm, NULL); 153 (void)sigaction(SIGTSTP, &savetstp, NULL); 154 (void)sigaction(SIGTTIN, &savettin, NULL); |
146 (void)sigaction(SIGTTOU, &savettou, NULL); | |
147 if (input != STDIN_FILENO) 148 (void)close(input); 149 150 /* 151 * If we were interrupted by a signal, resend it to ourselves 152 * now that we have restored the signal handlers. 153 */ 154 if (signo) { | 155 if (input != STDIN_FILENO) 156 (void)close(input); 157 158 /* 159 * If we were interrupted by a signal, resend it to ourselves 160 * now that we have restored the signal handlers. 161 */ 162 if (signo) { |
155 kill(getpid(), signo); | 163 kill(getpid(), signo); |
156 switch (signo) { 157 case SIGTSTP: 158 case SIGTTIN: 159 case SIGTTOU: | 164 switch (signo) { 165 case SIGTSTP: 166 case SIGTTIN: 167 case SIGTTOU: |
160 signo = 0; | |
161 goto restart; 162 } 163 } 164 165 errno = save_errno; 166 return(nr == -1 ? NULL : buf); 167} 168 --- 15 unchanged lines hidden --- | 168 goto restart; 169 } 170 } 171 172 errno = save_errno; 173 return(nr == -1 ? NULL : buf); 174} 175 --- 15 unchanged lines hidden --- |