/** * $Id: SshCallbackAdapter.java 3 2005-06-11 14:50:40Z elkner $ */ package com.jcraft.jsch; import java.awt.Component; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.io.File; import java.io.IOException; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import org.apache.log4j.Logger; /** * A GUI based SshCallback implementation. * * @author Jens Elkner * @version $Revision: 3 $ */ public class SshCallbackAdapter implements SshCallback { private static Logger log = Logger.getLogger(SshCallbackAdapter.class.getName()); Component parent; /** * A new call back, which has no parent component, used display dialogs. */ public SshCallbackAdapter() { this(null); } /** * A call back, uses the given parent component to display dialogs. * @param parent parent component to use for anchoring dialogs. Might be * null. */ public SshCallbackAdapter(Component parent) { this.parent = parent; } /** * {@inheritDoc} */ public boolean createFile(String name) { if (name == null) { return false; } File goo = new File(name); if (goo.isDirectory()) { JOptionPane.showMessageDialog(parent, "'" + name + "' is a directory!", "error", JOptionPane.ERROR_MESSAGE); return false; } if (goo.exists()) { if (goo.canWrite()) { return true; } JOptionPane.showMessageDialog(parent, "File '" + name + "' is not writable"); return false; } int res = JOptionPane.showConfirmDialog(parent, "The file '" + name + "' does not exist.\n" + "Are you sure you want to create it?", "Create File", JOptionPane.YES_NO_OPTION); if (res != JOptionPane.YES_OPTION) { return false; } File pgoo = goo.getParentFile(); if (pgoo != null && !pgoo.exists()) { res = JOptionPane.showConfirmDialog(parent, "The parent directory '" + pgoo + "' does not exist.\n" + "Are you sure you want to create it?", "Create directory", JOptionPane.YES_NO_OPTION); if (res != JOptionPane.YES_OPTION) { return false; } } boolean ok = false; String error = null; try { ok = goo.createNewFile(); } catch (IOException e) { error = e.getLocalizedMessage(); log.warn(error); if (log.isDebugEnabled()) { log.debug("createFile", e); } } if (ok) { JOptionPane.showMessageDialog(parent, goo.getName() + " has been succesfully created.\n" + "Please check its access permission.", "File created", JOptionPane.OK_OPTION); } else { JOptionPane.showMessageDialog(parent, goo.getName() + " has not been created: " + error, "Error", JOptionPane.OK_OPTION); return false; } return true; } /** * {@inheritDoc} */ public void showInfoMessage(String message) { JOptionPane.showConfirmDialog(parent, message, "Info", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE); } /** * {@inheritDoc} */ public char[] getPassphrase(String keyStore) { JPasswordField pwField = new JPasswordField(); Object[] ob = { pwField }; int result = JOptionPane.showConfirmDialog(null, ob, "Passphrase for " + keyStore, JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { return pwField.getPassword(); } return null; } /** * {@inheritDoc} */ @SuppressWarnings("all") public String[] getAuthenticationResponse(String name, String instruction, String[] prompt, boolean[] echo) throws UnsupportedAuthenticationException { final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0); Container panel = new JPanel(); panel.setLayout(new GridBagLayout()); gbc.weightx = 1.0; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.gridx = 0; panel.add(new JLabel(instruction), gbc); gbc.gridy++ ; gbc.gridwidth = GridBagConstraints.RELATIVE; JTextField[] texts = new JTextField[prompt.length]; for (int i = 0; i < prompt.length; i++ ) { gbc.fill = GridBagConstraints.NONE; gbc.gridx = 0; gbc.weightx = 1; panel.add(new JLabel(prompt[i]), gbc); gbc.gridx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.weighty = 1; if (echo[i]) { texts[i] = new JTextField(20); } else { texts[i] = new JPasswordField(20); } panel.add(texts[i], gbc); gbc.gridy++ ; } int res = JOptionPane.showConfirmDialog(null, panel, name, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (res == JOptionPane.OK_OPTION) { String[] response = new String[prompt.length]; for (int i = 0; i < prompt.length; i++ ) { response[i] = texts[i].getText(); } return response; } return null; } /** * {@inheritDoc} */ @SuppressWarnings("all") public char[] getPassword(String username, String host, int port) throws UnsupportedAuthenticationException { JPasswordField passwordField = new JPasswordField(); Object[] ob = { passwordField }; int result = JOptionPane.showConfirmDialog(null, ob, "Password for " + username + "@" + host + ":" + port, JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { return passwordField.getPassword(); } return null; } /** * {@inheritDoc} */ public boolean hostKeyValidationFailed(String host, String keyType, String fingerprint) { int res = JOptionPane.showConfirmDialog(parent, "The authenticity of host '" + host + "' can't be established.\n" + keyType + " key fingerprint is " + fingerprint + ".\n" + "Are you sure you want to continue connecting?", "Invalid or Unknown Host Key!", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); return res == JOptionPane.YES_OPTION; } /** * {@inheritDoc} */ public void hostKeyChanged(String keyType, String fingerprint, String knownHostsFile) { String message = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" + "@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @\n" + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" + "IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! \n" + "Someone could be eavesdropping on you right now (man-in-the" + "-middle attack)!\n" + "It is also possible that the " + keyType + " host key has just been changed.\n" + "The fingerprint for the " + keyType + " key sent by the remote host is\n" + fingerprint + ".\n" + "Please contact your system administrator.\n" + "Add correct host key in " + knownHostsFile + " to get rid of this message."; JOptionPane.showConfirmDialog(parent, message, "Host Key Changed!", JOptionPane.OK_OPTION, JOptionPane.WARNING_MESSAGE); } }