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:
030: import java.io.BufferedReader;
031: import java.io.IOException;
032: import java.io.InputStreamReader;
033:
034: /**
035: *
036: *
037: * @author $author$
038: * @version $Revision: 1.14 $
039: */
040: public class ConsoleHostKeyVerification extends
041: AbstractHostKeyVerification {
042: /**
043: * Creates a new ConsoleHostKeyVerification object.
044: *
045: * @throws InvalidHostFileException
046: */
047: public ConsoleHostKeyVerification() throws InvalidHostFileException {
048: super ();
049: }
050:
051: /**
052: * Creates a new ConsoleHostKeyVerification object.
053: *
054: * @param hostFile
055: *
056: * @throws InvalidHostFileException
057: */
058: public ConsoleHostKeyVerification(String hostFile)
059: throws InvalidHostFileException {
060: super (hostFile);
061: }
062:
063: /**
064: *
065: *
066: * @param hostname
067: */
068: public void onDeniedHost(String hostname) {
069: System.out.println("Access to the host " + hostname
070: + " is denied from this system");
071: }
072:
073: /**
074: *
075: *
076: * @param host
077: * @param fingerprint
078: * @param actual
079: */
080: public void onHostKeyMismatch(String host, String fingerprint,
081: String actual) {
082: try {
083: System.out.println("The host key supplied by " + host
084: + " is: " + actual);
085: System.out.println("The current allowed key for " + host
086: + " is: " + fingerprint);
087: getResponse(host, actual);
088: } catch (Exception e) {
089: e.printStackTrace();
090: }
091: }
092:
093: /**
094: *
095: *
096: * @param host
097: * @param fingerprint
098: */
099: public void onUnknownHost(String host, String fingerprint) {
100: try {
101: System.out.println("The host " + host
102: + " is currently unknown to the system");
103: System.out.println("The host key fingerprint is: "
104: + fingerprint);
105: getResponse(host, fingerprint);
106: } catch (Exception e) {
107: e.printStackTrace();
108: }
109: }
110:
111: private void getResponse(String host, String fingerprint)
112: throws InvalidHostFileException, IOException {
113: String response = "";
114: BufferedReader reader = new BufferedReader(
115: new InputStreamReader(System.in));
116:
117: while (!(response.equalsIgnoreCase("YES")
118: || response.equalsIgnoreCase("NO") || (response
119: .equalsIgnoreCase("ALWAYS") && isHostFileWriteable()))) {
120: String options = (isHostFileWriteable() ? "Yes|No|Always"
121: : "Yes|No");
122:
123: if (!isHostFileWriteable()) {
124: System.out
125: .println("Always option disabled, host file is not writeable");
126: }
127:
128: System.out.print("Do you want to allow this host key? ["
129: + options + "]: ");
130: response = reader.readLine();
131: }
132:
133: if (response.equalsIgnoreCase("YES")) {
134: allowHost(host, fingerprint, false);
135: }
136:
137: if (response.equalsIgnoreCase("ALWAYS")
138: && isHostFileWriteable()) {
139: allowHost(host, fingerprint, true);
140: }
141:
142: // Do nothing on NO
143: }
144: }
|