2 * Copyright (c) 1996, 1998-2005, 2007-2009
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>
38 #endif /* STDC_HEADERS */
40 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
45 # ifdef HAVE_STRINGS_H
48 #endif /* HAVE_STRING_H */
51 #endif /* HAVE_UNISTD_H */
59 static volatile sig_atomic_t signo;
61 static void handler __P((int));
62 static char *getln __P((int, char *, size_t, int));
63 static char *sudo_askpass __P((const char *));
65 extern int term_restore __P((int));
66 extern int term_noecho __P((int));
67 extern int term_raw __P((int));
70 * Like getpass(3) but with timeout and echo flags.
73 tgetpass(prompt, timeout, flags)
78 sigaction_t sa, savealrm, saveint, savehup, savequit, saveterm;
79 sigaction_t savetstp, savettin, savettou;
81 static char buf[SUDO_PASS_MAX + 1];
82 int input, output, save_errno, neednl;;
84 (void) fflush(stdout);
86 /* If using a helper program to get the password, run it instead. */
87 if (ISSET(flags, TGP_ASKPASS) && user_askpass)
88 return(sudo_askpass(prompt));
94 /* Open /dev/tty for reading/writing if possible else use stdin/stderr. */
95 if (ISSET(flags, TGP_STDIN) ||
96 (input = output = open(_PATH_TTY, O_RDWR|O_NOCTTY)) == -1) {
98 output = STDERR_FILENO;
102 * Catch signals that would otherwise cause the user to end
103 * up with echo turned off in the shell.
105 zero_bytes(&sa, sizeof(sa));
106 sigemptyset(&sa.sa_mask);
107 sa.sa_flags = SA_INTERRUPT; /* don't restart system calls */
108 sa.sa_handler = handler;
109 (void) sigaction(SIGALRM, &sa, &savealrm);
110 (void) sigaction(SIGINT, &sa, &saveint);
111 (void) sigaction(SIGHUP, &sa, &savehup);
112 (void) sigaction(SIGQUIT, &sa, &savequit);
113 (void) sigaction(SIGTERM, &sa, &saveterm);
114 (void) sigaction(SIGTSTP, &sa, &savetstp);
115 (void) sigaction(SIGTTIN, &sa, &savettin);
116 (void) sigaction(SIGTTOU, &sa, &savettou);
119 neednl = term_raw(input);
121 neednl = term_noecho(input);
123 /* No output if we are already backgrounded. */
124 if (signo != SIGTTOU && signo != SIGTTIN) {
126 (void) write(output, prompt, strlen(prompt));
130 pass = getln(input, buf, sizeof(buf), def_pwfeedback);
135 (void) write(output, "\n", 1);
138 /* Restore old tty settings and signals. */
140 (void) sigaction(SIGALRM, &savealrm, NULL);
141 (void) sigaction(SIGINT, &saveint, NULL);
142 (void) sigaction(SIGHUP, &savehup, NULL);
143 (void) sigaction(SIGQUIT, &savequit, NULL);
144 (void) sigaction(SIGTERM, &saveterm, NULL);
145 (void) sigaction(SIGTSTP, &savetstp, NULL);
146 (void) sigaction(SIGTTIN, &savettin, NULL);
147 (void) sigaction(SIGTTOU, &savettou, NULL);
148 if (input != STDIN_FILENO)
152 * If we were interrupted by a signal, resend it to ourselves
153 * now that we have restored the signal handlers.
156 kill(getpid(), signo);
171 * Fork a child and exec sudo-askpass to get the password from the user.
177 static char buf[SUDO_PASS_MAX + 1], *pass;
178 sigaction_t sa, saved_sa_pipe;
183 error(1, "unable to create pipe");
185 if ((pid = fork()) == -1)
186 error(1, "unable to fork");
189 /* child, point stdout to output side of the pipe and exec askpass */
190 (void) dup2(pfd[1], STDOUT_FILENO);
191 set_perms(PERM_FULL_USER);
192 closefrom(STDERR_FILENO + 1);
193 execl(user_askpass, user_askpass, prompt, (char *)NULL);
194 warning("unable to run %s", user_askpass);
198 /* Ignore SIGPIPE in case child exits prematurely */
199 zero_bytes(&sa, sizeof(sa));
200 sigemptyset(&sa.sa_mask);
202 sa.sa_handler = SIG_IGN;
203 (void) sigaction(SIGPIPE, &sa, &saved_sa_pipe);
205 /* Get response from child (askpass) and restore SIGPIPE handler */
206 (void) close(pfd[1]);
207 pass = getln(pfd[0], buf, sizeof(buf), 0);
208 (void) close(pfd[0]);
209 (void) sigaction(SIGPIPE, &saved_sa_pipe, NULL);
214 extern int term_erase, term_kill;
217 getln(fd, buf, bufsiz, feedback)
223 size_t left = bufsiz;
230 return(NULL); /* sanity */
234 nr = read(fd, &c, 1);
235 if (nr != 1 || c == '\n' || c == '\r')
238 if (c == term_kill) {
240 (void) write(fd, "\b \b", 3);
245 } else if (c == term_erase) {
247 (void) write(fd, "\b \b", 3);
253 (void) write(fd, "*", 1);
261 (void) write(fd, "\b \b", 3);
266 return(nr == 1 ? buf : NULL);
282 if ((fd = open(_PATH_TTY, O_RDWR|O_NOCTTY)) != -1)