summaryrefslogtreecommitdiff
path: root/openbsd-compat/readpassphrase.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2002-09-11 10:29:11 +1000
committerDamien Miller <djm@mindrot.org>2002-09-11 10:29:11 +1000
commit71eb0c1550ec6ad10d29779df7a7f97c44000e3a (patch)
tree1cbfd081547bcf3bfcc26e17f2f4537a03ced5ff /openbsd-compat/readpassphrase.c
parentc34e03e4711cabffae3504bcfdac26b67250c45d (diff)
downloadopenssh-git-71eb0c1550ec6ad10d29779df7a7f97c44000e3a.tar.gz
- (djm) Sync openbsd-compat with OpenBSD -current
Diffstat (limited to 'openbsd-compat/readpassphrase.c')
-rw-r--r--openbsd-compat/readpassphrase.c35
1 files changed, 21 insertions, 14 deletions
diff --git a/openbsd-compat/readpassphrase.c b/openbsd-compat/readpassphrase.c
index 8c2f5f84..4e549b62 100644
--- a/openbsd-compat/readpassphrase.c
+++ b/openbsd-compat/readpassphrase.c
@@ -1,7 +1,7 @@
-/* $OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $ */
+/* $OpenBSD: readpassphrase.c,v 1.14 2002/06/28 01:43:58 millert Exp $ */
/*
- * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2000-2002 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -28,7 +28,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $";
+static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.14 2002/06/28 01:43:58 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include "includes.h"
@@ -60,8 +60,8 @@ readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
int input, output, save_errno;
char ch, *p, *end;
struct termios term, oterm;
- struct sigaction sa, saveint, savehup, savequit, saveterm;
- struct sigaction savetstp, savettin, savettou;
+ struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
+ struct sigaction savetstp, savettin, savettou, savepipe;
/* I suppose we could alloc on demand in this case (XXX). */
if (bufsiz == 0) {
@@ -70,11 +70,13 @@ readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
}
restart:
+ signo = 0;
/*
* Read and write to /dev/tty if available. If not, read from
* stdin and write to stderr unless a tty is required.
*/
- if ((input = output = open(_PATH_TTY, O_RDWR)) == -1) {
+ if ((flags & RPP_STDIN) ||
+ (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
if (flags & RPP_REQUIRE_TTY) {
errno = ENOTTY;
return(NULL);
@@ -86,13 +88,15 @@ restart:
/*
* Catch signals that would otherwise cause the user to end
* up with echo turned off in the shell. Don't worry about
- * things like SIGALRM and SIGPIPE for now.
+ * things like SIGXCPU and SIGVTALRM for now.
*/
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0; /* don't restart system calls */
sa.sa_handler = handler;
- (void)sigaction(SIGINT, &sa, &saveint);
+ (void)sigaction(SIGALRM, &sa, &savealrm);
(void)sigaction(SIGHUP, &sa, &savehup);
+ (void)sigaction(SIGINT, &sa, &saveint);
+ (void)sigaction(SIGPIPE, &sa, &savepipe);
(void)sigaction(SIGQUIT, &sa, &savequit);
(void)sigaction(SIGTERM, &sa, &saveterm);
(void)sigaction(SIGTSTP, &sa, &savetstp);
@@ -100,7 +104,7 @@ restart:
(void)sigaction(SIGTTOU, &sa, &savettou);
/* Turn off echo if possible. */
- if (tcgetattr(input, &oterm) == 0) {
+ if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
memcpy(&term, &oterm, sizeof(term));
if (!(flags & RPP_ECHO_ON))
term.c_lflag &= ~(ECHO | ECHONL);
@@ -111,10 +115,13 @@ restart:
(void)tcsetattr(input, _T_FLUSH, &term);
} else {
memset(&term, 0, sizeof(term));
+ term.c_lflag |= ECHO;
memset(&oterm, 0, sizeof(oterm));
+ oterm.c_lflag |= ECHO;
}
- (void)write(output, prompt, strlen(prompt));
+ if (!(flags & RPP_STDIN))
+ (void)write(output, prompt, strlen(prompt));
end = buf + bufsiz - 1;
for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
if (p < end) {
@@ -137,13 +144,14 @@ restart:
/* Restore old terminal settings and signals. */
if (memcmp(&term, &oterm, sizeof(term)) != 0)
(void)tcsetattr(input, _T_FLUSH, &oterm);
- (void)sigaction(SIGINT, &saveint, NULL);
+ (void)sigaction(SIGALRM, &savealrm, NULL);
(void)sigaction(SIGHUP, &savehup, NULL);
+ (void)sigaction(SIGINT, &saveint, NULL);
(void)sigaction(SIGQUIT, &savequit, NULL);
+ (void)sigaction(SIGPIPE, &savepipe, NULL);
(void)sigaction(SIGTERM, &saveterm, NULL);
(void)sigaction(SIGTSTP, &savetstp, NULL);
(void)sigaction(SIGTTIN, &savettin, NULL);
- (void)sigaction(SIGTTOU, &savettou, NULL);
if (input != STDIN_FILENO)
(void)close(input);
@@ -152,12 +160,11 @@ restart:
* now that we have restored the signal handlers.
*/
if (signo) {
- kill(getpid(), signo);
+ kill(getpid(), signo);
switch (signo) {
case SIGTSTP:
case SIGTTIN:
case SIGTTOU:
- signo = 0;
goto restart;
}
}