Mercurial > projects > pwm
comparison compat/readpassphrase.c @ 0:a7e41e1a79c8
Initial revision
author | Guido Berhoerster <guido+pwm@berhoerster.name> |
---|---|
date | Thu, 19 Jan 2017 22:39:51 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a7e41e1a79c8 |
---|---|
1 /* $OpenBSD: readpassphrase.c,v 1.24 2013/11/24 23:51:29 deraadt Exp $ */ | |
2 | |
3 /* | |
4 * Copyright (c) 2000-2002, 2007, 2010 | |
5 * Todd C. Miller <Todd.Miller@courtesan.com> | |
6 * | |
7 * Permission to use, copy, modify, and distribute this software for any | |
8 * purpose with or without fee is hereby granted, provided that the above | |
9 * copyright notice and this permission notice appear in all copies. | |
10 * | |
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
18 * | |
19 * Sponsored in part by the Defense Advanced Research Projects | |
20 * Agency (DARPA) and Air Force Research Laboratory, Air Force | |
21 * Materiel Command, USAF, under agreement number F39502-99-1-0512. | |
22 */ | |
23 | |
24 #include <ctype.h> | |
25 #include <errno.h> | |
26 #include <fcntl.h> | |
27 #ifdef HAVE_PATHS_H | |
28 #include <paths.h> | |
29 #endif /* HAVE_PATHS_H */ | |
30 #include <pwd.h> | |
31 #include <signal.h> | |
32 #include <string.h> | |
33 #include <termios.h> | |
34 #include <unistd.h> | |
35 #include "readpassphrase.h" | |
36 | |
37 #ifndef TCSASOFT | |
38 #define TCSASOFT 0 | |
39 #endif /* !TCSASOFT */ | |
40 | |
41 #ifndef _NSIG | |
42 #ifdef NSIG | |
43 #define _NSIG NSIG | |
44 #else /* NSIG */ | |
45 #define _NSIG 128 | |
46 #endif /* NSIG */ | |
47 #endif /* !_NSIG */ | |
48 | |
49 #ifndef _PATH_TTY | |
50 #define _PATH_TTY "/dev/tty" | |
51 #endif | |
52 | |
53 static volatile sig_atomic_t signo[_NSIG]; | |
54 | |
55 static void handler(int); | |
56 | |
57 char * | |
58 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) | |
59 { | |
60 ssize_t nr; | |
61 int input, output, save_errno, i, need_restart; | |
62 char ch, *p, *end; | |
63 struct termios term, oterm; | |
64 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm; | |
65 struct sigaction savetstp, savettin, savettou, savepipe; | |
66 | |
67 /* I suppose we could alloc on demand in this case (XXX). */ | |
68 if (bufsiz == 0) { | |
69 errno = EINVAL; | |
70 return(NULL); | |
71 } | |
72 | |
73 restart: | |
74 for (i = 0; i < _NSIG; i++) | |
75 signo[i] = 0; | |
76 nr = -1; | |
77 save_errno = 0; | |
78 need_restart = 0; | |
79 /* | |
80 * Read and write to /dev/tty if available. If not, read from | |
81 * stdin and write to stderr unless a tty is required. | |
82 */ | |
83 if ((flags & RPP_STDIN) || | |
84 (input = output = open(_PATH_TTY, O_RDWR)) == -1) { | |
85 if (flags & RPP_REQUIRE_TTY) { | |
86 errno = ENOTTY; | |
87 return(NULL); | |
88 } | |
89 input = STDIN_FILENO; | |
90 output = STDERR_FILENO; | |
91 } | |
92 | |
93 /* | |
94 * Turn off echo if possible. | |
95 * If we are using a tty but are not the foreground pgrp this will | |
96 * generate SIGTTOU, so do it *before* installing the signal handlers. | |
97 */ | |
98 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) { | |
99 memcpy(&term, &oterm, sizeof(term)); | |
100 if (!(flags & RPP_ECHO_ON)) | |
101 term.c_lflag &= ~(ECHO | ECHONL); | |
102 #ifdef VSTATUS | |
103 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) | |
104 term.c_cc[VSTATUS] = _POSIX_VDISABLE; | |
105 #endif /* VSTATUS */ | |
106 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term); | |
107 } else { | |
108 memset(&term, 0, sizeof(term)); | |
109 term.c_lflag |= ECHO; | |
110 memset(&oterm, 0, sizeof(oterm)); | |
111 oterm.c_lflag |= ECHO; | |
112 } | |
113 | |
114 /* | |
115 * Catch signals that would otherwise cause the user to end | |
116 * up with echo turned off in the shell. Don't worry about | |
117 * things like SIGXCPU and SIGVTALRM for now. | |
118 */ | |
119 sigemptyset(&sa.sa_mask); | |
120 sa.sa_flags = 0; /* don't restart system calls */ | |
121 sa.sa_handler = handler; | |
122 (void)sigaction(SIGALRM, &sa, &savealrm); | |
123 (void)sigaction(SIGHUP, &sa, &savehup); | |
124 (void)sigaction(SIGINT, &sa, &saveint); | |
125 (void)sigaction(SIGPIPE, &sa, &savepipe); | |
126 (void)sigaction(SIGQUIT, &sa, &savequit); | |
127 (void)sigaction(SIGTERM, &sa, &saveterm); | |
128 (void)sigaction(SIGTSTP, &sa, &savetstp); | |
129 (void)sigaction(SIGTTIN, &sa, &savettin); | |
130 (void)sigaction(SIGTTOU, &sa, &savettou); | |
131 | |
132 if (!(flags & RPP_STDIN)) | |
133 (void)write(output, prompt, strlen(prompt)); | |
134 end = buf + bufsiz - 1; | |
135 p = buf; | |
136 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') { | |
137 if (p < end) { | |
138 if ((flags & RPP_SEVENBIT)) | |
139 ch &= 0x7f; | |
140 if (isalpha((unsigned char)ch)) { | |
141 if ((flags & RPP_FORCELOWER)) | |
142 ch = (char)tolower((unsigned char)ch); | |
143 if ((flags & RPP_FORCEUPPER)) | |
144 ch = (char)toupper((unsigned char)ch); | |
145 } | |
146 *p++ = ch; | |
147 } | |
148 } | |
149 *p = '\0'; | |
150 save_errno = errno; | |
151 if (!(term.c_lflag & ECHO)) | |
152 (void)write(output, "\n", 1); | |
153 | |
154 /* Restore old terminal settings and signals. */ | |
155 if (memcmp(&term, &oterm, sizeof(term)) != 0) { | |
156 while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 && | |
157 errno == EINTR && !signo[SIGTTOU]) | |
158 continue; | |
159 } | |
160 (void)sigaction(SIGALRM, &savealrm, NULL); | |
161 (void)sigaction(SIGHUP, &savehup, NULL); | |
162 (void)sigaction(SIGINT, &saveint, NULL); | |
163 (void)sigaction(SIGQUIT, &savequit, NULL); | |
164 (void)sigaction(SIGPIPE, &savepipe, NULL); | |
165 (void)sigaction(SIGTERM, &saveterm, NULL); | |
166 (void)sigaction(SIGTSTP, &savetstp, NULL); | |
167 (void)sigaction(SIGTTIN, &savettin, NULL); | |
168 (void)sigaction(SIGTTOU, &savettou, NULL); | |
169 if (input != STDIN_FILENO) | |
170 (void)close(input); | |
171 | |
172 /* | |
173 * If we were interrupted by a signal, resend it to ourselves | |
174 * now that we have restored the signal handlers. | |
175 */ | |
176 for (i = 0; i < _NSIG; i++) { | |
177 if (signo[i]) { | |
178 kill(getpid(), i); | |
179 switch (i) { | |
180 case SIGTSTP: | |
181 case SIGTTIN: | |
182 case SIGTTOU: | |
183 need_restart = 1; | |
184 } | |
185 } | |
186 } | |
187 if (need_restart) | |
188 goto restart; | |
189 | |
190 if (save_errno) | |
191 errno = save_errno; | |
192 return(nr == -1 ? NULL : buf); | |
193 } | |
194 | |
195 #if 0 | |
196 char * | |
197 getpass(const char *prompt) | |
198 { | |
199 static char buf[_PASSWORD_LEN + 1]; | |
200 | |
201 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); | |
202 } | |
203 #endif | |
204 | |
205 static void handler(int s) | |
206 { | |
207 | |
208 signo[s] = 1; | |
209 } |