source: other-projects/trunk/gs3-release-maker/tasks/sshtaskdef/src/mindbright/ssh/SSHRSAPublicKeyFile.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 4.9 KB
Line 
1/******************************************************************************
2 *
3 * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
4 * www.mindbright.se, [email protected]
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 *****************************************************************************
17 * $Author: mats $
18 * $Date: 2000/02/12 11:37:55 $
19 * $Name: rel1-2-1 $
20 *****************************************************************************/
21package mindbright.ssh;
22
23import java.io.*;
24import java.util.Vector;
25import java.util.Enumeration;
26import java.math.BigInteger;
27
28import mindbright.security.*;
29
30public class SSHRSAPublicKeyFile {
31
32 Vector pubKeyList;
33
34 public SSHRSAPublicKeyFile(InputStream fileIn, String name, boolean hostFile) throws IOException {
35 BufferedReader reader = new BufferedReader(new InputStreamReader(fileIn));
36 String row;
37
38 pubKeyList = new Vector();
39
40 while((row = reader.readLine()) != null) {
41 row = row.trim();
42 if(row.equals("") || row.charAt(0) == '#') // Skip comment-lines and empty lines...
43 continue;
44 String opts;
45 if(hostFile) {
46 // If we are reading a 'known_hosts' file we know that first token of line is the host-addr.
47 // in this case we store the host-addr in the opts-field of the SSHRSAPublicKeyString
48 //
49 int i = row.indexOf(' ');
50 opts = row.substring(0, i);
51 row = row.substring(i);
52 } else {
53 opts = ""; // !!! Read options from start of line, we don't support options for now...
54 }
55 try {
56 SSHRSAPublicKeyString pubKey = SSHRSAPublicKeyString.createKey(opts, row);
57 pubKeyList.addElement(pubKey);
58 } catch (Exception e) {
59 throw new IOException("Corrupt public keys file: " + name);
60 }
61 }
62 }
63
64 public static SSHRSAPublicKeyFile loadFromFile(String name, boolean hostFile) throws IOException {
65 FileInputStream fileIn = new FileInputStream(name);
66 SSHRSAPublicKeyFile keyFile = new SSHRSAPublicKeyFile(fileIn, name, hostFile);
67 fileIn.close();
68 return keyFile;
69 }
70
71 public void saveToFile(String fileName) throws IOException {
72 FileWriter fileOut = new FileWriter(fileName);
73 BufferedWriter writer = new BufferedWriter(fileOut);
74 SSHRSAPublicKeyString pk = null;
75 Enumeration elmts = elements();
76 String row;
77
78 try {
79 while(elmts.hasMoreElements()) {
80 pk = (SSHRSAPublicKeyString) elmts.nextElement();
81 row = pk.toString();
82 writer.write(row, 0, row.length());
83 writer.newLine();
84 }
85 } catch (Exception e) {
86 throw new IOException("Error while writing public-keys-file: " + fileName);
87 }
88 writer.flush();
89 writer.close();
90 fileOut.close();
91 }
92
93 public Enumeration elements() {
94 return pubKeyList.elements();
95 }
96
97 public RSAPublicKey getPublic(BigInteger n, String user) {
98 SSHRSAPublicKeyString pk = null;
99
100 Enumeration e = pubKeyList.elements();
101 while(e.hasMoreElements()) {
102 pk = (SSHRSAPublicKeyString) e.nextElement();
103 if(pk.getN().equals(n))
104 break;
105 pk = null;
106 }
107
108 return pk;
109 }
110
111 public int checkPublic(BigInteger n, String host) {
112 SSHRSAPublicKeyString pk = null;
113 int hostCheck = SSH.SRV_HOSTKEY_NEW;
114
115 Enumeration e = pubKeyList.elements();
116 while(e.hasMoreElements()) {
117 pk = (SSHRSAPublicKeyString) e.nextElement();
118 if(pk.getOpts().equals(host)) {
119 if(pk.getN().equals(n)) {
120 hostCheck = SSH.SRV_HOSTKEY_KNOWN;
121 } else {
122 hostCheck = SSH.SRV_HOSTKEY_CHANGED;
123 }
124 break;
125 }
126 }
127 return hostCheck;
128 }
129
130 public void addPublic(String opts, String user, BigInteger e, BigInteger n) {
131 SSHRSAPublicKeyString pubKey = new SSHRSAPublicKeyString(opts, user, e, n);
132 pubKeyList.addElement(pubKey);
133 }
134
135 public void removePublic(String host) {
136 SSHRSAPublicKeyString pk = null;
137
138 Enumeration e = pubKeyList.elements();
139 while(e.hasMoreElements()) {
140 pk = (SSHRSAPublicKeyString) e.nextElement();
141 if(pk.getOpts().equals(host)) {
142 pubKeyList.removeElement(pk);
143 break;
144 }
145 }
146 }
147
148 /* !!! DEBUG
149 public static void main(String[] argv) {
150 SSHRSAPublicKeyFile file = null;
151
152 try {
153 file = new SSHRSAPublicKeyFile("/home/mats/.ssh/known_hosts", true);
154
155 SSHRSAPublicKeyString pk = null;
156
157 Enumeration e = file.elements();
158 while(e.hasMoreElements()) {
159 pk = (SSHRSAPublicKeyString) e.nextElement();
160 System.out.println(pk);
161 }
162 } catch (Exception e) {
163 e.printStackTrace();
164 System.out.println("Error: " + e.toString());
165 }
166 }
167 */
168
169}
170
171
Note: See TracBrowser for help on using the repository browser.