2021-07-23 07:56:47 +02:00
|
|
|
/* $OpenBSD: readpass.c,v 1.69 2021/07/23 05:56:47 djm Exp $ */
|
1999-10-27 05:42:43 +02:00
|
|
|
/*
|
2002-02-13 04:05:23 +01:00
|
|
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
1999-12-09 00:31:37 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
2002-02-13 04:05:23 +01:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1999-11-24 14:26:21 +01:00
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
|
|
|
|
#include "includes.h"
|
2006-03-15 01:29:24 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2006-03-15 01:16:59 +01:00
|
|
|
|
2006-07-12 14:22:46 +02:00
|
|
|
#include <errno.h>
|
2006-07-10 13:13:46 +02:00
|
|
|
#include <fcntl.h>
|
2006-03-15 01:16:59 +01:00
|
|
|
#ifdef HAVE_PATHS_H
|
2006-03-15 01:18:26 +01:00
|
|
|
# include <paths.h>
|
2006-03-15 01:16:59 +01:00
|
|
|
#endif
|
2011-01-06 12:43:44 +01:00
|
|
|
#include <signal.h>
|
2006-07-12 14:15:16 +02:00
|
|
|
#include <stdarg.h>
|
2006-08-05 03:37:59 +02:00
|
|
|
#include <stdio.h>
|
2006-08-05 03:34:19 +02:00
|
|
|
#include <stdlib.h>
|
2006-07-24 06:13:33 +02:00
|
|
|
#include <string.h>
|
2006-07-24 06:01:23 +02:00
|
|
|
#include <unistd.h>
|
2001-06-25 07:20:31 +02:00
|
|
|
|
1999-10-27 05:42:43 +02:00
|
|
|
#include "xmalloc.h"
|
2004-05-13 08:15:47 +02:00
|
|
|
#include "misc.h"
|
2001-04-19 22:33:07 +02:00
|
|
|
#include "pathnames.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "ssh.h"
|
2006-06-13 05:05:15 +02:00
|
|
|
#include "uidswap.h"
|
2001-04-19 22:33:07 +02:00
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static char *
|
2019-12-06 04:06:08 +01:00
|
|
|
ssh_askpass(char *askpass, const char *msg, const char *env_hint)
|
2001-04-19 22:33:07 +02:00
|
|
|
{
|
2011-01-06 12:43:44 +01:00
|
|
|
pid_t pid, ret;
|
2001-04-19 22:33:07 +02:00
|
|
|
size_t len;
|
2001-11-12 01:05:20 +01:00
|
|
|
char *pass;
|
2011-01-06 12:43:44 +01:00
|
|
|
int p[2], status;
|
2001-04-19 22:33:07 +02:00
|
|
|
char buf[1024];
|
2011-01-06 12:43:44 +01:00
|
|
|
void (*osigchld)(int);
|
2001-04-19 22:33:07 +02:00
|
|
|
|
|
|
|
if (fflush(stdout) != 0)
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("fflush: %s", strerror(errno));
|
2001-04-19 22:33:07 +02:00
|
|
|
if (askpass == NULL)
|
|
|
|
fatal("internal error: askpass undefined");
|
2019-06-28 15:35:04 +02:00
|
|
|
if (pipe(p) == -1) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("pipe: %s", strerror(errno));
|
2003-01-24 01:36:23 +01:00
|
|
|
return NULL;
|
2001-07-14 04:19:56 +02:00
|
|
|
}
|
2020-01-23 08:10:22 +01:00
|
|
|
osigchld = ssh_signal(SIGCHLD, SIG_DFL);
|
2019-06-28 15:35:04 +02:00
|
|
|
if ((pid = fork()) == -1) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("fork: %s", strerror(errno));
|
2020-01-23 08:10:22 +01:00
|
|
|
ssh_signal(SIGCHLD, osigchld);
|
2003-01-24 01:36:23 +01:00
|
|
|
return NULL;
|
2001-07-14 04:19:56 +02:00
|
|
|
}
|
2001-04-19 22:33:07 +02:00
|
|
|
if (pid == 0) {
|
|
|
|
close(p[0]);
|
2019-06-28 15:35:04 +02:00
|
|
|
if (dup2(p[1], STDOUT_FILENO) == -1)
|
2020-10-18 13:32:01 +02:00
|
|
|
fatal_f("dup2: %s", strerror(errno));
|
2019-12-06 04:06:08 +01:00
|
|
|
if (env_hint != NULL)
|
|
|
|
setenv("SSH_ASKPASS_PROMPT", env_hint, 1);
|
2015-12-11 01:20:04 +01:00
|
|
|
execlp(askpass, askpass, msg, (char *)NULL);
|
2020-10-18 13:32:01 +02:00
|
|
|
fatal_f("exec(%s): %s", askpass, strerror(errno));
|
2001-04-19 22:33:07 +02:00
|
|
|
}
|
|
|
|
close(p[1]);
|
2002-01-22 13:05:31 +01:00
|
|
|
|
2011-01-06 12:43:44 +01:00
|
|
|
len = 0;
|
2002-01-22 13:05:31 +01:00
|
|
|
do {
|
2011-01-06 12:43:44 +01:00
|
|
|
ssize_t r = read(p[0], buf + len, sizeof(buf) - 1 - len);
|
|
|
|
|
|
|
|
if (r == -1 && errno == EINTR)
|
2002-01-22 13:05:31 +01:00
|
|
|
continue;
|
2011-01-06 12:43:44 +01:00
|
|
|
if (r <= 0)
|
2002-01-22 13:05:31 +01:00
|
|
|
break;
|
2011-01-06 12:43:44 +01:00
|
|
|
len += r;
|
2002-01-22 13:05:31 +01:00
|
|
|
} while (sizeof(buf) - 1 - len > 0);
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
2001-04-19 22:33:07 +02:00
|
|
|
close(p[0]);
|
2019-06-28 15:35:04 +02:00
|
|
|
while ((ret = waitpid(pid, &status, 0)) == -1)
|
2001-04-19 22:33:07 +02:00
|
|
|
if (errno != EINTR)
|
|
|
|
break;
|
2020-01-23 08:10:22 +01:00
|
|
|
ssh_signal(SIGCHLD, osigchld);
|
2011-01-06 12:43:44 +01:00
|
|
|
if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
2014-02-04 01:20:14 +01:00
|
|
|
explicit_bzero(buf, sizeof(buf));
|
2003-01-24 01:36:23 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-11-12 01:05:20 +01:00
|
|
|
buf[strcspn(buf, "\r\n")] = '\0';
|
2001-04-19 22:33:07 +02:00
|
|
|
pass = xstrdup(buf);
|
2014-02-04 01:20:14 +01:00
|
|
|
explicit_bzero(buf, sizeof(buf));
|
2001-04-19 22:33:07 +02:00
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
2019-12-06 04:06:08 +01:00
|
|
|
/* private/internal read_passphrase flags */
|
|
|
|
#define RP_ASK_PERMISSION 0x8000 /* pass hint to askpass for confirm UI */
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
2001-06-25 07:20:31 +02:00
|
|
|
* Reads a passphrase from /dev/tty with echo turned off/on. Returns the
|
|
|
|
* passphrase (allocated with xmalloc). Exits if EOF is encountered. If
|
|
|
|
* RP_ALLOW_STDIN is set, the passphrase will be read from stdin if no
|
2021-07-23 07:56:47 +02:00
|
|
|
* tty is or askpass program is available
|
2000-10-14 07:23:11 +02:00
|
|
|
*/
|
1999-11-24 14:26:21 +01:00
|
|
|
char *
|
2001-06-25 07:20:31 +02:00
|
|
|
read_passphrase(const char *prompt, int flags)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2019-01-19 05:15:56 +01:00
|
|
|
char cr = '\r', *askpass = NULL, *ret, buf[1024];
|
2020-07-15 01:57:01 +02:00
|
|
|
int rppflags, ttyfd, use_askpass = 0, allow_askpass = 0;
|
2019-12-06 04:06:08 +01:00
|
|
|
const char *askpass_hint = NULL;
|
2020-07-15 01:57:01 +02:00
|
|
|
const char *s;
|
|
|
|
|
|
|
|
if ((s = getenv("DISPLAY")) != NULL)
|
|
|
|
allow_askpass = *s != '\0';
|
|
|
|
if ((s = getenv(SSH_ASKPASS_REQUIRE_ENV)) != NULL) {
|
|
|
|
if (strcasecmp(s, "force") == 0) {
|
|
|
|
use_askpass = 1;
|
|
|
|
allow_askpass = 1;
|
|
|
|
} else if (strcasecmp(s, "prefer") == 0)
|
|
|
|
use_askpass = allow_askpass;
|
|
|
|
else if (strcasecmp(s, "never") == 0)
|
|
|
|
allow_askpass = 0;
|
|
|
|
}
|
2001-04-19 22:33:07 +02:00
|
|
|
|
2001-06-25 07:20:31 +02:00
|
|
|
rppflags = (flags & RP_ECHO) ? RPP_ECHO_ON : RPP_ECHO_OFF;
|
2020-07-15 01:57:01 +02:00
|
|
|
if (use_askpass)
|
2020-10-18 13:32:01 +02:00
|
|
|
debug_f("requested to askpass");
|
2020-07-15 01:57:01 +02:00
|
|
|
else if (flags & RP_USE_ASKPASS)
|
2004-06-17 17:19:03 +02:00
|
|
|
use_askpass = 1;
|
|
|
|
else if (flags & RP_ALLOW_STDIN) {
|
2005-05-26 04:07:47 +02:00
|
|
|
if (!isatty(STDIN_FILENO)) {
|
2021-07-23 07:56:47 +02:00
|
|
|
debug_f("stdin is not a tty");
|
2001-04-19 22:33:07 +02:00
|
|
|
use_askpass = 1;
|
2005-05-26 04:07:47 +02:00
|
|
|
}
|
2001-04-19 22:33:07 +02:00
|
|
|
} else {
|
2001-06-25 07:20:31 +02:00
|
|
|
rppflags |= RPP_REQUIRE_TTY;
|
2002-01-22 13:24:51 +01:00
|
|
|
ttyfd = open(_PATH_TTY, O_RDWR);
|
2019-01-19 05:15:56 +01:00
|
|
|
if (ttyfd >= 0) {
|
|
|
|
/*
|
|
|
|
* If we're on a tty, ensure that show the prompt at
|
|
|
|
* the beginning of the line. This will hopefully
|
|
|
|
* clobber any password characters the user has
|
|
|
|
* optimistically typed before echo is disabled.
|
|
|
|
*/
|
|
|
|
(void)write(ttyfd, &cr, 1);
|
2001-04-19 22:33:07 +02:00
|
|
|
close(ttyfd);
|
2019-01-19 05:15:56 +01:00
|
|
|
} else {
|
2021-07-23 07:56:47 +02:00
|
|
|
debug_f("can't open %s: %s", _PATH_TTY,
|
2005-05-26 04:05:28 +02:00
|
|
|
strerror(errno));
|
2001-04-19 22:33:07 +02:00
|
|
|
use_askpass = 1;
|
2005-05-26 04:05:28 +02:00
|
|
|
}
|
2001-04-19 22:33:07 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:57:01 +02:00
|
|
|
if ((flags & RP_USE_ASKPASS) && !allow_askpass)
|
2004-06-17 17:19:03 +02:00
|
|
|
return (flags & RP_ALLOW_EOF) ? NULL : xstrdup("");
|
|
|
|
|
2020-07-15 01:57:01 +02:00
|
|
|
if (use_askpass && allow_askpass) {
|
2001-04-19 22:33:07 +02:00
|
|
|
if (getenv(SSH_ASKPASS_ENV))
|
|
|
|
askpass = getenv(SSH_ASKPASS_ENV);
|
|
|
|
else
|
|
|
|
askpass = _PATH_SSH_ASKPASS_DEFAULT;
|
2019-12-06 04:06:08 +01:00
|
|
|
if ((flags & RP_ASK_PERMISSION) != 0)
|
|
|
|
askpass_hint = "confirm";
|
|
|
|
if ((ret = ssh_askpass(askpass, prompt, askpass_hint)) == NULL)
|
2003-01-24 01:36:23 +01:00
|
|
|
if (!(flags & RP_ALLOW_EOF))
|
|
|
|
return xstrdup("");
|
|
|
|
return ret;
|
2001-04-19 22:33:07 +02:00
|
|
|
}
|
|
|
|
|
2002-03-27 18:28:46 +01:00
|
|
|
if (readpassphrase(prompt, buf, sizeof buf, rppflags) == NULL) {
|
|
|
|
if (flags & RP_ALLOW_EOF)
|
|
|
|
return NULL;
|
2001-07-04 07:19:27 +02:00
|
|
|
return xstrdup("");
|
2002-03-27 18:28:46 +01:00
|
|
|
}
|
2001-06-25 07:20:31 +02:00
|
|
|
|
|
|
|
ret = xstrdup(buf);
|
2014-02-04 01:20:14 +01:00
|
|
|
explicit_bzero(buf, sizeof(buf));
|
2001-06-25 07:20:31 +02:00
|
|
|
return ret;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
2004-11-05 10:38:03 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-12-06 04:06:08 +01:00
|
|
|
p = read_passphrase(prompt,
|
|
|
|
RP_USE_ASKPASS|RP_ALLOW_EOF|RP_ASK_PERMISSION);
|
2004-11-05 10:38:03 +01:00
|
|
|
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;
|
2013-06-01 23:31:17 +02:00
|
|
|
free(p);
|
2004-11-05 10:38:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (allowed);
|
|
|
|
}
|
2019-11-12 23:34:20 +01:00
|
|
|
|
2020-11-08 23:37:24 +01:00
|
|
|
static void
|
|
|
|
writemsg(const char *msg)
|
|
|
|
{
|
|
|
|
(void)write(STDERR_FILENO, "\r", 1);
|
|
|
|
(void)write(STDERR_FILENO, msg, strlen(msg));
|
|
|
|
(void)write(STDERR_FILENO, "\r\n", 2);
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:34:20 +01:00
|
|
|
struct notifier_ctx {
|
|
|
|
pid_t pid;
|
|
|
|
void (*osigchld)(int);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct notifier_ctx *
|
|
|
|
notify_start(int force_askpass, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char *prompt = NULL;
|
2020-11-08 23:37:24 +01:00
|
|
|
pid_t pid = -1;
|
|
|
|
void (*osigchld)(int) = NULL;
|
2020-08-11 11:45:54 +02:00
|
|
|
const char *askpass, *s;
|
|
|
|
struct notifier_ctx *ret = NULL;
|
2019-11-12 23:34:20 +01:00
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
xvasprintf(&prompt, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (fflush(NULL) != 0)
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("fflush: %s", strerror(errno));
|
2019-11-12 23:34:20 +01:00
|
|
|
if (!force_askpass && isatty(STDERR_FILENO)) {
|
2020-11-08 23:37:24 +01:00
|
|
|
writemsg(prompt);
|
|
|
|
goto out_ctx;
|
2019-11-12 23:34:20 +01:00
|
|
|
}
|
2019-11-15 01:32:40 +01:00
|
|
|
if ((askpass = getenv("SSH_ASKPASS")) == NULL)
|
|
|
|
askpass = _PATH_SSH_ASKPASS_DEFAULT;
|
2020-08-11 11:45:54 +02:00
|
|
|
if (*askpass == '\0') {
|
2020-10-18 13:32:01 +02:00
|
|
|
debug3_f("cannot notify: no askpass");
|
2020-08-11 11:45:54 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (getenv("DISPLAY") == NULL &&
|
|
|
|
((s = getenv(SSH_ASKPASS_REQUIRE_ENV)) == NULL ||
|
|
|
|
strcmp(s, "force") != 0)) {
|
2020-10-18 13:32:01 +02:00
|
|
|
debug3_f("cannot notify: no display");
|
2020-08-11 11:45:54 +02:00
|
|
|
goto out;
|
2019-11-12 23:34:20 +01:00
|
|
|
}
|
2020-01-23 08:10:22 +01:00
|
|
|
osigchld = ssh_signal(SIGCHLD, SIG_DFL);
|
2019-11-12 23:34:20 +01:00
|
|
|
if ((pid = fork()) == -1) {
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("fork: %s", strerror(errno));
|
2020-01-23 08:10:22 +01:00
|
|
|
ssh_signal(SIGCHLD, osigchld);
|
2019-11-12 23:34:20 +01:00
|
|
|
free(prompt);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (pid == 0) {
|
2020-10-03 11:22:26 +02:00
|
|
|
if (stdfd_devnull(1, 1, 0) == -1)
|
2020-10-18 13:32:01 +02:00
|
|
|
fatal_f("stdfd_devnull failed");
|
2019-11-12 23:34:20 +01:00
|
|
|
closefrom(STDERR_FILENO + 1);
|
|
|
|
setenv("SSH_ASKPASS_PROMPT", "none", 1); /* hint to UI */
|
|
|
|
execlp(askpass, askpass, prompt, (char *)NULL);
|
2020-10-18 13:32:01 +02:00
|
|
|
error_f("exec(%s): %s", askpass, strerror(errno));
|
2019-11-27 06:00:17 +01:00
|
|
|
_exit(1);
|
2019-11-12 23:34:20 +01:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2020-11-08 23:37:24 +01:00
|
|
|
out_ctx:
|
2019-11-12 23:34:20 +01:00
|
|
|
if ((ret = calloc(1, sizeof(*ret))) == NULL) {
|
|
|
|
kill(pid, SIGTERM);
|
2020-10-18 13:32:01 +02:00
|
|
|
fatal_f("calloc failed");
|
2019-11-12 23:34:20 +01:00
|
|
|
}
|
|
|
|
ret->pid = pid;
|
|
|
|
ret->osigchld = osigchld;
|
2020-08-11 11:45:54 +02:00
|
|
|
out:
|
2019-11-12 23:34:20 +01:00
|
|
|
free(prompt);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-11-08 23:37:24 +01:00
|
|
|
notify_complete(struct notifier_ctx *ctx, const char *fmt, ...)
|
2019-11-12 23:34:20 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2020-11-08 23:37:24 +01:00
|
|
|
char *msg = NULL;
|
|
|
|
va_list args;
|
|
|
|
|
2020-11-09 00:19:03 +01:00
|
|
|
if (ctx != NULL && fmt != NULL && ctx->pid == -1) {
|
2020-11-08 23:37:24 +01:00
|
|
|
/*
|
|
|
|
* notify_start wrote to stderr, so send conclusion message
|
|
|
|
* there too
|
|
|
|
*/
|
|
|
|
va_start(args, fmt);
|
|
|
|
xvasprintf(&msg, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
writemsg(msg);
|
2020-11-10 08:46:20 +01:00
|
|
|
free(msg);
|
2020-11-08 23:37:24 +01:00
|
|
|
}
|
2019-11-12 23:34:20 +01:00
|
|
|
|
|
|
|
if (ctx == NULL || ctx->pid <= 0) {
|
|
|
|
free(ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kill(ctx->pid, SIGTERM);
|
|
|
|
while ((ret = waitpid(ctx->pid, NULL, 0)) == -1) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret == -1)
|
2020-10-18 13:32:01 +02:00
|
|
|
fatal_f("waitpid: %s", strerror(errno));
|
2020-01-23 08:10:22 +01:00
|
|
|
ssh_signal(SIGCHLD, ctx->osigchld);
|
2019-11-12 23:34:20 +01:00
|
|
|
free(ctx);
|
|
|
|
}
|