You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SshConfigSessionFactory.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
  3. * Copyright (C) 2008-2009, Google Inc.
  4. * Copyright (C) 2009, Google, Inc.
  5. * Copyright (C) 2009, JetBrains s.r.o.
  6. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  7. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  8. * and other copyright owners as documented in the project's IP log.
  9. *
  10. * This program and the accompanying materials are made available
  11. * under the terms of the Eclipse Distribution License v1.0 which
  12. * accompanies this distribution, is reproduced below, and is
  13. * available at http://www.eclipse.org/org/documents/edl-v10.php
  14. *
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or
  18. * without modification, are permitted provided that the following
  19. * conditions are met:
  20. *
  21. * - Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. *
  24. * - Redistributions in binary form must reproduce the above
  25. * copyright notice, this list of conditions and the following
  26. * disclaimer in the documentation and/or other materials provided
  27. * with the distribution.
  28. *
  29. * - Neither the name of the Eclipse Foundation, Inc. nor the
  30. * names of its contributors may be used to endorse or promote
  31. * products derived from this software without specific prior
  32. * written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  35. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  36. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  38. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  39. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  43. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  46. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. */
  48. package org.eclipse.jgit.transport;
  49. import java.io.File;
  50. import java.io.FileInputStream;
  51. import java.io.FileNotFoundException;
  52. import java.io.IOException;
  53. import java.util.HashMap;
  54. import java.util.Map;
  55. import org.eclipse.jgit.util.FS;
  56. import com.jcraft.jsch.JSch;
  57. import com.jcraft.jsch.JSchException;
  58. import com.jcraft.jsch.Session;
  59. import com.jcraft.jsch.UserInfo;
  60. /**
  61. * The base session factory that loads known hosts and private keys from
  62. * <code>$HOME/.ssh</code>.
  63. * <p>
  64. * This is the default implementation used by JGit and provides most of the
  65. * compatibility necessary to match OpenSSH, a popular implementation of SSH
  66. * used by C Git.
  67. * <p>
  68. * The factory does not provide UI behavior. Override the method
  69. * {@link #configure(org.eclipse.jgit.transport.OpenSshConfig.Host, Session)}
  70. * to supply appropriate {@link UserInfo} to the session.
  71. */
  72. public abstract class SshConfigSessionFactory extends SshSessionFactory {
  73. private final Map<String, JSch> byIdentityFile = new HashMap<String, JSch>();
  74. private JSch defaultJSch;
  75. private OpenSshConfig config;
  76. @Override
  77. public synchronized Session getSession(String user, String pass,
  78. String host, int port, CredentialsProvider credentialsProvider,
  79. FS fs) throws JSchException {
  80. if (config == null)
  81. config = OpenSshConfig.get(fs);
  82. final OpenSshConfig.Host hc = config.lookup(host);
  83. host = hc.getHostName();
  84. if (port <= 0)
  85. port = hc.getPort();
  86. if (user == null)
  87. user = hc.getUser();
  88. final Session session = createSession(hc, user, host, port, fs);
  89. if (pass != null)
  90. session.setPassword(pass);
  91. final String strictHostKeyCheckingPolicy = hc
  92. .getStrictHostKeyChecking();
  93. if (strictHostKeyCheckingPolicy != null)
  94. session.setConfig("StrictHostKeyChecking",
  95. strictHostKeyCheckingPolicy);
  96. final String pauth = hc.getPreferredAuthentications();
  97. if (pauth != null)
  98. session.setConfig("PreferredAuthentications", pauth);
  99. if (credentialsProvider != null
  100. && (!hc.isBatchMode() || !credentialsProvider.isInteractive())) {
  101. session.setUserInfo(new CredentialsProviderUserInfo(session,
  102. credentialsProvider));
  103. }
  104. configure(hc, session);
  105. return session;
  106. }
  107. /**
  108. * Create a new JSch session for the requested address.
  109. *
  110. * @param hc
  111. * host configuration
  112. * @param user
  113. * login to authenticate as.
  114. * @param host
  115. * server name to connect to.
  116. * @param port
  117. * port number of the SSH daemon (typically 22).
  118. * @param fs
  119. * the file system abstraction which will be necessary to
  120. * perform certain file system operations.
  121. * @return new session instance, but otherwise unconfigured.
  122. * @throws JSchException
  123. * the session could not be created.
  124. */
  125. protected Session createSession(final OpenSshConfig.Host hc,
  126. final String user, final String host, final int port, FS fs)
  127. throws JSchException {
  128. return getJSch(hc, fs).getSession(user, host, port);
  129. }
  130. /**
  131. * Provide additional configuration for the session based on the host
  132. * information. This method could be used to supply {@link UserInfo}.
  133. *
  134. * @param hc
  135. * host configuration
  136. * @param session
  137. * session to configure
  138. */
  139. protected abstract void configure(OpenSshConfig.Host hc, Session session);
  140. /**
  141. * Obtain the JSch used to create new sessions.
  142. *
  143. * @param hc
  144. * host configuration
  145. * @param fs
  146. * the file system abstraction which will be necessary to
  147. * perform certain file system operations.
  148. * @return the JSch instance to use.
  149. * @throws JSchException
  150. * the user configuration could not be created.
  151. */
  152. protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException {
  153. if (defaultJSch == null) {
  154. defaultJSch = createDefaultJSch(fs);
  155. for (Object name : defaultJSch.getIdentityNames()) {
  156. byIdentityFile.put((String) name, defaultJSch);
  157. }
  158. }
  159. final File identityFile = hc.getIdentityFile();
  160. if (identityFile == null) {
  161. return defaultJSch;
  162. }
  163. final String identityKey = identityFile.getAbsolutePath();
  164. JSch jsch = byIdentityFile.get(identityKey);
  165. if (jsch == null) {
  166. jsch = new JSch();
  167. jsch.setHostKeyRepository(defaultJSch.getHostKeyRepository());
  168. jsch.addIdentity(identityKey);
  169. byIdentityFile.put(identityKey, jsch);
  170. }
  171. return jsch;
  172. }
  173. /**
  174. * @param fs
  175. * the file system abstraction which will be necessary to
  176. * perform certain file system operations.
  177. * @return the new default JSch implementation.
  178. * @throws JSchException
  179. * known host keys cannot be loaded.
  180. */
  181. protected JSch createDefaultJSch(FS fs) throws JSchException {
  182. final JSch jsch = new JSch();
  183. knownHosts(jsch, fs);
  184. identities(jsch, fs);
  185. return jsch;
  186. }
  187. private static void knownHosts(final JSch sch, FS fs) throws JSchException {
  188. final File home = fs.userHome();
  189. if (home == null)
  190. return;
  191. final File known_hosts = new File(new File(home, ".ssh"), "known_hosts");
  192. try {
  193. final FileInputStream in = new FileInputStream(known_hosts);
  194. try {
  195. sch.setKnownHosts(in);
  196. } finally {
  197. in.close();
  198. }
  199. } catch (FileNotFoundException none) {
  200. // Oh well. They don't have a known hosts in home.
  201. } catch (IOException err) {
  202. // Oh well. They don't have a known hosts in home.
  203. }
  204. }
  205. private static void identities(final JSch sch, FS fs) {
  206. final File home = fs.userHome();
  207. if (home == null)
  208. return;
  209. final File sshdir = new File(home, ".ssh");
  210. if (sshdir.isDirectory()) {
  211. loadIdentity(sch, new File(sshdir, "identity"));
  212. loadIdentity(sch, new File(sshdir, "id_rsa"));
  213. loadIdentity(sch, new File(sshdir, "id_dsa"));
  214. }
  215. }
  216. private static void loadIdentity(final JSch sch, final File priv) {
  217. if (priv.isFile()) {
  218. try {
  219. sch.addIdentity(priv.getAbsolutePath());
  220. } catch (JSchException e) {
  221. // Instead, pretend the key doesn't exist.
  222. }
  223. }
  224. }
  225. }