Imported Debian patch 1.6.9p8-1
[debian/sudo] / tgetpass.c
1 /*
2  * Copyright (c) 1996, 1998-2005 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
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.
7  *
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.
15  *
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.
19  */
20
21 #ifdef __TANDEM
22 # include <floss.h>
23 #endif
24
25 #include <config.h>
26
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 */
32 #include <sys/time.h>
33 #include <stdio.h>
34 #ifdef STDC_HEADERS
35 # include <stdlib.h>
36 # include <stddef.h>
37 #else
38 # ifdef HAVE_STDLIB_H
39 #  include <stdlib.h>
40 # endif
41 #endif /* STDC_HEADERS */
42 #ifdef HAVE_STRING_H
43 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
44 #  include <memory.h>
45 # endif
46 # include <string.h>
47 #else
48 # ifdef HAVE_STRINGS_H
49 #  include <strings.h>
50 # endif
51 #endif /* HAVE_STRING_H */
52 #ifdef HAVE_UNISTD_H
53 # include <unistd.h>
54 #endif /* HAVE_UNISTD_H */
55 #include <pwd.h>
56 #include <errno.h>
57 #include <signal.h>
58 #include <fcntl.h>
59 #ifdef HAVE_TERMIOS_H
60 # include <termios.h>
61 #else
62 # ifdef HAVE_TERMIO_H
63 #  include <termio.h>
64 # else
65 #  include <sgtty.h>
66 #  include <sys/ioctl.h>
67 # endif /* HAVE_TERMIO_H */
68 #endif /* HAVE_TERMIOS_H */
69
70 #include "sudo.h"
71
72 #ifndef lint
73 __unused static const char rcsid[] = "$Sudo: tgetpass.c,v 1.111.2.5 2007/10/17 15:39:43 millert Exp $";
74 #endif /* lint */
75
76 #ifndef TCSASOFT
77 # define TCSASOFT       0
78 #endif
79 #ifndef ECHONL
80 # define ECHONL 0
81 #endif
82
83 #ifndef _POSIX_VDISABLE
84 # ifdef VDISABLE
85 #  define _POSIX_VDISABLE       VDISABLE
86 # else
87 #  define _POSIX_VDISABLE       0
88 # endif
89 #endif
90
91 /*
92  * QNX 6 (at least) has issues with TCSAFLUSH.
93  */
94 #ifdef __QNX__
95 #undef TCSAFLUSH
96 #define TCSAFLUSH       TCSADRAIN
97 #endif
98
99 /*
100  * Compat macros for non-termios systems.
101  */
102 #ifndef HAVE_TERMIOS_H
103 # ifdef HAVE_TERMIO_H
104 #  undef termios
105 #  define termios               termio
106 #  define tcgetattr(f, t)       ioctl(f, TCGETA, t)
107 #  define tcsetattr(f, a, t)    ioctl(f, a, t)
108 #  undef TCSAFLUSH
109 #  define TCSAFLUSH             TCSETAF
110 #  undef TCSANOW
111 #  define TCSANOW               TCSETA
112 # else
113 #  undef termios
114 #  define termios               sgttyb
115 #  define c_lflag               sg_flags
116 #  define tcgetattr(f, t)       ioctl(f, TIOCGETP, t)
117 #  define tcsetattr(f, a, t)    ioctl(f, a, t)
118 #  undef TCSAFLUSH
119 #  define TCSAFLUSH             TIOCSETP
120 #  undef TCSANOW
121 #  define TCSANOW               TIOCSETN
122 # endif /* HAVE_TERMIO_H */
123 #endif /* HAVE_TERMIOS_H */
124
125 static volatile sig_atomic_t signo;
126
127 static void handler __P((int));
128 static char *getln __P((int, char *, size_t));
129
130 /*
131  * Like getpass(3) but with timeout and echo flags.
132  */
133 char *
134 tgetpass(prompt, timeout, flags)
135     const char *prompt;
136     int timeout;
137     int flags;
138 {
139     sigaction_t sa, savealrm, saveint, savehup, savequit, saveterm;
140     sigaction_t savetstp, savettin, savettou;
141     struct termios term, oterm;
142     char *pass;
143     static char buf[SUDO_PASS_MAX + 1];
144     int input, output, save_errno;
145
146     (void) fflush(stdout);
147 restart:
148     signo = 0;
149     pass = NULL;
150     save_errno = 0;
151     /* Open /dev/tty for reading/writing if possible else use stdin/stderr. */
152     if (ISSET(flags, TGP_STDIN) ||
153         (input = output = open(_PATH_TTY, O_RDWR|O_NOCTTY)) == -1) {
154         input = STDIN_FILENO;
155         output = STDERR_FILENO;
156     }
157
158     /*
159      * Catch signals that would otherwise cause the user to end
160      * up with echo turned off in the shell.
161      */
162     sigemptyset(&sa.sa_mask);
163     sa.sa_flags = SA_INTERRUPT; /* don't restart system calls */
164     sa.sa_handler = handler;
165     (void) sigaction(SIGALRM, &sa, &savealrm);
166     (void) sigaction(SIGINT, &sa, &saveint);
167     (void) sigaction(SIGHUP, &sa, &savehup);
168     (void) sigaction(SIGQUIT, &sa, &savequit);
169     (void) sigaction(SIGTERM, &sa, &saveterm);
170     (void) sigaction(SIGTSTP, &sa, &savetstp);
171     (void) sigaction(SIGTTIN, &sa, &savettin);
172     (void) sigaction(SIGTTOU, &sa, &savettou);
173
174     /* Turn echo off/on as specified by flags.  */
175     if (tcgetattr(input, &oterm) == 0) {
176         (void) memcpy(&term, &oterm, sizeof(term));
177         if (!ISSET(flags, TGP_ECHO))
178             CLR(term.c_lflag, ECHO|ECHONL);
179 #ifdef VSTATUS
180         term.c_cc[VSTATUS] = _POSIX_VDISABLE;
181 #endif
182         (void) tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
183     } else {
184         memset(&term, 0, sizeof(term));
185         memset(&oterm, 0, sizeof(oterm));
186     }
187
188     /* No output if we are already backgrounded. */
189     if (signo != SIGTTOU && signo != SIGTTIN) {
190         if (prompt)
191             (void) write(output, prompt, strlen(prompt));
192
193         if (timeout > 0)
194             alarm(timeout);
195         pass = getln(input, buf, sizeof(buf));
196         alarm(0);
197         save_errno = errno;
198
199         if (!ISSET(term.c_lflag, ECHO))
200             (void) write(output, "\n", 1);
201     }
202
203     /* Restore old tty settings and signals. */
204     if (memcmp(&term, &oterm, sizeof(term)) != 0) {
205         while (tcsetattr(input, TCSANOW|TCSASOFT, &oterm) == -1 &&
206             errno == EINTR)
207             continue;
208     }
209     (void) sigaction(SIGALRM, &savealrm, NULL);
210     (void) sigaction(SIGINT, &saveint, NULL);
211     (void) sigaction(SIGHUP, &savehup, NULL);
212     (void) sigaction(SIGQUIT, &savequit, NULL);
213     (void) sigaction(SIGTERM, &saveterm, NULL);
214     (void) sigaction(SIGTSTP, &savetstp, NULL);
215     (void) sigaction(SIGTTIN, &savettin, NULL);
216     (void) sigaction(SIGTTOU, &savettou, NULL);
217     if (input != STDIN_FILENO)
218         (void) close(input);
219
220     /*
221      * If we were interrupted by a signal, resend it to ourselves
222      * now that we have restored the signal handlers.
223      */
224     if (signo) {
225         kill(getpid(), signo);
226         switch (signo) {
227             case SIGTSTP:
228             case SIGTTIN:
229             case SIGTTOU:
230                 goto restart;
231         }
232     }
233
234     if (save_errno)
235         errno = save_errno;
236     return(pass);
237 }
238
239 static char *
240 getln(fd, buf, bufsiz)
241     int fd;
242     char *buf;
243     size_t bufsiz;
244 {
245     char c, *cp;
246     ssize_t nr;
247
248     if (bufsiz == 0) {
249         errno = EINVAL;
250         return(NULL);                   /* sanity */
251     }
252
253     cp = buf;
254     nr = -1;
255     while (--bufsiz && (nr = read(fd, &c, 1)) == 1 && c != '\n' && c != '\r')
256         *cp++ = c;
257     *cp = '\0';
258     return(nr == -1 ? NULL : buf);
259 }
260
261 static void
262 handler(s)
263     int s;
264 {
265     if (s != SIGALRM)
266         signo = s;
267 }