2022-05-27 04:27:49 +00:00
|
|
|
/* $OpenBSD: readpass.c,v 1.70 2022/05/27 04:27:49 dtucker Exp $ */
|
1999-10-27 03:42:43 +00:00
|
|
|
/*
|
2002-02-13 03:05:23 +00:00
|
|
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
1999-12-08 23:31:37 +00: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 03:05:23 +00: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 13:26:21 +00:00
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
#include "includes.h"
|
2006-03-15 00:29:24 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2006-03-15 00:16:59 +00:00
|
|
|
|
2006-07-12 12:22:46 +00:00
|
|
|
#include <errno.h>
|
2006-07-10 11:13:46 +00:00
|
|
|
#include <fcntl.h>
|
2006-03-15 00:16:59 +00:00
|
|
|
#ifdef HAVE_PATHS_H
|
2006-03-15 00:18:26 +00:00
|
|
|
# include <paths.h>
|
2006-03-15 00:16:59 +00:00
|
|
|
#endif
|
2011-01-06 11:43:44 +00:00
|
|
|
#include <signal.h>
|
2006-07-12 12:15:16 +00:00
|
|
|
#include <stdarg.h>
|
2006-08-05 01:37:59 +00:00
|
|
|
#include <stdio.h>
|
2006-08-05 01:34:19 +00:00
|
|
|
#include <stdlib.h>
|
2006-07-24 04:13:33 +00:00
|
|
|
#include <string.h>
|
2006-07-24 04:01:23 +00:00
|
|
|
#include <unistd.h>
|
2001-06-25 05:20:31 +00:00
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
#include "xmalloc.h"
|
2004-05-13 06:15:47 +00:00
|
|
|
#include "misc.h"
|
2001-04-19 20:33:07 +00:00
|
|
|
#include "pathnames.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "ssh.h"
|
2006-06-13 03:05:15 +00:00
|
|
|
#include "uidswap.h"
|
2001-04-19 20:33:07 +00:00
|
|
|
|
2001-06-25 05:01:22 +00:00
|
|
|
static char *
|
2019-12-06 03:06:08 +00:00
|
|
|
ssh_askpass(char *askpass, const char *msg, const char *env_hint)
|
2001-04-19 20:33:07 +00:00
|
|
|
{
|
2011-01-06 11:43:44 +00:00
|
|
|
pid_t pid, ret;
|
2001-04-19 20:33:07 +00:00
|
|
|
size_t len;
|
2001-11-12 00:05:20 +00:00
|
|
|
char *pass;
|
2011-01-06 11:43:44 +00:00
|
|
|
int p[2], status;
|
2001-04-19 20:33:07 +00:00
|
|
|
char buf[1024];
|
2011-01-06 11:43:44 +00:00
|
|
|
void (*osigchld)(int);
|
2001-04-19 20:33:07 +00:00
|
|
|
|
|
|
|
if (fflush(stdout) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("fflush: %s", strerror(errno));
|
2001-04-19 20:33:07 +00:00
|
|
|
if (askpass == NULL)
|
|
|
|
fatal("internal error: askpass undefined");
|
2019-06-28 13:35:04 +00:00
|
|
|
if (pipe(p) == -1) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("pipe: %s", strerror(errno));
|
2003-01-24 00:36:23 +00:00
|
|
|
return NULL;
|
2001-07-14 02:19:56 +00:00
|
|
|
}
|
2020-01-23 07:10:22 +00:00
|
|
|
osigchld = ssh_signal(SIGCHLD, SIG_DFL);
|
2019-06-28 13:35:04 +00:00
|
|
|
if ((pid = fork()) == -1) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("fork: %s", strerror(errno));
|
2020-01-23 07:10:22 +00:00
|
|
|
ssh_signal(SIGCHLD, osigchld);
|
2003-01-24 00:36:23 +00:00
|
|
|
return NULL;
|
2001-07-14 02:19:56 +00:00
|
|
|
}
|
2001-04-19 20:33:07 +00:00
|
|
|
if (pid == 0) {
|
|
|
|
close(p[0]);
|
2019-06-28 13:35:04 +00:00
|
|
|
if (dup2(p[1], STDOUT_FILENO) == -1)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("dup2: %s", strerror(errno));
|
2019-12-06 03:06:08 +00:00
|
|
|
if (env_hint != NULL)
|
|
|
|
setenv("SSH_ASKPASS_PROMPT", env_hint, 1);
|
2015-12-11 00:20:04 +00:00
|
|
|
execlp(askpass, askpass, msg, (char *)NULL);
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("exec(%s): %s", askpass, strerror(errno));
|
2001-04-19 20:33:07 +00:00
|
|
|
}
|
|
|
|
close(p[1]);
|
2002-01-22 12:05:31 +00:00
|
|
|
|
2011-01-06 11:43:44 +00:00
|
|
|
len = 0;
|
2002-01-22 12:05:31 +00:00
|
|
|
do {
|
2011-01-06 11:43:44 +00:00
|
|
|
ssize_t r = read(p[0], buf + len, sizeof(buf) - 1 - len);
|
|
|
|
|
|
|
|
if (r == -1 && errno == EINTR)
|
2002-01-22 12:05:31 +00:00
|
|
|
continue;
|
2011-01-06 11:43:44 +00:00
|
|
|
if (r <= 0)
|
2002-01-22 12:05:31 +00:00
|
|
|
break;
|
2011-01-06 11:43:44 +00:00
|
|
|
len += r;
|
2002-01-22 12:05:31 +00:00
|
|
|
} while (sizeof(buf) - 1 - len > 0);
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
2001-04-19 20:33:07 +00:00
|
|
|
close(p[0]);
|
2019-06-28 13:35:04 +00:00
|
|
|
while ((ret = waitpid(pid, &status, 0)) == -1)
|
2001-04-19 20:33:07 +00:00
|
|
|
if (errno != EINTR)
|
|
|
|
break;
|
2020-01-23 07:10:22 +00:00
|
|
|
ssh_signal(SIGCHLD, osigchld);
|
2011-01-06 11:43:44 +00:00
|
|
|
if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
2014-02-04 00:20:14 +00:00
|
|
|
explicit_bzero(buf, sizeof(buf));
|
2003-01-24 00:36:23 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-11-12 00:05:20 +00:00
|
|
|
buf[strcspn(buf, "\r\n")] = '\0';
|
2001-04-19 20:33:07 +00:00
|
|
|
pass = xstrdup(buf);
|
2014-02-04 00:20:14 +00:00
|
|
|
explicit_bzero(buf, sizeof(buf));
|
2001-04-19 20:33:07 +00:00
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
2019-12-06 03:06:08 +00:00
|
|
|
/* private/internal read_passphrase flags */
|
|
|
|
#define RP_ASK_PERMISSION 0x8000 /* pass hint to askpass for confirm UI */
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
2001-06-25 05:20:31 +00: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 05:56:47 +00:00
|
|
|
* tty is or askpass program is available
|
2000-10-14 05:23:11 +00:00
|
|
|
*/
|
1999-11-24 13:26:21 +00:00
|
|
|
char *
|
2001-06-25 05:20:31 +00:00
|
|
|
read_passphrase(const char *prompt, int flags)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2019-01-19 04:15:56 +00:00
|
|
|
char cr = '\r', *askpass = NULL, *ret, buf[1024];
|
2020-07-14 23:57:01 +00:00
|
|
|
int rppflags, ttyfd, use_askpass = 0, allow_askpass = 0;
|
2019-12-06 03:06:08 +00:00
|
|
|
const char *askpass_hint = NULL;
|
2020-07-14 23:57:01 +00: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 20:33:07 +00:00
|
|
|
|
2001-06-25 05:20:31 +00:00
|
|
|
rppflags = (flags & RP_ECHO) ? RPP_ECHO_ON : RPP_ECHO_OFF;
|
2020-07-14 23:57:01 +00:00
|
|
|
if (use_askpass)
|
2020-10-18 11:32:01 +00:00
|
|
|
debug_f("requested to askpass");
|
2020-07-14 23:57:01 +00:00
|
|
|
else if (flags & RP_USE_ASKPASS)
|
2004-06-17 15:19:03 +00:00
|
|
|
use_askpass = 1;
|
|
|
|
else if (flags & RP_ALLOW_STDIN) {
|
2005-05-26 02:07:47 +00:00
|
|
|
if (!isatty(STDIN_FILENO)) {
|
2021-07-23 05:56:47 +00:00
|
|
|
debug_f("stdin is not a tty");
|
2001-04-19 20:33:07 +00:00
|
|
|
use_askpass = 1;
|
2005-05-26 02:07:47 +00:00
|
|
|
}
|
2001-04-19 20:33:07 +00:00
|
|
|
} else {
|
2001-06-25 05:20:31 +00:00
|
|
|
rppflags |= RPP_REQUIRE_TTY;
|
2002-01-22 12:24:51 +00:00
|
|
|
ttyfd = open(_PATH_TTY, O_RDWR);
|
2019-01-19 04:15:56 +00: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 20:33:07 +00:00
|
|
|
close(ttyfd);
|
2019-01-19 04:15:56 +00:00
|
|
|
} else {
|
2021-07-23 05:56:47 +00:00
|
|
|
debug_f("can't open %s: %s", _PATH_TTY,
|
2005-05-26 02:05:28 +00:00
|
|
|
strerror(errno));
|
2001-04-19 20:33:07 +00:00
|
|
|
use_askpass = 1;
|
2005-05-26 02:05:28 +00:00
|
|
|
}
|
2001-04-19 20:33:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 23:57:01 +00:00
|
|
|
if ((flags & RP_USE_ASKPASS) && !allow_askpass)
|
2004-06-17 15:19:03 +00:00
|
|
|
return (flags & RP_ALLOW_EOF) ? NULL : xstrdup("");
|
|
|
|
|
2020-07-14 23:57:01 +00:00
|
|
|
if (use_askpass && allow_askpass) {
|
2001-04-19 20:33:07 +00:00
|
|
|
if (getenv(SSH_ASKPASS_ENV))
|
|
|
|
askpass = getenv(SSH_ASKPASS_ENV);
|
|
|
|
else
|
|
|
|
askpass = _PATH_SSH_ASKPASS_DEFAULT;
|
2019-12-06 03:06:08 +00:00
|
|
|
if ((flags & RP_ASK_PERMISSION) != 0)
|
|
|
|
askpass_hint = "confirm";
|
|
|
|
if ((ret = ssh_askpass(askpass, prompt, askpass_hint)) == NULL)
|
2003-01-24 00:36:23 +00:00
|
|
|
if (!(flags & RP_ALLOW_EOF))
|
|
|
|
return xstrdup("");
|
|
|
|
return ret;
|
2001-04-19 20:33:07 +00:00
|
|
|
}
|
|
|
|
|
2002-03-27 17:28:46 +00:00
|
|
|
if (readpassphrase(prompt, buf, sizeof buf, rppflags) == NULL) {
|
|
|
|
if (flags & RP_ALLOW_EOF)
|
|
|
|
return NULL;
|
2001-07-04 05:19:27 +00:00
|
|
|
return xstrdup("");
|
2002-03-27 17:28:46 +00:00
|
|
|
}
|
2001-06-25 05:20:31 +00:00
|
|
|
|
|
|
|
ret = xstrdup(buf);
|
2014-02-04 00:20:14 +00:00
|
|
|
explicit_bzero(buf, sizeof(buf));
|
2001-06-25 05:20:31 +00:00
|
|
|
return ret;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2004-11-05 09:38:03 +00: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 03:06:08 +00:00
|
|
|
p = read_passphrase(prompt,
|
|
|
|
RP_USE_ASKPASS|RP_ALLOW_EOF|RP_ASK_PERMISSION);
|
2004-11-05 09:38:03 +00: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 21:31:17 +00:00
|
|
|
free(p);
|
2004-11-05 09:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (allowed);
|
|
|
|
}
|
2019-11-12 22:34:20 +00:00
|
|
|
|
2020-11-08 22:37:24 +00: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 22:34:20 +00: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 22:37:24 +00:00
|
|
|
pid_t pid = -1;
|
|
|
|
void (*osigchld)(int) = NULL;
|
2020-08-11 09:45:54 +00:00
|
|
|
const char *askpass, *s;
|
|
|
|
struct notifier_ctx *ret = NULL;
|
2019-11-12 22:34:20 +00:00
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
xvasprintf(&prompt, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (fflush(NULL) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("fflush: %s", strerror(errno));
|
2019-11-12 22:34:20 +00:00
|
|
|
if (!force_askpass && isatty(STDERR_FILENO)) {
|
2020-11-08 22:37:24 +00:00
|
|
|
writemsg(prompt);
|
|
|
|
goto out_ctx;
|
2019-11-12 22:34:20 +00:00
|
|
|
}
|
2019-11-15 00:32:40 +00:00
|
|
|
if ((askpass = getenv("SSH_ASKPASS")) == NULL)
|
|
|
|
askpass = _PATH_SSH_ASKPASS_DEFAULT;
|
2020-08-11 09:45:54 +00:00
|
|
|
if (*askpass == '\0') {
|
2020-10-18 11:32:01 +00:00
|
|
|
debug3_f("cannot notify: no askpass");
|
2020-08-11 09:45:54 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (getenv("DISPLAY") == NULL &&
|
|
|
|
((s = getenv(SSH_ASKPASS_REQUIRE_ENV)) == NULL ||
|
|
|
|
strcmp(s, "force") != 0)) {
|
2020-10-18 11:32:01 +00:00
|
|
|
debug3_f("cannot notify: no display");
|
2020-08-11 09:45:54 +00:00
|
|
|
goto out;
|
2019-11-12 22:34:20 +00:00
|
|
|
}
|
2020-01-23 07:10:22 +00:00
|
|
|
osigchld = ssh_signal(SIGCHLD, SIG_DFL);
|
2019-11-12 22:34:20 +00:00
|
|
|
if ((pid = fork()) == -1) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("fork: %s", strerror(errno));
|
2020-01-23 07:10:22 +00:00
|
|
|
ssh_signal(SIGCHLD, osigchld);
|
2019-11-12 22:34:20 +00:00
|
|
|
free(prompt);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (pid == 0) {
|
2020-10-03 09:22:26 +00:00
|
|
|
if (stdfd_devnull(1, 1, 0) == -1)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("stdfd_devnull failed");
|
2019-11-12 22:34:20 +00:00
|
|
|
closefrom(STDERR_FILENO + 1);
|
|
|
|
setenv("SSH_ASKPASS_PROMPT", "none", 1); /* hint to UI */
|
|
|
|
execlp(askpass, askpass, prompt, (char *)NULL);
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("exec(%s): %s", askpass, strerror(errno));
|
2019-11-27 05:00:17 +00:00
|
|
|
_exit(1);
|
2019-11-12 22:34:20 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2020-11-08 22:37:24 +00:00
|
|
|
out_ctx:
|
2019-11-12 22:34:20 +00:00
|
|
|
if ((ret = calloc(1, sizeof(*ret))) == NULL) {
|
2022-05-27 04:27:49 +00:00
|
|
|
if (pid != -1)
|
|
|
|
kill(pid, SIGTERM);
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("calloc failed");
|
2019-11-12 22:34:20 +00:00
|
|
|
}
|
|
|
|
ret->pid = pid;
|
|
|
|
ret->osigchld = osigchld;
|
2020-08-11 09:45:54 +00:00
|
|
|
out:
|
2019-11-12 22:34:20 +00:00
|
|
|
free(prompt);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-11-08 22:37:24 +00:00
|
|
|
notify_complete(struct notifier_ctx *ctx, const char *fmt, ...)
|
2019-11-12 22:34:20 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2020-11-08 22:37:24 +00:00
|
|
|
char *msg = NULL;
|
|
|
|
va_list args;
|
|
|
|
|
2020-11-08 23:19:03 +00:00
|
|
|
if (ctx != NULL && fmt != NULL && ctx->pid == -1) {
|
2020-11-08 22:37:24 +00: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 07:46:20 +00:00
|
|
|
free(msg);
|
2020-11-08 22:37:24 +00:00
|
|
|
}
|
2019-11-12 22:34:20 +00: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 11:32:01 +00:00
|
|
|
fatal_f("waitpid: %s", strerror(errno));
|
2020-01-23 07:10:22 +00:00
|
|
|
ssh_signal(SIGCHLD, ctx->osigchld);
|
2019-11-12 22:34:20 +00:00
|
|
|
free(ctx);
|
|
|
|
}
|