1 package org.jahia.security.license; 2 3 import java.net.InetAddress ; 4 import java.net.UnknownHostException ; 5 import org.jahia.resourcebundle.ResourceMessage; 6 import java.math.BigInteger ; 7 8 17 18 public class IPValidator extends AbstractValidator { 19 20 private static org.apache.log4j.Logger logger = 21 org.apache.log4j.Logger.getLogger(IPValidator.class); 22 23 public IPValidator(String name, String value, License license) { 24 super(name, value, license); 25 } 26 27 public boolean assertEquals(String value) { 28 29 InetAddress inetA = null; 30 try { 31 inetA = InetAddress.getLocalHost(); 32 if ( inetA == null ){ 33 logger.error("The inet address could not be found"); 34 errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.errorRetrievingLocalhostIP.label"); 35 return false; 36 } 37 } catch ( UnknownHostException uhe ){ 38 logger.error ("The inet address could not be found", uhe); 39 errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.errorRetrievingLocalhostIP.label"); 40 return false; 41 } 42 43 logger.debug( "inet address hostname=" + inetA.getHostName() + ", hostAddress=" + inetA.getHostAddress()); 44 45 InetAddress authorizedIP = null; 46 try { 47 authorizedIP = InetAddress.getByName(value); 48 } catch (UnknownHostException uhe) { 49 logger.error ("The inet address format is invalid", uhe); 50 errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.invalidIPFormat.label", value); 51 return false; 52 } 53 54 if ( !isIPAllowed(inetA, authorizedIP) ) { 55 logger.error ("Invalid license ID <" + value + "> for host <" + inetA.getHostAddress() + ">"); 56 errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.invalidIPAddress.label", inetA.getHostAddress(), value); 57 return false; 58 } 59 return true; 60 } 61 62 public boolean assertInRange(String fromValue, String toValue) { 63 64 InetAddress inetA = null; 65 try { 66 inetA = InetAddress.getLocalHost(); 67 if ( inetA == null ){ 68 logger.error("The inet address could not be found"); 69 errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.errorRetrievingLocalhostIP.label"); 70 return false; 71 } 72 } catch ( UnknownHostException uhe ){ 73 logger.error ("The inet address could not be found", uhe); 74 errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.errorRetrievingLocalhostIP.label"); 75 return false; 76 } 77 78 InetAddress fromIP = null; 79 try { 80 fromIP = InetAddress.getByName(fromValue); 81 } catch (UnknownHostException uhe) { 82 logger.error ("The inet address format is invalid", uhe); 83 errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.invalidIPFormat.label", fromValue); 84 return false; 85 } 86 InetAddress toIP = null; 87 try { 88 toIP = InetAddress.getByName(toValue); 89 } catch (UnknownHostException uhe) { 90 logger.error ("The inet address format is invalid", uhe); 91 errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.invalidIPFormat.label", toValue); 92 return false; 93 } 94 95 if (!isIPAllowed(inetA, fromIP, toIP)) { 96 logger.error ("Host <" + inetA.getHostAddress() + "> is not in IP range " + fromValue + " to " + toValue); 97 errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.invalidIPAddressForRange.label", inetA.getHostAddress(), fromValue, toValue); 98 return false; 99 } 100 101 return true; 102 } 103 104 private boolean isIPAllowed(InetAddress testIP, InetAddress authorizationIP) { 105 byte[] testAddress = testIP.getAddress(); 106 byte[] authorizationAddress = authorizationIP.getAddress(); 107 108 for (int i=0; i < authorizationAddress.length; i++) { 109 if (authorizationAddress[i] != testAddress[i]) { 110 return false; 111 } 112 } 113 return true; 114 } 115 116 private boolean isIPAllowed(InetAddress testIP, InetAddress fromIP, InetAddress toIP) { 117 byte[] testAddress = testIP.getAddress(); 118 byte[] fromAddress = fromIP.getAddress(); 119 byte[] toAddress = toIP.getAddress(); 120 121 BigInteger testInt = new BigInteger (testAddress); 122 BigInteger fromInt = new BigInteger (fromAddress); 123 BigInteger toInt = new BigInteger (toAddress); 124 125 if ((testInt.compareTo(fromInt) >= 0) && (testInt.compareTo(toInt) <= 0)) { 126 return true; 127 } 128 return false; 129 } 130 131 } 132 | Popular Tags |