mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-08 08:10:55 +03:00
Added comment field in HostEntry for end-of-line comments in known_hosts file (#517)
* Added comment field in HostEntry for end-of-line comments in known_hosts file. * Also modified the getLine() method to return the comment, if it exists. * Fixed implementation * Add CODEOWNERS file Co-authored-by: Jeroen van Erp <jeroen@hierynomus.com>
This commit is contained in:
@@ -199,24 +199,22 @@ public class OpenSSHKnownHosts
|
||||
return new CommentEntry(line);
|
||||
}
|
||||
|
||||
final String[] split = line.split("\\s+");
|
||||
if(split.length < 3) {
|
||||
final String trimmed = line.trim();
|
||||
int minComponents = 3;
|
||||
if (trimmed.startsWith("@")) {
|
||||
minComponents = 4;
|
||||
}
|
||||
String[] split = trimmed.split("\\s+", minComponents + 1); // Add 1 for optional comments
|
||||
if(split.length < minComponents) {
|
||||
log.error("Error reading entry `{}`", line);
|
||||
return new BadHostEntry(line);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
if (split[i].isEmpty()) {
|
||||
i++;
|
||||
}
|
||||
|
||||
final Marker marker = Marker.fromString(split[i]);
|
||||
if (marker != null) {
|
||||
i++;
|
||||
}
|
||||
if(split.length < i + 3) {
|
||||
log.error("Error reading entry `{}`", line);
|
||||
return new BadHostEntry(line);
|
||||
}
|
||||
final String hostnames = split[i++];
|
||||
final String sType = split[i++];
|
||||
|
||||
@@ -234,6 +232,9 @@ public class OpenSSHKnownHosts
|
||||
}
|
||||
} else if (isBits(sType)) {
|
||||
type = KeyType.RSA;
|
||||
minComponents += 1;
|
||||
// re-split
|
||||
split = trimmed.split("\\s+", minComponents + 1); // Add 1 for optional comments
|
||||
// int bits = Integer.valueOf(sType);
|
||||
final BigInteger e = new BigInteger(split[i++]);
|
||||
final BigInteger n = new BigInteger(split[i++]);
|
||||
@@ -249,7 +250,13 @@ public class OpenSSHKnownHosts
|
||||
return new BadHostEntry(line);
|
||||
}
|
||||
|
||||
return new HostEntry(marker, hostnames, type, key);
|
||||
final String comment;
|
||||
if (i < split.length) {
|
||||
comment = split[i++];
|
||||
} else {
|
||||
comment = null;
|
||||
}
|
||||
return new HostEntry(marker, hostnames, type, key, comment);
|
||||
}
|
||||
|
||||
private boolean isBits(String type) {
|
||||
@@ -330,13 +337,19 @@ public class OpenSSHKnownHosts
|
||||
private final String hostPart;
|
||||
protected final KeyType type;
|
||||
protected final PublicKey key;
|
||||
private final String comment;
|
||||
private final KnownHostMatchers.HostMatcher matcher;
|
||||
|
||||
public HostEntry(Marker marker, String hostPart, KeyType type, PublicKey key) throws SSHException {
|
||||
this(marker, hostPart, type, key, "");
|
||||
}
|
||||
|
||||
public HostEntry(Marker marker, String hostPart, KeyType type, PublicKey key, String comment) throws SSHException {
|
||||
this.marker = marker;
|
||||
this.hostPart = hostPart;
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.comment = comment;
|
||||
this.matcher = KnownHostMatchers.createMatcher(hostPart);
|
||||
}
|
||||
|
||||
@@ -373,6 +386,9 @@ public class OpenSSHKnownHosts
|
||||
line.append(getHostPart());
|
||||
line.append(" ").append(type.toString());
|
||||
line.append(" ").append(getKeyString(key));
|
||||
|
||||
if (!comment.isEmpty()) line.append(" ").append(comment);
|
||||
|
||||
return line.toString();
|
||||
}
|
||||
|
||||
@@ -384,6 +400,10 @@ public class OpenSSHKnownHosts
|
||||
protected String getHostPart() {
|
||||
return hostPart;
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BadHostEntry implements KnownHostEntry {
|
||||
|
||||
Reference in New Issue
Block a user