1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* Copyright (C) 2000-2002 i-scream |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
|
20 |
//---PACKAGE DECLARATION--- |
21 |
package uk.org.iscream.cms.server.filter; |
22 |
|
23 |
//---IMPORTS--- |
24 |
import uk.org.iscream.cms.server.util.*; |
25 |
import uk.org.iscream.cms.server.core.*; |
26 |
import uk.org.iscream.cms.server.componentmanager.*; |
27 |
import java.util.Random; |
28 |
import java.util.HashMap; |
29 |
|
30 |
/** |
31 |
* Acts as a store and checking mechanism for host keys. |
32 |
* |
33 |
* @author $Author: tdb $ |
34 |
* @version $Id: KeyManager.java,v 1.2 2002/03/22 14:22:58 tdb Exp $ |
35 |
*/ |
36 |
public class KeyManager { |
37 |
|
38 |
//---FINAL ATTRIBUTES--- |
39 |
|
40 |
/** |
41 |
* The current CVS revision of this class |
42 |
*/ |
43 |
public final String REVISION = "$Revision: 1.2 $"; |
44 |
|
45 |
/** |
46 |
* The set of characters to be used for our keys |
47 |
*/ |
48 |
private final char[] KEYSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); |
49 |
|
50 |
/** |
51 |
* Default key length |
52 |
*/ |
53 |
public static final int DEFKEYLEN = 15; |
54 |
|
55 |
//---STATIC METHODS--- |
56 |
|
57 |
/** |
58 |
* Return a reference to the single class. |
59 |
* Construct it if it does not already exist, otherwise just return the reference. |
60 |
*/ |
61 |
public synchronized static KeyManager getInstance() { |
62 |
if (_instance == null){ |
63 |
_instance = new KeyManager(); |
64 |
} |
65 |
return _instance; |
66 |
} |
67 |
|
68 |
//---CONSTRUCTORS--- |
69 |
|
70 |
/** |
71 |
* Private constructor to avoid external construction. |
72 |
*/ |
73 |
private KeyManager() { |
74 |
try { |
75 |
String keylen = ConfigurationProxy.getInstance().getProperty("Filter." + FilterMain.NAME, "Filter.KeyLength"); |
76 |
_keylen = Integer.parseInt(keylen); |
77 |
} |
78 |
catch (PropertyNotFoundException e) { |
79 |
_keylen = DEFKEYLEN; |
80 |
_logger.write(toString(), Logger.WARNING, "No key length found, using default of " +_keylen+ " : " + e); |
81 |
} |
82 |
catch (NumberFormatException e) { |
83 |
_keylen = DEFKEYLEN; |
84 |
_logger.write(toString(), Logger.WARNING, "Malformed key length found, using default of " +_keylen+ " : " + e); |
85 |
} |
86 |
} |
87 |
|
88 |
//---PUBLIC METHODS--- |
89 |
|
90 |
/** |
91 |
* Add or replace a key. |
92 |
* |
93 |
* @param host the hostname to add a key for |
94 |
* @param key the key to add |
95 |
*/ |
96 |
public synchronized void addKey(String host, String key) { |
97 |
_keys.put(host, key); |
98 |
} |
99 |
|
100 |
/** |
101 |
* Check if a given key is currently valid. |
102 |
* Will respond negatively if the key doesn't match, |
103 |
* or if no key is found (and EnforceHostAuth is on). |
104 |
* |
105 |
* @param host the hostname to check the key for |
106 |
* @param key the key to check |
107 |
* @return whether the check was successful |
108 |
*/ |
109 |
public synchronized boolean checkKey(String host, String key) { |
110 |
Object o = _keys.get(host); |
111 |
if(o != null) { |
112 |
// if our key isn't null, we want to make |
113 |
// sure it matches that given |
114 |
String ourKey = (String) o; |
115 |
return ourKey.equals(key); |
116 |
} |
117 |
else { |
118 |
// default to enforcing host auth |
119 |
boolean enforceHostAuth = true; |
120 |
try { |
121 |
// try to get see what's in the config |
122 |
String enforce = ConfigurationProxy.getInstance().getProperty("Filter." + FilterMain.NAME, "Filter.EnforceHostAuth"); |
123 |
enforceHostAuth = (Integer.parseInt(enforce) == 1); |
124 |
} |
125 |
catch(PropertyNotFoundException e) { |
126 |
// if it's not set, not enforced |
127 |
_logger.write(toString(), Logger.WARNING, "EnforceHostAuth property not found: " + e); |
128 |
enforceHostAuth = false; |
129 |
} |
130 |
catch(NumberFormatException e) { |
131 |
// if it's not a number, not enforced |
132 |
_logger.write(toString(), Logger.WARNING, "EnforceHostAuth property malformed: " + e); |
133 |
enforceHostAuth = false; |
134 |
} |
135 |
// if enforceHostAuth is true we want to fail the check |
136 |
// and vice-versa |
137 |
return !enforceHostAuth; |
138 |
} |
139 |
} |
140 |
|
141 |
/** |
142 |
* Generate a random key with length KEYLEN. |
143 |
* |
144 |
* @return a random key as a String |
145 |
*/ |
146 |
public String genKey() { |
147 |
Random r = new Random(); |
148 |
StringBuffer s = new StringBuffer(); |
149 |
for(int i=0; i < _keylen; i++) { |
150 |
s.append(KEYSET[r.nextInt(KEYSET.length)]); |
151 |
} |
152 |
return s.toString(); |
153 |
} |
154 |
|
155 |
/** |
156 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
157 |
* method to provide clean logging (every class should have this). |
158 |
* |
159 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
160 |
* to format the toString() |
161 |
* |
162 |
* @return the name of this class and its CVS revision |
163 |
*/ |
164 |
public String toString() { |
165 |
return FormatName.getName( |
166 |
_name, |
167 |
getClass().getName(), |
168 |
REVISION); |
169 |
} |
170 |
|
171 |
//---PRIVATE METHODS--- |
172 |
|
173 |
//---ACCESSOR/MUTATOR METHODS--- |
174 |
|
175 |
//---ATTRIBUTES--- |
176 |
|
177 |
/** |
178 |
* This holds the current keys for |
179 |
* the hosts we're authenticating |
180 |
*/ |
181 |
private HashMap _keys = new HashMap(); |
182 |
|
183 |
/** |
184 |
* The length of our keys |
185 |
*/ |
186 |
private int _keylen; |
187 |
|
188 |
/** |
189 |
* This holds a reference to the |
190 |
* system logger that is being used. |
191 |
*/ |
192 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
193 |
|
194 |
/** |
195 |
* This is the friendly identifier of the |
196 |
* component this class is running in. |
197 |
* eg, a Filter may be called "filter1", |
198 |
* If this class does not have an owning |
199 |
* component, a name from the configuration |
200 |
* can be placed here. This name could also |
201 |
* be changed to null for utility classes. |
202 |
*/ |
203 |
private String _name = FilterMain.NAME; |
204 |
|
205 |
//---STATIC ATTRIBUTES--- |
206 |
|
207 |
/** |
208 |
* A reference to the single instance of this class |
209 |
*/ |
210 |
private static KeyManager _instance; |
211 |
|
212 |
} |