1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 package com.sshtools.common.hosts;
27
28 import com.sshtools.j2ssh.transport.InvalidHostFileException;
29
30 import java.io.BufferedReader;
31 import java.io.IOException;
32 import java.io.InputStreamReader;
33
34
35 /**
36 *
37 *
38 * @author $author$
39 * @version $Revision: 1.14 $
40 */
41 public class ConsoleHostKeyVerification extends AbstractHostKeyVerification {
42 /**
43 * Creates a new ConsoleHostKeyVerification object.
44 *
45 * @throws InvalidHostFileException
46 */
47 public ConsoleHostKeyVerification() throws InvalidHostFileException {
48 super();
49 }
50
51 /**
52 * Creates a new ConsoleHostKeyVerification object.
53 *
54 * @param hostFile
55 *
56 * @throws InvalidHostFileException
57 */
58 public ConsoleHostKeyVerification(String hostFile)
59 throws InvalidHostFileException {
60 super(hostFile);
61 }
62
63 /**
64 *
65 *
66 * @param hostname
67 */
68 public void onDeniedHost(String hostname) {
69 System.out.println("Access to the host " + hostname +
70 " is denied from this system");
71 }
72
73 /**
74 *
75 *
76 * @param host
77 * @param fingerprint
78 * @param actual
79 */
80 public void onHostKeyMismatch(String host, String fingerprint, String actual) {
81 try {
82 System.out.println("The host key supplied by " + host + " is: " +
83 actual);
84 System.out.println("The current allowed key for " + host + " is: " +
85 fingerprint);
86 getResponse(host, actual);
87 } catch (Exception e) {
88 e.printStackTrace();
89 }
90 }
91
92 /**
93 *
94 *
95 * @param host
96 * @param fingerprint
97 */
98 public void onUnknownHost(String host, String fingerprint) {
99 try {
100 System.out.println("The host " + host +
101 " is currently unknown to the system");
102 System.out.println("The host key fingerprint is: " + fingerprint);
103 getResponse(host, fingerprint);
104 } catch (Exception e) {
105 e.printStackTrace();
106 }
107 }
108
109 private void getResponse(String host, String fingerprint)
110 throws InvalidHostFileException, IOException {
111 String response = "";
112 BufferedReader reader = new BufferedReader(new InputStreamReader(
113 System.in));
114
115 while (!(response.equalsIgnoreCase("YES") ||
116 response.equalsIgnoreCase("NO") ||
117 (response.equalsIgnoreCase("ALWAYS") && isHostFileWriteable()))) {
118 String options = (isHostFileWriteable() ? "Yes|No|Always" : "Yes|No");
119
120 if (!isHostFileWriteable()) {
121 System.out.println(
122 "Always option disabled, host file is not writeable");
123 }
124
125 System.out.print("Do you want to allow this host key? [" + options +
126 "]: ");
127 response = reader.readLine();
128 }
129
130 if (response.equalsIgnoreCase("YES")) {
131 allowHost(host, fingerprint, false);
132 }
133
134 if (response.equalsIgnoreCase("ALWAYS") && isHostFileWriteable()) {
135 allowHost(host, fingerprint, true);
136 }
137
138 // Do nothing on NO
139 }
140 }