1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21 package org.apache.james.jspf.terms;
22
23 import org.apache.james.jspf.core.DNSLookupContinuation;
24 import org.apache.james.jspf.core.IPAddr;
25 import org.apache.james.jspf.core.Inet6Util;
26 import org.apache.james.jspf.core.SPFSession;
27 import org.apache.james.jspf.core.exceptions.PermErrorException;
28
29 /**
30 * This class represent the ip4 mechanism
31 *
32 */
33 public class IP4Mechanism extends GenericMechanism {
34
35 /**
36 * ABNF: IP4 = "ip4" ":" ip4-network [ ip4-cidr-length ]
37 */
38 public static final String REGEX = "[iI][pP][4]" + "\\:" + "([0-9.]+)"
39 + "(?:" + IP4_CIDR_LENGTH_REGEX + ")?";
40
41 private IPAddr ip = null;
42
43 /**
44 * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
45 */
46 public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException {
47 IPAddr originalIP;
48
49 originalIP = IPAddr.getAddress(spfData.getIpAddress(), getIp()
50 .getMaskLength());
51
52 spfData.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.valueOf(getIp().getMaskedIPAddress().equals(originalIP.getMaskedIPAddress())));
53
54 return null;
55 }
56
57 /**
58 * @see org.apache.james.jspf.terms.GenericMechanism#config(org.apache.james.jspf.terms.Configuration)
59 */
60 public synchronized void config(Configuration params) throws PermErrorException {
61 if (params.groupCount() == 0) {
62 throw new PermErrorException("Missing ip");
63 }
64 String ipString = params.group(1);
65 if (!isValidAddress(ipString)) {
66 throw new PermErrorException("Invalid Address: " + ipString);
67 }
68 int maskLength = getMaxCidr();
69 if (params.groupCount() >= 2 && params.group(2) != null) {
70 String maskLengthString = params.group(2);
71 maskLength = Integer.parseInt(maskLengthString);
72
73 if (maskLength > getMaxCidr() || (maskLengthString.length() > 1 && maskLengthString.startsWith("0"))) {
74 throw new PermErrorException("Invalid CIDR: " + maskLengthString);
75 }
76 }
77 ip = IPAddr.getAddress(ipString, maskLength);
78 }
79
80 /**
81 * @see org.apache.james.jspf.core.Inet6Util#isValidIPV4Address(String)
82 */
83 protected boolean isValidAddress(String ipString) {
84 return Inet6Util.isValidIPV4Address(ipString);
85 }
86
87 /**
88 * Returns the max cidr for ip4
89 *
90 * @return maxCidr The max cidr
91 */
92 protected int getMaxCidr() {
93 return 32;
94 }
95
96 /**
97 * @return Returns the ip.
98 */
99 protected synchronized IPAddr getIp() {
100 return ip;
101 }
102
103 /**
104 * @see java.lang.Object#toString()
105 */
106 public String toString() {
107 if (getIp().getMaskLength() == getMaxCidr()) {
108 return "ip4:"+getIp().getIPAddress();
109 } else {
110 return "ip4:"+getIp().getIPAddress()+"/"+getIp().getMaskLength();
111 }
112 }
113
114 }