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