2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
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
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
;
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
;
67 * Loads known hosts and private keys from <code>$HOME/.ssh</code>.
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
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
;
83 public synchronized Session
getSession(String user
, String pass
,
84 String host
, int port
) throws JSchException
{
90 final Session session
= getUserJSch().getSession(user
, host
, port
);
92 session
.setPassword(pass
);
94 session
.setUserInfo(new AWT_UserInfo());
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();
116 private void knownHosts(final JSch sch
) throws JSchException
{
117 final File home
= FS
.userHome();
120 final File known_hosts
= new File(new File(home
, ".ssh"), "known_hosts");
122 final FileInputStream in
= new FileInputStream(known_hosts
);
124 sch
.setKnownHosts(in
);
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();
139 final File sshdir
= new File(home
, ".ssh");
140 final File
[] keys
= sshdir
.listFiles();
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"))
148 final File k
= new File(sshdir
, n
.substring(0, n
.length() - 4));
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
) {
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());
183 public boolean promptPassphrase(final String msg
) {
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());
196 public String
getPassword() {
200 public String
getPassphrase() {
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());
214 gbc
.gridwidth
= GridBagConstraints
.REMAINDER
;
216 panel
.add(new JLabel(instruction
), gbc
);
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
;
226 panel
.add(new JLabel(prompt
[i
]), gbc
);
229 gbc
.fill
= GridBagConstraints
.HORIZONTAL
;
232 texts
[i
] = new JTextField(20);
234 texts
[i
] = new JPasswordField(20);
236 panel
.add(texts
[i
], gbc
);
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();
249 return null; // cancel