Repository: mina-sshd
Updated Branches:
  refs/heads/master 31c8c3d31 -> 92c24d13c


[SSHD-752] HostConfigEntry can't parse tabbed SSH config files


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/92c24d13
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/92c24d13
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/92c24d13

Branch: refs/heads/master
Commit: 92c24d13cf5333118402288c33750434058556bf
Parents: 31c8c3d
Author: Goldstein Lyor <l...@c-b4.com>
Authored: Wed Jun 21 12:46:20 2017 +0300
Committer: Lyor Goldstein <lyor.goldst...@gmail.com>
Committed: Wed Jun 21 22:38:05 2017 +0300

----------------------------------------------------------------------
 .../client/config/hosts/HostConfigEntry.java    |  4 ++--
 .../client/config/hosts/HostPatternsHolder.java | 12 ++++++----
 .../client/config/hosts/KnownHostEntry.java     |  2 +-
 .../client/config/hosts/KnownHostHashValue.java |  6 +++--
 .../common/config/keys/AuthorizedKeyEntry.java  | 24 ++++++++++----------
 .../sshd/common/config/keys/PublicKeyEntry.java |  8 ++++---
 .../apache/sshd/common/util/GenericUtils.java   |  8 +++++++
 7 files changed, 39 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/92c24d13/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/HostConfigEntry.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/HostConfigEntry.java
 
b/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/HostConfigEntry.java
index c17c6ce..3711e45 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/HostConfigEntry.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/HostConfigEntry.java
@@ -828,7 +828,7 @@ public class HostConfigEntry extends HostPatternsHolder 
implements MutableUserHo
 
         int lineNumber = 1;
         for (String line = rdr.readLine(); line != null; line = 
rdr.readLine(), lineNumber++) {
-            line = GenericUtils.trimToEmpty(line);
+            line = GenericUtils.replaceWhitespaceAndTrim(line);
             if (GenericUtils.isEmpty(line)) {
                 continue;
             }
@@ -1046,7 +1046,7 @@ public class HostConfigEntry extends HostPatternsHolder 
implements MutableUserHo
      * @return A {@link List} of the encountered values
      */
     public static List<String> parseConfigValue(String value) {
-        String s = GenericUtils.trimToEmpty(value);
+        String s = GenericUtils.replaceWhitespaceAndTrim(value);
         if (GenericUtils.isEmpty(s)) {
             return Collections.emptyList();
         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/92c24d13/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/HostPatternsHolder.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/HostPatternsHolder.java
 
b/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/HostPatternsHolder.java
index fdef86e..9d90dac 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/HostPatternsHolder.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/HostPatternsHolder.java
@@ -25,6 +25,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Objects;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -242,7 +243,7 @@ public abstract class HostPatternsHolder {
      * Converts a host pattern string to a regular expression matcher.
      * <B>Note:</B> pattern matching is <U>case insensitive</U>
      *
-     * @param pattern The original pattern string - ignored if {@code 
null}/empty
+     * @param patternString The original pattern string - ignored if {@code 
null}/empty
      * @return The regular expression matcher {@link Pattern} and the 
indication
      * whether it is a negating pattern or not - {@code null} if no original 
string
      * @see #NON_STANDARD_PORT_PATTERN_ENCLOSURE_START_DELIM
@@ -251,7 +252,8 @@ public abstract class HostPatternsHolder {
      * @see #SINGLE_CHAR_PATTERN
      * @see #NEGATION_CHAR_PATTERN
      */
-    public static HostPatternValue toPattern(CharSequence pattern) {
+    public static HostPatternValue toPattern(CharSequence patternString) {
+        String pattern = 
GenericUtils.replaceWhitespaceAndTrim(Objects.toString(patternString, null));
         if (GenericUtils.isEmpty(pattern)) {
             return null;
         }
@@ -267,11 +269,11 @@ public abstract class HostPatternsHolder {
             ValidateUtils.checkTrue(pattern.charAt(pos - 1) == 
HostPatternsHolder.NON_STANDARD_PORT_PATTERN_ENCLOSURE_END_DELIM,
                 "Invalid non-standard port value host pattern enclosure 
delimiters in %s", pattern);
 
-            CharSequence csPort = pattern.subSequence(pos + 1, patternLen);
-            port = Integer.parseInt(csPort.toString());
+            String csPort = pattern.substring(pos + 1, patternLen);
+            port = Integer.parseInt(csPort);
             ValidateUtils.checkTrue((port > 0) && (port <= 0xFFFF), "Invalid 
non-start port value (%d) in %s", port, pattern);
 
-            pattern = pattern.subSequence(1, pos - 1);
+            pattern = pattern.substring(1, pos - 1);
             patternLen = pattern.length();
         }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/92c24d13/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/KnownHostEntry.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/KnownHostEntry.java
 
b/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/KnownHostEntry.java
index bf56ddf..544a5ce 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/KnownHostEntry.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/KnownHostEntry.java
@@ -229,7 +229,7 @@ public class KnownHostEntry extends HostPatternsHolder {
     }
 
     public static <E extends KnownHostEntry> E parseKnownHostEntry(E entry, 
String data) {
-        String line = data;
+        String line = GenericUtils.replaceWhitespaceAndTrim(data);
         if (GenericUtils.isEmpty(line) || (line.charAt(0) == 
PublicKeyEntry.COMMENT_CHAR)) {
             return entry;
         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/92c24d13/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/KnownHostHashValue.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/KnownHostHashValue.java
 
b/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/KnownHostHashValue.java
index 68ab000..28a3d1f 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/KnownHostHashValue.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/client/config/hosts/KnownHostHashValue.java
@@ -143,11 +143,13 @@ public class KnownHostHashValue {
         return sb;
     }
 
-    public static KnownHostHashValue parse(String pattern) {
+    public static KnownHostHashValue parse(String patternString) {
+        String pattern = GenericUtils.replaceWhitespaceAndTrim(patternString);
         return parse(pattern, GenericUtils.isEmpty(pattern) ? null : new 
KnownHostHashValue());
     }
 
-    public static <V extends KnownHostHashValue> V parse(String pattern, V 
value) {
+    public static <V extends KnownHostHashValue> V parse(String patternString, 
V value) {
+        String pattern = GenericUtils.replaceWhitespaceAndTrim(patternString);
         if (GenericUtils.isEmpty(pattern)) {
             return value;
         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/92c24d13/sshd-core/src/main/java/org/apache/sshd/common/config/keys/AuthorizedKeyEntry.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/config/keys/AuthorizedKeyEntry.java
 
b/sshd-core/src/main/java/org/apache/sshd/common/config/keys/AuthorizedKeyEntry.java
index 9df2872..ac4ddde 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/common/config/keys/AuthorizedKeyEntry.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/common/config/keys/AuthorizedKeyEntry.java
@@ -39,9 +39,11 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.NavigableMap;
 import java.util.TreeMap;
 
 import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.common.util.ValidateUtils;
 import org.apache.sshd.common.util.io.NoCloseInputStream;
 import org.apache.sshd.common.util.io.NoCloseReader;
 import org.apache.sshd.server.auth.pubkey.KeySetPublickeyAuthenticator;
@@ -264,7 +266,7 @@ public class AuthorizedKeyEntry extends PublicKeyEntry {
         for (String line = rdr.readLine(); line != null; line = 
rdr.readLine()) {
             AuthorizedKeyEntry entry;
             try {
-                entry = parseAuthorizedKeyEntry(line.trim());
+                entry = parseAuthorizedKeyEntry(line);
                 if (entry == null) {    // null, empty or comment line
                     continue;
                 }
@@ -288,13 +290,14 @@ public class AuthorizedKeyEntry extends PublicKeyEntry {
     }
 
     /**
-     * @param line Original line from an <code>authorized_keys</code> file
+     * @param value Original line from an <code>authorized_keys</code> file
      * @return {@link AuthorizedKeyEntry} or {@code null} if the line is
      * {@code null}/empty or a comment line
      * @throws IllegalArgumentException If failed to parse/decode the line
      * @see #COMMENT_CHAR
      */
-    public static AuthorizedKeyEntry parseAuthorizedKeyEntry(String line) 
throws IllegalArgumentException {
+    public static AuthorizedKeyEntry parseAuthorizedKeyEntry(String value) 
throws IllegalArgumentException {
+        String line = GenericUtils.replaceWhitespaceAndTrim(value);
         if (GenericUtils.isEmpty(line) || (line.charAt(0) == COMMENT_CHAR) /* 
comment ? */) {
             return null;
         }
@@ -311,13 +314,10 @@ public class AuthorizedKeyEntry extends PublicKeyEntry {
 
         String keyType = line.substring(0, startPos);
         PublicKeyEntryDecoder<?, ?> decoder = 
KeyUtils.getPublicKeyEntryDecoder(keyType);
-        final AuthorizedKeyEntry entry;
+        AuthorizedKeyEntry entry;
         if (decoder == null) {  // assume this is due to the fact that it 
starts with login options
             entry = parseAuthorizedKeyEntry(line.substring(startPos + 
1).trim());
-            if (entry == null) {
-                throw new IllegalArgumentException("Bad format (no key data 
after login options): " + line);
-            }
-
+            ValidateUtils.checkTrue(entry != null, "Bad format (no key data 
after login options): %s", line);
             entry.setLoginOptions(parseLoginOptions(keyType));
         } else {
             String encData = (endPos < (line.length() - 1)) ? 
line.substring(0, endPos).trim() : line;
@@ -329,14 +329,14 @@ public class AuthorizedKeyEntry extends PublicKeyEntry {
         return entry;
     }
 
-    public static Map<String, String> parseLoginOptions(String options) {
+    public static NavigableMap<String, String> parseLoginOptions(String 
options) {
         // TODO add support if quoted values contain ','
-        String[] pairs = GenericUtils.split(options, ',');
+        String[] pairs = 
GenericUtils.split(GenericUtils.replaceWhitespaceAndTrim(options), ',');
         if (GenericUtils.isEmpty(pairs)) {
-            return Collections.emptyMap();
+            return Collections.emptyNavigableMap();
         }
 
-        Map<String, String> optsMap = new 
TreeMap<>(String.CASE_INSENSITIVE_ORDER);
+        NavigableMap<String, String> optsMap = new 
TreeMap<>(String.CASE_INSENSITIVE_ORDER);
         for (String p : pairs) {
             p = GenericUtils.trimToEmpty(p);
             if (GenericUtils.isEmpty(p)) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/92c24d13/sshd-core/src/main/java/org/apache/sshd/common/config/keys/PublicKeyEntry.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/config/keys/PublicKeyEntry.java
 
b/sshd-core/src/main/java/org/apache/sshd/common/config/keys/PublicKeyEntry.java
index 95720b4..41e7b42 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/common/config/keys/PublicKeyEntry.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/common/config/keys/PublicKeyEntry.java
@@ -173,7 +173,8 @@ public class PublicKeyEntry implements Serializable {
      * @throws IllegalArgumentException if bad format found
      * @see #parsePublicKeyEntry(PublicKeyEntry, String)
      */
-    public static PublicKeyEntry parsePublicKeyEntry(String data) throws 
IllegalArgumentException {
+    public static PublicKeyEntry parsePublicKeyEntry(String encData) throws 
IllegalArgumentException {
+        String data = GenericUtils.replaceWhitespaceAndTrim(encData);
         if (GenericUtils.isEmpty(data)) {
             return null;
         } else {
@@ -185,12 +186,13 @@ public class PublicKeyEntry implements Serializable {
      * @param <E>   The generic entry type
      * @param entry The {@link PublicKeyEntry} whose contents are to be
      *              updated - ignored if {@code null}
-     * @param data  Assumed to contain at least {@code key-type base64-data} 
(anything
+     * @param encData Assumed to contain at least {@code key-type base64-data} 
(anything
      *              beyond the BASE64 data is ignored) - ignored if {@code 
null}/empty
      * @return The updated entry instance
      * @throws IllegalArgumentException if bad format found
      */
-    public static <E extends PublicKeyEntry> E parsePublicKeyEntry(E entry, 
String data) throws IllegalArgumentException {
+    public static <E extends PublicKeyEntry> E parsePublicKeyEntry(E entry, 
String encData) throws IllegalArgumentException {
+        String data = GenericUtils.replaceWhitespaceAndTrim(encData);
         if (GenericUtils.isEmpty(data) || (entry == null)) {
             return entry;
         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/92c24d13/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java
----------------------------------------------------------------------
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java 
b/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java
index bd8a7a3..28456f9 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/GenericUtils.java
@@ -97,6 +97,14 @@ public final class GenericUtils {
         }
     }
 
+    public static String replaceWhitespaceAndTrim(String s) {
+        if (s != null) {
+            s = s.replace('\t', ' ');
+        }
+
+        return trimToEmpty(s);
+    }
+
     /**
      * @param s The {@link String} value to calculate the hash code on - may
      * be <code>null</code>/empty in which case a value of zero is returned

Reply via email to