%h,%p,%u expansion for ControlPath

David Woodhouse dwmw2 at infradead.org
Sun Jun 12 18:59:22 EST 2005


This allows me to set 'ControlPath ~/.ssh/sockets/%h.%p.%u' for example.

Have I missed a good reason why ssh_connect finds the default port
number for itself instead of just having it in options.port (like we do
for the the default in options.user)?

--- openssh-4.1p1/ssh.c~	2005-06-12 09:47:18.000000000 +0100
+++ openssh-4.1p1/ssh.c	2005-06-12 09:40:53.000000000 +0100
@@ -604,6 +604,17 @@ again:
 				*p = tolower(*p);
 	}
 
+	/* Get default port if port has not been set. */
+	if (options.port == 0) {
+		struct servent *sp;
+
+		sp = getservbyname(SSH_SERVICE_NAME, "tcp");
+		if (sp)
+			options.port = ntohs(sp->s_port);
+		else
+			options.port = SSH_DEFAULT_PORT;
+	}
+
 	if (options.proxy_command != NULL &&
 	    strcmp(options.proxy_command, "none") == 0)
 		options.proxy_command = NULL;
@@ -611,6 +622,42 @@ again:
 	if (options.control_path != NULL) {
 		options.control_path = tilde_expand_filename(
 		   options.control_path, original_real_uid);
+
+		if (strchr(options.control_path, '%')) {
+			Buffer path;
+			const char *cp;
+			char strport[NI_MAXSERV];
+
+			/* Convert the port number into a string. */
+			snprintf(strport, sizeof strport, "%hu", options.port);	
+
+			buffer_init(&path);
+			for (cp = options.control_path; *cp; cp++) {
+				if (cp[0] == '%' && cp[1] == '%') {
+					buffer_append(&path, "%", 1);
+					cp++;
+					continue;
+				}
+				if (cp[0] == '%' && cp[1] == 'h') {
+					buffer_append(&path, host, strlen(host));
+					cp++;
+					continue;
+				}
+				if (cp[0] == '%' && cp[1] == 'p') {
+					buffer_append(&path, strport, strlen(strport));
+					cp++;
+					continue;
+				}
+				if (cp[0] == '%' && cp[1] == 'u') {
+					buffer_append(&path, options.user, strlen(options.user));
+					cp++;
+					continue;
+				}
+				buffer_append(&path, cp, 1);
+			}
+			buffer_append(&path, "\0", 1);
+			options.control_path = xstrdup(buffer_ptr(&path));
+		}
 	}
 	if (options.control_path != NULL && options.control_master == 0)
 		control_client(options.control_path); /* This doesn't return */
--- openssh-4.1p1/sshconnect.c~	2005-03-14 12:08:12.000000000 +0000
+++ openssh-4.1p1/sshconnect.c	2005-06-12 09:36:33.000000000 +0100
@@ -308,18 +308,9 @@ ssh_connect(const char *host, struct soc
 	int sock = -1, attempt;
 	char ntop[NI_MAXHOST], strport[NI_MAXSERV];
 	struct addrinfo hints, *ai, *aitop;
-	struct servent *sp;
 
 	debug2("ssh_connect: needpriv %d", needpriv);
 
-	/* Get default port if port has not been set. */
-	if (port == 0) {
-		sp = getservbyname(SSH_SERVICE_NAME, "tcp");
-		if (sp)
-			port = ntohs(sp->s_port);
-		else
-			port = SSH_DEFAULT_PORT;
-	}
 	/* If a proxy command is given, connect using it. */
 	if (proxy_command != NULL)
 		return ssh_proxy_connect(host, port, proxy_command);

-- 
dwmw2




More information about the openssh-unix-dev mailing list