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 org.apache.log4j.Logger;
021:
022: import org.apache.oro.text.GlobCompiler;
023: import org.apache.oro.text.regex.MalformedPatternException;
024: import org.apache.oro.text.regex.Pattern;
025: import org.apache.oro.text.regex.Perl5Matcher;
026:
027: import java.net.InetAddress;
028:
029: /**
030: * @author mog
031: * @version $Id: HostMask.java 1513 2006-10-13 22:41:08Z tdsoul $
032: */
033: public class HostMask {
034:
035: private static final Logger logger = Logger
036: .getLogger(HostMask.class);
037: private String _hostMask;
038: private String _identMask;
039:
040: public HostMask(String string) {
041: int pos = string.indexOf('@');
042:
043: if (pos == -1) {
044: _identMask = "*";
045: _hostMask = string;
046: } else {
047: _identMask = string.substring(0, pos);
048: _hostMask = string.substring(pos + 1);
049: }
050: if (_identMask.equals("")) {
051: _identMask = "*";
052: }
053: if (_hostMask.equals("")) {
054: _hostMask = "*";
055: }
056: }
057:
058: public boolean equals(Object obj) {
059: if (obj == null)
060: return false;
061: HostMask h = (HostMask) obj;
062: return h.getIdentMask().equals(getIdentMask())
063: && h.getHostMask().equals(getHostMask());
064: }
065:
066: public String getHostMask() {
067: return _hostMask;
068: }
069:
070: public String getIdentMask() {
071: return _identMask;
072: }
073:
074: public String getMask() {
075: return getIdentMask() + "@" + getHostMask();
076: }
077:
078: /**
079: * Is ident used?
080: * @return false is ident mask equals "*"
081: */
082: public boolean isIdentMaskSignificant() {
083: return !_identMask.equals("*");
084: }
085:
086: public boolean matchesHost(InetAddress a)
087: throws MalformedPatternException {
088: Perl5Matcher m = new Perl5Matcher();
089: GlobCompiler c = new GlobCompiler();
090: Pattern p = c.compile(getHostMask());
091:
092: return (m.matches(a.getHostAddress(), p) || m.matches(a
093: .getHostName(), p));
094: }
095:
096: public boolean matchesIdent(String ident)
097: throws MalformedPatternException {
098: Perl5Matcher m = new Perl5Matcher();
099: GlobCompiler c = new GlobCompiler();
100:
101: if (ident == null) {
102: ident = "";
103: }
104:
105: return !isIdentMaskSignificant()
106: || m.matches(ident, c.compile(getIdentMask()));
107: }
108:
109: public String toString() {
110: return _identMask + "@" + _hostMask;
111: }
112: }
|