summaryrefslogtreecommitdiff
path: root/openbsd-compat/readpassphrase.c
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/readpassphrase.c')
-rw-r--r--openbsd-compat/readpassphrase.c35
1 files changed, 21 insertions, 14 deletions
diff --git a/openbsd-compat/readpassphrase.c b/openbsd-compat/readpassphrase.c
index 8c2f5f841..4e549b62b 100644
--- a/openbsd-compat/readpassphrase.c
+++ b/openbsd-compat/readpassphrase.c
@@ -1,7 +1,7 @@
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 2
3/* 3/*
4 * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com> 4 * Copyright (c) 2000-2002 Todd C. Miller <Todd.Miller@courtesan.com>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
@@ -28,7 +28,7 @@
28 */ 28 */
29 29
30#if defined(LIBC_SCCS) && !defined(lint) 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 */ 32#endif /* LIBC_SCCS and not lint */
33 33
34#include "includes.h" 34#include "includes.h"
@@ -60,8 +60,8 @@ readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
60 int input, output, save_errno; 60 int input, output, save_errno;
61 char ch, *p, *end; 61 char ch, *p, *end;
62 struct termios term, oterm; 62 struct termios term, oterm;
63 struct sigaction sa, saveint, savehup, savequit, saveterm; 63 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
64 struct sigaction savetstp, savettin, savettou; 64 struct sigaction savetstp, savettin, savettou, savepipe;
65 65
66 /* I suppose we could alloc on demand in this case (XXX). */ 66 /* I suppose we could alloc on demand in this case (XXX). */
67 if (bufsiz == 0) { 67 if (bufsiz == 0) {
@@ -70,11 +70,13 @@ readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
70 } 70 }
71 71
72restart: 72restart:
73 signo = 0;
73 /* 74 /*
74 * Read and write to /dev/tty if available. If not, read from 75 * Read and write to /dev/tty if available. If not, read from
75 * stdin and write to stderr unless a tty is required. 76 * stdin and write to stderr unless a tty is required.
76 */ 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) { 80 if (flags & RPP_REQUIRE_TTY) {
79 errno = ENOTTY; 81 errno = ENOTTY;
80 return(NULL); 82 return(NULL);
@@ -86,13 +88,15 @@ restart:
86 /* 88 /*
87 * Catch signals that would otherwise cause the user to end 89 * Catch signals that would otherwise cause the user to end
88 * up with echo turned off in the shell. Don't worry about 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 */ 92 */
91 sigemptyset(&sa.sa_mask); 93 sigemptyset(&sa.sa_mask);
92 sa.sa_flags = 0; /* don't restart system calls */ 94 sa.sa_flags = 0; /* don't restart system calls */
93 sa.sa_handler = handler; 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); 100 (void)sigaction(SIGQUIT, &sa, &savequit);
97 (void)sigaction(SIGTERM, &sa, &saveterm); 101 (void)sigaction(SIGTERM, &sa, &saveterm);
98 (void)sigaction(SIGTSTP, &sa, &savetstp); 102 (void)sigaction(SIGTSTP, &sa, &savetstp);
@@ -100,7 +104,7 @@ restart:
100 (void)sigaction(SIGTTOU, &sa, &savettou); 104 (void)sigaction(SIGTTOU, &sa, &savettou);
101 105
102 /* Turn off echo if possible. */ 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)); 108 memcpy(&term, &oterm, sizeof(term));
105 if (!(flags & RPP_ECHO_ON)) 109 if (!(flags & RPP_ECHO_ON))
106 term.c_lflag &= ~(ECHO | ECHONL); 110 term.c_lflag &= ~(ECHO | ECHONL);
@@ -111,10 +115,13 @@ restart:
111 (void)tcsetattr(input, _T_FLUSH, &term); 115 (void)tcsetattr(input, _T_FLUSH, &term);
112 } else { 116 } else {
113 memset(&term, 0, sizeof(term)); 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 } 121 }
116 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; 125 end = buf + bufsiz - 1;
119 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) { 126 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
120 if (p < end) { 127 if (p < end) {
@@ -137,13 +144,14 @@ restart:
137 /* Restore old terminal settings and signals. */ 144 /* Restore old terminal settings and signals. */
138 if (memcmp(&term, &oterm, sizeof(term)) != 0) 145 if (memcmp(&term, &oterm, sizeof(term)) != 0)
139 (void)tcsetattr(input, _T_FLUSH, &oterm); 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); 152 (void)sigaction(SIGTERM, &saveterm, NULL);
144 (void)sigaction(SIGTSTP, &savetstp, NULL); 153 (void)sigaction(SIGTSTP, &savetstp, NULL);
145 (void)sigaction(SIGTTIN, &savettin, NULL); 154 (void)sigaction(SIGTTIN, &savettin, NULL);
146 (void)sigaction(SIGTTOU, &savettou, NULL);
147 if (input != STDIN_FILENO) 155 if (input != STDIN_FILENO)
148 (void)close(input); 156 (void)close(input);
149 157
@@ -152,12 +160,11 @@ restart:
152 * now that we have restored the signal handlers. 160 * now that we have restored the signal handlers.
153 */ 161 */
154 if (signo) { 162 if (signo) {
155 kill(getpid(), signo); 163 kill(getpid(), signo);
156 switch (signo) { 164 switch (signo) {
157 case SIGTSTP: 165 case SIGTSTP:
158 case SIGTTIN: 166 case SIGTTIN:
159 case SIGTTOU: 167 case SIGTTOU:
160 signo = 0;
161 goto restart; 168 goto restart;
162 } 169 }
163 } 170 }