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.c78
1 files changed, 46 insertions, 32 deletions
diff --git a/openbsd-compat/readpassphrase.c b/openbsd-compat/readpassphrase.c
index 11bd8f646..62b6d0d84 100644
--- a/openbsd-compat/readpassphrase.c
+++ b/openbsd-compat/readpassphrase.c
@@ -1,7 +1,7 @@
1/* $OpenBSD: readpassphrase.c,v 1.18 2005/08/08 08:05:34 espie Exp $ */ 1/* $OpenBSD: readpassphrase.c,v 1.22 2010/01/13 10:20:54 dtucker Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2000-2002 Todd C. Miller <Todd.Miller@courtesan.com> 4 * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
5 * 5 *
6 * Permission to use, copy, modify, and distribute this software for any 6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
@@ -46,7 +46,7 @@
46# define _POSIX_VDISABLE VDISABLE 46# define _POSIX_VDISABLE VDISABLE
47#endif 47#endif
48 48
49static volatile sig_atomic_t signo; 49static volatile sig_atomic_t signo[_NSIG];
50 50
51static void handler(int); 51static void handler(int);
52 52
@@ -54,7 +54,7 @@ char *
54readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) 54readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
55{ 55{
56 ssize_t nr; 56 ssize_t nr;
57 int input, output, save_errno; 57 int input, output, save_errno, i, need_restart;
58 char ch, *p, *end; 58 char ch, *p, *end;
59 struct termios term, oterm; 59 struct termios term, oterm;
60 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm; 60 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
@@ -67,7 +67,11 @@ readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
67 } 67 }
68 68
69restart: 69restart:
70 signo = 0; 70 for (i = 0; i < _NSIG; i++)
71 signo[i] = 0;
72 nr = -1;
73 save_errno = 0;
74 need_restart = 0;
71 /* 75 /*
72 * Read and write to /dev/tty if available. If not, read from 76 * Read and write to /dev/tty if available. If not, read from
73 * stdin and write to stderr unless a tty is required. 77 * stdin and write to stderr unless a tty is required.
@@ -117,26 +121,30 @@ restart:
117 oterm.c_lflag |= ECHO; 121 oterm.c_lflag |= ECHO;
118 } 122 }
119 123
120 if (!(flags & RPP_STDIN)) 124 /* No I/O if we are already backgrounded. */
121 (void)write(output, prompt, strlen(prompt)); 125 if (signo[SIGTTOU] != 1 && signo[SIGTTIN] != 1) {
122 end = buf + bufsiz - 1; 126 if (!(flags & RPP_STDIN))
123 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) { 127 (void)write(output, prompt, strlen(prompt));
124 if (p < end) { 128 end = buf + bufsiz - 1;
125 if ((flags & RPP_SEVENBIT)) 129 p = buf;
126 ch &= 0x7f; 130 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
127 if (isalpha(ch)) { 131 if (p < end) {
128 if ((flags & RPP_FORCELOWER)) 132 if ((flags & RPP_SEVENBIT))
129 ch = tolower(ch); 133 ch &= 0x7f;
130 if ((flags & RPP_FORCEUPPER)) 134 if (isalpha(ch)) {
131 ch = toupper(ch); 135 if ((flags & RPP_FORCELOWER))
136 ch = (char)tolower(ch);
137 if ((flags & RPP_FORCEUPPER))
138 ch = (char)toupper(ch);
139 }
140 *p++ = ch;
132 } 141 }
133 *p++ = ch;
134 } 142 }
143 *p = '\0';
144 save_errno = errno;
145 if (!(term.c_lflag & ECHO))
146 (void)write(output, "\n", 1);
135 } 147 }
136 *p = '\0';
137 save_errno = errno;
138 if (!(term.c_lflag & ECHO))
139 (void)write(output, "\n", 1);
140 148
141 /* Restore old terminal settings and signals. */ 149 /* Restore old terminal settings and signals. */
142 if (memcmp(&term, &oterm, sizeof(term)) != 0) { 150 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
@@ -152,6 +160,7 @@ restart:
152 (void)sigaction(SIGTERM, &saveterm, NULL); 160 (void)sigaction(SIGTERM, &saveterm, NULL);
153 (void)sigaction(SIGTSTP, &savetstp, NULL); 161 (void)sigaction(SIGTSTP, &savetstp, NULL);
154 (void)sigaction(SIGTTIN, &savettin, NULL); 162 (void)sigaction(SIGTTIN, &savettin, NULL);
163 (void)sigaction(SIGTTOU, &savettou, NULL);
155 if (input != STDIN_FILENO) 164 if (input != STDIN_FILENO)
156 (void)close(input); 165 (void)close(input);
157 166
@@ -159,20 +168,25 @@ restart:
159 * If we were interrupted by a signal, resend it to ourselves 168 * If we were interrupted by a signal, resend it to ourselves
160 * now that we have restored the signal handlers. 169 * now that we have restored the signal handlers.
161 */ 170 */
162 if (signo) { 171 for (i = 0; i < _NSIG; i++) {
163 kill(getpid(), signo); 172 if (signo[i]) {
164 switch (signo) { 173 kill(getpid(), i);
165 case SIGTSTP: 174 switch (i) {
166 case SIGTTIN: 175 case SIGTSTP:
167 case SIGTTOU: 176 case SIGTTIN:
168 goto restart; 177 case SIGTTOU:
178 need_restart = 1;
179 }
169 } 180 }
170 } 181 }
182 if (need_restart)
183 goto restart;
171 184
172 errno = save_errno; 185 if (save_errno)
186 errno = save_errno;
173 return(nr == -1 ? NULL : buf); 187 return(nr == -1 ? NULL : buf);
174} 188}
175 189
176#if 0 190#if 0
177char * 191char *
178getpass(const char *prompt) 192getpass(const char *prompt)
@@ -186,6 +200,6 @@ getpass(const char *prompt)
186static void handler(int s) 200static void handler(int s)
187{ 201{
188 202
189 signo = s; 203 signo[s] = 1;
190} 204}
191#endif /* HAVE_READPASSPHRASE */ 205#endif /* HAVE_READPASSPHRASE */