KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ssh > KnownHosts


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ssh;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.FileReader JavaDoc;
17 import java.io.FileWriter JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.math.BigInteger JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.osgi.util.NLS;
25
26 /**
27  * I represent a database of known hosts usually placed in ~/.ssh/known_hosts
28  * on Unix/Linux systems.
29  * Currently, only RSA keys are supported, as these are the only keys we
30  * have to deal with during SSH1 key exchange.
31  */

32 public class KnownHosts {
33
34     private String JavaDoc filename;
35     
36     public KnownHosts() {
37         this.filename = KnownHosts.defaultFilename();
38     }
39     
40     static String JavaDoc defaultFilename() {
41         if (!Platform.getOS().equals(Platform.OS_LINUX)) return internalFilename();
42         String JavaDoc HOME = System.getProperty("user.home"); //$NON-NLS-1$
43
if (HOME==null) return internalFilename();
44         return HOME+"/.ssh/known_hosts"; //$NON-NLS-1$
45
}
46
47     private static String JavaDoc internalFilename() {
48         return SSHPlugin.getPlugin().getStateLocation().append("known_hosts").toOSString(); //$NON-NLS-1$
49
}
50     
51     /**
52      * Verify if the public key for the specified host is known.
53      * If the public key matches, return true.
54      * If the key does not match, return false.
55      * If the key is not listed in <code>known_hosts</code>, or
56      * <code>known_hosts</code> does not exist, assume we are connecting
57      * to the authentic server, add the key, and return true.
58      * @param e key exponent
59      * @param n key modulus
60      * @return boolean whether the key is correct
61      */

62     public boolean verifyKey(String JavaDoc hostname, byte[] host_key_bits, BigInteger JavaDoc e, BigInteger JavaDoc n) {
63         FileReader JavaDoc f;
64         BigInteger JavaDoc nbits = new BigInteger JavaDoc(1, host_key_bits);
65         try {
66             f= new FileReader JavaDoc(filename);
67         } catch (FileNotFoundException JavaDoc ex) {
68             createHostFile();
69             addHost(hostname, nbits, e, n);
70             return true;
71         }
72         BufferedReader JavaDoc r = new BufferedReader JavaDoc(f);
73         try {
74             String JavaDoc line;
75             while ((line = r.readLine()) != null) {
76                 if (line.trim().length()==0) continue;
77                 if (line.startsWith("#")) continue; //$NON-NLS-1$
78
String JavaDoc[] tokens=subStrings(line);
79                 if (tokens.length==4 && Character.isDigit(tokens[1].charAt(0)) && tokens[0].equalsIgnoreCase(hostname)) {
80                     if (nbits.equals(new BigInteger JavaDoc(tokens[1])) && e.equals(new BigInteger JavaDoc(tokens[2])) && n.equals(new BigInteger JavaDoc(tokens[3]))) {
81                         f.close();
82                         return true;
83                     } else {
84                         f.close();
85                         return false;
86                     }
87                 }
88             }
89             f.close();
90             addHost(hostname, nbits, e, n);
91             return true;
92         } catch (IOException JavaDoc ex) {
93             SSHPlugin.log(IStatus.ERROR, CVSSSHMessages.KnownHosts_8, ex);
94             return false;
95         }
96     }
97     
98     /*
99      * Append the host key information to known_hosts.
100      * Always assume the file exists.
101      */

102     void addHost(String JavaDoc hostname, BigInteger JavaDoc key_bits, BigInteger JavaDoc e, BigInteger JavaDoc n) {
103         try {
104             FileWriter JavaDoc w = new FileWriter JavaDoc(defaultFilename(), true);
105             w.write(Character.LINE_SEPARATOR);
106             w.write(hostname + " " + key_bits.toString(10) + " " + e.toString(10) + " " + n.toString(10)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
107
w.close();
108             String JavaDoc message = NLS.bind(CVSSSHMessages.Client_addedHostKey, (new String JavaDoc[] {hostname, defaultFilename()}));
109             SSHPlugin.log(IStatus.INFO, message, null);
110         } catch (IOException JavaDoc ex) {
111             SSHPlugin.log(IStatus.ERROR, CVSSSHMessages.KnownHosts_9, ex);
112         }
113     }
114     
115     /*
116      * Create the known_hosts file in the default location.
117      * Fail if the file can not be created (issue a warning in the log).
118      */

119     void createHostFile() {
120         try {
121             File JavaDoc file = new File JavaDoc(defaultFilename());
122             // Ensure the parent directory exists
123
File JavaDoc parentDir = file.getParentFile();
124             parentDir.mkdirs();
125             // Create the file
126
file.createNewFile();
127         } catch (IOException JavaDoc ee) {
128             SSHPlugin.log(IStatus.ERROR, CVSSSHMessages.KnownHosts_10, ee);
129         }
130
131     }
132     private static String JavaDoc[] subStrings(String JavaDoc s) {
133         Vector JavaDoc v = subStringsVector(s);
134         String JavaDoc[] substrings = new String JavaDoc[v.size()];
135         v.copyInto(substrings);
136         return substrings;
137     }
138     private static Vector JavaDoc subStringsVector(String JavaDoc s) {
139         Vector JavaDoc v = new Vector JavaDoc();
140         s = s.trim();
141         if (s.length()==0) return v;
142         int first1 = s.indexOf(' ');
143         int first2 = s.indexOf('\t');
144         int first;
145         if ((first1==-1)&&(first2==-1)) first=-1;
146         else if ((first1!=-1)&&(first2!=-1)) first = Math.min(first1, first2);
147         else if (first1!=-1) first=first1; else first=first2;
148         if (first==-1) {
149             v.add(s);
150             return v;
151         }
152         v.add(s.substring(0,first));
153         v.addAll(subStringsVector(s.substring(first+1)));
154         return v;
155     }
156 }
157
Popular Tags