]> andersk Git - openssh.git/blame - openbsd-compat/readpassphrase.c
- (dtucker) [loginrec.c openbsd-compat/xmmap.c openbsd-compat/bindresvport.c
[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>
3159d49a 35
36#ifdef TCSASOFT
37# define _T_FLUSH (TCSAFLUSH|TCSASOFT)
38#else
39# define _T_FLUSH (TCSAFLUSH)
40#endif
41
d321c94b 42/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
43#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
44# define _POSIX_VDISABLE VDISABLE
45#endif
46
1656cbed 47static volatile sig_atomic_t signo;
48
49static void handler(int);
50
3159d49a 51char *
1656cbed 52readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
3159d49a 53{
1656cbed 54 ssize_t nr;
55 int input, output, save_errno;
3159d49a 56 char ch, *p, *end;
1656cbed 57 struct termios term, oterm;
ac8802eb 58 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
59 struct sigaction savetstp, savettin, savettou, savepipe;
3159d49a 60
61 /* I suppose we could alloc on demand in this case (XXX). */
62 if (bufsiz == 0) {
63 errno = EINVAL;
64 return(NULL);
65 }
66
1656cbed 67restart:
ac8802eb 68 signo = 0;
3159d49a 69 /*
70 * Read and write to /dev/tty if available. If not, read from
71 * stdin and write to stderr unless a tty is required.
72 */
ac8802eb 73 if ((flags & RPP_STDIN) ||
74 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
3159d49a 75 if (flags & RPP_REQUIRE_TTY) {
76 errno = ENOTTY;
77 return(NULL);
78 }
79 input = STDIN_FILENO;
80 output = STDERR_FILENO;
81 }
82
83 /*
1656cbed 84 * Catch signals that would otherwise cause the user to end
85 * up with echo turned off in the shell. Don't worry about
ac8802eb 86 * things like SIGXCPU and SIGVTALRM for now.
3159d49a 87 */
1656cbed 88 sigemptyset(&sa.sa_mask);
89 sa.sa_flags = 0; /* don't restart system calls */
90 sa.sa_handler = handler;
ac8802eb 91 (void)sigaction(SIGALRM, &sa, &savealrm);
1656cbed 92 (void)sigaction(SIGHUP, &sa, &savehup);
ac8802eb 93 (void)sigaction(SIGINT, &sa, &saveint);
94 (void)sigaction(SIGPIPE, &sa, &savepipe);
1656cbed 95 (void)sigaction(SIGQUIT, &sa, &savequit);
96 (void)sigaction(SIGTERM, &sa, &saveterm);
97 (void)sigaction(SIGTSTP, &sa, &savetstp);
98 (void)sigaction(SIGTTIN, &sa, &savettin);
99 (void)sigaction(SIGTTOU, &sa, &savettou);
3159d49a 100
101 /* Turn off echo if possible. */
ac8802eb 102 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
1656cbed 103 memcpy(&term, &oterm, sizeof(term));
104 if (!(flags & RPP_ECHO_ON))
105 term.c_lflag &= ~(ECHO | ECHONL);
3159d49a 106#ifdef VSTATUS
1656cbed 107 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
3159d49a 108 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
3159d49a 109#endif
110 (void)tcsetattr(input, _T_FLUSH, &term);
1656cbed 111 } else {
112 memset(&term, 0, sizeof(term));
ac8802eb 113 term.c_lflag |= ECHO;
1656cbed 114 memset(&oterm, 0, sizeof(oterm));
ac8802eb 115 oterm.c_lflag |= ECHO;
3159d49a 116 }
117
ac8802eb 118 if (!(flags & RPP_STDIN))
119 (void)write(output, prompt, strlen(prompt));
3159d49a 120 end = buf + bufsiz - 1;
1656cbed 121 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
3159d49a 122 if (p < end) {
123 if ((flags & RPP_SEVENBIT))
0eb1a22d 124 ch &= 0x7f;
3159d49a 125 if (isalpha(ch)) {
126 if ((flags & RPP_FORCELOWER))
127 ch = tolower(ch);
128 if ((flags & RPP_FORCEUPPER))
129 ch = toupper(ch);
130 }
131 *p++ = ch;
132 }
133 }
134 *p = '\0';
1656cbed 135 save_errno = errno;
136 if (!(term.c_lflag & ECHO))
137 (void)write(output, "\n", 1);
138
139 /* Restore old terminal settings and signals. */
2f4f4297 140 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
ac9862c5 141 while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
2f4f4297 142 errno == EINTR)
143 continue;
144 }
ac8802eb 145 (void)sigaction(SIGALRM, &savealrm, NULL);
1656cbed 146 (void)sigaction(SIGHUP, &savehup, NULL);
ac8802eb 147 (void)sigaction(SIGINT, &saveint, NULL);
1656cbed 148 (void)sigaction(SIGQUIT, &savequit, NULL);
ac8802eb 149 (void)sigaction(SIGPIPE, &savepipe, NULL);
1656cbed 150 (void)sigaction(SIGTERM, &saveterm, NULL);
151 (void)sigaction(SIGTSTP, &savetstp, NULL);
152 (void)sigaction(SIGTTIN, &savettin, NULL);
3159d49a 153 if (input != STDIN_FILENO)
154 (void)close(input);
1656cbed 155
156 /*
157 * If we were interrupted by a signal, resend it to ourselves
158 * now that we have restored the signal handlers.
159 */
160 if (signo) {
ac8802eb 161 kill(getpid(), signo);
1656cbed 162 switch (signo) {
163 case SIGTSTP:
164 case SIGTTIN:
165 case SIGTTOU:
1656cbed 166 goto restart;
167 }
168 }
169
170 errno = save_errno;
171 return(nr == -1 ? NULL : buf);
3159d49a 172}
1656cbed 173
3159d49a 174#if 0
175char *
1656cbed 176getpass(const char *prompt)
3159d49a 177{
178 static char buf[_PASSWORD_LEN + 1];
179
180 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
181}
182#endif
1656cbed 183
184static void handler(int s)
185{
510a42ce 186
1656cbed 187 signo = s;
188}
50903cc7 189#endif /* HAVE_READPASSPHRASE */
This page took 1.752593 seconds and 5 git commands to generate.