upstream: allow ssh_config SetEnv to override $TERM, which is otherwise

handled specially by the protocol. Useful in ~/.ssh/config to set TERM to
something generic (e.g. "xterm" instead of "xterm-256color") for destinations
that lack terminfo entries. feedback and ok dtucker@

OpenBSD-Commit-ID: 38b1ef4d5bc159c7d9d589d05e3017433e2d5758
This commit is contained in:
djm@openbsd.org 2021-06-04 05:02:40 +00:00 committed by Damien Miller
parent 60107677dc
commit f64f8c00d1
4 changed files with 32 additions and 10 deletions

17
misc.c
View File

@ -1,4 +1,4 @@
/* $OpenBSD: misc.c,v 1.164 2021/04/03 06:18:40 djm Exp $ */ /* $OpenBSD: misc.c,v 1.165 2021/06/04 05:02:40 djm Exp $ */
/* /*
* Copyright (c) 2000 Markus Friedl. All rights reserved. * Copyright (c) 2000 Markus Friedl. All rights reserved.
* Copyright (c) 2005-2020 Damien Miller. All rights reserved. * Copyright (c) 2005-2020 Damien Miller. All rights reserved.
@ -2671,3 +2671,18 @@ subprocess(const char *tag, const char *command,
*child = f; *child = f;
return pid; return pid;
} }
const char *
lookup_env_in_list(const char *env, char * const *envs, size_t nenvs)
{
size_t i, envlen;
envlen = strlen(env);
for (i = 0; i < nenvs; i++) {
if (strncmp(envs[i], env, envlen) == 0 &&
envs[i][envlen] == '=') {
return envs[i] + envlen + 1;
}
}
return NULL;
}

4
misc.h
View File

@ -1,4 +1,4 @@
/* $OpenBSD: misc.h,v 1.95 2021/04/03 06:18:40 djm Exp $ */ /* $OpenBSD: misc.h,v 1.96 2021/06/04 05:02:40 djm Exp $ */
/* /*
* Author: Tatu Ylonen <ylo@cs.hut.fi> * Author: Tatu Ylonen <ylo@cs.hut.fi>
@ -175,6 +175,8 @@ void mktemp_proto(char *, size_t);
void child_set_env(char ***envp, u_int *envsizep, const char *name, void child_set_env(char ***envp, u_int *envsizep, const char *name,
const char *value); const char *value);
const char *lookup_env_in_list(const char *env,
char * const *envs, size_t nenvs);
int argv_split(const char *, int *, char ***); int argv_split(const char *, int *, char ***);
char *argv_assemble(int, char **argv); char *argv_assemble(int, char **argv);

12
mux.c
View File

@ -1,4 +1,4 @@
/* $OpenBSD: mux.c,v 1.88 2021/05/19 01:24:05 djm Exp $ */ /* $OpenBSD: mux.c,v 1.89 2021/06/04 05:02:40 djm Exp $ */
/* /*
* Copyright (c) 2002-2008 Damien Miller <djm@openbsd.org> * Copyright (c) 2002-2008 Damien Miller <djm@openbsd.org>
* *
@ -1866,7 +1866,7 @@ mux_client_request_session(int fd)
{ {
struct sshbuf *m; struct sshbuf *m;
char *e; char *e;
const char *term; const char *term = NULL;
u_int echar, rid, sid, esid, exitval, type, exitval_seen; u_int echar, rid, sid, esid, exitval, type, exitval_seen;
extern char **environ; extern char **environ;
int r, i, rawmode; int r, i, rawmode;
@ -1883,8 +1883,10 @@ mux_client_request_session(int fd)
if (stdin_null_flag && stdfd_devnull(1, 0, 0) == -1) if (stdin_null_flag && stdfd_devnull(1, 0, 0) == -1)
fatal_f("stdfd_devnull failed"); fatal_f("stdfd_devnull failed");
if ((term = getenv("TERM")) == NULL) if ((term = lookup_env_in_list("TERM", options.setenv,
term = ""; options.num_setenv)) == NULL || *term == '\0')
term = getenv("TERM");
echar = 0xffffffff; echar = 0xffffffff;
if (options.escape_char != SSH_ESCAPECHAR_NONE) if (options.escape_char != SSH_ESCAPECHAR_NONE)
echar = (u_int)options.escape_char; echar = (u_int)options.escape_char;
@ -1899,7 +1901,7 @@ mux_client_request_session(int fd)
(r = sshbuf_put_u32(m, options.forward_agent)) != 0 || (r = sshbuf_put_u32(m, options.forward_agent)) != 0 ||
(r = sshbuf_put_u32(m, subsystem_flag)) != 0 || (r = sshbuf_put_u32(m, subsystem_flag)) != 0 ||
(r = sshbuf_put_u32(m, echar)) != 0 || (r = sshbuf_put_u32(m, echar)) != 0 ||
(r = sshbuf_put_cstring(m, term)) != 0 || (r = sshbuf_put_cstring(m, term == NULL ? "" : term)) != 0 ||
(r = sshbuf_put_stringb(m, command)) != 0) (r = sshbuf_put_stringb(m, command)) != 0)
fatal_fr(r, "request"); fatal_fr(r, "request");

9
ssh.c
View File

@ -1,4 +1,4 @@
/* $OpenBSD: ssh.c,v 1.557 2021/05/19 01:24:05 djm Exp $ */ /* $OpenBSD: ssh.c,v 1.558 2021/06/04 05:02:40 djm Exp $ */
/* /*
* Author: Tatu Ylonen <ylo@cs.hut.fi> * Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@ -2018,7 +2018,7 @@ static void
ssh_session2_setup(struct ssh *ssh, int id, int success, void *arg) ssh_session2_setup(struct ssh *ssh, int id, int success, void *arg)
{ {
extern char **environ; extern char **environ;
const char *display; const char *display, *term;
int r, interactive = tty_flag; int r, interactive = tty_flag;
char *proto = NULL, *data = NULL; char *proto = NULL, *data = NULL;
@ -2053,7 +2053,10 @@ ssh_session2_setup(struct ssh *ssh, int id, int success, void *arg)
ssh_packet_set_interactive(ssh, interactive, ssh_packet_set_interactive(ssh, interactive,
options.ip_qos_interactive, options.ip_qos_bulk); options.ip_qos_interactive, options.ip_qos_bulk);
client_session2_setup(ssh, id, tty_flag, subsystem_flag, getenv("TERM"), if ((term = lookup_env_in_list("TERM", options.setenv,
options.num_setenv)) == NULL || *term == '\0')
term = getenv("TERM");
client_session2_setup(ssh, id, tty_flag, subsystem_flag, term,
NULL, fileno(stdin), command, environ); NULL, fileno(stdin), command, environ);
} }