-/* $OpenBSD: ssh.c,v 1.616 2025/08/29 03:50:38 djm Exp $ */
+/* $OpenBSD: ssh.c,v 1.617 2025/09/04 00:29:09 djm Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
if (*s == '-')
return 0;
for (i = 0; s[i] != 0; i++) {
+ if (iscntrl((u_char)s[i]))
+ return 0;
if (strchr("'`\";&<>|(){}", s[i]) != NULL)
return 0;
/* Disallow '-' after whitespace */
struct ssh *ssh = NULL;
int i, r, opt, exit_status, use_syslog, direct, timeout_ms;
int was_addr, config_test = 0, opt_terminated = 0, want_final_pass = 0;
+ int user_on_commandline = 0, user_was_default = 0, user_expanded = 0;
char *p, *cp, *line, *argv0, *logfile, *args;
char cname[NI_MAXHOST], thishost[NI_MAXHOST];
struct stat st;
}
break;
case 'l':
- if (options.user == NULL)
+ if (options.user == NULL) {
options.user = xstrdup(optarg);
+ user_on_commandline = 1;
+ }
break;
case 'L':
if (options.user == NULL) {
options.user = tuser;
tuser = NULL;
+ user_on_commandline = 1;
}
free(tuser);
if (options.port == -1 && tport != -1)
if (options.user == NULL) {
options.user = p;
p = NULL;
+ user_on_commandline = 1;
}
*cp++ = '\0';
host = xstrdup(cp);
if (fill_default_options(&options) != 0)
cleanup_exit(255);
- if (options.user == NULL)
+ if (options.user == NULL) {
+ user_was_default = 1;
options.user = xstrdup(pw->pw_name);
+ }
/*
* If ProxyJump option specified, then construct a ProxyCommand now.
"" : options.jump_host);
/*
- * Expand User. It cannot contain %r (itself) or %C since User is
+ * If the user was specified via a configuration directive then attempt
+ * to expand it. It cannot contain %r (itself) or %C since User is
* a component of the hash.
*/
- if (options.user != NULL) {
+ if (!user_on_commandline && !user_was_default) {
if ((p = percent_dollar_expand(options.user,
DEFAULT_CLIENT_PERCENT_EXPAND_ARGS_NOUSER(cinfo),
(char *)NULL)) == NULL)
fatal("invalid environment variable expansion");
+ user_expanded = strcmp(p, options.user) != 0;
free(options.user);
options.user = p;
- if (!valid_ruser(options.user))
- fatal("remote username contains invalid characters");
}
+ /*
+ * Usernames specified on the commandline or expanded from the
+ * configuration file must be validated.
+ * Conversely, usernames from getpwnam(3) or specified as literals
+ * via configuration (i.e. not expanded) are not subject to validation.
+ */
+ if ((user_on_commandline || user_expanded) &&
+ !valid_ruser(options.user))
+ fatal("remote username contains invalid characters");
+
/* Now User is expanded, store it and calculate hash. */
cinfo->remuser = xstrdup(options.user);
cinfo->conn_hash_hex = ssh_connection_hash(cinfo->thishost,