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