| 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 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 37 public class ClamAVChat { 38 39 private String host; 40 private int port; 41 private byte[] toScan; 42 43 private Socket chat = null; 44 private Socket data = null; 45 private String virus = ""; 46 47 private Log log = LogFactory.getLog(ClamAVChat.class); 48 49 private int connectionTimeout=90; 50 51 public ClamAVChat(String host, int port, byte[] toScan, int connectionTimeout) { 52 this.host = host; 53 this.port = port; 54 this.toScan = toScan; 55 this.connectionTimeout=connectionTimeout; 56 } 57 58 62 public boolean doScan() { 63 chat = new Socket (); 64 SocketAddress sockaddr = new InetSocketAddress (host, port); 65 try { 66 chat.setSoTimeout(connectionTimeout*1000); 67 } catch (SocketException e1) { 68 } 69 70 String responseValue = ""; 71 try { 72 chat.connect(sockaddr); 74 byte[] b = { 'S', 'T', 'R', 'E', 'A', 'M', '\n' }; 75 chat.getOutputStream().write(b); 77 byte[] rec = new byte[1]; 79 while (true) { 80 chat.getInputStream().read(rec); 81 if (rec[0] == '\n') 82 break; 83 responseValue += new String (rec); 84 } 85 log.debug("response: " + responseValue); 86 87 int dataPort = -1; 89 if (responseValue.contains(" ")) 90 dataPort = Integer.parseInt(responseValue.split(" ")[1]); 91 92 data = new Socket (); 94 SocketAddress sockaddrData = new InetSocketAddress (host, dataPort); 95 try { 96 data.setSoTimeout(connectionTimeout*1000); } catch (SocketException e1) { 98 } 99 try { 100 data.connect(sockaddrData); 101 data.getOutputStream().write(toScan); data.close(); } catch (IOException e2) { 104 105 } 106 responseValue = ""; 108 while (true) { 109 try { 110 chat.getInputStream().read(rec); 111 } catch (IOException e3) { 112 break; 113 } 114 if (rec[0] == '\n') 115 break; 116 responseValue += new String (rec); 117 } 118 log.debug( "response: " + responseValue); 119 120 } catch (IOException e) { 121 log.debug("IO Error : " + e.getMessage()); 122 } finally { 123 if (data != null) { 124 try { 125 data.close(); 126 } catch (IOException e3) { 127 } 128 } 129 130 if (chat != null) { 131 try { 132 chat.close(); 133 } catch (IOException e3) { 134 } 135 } 136 } 137 138 if (responseValue==null) { 139 log.error("response is null. Passing the mail anyway..."); 140 return true; 141 } 142 143 if (responseValue.contains("ERROR")) { 144 log.error("response is erroneous ("+responseValue+"). Passing the mail anyway..."); 145 } 146 147 if (responseValue.equals("stream: OK")) return true; 149 150 virus = responseValue; return false; 152 153 } 154 155 public String getVirus() { 156 return virus; 157 } 158 } | Popular Tags |