001: /*
002: *
003: * Jsmtpd, Java SMTP daemon
004: * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package org.jsmtpd.plugins.filters.builtin;
022:
023: import java.io.ByteArrayInputStream;
024: import java.util.LinkedList;
025:
026: import javax.mail.MessagingException;
027: import javax.mail.internet.MimeMessage;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.jsmtpd.config.ModuleNotFoundException;
032: import org.jsmtpd.core.common.IGenericPlugin;
033: import org.jsmtpd.core.common.PluginInitException;
034: import org.jsmtpd.core.common.PluginStore;
035: import org.jsmtpd.core.common.filter.FilterTreeFailureException;
036: import org.jsmtpd.core.common.filter.FilterTreeSuccesException;
037: import org.jsmtpd.core.common.filter.IFilter;
038: import org.jsmtpd.core.mail.Email;
039: import org.jsmtpd.core.mail.EmailAddress;
040: import org.jsmtpd.core.mail.InvalidAddress;
041: import org.jsmtpd.core.mail.Rcpt;
042: import org.jsmtpd.core.send.QueueService;
043: import org.jsmtpd.plugins.inputIPFilters.BlackList;
044:
045: /**
046: * @author Jean-Francois POUX
047: */
048: public class SimpleBlackListManager implements IFilter {
049:
050: String blackListPluginName = "";
051: BlackList controlled = null;
052:
053: String allowedSource = "";
054: String managerMail = "";
055: EmailAddress manMail = null;
056: QueueService dsvc;
057:
058: private Log log = LogFactory.getLog(SimpleBlackListManager.class);
059:
060: public boolean doFilter(Email input)
061: throws FilterTreeFailureException,
062: FilterTreeSuccesException {
063: // if mail is not from relayed domains, return true (continue process)
064: // if mail is not for jsmtpd-blacklistmanager return true
065: // If mail is not from admin ip adresses, return true
066: // else : query the plugin manager for the plugin name
067: // Then analyse the mail for command an execute it over the ip
068: // send confirm message
069: // return false (break of chain, don't deliver the mail)
070:
071: if (!input.getReceivedFrom().equals(allowedSource))
072: return true; // Skip
073:
074: if (!(input.getRcpt().size() != 1))
075: return true; // Skip
076:
077: Rcpt tmp = (Rcpt) input.getRcpt().get(0);
078: if (!tmp.getEmailAddress().equals(manMail))
079: return true;
080:
081: try {
082: MimeMessage msg = new MimeMessage(null,
083: new ByteArrayInputStream(input.getDataAsByte()));
084: String command = msg.getSubject();
085:
086: if ((command == null) || (command.length() < 7))
087: return true;
088:
089: if (command.equals("add")) {
090: String ip = command.substring(4);
091: //controlled.addPermanentBL(ip, new Date(0));
092: log.info("IP address " + ip + " added to BlackList");
093:
094: LinkedList<String> mesgs = new LinkedList<String>();
095: mesgs.add("Sucessfully added IP " + ip
096: + " to blacklist manager");
097: mesgs.add("");
098: mesgs.add("This host can't connect now to Jsmtpd");
099: mesgs.add("");
100:
101: Email e = Email.createInternalMail(input.getFrom(),
102: "BlackList admin - added address", mesgs, null);
103: dsvc.queueMail(e);
104: }
105:
106: if (command.equals("remove")) {
107: String ip = command.substring(4);
108: //controlled.removePermanentBL(ip);
109: log
110: .info("IP address " + ip
111: + " removed from BlackList");
112:
113: }
114:
115: return false; // Break of chain.
116:
117: } catch (MessagingException e) {
118: log.error("Can't parse message " + input.getDiskName()
119: + " ", e);
120: return false;
121: }
122:
123: // transform to MimeMsg
124: // Parse command
125: //Create new Email with mmsg
126: // Apply & confirm
127:
128: }
129:
130: public String getPluginName() {
131: return "Email controlled BlackList manager";
132: }
133:
134: public void initPlugin() throws PluginInitException {
135: PluginStore pluginStore = PluginStore.getInstance();
136: IGenericPlugin query;
137: try {
138: query = pluginStore
139: .getPluginByLogicalName(blackListPluginName);
140: } catch (ModuleNotFoundException e) {
141: throw new PluginInitException(e);
142: }
143:
144: if (!(query instanceof BlackList))
145: throw new PluginInitException("The plugin name "
146: + blackListPluginName + " is not controlable");
147:
148: dsvc = QueueService.getInstance();
149: controlled = (BlackList) query;
150:
151: try {
152: manMail = EmailAddress.parseAddress(managerMail);
153: } catch (InvalidAddress e1) {
154: throw new PluginInitException(e1);
155: }
156:
157: }
158:
159: public void shutdownPlugin() {
160:
161: }
162:
163: public String getBlackListPluginName() {
164: return blackListPluginName;
165: }
166:
167: public void setBlackListPluginName(String blackListPluginName) {
168: this .blackListPluginName = blackListPluginName;
169: }
170:
171: public String getAllowedSource() {
172: return allowedSource;
173: }
174:
175: public void setAllowedSource(String allowedSource) {
176: this .allowedSource = allowedSource;
177: }
178:
179: public String getManagerMail() {
180: return managerMail;
181: }
182:
183: public void setManagerMail(String managerMail) {
184: this.managerMail = managerMail;
185: }
186: }
|