📄 clamavscan.java
字号:
reader.close(); writer.close(); if (virusFound) { String errorMessage = mail.getErrorMessage(); if (errorMessage == null) { errorMessage = ""; } else { errorMessage += "\r\n"; } StringBuffer sb = new StringBuffer(errorMessage); sb.append(logMessage + "\r\n"); // write mail and message info to log logMailInfo(mail); logMessageInfo(mimeMessage); // mark the mail with a mail attribute to check later on by other matchers/mailets mail.setAttribute(MAIL_ATTRIBUTE_NAME, "true"); // sets the error message to be shown in any "notifyXxx" message mail.setErrorMessage(sb.toString()); // mark the message with a header string mimeMessage.setHeader(HEADER_NAME, "true"); } else { if (isDebug()) { log("OK (by CLAMD on " + socket.getInetAddress() + ")"); } mail.setAttribute(MAIL_ATTRIBUTE_NAME, "false"); // mark the message with a header string mimeMessage.setHeader(HEADER_NAME, "false"); } try { saveChanges(mimeMessage); } catch (Exception ex) { log("Exception caught while saving changes (header) to the MimeMessage. Ignoring ...", ex); } } catch (Exception ex) { log("Exception caught calling CLAMD on " + socket.getInetAddress() + ": " + ex.getMessage(), ex); throw new MessagingException("Exception caught", ex); } finally { try { if (reader != null) { reader.close(); } } catch (Throwable t) {} try { if (writer != null) { writer.close(); } } catch (Throwable t) {} try { if (bos != null) { bos.close(); } } catch (Throwable t) {} try { if (streamSocket != null) { streamSocket.close(); } } catch (Throwable t) {} try { if (socket != null) { socket.close(); } } catch (Throwable t) {} } } /** * Checks if there are unallowed init parameters specified in the configuration file * against the String[] allowedInitParameters. * @param allowedArray array of strings containing the allowed parameter names * @throws MessagingException if an unknown parameter name is found */ protected final void checkInitParameters(String[] allowedArray) throws MessagingException { // if null then no check is requested if (allowedArray == null) { return; } Collection allowed = new HashSet(); Collection bad = new ArrayList(); for (int i = 0; i < allowedArray.length; i++) { allowed.add(allowedArray[i]); } Iterator iterator = getInitParameterNames(); while (iterator.hasNext()) { String parameter = (String) iterator.next(); if (!allowed.contains(parameter)) { bad.add(parameter); } } if (bad.size() > 0) { throw new MessagingException("Unexpected init parameters found: " + arrayToString(bad.toArray())); } } /** * Utility method for obtaining a string representation of an array of Objects. */ private final String arrayToString(Object[] array) { if (array == null) { return "null"; } StringBuffer sb = new StringBuffer(1024); sb.append("["); for (int i = 0; i < array.length; i++) { if (i > 0) { sb.append(","); } sb.append(array[i]); } sb.append("]"); return sb.toString(); } /** * Tries to "ping" all the CLAMD daemons to * check if they are up and accepting requests. **/ protected void ping() throws Exception { for (int i = 0; i < getAddressesCount(); i++) { ping(getAddresses(i)); } } /** * Tries (and retries as specified up to 'getMaxPings()') to "ping" the specified CLAMD daemon to * check if it is up and accepting requests. * @param address the address to "ping" */ protected void ping(InetAddress address) throws Exception { Socket socket = null; int ping = 1; for (; ; ) { if (isDebug()) { log("Trial #" + ping + "/" + getMaxPings() + " - creating socket connected to " + address + " on port " + getPort()); } try { socket = new Socket(address, getPort()); break; } catch (ConnectException ce) { log("Trial #" + ping + "/" + getMaxPings() + " - exception caught: " + ce.toString() + " while creating socket connected to " + address + " on port " + getPort()); ping++; if (ping <= getMaxPings()) { log("Waiting " + getPingIntervalMilli() + " milliseconds before retrying ..."); Thread.sleep(getPingIntervalMilli()); } else { break; } } } // if 'socket' is still null then 'maxPings' has been exceeded if (socket == null) { throw new ConnectException("maxPings exceeded: " + getMaxPings() + ". Giving up. The clamd daemon seems not to be running"); } try { // get the reader and writer to ping and receive pong BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ASCII")); PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); log("Sending: \"PING\" to " + address + " ..."); writer.println("PING"); writer.flush(); boolean pongReceived = false; for (;;) { String answer = reader.readLine(); if (answer != null) { answer = answer.trim(); log("Received: \"" + answer + "\""); answer = answer.trim(); if (answer.equals("PONG")) { pongReceived = true; } } else { break; } } reader.close(); writer.close(); if (!pongReceived) { throw new ConnectException("Bad answer from \"PING\" probe: expecting \"PONG\""); } } finally { socket.close(); } } /** * Parses the answer from a STREAM request and gets the port number. * * @param answer the answer from CLAMD containing the port number * @return the port number for streaming out the data to scan */ protected final int getStreamPortFromAnswer(String answer) throws ConnectException { int port = -1; if (answer != null && answer.startsWith(STREAM_PORT_STRING)) { try { port = Integer.parseInt(answer.substring(STREAM_PORT_STRING.length())); } catch (NumberFormatException nfe) { } } if (port <= 0) { throw new ConnectException("\"PORT nn\" expected - unable to parse: " + "\"" + answer + "\""); } return port; } /** * Saves changes resetting the original message id. * * @param message the message to save */ protected final void saveChanges(MimeMessage message) throws MessagingException { String messageId = message.getMessageID(); message.saveChanges(); if (messageId != null) { message.setHeader(RFC2822Headers.MESSAGE_ID, messageId); } } private void logMailInfo(Mail mail) { // writes the error message to the log StringWriter sout = new StringWriter(); PrintWriter out = new PrintWriter(sout, true); out.print("Mail details:"); out.print(" MAIL FROM: " + mail.getSender()); Iterator rcptTo = mail.getRecipients().iterator(); out.print(", RCPT TO: " + rcptTo.next()); while (rcptTo.hasNext()) { out.print(", " + rcptTo.next()); } log(sout.toString()); } private void logMessageInfo(MimeMessage mimeMessage) { // writes the error message to the log StringWriter sout = new StringWriter(); PrintWriter out = new PrintWriter(sout, true); out.println("MimeMessage details:"); try { if (mimeMessage.getSubject() != null) { out.println(" Subject: " + mimeMessage.getSubject()); } if (mimeMessage.getSentDate() != null) { out.println(" Sent date: " + mimeMessage.getSentDate()); } String[] sender = null; sender = mimeMessage.getHeader(RFC2822Headers.FROM); if (sender != null) { out.print(" From: "); for (int i = 0; i < sender.length; i++) { out.print(sender[i] + " "); } out.println(); } String[] rcpts = null; rcpts = mimeMessage.getHeader(RFC2822Headers.TO); if (rcpts != null) { out.print(" To: "); for (int i = 0; i < rcpts.length; i++) { out.print(rcpts[i] + " "); } out.println(); } rcpts = mimeMessage.getHeader(RFC2822Headers.CC); if (rcpts != null) { out.print(" CC: "); for (int i = 0; i < rcpts.length; i++) { out.print(rcpts[i] + " "); } out.println(); } out.print(" Size (in bytes): " + mimeMessage.getSize()); if (mimeMessage.getLineCount() >= 0) { out.print(", Number of lines: " + mimeMessage.getLineCount()); } } catch (MessagingException me) { log("Exception caught reporting message details", me); } log(sout.toString()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -