ssh-keyscan namelist fixup

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

 



From b55d9be2122f52ed616f66c52b05dd30c4fd6da1 Mon Sep 17 00:00:00 2001
From: Reuben Hawkins <reubenhwk@xxxxxxxxx>
Date: Tue, 27 Oct 2015 11:42:17 -0700
Subject: [PATCH] ssh-keyscan.c: fixup output host namelist

Scan the namelist for spaces, squash them, and replace with a single comma.  The result is
a properly formatted known_host file even when the namelist is improperly formatted.

before:

 $ ssh-keyscan "farnsworth.local altname1 altname2   farnsworth" 2>/dev/null | cut -c 1-70
 altname1 altname2   farnsworth ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC

after:

 $ ssh-keyscan "farnsworth.local altname1 altname2   farnsworth" 2>/dev/null | cut -c 1-70
 altname1,altname2,farnsworth ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC1G
---
 ssh-keyscan.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/ssh-keyscan.c b/ssh-keyscan.c
index 7db0e10..749c044 100644
--- a/ssh-keyscan.c
+++ b/ssh-keyscan.c
@@ -21,6 +21,7 @@
 
 #include <openssl/bn.h>
 
+#include <ctype.h>
 #include <netdb.h>
 #include <errno.h>
 #include <stdarg.h>
@@ -622,6 +623,44 @@ conloop(void)
 	}
 }
 
+static int
+isseperator(char c)
+{
+	return c == ',' || isspace(c);
+}
+
+/* Sanitize the output namelist by replacing spaces with commas. */
+static char *
+fixup_namelist(char *namelist)
+{
+	while (namelist[0] && isseperator(namelist[0]))
+		++namelist;
+
+	char * retval = namelist;
+
+	int next = 0;
+	while (namelist[0]) {
+		if (isseperator(namelist[next])) {
+			while (namelist[next+1] && isseperator(namelist[next+1]))
+				++next;
+
+			/* End of list?  End with a '\0'. */
+			if (namelist[next+1]) {
+				namelist[0] = ',';
+			} else {
+				namelist[0] = '\0';
+				goto done;
+			}
+		} else {
+			namelist[0] = namelist[next];
+		}
+
+		++namelist;
+	}
+done:
+	return retval;
+}
+
 static void
 do_host(char *host)
 {
@@ -630,6 +669,10 @@ do_host(char *host)
 
 	if (name == NULL)
 		return;
+
+	if (host)
+		host = fixup_namelist(host);
+
 	for (j = KT_RSA1; j <= KT_ED25519; j *= 2) {
 		if (get_keytypes & j) {
 			while (ncon >= MAXCON)
-- 
1.9.1

_______________________________________________
openssh-unix-dev mailing list
openssh-unix-dev@xxxxxxxxxxx
https://lists.mindrot.org/mailman/listinfo/openssh-unix-dev

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux