1 11 package org.eclipse.team.internal.ccvs.ssh; 12 13 import java.io.BufferedReader ; 14 import java.io.File ; 15 import java.io.FileNotFoundException ; 16 import java.io.FileReader ; 17 import java.io.FileWriter ; 18 import java.io.IOException ; 19 import java.math.BigInteger ; 20 import java.util.Vector ; 21 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.Platform; 24 import org.eclipse.osgi.util.NLS; 25 26 32 public class KnownHosts { 33 34 private String filename; 35 36 public KnownHosts() { 37 this.filename = KnownHosts.defaultFilename(); 38 } 39 40 static String defaultFilename() { 41 if (!Platform.getOS().equals(Platform.OS_LINUX)) return internalFilename(); 42 String HOME = System.getProperty("user.home"); if (HOME==null) return internalFilename(); 44 return HOME+"/.ssh/known_hosts"; } 46 47 private static String internalFilename() { 48 return SSHPlugin.getPlugin().getStateLocation().append("known_hosts").toOSString(); } 50 51 62 public boolean verifyKey(String hostname, byte[] host_key_bits, BigInteger e, BigInteger n) { 63 FileReader f; 64 BigInteger nbits = new BigInteger (1, host_key_bits); 65 try { 66 f= new FileReader (filename); 67 } catch (FileNotFoundException ex) { 68 createHostFile(); 69 addHost(hostname, nbits, e, n); 70 return true; 71 } 72 BufferedReader r = new BufferedReader (f); 73 try { 74 String line; 75 while ((line = r.readLine()) != null) { 76 if (line.trim().length()==0) continue; 77 if (line.startsWith("#")) continue; String [] tokens=subStrings(line); 79 if (tokens.length==4 && Character.isDigit(tokens[1].charAt(0)) && tokens[0].equalsIgnoreCase(hostname)) { 80 if (nbits.equals(new BigInteger (tokens[1])) && e.equals(new BigInteger (tokens[2])) && n.equals(new BigInteger (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 ex) { 93 SSHPlugin.log(IStatus.ERROR, CVSSSHMessages.KnownHosts_8, ex); 94 return false; 95 } 96 } 97 98 102 void addHost(String hostname, BigInteger key_bits, BigInteger e, BigInteger n) { 103 try { 104 FileWriter w = new FileWriter (defaultFilename(), true); 105 w.write(Character.LINE_SEPARATOR); 106 w.write(hostname + " " + key_bits.toString(10) + " " + e.toString(10) + " " + n.toString(10)); w.close(); 108 String message = NLS.bind(CVSSSHMessages.Client_addedHostKey, (new String [] {hostname, defaultFilename()})); 109 SSHPlugin.log(IStatus.INFO, message, null); 110 } catch (IOException ex) { 111 SSHPlugin.log(IStatus.ERROR, CVSSSHMessages.KnownHosts_9, ex); 112 } 113 } 114 115 119 void createHostFile() { 120 try { 121 File file = new File (defaultFilename()); 122 File parentDir = file.getParentFile(); 124 parentDir.mkdirs(); 125 file.createNewFile(); 127 } catch (IOException ee) { 128 SSHPlugin.log(IStatus.ERROR, CVSSSHMessages.KnownHosts_10, ee); 129 } 130 131 } 132 private static String [] subStrings(String s) { 133 Vector v = subStringsVector(s); 134 String [] substrings = new String [v.size()]; 135 v.copyInto(substrings); 136 return substrings; 137 } 138 private static Vector subStringsVector(String s) { 139 Vector v = new Vector (); 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 |