Patch to allow local port forwarding from an existing connection

Brian Wellington bwelling at xbill.org
Fri Sep 21 06:49:20 EST 2001


On Thu, 20 Sep 2001, Markus Friedl wrote:

> or perhaps use readpass.[ch] from -current with echo enabled?

I just changed it to use readpassphrase(), although I honestly don't see
any advantages.  New patch appended, now against last night's portable
snapshot.

Brian

-------
--- clientloop.c.old	Mon Sep 17 22:51:14 2001
+++ clientloop.c	Thu Sep 20 13:46:23 2001
@@ -467,6 +467,71 @@
 	}
 }

+static void
+process_cmdline(Buffer *bin, Buffer *bout, Buffer *berr)
+{
+	char string[1024];
+	void (*handler)(int);
+	char *s;
+	u_short fwd_port, fwd_host_port;
+	char buf[256];
+	int local = 0;
+	char *msg = NULL;
+	int n;
+
+	leave_raw_mode();
+	handler = signal(SIGINT, SIG_IGN);
+	s = readpassphrase("\r\n> ", string, sizeof string, RPP_ECHO_ON);
+	if (s == NULL)
+		goto out;
+
+	while (*s && isspace(*s))
+		s++;
+
+	if (*s == 0)
+		goto out;
+
+	if (strlen(s) < 2 || s[0] != '-' || !(s[1] == 'L' || s[1] == 'R')) {
+		msg = "Invalid command";
+		goto out;
+	}
+	if (s[1] == 'L')
+		local = 1;
+	else {
+		msg = "Remote forwarding doesn't work";
+		goto out;
+	}
+
+	s += 2;
+
+	if (sscanf(s, "%hu/%255[^/]/%hu", &fwd_port, buf, &fwd_host_port) != 3
+	    &&
+	    sscanf(s, "%hu:%255[^:]:%hu", &fwd_port, buf, &fwd_host_port) != 3)
+	{
+		msg = "Invalid port forward";
+		goto out;
+	}
+	if (local) {
+		n = channel_request_local_forwarding(fwd_port, buf,
+						     fwd_host_port,
+						     options.gateway_ports);
+		if (n <= 0) {
+			msg = "Port forwarding failed";
+			goto out;
+		}
+	}
+	else
+		channel_request_remote_forwarding(fwd_port, buf, fwd_host_port);
+
+	msg = "Forwarding port";
+ out:
+	signal(SIGINT, handler);
+	enter_raw_mode();
+	if (msg) {
+		snprintf(string, sizeof string, "%s\r\n", msg);
+		buffer_append(berr, string, strlen(string));
+	}
+}
 /* process the characters one by one */
 static int
 process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
@@ -585,6 +650,10 @@
 				s = channel_open_message();
 				buffer_append(berr, s, strlen(s));
 				xfree(s);
+				continue;
+
+			case 'c':
+				process_cmdline(bin, bout, berr);
 				continue;

 			default:




More information about the openssh-unix-dev mailing list