Deleted Added
full compact
readpassphrase.c (181110) readpassphrase.c (204917)
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
3/*
2
3/*
4 * Copyright (c) 2000-2002 Todd C. Miller
4 * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
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
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR

--- 28 unchanged lines hidden (view full) ---

41# define _T_FLUSH (TCSAFLUSH)
42#endif
43
44/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
45#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
46# define _POSIX_VDISABLE VDISABLE
47#endif
48
5 *
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
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR

--- 28 unchanged lines hidden (view full) ---

41# define _T_FLUSH (TCSAFLUSH)
42#endif
43
44/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
45#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
46# define _POSIX_VDISABLE VDISABLE
47#endif
48
49static volatile sig_atomic_t signo;
49static volatile sig_atomic_t signo[_NSIG];
50
51static void handler(int);
52
53char *
54readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
55{
56 ssize_t nr;
50
51static void handler(int);
52
53char *
54readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
55{
56 ssize_t nr;
57 int input, output, save_errno;
57 int input, output, save_errno, i, need_restart;
58 char ch, *p, *end;
59 struct termios term, oterm;
60 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
61 struct sigaction savetstp, savettin, savettou, savepipe;
62
63 /* I suppose we could alloc on demand in this case (XXX). */
64 if (bufsiz == 0) {
65 errno = EINVAL;
66 return(NULL);
67 }
68
69restart:
58 char ch, *p, *end;
59 struct termios term, oterm;
60 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
61 struct sigaction savetstp, savettin, savettou, savepipe;
62
63 /* I suppose we could alloc on demand in this case (XXX). */
64 if (bufsiz == 0) {
65 errno = EINVAL;
66 return(NULL);
67 }
68
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 /*
72 * Read and write to /dev/tty if available. If not, read from
73 * stdin and write to stderr unless a tty is required.
74 */
75 if ((flags & RPP_STDIN) ||
76 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
77 if (flags & RPP_REQUIRE_TTY) {
78 errno = ENOTTY;

--- 33 unchanged lines hidden (view full) ---

112 (void)tcsetattr(input, _T_FLUSH, &term);
113 } else {
114 memset(&term, 0, sizeof(term));
115 term.c_lflag |= ECHO;
116 memset(&oterm, 0, sizeof(oterm));
117 oterm.c_lflag |= ECHO;
118 }
119
75 /*
76 * Read and write to /dev/tty if available. If not, read from
77 * stdin and write to stderr unless a tty is required.
78 */
79 if ((flags & RPP_STDIN) ||
80 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
81 if (flags & RPP_REQUIRE_TTY) {
82 errno = ENOTTY;

--- 33 unchanged lines hidden (view full) ---

116 (void)tcsetattr(input, _T_FLUSH, &term);
117 } else {
118 memset(&term, 0, sizeof(term));
119 term.c_lflag |= ECHO;
120 memset(&oterm, 0, sizeof(oterm));
121 oterm.c_lflag |= ECHO;
122 }
123
120 if (!(flags & RPP_STDIN))
121 (void)write(output, prompt, strlen(prompt));
122 end = buf + bufsiz - 1;
123 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
124 if (p < end) {
125 if ((flags & RPP_SEVENBIT))
126 ch &= 0x7f;
127 if (isalpha(ch)) {
128 if ((flags & RPP_FORCELOWER))
129 ch = tolower(ch);
130 if ((flags & RPP_FORCEUPPER))
131 ch = toupper(ch);
124 /* No I/O if we are already backgrounded. */
125 if (signo[SIGTTOU] != 1 && signo[SIGTTIN] != 1) {
126 if (!(flags & RPP_STDIN))
127 (void)write(output, prompt, strlen(prompt));
128 end = buf + bufsiz - 1;
129 p = buf;
130 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
131 if (p < end) {
132 if ((flags & RPP_SEVENBIT))
133 ch &= 0x7f;
134 if (isalpha(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
141 /* Restore old terminal settings and signals. */
142 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
143 while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
144 errno == EINTR)
145 continue;
146 }
147 (void)sigaction(SIGALRM, &savealrm, NULL);
148 (void)sigaction(SIGHUP, &savehup, NULL);
149 (void)sigaction(SIGINT, &saveint, NULL);
150 (void)sigaction(SIGQUIT, &savequit, NULL);
151 (void)sigaction(SIGPIPE, &savepipe, NULL);
152 (void)sigaction(SIGTERM, &saveterm, NULL);
153 (void)sigaction(SIGTSTP, &savetstp, NULL);
154 (void)sigaction(SIGTTIN, &savettin, NULL);
148
149 /* Restore old terminal settings and signals. */
150 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
151 while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
152 errno == EINTR)
153 continue;
154 }
155 (void)sigaction(SIGALRM, &savealrm, NULL);
156 (void)sigaction(SIGHUP, &savehup, NULL);
157 (void)sigaction(SIGINT, &saveint, NULL);
158 (void)sigaction(SIGQUIT, &savequit, NULL);
159 (void)sigaction(SIGPIPE, &savepipe, NULL);
160 (void)sigaction(SIGTERM, &saveterm, NULL);
161 (void)sigaction(SIGTSTP, &savetstp, NULL);
162 (void)sigaction(SIGTTIN, &savettin, NULL);
163 (void)sigaction(SIGTTOU, &savettou, NULL);
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 */
164 if (input != STDIN_FILENO)
165 (void)close(input);
166
167 /*
168 * If we were interrupted by a signal, resend it to ourselves
169 * now that we have restored the signal handlers.
170 */
162 if (signo) {
163 kill(getpid(), signo);
164 switch (signo) {
165 case SIGTSTP:
166 case SIGTTIN:
167 case SIGTTOU:
168 goto restart;
171 for (i = 0; i < _NSIG; i++) {
172 if (signo[i]) {
173 kill(getpid(), i);
174 switch (i) {
175 case SIGTSTP:
176 case SIGTTIN:
177 case SIGTTOU:
178 need_restart = 1;
179 }
169 }
170 }
180 }
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);
174}
187 return(nr == -1 ? NULL : buf);
188}
175
189
176#if 0
177char *
178getpass(const char *prompt)
179{
180 static char buf[_PASSWORD_LEN + 1];
181
182 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
183}
184#endif
185
186static void handler(int s)
187{
188
190#if 0
191char *
192getpass(const char *prompt)
193{
194 static char buf[_PASSWORD_LEN + 1];
195
196 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
197}
198#endif
199
200static void handler(int s)
201{
202
189 signo = s;
203 signo[s] = 1;
190}
191#endif /* HAVE_READPASSPHRASE */
204}
205#endif /* HAVE_READPASSPHRASE */