1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org.uk |
4 |
+ |
* Copyright (C) 2000-2002 i-scream |
5 |
+ |
* |
6 |
+ |
* This program is free software; you can redistribute it and/or |
7 |
+ |
* modify it under the terms of the GNU General Public License |
8 |
+ |
* as published by the Free Software Foundation; either version 2 |
9 |
+ |
* of the License, or (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 |
+ |
* You should have received a copy of the GNU General Public License |
17 |
+ |
* along with this program; if not, write to the Free Software |
18 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
+ |
*/ |
20 |
+ |
|
21 |
|
//---PACKAGE DECLARATION--- |
22 |
< |
package uk.org.iscream.cms.server.util; |
22 |
> |
package uk.org.iscream.cms.util; |
23 |
|
|
24 |
|
//---IMPORTS--- |
25 |
|
import java.util.ArrayList; |
56 |
|
* static to be used when adding a DENY rule to the ACL. |
57 |
|
*/ |
58 |
|
public static final boolean DENY = false; |
59 |
+ |
|
60 |
+ |
/** |
61 |
+ |
* default setting for the default mode for a new ACL. |
62 |
+ |
*/ |
63 |
+ |
public static final boolean DEFMODE = ACL.ALLOW; |
64 |
|
|
65 |
|
//---STATIC METHODS--- |
66 |
|
|
72 |
|
* blocked by a rule. |
73 |
|
*/ |
74 |
|
public ACL() { |
75 |
< |
// default to ACL.ALLOW |
76 |
< |
this(ACL.ALLOW); |
75 |
> |
// default to DEFMODE |
76 |
> |
this(DEFMODE); |
77 |
|
} |
78 |
|
|
79 |
|
/** |
84 |
|
* @param defaultMode the default mode for non-matched checks |
85 |
|
*/ |
86 |
|
public ACL(boolean defaultMode) { |
87 |
< |
_defaultMode = defaultMode; |
87 |
> |
setDefaultMode(defaultMode); |
88 |
|
} |
89 |
|
|
90 |
+ |
/** |
91 |
+ |
* Construct a new Access Control List with a given |
92 |
+ |
* String representation of the ACL rules. The String |
93 |
+ |
* should be of the format: |
94 |
+ |
* expression:rule;expression:rule;expression:rule... |
95 |
+ |
* Where expression is a wildcard to match against, and |
96 |
+ |
* rule is either 'ALLOW' or 'DENY'. There is a special |
97 |
+ |
* expression of 'DEFAULT' which represents the default |
98 |
+ |
* rule (what should happen if no expression is matched |
99 |
+ |
* when performing a check). |
100 |
+ |
* The default mode is set to ALLOW if one is not |
101 |
+ |
* specified in the String. |
102 |
+ |
* |
103 |
+ |
* @param acl a String representation of the ACL. |
104 |
+ |
*/ |
105 |
+ |
public ACL(String acl) { |
106 |
+ |
setDefaultMode(DEFMODE); |
107 |
+ |
add(acl); |
108 |
+ |
} |
109 |
+ |
|
110 |
|
//---PUBLIC METHODS--- |
111 |
|
|
112 |
|
/** |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
+ |
* Add some new rules to the Access Control List in |
137 |
+ |
* the form of a String. The String should be of the |
138 |
+ |
* following format: |
139 |
+ |
* expression:rule;expression:rule;expression:rule... |
140 |
+ |
* Where expression is a wildcard to match against, and |
141 |
+ |
* rule is either 'ALLOW' or 'DENY'. There is a special |
142 |
+ |
* expression of 'DEFAULT' which represents the default |
143 |
+ |
* rule (what should happen if no expression is matched |
144 |
+ |
* when performing a check). |
145 |
+ |
* |
146 |
+ |
* @param acl a String representation of the ACL. |
147 |
+ |
*/ |
148 |
+ |
public void add(String acl) { |
149 |
+ |
if(acl != null) { |
150 |
+ |
// split the String into expression:rule parts |
151 |
+ |
StringTokenizer st1 = new StringTokenizer(acl, ";"); |
152 |
+ |
while(st1.hasMoreTokens()) { |
153 |
+ |
String token1 = st1.nextToken(); |
154 |
+ |
// if it doesn't have a :, it's not the correct format |
155 |
+ |
if(token1.indexOf(":") != -1) { |
156 |
+ |
// split into expression and rule part |
157 |
+ |
StringTokenizer st2 = new StringTokenizer(token1, ":"); |
158 |
+ |
String expression = ""; |
159 |
+ |
String rule = ""; |
160 |
+ |
if(st2.hasMoreTokens()) { |
161 |
+ |
expression = st2.nextToken(); |
162 |
+ |
} |
163 |
+ |
else { |
164 |
+ |
// mall-formed? |
165 |
+ |
continue; |
166 |
+ |
} |
167 |
+ |
if(st2.hasMoreTokens()) { |
168 |
+ |
rule = st2.nextToken(); |
169 |
+ |
} |
170 |
+ |
else { |
171 |
+ |
// mall-formed? |
172 |
+ |
continue; |
173 |
+ |
} |
174 |
+ |
// check to see what sort of rule |
175 |
+ |
if(rule.equals("ALLOW")) { |
176 |
+ |
// case for special 'DEFAULT' expression |
177 |
+ |
if(expression.equals("DEFAULT")) { |
178 |
+ |
setDefaultMode(ACL.ALLOW); |
179 |
+ |
} |
180 |
+ |
else { |
181 |
+ |
add(ACL.ALLOW, expression); |
182 |
+ |
} |
183 |
+ |
} |
184 |
+ |
else if(rule.equals("DENY")) { |
185 |
+ |
// case for special 'DEFAULT' expression |
186 |
+ |
if(expression.equals("DEFAULT")) { |
187 |
+ |
setDefaultMode(ACL.DENY); |
188 |
+ |
} |
189 |
+ |
else { |
190 |
+ |
add(ACL.DENY, expression); |
191 |
+ |
} |
192 |
+ |
} |
193 |
+ |
// if it's not ALLOW or DENY, it's not a |
194 |
+ |
// proper rule, so we'll ignore it |
195 |
+ |
} |
196 |
+ |
} |
197 |
+ |
} |
198 |
+ |
} |
199 |
+ |
|
200 |
+ |
/** |
201 |
|
* Check to see if a string is permitted by the |
202 |
|
* ACL. Useful for testing, and non-Socket uses |
203 |
|
* of this class. |
254 |
|
} |
255 |
|
|
256 |
|
/** |
257 |
+ |
* Clears the ACL and resets the default mode. |
258 |
+ |
*/ |
259 |
+ |
public void clear() { |
260 |
+ |
// just clear out our underlying ArrayList |
261 |
+ |
// containing our ACL objects |
262 |
+ |
_acl.clear(); |
263 |
+ |
// and reset the default mode to the default |
264 |
+ |
setDefaultMode(DEFMODE); |
265 |
+ |
} |
266 |
+ |
|
267 |
+ |
/** |
268 |
+ |
* Changes the default mode of the ACL. This is what |
269 |
+ |
* the check will return if it does not find an explict |
270 |
+ |
* rule to match against. |
271 |
+ |
* |
272 |
+ |
* @param defaultMode the new default mode |
273 |
+ |
*/ |
274 |
+ |
public void setDefaultMode(boolean defaultMode) { |
275 |
+ |
_defaultMode = defaultMode; |
276 |
+ |
} |
277 |
+ |
|
278 |
+ |
/** |
279 |
|
* Gives a String representation of this ACL. |
280 |
|
* |
281 |
|
* @return A String representation of this ACL. |
373 |
|
* |
374 |
|
* @param first The first array to compare (with -1 wildcard if required) |
375 |
|
* @param second The second array to compare |
376 |
< |
* @result the result of the comparison |
376 |
> |
* @return the result of the comparison |
377 |
|
*/ |
378 |
|
private boolean compareShorts(short[] first, short[] second) { |
379 |
|
if(first.length != second.length) { |
413 |
|
/** |
414 |
|
* The default mode of this ACL. |
415 |
|
*/ |
416 |
< |
private boolean _defaultMode; |
416 |
> |
private boolean _defaultMode = DEFMODE; |
417 |
|
|
418 |
|
//---STATIC ATTRIBUTES--- |
419 |
|
|