Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / transport / DefaultSshSessionFactory.java
blob8a59904c66b4e0bce2b5596d0aa9a52bbbd2fb6a
1 /*
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.transport;
41 import java.awt.Container;
42 import java.awt.GridBagConstraints;
43 import java.awt.GridBagLayout;
44 import java.awt.Insets;
45 import java.io.File;
46 import java.io.FileInputStream;
47 import java.io.FileNotFoundException;
48 import java.io.IOException;
49 import java.security.AccessController;
50 import java.security.PrivilegedAction;
52 import javax.swing.JLabel;
53 import javax.swing.JOptionPane;
54 import javax.swing.JPanel;
55 import javax.swing.JPasswordField;
56 import javax.swing.JTextField;
58 import org.spearce.jgit.util.FS;
60 import com.jcraft.jsch.JSch;
61 import com.jcraft.jsch.JSchException;
62 import com.jcraft.jsch.Session;
63 import com.jcraft.jsch.UIKeyboardInteractive;
64 import com.jcraft.jsch.UserInfo;
66 /**
67 * Loads known hosts and private keys from <code>$HOME/.ssh</code>.
68 * <p>
69 * This is the default implementation used by JGit and provides most of the
70 * compatibility necessary to match OpenSSH, a popular implementation of SSH
71 * used by C Git.
72 * <p>
73 * If user interactivity is required by SSH (e.g. to obtain a password) AWT is
74 * used to display a password input field to the end-user.
76 class DefaultSshSessionFactory extends SshSessionFactory {
77 /** IANA assigned port number for SSH. */
78 private static final int SSH_PORT = 22;
80 private JSch userJSch;
82 @Override
83 public synchronized Session getSession(String user, String pass,
84 String host, int port) throws JSchException {
85 if (port <= 0)
86 port = SSH_PORT;
87 if (user == null)
88 user = userName();
90 final Session session = getUserJSch().getSession(user, host, port);
91 if (pass != null)
92 session.setPassword(pass);
93 else
94 session.setUserInfo(new AWT_UserInfo());
95 return session;
98 private static String userName() {
99 return AccessController.doPrivileged(new PrivilegedAction<String>() {
100 public String run() {
101 return System.getProperty("user.name");
106 private JSch getUserJSch() throws JSchException {
107 if (userJSch == null) {
108 final JSch sch = new JSch();
109 knownHosts(sch);
110 identities(sch);
111 userJSch = sch;
113 return userJSch;
116 private void knownHosts(final JSch sch) throws JSchException {
117 final File home = FS.userHome();
118 if (home == null)
119 return;
120 final File known_hosts = new File(new File(home, ".ssh"), "known_hosts");
121 try {
122 final FileInputStream in = new FileInputStream(known_hosts);
123 try {
124 sch.setKnownHosts(in);
125 } finally {
126 in.close();
128 } catch (FileNotFoundException none) {
129 // Oh well. They don't have a known hosts in home.
130 } catch (IOException err) {
131 // Oh well. They don't have a known hosts in home.
135 private void identities(final JSch sch) throws JSchException {
136 final File home = FS.userHome();
137 if (home == null)
138 return;
139 final File sshdir = new File(home, ".ssh");
140 final File[] keys = sshdir.listFiles();
141 if (keys == null)
142 return;
143 for (int i = 0; i < keys.length; i++) {
144 final File pk = keys[i];
145 final String n = pk.getName();
146 if (!n.endsWith(".pub"))
147 continue;
148 final File k = new File(sshdir, n.substring(0, n.length() - 4));
149 if (!k.isFile())
150 continue;
151 sch.addIdentity(k.getAbsolutePath());
155 private static class AWT_UserInfo implements UserInfo,
156 UIKeyboardInteractive {
157 private String passwd;
159 private String passphrase;
161 public void showMessage(final String msg) {
162 JOptionPane.showMessageDialog(null, msg);
165 public boolean promptYesNo(final String msg) {
166 return JOptionPane.showConfirmDialog(null, msg, "Warning",
167 JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
170 public boolean promptPassword(final String msg) {
171 passwd = null;
172 final JPasswordField passwordField = new JPasswordField(20);
173 final int result = JOptionPane.showConfirmDialog(null,
174 new Object[] { passwordField }, msg,
175 JOptionPane.OK_CANCEL_OPTION);
176 if (result == JOptionPane.OK_OPTION) {
177 passwd = new String(passwordField.getPassword());
178 return true;
180 return false;
183 public boolean promptPassphrase(final String msg) {
184 passphrase = null;
185 final JPasswordField passwordField = new JPasswordField(20);
186 final int result = JOptionPane.showConfirmDialog(null,
187 new Object[] { passwordField }, msg,
188 JOptionPane.OK_CANCEL_OPTION);
189 if (result == JOptionPane.OK_OPTION) {
190 passphrase = new String(passwordField.getPassword());
191 return true;
193 return false;
196 public String getPassword() {
197 return passwd;
200 public String getPassphrase() {
201 return passphrase;
204 public String[] promptKeyboardInteractive(final String destination,
205 final String name, final String instruction,
206 final String[] prompt, final boolean[] echo) {
207 final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1,
208 1, 1, GridBagConstraints.NORTHWEST,
209 GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
210 final Container panel = new JPanel();
211 panel.setLayout(new GridBagLayout());
213 gbc.weightx = 1.0;
214 gbc.gridwidth = GridBagConstraints.REMAINDER;
215 gbc.gridx = 0;
216 panel.add(new JLabel(instruction), gbc);
217 gbc.gridy++;
219 gbc.gridwidth = GridBagConstraints.RELATIVE;
221 final JTextField[] texts = new JTextField[prompt.length];
222 for (int i = 0; i < prompt.length; i++) {
223 gbc.fill = GridBagConstraints.NONE;
224 gbc.gridx = 0;
225 gbc.weightx = 1;
226 panel.add(new JLabel(prompt[i]), gbc);
228 gbc.gridx = 1;
229 gbc.fill = GridBagConstraints.HORIZONTAL;
230 gbc.weighty = 1;
231 if (echo[i]) {
232 texts[i] = new JTextField(20);
233 } else {
234 texts[i] = new JPasswordField(20);
236 panel.add(texts[i], gbc);
237 gbc.gridy++;
240 if (JOptionPane.showConfirmDialog(null, panel, destination + ": "
241 + name, JOptionPane.OK_CANCEL_OPTION,
242 JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
243 String[] response = new String[prompt.length];
244 for (int i = 0; i < prompt.length; i++) {
245 response[i] = texts[i].getText();
247 return response;
249 return null; // cancel