001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.hosts;
027:
028: import com.sshtools.j2ssh.transport.InvalidHostFileException;
029: import com.sshtools.j2ssh.transport.TransportProtocolException;
030:
031: import java.awt.Component;
032:
033: import java.lang.reflect.InvocationTargetException;
034:
035: import javax.swing.JOptionPane;
036: import javax.swing.SwingUtilities;
037:
038: /**
039: *
040: *
041: * @author $author$
042: * @version $Revision: 1.13 $
043: */
044: public class DialogHostKeyVerification extends
045: AbstractHostKeyVerification {
046: Component parent;
047: private boolean verificationEnabled = true;
048:
049: /**
050: * Creates a new DialogHostKeyVerification object.
051: *
052: * @param parent
053: *
054: * @throws InvalidHostFileException
055: */
056: public DialogHostKeyVerification(Component parent)
057: throws InvalidHostFileException {
058: this .parent = parent;
059: }
060:
061: /**
062: * Creates a new DialogHostKeyVerification object.
063: *
064: * @param parent
065: * @param hostFileName
066: *
067: * @throws InvalidHostFileException
068: */
069: public DialogHostKeyVerification(Component parent,
070: String hostFileName) throws InvalidHostFileException {
071: super (hostFileName);
072: this .parent = parent;
073: }
074:
075: /**
076: *
077: *
078: * @param enabled
079: */
080: public void setVerificationEnabled(boolean enabled) {
081: this .verificationEnabled = verificationEnabled;
082: }
083:
084: /**
085: *
086: *
087: * @param host
088: *
089: * @throws TransportProtocolException
090: */
091: public void onDeniedHost(final String host)
092: throws TransportProtocolException {
093: // Show a message to the user to inform them that the host
094: // is denied
095: try {
096: if (verificationEnabled) {
097: SwingUtilities.invokeAndWait(new Runnable() {
098: public void run() {
099: JOptionPane
100: .showMessageDialog(
101: parent,
102: "Access to '"
103: + host
104: + "' is denied.\n"
105: + "Verify the access granted/denied in the allowed hosts file.",
106: "Remote Host Authentication",
107: JOptionPane.ERROR_MESSAGE);
108: }
109: });
110: }
111: } catch (InvocationTargetException ite) {
112: throw new TransportProtocolException(
113: "Invocation Exception: " + ite.getMessage());
114: } catch (InterruptedException ie) {
115: throw new TransportProtocolException(
116: "SwingUtilities thread interrupted!");
117: }
118: }
119:
120: /**
121: *
122: *
123: * @param host
124: * @param recordedFingerprint
125: * @param actualFingerprint
126: *
127: * @throws TransportProtocolException
128: */
129: public void onHostKeyMismatch(final String host,
130: final String recordedFingerprint,
131: final String actualFingerprint)
132: throws TransportProtocolException {
133: try {
134: if (verificationEnabled) {
135: SwingUtilities.invokeAndWait(new Runnable() {
136: public void run() {
137: Object[] options = getOptions();
138: int res = JOptionPane
139: .showOptionDialog(
140: parent,
141: "The host '"
142: + host
143: + "' has provided a different host key.\nThe host key"
144: + " fingerprint provided is '"
145: + actualFingerprint
146: + "'.\n"
147: + "The allowed host key fingerprint is "
148: + recordedFingerprint
149: + ".\nDo you want to allow this host?",
150: "Remote host authentication",
151: JOptionPane.YES_NO_CANCEL_OPTION,
152: JOptionPane.QUESTION_MESSAGE,
153: null, options, options[0]);
154:
155: try {
156: // Handle the reply
157: if ((options.length == 3) && (res == 0)) {
158: // Always allow the host with the new fingerprint
159: allowHost(host, actualFingerprint, true);
160: } else if (((options.length == 2) && (res == 0))
161: || ((options.length == 3) && (res == 1))) {
162: // Only allow the host this once
163: allowHost(host, actualFingerprint,
164: false);
165: }
166: } catch (InvalidHostFileException e) {
167: showExceptionMessage(e);
168: }
169: }
170: });
171: }
172: } catch (InvocationTargetException ite) {
173: throw new TransportProtocolException(
174: "Invocation Exception: " + ite.getMessage());
175: } catch (InterruptedException ie) {
176: throw new TransportProtocolException(
177: "SwingUtilities thread interrupted!");
178: }
179: }
180:
181: /**
182: *
183: *
184: * @param host
185: * @param fingerprint
186: *
187: * @throws TransportProtocolException
188: */
189: public void onUnknownHost(final String host,
190: final String fingerprint) throws TransportProtocolException {
191: // Set up the users options. Only allow always if we can
192: // write to the hosts file
193: try {
194: if (verificationEnabled) {
195: SwingUtilities.invokeAndWait(new Runnable() {
196: public void run() {
197: Object[] options = getOptions();
198: int res = JOptionPane
199: .showOptionDialog(
200: parent,
201: "The host '"
202: + host
203: + "' is unknown. The host key"
204: + " fingerprint is\n'"
205: + fingerprint
206: + "'.\nDo you want to allow this host?",
207: "Remote host authentication",
208: JOptionPane.YES_NO_CANCEL_OPTION,
209: JOptionPane.QUESTION_MESSAGE,
210: null, options, options[0]);
211:
212: try {
213: // Handle the reply
214: if ((options.length == 3) && (res == 0)) {
215: // Always allow the host with the new fingerprint
216: allowHost(host, fingerprint, true);
217: } else if (((options.length == 2) && (res == 0))
218: || ((options.length == 3) && (res == 1))) {
219: // Only allow the host this once
220: allowHost(host, fingerprint, false);
221: }
222: } catch (InvalidHostFileException e) {
223: showExceptionMessage(e);
224: }
225: }
226: });
227: }
228: } catch (InvocationTargetException ite) {
229: throw new TransportProtocolException(
230: "Invocation Exception: " + ite.getMessage());
231: } catch (InterruptedException ie) {
232: throw new TransportProtocolException(
233: "SwingUtilities thread interrupted!");
234: }
235: }
236:
237: private String[] getOptions() {
238: return isHostFileWriteable() ? new String[] { "Always", "Yes",
239: "No" } : new String[] { "Yes", "No" };
240: }
241:
242: private void showExceptionMessage(Exception e) {
243: JOptionPane.showMessageDialog(parent,
244: "An unexpected error occured!\n\n" + e.getMessage(),
245: "Host Verification", JOptionPane.ERROR_MESSAGE);
246: }
247: }
|