2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-09-04 16:25:25 +00:00

Use SA_INTERRUPT so SunOS works correctly, avoid stdio and just use read/write

as it is simpler.
This commit is contained in:
Todd C. Miller
2004-05-28 22:06:50 +00:00
parent 1a76be42f6
commit f8176ceda2

View File

@@ -115,6 +115,7 @@ static const char rcsid[] = "$Sudo$";
static volatile sig_atomic_t signo; static volatile sig_atomic_t signo;
static void handler __P((int)); static void handler __P((int));
static char *getln __P((int, char *, size_t));
/* /*
* Like getpass(3) but with timeout and echo flags. * Like getpass(3) but with timeout and echo flags.
@@ -127,29 +128,26 @@ tgetpass(prompt, timeout, flags)
{ {
sigaction_t sa, savealrm, saveint, savehup, savequit, saveterm; sigaction_t sa, savealrm, saveint, savehup, savequit, saveterm;
sigaction_t savetstp, savettin, savettou; sigaction_t savetstp, savettin, savettou;
FILE *input, *output;
struct TERM term, oterm; struct TERM term, oterm;
char *pass, *ep; char *pass;
static char buf[SUDO_PASS_MAX + 1]; static char buf[SUDO_PASS_MAX + 1];
int fd, save_errno; int input, output, save_errno;
(void) fflush(stdout);
restart: restart:
/* Open /dev/tty for reading/writing if possible else use stdin/stderr. */ /* Open /dev/tty for reading/writing if possible else use stdin/stderr. */
if (ISSET(flags, TGP_STDIN) || if (ISSET(flags, TGP_STDIN) ||
(fd = open(_PATH_TTY, O_RDWR|O_NOCTTY)) == -1 || (input = output = open(_PATH_TTY, O_RDWR|O_NOCTTY)) == -1) {
(input = output = fdopen(fd, "r+")) == NULL) { input = STDIN_FILENO;
input = stdin; output = STDERR_FILENO;
output = stderr;
(void) fflush(output);
} }
/* /*
* Catch signals that would otherwise cause the user to end * Catch signals that would otherwise cause the user to end
* up with echo turned off in the shell. Don't worry about * up with echo turned off in the shell.
* things like SIGALRM and SIGPIPE for now.
*/ */
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
sa.sa_flags = 0; /* don't restart system calls */ sa.sa_flags = SA_INTERRUPT; /* don't restart system calls */
sa.sa_handler = handler; sa.sa_handler = handler;
(void) sigaction(SIGALRM, &sa, &savealrm); (void) sigaction(SIGALRM, &sa, &savealrm);
(void) sigaction(SIGINT, &sa, &saveint); (void) sigaction(SIGINT, &sa, &saveint);
@@ -161,43 +159,34 @@ restart:
(void) sigaction(SIGTTOU, &sa, &savettou); (void) sigaction(SIGTTOU, &sa, &savettou);
/* Turn echo off/on as specified by flags. */ /* Turn echo off/on as specified by flags. */
if (term_getattr(fileno(input), &oterm) == 0) { if (term_getattr(input, &oterm) == 0) {
(void) memcpy(&term, &oterm, sizeof(term)); (void) memcpy(&term, &oterm, sizeof(term));
if (!ISSET(flags, TGP_ECHO)) if (!ISSET(flags, TGP_ECHO))
CLR(term.tflags, (ECHO | ECHONL)); CLR(term.tflags, (ECHO | ECHONL));
#ifdef VSTATUS #ifdef VSTATUS
term.c_cc[VSTATUS] = _POSIX_VDISABLE; term.c_cc[VSTATUS] = _POSIX_VDISABLE;
#endif #endif
(void) term_setattr(fileno(input), &term); (void) term_setattr(input, &term);
} else { } else {
memset(&term, 0, sizeof(term)); memset(&term, 0, sizeof(term));
memset(&oterm, 0, sizeof(oterm)); memset(&oterm, 0, sizeof(oterm));
} }
if (prompt) { if (prompt)
(void) fputs(prompt, output); (void) write(output, prompt, strlen(prompt));
(void) fflush(output);
if (input == output)
(void) rewind(input);
}
if (timeout > 0) if (timeout > 0)
alarm(timeout); alarm(timeout);
pass = fgets(buf, sizeof(buf), input); pass = getln(input, buf, sizeof(buf));
alarm(0); alarm(0);
if (pass != NULL) {
ep = pass + strlen(pass);
while (ep > pass && (*--ep == '\n' || *ep == '\r'))
*ep = '\0';
}
save_errno = errno; save_errno = errno;
if (!ISSET(term.tflags, ECHO)) if (!ISSET(term.tflags, ECHO))
fputc('\n', output); (void) write(output, "\n", 1);
/* Restore old tty settings and signals. */ /* Restore old tty settings and signals. */
if (memcmp(&term, &oterm, sizeof(term)) != 0) if (memcmp(&term, &oterm, sizeof(term)) != 0)
(void) term_setattr(fileno(input), &oterm); (void) term_setattr(input, &oterm);
(void) sigaction(SIGALRM, &savealrm, NULL); (void) sigaction(SIGALRM, &savealrm, NULL);
(void) sigaction(SIGINT, &saveint, NULL); (void) sigaction(SIGINT, &saveint, NULL);
(void) sigaction(SIGHUP, &savehup, NULL); (void) sigaction(SIGHUP, &savehup, NULL);
@@ -206,8 +195,8 @@ restart:
(void) sigaction(SIGTSTP, &savetstp, NULL); (void) sigaction(SIGTSTP, &savetstp, NULL);
(void) sigaction(SIGTTIN, &savettin, NULL); (void) sigaction(SIGTTIN, &savettin, NULL);
(void) sigaction(SIGTTOU, &savettou, NULL); (void) sigaction(SIGTTOU, &savettou, NULL);
if (input != stdin) if (input != STDIN_FILENO)
(void) fclose(input); (void) close(input);
/* /*
* If we were interrupted by a signal, resend it to ourselves * If we were interrupted by a signal, resend it to ourselves
@@ -228,6 +217,28 @@ restart:
return(pass); return(pass);
} }
static char *
getln(fd, buf, bufsiz)
int fd;
char *buf;
size_t bufsiz;
{
char c, *cp;
ssize_t nr;
if (bufsiz == 0) {
errno = EINVAL;
return(NULL); /* sanity */
}
cp = buf;
nr = -1;
while (--bufsiz && (nr = read(fd, &c, 1)) == 1 && c != '\n' && c != '\r')
*cp++ = c;
*cp = '\0';
return(nr == -1 ? NULL : buf);
}
static void static void
handler(s) handler(s)
int s; int s;