JGitClientSession.java

  1. /*
  2.  * Copyright (C) 2018, 2019 Thomas Wolf <thomas.wolf@paranor.ch> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.internal.transport.sshd;

  11. import static java.text.MessageFormat.format;
  12. import static org.apache.sshd.core.CoreModuleProperties.MAX_IDENTIFICATION_SIZE;

  13. import java.io.IOException;
  14. import java.io.StreamCorruptedException;
  15. import java.net.SocketAddress;
  16. import java.nio.charset.StandardCharsets;
  17. import java.security.GeneralSecurityException;
  18. import java.security.PublicKey;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.Iterator;
  23. import java.util.LinkedHashSet;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Objects;
  27. import java.util.Set;

  28. import org.apache.sshd.client.ClientFactoryManager;
  29. import org.apache.sshd.client.config.hosts.HostConfigEntry;
  30. import org.apache.sshd.client.keyverifier.ServerKeyVerifier;
  31. import org.apache.sshd.client.session.ClientSessionImpl;
  32. import org.apache.sshd.common.AttributeRepository;
  33. import org.apache.sshd.common.FactoryManager;
  34. import org.apache.sshd.common.PropertyResolver;
  35. import org.apache.sshd.common.config.keys.KeyUtils;
  36. import org.apache.sshd.common.io.IoSession;
  37. import org.apache.sshd.common.io.IoWriteFuture;
  38. import org.apache.sshd.common.util.Readable;
  39. import org.apache.sshd.common.util.buffer.Buffer;
  40. import org.eclipse.jgit.errors.InvalidPatternException;
  41. import org.eclipse.jgit.fnmatch.FileNameMatcher;
  42. import org.eclipse.jgit.internal.transport.sshd.proxy.StatefulProxyConnector;
  43. import org.eclipse.jgit.transport.CredentialsProvider;
  44. import org.eclipse.jgit.transport.SshConstants;

  45. /**
  46.  * A {@link org.apache.sshd.client.session.ClientSession ClientSession} that can
  47.  * be associated with the {@link HostConfigEntry} the session was created for.
  48.  * The {@link JGitSshClient} creates such sessions and sets this association.
  49.  * <p>
  50.  * Also provides for associating a JGit {@link CredentialsProvider} with a
  51.  * session.
  52.  * </p>
  53.  */
  54. public class JGitClientSession extends ClientSessionImpl {

  55.     /**
  56.      * Default setting for the maximum number of bytes to read in the initial
  57.      * protocol version exchange. 64kb is what OpenSSH < 8.0 read; OpenSSH 8.0
  58.      * changed it to 8Mb, but that seems excessive for the purpose stated in RFC
  59.      * 4253. The Apache MINA sshd default in
  60.      * {@link org.apache.sshd.core.CoreModuleProperties#MAX_IDENTIFICATION_SIZE}
  61.      * is 16kb.
  62.      */
  63.     private static final int DEFAULT_MAX_IDENTIFICATION_SIZE = 64 * 1024;

  64.     private HostConfigEntry hostConfig;

  65.     private CredentialsProvider credentialsProvider;

  66.     private volatile StatefulProxyConnector proxyHandler;

  67.     /**
  68.      * @param manager
  69.      * @param session
  70.      * @throws Exception
  71.      */
  72.     public JGitClientSession(ClientFactoryManager manager, IoSession session)
  73.             throws Exception {
  74.         super(manager, session);
  75.     }

  76.     /**
  77.      * Retrieves the {@link HostConfigEntry} this session was created for.
  78.      *
  79.      * @return the {@link HostConfigEntry}, or {@code null} if none set
  80.      */
  81.     public HostConfigEntry getHostConfigEntry() {
  82.         return hostConfig;
  83.     }

  84.     /**
  85.      * Sets the {@link HostConfigEntry} this session was created for.
  86.      *
  87.      * @param hostConfig
  88.      *            the {@link HostConfigEntry}
  89.      */
  90.     public void setHostConfigEntry(HostConfigEntry hostConfig) {
  91.         this.hostConfig = hostConfig;
  92.     }

  93.     /**
  94.      * Sets the {@link CredentialsProvider} for this session.
  95.      *
  96.      * @param provider
  97.      *            to set
  98.      */
  99.     public void setCredentialsProvider(CredentialsProvider provider) {
  100.         credentialsProvider = provider;
  101.     }

  102.     /**
  103.      * Retrieves the {@link CredentialsProvider} set for this session.
  104.      *
  105.      * @return the provider, or {@code null} if none is set.
  106.      */
  107.     public CredentialsProvider getCredentialsProvider() {
  108.         return credentialsProvider;
  109.     }

  110.     /**
  111.      * Sets a {@link StatefulProxyConnector} to handle proxy connection
  112.      * protocols.
  113.      *
  114.      * @param handler
  115.      *            to set
  116.      */
  117.     public void setProxyHandler(StatefulProxyConnector handler) {
  118.         proxyHandler = handler;
  119.     }

  120.     @Override
  121.     protected IoWriteFuture sendIdentification(String ident)
  122.             throws IOException {
  123.         StatefulProxyConnector proxy = proxyHandler;
  124.         if (proxy != null) {
  125.             try {
  126.                 // We must not block here; the framework starts reading messages
  127.                 // from the peer only once the initial sendKexInit() following
  128.                 // this call to sendIdentification() has returned!
  129.                 proxy.runWhenDone(() -> {
  130.                     JGitClientSession.super.sendIdentification(ident);
  131.                     return null;
  132.                 });
  133.                 // Called only from the ClientSessionImpl constructor, where the
  134.                 // return value is ignored.
  135.                 return null;
  136.             } catch (IOException e) {
  137.                 throw e;
  138.             } catch (Exception other) {
  139.                 throw new IOException(other.getLocalizedMessage(), other);
  140.             }
  141.         }
  142.         return super.sendIdentification(ident);
  143.     }

  144.     @Override
  145.     protected byte[] sendKexInit()
  146.             throws IOException, GeneralSecurityException {
  147.         StatefulProxyConnector proxy = proxyHandler;
  148.         if (proxy != null) {
  149.             try {
  150.                 // We must not block here; the framework starts reading messages
  151.                 // from the peer only once the initial sendKexInit() has
  152.                 // returned!
  153.                 proxy.runWhenDone(() -> {
  154.                     JGitClientSession.super.sendKexInit();
  155.                     return null;
  156.                 });
  157.                 // This is called only from the ClientSessionImpl
  158.                 // constructor, where the return value is ignored.
  159.                 return null;
  160.             } catch (IOException | GeneralSecurityException e) {
  161.                 throw e;
  162.             } catch (Exception other) {
  163.                 throw new IOException(other.getLocalizedMessage(), other);
  164.             }
  165.         }
  166.         return super.sendKexInit();
  167.     }

  168.     /**
  169.      * {@inheritDoc}
  170.      *
  171.      * As long as we're still setting up the proxy connection, diverts messages
  172.      * to the {@link StatefulProxyConnector}.
  173.      */
  174.     @Override
  175.     public void messageReceived(Readable buffer) throws Exception {
  176.         StatefulProxyConnector proxy = proxyHandler;
  177.         if (proxy != null) {
  178.             proxy.messageReceived(getIoSession(), buffer);
  179.         } else {
  180.             super.messageReceived(buffer);
  181.         }
  182.     }

  183.     @Override
  184.     protected String resolveAvailableSignaturesProposal(
  185.             FactoryManager manager) {
  186.         Set<String> defaultSignatures = new LinkedHashSet<>();
  187.         defaultSignatures.addAll(getSignatureFactoriesNames());
  188.         HostConfigEntry config = resolveAttribute(
  189.                 JGitSshClient.HOST_CONFIG_ENTRY);
  190.         String hostKeyAlgorithms = config
  191.                 .getProperty(SshConstants.HOST_KEY_ALGORITHMS);
  192.         if (hostKeyAlgorithms != null && !hostKeyAlgorithms.isEmpty()) {
  193.             char first = hostKeyAlgorithms.charAt(0);
  194.             switch (first) {
  195.             case '+':
  196.                 // Additions make not much sense -- it's either in
  197.                 // defaultSignatures already, or we have no implementation for
  198.                 // it. No point in proposing it.
  199.                 return String.join(",", defaultSignatures); //$NON-NLS-1$
  200.             case '-':
  201.                 // This takes wildcard patterns!
  202.                 removeFromList(defaultSignatures,
  203.                         SshConstants.HOST_KEY_ALGORITHMS,
  204.                         hostKeyAlgorithms.substring(1));
  205.                 if (defaultSignatures.isEmpty()) {
  206.                     // Too bad: user config error. Warn here, and then fail
  207.                     // later.
  208.                     log.warn(format(
  209.                             SshdText.get().configNoRemainingHostKeyAlgorithms,
  210.                             hostKeyAlgorithms));
  211.                 }
  212.                 return String.join(",", defaultSignatures); //$NON-NLS-1$
  213.             default:
  214.                 // Default is overridden -- only accept the ones for which we do
  215.                 // have an implementation.
  216.                 List<String> newNames = filteredList(defaultSignatures,
  217.                         hostKeyAlgorithms);
  218.                 if (newNames.isEmpty()) {
  219.                     log.warn(format(
  220.                             SshdText.get().configNoKnownHostKeyAlgorithms,
  221.                             hostKeyAlgorithms));
  222.                     // Use the default instead.
  223.                 } else {
  224.                     return String.join(",", newNames); //$NON-NLS-1$
  225.                 }
  226.                 break;
  227.             }
  228.         }
  229.         // No HostKeyAlgorithms; using default -- change order to put existing
  230.         // keys first.
  231.         ServerKeyVerifier verifier = getServerKeyVerifier();
  232.         if (verifier instanceof ServerKeyLookup) {
  233.             SocketAddress remoteAddress = resolvePeerAddress(
  234.                     resolveAttribute(JGitSshClient.ORIGINAL_REMOTE_ADDRESS));
  235.             List<PublicKey> allKnownKeys = ((ServerKeyLookup) verifier)
  236.                     .lookup(this, remoteAddress);
  237.             Set<String> reordered = new LinkedHashSet<>();
  238.             for (PublicKey key : allKnownKeys) {
  239.                 if (key != null) {
  240.                     String keyType = KeyUtils.getKeyType(key);
  241.                     if (keyType != null) {
  242.                         reordered.add(keyType);
  243.                     }
  244.                 }
  245.             }
  246.             reordered.addAll(defaultSignatures);
  247.             return String.join(",", reordered); //$NON-NLS-1$
  248.         }
  249.         return String.join(",", defaultSignatures); //$NON-NLS-1$
  250.     }

  251.     private void removeFromList(Set<String> current, String key,
  252.             String patterns) {
  253.         for (String toRemove : patterns.split("\\s*,\\s*")) { //$NON-NLS-1$
  254.             if (toRemove.indexOf('*') < 0 && toRemove.indexOf('?') < 0) {
  255.                 current.remove(toRemove);
  256.                 continue;
  257.             }
  258.             try {
  259.                 FileNameMatcher matcher = new FileNameMatcher(toRemove, null);
  260.                 for (Iterator<String> i = current.iterator(); i.hasNext();) {
  261.                     matcher.reset();
  262.                     matcher.append(i.next());
  263.                     if (matcher.isMatch()) {
  264.                         i.remove();
  265.                     }
  266.                 }
  267.             } catch (InvalidPatternException e) {
  268.                 log.warn(format(SshdText.get().configInvalidPattern, key,
  269.                         toRemove));
  270.             }
  271.         }
  272.     }

  273.     private List<String> filteredList(Set<String> known, String values) {
  274.         List<String> newNames = new ArrayList<>();
  275.         for (String newValue : values.split("\\s*,\\s*")) { //$NON-NLS-1$
  276.             if (known.contains(newValue)) {
  277.                 newNames.add(newValue);
  278.             }
  279.         }
  280.         return newNames;
  281.     }

  282.     /**
  283.      * Reads the RFC 4253, section 4.2 protocol version identification. The
  284.      * Apache MINA sshd default implementation checks for NUL bytes also in any
  285.      * preceding lines, whereas RFC 4253 requires such a check only for the
  286.      * actual identification string starting with "SSH-". Likewise, the 255
  287.      * character limit exists only for the identification string, not for the
  288.      * preceding lines. CR-LF handling is also relaxed.
  289.      *
  290.      * @param buffer
  291.      *            to read from
  292.      * @param server
  293.      *            whether we're an SSH server (should always be {@code false})
  294.      * @return the lines read, with the server identification line last, or
  295.      *         {@code null} if no identification line was found and more bytes
  296.      *         are needed
  297.      * @throws StreamCorruptedException
  298.      *             if the identification is malformed
  299.      * @see <a href="https://tools.ietf.org/html/rfc4253#section-4.2">RFC 4253,
  300.      *      section 4.2</a>
  301.      */
  302.     @Override
  303.     protected List<String> doReadIdentification(Buffer buffer, boolean server)
  304.             throws StreamCorruptedException {
  305.         if (server) {
  306.             // Should never happen. No translation; internal bug.
  307.             throw new IllegalStateException(
  308.                     "doReadIdentification of client called with server=true"); //$NON-NLS-1$
  309.         }
  310.         Integer maxIdentLength = MAX_IDENTIFICATION_SIZE.get(this).orElse(null);
  311.         int maxIdentSize;
  312.         if (maxIdentLength == null || maxIdentLength
  313.                 .intValue() < DEFAULT_MAX_IDENTIFICATION_SIZE) {
  314.             maxIdentSize = DEFAULT_MAX_IDENTIFICATION_SIZE;
  315.             MAX_IDENTIFICATION_SIZE.set(this, Integer.valueOf(maxIdentSize));
  316.         } else {
  317.             maxIdentSize = maxIdentLength.intValue();
  318.         }
  319.         int current = buffer.rpos();
  320.         int end = current + buffer.available();
  321.         if (current >= end) {
  322.             return null;
  323.         }
  324.         byte[] raw = buffer.array();
  325.         List<String> ident = new ArrayList<>();
  326.         int start = current;
  327.         boolean hasNul = false;
  328.         for (int i = current; i < end; i++) {
  329.             switch (raw[i]) {
  330.             case 0:
  331.                 hasNul = true;
  332.                 break;
  333.             case '\n':
  334.                 int eol = 1;
  335.                 if (i > start && raw[i - 1] == '\r') {
  336.                     eol++;
  337.                 }
  338.                 String line = new String(raw, start, i + 1 - eol - start,
  339.                         StandardCharsets.UTF_8);
  340.                 start = i + 1;
  341.                 if (log.isDebugEnabled()) {
  342.                     log.debug(format("doReadIdentification({0}) line: ", this) + //$NON-NLS-1$
  343.                             escapeControls(line));
  344.                 }
  345.                 ident.add(line);
  346.                 if (line.startsWith("SSH-")) { //$NON-NLS-1$
  347.                     if (hasNul) {
  348.                         throw new StreamCorruptedException(
  349.                                 format(SshdText.get().serverIdWithNul,
  350.                                         escapeControls(line)));
  351.                     }
  352.                     if (line.length() + eol > 255) {
  353.                         throw new StreamCorruptedException(
  354.                                 format(SshdText.get().serverIdTooLong,
  355.                                         escapeControls(line)));
  356.                     }
  357.                     buffer.rpos(start);
  358.                     return ident;
  359.                 }
  360.                 // If this were a server, we could throw an exception here: a
  361.                 // client is not supposed to send any extra lines before its
  362.                 // identification string.
  363.                 hasNul = false;
  364.                 break;
  365.             default:
  366.                 break;
  367.             }
  368.             if (i - current + 1 >= maxIdentSize) {
  369.                 String msg = format(SshdText.get().serverIdNotReceived,
  370.                         Integer.toString(maxIdentSize));
  371.                 if (log.isDebugEnabled()) {
  372.                     log.debug(msg);
  373.                     log.debug(buffer.toHex());
  374.                 }
  375.                 throw new StreamCorruptedException(msg);
  376.             }
  377.         }
  378.         // Need more data
  379.         return null;
  380.     }

  381.     private static String escapeControls(String s) {
  382.         StringBuilder b = new StringBuilder();
  383.         int l = s.length();
  384.         for (int i = 0; i < l; i++) {
  385.             char ch = s.charAt(i);
  386.             if (Character.isISOControl(ch)) {
  387.                 b.append(ch <= 0xF ? "\\u000" : "\\u00") //$NON-NLS-1$ //$NON-NLS-2$
  388.                         .append(Integer.toHexString(ch));
  389.             } else {
  390.                 b.append(ch);
  391.             }
  392.         }
  393.         return b.toString();
  394.     }

  395.     @Override
  396.     public <T> T getAttribute(AttributeKey<T> key) {
  397.         T value = super.getAttribute(key);
  398.         if (value == null) {
  399.             IoSession ioSession = getIoSession();
  400.             if (ioSession != null) {
  401.                 Object obj = ioSession.getAttribute(AttributeRepository.class);
  402.                 if (obj instanceof AttributeRepository) {
  403.                     AttributeRepository sessionAttributes = (AttributeRepository) obj;
  404.                     value = sessionAttributes.resolveAttribute(key);
  405.                 }
  406.             }
  407.         }
  408.         return value;
  409.     }

  410.     @Override
  411.     public PropertyResolver getParentPropertyResolver() {
  412.         IoSession ioSession = getIoSession();
  413.         if (ioSession != null) {
  414.             Object obj = ioSession.getAttribute(AttributeRepository.class);
  415.             if (obj instanceof PropertyResolver) {
  416.                 return (PropertyResolver) obj;
  417.             }
  418.         }
  419.         return super.getParentPropertyResolver();
  420.     }

  421.     /**
  422.      * An {@link AttributeRepository} that chains together two other attribute
  423.      * sources in a hierarchy.
  424.      */
  425.     public static class ChainingAttributes implements AttributeRepository {

  426.         private final AttributeRepository delegate;

  427.         private final AttributeRepository parent;

  428.         /**
  429.          * Create a new {@link ChainingAttributes} attribute source.
  430.          *
  431.          * @param self
  432.          *            to search for attributes first
  433.          * @param parent
  434.          *            to search for attributes if not found in {@code self}
  435.          */
  436.         public ChainingAttributes(AttributeRepository self,
  437.                 AttributeRepository parent) {
  438.             this.delegate = self;
  439.             this.parent = parent;
  440.         }

  441.         @Override
  442.         public int getAttributesCount() {
  443.             return delegate.getAttributesCount();
  444.         }

  445.         @Override
  446.         public <T> T getAttribute(AttributeKey<T> key) {
  447.             return delegate.getAttribute(Objects.requireNonNull(key));
  448.         }

  449.         @Override
  450.         public Collection<AttributeKey<?>> attributeKeys() {
  451.             return delegate.attributeKeys();
  452.         }

  453.         @Override
  454.         public <T> T resolveAttribute(AttributeKey<T> key) {
  455.             T value = getAttribute(Objects.requireNonNull(key));
  456.             if (value == null) {
  457.                 return parent.getAttribute(key);
  458.             }
  459.             return value;
  460.         }
  461.     }

  462.     /**
  463.      * A {@link ChainingAttributes} repository that doubles as a
  464.      * {@link PropertyResolver}. The property map can be set via the attribute
  465.      * key {@link SessionAttributes#PROPERTIES}.
  466.      */
  467.     public static class SessionAttributes extends ChainingAttributes
  468.             implements PropertyResolver {

  469.         /** Key for storing a map of properties in the attributes. */
  470.         public static final AttributeKey<Map<String, Object>> PROPERTIES = new AttributeKey<>();

  471.         private final PropertyResolver parentProperties;

  472.         /**
  473.          * Creates a new {@link SessionAttributes} attribute and property
  474.          * source.
  475.          *
  476.          * @param self
  477.          *            to search for attributes first
  478.          * @param parent
  479.          *            to search for attributes if not found in {@code self}
  480.          * @param parentProperties
  481.          *            to search for properties if not found in {@code self}
  482.          */
  483.         public SessionAttributes(AttributeRepository self,
  484.                 AttributeRepository parent, PropertyResolver parentProperties) {
  485.             super(self, parent);
  486.             this.parentProperties = parentProperties;
  487.         }

  488.         @Override
  489.         public PropertyResolver getParentPropertyResolver() {
  490.             return parentProperties;
  491.         }

  492.         @Override
  493.         public Map<String, Object> getProperties() {
  494.             Map<String, Object> props = getAttribute(PROPERTIES);
  495.             return props == null ? Collections.emptyMap() : props;
  496.         }
  497.     }
  498. }