argv[0] => host feature considered harmful

Jim Knoble jmknoble at jmknoble.cx
Wed Feb 7 08:35:09 EST 2001


2001-Feb-06 12:17:40 -0800 dixit Jos Backus:

: OpenSSH still has this feature, SSH-1.2.27 no longer has it.
: Admittedly it can be useful sometimes, even though I'd prefer this
: to be done using a trivial shell wrapper, which would be the UNIX
: way of doing things.
: 
: Not being able to call OpenSSH's ssh by another name (say ``ssh1'')
: can get in the way when having to maintain two versions of ssh in
: parallel because the ``ssh -> ssh1'' symlink trick doesn't work (ssh
: gives a ``bad hostname: ssh1'' error message).
: 
: How do others feel about at least conditionalizing this feature? I
: can come up with a patch if needed.

I have no opinion about the hostname => "ssh hostname" feature, but the
attached patch is one that i use when i build OpenSSH.  It does the
following:

  (1) Enables both ssh and scp to be called as 'ssh1' or 'ssh2' (or
     'scp1' or 'scp2') and force the corresponding protocol version.

  (2) Implements a '-1' option for both ssh and scp (as well as '-2'
      for scp) for symmetry with the '-2' option of ssh.  (Yes, i'm
      aware there's been discussion about this before, but i
      implemented it for my own use, so i don't care whether anyone
      likes it. :)

  (3) Documents the new options in [2] above.

The patch is against OpenSSH-2.3.0p1.  Go wild.

-- 
jim knoble | jmknoble at jmknoble.cx | http://www.jmknoble.cx/
-------------- next part --------------
--- ./scp.c.orig-protoargs	Fri Oct 27 23:19:58 2000
+++ ./scp.c	Tue Jan  9 15:38:13 2001
@@ -248,16 +248,46 @@
 	char *targ;
 	extern char *optarg;
 	extern int optind;
+	char *cp;
+	int protoflag = 0;
 
 	args.list = NULL;
 	addargs("ssh");	 	/* overwritten with ssh_program */
 	addargs("-x");
 	addargs("-oFallBackToRsh no");
 
+	cp = strchr(argv[0], '/');
+	if (NULL == cp)
+		cp = argv[0];
+#ifdef HAVE_CYGWIN
+	if (!strcasecmp(cp, "scp1") || !strcasecmp(cp, "scp1.exe"))
+#else
+	if (!strcmp(cp, "scp1"))
+#endif
+	 {
+		protoflag = 1;
+		addargs("-oProtocol %c", '1');
+	 }
+#ifdef HAVE_CYGWIN
+	else if (!strcasecmp(cp, "scp2") || !strcasecmp(cp, "scp2.exe"))
+#else
+	else if (!strcmp(cp, "scp2"))
+#endif
+	 {
+		protoflag = 2;
+		addargs("-oProtocol %c", '2');
+	 }
+
 	fflag = tflag = 0;
-	while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != EOF)
+	while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q1246S:o:")) != EOF)
 		switch (ch) {
 		/* User-visible flags. */
+		case '1':
+		case '2':
+			if (!protoflag) {
+				addargs("-oProtocol %c", ch);
+			}
+			break;
 		case '4':
 		case '6':
 		case 'C':
@@ -961,7 +991,7 @@
 usage()
 {
 	(void) fprintf(stderr, "usage: scp "
-	    "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
+	    "[-pqrvC1246] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
 	    "       scp [options] f1 ... fn directory\n");
 	exit(1);
 }
--- ./ssh.c.orig-protoargs	Fri Oct 27 23:19:58 2000
+++ ./ssh.c	Tue Jan  9 15:38:27 2001
@@ -173,9 +173,10 @@
 	fprintf(stderr, "  -C          Enable compression.\n");
 	fprintf(stderr, "  -N          Do not execute a shell or command.\n");
 	fprintf(stderr, "  -g          Allow remote hosts to connect to forwarded ports.\n");
+	fprintf(stderr, "  -1          Force protocol version 1.\n");
+	fprintf(stderr, "  -2          Force protocol version 2.\n");
 	fprintf(stderr, "  -4          Use IPv4 only.\n");
 	fprintf(stderr, "  -6          Use IPv6 only.\n");
-	fprintf(stderr, "  -2          Force protocol version 2.\n");
 	fprintf(stderr, "  -o 'option' Process the option as if it was read from a configuration file.\n");
 	exit(1);
 }
@@ -286,16 +287,44 @@
 		cp = av0;
 #ifdef HAVE_CYGWIN
 	if (strcasecmp(cp, "rsh") && strcasecmp(cp, "ssh") &&
+	    strcasecmp(cp, "ssh1") && strcasecmp(cp, "ssh2") &&
 	    strcasecmp(cp, "rlogin") && strcasecmp(cp, "slogin") &&
+	    strcasecmp(cp, "slogin1") && strcasecmp(cp, "slogin2") &&
 	    strcasecmp(cp, "remsh") &&
 	    strcasecmp(cp, "rsh.exe") && strcasecmp(cp, "ssh.exe") &&
+	    strcasecmp(cp, "ssh1.exe") && strcasecmp(cp, "ssh2.exe") &&
 	    strcasecmp(cp, "rlogin.exe") && strcasecmp(cp, "slogin.exe") &&
+	    strcasecmp(cp, "slogin1.exe") && strcasecmp(cp, "slogin2.exe") &&
 	    strcasecmp(cp, "remsh.exe"))
 #else
 	if (strcmp(cp, "rsh") && strcmp(cp, "ssh") && strcmp(cp, "rlogin") &&
+	    strcmp(cp, "ssh1") && strcmp(cp, "ssh2") &&
+	    strcmp(cp, "slogin1") && strcmp(cp, "slogin2") &&
 	    strcmp(cp, "slogin") && strcmp(cp, "remsh"))
 #endif
 		host = cp;
+#ifdef HAVE_CYGWIN
+	else if (!strcasecmp(cp, "ssh1") ||
+		 !strcasecmp(cp, "slogin1") ||
+		 !strcasecmp(cp, "ssh1.exe") ||
+		 !strcasecmp(cp, "slogin1.exe"))
+#else
+	else if (!strcmp(cp, "ssh1") || !strcmp(cp, "slogin1"))
+#endif
+	 {
+		options.protocol = SSH_PROTO_1;
+	 }
+#ifdef HAVE_CYGWIN
+	else if (!strcasecmp(cp, "ssh2") ||
+		 !strcasecmp(cp, "slogin2") ||
+		 !strcasecmp(cp, "ssh2.exe") ||
+		 !strcasecmp(cp, "slogin2.exe"))
+#else
+	else if (!strcmp(cp, "ssh2") || !strcmp(cp, "slogin2"))
+#endif
+	 {
+		options.protocol = SSH_PROTO_2;
+	 }
 
 	for (optind = 1; optind < ac; optind++) {
 		if (av[optind][0] != '-') {
@@ -327,6 +356,9 @@
 			optarg = NULL;
 		}
 		switch (opt) {
+		case '1':
+			options.protocol = SSH_PROTO_1;
+			break;
 		case '2':
 			options.protocol = SSH_PROTO_2;
 			break;
--- ./ssh.1.orig-protoargs	Fri Oct 27 23:19:58 2000
+++ ./ssh.1	Tue Jan  9 15:40:01 2001
@@ -48,7 +48,7 @@
 .Op Ar command
 .Pp
 .Nm ssh
-.Op Fl afgknqtvxACNPTX246
+.Op Fl afgknqtvxACNPTX1246
 .Op Fl c Ar cipher_spec
 .Op Fl e Ar escape_char
 .Op Fl i Ar identity_file
@@ -539,6 +539,10 @@
 Port forwardings can also be specified in the configuration file.
 Privileged ports can be forwarded only when
 logging in as root on the remote machine.
+.It Fl 1
+Forces
+.Nm
+to try protocol version 1 only.
 .It Fl 2
 Forces
 .Nm
--- ./scp.1.orig-protoargs	Sun Nov  5 20:39:34 2000
+++ ./scp.1	Tue Jan  9 15:41:18 2001
@@ -19,7 +19,7 @@
 .Nd secure copy (remote file copy program)
 .Sh SYNOPSIS
 .Nm scp
-.Op Fl pqrvC46
+.Op Fl pqrvC1246
 .Op Fl S Ar program
 .Op Fl P Ar port
 .Op Fl c Ar cipher
@@ -110,6 +110,14 @@
 .It Fl o Ar option
 The given option is directly passed to
 .Xr ssh 1 .
+.It Fl 1
+Forces
+.Nm
+to try protocol version 1 only.
+.It Fl 2
+Forces
+.Nm
+to try protocol version 2 only.
 .It Fl 4
 Forces
 .Nm


More information about the openssh-unix-dev mailing list