blob: 4e549b62b91e73253ac9b86f5a3431f5e130de34 [file] [log] [blame]
Damien Miller71eb0c12002-09-11 10:29:11 +10001/* $OpenBSD: readpassphrase.c,v 1.14 2002/06/28 01:43:58 millert Exp $ */
Damien Miller8e3bdca2002-02-13 16:00:15 +11002
Damien Millerc8a38682001-06-25 18:09:16 +10003/*
Damien Miller71eb0c12002-09-11 10:29:11 +10004 * Copyright (c) 2000-2002 Todd C. Miller <Todd.Miller@courtesan.com>
Damien Millerc8a38682001-06-25 18:09:16 +10005 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
Damien Miller71eb0c12002-09-11 10:29:11 +100031static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.14 2002/06/28 01:43:58 millert Exp $";
Damien Millerc8a38682001-06-25 18:09:16 +100032#endif /* LIBC_SCCS and not lint */
33
34#include "includes.h"
35
36#ifndef HAVE_READPASSPHRASE
37
38#include <termios.h>
39#include <readpassphrase.h>
40
41#ifdef TCSASOFT
42# define _T_FLUSH (TCSAFLUSH|TCSASOFT)
43#else
44# define _T_FLUSH (TCSAFLUSH)
45#endif
46
Ben Lindstroma0bd44c2001-10-25 15:02:35 +000047/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
48#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
49# define _POSIX_VDISABLE VDISABLE
50#endif
51
Damien Miller8e3bdca2002-02-13 16:00:15 +110052static volatile sig_atomic_t signo;
53
54static void handler(int);
55
Damien Millerc8a38682001-06-25 18:09:16 +100056char *
Damien Miller8e3bdca2002-02-13 16:00:15 +110057readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
Damien Millerc8a38682001-06-25 18:09:16 +100058{
Damien Miller8e3bdca2002-02-13 16:00:15 +110059 ssize_t nr;
60 int input, output, save_errno;
Damien Millerc8a38682001-06-25 18:09:16 +100061 char ch, *p, *end;
Damien Miller8e3bdca2002-02-13 16:00:15 +110062 struct termios term, oterm;
Damien Miller71eb0c12002-09-11 10:29:11 +100063 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
64 struct sigaction savetstp, savettin, savettou, savepipe;
Damien Millerc8a38682001-06-25 18:09:16 +100065
66 /* I suppose we could alloc on demand in this case (XXX). */
67 if (bufsiz == 0) {
68 errno = EINVAL;
69 return(NULL);
70 }
71
Damien Miller8e3bdca2002-02-13 16:00:15 +110072restart:
Damien Miller71eb0c12002-09-11 10:29:11 +100073 signo = 0;
Damien Millerc8a38682001-06-25 18:09:16 +100074 /*
75 * Read and write to /dev/tty if available. If not, read from
76 * stdin and write to stderr unless a tty is required.
77 */
Damien Miller71eb0c12002-09-11 10:29:11 +100078 if ((flags & RPP_STDIN) ||
79 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
Damien Millerc8a38682001-06-25 18:09:16 +100080 if (flags & RPP_REQUIRE_TTY) {
81 errno = ENOTTY;
82 return(NULL);
83 }
84 input = STDIN_FILENO;
85 output = STDERR_FILENO;
86 }
87
88 /*
Damien Miller8e3bdca2002-02-13 16:00:15 +110089 * Catch signals that would otherwise cause the user to end
90 * up with echo turned off in the shell. Don't worry about
Damien Miller71eb0c12002-09-11 10:29:11 +100091 * things like SIGXCPU and SIGVTALRM for now.
Damien Millerc8a38682001-06-25 18:09:16 +100092 */
Damien Miller8e3bdca2002-02-13 16:00:15 +110093 sigemptyset(&sa.sa_mask);
94 sa.sa_flags = 0; /* don't restart system calls */
95 sa.sa_handler = handler;
Damien Miller71eb0c12002-09-11 10:29:11 +100096 (void)sigaction(SIGALRM, &sa, &savealrm);
Damien Miller8e3bdca2002-02-13 16:00:15 +110097 (void)sigaction(SIGHUP, &sa, &savehup);
Damien Miller71eb0c12002-09-11 10:29:11 +100098 (void)sigaction(SIGINT, &sa, &saveint);
99 (void)sigaction(SIGPIPE, &sa, &savepipe);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100100 (void)sigaction(SIGQUIT, &sa, &savequit);
101 (void)sigaction(SIGTERM, &sa, &saveterm);
102 (void)sigaction(SIGTSTP, &sa, &savetstp);
103 (void)sigaction(SIGTTIN, &sa, &savettin);
104 (void)sigaction(SIGTTOU, &sa, &savettou);
Damien Millerc8a38682001-06-25 18:09:16 +1000105
106 /* Turn off echo if possible. */
Damien Miller71eb0c12002-09-11 10:29:11 +1000107 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
Damien Miller8e3bdca2002-02-13 16:00:15 +1100108 memcpy(&term, &oterm, sizeof(term));
109 if (!(flags & RPP_ECHO_ON))
110 term.c_lflag &= ~(ECHO | ECHONL);
Damien Millerc8a38682001-06-25 18:09:16 +1000111#ifdef VSTATUS
Damien Miller8e3bdca2002-02-13 16:00:15 +1100112 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
Damien Millerc8a38682001-06-25 18:09:16 +1000113 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
Damien Millerc8a38682001-06-25 18:09:16 +1000114#endif
115 (void)tcsetattr(input, _T_FLUSH, &term);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100116 } else {
117 memset(&term, 0, sizeof(term));
Damien Miller71eb0c12002-09-11 10:29:11 +1000118 term.c_lflag |= ECHO;
Damien Miller8e3bdca2002-02-13 16:00:15 +1100119 memset(&oterm, 0, sizeof(oterm));
Damien Miller71eb0c12002-09-11 10:29:11 +1000120 oterm.c_lflag |= ECHO;
Damien Millerc8a38682001-06-25 18:09:16 +1000121 }
122
Damien Miller71eb0c12002-09-11 10:29:11 +1000123 if (!(flags & RPP_STDIN))
124 (void)write(output, prompt, strlen(prompt));
Damien Millerc8a38682001-06-25 18:09:16 +1000125 end = buf + bufsiz - 1;
Damien Miller8e3bdca2002-02-13 16:00:15 +1100126 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
Damien Millerc8a38682001-06-25 18:09:16 +1000127 if (p < end) {
128 if ((flags & RPP_SEVENBIT))
Damien Millerb90416b2001-06-27 23:26:38 +1000129 ch &= 0x7f;
Damien Millerc8a38682001-06-25 18:09:16 +1000130 if (isalpha(ch)) {
131 if ((flags & RPP_FORCELOWER))
132 ch = tolower(ch);
133 if ((flags & RPP_FORCEUPPER))
134 ch = toupper(ch);
135 }
136 *p++ = ch;
137 }
138 }
139 *p = '\0';
Damien Miller8e3bdca2002-02-13 16:00:15 +1100140 save_errno = errno;
141 if (!(term.c_lflag & ECHO))
142 (void)write(output, "\n", 1);
143
144 /* Restore old terminal settings and signals. */
145 if (memcmp(&term, &oterm, sizeof(term)) != 0)
Damien Miller89a1b9f2002-02-13 16:43:52 +1100146 (void)tcsetattr(input, _T_FLUSH, &oterm);
Damien Miller71eb0c12002-09-11 10:29:11 +1000147 (void)sigaction(SIGALRM, &savealrm, NULL);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100148 (void)sigaction(SIGHUP, &savehup, NULL);
Damien Miller71eb0c12002-09-11 10:29:11 +1000149 (void)sigaction(SIGINT, &saveint, NULL);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100150 (void)sigaction(SIGQUIT, &savequit, NULL);
Damien Miller71eb0c12002-09-11 10:29:11 +1000151 (void)sigaction(SIGPIPE, &savepipe, NULL);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100152 (void)sigaction(SIGTERM, &saveterm, NULL);
153 (void)sigaction(SIGTSTP, &savetstp, NULL);
154 (void)sigaction(SIGTTIN, &savettin, NULL);
Damien Millerc8a38682001-06-25 18:09:16 +1000155 if (input != STDIN_FILENO)
156 (void)close(input);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100157
158 /*
159 * If we were interrupted by a signal, resend it to ourselves
160 * now that we have restored the signal handlers.
161 */
162 if (signo) {
Damien Miller71eb0c12002-09-11 10:29:11 +1000163 kill(getpid(), signo);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100164 switch (signo) {
165 case SIGTSTP:
166 case SIGTTIN:
167 case SIGTTOU:
Damien Miller8e3bdca2002-02-13 16:00:15 +1100168 goto restart;
169 }
170 }
171
172 errno = save_errno;
173 return(nr == -1 ? NULL : buf);
Damien Millerc8a38682001-06-25 18:09:16 +1000174}
Damien Miller8e3bdca2002-02-13 16:00:15 +1100175
Damien Millerc8a38682001-06-25 18:09:16 +1000176#if 0
177char *
Damien Miller8e3bdca2002-02-13 16:00:15 +1100178getpass(const char *prompt)
Damien Millerc8a38682001-06-25 18:09:16 +1000179{
180 static char buf[_PASSWORD_LEN + 1];
181
182 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
183}
184#endif
Damien Miller8e3bdca2002-02-13 16:00:15 +1100185
186static void handler(int s)
187{
Damien Miller8e3bdca2002-02-13 16:00:15 +1100188 signo = s;
189}
Damien Miller804357a2002-05-01 22:00:22 +1000190#endif /* HAVE_READPASSPHRASE */