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