]> andersk Git - openssh.git/blame - openbsd-compat/readpassphrase.c
- (dtucker) [openbsd-compat/readpassphrase.c] Resync against OpenBSD's r1.18: ...
[openssh.git] / openbsd-compat / readpassphrase.c
CommitLineData
e1829842 1/* $OpenBSD: readpassphrase.c,v 1.18 2005/08/08 08:05:34 espie Exp $ */
1656cbed 2
3159d49a 3/*
ac8802eb 4 * Copyright (c) 2000-2002 Todd C. Miller <Todd.Miller@courtesan.com>
3159d49a 5 *
510a42ce 6 * 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.
3159d49a 9 *
510a42ce 10 * 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.
3159d49a 21 */
22
f6d4fb87 23/* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
24
3159d49a 25#include "includes.h"
26
27#ifndef HAVE_READPASSPHRASE
28
29#include <termios.h>
b5b88c19 30#include <signal.h>
31#include <ctype.h>
22bbb3e6 32#include <fcntl.h>
3159d49a 33#include <readpassphrase.h>
fec71b2f 34#include <errno.h>
28cb0a43 35#include <string.h>
36#include <unistd.h>
3159d49a 37
38#ifdef TCSASOFT
39# define _T_FLUSH (TCSAFLUSH|TCSASOFT)
40#else
41# define _T_FLUSH (TCSAFLUSH)
42#endif
43
d321c94b 44/* 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
1656cbed 49static volatile sig_atomic_t signo;
50
51static void handler(int);
52
3159d49a 53char *
1656cbed 54readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
3159d49a 55{
1656cbed 56 ssize_t nr;
57 int input, output, save_errno;
3159d49a 58 char ch, *p, *end;
1656cbed 59 struct termios term, oterm;
ac8802eb 60 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
61 struct sigaction savetstp, savettin, savettou, savepipe;
3159d49a 62
63 /* I suppose we could alloc on demand in this case (XXX). */
64 if (bufsiz == 0) {
65 errno = EINVAL;
66 return(NULL);
67 }
68
1656cbed 69restart:
ac8802eb 70 signo = 0;
3159d49a 71 /*
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 */
ac8802eb 75 if ((flags & RPP_STDIN) ||
76 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
3159d49a 77 if (flags & RPP_REQUIRE_TTY) {
78 errno = ENOTTY;
79 return(NULL);
80 }
81 input = STDIN_FILENO;
82 output = STDERR_FILENO;
83 }
84
85 /*
1656cbed 86 * Catch signals that would otherwise cause the user to end
87 * up with echo turned off in the shell. Don't worry about
ac8802eb 88 * things like SIGXCPU and SIGVTALRM for now.
3159d49a 89 */
1656cbed 90 sigemptyset(&sa.sa_mask);
91 sa.sa_flags = 0; /* don't restart system calls */
92 sa.sa_handler = handler;
ac8802eb 93 (void)sigaction(SIGALRM, &sa, &savealrm);
1656cbed 94 (void)sigaction(SIGHUP, &sa, &savehup);
ac8802eb 95 (void)sigaction(SIGINT, &sa, &saveint);
96 (void)sigaction(SIGPIPE, &sa, &savepipe);
1656cbed 97 (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);
3159d49a 102
103 /* Turn off echo if possible. */
ac8802eb 104 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
1656cbed 105 memcpy(&term, &oterm, sizeof(term));
106 if (!(flags & RPP_ECHO_ON))
107 term.c_lflag &= ~(ECHO | ECHONL);
3159d49a 108#ifdef VSTATUS
1656cbed 109 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
3159d49a 110 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
3159d49a 111#endif
112 (void)tcsetattr(input, _T_FLUSH, &term);
1656cbed 113 } else {
114 memset(&term, 0, sizeof(term));
ac8802eb 115 term.c_lflag |= ECHO;
1656cbed 116 memset(&oterm, 0, sizeof(oterm));
ac8802eb 117 oterm.c_lflag |= ECHO;
3159d49a 118 }
119
ac8802eb 120 if (!(flags & RPP_STDIN))
121 (void)write(output, prompt, strlen(prompt));
3159d49a 122 end = buf + bufsiz - 1;
1656cbed 123 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
3159d49a 124 if (p < end) {
125 if ((flags & RPP_SEVENBIT))
0eb1a22d 126 ch &= 0x7f;
3159d49a 127 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';
1656cbed 137 save_errno = errno;
138 if (!(term.c_lflag & ECHO))
139 (void)write(output, "\n", 1);
140
141 /* Restore old terminal settings and signals. */
2f4f4297 142 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
ac9862c5 143 while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
2f4f4297 144 errno == EINTR)
145 continue;
146 }
ac8802eb 147 (void)sigaction(SIGALRM, &savealrm, NULL);
1656cbed 148 (void)sigaction(SIGHUP, &savehup, NULL);
ac8802eb 149 (void)sigaction(SIGINT, &saveint, NULL);
1656cbed 150 (void)sigaction(SIGQUIT, &savequit, NULL);
ac8802eb 151 (void)sigaction(SIGPIPE, &savepipe, NULL);
1656cbed 152 (void)sigaction(SIGTERM, &saveterm, NULL);
153 (void)sigaction(SIGTSTP, &savetstp, NULL);
154 (void)sigaction(SIGTTIN, &savettin, NULL);
1f4dfa18 155 (void)sigaction(SIGTTOU, &savettou, NULL);
3159d49a 156 if (input != STDIN_FILENO)
157 (void)close(input);
1656cbed 158
159 /*
160 * If we were interrupted by a signal, resend it to ourselves
161 * now that we have restored the signal handlers.
162 */
163 if (signo) {
ac8802eb 164 kill(getpid(), signo);
1656cbed 165 switch (signo) {
166 case SIGTSTP:
167 case SIGTTIN:
168 case SIGTTOU:
1656cbed 169 goto restart;
170 }
171 }
172
173 errno = save_errno;
174 return(nr == -1 ? NULL : buf);
3159d49a 175}
1f4dfa18 176
3159d49a 177#if 0
178char *
1656cbed 179getpass(const char *prompt)
3159d49a 180{
181 static char buf[_PASSWORD_LEN + 1];
182
183 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
184}
185#endif
1656cbed 186
187static void handler(int s)
188{
510a42ce 189
1656cbed 190 signo = s;
191}
50903cc7 192#endif /* HAVE_READPASSPHRASE */
This page took 0.245378 seconds and 5 git commands to generate.