ssh-keyscan for ssh2 (was Re: openssh-2.9p1)

Wayne Davison wayne at blorf.net
Sat May 19 07:40:18 EST 2001


On Fri, 18 May 2001, Wayne Davison wrote:
> I could change it so that it fired off both (all) requests in parallel
> to the same host.

I had some free time, so here's a patch that implements this.  If you
like the idea, apply it after my last patch.

..wayne..

---8<------8<------8<------8<---cut here--->8------>8------>8------>8---
Index: ssh-keyscan.c
--- old/ssh-keyscan.c	Fri May 18 14:20:53 2001
+++ ./ssh-keyscan.c	Fri May 18 14:31:17 2001
@@ -44,7 +44,11 @@
 int IPv4or6 = AF_UNSPEC;
 #endif

-int get_keytypes = 1;		/* Get only RSA1 keys by default */
+#define KT_RSA1	1
+#define KT_DSA	2
+#define KT_RSA	4
+
+int get_keytypes = KT_RSA1;	/* Get only RSA1 keys by default */

 #define MAXMAXFD 256

@@ -78,7 +82,7 @@
 	int c_plen;		/* Packet length field for ssh packet */
 	int c_len;		/* Total bytes which must be read. */
 	int c_off;		/* Length of data read so far. */
-	int c_keytypes;		/* bits: 1 = RSA1, 2 = DSA, 4 = RSA */
+	int c_keytype;		/* Only one of KT_RSA1, KT_DSA, or KT_RSA */
 	char *c_namebase;	/* Address to free for c_name and c_namelist */
 	char *c_name;		/* Hostname of connection for errors */
 	char *c_namelist;	/* Pointer to other possible addresses */
@@ -325,7 +329,7 @@

 	packet_set_connection(c->c_fd, c->c_fd);
 	packet_set_ssh2_format();
-	myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytypes & 2?
+	myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
 	    "ssh-dss": "ssh-rsa";
 	c->c_kex = kex_setup(myproposal);
 	c->c_kex->check_host_key = hostjump;
@@ -387,7 +391,7 @@
 }

 int
-conalloc(char *iname, char *oname, int keytypes)
+conalloc(char *iname, char *oname, int keytype)
 {
 	int s;
 	char *namebase, *name, *namelist;
@@ -416,7 +420,7 @@
 	fdcon[s].c_data = (char *) &fdcon[s].c_plen;
 	fdcon[s].c_len = 4;
 	fdcon[s].c_off = 0;
-	fdcon[s].c_keytypes = keytypes;
+	fdcon[s].c_keytype = keytype;
 	gettimeofday(&fdcon[s].c_tv, NULL);
 	fdcon[s].c_tv.tv_sec += timeout;
 	TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
@@ -451,23 +455,17 @@
 }

 int
-conrecycle(int s, int include_current_host)
+conrecycle(int s)
 {
 	int ret;
 	con *c = &fdcon[s];
 	char *iname, *oname;
-	int keytypes = c->c_keytypes;
+	int keytype = c->c_keytype;

-	if (include_current_host) {
-		iname = xmalloc(strlen(c->c_name) + strlen(c->c_namelist) + 2);
-		strcpy(iname, c->c_name);
-		if (*c->c_namelist)
-			sprintf(iname + strlen(iname), ",%s", c->c_namelist);
-	} else
-		iname = xstrdup(c->c_namelist);
+	iname = xstrdup(c->c_namelist);
 	oname = xstrdup(c->c_output_name);
 	confree(s);
-	ret = conalloc(iname, oname, keytypes);
+	ret = conalloc(iname, oname, keytype);
 	xfree(iname);
 	xfree(oname);
 	return (ret);
@@ -488,7 +486,7 @@
 	if (n < 0) {
 		if (errno != ECONNREFUSED)
 			error("read (%s): %s", c->c_name, strerror(errno));
-		conrecycle(s, 0);
+		conrecycle(s);
 		return;
 	}
 	if (*cp != '\n' && *cp != '\r') {
@@ -498,7 +496,7 @@
 	}
 	*cp = '\0';
 	fprintf(stderr, "# %s %s\n", c->c_name, buf);
-	if (!(c->c_keytypes & 1)) {
+	if (c->c_keytype != KT_RSA1) {
 		char remote_version[sizeof buf];

 		if (sscanf(buf, "SSH-%*d.%*d-%[^\n]\n", remote_version) == 1)
@@ -507,23 +505,16 @@
 			datafellows = 0;
 	}
 	n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
-	    c->c_keytypes & 1 ? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
-	    c->c_keytypes & 1 ? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
+	    c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
+	    c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
 	if (atomicio(write, s, buf, n) != n) {
 		error("write (%s): %s", c->c_name, strerror(errno));
 		confree(s);
 		return;
 	}
-	if (!(c->c_keytypes & 1)) {
+	if (c->c_keytype != KT_RSA1) {
 		keyprint(c, keygrab_ssh2(c));
-		if (c->c_keytypes & 2)
-			c->c_keytypes &= ~2;
-		else
-			c->c_keytypes &= ~4;
-		if (c->c_keytypes)
-			conrecycle(s, 1);
-		else
-			confree(s);
+		confree(s);
 		return;
 	}
 	c->c_status = CS_SIZE;
@@ -559,12 +550,7 @@
 			break;
 		case CS_KEYS:
 			keyprint(c, keygrab_ssh1(c));
-			c->c_keytypes &= ~1;
-
-			if (c->c_keytypes)
-				conrecycle(s, 1);
-			else
-				confree(s);
+			confree(s);
 			return;
 			break;
 		default:
@@ -623,7 +609,7 @@
 		int s = c->c_fd;

 		c = c->c_link.tqe_next;
-		conrecycle(s, 0);
+		conrecycle(s);
 	}
 }

@@ -670,21 +656,21 @@
 					get_keytypes = 0;
 					first_proto_option = 0;
 				}
-				get_keytypes |= 1;
+				get_keytypes |= KT_RSA1;
 				break;
 			case 'D':
 				if (first_proto_option) {
 					get_keytypes = 0;
 					first_proto_option = 0;
 				}
-				get_keytypes |= 2;
+				get_keytypes |= KT_DSA;
 				break;
 			case 'R':
 				if (first_proto_option) {
 					get_keytypes = 0;
 					first_proto_option = 0;
 				}
-				get_keytypes |= 4;
+				get_keytypes |= KT_RSA;
 				break;
 			case '4':
 				IPv4or6 = AF_INET;
@@ -758,12 +744,16 @@
 	do {
 		while (ncon < MAXCON) {
 			char *name;
+			int j;

 			host = nexthost(argc, argv);
 			if (host == NULL)
 				break;
 			name = strnnsep(&host, " \t\n");
-			conalloc(name, *host ? host : name, get_keytypes);
+			for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
+				if (get_keytypes & j)
+					conalloc(name, *host ? host : name, j);
+			}
 		}
 		conloop();
 	} while (host);
---8<------8<------8<------8<---cut here--->8------>8------>8------>8---




More information about the openssh-unix-dev mailing list