blob: 11bd8f646e1dbeae558d78ff06b287a80e07e8df [file] [log] [blame]
Darren Tuckerdbb631c2005-11-10 16:56:28 +11001/* $OpenBSD: readpassphrase.c,v 1.18 2005/08/08 08:05:34 espie 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 *
Ben Lindstromaf4a6c32003-08-25 01:10:51 +00006 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
Damien Millerc8a38682001-06-25 18:09:16 +10009 *
Ben Lindstromaf4a6c32003-08-25 01:10:51 +000010 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * Sponsored in part by the Defense Advanced Research Projects
19 * Agency (DARPA) and Air Force Research Laboratory, Air Force
20 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
Damien Millerc8a38682001-06-25 18:09:16 +100021 */
22
Darren Tucker7f24a0e2005-11-10 16:18:56 +110023/* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
24
Damien Millerc8a38682001-06-25 18:09:16 +100025#include "includes.h"
26
27#ifndef HAVE_READPASSPHRASE
28
29#include <termios.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110030#include <signal.h>
31#include <ctype.h>
Damien Millera1738e42006-07-10 21:33:04 +100032#include <fcntl.h>
Damien Millerc8a38682001-06-25 18:09:16 +100033#include <readpassphrase.h>
Darren Tucker2c1a02a2006-07-12 22:40:50 +100034#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100035#include <string.h>
36#include <unistd.h>
Damien Millerc8a38682001-06-25 18:09:16 +100037
38#ifdef TCSASOFT
39# define _T_FLUSH (TCSAFLUSH|TCSASOFT)
40#else
41# define _T_FLUSH (TCSAFLUSH)
42#endif
43
Ben Lindstroma0bd44c2001-10-25 15:02:35 +000044/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
45#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
46# define _POSIX_VDISABLE VDISABLE
47#endif
48
Damien Miller8e3bdca2002-02-13 16:00:15 +110049static volatile sig_atomic_t signo;
50
51static void handler(int);
52
Damien Millerc8a38682001-06-25 18:09:16 +100053char *
Damien Miller8e3bdca2002-02-13 16:00:15 +110054readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
Damien Millerc8a38682001-06-25 18:09:16 +100055{
Damien Miller8e3bdca2002-02-13 16:00:15 +110056 ssize_t nr;
57 int input, output, save_errno;
Damien Millerc8a38682001-06-25 18:09:16 +100058 char ch, *p, *end;
Damien Miller8e3bdca2002-02-13 16:00:15 +110059 struct termios term, oterm;
Damien Miller71eb0c12002-09-11 10:29:11 +100060 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
61 struct sigaction savetstp, savettin, savettou, savepipe;
Damien Millerc8a38682001-06-25 18:09:16 +100062
63 /* I suppose we could alloc on demand in this case (XXX). */
64 if (bufsiz == 0) {
65 errno = EINVAL;
66 return(NULL);
67 }
68
Damien Miller8e3bdca2002-02-13 16:00:15 +110069restart:
Damien Miller71eb0c12002-09-11 10:29:11 +100070 signo = 0;
Damien Millerc8a38682001-06-25 18:09:16 +100071 /*
72 * Read and write to /dev/tty if available. If not, read from
73 * stdin and write to stderr unless a tty is required.
74 */
Damien Miller71eb0c12002-09-11 10:29:11 +100075 if ((flags & RPP_STDIN) ||
76 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
Damien Millerc8a38682001-06-25 18:09:16 +100077 if (flags & RPP_REQUIRE_TTY) {
78 errno = ENOTTY;
79 return(NULL);
80 }
81 input = STDIN_FILENO;
82 output = STDERR_FILENO;
83 }
84
85 /*
Damien Miller8e3bdca2002-02-13 16:00:15 +110086 * Catch signals that would otherwise cause the user to end
87 * up with echo turned off in the shell. Don't worry about
Damien Miller71eb0c12002-09-11 10:29:11 +100088 * things like SIGXCPU and SIGVTALRM for now.
Damien Millerc8a38682001-06-25 18:09:16 +100089 */
Damien Miller8e3bdca2002-02-13 16:00:15 +110090 sigemptyset(&sa.sa_mask);
91 sa.sa_flags = 0; /* don't restart system calls */
92 sa.sa_handler = handler;
Damien Miller71eb0c12002-09-11 10:29:11 +100093 (void)sigaction(SIGALRM, &sa, &savealrm);
Damien Miller8e3bdca2002-02-13 16:00:15 +110094 (void)sigaction(SIGHUP, &sa, &savehup);
Damien Miller71eb0c12002-09-11 10:29:11 +100095 (void)sigaction(SIGINT, &sa, &saveint);
96 (void)sigaction(SIGPIPE, &sa, &savepipe);
Damien Miller8e3bdca2002-02-13 16:00:15 +110097 (void)sigaction(SIGQUIT, &sa, &savequit);
98 (void)sigaction(SIGTERM, &sa, &saveterm);
99 (void)sigaction(SIGTSTP, &sa, &savetstp);
100 (void)sigaction(SIGTTIN, &sa, &savettin);
101 (void)sigaction(SIGTTOU, &sa, &savettou);
Damien Millerc8a38682001-06-25 18:09:16 +1000102
103 /* Turn off echo if possible. */
Damien Miller71eb0c12002-09-11 10:29:11 +1000104 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
Damien Miller8e3bdca2002-02-13 16:00:15 +1100105 memcpy(&term, &oterm, sizeof(term));
106 if (!(flags & RPP_ECHO_ON))
107 term.c_lflag &= ~(ECHO | ECHONL);
Damien Millerc8a38682001-06-25 18:09:16 +1000108#ifdef VSTATUS
Damien Miller8e3bdca2002-02-13 16:00:15 +1100109 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
Damien Millerc8a38682001-06-25 18:09:16 +1000110 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
Damien Millerc8a38682001-06-25 18:09:16 +1000111#endif
112 (void)tcsetattr(input, _T_FLUSH, &term);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100113 } else {
114 memset(&term, 0, sizeof(term));
Damien Miller71eb0c12002-09-11 10:29:11 +1000115 term.c_lflag |= ECHO;
Damien Miller8e3bdca2002-02-13 16:00:15 +1100116 memset(&oterm, 0, sizeof(oterm));
Damien Miller71eb0c12002-09-11 10:29:11 +1000117 oterm.c_lflag |= ECHO;
Damien Millerc8a38682001-06-25 18:09:16 +1000118 }
119
Damien Miller71eb0c12002-09-11 10:29:11 +1000120 if (!(flags & RPP_STDIN))
121 (void)write(output, prompt, strlen(prompt));
Damien Millerc8a38682001-06-25 18:09:16 +1000122 end = buf + bufsiz - 1;
Damien Miller8e3bdca2002-02-13 16:00:15 +1100123 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
Damien Millerc8a38682001-06-25 18:09:16 +1000124 if (p < end) {
125 if ((flags & RPP_SEVENBIT))
Damien Millerb90416b2001-06-27 23:26:38 +1000126 ch &= 0x7f;
Damien Millerc8a38682001-06-25 18:09:16 +1000127 if (isalpha(ch)) {
128 if ((flags & RPP_FORCELOWER))
129 ch = tolower(ch);
130 if ((flags & RPP_FORCEUPPER))
131 ch = toupper(ch);
132 }
133 *p++ = ch;
134 }
135 }
136 *p = '\0';
Damien Miller8e3bdca2002-02-13 16:00:15 +1100137 save_errno = errno;
138 if (!(term.c_lflag & ECHO))
139 (void)write(output, "\n", 1);
140
141 /* Restore old terminal settings and signals. */
Damien Miller308c8b12005-05-24 16:39:03 +1000142 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
Damien Miller101afbb2005-05-24 16:42:00 +1000143 while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
Damien Miller308c8b12005-05-24 16:39:03 +1000144 errno == EINTR)
145 continue;
146 }
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{
Ben Lindstromaf4a6c32003-08-25 01:10:51 +0000188
Damien Miller8e3bdca2002-02-13 16:00:15 +1100189 signo = s;
190}
Damien Miller804357a2002-05-01 22:00:22 +1000191#endif /* HAVE_READPASSPHRASE */