--- experimental/server/ACL/ACL.java 2001/12/24 04:17:29 1.6 +++ experimental/server/ACL/ACL.java 2001/12/31 01:49:13 1.7 @@ -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.6 2001/12/24 04:17:29 tdb Exp $ + * @version $Id: ACL.java,v 1.7 2001/12/31 01:49:13 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.6 $"; + public static final String REVISION = "$Revision: 1.7 $"; /** * static to be used when adding an ALLOW rule to the ACL. @@ -75,7 +76,67 @@ 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; + } + // finally print out what we've found. + System.out.println("IP("+ip+"): "+expression); + if(ip) { + for(int j=0; j < ipaddr.length; j++) { + System.out.print(ipaddr[j] + " "); + } + System.out.println(); + } + _acl.add(new ACLRule(allow, expression, ipaddr, ip)); } /** @@ -108,14 +169,25 @@ public class ACL implements Serializable { * @return whether the InetAddress was permitted by the ACL */ public boolean check(InetAddress address) { + String hostname = address.getHostName(); + String ip = address.getHostAddress(); + short[] ipaddr = ipStringToShort(ip); 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) { + System.out.println("checking ip rule "+rule._expression); + //if(StringUtils.wildcardCheck(ip, rule._expression)) { + if(compareShorts(ipaddr, rule._ipaddr)) { + return rule._allow; + } } - if(StringUtils.wildcardCheck(address.getHostAddress(), rule._expression)) { - return rule._allow; + else { + System.out.println("checking name rule: "+rule._expression); + if(StringUtils.wildcardCheck(hostname, rule._expression)) { + return rule._allow; + } } + } return _defaultMode; } @@ -144,6 +216,40 @@ public class ACL implements Serializable { //---PRIVATE METHODS--- + 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? + } + } + return ipaddr; + } + + private boolean compareShorts(short[] first, short[] second) { + if(first.length != second.length) { + System.out.println("not equal length"); + return false; + } + for(int i=0; i < first.length; i++) { + // -- might want to consider specify which is the wildcard one? + System.out.println(i + ":" + first[i] + "," + second[i]); + if(first[i] == -1 || second[i] == -1) { + continue; + } + if(first[i] != second[i]) { + System.out.println("not equal"); + return false; + } + } + System.out.println("equal"); + return true; + } + //---ACCESSOR/MUTATOR METHODS--- //---ATTRIBUTES--- @@ -183,10 +289,13 @@ public class ACL implements Serializable { * * @param allow whether this is an ALLOW or DENY rule * @param expression what this rule matches + * @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; } /** @@ -212,6 +321,13 @@ public class ACL implements Serializable { * What this rule matches. */ private String _expression; + + private short[] _ipaddr; + + /** + * Whether this is an IP rule. + */ + private boolean _iprule; }