Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.j2ssh.transport » [javadoc | source]
    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.j2ssh.transport;
   27   
   28   import com.sshtools.j2ssh.transport.publickey.SshPublicKey;
   29   
   30   import java.io.BufferedReader;
   31   import java.io.File;
   32   import java.io.IOException;
   33   import java.io.InputStreamReader;
   34   
   35   
   36   /**
   37    * <p>
   38    * Implements the <code>AbstractKnownHostsKeyVerification</code> to provide
   39    * host key verification through the console.
   40    * </p>
   41    *
   42    * @author Lee David Painter
   43    * @version $Revision: 1.14 $
   44    *
   45    * @since 0.2.0
   46    */
   47   public class ConsoleKnownHostsKeyVerification
   48       extends AbstractKnownHostsKeyVerification {
   49       /**
   50        * <p>
   51        * Constructs the verification instance with the default known_hosts file
   52        * from $HOME/.ssh/known_hosts.
   53        * </p>
   54        *
   55        * @throws InvalidHostFileException if the known_hosts file is invalid.
   56        *
   57        * @since 0.2.0
   58        */
   59       public ConsoleKnownHostsKeyVerification() throws InvalidHostFileException {
   60           super(new File(System.getProperty("user.home"),
   61                   ".ssh" + File.separator + "known_hosts").getAbsolutePath());
   62       }
   63   
   64       /**
   65        * <p>
   66        * Constructs the verification instance with the specified known_hosts
   67        * file.
   68        * </p>
   69        *
   70        * @param knownhosts the path to the known_hosts file
   71        *
   72        * @throws InvalidHostFileException if the known_hosts file is invalid.
   73        *
   74        * @since 0.2.0
   75        */
   76       public ConsoleKnownHostsKeyVerification(String knownhosts)
   77           throws InvalidHostFileException {
   78           super(knownhosts);
   79       }
   80   
   81       /**
   82        * <p>
   83        * Prompts the user through the console to verify the host key.
   84        * </p>
   85        *
   86        * @param host the name of the host
   87        * @param pk the current public key of the host
   88        * @param actual the actual public key supplied by the host
   89        *
   90        * @since 0.2.0
   91        */
   92       public void onHostKeyMismatch(String host, SshPublicKey pk,
   93           SshPublicKey actual) {
   94           try {
   95               System.out.println("The host key supplied by " + host + " is: " +
   96                   actual.getFingerprint());
   97               System.out.println("The current allowed key for " + host + " is: " +
   98                   pk.getFingerprint());
   99               getResponse(host, pk);
  100           } catch (Exception e) {
  101               e.printStackTrace();
  102           }
  103       }
  104   
  105       /**
  106        * <p>
  107        * Prompts the user through the console to verify the host key.
  108        * </p>
  109        *
  110        * @param host the name of the host
  111        * @param pk the public key supplied by the host
  112        *
  113        * @since 0.2.0
  114        */
  115       public void onUnknownHost(String host, SshPublicKey pk) {
  116           try {
  117               System.out.println("The host " + host +
  118                   " is currently unknown to the system");
  119               System.out.println("The host key fingerprint is: " +
  120                   pk.getFingerprint());
  121               getResponse(host, pk);
  122           } catch (Exception e) {
  123               e.printStackTrace();
  124           }
  125       }
  126   
  127       private void getResponse(String host, SshPublicKey pk)
  128           throws InvalidHostFileException, IOException {
  129           String response = "";
  130           BufferedReader reader = new BufferedReader(new InputStreamReader(
  131                       System.in));
  132   
  133           while (!(response.equalsIgnoreCase("YES") ||
  134                   response.equalsIgnoreCase("NO") ||
  135                   (response.equalsIgnoreCase("ALWAYS") && isHostFileWriteable()))) {
  136               String options = (isHostFileWriteable() ? "Yes|No|Always" : "Yes|No");
  137   
  138               if (!isHostFileWriteable()) {
  139                   System.out.println(
  140                       "Always option disabled, host file is not writeable");
  141               }
  142   
  143               System.out.print("Do you want to allow this host key? [" + options +
  144                   "]: ");
  145               response = reader.readLine();
  146           }
  147   
  148           if (response.equalsIgnoreCase("YES")) {
  149               allowHost(host, pk, false);
  150           }
  151   
  152           if (response.equalsIgnoreCase("NO")) {
  153               System.out.println("Cannot continue without a valid host key");
  154               System.exit(1);
  155           }
  156   
  157           if (response.equalsIgnoreCase("ALWAYS") && isHostFileWriteable()) {
  158               allowHost(host, pk, true);
  159           }
  160       }
  161   }

Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.j2ssh.transport » [javadoc | source]