update Debian standards version
[debian/sudo] / tgetpass.c
1 /*
2  * Copyright (c) 1996, 1998-2005, 2007-2009
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
4  *
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.
8  *
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.
16  *
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.
20  */
21
22 #ifdef __TANDEM
23 # include <floss.h>
24 #endif
25
26 #include <config.h>
27
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <stdio.h>
31 #ifdef STDC_HEADERS
32 # include <stdlib.h>
33 # include <stddef.h>
34 #else
35 # ifdef HAVE_STDLIB_H
36 #  include <stdlib.h>
37 # endif
38 #endif /* STDC_HEADERS */
39 #ifdef HAVE_STRING_H
40 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
41 #  include <memory.h>
42 # endif
43 # include <string.h>
44 #else
45 # ifdef HAVE_STRINGS_H
46 #  include <strings.h>
47 # endif
48 #endif /* HAVE_STRING_H */
49 #ifdef HAVE_UNISTD_H
50 # include <unistd.h>
51 #endif /* HAVE_UNISTD_H */
52 #include <pwd.h>
53 #include <errno.h>
54 #include <signal.h>
55 #include <fcntl.h>
56
57 #include "sudo.h"
58
59 #ifndef lint
60 __unused static const char rcsid[] = "$Sudo: tgetpass.c,v 1.131 2009/05/25 12:02:42 millert Exp $";
61 #endif /* lint */
62
63 static volatile sig_atomic_t signo;
64
65 static void handler __P((int));
66 static char *getln __P((int, char *, size_t, int));
67 static char *sudo_askpass __P((const char *));
68
69 extern int term_restore __P((int));
70 extern int term_noecho __P((int));
71 extern int term_raw __P((int));
72
73 /*
74  * Like getpass(3) but with timeout and echo flags.
75  */
76 char *
77 tgetpass(prompt, timeout, flags)
78     const char *prompt;
79     int timeout;
80     int flags;
81 {
82     sigaction_t sa, savealrm, saveint, savehup, savequit, saveterm;
83     sigaction_t savetstp, savettin, savettou;
84     char *pass;
85     static char buf[SUDO_PASS_MAX + 1];
86     int input, output, save_errno, neednl;;
87
88     (void) fflush(stdout);
89
90     /* If using a helper program to get the password, run it instead. */
91     if (ISSET(flags, TGP_ASKPASS) && user_askpass)
92         return(sudo_askpass(prompt));
93
94 restart:
95     signo = 0;
96     pass = NULL;
97     save_errno = 0;
98     /* Open /dev/tty for reading/writing if possible else use stdin/stderr. */
99     if (ISSET(flags, TGP_STDIN) ||
100         (input = output = open(_PATH_TTY, O_RDWR|O_NOCTTY)) == -1) {
101         input = STDIN_FILENO;
102         output = STDERR_FILENO;
103     }
104
105     /*
106      * Catch signals that would otherwise cause the user to end
107      * up with echo turned off in the shell.
108      */
109     zero_bytes(&sa, sizeof(sa));
110     sigemptyset(&sa.sa_mask);
111     sa.sa_flags = SA_INTERRUPT; /* don't restart system calls */
112     sa.sa_handler = handler;
113     (void) sigaction(SIGALRM, &sa, &savealrm);
114     (void) sigaction(SIGINT, &sa, &saveint);
115     (void) sigaction(SIGHUP, &sa, &savehup);
116     (void) sigaction(SIGQUIT, &sa, &savequit);
117     (void) sigaction(SIGTERM, &sa, &saveterm);
118     (void) sigaction(SIGTSTP, &sa, &savetstp);
119     (void) sigaction(SIGTTIN, &sa, &savettin);
120     (void) sigaction(SIGTTOU, &sa, &savettou);
121
122     if (def_pwfeedback)
123         neednl = term_raw(input);
124     else
125         neednl = term_noecho(input);
126
127     /* No output if we are already backgrounded. */
128     if (signo != SIGTTOU && signo != SIGTTIN) {
129         if (prompt)
130             (void) write(output, prompt, strlen(prompt));
131
132         if (timeout > 0)
133             alarm(timeout);
134         pass = getln(input, buf, sizeof(buf), def_pwfeedback);
135         alarm(0);
136         save_errno = errno;
137
138         if (neednl)
139             (void) write(output, "\n", 1);
140     }
141
142     /* Restore old tty settings and signals. */
143     term_restore(input);
144     (void) sigaction(SIGALRM, &savealrm, NULL);
145     (void) sigaction(SIGINT, &saveint, NULL);
146     (void) sigaction(SIGHUP, &savehup, NULL);
147     (void) sigaction(SIGQUIT, &savequit, NULL);
148     (void) sigaction(SIGTERM, &saveterm, NULL);
149     (void) sigaction(SIGTSTP, &savetstp, NULL);
150     (void) sigaction(SIGTTIN, &savettin, NULL);
151     (void) sigaction(SIGTTOU, &savettou, NULL);
152     if (input != STDIN_FILENO)
153         (void) close(input);
154
155     /*
156      * If we were interrupted by a signal, resend it to ourselves
157      * now that we have restored the signal handlers.
158      */
159     if (signo) {
160         kill(getpid(), signo);
161         switch (signo) {
162             case SIGTSTP:
163             case SIGTTIN:
164             case SIGTTOU:
165                 goto restart;
166         }
167     }
168
169     if (save_errno)
170         errno = save_errno;
171     return(pass);
172 }
173
174 /*
175  * Fork a child and exec sudo-askpass to get the password from the user.
176  */
177 static char *
178 sudo_askpass(prompt)
179     const char *prompt;
180 {
181     static char buf[SUDO_PASS_MAX + 1], *pass;
182     sigaction_t sa, saved_sa_pipe;
183     int pfd[2];
184     pid_t pid;
185
186     if (pipe(pfd) == -1)
187         error(1, "unable to create pipe");
188
189     if ((pid = fork()) == -1)
190         error(1, "unable to fork");
191
192     if (pid == 0) {
193         /* child, point stdout to output side of the pipe and exec askpass */
194         (void) dup2(pfd[1], STDOUT_FILENO);
195         set_perms(PERM_FULL_USER);
196         closefrom(STDERR_FILENO + 1);
197         execl(user_askpass, user_askpass, prompt, (char *)NULL);
198         warning("unable to run %s", user_askpass);
199         _exit(255);
200     }
201
202     /* Ignore SIGPIPE in case child exits prematurely */
203     zero_bytes(&sa, sizeof(sa));
204     sigemptyset(&sa.sa_mask);
205     sa.sa_flags = 0;
206     sa.sa_handler = SIG_IGN;
207     (void) sigaction(SIGPIPE, &sa, &saved_sa_pipe);
208
209     /* Get response from child (askpass) and restore SIGPIPE handler */
210     (void) close(pfd[1]);
211     pass = getln(pfd[0], buf, sizeof(buf), 0);
212     (void) close(pfd[0]);
213     (void) sigaction(SIGPIPE, &saved_sa_pipe, NULL);
214
215     return(pass);
216 }
217
218 extern int term_erase, term_kill;
219
220 static char *
221 getln(fd, buf, bufsiz, feedback)
222     int fd;
223     char *buf;
224     size_t bufsiz;
225     int feedback;
226 {
227     size_t left = bufsiz;
228     ssize_t nr = -1;
229     char *cp = buf;
230     char c = '\0';
231
232     if (left == 0) {
233         errno = EINVAL;
234         return(NULL);                   /* sanity */
235     }
236
237     while (--left) {
238         nr = read(fd, &c, 1);
239         if (nr != 1 || c == '\n' || c == '\r')
240             break;
241         if (feedback) {
242             if (c == term_kill) {
243                 while (cp > buf) {
244                     (void) write(fd, "\b \b", 3);
245                     --cp;
246                 }
247                 left = bufsiz;
248                 continue;
249             } else if (c == term_erase) {
250                 if (cp > buf) {
251                     (void) write(fd, "\b \b", 3);
252                     --cp;
253                     left++;
254                 }
255                 continue;
256             }
257             (void) write(fd, "*", 1);
258         }
259         *cp++ = c;
260     }
261     *cp = '\0';
262     if (feedback) {
263         /* erase stars */
264         while (cp > buf) {
265             (void) write(fd, "\b \b", 3);
266             --cp;
267         }
268     }
269
270     return(nr == 1 ? buf : NULL);
271 }
272
273 static void
274 handler(s)
275     int s;
276 {
277     if (s != SIGALRM)
278         signo = s;
279 }
280
281 int
282 tty_present()
283 {
284     int fd;
285
286     if ((fd = open(_PATH_TTY, O_RDWR|O_NOCTTY)) != -1)
287         close(fd);
288     return(fd != -1);
289 }