X-Git-Url: http://andersk.mit.edu/gitweb/openssh.git/blobdiff_plain/c4087616f1a10711c137daf020267a2557417138..c9ecc3c71562790fd69d7d595322e9eca55b875b:/readpass.c diff --git a/readpass.c b/readpass.c index 95ec5d87..7914799a 100644 --- a/readpass.c +++ b/readpass.c @@ -23,10 +23,10 @@ */ #include "includes.h" -RCSID("$OpenBSD: readpass.c,v 1.28 2003/01/23 13:50:27 markus Exp $"); +RCSID("$OpenBSD: readpass.c,v 1.33 2005/05/02 21:13:22 markus Exp $"); #include "xmalloc.h" -#include "readpass.h" +#include "misc.h" #include "pathnames.h" #include "log.h" #include "ssh.h" @@ -103,18 +103,28 @@ read_passphrase(const char *prompt, int flags) int rppflags, use_askpass = 0, ttyfd; rppflags = (flags & RP_ECHO) ? RPP_ECHO_ON : RPP_ECHO_OFF; - if (flags & RP_ALLOW_STDIN) { - if (!isatty(STDIN_FILENO)) + if (flags & RP_USE_ASKPASS) + use_askpass = 1; + else if (flags & RP_ALLOW_STDIN) { + if (!isatty(STDIN_FILENO)) { + debug("read_passphrase: stdin is not a tty"); use_askpass = 1; + } } else { rppflags |= RPP_REQUIRE_TTY; ttyfd = open(_PATH_TTY, O_RDWR); if (ttyfd >= 0) close(ttyfd); - else + else { + debug("read_passphrase: can't open %s: %s", _PATH_TTY, + strerror(errno)); use_askpass = 1; + } } + if ((flags & RP_USE_ASKPASS) && getenv("DISPLAY") == NULL) + return (flags & RP_ALLOW_EOF) ? NULL : xstrdup(""); + if (use_askpass && getenv("DISPLAY")) { if (getenv(SSH_ASKPASS_ENV)) askpass = getenv(SSH_ASKPASS_ENV); @@ -136,3 +146,29 @@ read_passphrase(const char *prompt, int flags) memset(buf, 'x', sizeof buf); return ret; } + +int +ask_permission(const char *fmt, ...) +{ + va_list args; + char *p, prompt[1024]; + int allowed = 0; + + va_start(args, fmt); + vsnprintf(prompt, sizeof(prompt), fmt, args); + va_end(args); + + p = read_passphrase(prompt, RP_USE_ASKPASS|RP_ALLOW_EOF); + if (p != NULL) { + /* + * Accept empty responses and responses consisting + * of the word "yes" as affirmative. + */ + if (*p == '\0' || *p == '\n' || + strcasecmp(p, "yes") == 0) + allowed = 1; + xfree(p); + } + + return (allowed); +}