1 21 package org.jsmtpd.plugins.filters.ClamAV; 22 23 import java.io.IOException ; 24 import java.net.InetSocketAddress ; 25 import java.net.Socket ; 26 import java.net.SocketAddress ; 27 import java.net.SocketException ; 28 import java.util.LinkedList ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.jsmtpd.core.common.PluginInitException; 33 import org.jsmtpd.core.common.filter.FilterTreeFailureException; 34 import org.jsmtpd.core.common.filter.IFilter; 35 import org.jsmtpd.core.mail.Email; 36 import org.jsmtpd.core.send.QueueService; 37 38 66 public class ClamAVFilter implements IFilter { 67 68 private String clamdHost; 69 private int clamdPort; 70 private int socketTimeout; 71 private Log log = LogFactory.getLog(ClamAVFilter.class); 72 private int connectionTimeout=90; 73 77 private boolean failOnError = false; 78 79 80 public boolean doFilter(Email input) throws FilterTreeFailureException { 81 90 log.debug("Starting ClamAV Scan of "+input.getDiskName()); 91 long time = System.currentTimeMillis(); 92 ClamAVChat chat = new ClamAVChat(clamdHost, clamdPort, input.getDataAsByte(), connectionTimeout); 93 boolean response =chat.doScan(); 94 log.debug("Scanned "+input.getDiskName()+ ", "+input.getSize()+" octs in "+(System.currentTimeMillis()-time)+" ms"); 95 if ( response == true) { 96 return true; 97 } 98 99 if (failOnError) { 100 if (!input.getFrom().toString().equals("<>")) { 102 LinkedList <String > messages = new LinkedList <String >(); 103 messages.add(""); 104 messages.add("Hello, this is the Jsmtpd mailer daemon"); 105 messages.add(""); 106 messages.add("I'm affraid I can't deliver your email to " + input.getRcptAsString()); 107 messages.add(""); 108 messages.add(getPluginName() + " has detected the virus : " + chat.getVirus()); 109 messages.add(""); 110 messages.add("This is a fatal error, giving up"); 111 Email error = Email.createInternalMail(input.getFrom(), "Mailer-daemon error, virus found", messages, input); 112 QueueService.getInstance().queueMail(error); 113 } 114 throw new FilterTreeFailureException(); 115 } 116 return false; 117 } 118 119 122 public String getPluginName() { 123 124 return "Jsmtpd-ClamAV antivirus filter"; 125 } 126 127 130 public void initPlugin() throws PluginInitException { 131 136 log.debug(getPluginName() + " initing"); 137 138 Socket sock = new Socket (); 139 SocketAddress sockaddr = new InetSocketAddress (clamdHost, clamdPort); 140 try { 141 sock.setSoTimeout(socketTimeout * 1000); 142 } catch (SocketException e1) { 143 e1.printStackTrace(); 144 } 145 146 try { 147 sock.connect(sockaddr); 148 byte[] b = { 'P', 'I', 'N', 'G', '\n' }; 149 sock.getOutputStream().write(b); 150 byte[] c = new byte[4]; 151 sock.getInputStream().read(c); 152 String d = new String (c); 153 if (!d.equals("PONG")) 154 throw new PluginInitException(); 155 156 } catch (IOException e2) { 157 throw new PluginInitException(); 158 } 159 try { 160 if (sock!=null) 161 sock.close(); 162 } catch (Exception e3) { 163 164 } 165 log.debug(getPluginName() + " initialized"); 166 } 167 168 public void shutdownPlugin() { 169 170 } 171 172 public void setClamdHost(String host) { 173 clamdHost = host; 174 } 175 176 public void setClamdPort(int port) { 177 clamdPort = port; 178 } 179 180 public void setSocketTimeout(int time) { 181 socketTimeout = time; 182 } 183 184 public void setFailOnError(boolean pa) { 185 failOnError = pa; 186 } 187 public void setConnectionTimeout(int connectionTimeout) { 188 this.connectionTimeout = connectionTimeout; 189 } 190 } | Popular Tags |