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