2 * Copyright (c) 1996, 1998-2005, 2007-2008
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #ifdef HAVE_SYS_BSDTYPES_H
31 # include <sys/bsdtypes.h>
32 #endif /* HAVE_SYS_BSDTYPES_H */
42 #endif /* STDC_HEADERS */
44 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
49 # ifdef HAVE_STRINGS_H
52 #endif /* HAVE_STRING_H */
55 #endif /* HAVE_UNISTD_H */
67 # include <sys/ioctl.h>
68 # endif /* HAVE_TERMIO_H */
69 #endif /* HAVE_TERMIOS_H */
74 __unused static const char rcsid[] = "$Sudo: tgetpass.c,v 1.126 2008/12/09 20:55:49 millert Exp $";
84 #ifndef _POSIX_VDISABLE
86 # define _POSIX_VDISABLE VDISABLE
88 # define _POSIX_VDISABLE 0
93 * Compat macros for non-termios systems.
95 #ifndef HAVE_TERMIOS_H
98 # define termios termio
99 # define tcgetattr(f, t) ioctl(f, TCGETA, t)
100 # define tcsetattr(f, a, t) ioctl(f, a, t)
102 # define TCSAFLUSH TCSETAF
105 # define termios sgttyb
106 # define c_lflag sg_flags
107 # define tcgetattr(f, t) ioctl(f, TIOCGETP, t)
108 # define tcsetattr(f, a, t) ioctl(f, a, t)
110 # define TCSAFLUSH TIOCSETP
111 # endif /* HAVE_TERMIO_H */
112 #endif /* HAVE_TERMIOS_H */
114 static volatile sig_atomic_t signo;
116 static void handler __P((int));
117 static char *getln __P((int, char *, size_t));
118 static char *sudo_askpass __P((const char *));
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 termios term, oterm;
133 static char buf[SUDO_PASS_MAX + 1];
134 int input, output, save_errno;
136 (void) fflush(stdout);
138 /* If using a helper program to get the password, run it instead. */
139 if (ISSET(flags, TGP_ASKPASS) && user_askpass)
140 return(sudo_askpass(prompt));
146 /* Open /dev/tty for reading/writing if possible else use stdin/stderr. */
147 if (ISSET(flags, TGP_STDIN) ||
148 (input = output = open(_PATH_TTY, O_RDWR|O_NOCTTY)) == -1) {
149 input = STDIN_FILENO;
150 output = STDERR_FILENO;
154 * Catch signals that would otherwise cause the user to end
155 * up with echo turned off in the shell.
157 zero_bytes(&sa, sizeof(sa));
158 sigemptyset(&sa.sa_mask);
159 sa.sa_flags = SA_INTERRUPT; /* don't restart system calls */
160 sa.sa_handler = handler;
161 (void) sigaction(SIGALRM, &sa, &savealrm);
162 (void) sigaction(SIGINT, &sa, &saveint);
163 (void) sigaction(SIGHUP, &sa, &savehup);
164 (void) sigaction(SIGQUIT, &sa, &savequit);
165 (void) sigaction(SIGTERM, &sa, &saveterm);
166 (void) sigaction(SIGTSTP, &sa, &savetstp);
167 (void) sigaction(SIGTTIN, &sa, &savettin);
168 (void) sigaction(SIGTTOU, &sa, &savettou);
170 /* Turn echo off/on as specified by flags. */
171 if (tcgetattr(input, &oterm) == 0) {
172 (void) memcpy(&term, &oterm, sizeof(term));
173 if (!ISSET(flags, TGP_ECHO))
174 CLR(term.c_lflag, ECHO|ECHONL);
176 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
178 (void) tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
180 zero_bytes(&term, sizeof(term));
181 zero_bytes(&oterm, sizeof(oterm));
184 /* No output if we are already backgrounded. */
185 if (signo != SIGTTOU && signo != SIGTTIN) {
187 (void) write(output, prompt, strlen(prompt));
191 pass = getln(input, buf, sizeof(buf));
195 if (!ISSET(term.c_lflag, ECHO))
196 (void) write(output, "\n", 1);
199 /* Restore old tty settings and signals. */
200 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
201 while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
205 (void) sigaction(SIGALRM, &savealrm, NULL);
206 (void) sigaction(SIGINT, &saveint, NULL);
207 (void) sigaction(SIGHUP, &savehup, NULL);
208 (void) sigaction(SIGQUIT, &savequit, NULL);
209 (void) sigaction(SIGTERM, &saveterm, NULL);
210 (void) sigaction(SIGTSTP, &savetstp, NULL);
211 (void) sigaction(SIGTTIN, &savettin, NULL);
212 (void) sigaction(SIGTTOU, &savettou, NULL);
213 if (input != STDIN_FILENO)
217 * If we were interrupted by a signal, resend it to ourselves
218 * now that we have restored the signal handlers.
221 kill(getpid(), signo);
236 * Fork a child and exec sudo-askpass to get the password from the user.
242 static char buf[SUDO_PASS_MAX + 1], *pass;
243 sigaction_t sa, saved_sa_pipe;
248 error(1, "unable to create pipe");
250 if ((pid = fork()) == -1)
251 error(1, "unable to fork");
254 /* child, point stdout to output side of the pipe and exec askpass */
255 (void) dup2(pfd[1], STDOUT_FILENO);
256 set_perms(PERM_FULL_USER);
257 closefrom(STDERR_FILENO + 1);
258 execl(user_askpass, user_askpass, prompt, (char *)NULL);
259 warning("unable to run %s", user_askpass);
263 /* Ignore SIGPIPE in case child exits prematurely */
264 zero_bytes(&sa, sizeof(sa));
265 sigemptyset(&sa.sa_mask);
267 sa.sa_handler = SIG_IGN;
268 (void) sigaction(SIGPIPE, &sa, &saved_sa_pipe);
270 /* Get response from child (askpass) and restore SIGPIPE handler */
271 (void) close(pfd[1]);
272 pass = getln(pfd[0], buf, sizeof(buf));
273 (void) close(pfd[0]);
274 (void) sigaction(SIGPIPE, &saved_sa_pipe, NULL);
280 getln(fd, buf, bufsiz)
291 return(NULL); /* sanity */
295 nr = read(fd, &c, 1);
296 if (nr != 1 || c == '\n' || c == '\r')
302 return(nr == 1 ? buf : NULL);
318 if ((fd = open(_PATH_TTY, O_RDWR|O_NOCTTY)) != -1)