]> andersk Git - gssapi-openssh.git/blame - openssh/openbsd-compat/readpassphrase.c
Import of OpenSSH 3.6.1p1
[gssapi-openssh.git] / openssh / openbsd-compat / readpassphrase.c
CommitLineData
41b2f314 1/* $OpenBSD: readpassphrase.c,v 1.14 2002/06/28 01:43:58 millert Exp $ */
e9a17296 2
3c0ef626 3/*
41b2f314 4 * Copyright (c) 2000-2002 Todd C. Miller <Todd.Miller@courtesan.com>
3c0ef626 5 * 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)
41b2f314 31static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.14 2002/06/28 01:43:58 millert Exp $";
3c0ef626 32#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
47/* 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
e9a17296 52static volatile sig_atomic_t signo;
53
54static void handler(int);
55
3c0ef626 56char *
e9a17296 57readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
3c0ef626 58{
e9a17296 59 ssize_t nr;
60 int input, output, save_errno;
3c0ef626 61 char ch, *p, *end;
e9a17296 62 struct termios term, oterm;
41b2f314 63 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
64 struct sigaction savetstp, savettin, savettou, savepipe;
3c0ef626 65
66 /* I suppose we could alloc on demand in this case (XXX). */
67 if (bufsiz == 0) {
68 errno = EINVAL;
69 return(NULL);
70 }
71
e9a17296 72restart:
41b2f314 73 signo = 0;
3c0ef626 74 /*
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 */
41b2f314 78 if ((flags & RPP_STDIN) ||
79 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
3c0ef626 80 if (flags & RPP_REQUIRE_TTY) {
81 errno = ENOTTY;
82 return(NULL);
83 }
84 input = STDIN_FILENO;
85 output = STDERR_FILENO;
86 }
87
88 /*
e9a17296 89 * Catch signals that would otherwise cause the user to end
90 * up with echo turned off in the shell. Don't worry about
41b2f314 91 * things like SIGXCPU and SIGVTALRM for now.
3c0ef626 92 */
e9a17296 93 sigemptyset(&sa.sa_mask);
94 sa.sa_flags = 0; /* don't restart system calls */
95 sa.sa_handler = handler;
41b2f314 96 (void)sigaction(SIGALRM, &sa, &savealrm);
e9a17296 97 (void)sigaction(SIGHUP, &sa, &savehup);
41b2f314 98 (void)sigaction(SIGINT, &sa, &saveint);
99 (void)sigaction(SIGPIPE, &sa, &savepipe);
e9a17296 100 (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);
3c0ef626 105
106 /* Turn off echo if possible. */
41b2f314 107 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
e9a17296 108 memcpy(&term, &oterm, sizeof(term));
109 if (!(flags & RPP_ECHO_ON))
110 term.c_lflag &= ~(ECHO | ECHONL);
3c0ef626 111#ifdef VSTATUS
e9a17296 112 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
3c0ef626 113 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
3c0ef626 114#endif
115 (void)tcsetattr(input, _T_FLUSH, &term);
e9a17296 116 } else {
117 memset(&term, 0, sizeof(term));
41b2f314 118 term.c_lflag |= ECHO;
e9a17296 119 memset(&oterm, 0, sizeof(oterm));
41b2f314 120 oterm.c_lflag |= ECHO;
3c0ef626 121 }
122
41b2f314 123 if (!(flags & RPP_STDIN))
124 (void)write(output, prompt, strlen(prompt));
3c0ef626 125 end = buf + bufsiz - 1;
e9a17296 126 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
3c0ef626 127 if (p < end) {
128 if ((flags & RPP_SEVENBIT))
129 ch &= 0x7f;
130 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';
e9a17296 140 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)
146 (void)tcsetattr(input, _T_FLUSH, &oterm);
41b2f314 147 (void)sigaction(SIGALRM, &savealrm, NULL);
e9a17296 148 (void)sigaction(SIGHUP, &savehup, NULL);
41b2f314 149 (void)sigaction(SIGINT, &saveint, NULL);
e9a17296 150 (void)sigaction(SIGQUIT, &savequit, NULL);
41b2f314 151 (void)sigaction(SIGPIPE, &savepipe, NULL);
e9a17296 152 (void)sigaction(SIGTERM, &saveterm, NULL);
153 (void)sigaction(SIGTSTP, &savetstp, NULL);
154 (void)sigaction(SIGTTIN, &savettin, NULL);
3c0ef626 155 if (input != STDIN_FILENO)
156 (void)close(input);
e9a17296 157
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) {
41b2f314 163 kill(getpid(), signo);
e9a17296 164 switch (signo) {
165 case SIGTSTP:
166 case SIGTTIN:
167 case SIGTTOU:
e9a17296 168 goto restart;
169 }
170 }
171
172 errno = save_errno;
173 return(nr == -1 ? NULL : buf);
3c0ef626 174}
e9a17296 175
3c0ef626 176#if 0
177char *
e9a17296 178getpass(const char *prompt)
3c0ef626 179{
180 static char buf[_PASSWORD_LEN + 1];
181
182 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
183}
184#endif
e9a17296 185
186static void handler(int s)
187{
e9a17296 188 signo = s;
189}
700318f3 190#endif /* HAVE_READPASSPHRASE */
This page took 0.079585 seconds and 5 git commands to generate.