]>
Commit | Line | Data |
---|---|---|
3159d49a | 1 | /* |
2 | * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com> | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. The name of the author may not be used to endorse or promote products | |
14 | * derived from this software without specific prior written permission. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, | |
17 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | |
18 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
19 | * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 | */ | |
27 | ||
28 | #if defined(LIBC_SCCS) && !defined(lint) | |
29 | static char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.4 2001/06/18 17:41:39 millert Exp $"; | |
30 | #endif /* LIBC_SCCS and not lint */ | |
31 | ||
32 | #include "includes.h" | |
33 | ||
34 | #ifndef HAVE_READPASSPHRASE | |
35 | ||
36 | #include <termios.h> | |
37 | #include <readpassphrase.h> | |
38 | ||
39 | #ifdef TCSASOFT | |
40 | # define _T_FLUSH (TCSAFLUSH|TCSASOFT) | |
41 | #else | |
42 | # define _T_FLUSH (TCSAFLUSH) | |
43 | #endif | |
44 | ||
45 | char * | |
46 | readpassphrase(prompt, buf, bufsiz, flags) | |
47 | const char *prompt; | |
48 | char *buf; | |
49 | size_t bufsiz; | |
50 | int flags; | |
51 | { | |
52 | struct termios term; | |
53 | char ch, *p, *end; | |
54 | u_char status; | |
55 | int echo, input, output; | |
56 | sigset_t oset, nset; | |
57 | ||
58 | /* I suppose we could alloc on demand in this case (XXX). */ | |
59 | if (bufsiz == 0) { | |
60 | errno = EINVAL; | |
61 | return(NULL); | |
62 | } | |
63 | ||
64 | /* | |
65 | * Read and write to /dev/tty if available. If not, read from | |
66 | * stdin and write to stderr unless a tty is required. | |
67 | */ | |
68 | if ((input = output = open(_PATH_TTY, O_RDWR)) == -1) { | |
69 | if (flags & RPP_REQUIRE_TTY) { | |
70 | errno = ENOTTY; | |
71 | return(NULL); | |
72 | } | |
73 | input = STDIN_FILENO; | |
74 | output = STDERR_FILENO; | |
75 | } | |
76 | ||
77 | /* | |
78 | * We block SIGINT and SIGTSTP so the terminal is not left | |
79 | * in an inconsistent state (ie: no echo). It would probably | |
80 | * be better to simply catch these though. | |
81 | */ | |
82 | sigemptyset(&nset); | |
83 | sigaddset(&nset, SIGINT); | |
84 | sigaddset(&nset, SIGTSTP); | |
85 | (void)sigprocmask(SIG_BLOCK, &nset, &oset); | |
86 | ||
87 | /* Turn off echo if possible. */ | |
88 | echo = 0; | |
89 | status = _POSIX_VDISABLE; | |
90 | if (tcgetattr(input, &term) == 0) { | |
91 | if (!(flags & RPP_ECHO_ON) && (term.c_lflag & ECHO)) { | |
92 | echo = 1; | |
93 | term.c_lflag &= ~ECHO; | |
94 | } | |
95 | #ifdef VSTATUS | |
96 | if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) { | |
97 | status = term.c_cc[VSTATUS]; | |
98 | term.c_cc[VSTATUS] = _POSIX_VDISABLE; | |
99 | } | |
100 | #endif | |
101 | (void)tcsetattr(input, _T_FLUSH, &term); | |
102 | } | |
103 | if (!(flags & RPP_ECHO_ON)) { | |
104 | if (tcgetattr(input, &term) == 0 && (term.c_lflag & ECHO)) { | |
105 | echo = 1; | |
106 | term.c_lflag &= ~ECHO; | |
107 | (void)tcsetattr(input, _T_FLUSH, &term); | |
108 | } | |
109 | } | |
110 | ||
111 | (void)write(output, prompt, strlen(prompt)); | |
112 | end = buf + bufsiz - 1; | |
113 | for (p = buf; read(input, &ch, 1) == 1 && ch != '\n' && ch != '\r';) { | |
114 | if (p < end) { | |
115 | if ((flags & RPP_SEVENBIT)) | |
116 | ch = ch &= 0x7f; | |
117 | if (isalpha(ch)) { | |
118 | if ((flags & RPP_FORCELOWER)) | |
119 | ch = tolower(ch); | |
120 | if ((flags & RPP_FORCEUPPER)) | |
121 | ch = toupper(ch); | |
122 | } | |
123 | *p++ = ch; | |
124 | } | |
125 | } | |
126 | *p = '\0'; | |
127 | if (echo || status != _POSIX_VDISABLE) { | |
128 | if (echo) { | |
129 | (void)write(output, "\n", 1); | |
130 | term.c_lflag |= ECHO; | |
131 | } | |
132 | #ifdef VSTATUS | |
133 | if (status != _POSIX_VDISABLE) | |
134 | term.c_cc[VSTATUS] = status; | |
135 | #endif | |
136 | (void)tcsetattr(input, _T_FLUSH, &term); | |
137 | } | |
138 | (void)sigprocmask(SIG_SETMASK, &oset, NULL); | |
139 | if (input != STDIN_FILENO) | |
140 | (void)close(input); | |
141 | return(buf); | |
142 | } | |
143 | #endif /* HAVE_READPASSPHRASE */ | |
144 | ||
145 | #if 0 | |
146 | char * | |
147 | getpass(prompt) | |
148 | const char *prompt; | |
149 | { | |
150 | static char buf[_PASSWORD_LEN + 1]; | |
151 | ||
152 | return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); | |
153 | } | |
154 | #endif |