001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.usermanager;
019:
020: import net.sf.drftpd.DuplicateElementException;
021:
022: import org.apache.log4j.Logger;
023:
024: import org.apache.oro.text.regex.MalformedPatternException;
025:
026: import socks.server.Ident;
027:
028: import java.io.IOException;
029: import java.net.InetAddress;
030: import java.net.Socket;
031:
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.Iterator;
035:
036: /**
037: * @author mog
038: * @version $Id: HostMaskCollection.java 1513 2006-10-13 22:41:08Z tdsoul $
039: */
040: public class HostMaskCollection extends ArrayList {
041: private static final Logger logger = Logger
042: .getLogger(HostMaskCollection.class);
043:
044: public HostMaskCollection() {
045: }
046:
047: /**
048: * Converts an existing Collection of String-based masks to a
049: * HostMaskCollection
050: *
051: * @param masks
052: */
053: public HostMaskCollection(Collection<String> masks) {
054: for (String mask : masks) {
055: add(new HostMask(mask));
056: }
057: }
058:
059: public void addAllMasks(HostMaskCollection hostMaskCollection) {
060: for (Iterator i = hostMaskCollection.iterator(); i.hasNext();) {
061: HostMask mask = (HostMask) i.next();
062:
063: if (!contains(mask)) {
064: add(mask);
065: }
066: }
067: }
068:
069: public void addMask(String mask) throws DuplicateElementException {
070: HostMask newMask = new HostMask(mask);
071:
072: for (Iterator i = iterator(); i.hasNext();) {
073: HostMask hostMask = (HostMask) i.next();
074:
075: if (hostMask.equals(newMask)) {
076: throw new DuplicateElementException();
077: }
078: }
079:
080: add(newMask);
081: }
082:
083: public boolean check(Socket s) throws MalformedPatternException {
084: return check(null, s.getInetAddress(), s);
085: }
086:
087: public boolean check(String ident, InetAddress a, Socket s)
088: throws MalformedPatternException {
089: if (a == null) {
090: throw new NullPointerException();
091: }
092: for (Iterator iter = this .iterator(); iter.hasNext();) {
093: HostMask mask = (HostMask) iter.next();
094:
095: if (!mask.matchesHost(a)) {
096: continue;
097: }
098:
099: // host matched
100: // request ident if no IDNT, ident hasn't been requested
101: // and ident matters in this hostmask
102: if (mask.isIdentMaskSignificant() && (ident == null)) {
103: try {
104: ident = new Ident(s).getUserName();
105: } catch (IOException e) {
106: ident = "";
107: }
108: }
109:
110: if (mask.matchesIdent(ident)) {
111: return true;
112: }
113: }
114:
115: return false;
116: }
117:
118: /**
119: * @param mask
120: * @return
121: */
122: public boolean removeMask(String mask) {
123: for (Iterator iter = iterator(); iter.hasNext();) {
124: if (((HostMask) iter.next()).getMask().equals(mask)) {
125: iter.remove();
126: return true;
127: }
128: }
129:
130: return false;
131: }
132:
133: public String toString() {
134: if (isEmpty())
135: return "";
136: String masks = "";
137:
138: for (Iterator iter = iterator(); iter.hasNext();) {
139: masks = masks + iter.next() + ", ";
140: }
141:
142: return masks.substring(0, masks.length() - 2);
143: }
144: }
|