--- experimental/server/ACL/ACL.java 2001/12/23 01:05:35 1.5 +++ experimental/server/ACL/ACL.java 2001/12/31 19:25:39 1.10 @@ -4,6 +4,7 @@ //---IMPORTS--- import uk.org.iscream.cms.server.util.*; import java.util.ArrayList; +import java.util.StringTokenizer; import java.net.InetAddress; import java.io.Serializable; @@ -16,7 +17,7 @@ import java.io.Serializable; * the relevant check method. * * @author $Author: tdb $ - * @version $Id: ACL.java,v 1.5 2001/12/23 01:05:35 tdb Exp $ + * @version $Id: ACL.java,v 1.10 2001/12/31 19:25:39 tdb Exp $ */ public class ACL implements Serializable { @@ -25,7 +26,7 @@ public class ACL implements Serializable { /** * The current CVS revision of this class */ - public static final String REVISION = "$Revision: 1.5 $"; + public static final String REVISION = "$Revision: 1.10 $"; /** * static to be used when adding an ALLOW rule to the ACL. @@ -75,7 +76,60 @@ public class ACL implements Serializable { * param expression what this rule matches using wildcards */ public void add(boolean allow, String expression) { - _acl.add(new ACLRule(allow, expression)); + // default to expecting it to be an IP + // we will try to disprove this :) + boolean ip = true; + short[] ipaddr = {-1, -1, -1, -1}; + int i = 0; + String s = ""; + // tokenize the expression on fullstops, so we can break + // up the quads of an IP (if it's an IP!) + StringTokenizer st = new StringTokenizer(expression, "."); + while(st.hasMoreTokens() && i++ < 4) { + s = st.nextToken(); + // if it's a wildcard, we'll skip to the next one + // as no more checks are required + if(s.equals("*")) { + continue; + } + // attempt to parse it into a short + try { + short n = Short.parseShort(s); + // if it's an int but outside of the + // valid range, it can't be an IP + if(n < 0 || n > 255) { + ip = false; + // give up checking further + break; + } + ipaddr[i-1] = n; + } + // if it didn't parse as an int it can't be an IP + catch (NumberFormatException e) { + ip = false; + // give up checking further + break; + } + } + // we've done 4 parts, so if there's any + // more this can't be an IP + if(st.hasMoreTokens()) { + ip = false; + } + // if we've done less than 4, see if the last one + // was a wildcard - if it isn't then it's not an IP + // -- this allows 129.12.* + if(i < 4 && !s.equals("*")) { + ip = false; + } + // if we had one or less entries it can't be an IP + // -- this disallows * matching as an IP due + // to the rule above + if(i <= 1) { + ip = false; + } + // add the rule to our array + _acl.add(new ACLRule(allow, expression, ipaddr, ip)); } /** @@ -99,24 +153,37 @@ public class ACL implements Serializable { /** * Check to see if an InetAddress is permitted * by the ACL. Perfect for Socket uses of this - * class. It should be made clear that this will - * check both the hostname AND IP address against - * each rule in turn. The hostname will always be - * checked BEFORE the IP address. + * class. A rule will either be for a name, or + * an IP address (this is determined in the add + * method), and the appropriate comparison will + * be performed. * * @param address the InetAddress to check * @return whether the InetAddress was permitted by the ACL */ public boolean check(InetAddress address) { + // gather the details first + String hostname = address.getHostName(); + String ip = address.getHostAddress(); + short[] ipaddr = ipStringToShort(ip); + // check each rule against this InetAddress for(int i=0; i < _acl.size(); i++) { ACLRule rule = (ACLRule) _acl.get(i); - if(StringUtils.wildcardCheck(address.getHostName(), rule._expression)) { - return rule._allow; + if(rule._iprule) { + // if this is an IP rule do a short comparison + if(compareShorts(ipaddr, rule._ipaddr)) { + return rule._allow; + } } - if(StringUtils.wildcardCheck(address.getHostAddress(), rule._expression)) { - return rule._allow; + else { + // if not do a full blown String comparsion + if(StringUtils.wildcardCheck(hostname, rule._expression)) { + return rule._allow; + } } + } + // if we haven't matched a rule, return the default return _defaultMode; } @@ -127,17 +194,15 @@ public class ACL implements Serializable { */ public String toString() { StringBuffer acl = new StringBuffer(); + // put in the i-scream toString code + acl.append(FormatName.getName(_name, getClass().getName(), REVISION)); acl.append("{"); + // put the value of each Rule in the result for(int i=0; i < _acl.size(); i++) { - ACLRule rule = (ACLRule) _acl.get(i); - if(rule._allow) { - acl.append(rule._expression + "=ALLOW"); - } - else { - acl.append(rule._expression + "=DENY"); - } + acl.append((ACLRule) _acl.get(i)); acl.append(","); } + // put the default mode in the result if(_defaultMode) { acl.append("DEFAULT=ALLOW"); } @@ -150,6 +215,60 @@ public class ACL implements Serializable { //---PRIVATE METHODS--- + /** + * Converts an IP address in String format into + * a short array of length 4. Any wildcards, *, + * found in the IP address are represented by + * a -1. + * + * @param ip The IP address in String format + * @return The IP address in a short[] + */ + private short[] ipStringToShort(String ip) { + short[] ipaddr = {-1, -1, -1, -1}; + StringTokenizer st = new StringTokenizer(ip, "."); + for(int i=0; i < 4 && st.hasMoreTokens(); i++) { + try { + ipaddr[i] = Short.parseShort(st.nextToken()); + } + catch(NumberFormatException e) { + // do nothing? + // we just want to leave it as -1 + // -- actually, maybe we want to do more checks in here? + // although in this code context it'll probably be ok, + // it might be worth verifying wildcards and that the + // number is in range... + } + } + return ipaddr; + } + + /** + * Compares two short arrays. The array can contain a -1, which + * will always match any value -- it's a wildcard. They must be + * the same length to match. At the moment the order of the + * parameters does not matter. + * + * @param first The first array to compare + * @param second The second array to compare + * @result the result of the comparison + */ + private boolean compareShorts(short[] first, short[] second) { + if(first.length != second.length) { + return false; + } + for(int i=0; i < first.length; i++) { + // -- might want to consider specify which is the wildcard one? + if(first[i] == -1 || second[i] == -1) { + continue; + } + if(first[i] != second[i]) { + return false; + } + } + return true; + } + //---ACCESSOR/MUTATOR METHODS--- //---ATTRIBUTES--- @@ -189,13 +308,31 @@ public class ACL implements Serializable { * * @param allow whether this is an ALLOW or DENY rule * @param expression what this rule matches + * @param ipaddr the IP address wildcard this rule matches if it's an IP rule + * @param iprule whether this is an IP rule */ - private ACLRule(boolean allow, String expression) { + private ACLRule(boolean allow, String expression, short[] ipaddr, boolean iprule) { _allow = allow; _expression = expression; + _ipaddr = ipaddr; + _iprule = iprule; } /** + * Returns a String representation of this rule. + * + * @return A String representation of this rule. + */ + public String toString() { + if(_allow) { + return _expression + "=ALLOW"; + } + else { + return _expression + "=DENY"; + } + } + + /** * Whether this is an ALLOW or DENY rule. */ private boolean _allow; @@ -204,6 +341,17 @@ public class ACL implements Serializable { * What this rule matches. */ private String _expression; + + /** + * The IP wildcard, only valid if this + * is an IP rule. + */ + private short[] _ipaddr; + + /** + * Whether this is an IP rule. + */ + private boolean _iprule; }