summaryrefslogtreecommitdiff
path: root/sshconnect.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2019-09-13 04:27:35 +0000
committerDamien Miller <djm@mindrot.org>2019-09-13 14:28:44 +1000
commitfbe24b142915331ceb2a3a76be3dc5b6d204fddf (patch)
tree6adb5c456a64cdd74ca004a6ec8633cc53f4eac9 /sshconnect.c
parent2ce1d11600e13bee0667d6b717ffcc18a057b821 (diff)
upstream: allow %n to be expanded in ProxyCommand strings
From Zachary Harmany via github.com/openssh/openssh-portable/pull/118 ok dtucker@ OpenBSD-Commit-ID: 7eebf1b7695f50c66d42053d352a4db9e8fb84b6
Diffstat (limited to 'sshconnect.c')
-rw-r--r--sshconnect.c35
1 files changed, 20 insertions, 15 deletions
diff --git a/sshconnect.c b/sshconnect.c
index ed44fccb8..740780443 100644
--- a/sshconnect.c
+++ b/sshconnect.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sshconnect.c,v 1.317 2019/06/28 13:35:04 deraadt Exp $ */ 1/* $OpenBSD: sshconnect.c,v 1.318 2019/09/13 04:27:35 djm Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -87,14 +87,18 @@ static void warn_changed_key(struct sshkey *);
87/* Expand a proxy command */ 87/* Expand a proxy command */
88static char * 88static char *
89expand_proxy_command(const char *proxy_command, const char *user, 89expand_proxy_command(const char *proxy_command, const char *user,
90 const char *host, int port) 90 const char *host, const char *host_arg, int port)
91{ 91{
92 char *tmp, *ret, strport[NI_MAXSERV]; 92 char *tmp, *ret, strport[NI_MAXSERV];
93 93
94 snprintf(strport, sizeof strport, "%d", port); 94 snprintf(strport, sizeof strport, "%d", port);
95 xasprintf(&tmp, "exec %s", proxy_command); 95 xasprintf(&tmp, "exec %s", proxy_command);
96 ret = percent_expand(tmp, "h", host, "p", strport, 96 ret = percent_expand(tmp,
97 "r", options.user, (char *)NULL); 97 "h", host,
98 "n", host_arg,
99 "p", strport,
100 "r", options.user,
101 (char *)NULL);
98 free(tmp); 102 free(tmp);
99 return ret; 103 return ret;
100} 104}
@@ -122,8 +126,8 @@ stderr_null(void)
122 * a connected fd back to us. 126 * a connected fd back to us.
123 */ 127 */
124static int 128static int
125ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host, u_short port, 129ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host,
126 const char *proxy_command) 130 const char *host_arg, u_short port, const char *proxy_command)
127{ 131{
128 char *command_string; 132 char *command_string;
129 int sp[2], sock; 133 int sp[2], sock;
@@ -138,7 +142,7 @@ ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host, u_short port,
138 "proxy dialer: %.100s", strerror(errno)); 142 "proxy dialer: %.100s", strerror(errno));
139 143
140 command_string = expand_proxy_command(proxy_command, options.user, 144 command_string = expand_proxy_command(proxy_command, options.user,
141 host, port); 145 host_arg, host, port);
142 debug("Executing proxy dialer command: %.500s", command_string); 146 debug("Executing proxy dialer command: %.500s", command_string);
143 147
144 /* Fork and execute the proxy command. */ 148 /* Fork and execute the proxy command. */
@@ -204,8 +208,8 @@ ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host, u_short port,
204 * Connect to the given ssh server using a proxy command. 208 * Connect to the given ssh server using a proxy command.
205 */ 209 */
206static int 210static int
207ssh_proxy_connect(struct ssh *ssh, const char *host, u_short port, 211ssh_proxy_connect(struct ssh *ssh, const char *host, const char *host_arg,
208 const char *proxy_command) 212 u_short port, const char *proxy_command)
209{ 213{
210 char *command_string; 214 char *command_string;
211 int pin[2], pout[2]; 215 int pin[2], pout[2];
@@ -221,7 +225,7 @@ ssh_proxy_connect(struct ssh *ssh, const char *host, u_short port,
221 strerror(errno)); 225 strerror(errno));
222 226
223 command_string = expand_proxy_command(proxy_command, options.user, 227 command_string = expand_proxy_command(proxy_command, options.user,
224 host, port); 228 host_arg, host, port);
225 debug("Executing proxy command: %.500s", command_string); 229 debug("Executing proxy command: %.500s", command_string);
226 230
227 /* Fork and execute the proxy command. */ 231 /* Fork and execute the proxy command. */
@@ -543,9 +547,9 @@ ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop,
543} 547}
544 548
545int 549int
546ssh_connect(struct ssh *ssh, const char *host, struct addrinfo *addrs, 550ssh_connect(struct ssh *ssh, const char *host, const char *host_arg,
547 struct sockaddr_storage *hostaddr, u_short port, int family, 551 struct addrinfo *addrs, struct sockaddr_storage *hostaddr, u_short port,
548 int connection_attempts, int *timeout_ms, int want_keepalive) 552 int family, int connection_attempts, int *timeout_ms, int want_keepalive)
549{ 553{
550 int in, out; 554 int in, out;
551 555
@@ -564,10 +568,11 @@ ssh_connect(struct ssh *ssh, const char *host, struct addrinfo *addrs,
564 return -1; /* ssh_packet_set_connection logs error */ 568 return -1; /* ssh_packet_set_connection logs error */
565 return 0; 569 return 0;
566 } else if (options.proxy_use_fdpass) { 570 } else if (options.proxy_use_fdpass) {
567 return ssh_proxy_fdpass_connect(ssh, host, port, 571 return ssh_proxy_fdpass_connect(ssh, host, host_arg, port,
568 options.proxy_command); 572 options.proxy_command);
569 } 573 }
570 return ssh_proxy_connect(ssh, host, port, options.proxy_command); 574 return ssh_proxy_connect(ssh, host, host_arg, port,
575 options.proxy_command);
571} 576}
572 577
573/* defaults to 'no' */ 578/* defaults to 'no' */