| 45 | | * It connects to a remote daemon (via TCP), passes |
|---|
| 46 | | * it the stream to check. Any virus detection |
|---|
| 47 | | * makes the filter fail and drop the mail. |
|---|
| 48 | | * It generates a new mail to inform the sender.<br> |
|---|
| 49 | | * |
|---|
| 50 | | * You need to set up a ClamAV daemon, by example |
|---|
| 51 | | * on your favourite Linux distro : |
|---|
| 52 | | * apt-get install clamav-daemon should do the job |
|---|
| 53 | | * chose a port number to bind it to and edit the |
|---|
| 54 | | * clamav daemon config file clamd.conf :<br><br> |
|---|
| 55 | | * TCPSocket xxx<br> |
|---|
| 56 | | * where xxx is your port number |
|---|
| 57 | | * <br><br> |
|---|
| 58 | | * you should read http://www.clamav.net/doc/ |
|---|
| 59 | | * to get info on ClamAV |
|---|
| 60 | | * <br> |
|---|
| 61 | | * <br><br> |
|---|
| 62 | | * 7/3/2005<br> |
|---|
| 63 | | * changed Email type, so adapted here |
|---|
| 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 | | } |
|---|
| | 58 | |
|---|
| | 59 | ClamAVScanner scanner = ClamAVScannerFactory.getScanner(); |
|---|
| | 60 | ByteArrayInputStream is = new ByteArrayInputStream (input.getDataAsByte()); |
|---|
| | 61 | |
|---|
| | 62 | boolean response; |
|---|
| | 63 | try { |
|---|
| | 64 | response = scanner.performScan(is); |
|---|
| | 65 | |
|---|
| | 66 | log.debug("Scanned "+input.getDiskName()+ ", "+input.getSize()+" octs in "+(System.currentTimeMillis()-time)+" ms"); |
|---|
| | 67 | if ( response == true) { |
|---|
| | 68 | return true; |
|---|
| | 69 | } |
|---|
| 99 | | if (failOnError) { |
|---|
| 100 | | if (!input.getFrom().toString().equals("<>")) // is it an internal mail already ? |
|---|
| 101 | | { |
|---|
| 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 | | } |
|---|
| | 71 | if (failOnError) { |
|---|
| | 72 | if (!input.getFrom().toString().equals("<>")) // is it an internal mail already ? |
|---|
| | 73 | { |
|---|
| | 74 | LinkedList<String> messages = new LinkedList<String>(); |
|---|
| | 75 | messages.add(""); |
|---|
| | 76 | messages.add("Hello, this is the Jsmtpd mailer daemon"); |
|---|
| | 77 | messages.add(""); |
|---|
| | 78 | messages.add("I'm affraid I can't deliver your email to " + input.getRcptAsString()); |
|---|
| | 79 | messages.add(""); |
|---|
| | 80 | messages.add(getPluginName() + " has detected the virus : " + scanner.getMessage()); |
|---|
| | 81 | messages.add(""); |
|---|
| | 82 | messages.add("This is a fatal error, giving up"); |
|---|
| | 83 | Email error = Email.createInternalMail(input.getFrom(), "Mailer-daemon error, virus found", messages, input); |
|---|
| | 84 | QueueService.getInstance().queueMail(error); |
|---|
| | 85 | } |
|---|
| | 86 | throw new FilterTreeFailureException(); |
|---|
| | 87 | } |
|---|
| | 88 | |
|---|
| | 89 | } catch (ScannerException e) { |
|---|
| | 90 | log.error("Failed to scan "+input.getDiskName(),e); |
|---|
| | 91 | return failOnError; |
|---|
| | 92 | } |
|---|
| 131 | | /** |
|---|
| 132 | | * Read config |
|---|
| 133 | | * connect to tcp port |
|---|
| 134 | | * check answer of pong |
|---|
| 135 | | */ |
|---|
| 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 | | } |
|---|