Skip to content

Commit

Permalink
upstream commit
Browse files Browse the repository at this point in the history
Add RemoteCommand option to specify a command in the
ssh config file instead of giving it on the client's command line.  This
command will be executed on the remote host.  The feature allows to automate
tasks using ssh config. OK markus@

Upstream-ID: 5d982fc17adea373a9c68cae1021ce0a0904a5ee
  • Loading branch information
bluhm authored and djmdjm committed May 31, 2017
1 parent eb272ea commit 1112b53
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
13 changes: 11 additions & 2 deletions readconf.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: readconf.c,v 1.276 2017/05/20 02:35:47 djm Exp $ */
/* $OpenBSD: readconf.c,v 1.277 2017/05/30 18:58:37 bluhm Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
Expand Down Expand Up @@ -163,7 +163,8 @@ typedef enum {
oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
oSendEnv, oControlPath, oControlMaster, oControlPersist,
oHashKnownHosts,
oTunnel, oTunnelDevice, oLocalCommand, oPermitLocalCommand,
oTunnel, oTunnelDevice,
oLocalCommand, oPermitLocalCommand, oRemoteCommand,
oVisualHostKey,
oKexAlgorithms, oIPQoS, oRequestTTY, oIgnoreUnknown, oProxyUseFdpass,
oCanonicalDomains, oCanonicalizeHostname, oCanonicalizeMaxDots,
Expand Down Expand Up @@ -284,6 +285,7 @@ static struct {
{ "tunneldevice", oTunnelDevice },
{ "localcommand", oLocalCommand },
{ "permitlocalcommand", oPermitLocalCommand },
{ "remotecommand", oRemoteCommand },
{ "visualhostkey", oVisualHostKey },
{ "kexalgorithms", oKexAlgorithms },
{ "ipqos", oIPQoS },
Expand Down Expand Up @@ -1440,6 +1442,10 @@ process_config_line_depth(Options *options, struct passwd *pw, const char *host,
intptr = &options->permit_local_command;
goto parse_flag;

case oRemoteCommand:
charptr = &options->remote_command;
goto parse_command;

case oVisualHostKey:
intptr = &options->visual_host_key;
goto parse_flag;
Expand Down Expand Up @@ -1828,6 +1834,7 @@ initialize_options(Options * options)
options->tun_remote = -1;
options->local_command = NULL;
options->permit_local_command = -1;
options->remote_command = NULL;
options->add_keys_to_agent = -1;
options->identity_agent = NULL;
options->visual_host_key = -1;
Expand Down Expand Up @@ -2032,6 +2039,7 @@ fill_default_options(Options * options)
} \
} while(0)
CLEAR_ON_NONE(options->local_command);
CLEAR_ON_NONE(options->remote_command);
CLEAR_ON_NONE(options->proxy_command);
CLEAR_ON_NONE(options->control_path);
CLEAR_ON_NONE(options->revoked_host_keys);
Expand Down Expand Up @@ -2509,6 +2517,7 @@ dump_client_config(Options *o, const char *host)
dump_cfg_string(oKbdInteractiveDevices, o->kbd_interactive_devices);
dump_cfg_string(oKexAlgorithms, o->kex_algorithms ? o->kex_algorithms : KEX_CLIENT_KEX);
dump_cfg_string(oLocalCommand, o->local_command);
dump_cfg_string(oRemoteCommand, o->remote_command);
dump_cfg_string(oLogLevel, log_level_name(o->log_level));
dump_cfg_string(oMacs, o->macs ? o->macs : KEX_CLIENT_MAC);
#ifdef ENABLE_PKCS11
Expand Down
3 changes: 2 additions & 1 deletion readconf.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: readconf.h,v 1.121 2017/04/30 23:18:22 djm Exp $ */
/* $OpenBSD: readconf.h,v 1.122 2017/05/30 18:58:37 bluhm Exp $ */

/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
Expand Down Expand Up @@ -134,6 +134,7 @@ typedef struct {

char *local_command;
int permit_local_command;
char *remote_command;
int visual_host_key;

int request_tty;
Expand Down
5 changes: 3 additions & 2 deletions ssh.1
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $OpenBSD: ssh.1,v 1.381 2017/05/05 10:41:58 naddy Exp $
.Dd $Mdocdate: May 5 2017 $
.\" $OpenBSD: ssh.1,v 1.382 2017/05/30 18:58:37 bluhm Exp $
.Dd $Mdocdate: May 30 2017 $
.Dt SSH 1
.Os
.Sh NAME
Expand Down Expand Up @@ -518,6 +518,7 @@ For full details of the options listed below, and their possible values, see
.It PubkeyAcceptedKeyTypes
.It PubkeyAuthentication
.It RekeyLimit
.It RemoteCommand
.It RemoteForward
.It RequestTTY
.It SendEnv
Expand Down
40 changes: 32 additions & 8 deletions ssh.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: ssh.c,v 1.460 2017/05/30 08:52:19 markus Exp $ */
/* $OpenBSD: ssh.c,v 1.461 2017/05/30 18:58:37 bluhm Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
Expand Down Expand Up @@ -973,12 +973,6 @@ main(int ac, char **av)
}
}

/* Cannot fork to background if no command. */
if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
!no_shell_flag)
fatal("Cannot fork into background without a command "
"to execute.");

/*
* Initialize "log" output. Since we are the client all output
* goes to stderr unless otherwise specified by -y or -E.
Expand Down Expand Up @@ -1133,6 +1127,15 @@ main(int ac, char **av)
options.use_privileged_port = 0;
#endif

if (buffer_len(&command) != 0 && options.remote_command != NULL)
fatal("Cannot execute command-line and remote command.");

/* Cannot fork to background if no command. */
if (fork_after_authentication_flag && buffer_len(&command) == 0 &&
options.remote_command == NULL && !no_shell_flag)
fatal("Cannot fork into background without a command "
"to execute.");

/* reinit */
log_init(argv0, options.log_level, options.log_facility, !use_syslog);

Expand All @@ -1141,7 +1144,7 @@ main(int ac, char **av)
tty_flag = 1;

/* Allocate a tty by default if no command specified. */
if (buffer_len(&command) == 0)
if (buffer_len(&command) == 0 && options.remote_command == NULL)
tty_flag = options.request_tty != REQUEST_TTY_NO;

/* Force no tty */
Expand Down Expand Up @@ -1197,6 +1200,27 @@ main(int ac, char **av)
free(cp);
}

if (options.remote_command != NULL) {
debug3("expanding RemoteCommand: %s", options.remote_command);
cp = options.remote_command;
options.remote_command = percent_expand(cp,
"C", conn_hash_hex,
"L", shorthost,
"d", pw->pw_dir,
"h", host,
"l", thishost,
"n", host_arg,
"p", portstr,
"r", options.user,
"u", pw->pw_name,
(char *)NULL);
debug3("expanded RemoteCommand: %s", options.remote_command);
free(cp);
buffer_append(&command, options.remote_command,
strlen(options.remote_command));

}

if (options.control_path != NULL) {
cp = tilde_expand_filename(options.control_path,
original_real_uid);
Expand Down
12 changes: 10 additions & 2 deletions ssh_config.5
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $OpenBSD: ssh_config.5,v 1.248 2017/05/07 23:12:57 djm Exp $
.Dd $Mdocdate: May 7 2017 $
.\" $OpenBSD: ssh_config.5,v 1.249 2017/05/30 18:58:37 bluhm Exp $
.Dd $Mdocdate: May 30 2017 $
.Dt SSH_CONFIG 5
.Os
.Sh NAME
Expand Down Expand Up @@ -1287,6 +1287,14 @@ is
.Cm default none ,
which means that rekeying is performed after the cipher's default amount
of data has been sent or received and no time based rekeying is done.
.It Cm RemoteCommand
Specifies a command to execute on the remote machine after successfully
connecting to the server.
The command string extends to the end of the line, and is executed with
the user's shell.
The same escape character substitutions as for
.Cm LocalCommand
will be performed.
.It Cm RemoteForward
Specifies that a TCP port on the remote machine be forwarded over
the secure channel to the specified host and port from the local machine.
Expand Down

2 comments on commit 1112b53

@grooverdan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bluhm Thank you for implementing my this feature (that I requested quite a few years ago https://bugzilla.mindrot.org/show_bug.cgi?id=2103 )

@nhed
Copy link

@nhed nhed commented on 1112b53 Feb 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ 💟 😍

Best ssh option ever (well maybe "the most missing ssh option")

Please sign in to comment.