comparison compat/readpassphrase.c @ 2:97097b4b6bfb

Add pwsdump utility The pwsdum utility can dump PasswordSafe database files to a plaintext format and convert this format back into a PasswordSafe database.
author Guido Berhoerster <guido+libpws@berhoerster.name>
date Wed, 01 Apr 2015 14:57:57 +0200
parents
children
comparison
equal deleted inserted replaced
1:e1309515d111 2:97097b4b6bfb
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
36 #include "pws-compat.h"
37 #include "readpassphrase.h"
38
39 #ifndef TCSASOFT
40 #define TCSASOFT 0
41 #endif /* !TCSASOFT */
42
43 #ifndef _NSIG
44 #ifdef NSIG
45 #define _NSIG NSIG
46 #else /* NSIG */
47 #define _NSIG 128
48 #endif /* NSIG */
49 #endif /* !_NSIG */
50
51 #ifndef _PATH_TTY
52 #define _PATH_TTY "/dev/tty"
53 #endif
54
55 static volatile sig_atomic_t signo[_NSIG];
56
57 static void handler(int);
58
59 char *
60 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
61 {
62 ssize_t nr;
63 int input, output, save_errno, i, need_restart;
64 char ch, *p, *end;
65 struct termios term, oterm;
66 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
67 struct sigaction savetstp, savettin, savettou, savepipe;
68
69 /* I suppose we could alloc on demand in this case (XXX). */
70 if (bufsiz == 0) {
71 errno = EINVAL;
72 return(NULL);
73 }
74
75 restart:
76 for (i = 0; i < _NSIG; i++)
77 signo[i] = 0;
78 nr = -1;
79 save_errno = 0;
80 need_restart = 0;
81 /*
82 * Read and write to /dev/tty if available. If not, read from
83 * stdin and write to stderr unless a tty is required.
84 */
85 if ((flags & RPP_STDIN) ||
86 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
87 if (flags & RPP_REQUIRE_TTY) {
88 errno = ENOTTY;
89 return(NULL);
90 }
91 input = STDIN_FILENO;
92 output = STDERR_FILENO;
93 }
94
95 /*
96 * Turn off echo if possible.
97 * If we are using a tty but are not the foreground pgrp this will
98 * generate SIGTTOU, so do it *before* installing the signal handlers.
99 */
100 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
101 memcpy(&term, &oterm, sizeof(term));
102 if (!(flags & RPP_ECHO_ON))
103 term.c_lflag &= ~(ECHO | ECHONL);
104 #ifdef VSTATUS
105 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
106 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
107 #endif /* VSTATUS */
108 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
109 } else {
110 memset(&term, 0, sizeof(term));
111 term.c_lflag |= ECHO;
112 memset(&oterm, 0, sizeof(oterm));
113 oterm.c_lflag |= ECHO;
114 }
115
116 /*
117 * Catch signals that would otherwise cause the user to end
118 * up with echo turned off in the shell. Don't worry about
119 * things like SIGXCPU and SIGVTALRM for now.
120 */
121 sigemptyset(&sa.sa_mask);
122 sa.sa_flags = 0; /* don't restart system calls */
123 sa.sa_handler = handler;
124 (void)sigaction(SIGALRM, &sa, &savealrm);
125 (void)sigaction(SIGHUP, &sa, &savehup);
126 (void)sigaction(SIGINT, &sa, &saveint);
127 (void)sigaction(SIGPIPE, &sa, &savepipe);
128 (void)sigaction(SIGQUIT, &sa, &savequit);
129 (void)sigaction(SIGTERM, &sa, &saveterm);
130 (void)sigaction(SIGTSTP, &sa, &savetstp);
131 (void)sigaction(SIGTTIN, &sa, &savettin);
132 (void)sigaction(SIGTTOU, &sa, &savettou);
133
134 if (!(flags & RPP_STDIN))
135 (void)write(output, prompt, strlen(prompt));
136 end = buf + bufsiz - 1;
137 p = buf;
138 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
139 if (p < end) {
140 if ((flags & RPP_SEVENBIT))
141 ch &= 0x7f;
142 if (isalpha((unsigned char)ch)) {
143 if ((flags & RPP_FORCELOWER))
144 ch = (char)tolower((unsigned char)ch);
145 if ((flags & RPP_FORCEUPPER))
146 ch = (char)toupper((unsigned char)ch);
147 }
148 *p++ = ch;
149 }
150 }
151 *p = '\0';
152 save_errno = errno;
153 if (!(term.c_lflag & ECHO))
154 (void)write(output, "\n", 1);
155
156 /* Restore old terminal settings and signals. */
157 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
158 while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
159 errno == EINTR && !signo[SIGTTOU])
160 continue;
161 }
162 (void)sigaction(SIGALRM, &savealrm, NULL);
163 (void)sigaction(SIGHUP, &savehup, NULL);
164 (void)sigaction(SIGINT, &saveint, NULL);
165 (void)sigaction(SIGQUIT, &savequit, NULL);
166 (void)sigaction(SIGPIPE, &savepipe, NULL);
167 (void)sigaction(SIGTERM, &saveterm, NULL);
168 (void)sigaction(SIGTSTP, &savetstp, NULL);
169 (void)sigaction(SIGTTIN, &savettin, NULL);
170 (void)sigaction(SIGTTOU, &savettou, NULL);
171 if (input != STDIN_FILENO)
172 (void)close(input);
173
174 /*
175 * If we were interrupted by a signal, resend it to ourselves
176 * now that we have restored the signal handlers.
177 */
178 for (i = 0; i < _NSIG; i++) {
179 if (signo[i]) {
180 kill(getpid(), i);
181 switch (i) {
182 case SIGTSTP:
183 case SIGTTIN:
184 case SIGTTOU:
185 need_restart = 1;
186 }
187 }
188 }
189 if (need_restart)
190 goto restart;
191
192 if (save_errno)
193 errno = save_errno;
194 return(nr == -1 ? NULL : buf);
195 }
196
197 #if 0
198 char *
199 getpass(const char *prompt)
200 {
201 static char buf[_PASSWORD_LEN + 1];
202
203 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
204 }
205 #endif
206
207 static void handler(int s)
208 {
209
210 signo[s] = 1;
211 }