2 * Copyright (c) 1996, 1998-2004 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * Sponsored in part by the Defense Advanced Research Projects
17 * Agency (DARPA) and Air Force Research Laboratory, Air Force
18 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #ifdef HAVE_SYS_BSDTYPES_H
30 # include <sys/bsdtypes.h>
31 #endif /* HAVE_SYS_BSDTYPES_H */
41 #endif /* STDC_HEADERS */
43 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
48 # ifdef HAVE_STRINGS_H
51 #endif /* HAVE_STRING_H */
54 #endif /* HAVE_UNISTD_H */
66 # include <sys/ioctl.h>
67 # endif /* HAVE_TERMIO_H */
68 #endif /* HAVE_TERMIOS_H */
73 static const char rcsid[] = "$Sudo: tgetpass.c,v 1.111 2004/06/06 23:58:11 millert Exp $";
83 #ifndef _POSIX_VDISABLE
85 # define _POSIX_VDISABLE VDISABLE
87 # define _POSIX_VDISABLE 0
92 * Abstract method of getting at the term flags.
98 # define tflags c_lflag
99 # define term_getattr(f, t) tcgetattr(f, t)
100 # define term_setattr(f, t) tcsetattr(f, TCSAFLUSH|TCSASOFT, t)
102 # ifdef HAVE_TERMIO_H
104 # define tflags c_lflag
105 # define term_getattr(f, t) ioctl(f, TCGETA, t)
106 # define term_setattr(f, t) ioctl(f, TCSETAF, t)
109 # define tflags sg_flags
110 # define term_getattr(f, t) ioctl(f, TIOCGETP, t)
111 # define term_setattr(f, t) ioctl(f, TIOCSETP, t)
112 # endif /* HAVE_TERMIO_H */
113 #endif /* HAVE_TERMIOS_H */
115 static volatile sig_atomic_t signo;
117 static void handler __P((int));
118 static char *getln __P((int, char *, size_t));
121 * Like getpass(3) but with timeout and echo flags.
124 tgetpass(prompt, timeout, flags)
129 sigaction_t sa, savealrm, saveint, savehup, savequit, saveterm;
130 sigaction_t savetstp, savettin, savettou;
131 struct TERM term, oterm;
133 static char buf[SUDO_PASS_MAX + 1];
134 int input, output, save_errno;
136 (void) fflush(stdout);
138 /* Open /dev/tty for reading/writing if possible else use stdin/stderr. */
139 if (ISSET(flags, TGP_STDIN) ||
140 (input = output = open(_PATH_TTY, O_RDWR|O_NOCTTY)) == -1) {
141 input = STDIN_FILENO;
142 output = STDERR_FILENO;
146 * Catch signals that would otherwise cause the user to end
147 * up with echo turned off in the shell.
149 sigemptyset(&sa.sa_mask);
150 sa.sa_flags = SA_INTERRUPT; /* don't restart system calls */
151 sa.sa_handler = handler;
152 (void) sigaction(SIGALRM, &sa, &savealrm);
153 (void) sigaction(SIGINT, &sa, &saveint);
154 (void) sigaction(SIGHUP, &sa, &savehup);
155 (void) sigaction(SIGQUIT, &sa, &savequit);
156 (void) sigaction(SIGTERM, &sa, &saveterm);
157 (void) sigaction(SIGTSTP, &sa, &savetstp);
158 (void) sigaction(SIGTTIN, &sa, &savettin);
159 (void) sigaction(SIGTTOU, &sa, &savettou);
161 /* Turn echo off/on as specified by flags. */
162 if (term_getattr(input, &oterm) == 0) {
163 (void) memcpy(&term, &oterm, sizeof(term));
164 if (!ISSET(flags, TGP_ECHO))
165 CLR(term.tflags, (ECHO | ECHONL));
167 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
169 (void) term_setattr(input, &term);
171 memset(&term, 0, sizeof(term));
172 memset(&oterm, 0, sizeof(oterm));
176 (void) write(output, prompt, strlen(prompt));
180 pass = getln(input, buf, sizeof(buf));
184 if (!ISSET(term.tflags, ECHO))
185 (void) write(output, "\n", 1);
187 /* Restore old tty settings and signals. */
188 if (memcmp(&term, &oterm, sizeof(term)) != 0)
189 (void) term_setattr(input, &oterm);
190 (void) sigaction(SIGALRM, &savealrm, NULL);
191 (void) sigaction(SIGINT, &saveint, NULL);
192 (void) sigaction(SIGHUP, &savehup, NULL);
193 (void) sigaction(SIGQUIT, &savequit, NULL);
194 (void) sigaction(SIGTERM, &saveterm, NULL);
195 (void) sigaction(SIGTSTP, &savetstp, NULL);
196 (void) sigaction(SIGTTIN, &savettin, NULL);
197 (void) sigaction(SIGTTOU, &savettou, NULL);
198 if (input != STDIN_FILENO)
202 * If we were interrupted by a signal, resend it to ourselves
203 * now that we have restored the signal handlers.
206 kill(getpid(), signo);
221 getln(fd, buf, bufsiz)
231 return(NULL); /* sanity */
236 while (--bufsiz && (nr = read(fd, &c, 1)) == 1 && c != '\n' && c != '\r')
239 return(nr == -1 ? NULL : buf);