4 |
|
//---IMPORTS--- |
5 |
|
import uk.org.iscream.cms.server.util.*; |
6 |
|
import java.util.ArrayList; |
7 |
+ |
import java.util.StringTokenizer; |
8 |
|
import java.net.InetAddress; |
9 |
|
import java.io.Serializable; |
10 |
|
|
76 |
|
* param expression what this rule matches using wildcards |
77 |
|
*/ |
78 |
|
public void add(boolean allow, String expression) { |
79 |
< |
_acl.add(new ACLRule(allow, expression)); |
79 |
> |
// default to expecting it to be an IP |
80 |
> |
// we will try to disprove this :) |
81 |
> |
boolean ip = true; |
82 |
> |
short[] ipaddr = {-1, -1, -1, -1}; |
83 |
> |
int i = 0; |
84 |
> |
String s = ""; |
85 |
> |
// tokenize the expression on fullstops, so we can break |
86 |
> |
// up the quads of an IP (if it's an IP!) |
87 |
> |
StringTokenizer st = new StringTokenizer(expression, "."); |
88 |
> |
while(st.hasMoreTokens() && i++ < 4) { |
89 |
> |
s = st.nextToken(); |
90 |
> |
// if it's a wildcard, we'll skip to the next one |
91 |
> |
// as no more checks are required |
92 |
> |
if(s.equals("*")) { |
93 |
> |
continue; |
94 |
> |
} |
95 |
> |
// attempt to parse it into a short |
96 |
> |
try { |
97 |
> |
short n = Short.parseShort(s); |
98 |
> |
// if it's an int but outside of the |
99 |
> |
// valid range, it can't be an IP |
100 |
> |
if(n < 0 || n > 255) { |
101 |
> |
ip = false; |
102 |
> |
// give up checking further |
103 |
> |
break; |
104 |
> |
} |
105 |
> |
ipaddr[i-1] = n; |
106 |
> |
} |
107 |
> |
// if it didn't parse as an int it can't be an IP |
108 |
> |
catch (NumberFormatException e) { |
109 |
> |
ip = false; |
110 |
> |
// give up checking further |
111 |
> |
break; |
112 |
> |
} |
113 |
> |
} |
114 |
> |
// we've done 4 parts, so if there's any |
115 |
> |
// more this can't be an IP |
116 |
> |
if(st.hasMoreTokens()) { |
117 |
> |
ip = false; |
118 |
> |
} |
119 |
> |
// if we've done less than 4, see if the last one |
120 |
> |
// was a wildcard - if it isn't then it's not an IP |
121 |
> |
// -- this allows 129.12.* |
122 |
> |
if(i < 4 && !s.equals("*")) { |
123 |
> |
ip = false; |
124 |
> |
} |
125 |
> |
// if we had one or less entries it can't be an IP |
126 |
> |
// -- this disallows * matching as an IP due |
127 |
> |
// to the rule above |
128 |
> |
if(i <= 1) { |
129 |
> |
ip = false; |
130 |
> |
} |
131 |
> |
// finally print out what we've found. |
132 |
> |
System.out.println("IP("+ip+"): "+expression); |
133 |
> |
if(ip) { |
134 |
> |
for(int j=0; j < ipaddr.length; j++) { |
135 |
> |
System.out.print(ipaddr[j] + " "); |
136 |
> |
} |
137 |
> |
System.out.println(); |
138 |
> |
} |
139 |
> |
_acl.add(new ACLRule(allow, expression, ipaddr, ip)); |
140 |
|
} |
141 |
|
|
142 |
|
/** |
169 |
|
* @return whether the InetAddress was permitted by the ACL |
170 |
|
*/ |
171 |
|
public boolean check(InetAddress address) { |
172 |
+ |
String hostname = address.getHostName(); |
173 |
+ |
String ip = address.getHostAddress(); |
174 |
+ |
short[] ipaddr = ipStringToShort(ip); |
175 |
|
for(int i=0; i < _acl.size(); i++) { |
176 |
|
ACLRule rule = (ACLRule) _acl.get(i); |
177 |
< |
if(StringUtils.wildcardCheck(address.getHostName(), rule._expression)) { |
178 |
< |
return rule._allow; |
177 |
> |
if(rule._iprule) { |
178 |
> |
System.out.println("checking ip rule "+rule._expression); |
179 |
> |
//if(StringUtils.wildcardCheck(ip, rule._expression)) { |
180 |
> |
if(compareShorts(ipaddr, rule._ipaddr)) { |
181 |
> |
return rule._allow; |
182 |
> |
} |
183 |
|
} |
184 |
< |
if(StringUtils.wildcardCheck(address.getHostAddress(), rule._expression)) { |
185 |
< |
return rule._allow; |
184 |
> |
else { |
185 |
> |
System.out.println("checking name rule: "+rule._expression); |
186 |
> |
if(StringUtils.wildcardCheck(hostname, rule._expression)) { |
187 |
> |
return rule._allow; |
188 |
> |
} |
189 |
|
} |
190 |
+ |
|
191 |
|
} |
192 |
|
return _defaultMode; |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
< |
* Gets the ACL as a String for debugging. |
196 |
> |
* Gives a String representation of this ACL. |
197 |
|
* |
198 |
|
* @return A String representation of this ACL. |
199 |
|
*/ |
200 |
< |
public String getStringACL() { |
201 |
< |
String acl = ""; |
200 |
> |
public String toString() { |
201 |
> |
StringBuffer acl = new StringBuffer(); |
202 |
> |
acl.append("{"); |
203 |
|
for(int i=0; i < _acl.size(); i++) { |
204 |
< |
ACLRule rule = (ACLRule) _acl.get(i); |
205 |
< |
if(rule._allow) { |
133 |
< |
acl += "ALLOW:" + rule._expression + " "; |
134 |
< |
} |
135 |
< |
else { |
136 |
< |
acl += "DENY:" + rule._expression + " "; |
137 |
< |
} |
204 |
> |
acl.append((ACLRule) _acl.get(i)); |
205 |
> |
acl.append(","); |
206 |
|
} |
207 |
|
if(_defaultMode) { |
208 |
< |
acl += "DEFAULT:ALLOW"; |
208 |
> |
acl.append("DEFAULT=ALLOW"); |
209 |
|
} |
210 |
|
else { |
211 |
< |
acl += "DEFAULT:DENY"; |
211 |
> |
acl.append("DEFAULT=DENY"); |
212 |
|
} |
213 |
< |
return acl; |
213 |
> |
acl.append("}"); |
214 |
> |
return acl.toString(); |
215 |
|
} |
147 |
– |
|
148 |
– |
/** |
149 |
– |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
150 |
– |
* method to provide clean logging (every class should have this). |
151 |
– |
* |
152 |
– |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
153 |
– |
* to format the toString() |
154 |
– |
* |
155 |
– |
* @return the name of this class and its CVS revision |
156 |
– |
*/ |
157 |
– |
public String toString() { |
158 |
– |
return FormatName.getName( |
159 |
– |
_name, |
160 |
– |
getClass().getName(), |
161 |
– |
REVISION); |
162 |
– |
} |
216 |
|
|
217 |
|
//---PRIVATE METHODS--- |
218 |
|
|
219 |
+ |
private short[] ipStringToShort(String ip) { |
220 |
+ |
short[] ipaddr = {-1, -1, -1, -1}; |
221 |
+ |
StringTokenizer st = new StringTokenizer(ip, "."); |
222 |
+ |
for(int i=0; i < 4 && st.hasMoreTokens(); i++) { |
223 |
+ |
try { |
224 |
+ |
ipaddr[i] = Short.parseShort(st.nextToken()); |
225 |
+ |
} |
226 |
+ |
catch(NumberFormatException e) { |
227 |
+ |
// do nothing? |
228 |
+ |
} |
229 |
+ |
} |
230 |
+ |
return ipaddr; |
231 |
+ |
} |
232 |
+ |
|
233 |
+ |
private boolean compareShorts(short[] first, short[] second) { |
234 |
+ |
if(first.length != second.length) { |
235 |
+ |
System.out.println("not equal length"); |
236 |
+ |
return false; |
237 |
+ |
} |
238 |
+ |
for(int i=0; i < first.length; i++) { |
239 |
+ |
// -- might want to consider specify which is the wildcard one? |
240 |
+ |
System.out.println(i + ":" + first[i] + "," + second[i]); |
241 |
+ |
if(first[i] == -1 || second[i] == -1) { |
242 |
+ |
continue; |
243 |
+ |
} |
244 |
+ |
if(first[i] != second[i]) { |
245 |
+ |
System.out.println("not equal"); |
246 |
+ |
return false; |
247 |
+ |
} |
248 |
+ |
} |
249 |
+ |
System.out.println("equal"); |
250 |
+ |
return true; |
251 |
+ |
} |
252 |
+ |
|
253 |
|
//---ACCESSOR/MUTATOR METHODS--- |
254 |
|
|
255 |
|
//---ATTRIBUTES--- |
289 |
|
* |
290 |
|
* @param allow whether this is an ALLOW or DENY rule |
291 |
|
* @param expression what this rule matches |
292 |
+ |
* @param iprule whether this is an IP rule |
293 |
|
*/ |
294 |
< |
private ACLRule(boolean allow, String expression) { |
294 |
> |
private ACLRule(boolean allow, String expression, short[] ipaddr, boolean iprule) { |
295 |
|
_allow = allow; |
296 |
|
_expression = expression; |
297 |
+ |
_ipaddr = ipaddr; |
298 |
+ |
_iprule = iprule; |
299 |
|
} |
300 |
|
|
301 |
|
/** |
302 |
+ |
* Returns a String representation of this rule. |
303 |
+ |
* |
304 |
+ |
* @return A String representation of this rule. |
305 |
+ |
*/ |
306 |
+ |
public String toString() { |
307 |
+ |
if(_allow) { |
308 |
+ |
return _expression + "=ALLOW"; |
309 |
+ |
} |
310 |
+ |
else { |
311 |
+ |
return _expression + "=DENY"; |
312 |
+ |
} |
313 |
+ |
} |
314 |
+ |
|
315 |
+ |
/** |
316 |
|
* Whether this is an ALLOW or DENY rule. |
317 |
|
*/ |
318 |
|
private boolean _allow; |
321 |
|
* What this rule matches. |
322 |
|
*/ |
323 |
|
private String _expression; |
324 |
+ |
|
325 |
+ |
private short[] _ipaddr; |
326 |
+ |
|
327 |
+ |
/** |
328 |
+ |
* Whether this is an IP rule. |
329 |
+ |
*/ |
330 |
+ |
private boolean _iprule; |
331 |
|
|
332 |
|
} |
333 |
|
|