Imported Upstream version 1.6.9p12
[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.6 2008/01/16 18:03:24 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 # else
111 #  undef termios
112 #  define termios               sgttyb
113 #  define c_lflag               sg_flags
114 #  define tcgetattr(f, t)       ioctl(f, TIOCGETP, t)
115 #  define tcsetattr(f, a, t)    ioctl(f, a, t)
116 #  undef TCSAFLUSH
117 #  define TCSAFLUSH             TIOCSETP
118 # endif /* HAVE_TERMIO_H */
119 #endif /* HAVE_TERMIOS_H */
120
121 static volatile sig_atomic_t signo;
122
123 static void handler __P((int));
124 static char *getln __P((int, char *, size_t));
125
126 /*
127  * Like getpass(3) but with timeout and echo flags.
128  */
129 char *
130 tgetpass(prompt, timeout, flags)
131     const char *prompt;
132     int timeout;
133     int flags;
134 {
135     sigaction_t sa, savealrm, saveint, savehup, savequit, saveterm;
136     sigaction_t savetstp, savettin, savettou;
137     struct termios term, oterm;
138     char *pass;
139     static char buf[SUDO_PASS_MAX + 1];
140     int input, output, save_errno;
141
142     (void) fflush(stdout);
143 restart:
144     signo = 0;
145     pass = NULL;
146     save_errno = 0;
147     /* Open /dev/tty for reading/writing if possible else use stdin/stderr. */
148     if (ISSET(flags, TGP_STDIN) ||
149         (input = output = open(_PATH_TTY, O_RDWR|O_NOCTTY)) == -1) {
150         input = STDIN_FILENO;
151         output = STDERR_FILENO;
152     }
153
154     /*
155      * Catch signals that would otherwise cause the user to end
156      * up with echo turned off in the shell.
157      */
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);
169
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);
175 #ifdef VSTATUS
176         term.c_cc[VSTATUS] = _POSIX_VDISABLE;
177 #endif
178         (void) tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
179     } else {
180         memset(&term, 0, sizeof(term));
181         memset(&oterm, 0, sizeof(oterm));
182     }
183
184     /* No output if we are already backgrounded. */
185     if (signo != SIGTTOU && signo != SIGTTIN) {
186         if (prompt)
187             (void) write(output, prompt, strlen(prompt));
188
189         if (timeout > 0)
190             alarm(timeout);
191         pass = getln(input, buf, sizeof(buf));
192         alarm(0);
193         save_errno = errno;
194
195         if (!ISSET(term.c_lflag, ECHO))
196             (void) write(output, "\n", 1);
197     }
198
199     /* Restore old tty settings and signals. */
200     if (memcmp(&term, &oterm, sizeof(term)) != 0) {
201         while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
202             errno == EINTR)
203             continue;
204     }
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)
214         (void) close(input);
215
216     /*
217      * If we were interrupted by a signal, resend it to ourselves
218      * now that we have restored the signal handlers.
219      */
220     if (signo) {
221         kill(getpid(), signo);
222         switch (signo) {
223             case SIGTSTP:
224             case SIGTTIN:
225             case SIGTTOU:
226                 goto restart;
227         }
228     }
229
230     if (save_errno)
231         errno = save_errno;
232     return(pass);
233 }
234
235 static char *
236 getln(fd, buf, bufsiz)
237     int fd;
238     char *buf;
239     size_t bufsiz;
240 {
241     char c, *cp;
242     ssize_t nr;
243
244     if (bufsiz == 0) {
245         errno = EINVAL;
246         return(NULL);                   /* sanity */
247     }
248
249     cp = buf;
250     nr = -1;
251     while (--bufsiz && (nr = read(fd, &c, 1)) == 1 && c != '\n' && c != '\r')
252         *cp++ = c;
253     *cp = '\0';
254     return(nr == -1 ? NULL : buf);
255 }
256
257 static void
258 handler(s)
259     int s;
260 {
261     if (s != SIGALRM)
262         signo = s;
263 }