Permalink
Browse files

added file path scanning support

changed group id and version
  • Loading branch information...
1 parent a424f92 commit bc1905180168d6a81db85df29fae722c9b5ccc1d @gregjan gregjan committed Mar 8, 2013
Showing with 162 additions and 65 deletions.
  1. +76 −62 pom.xml
  2. +86 −3 src/main/java/com/philvarner/clamavj/ClamScan.java
View
138 pom.xml
@@ -1,65 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
- <name>ClamAV Daemon Java API</name>
- <modelVersion>4.0.0</modelVersion>
- <groupId>edu.unc.lib.cdr</groupId>
- <artifactId>clamavj</artifactId>
- <version>0.3</version>
- <description>A fork or the Phil Varner library to interact with clamd for virus scanning.</description>
- <url>http://philvarner.com/clamavj/</url>
- <licenses>
- <license>
- <name>Apache Software License, 2.0</name>
- <url>http://www.apache.org/licenses/LICENSE-2.0</url>
- </license>
- </licenses>
- <reporting>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-project-info-reports-plugin</artifactId>
- <reportSets>
- <reportSet>
- <reports>
- <report>dependencies</report>
- <report>license</report>
- <report>scm</report>
- <report>project-team</report>
- <report>index</report>
- </reports>
- </reportSet>
- </reportSets>
- </plugin>
- </plugins>
- </reporting>
- <build>
- <pluginManagement>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-javadoc-plugin</artifactId>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
- <dependencies>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.10</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
+ <name>ClamAV Daemon Java API</name>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>edu.unc.lib.cdr</groupId>
+ <artifactId>clamavj</artifactId>
+ <version>0.3</version>
+ <description>A fork or the Phil Varner library to interact with clamd for virus scanning.</description>
+ <url>http://philvarner.com/clamavj/</url>
+ <licenses>
+ <license>
+ <name>Apache Software License, 2.0</name>
+ <url>http://www.apache.org/licenses/LICENSE-2.0</url>
+ </license>
+ </licenses>
+
+ <distributionManagement>
+ <repository>
+ <id>dlbuild.libint.unc.edu</id>
+ <name>dlbuild.libint.unc.edu-releases</name>
+ <url>http://dlbuild.libint.unc.edu:8080/artifactory/libs-release-local</url>
+ </repository>
+ <snapshotRepository>
+ <id>dlbuild.libint.unc.edu</id>
+ <name>dlbuild.libint.unc.edu-snapshots</name>
+ <url>http://dlbuild.libint.unc.edu:8080/artifactory/libs-snapshot-local</url>
+ </snapshotRepository>
+ </distributionManagement>
+
+ <reporting>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-project-info-reports-plugin</artifactId>
+ <reportSets>
+ <reportSet>
+ <reports>
+ <report>dependencies</report>
+ <report>license</report>
+ <report>scm</report>
+ <report>project-team</report>
+ <report>index</report>
+ </reports>
+ </reportSet>
+ </reportSets>
+ </plugin>
+ </plugins>
+ </reporting>
+ <build>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-javadoc-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ <version>1.1</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
</project>
@@ -1,22 +1,24 @@
package com.philvarner.clamavj;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
public class ClamScan {
private static Log log = LogFactory.getLog(ClamScan.class);
public static final int CHUNK_SIZE = 2048;
private static final byte[] INSTREAM = "zINSTREAM\0".getBytes();
+ private static final byte[] SCAN = "zSCAN\0".getBytes();
private static final byte[] PING = "zPING\0".getBytes();
private static final byte[] STATS = "nSTATS\n".getBytes();
// TODO: IDSESSION, END
@@ -147,6 +149,87 @@ public String cmd(byte[] cmd) {
public ScanResult scan(byte[] in) throws IOException {
return scan(new ByteArrayInputStream(in));
}
+
+ /**
+ * The method to call if you have the content in a file.
+ *
+ * @param in the file to scan
+ * @return the result of the scan
+ * @throws IOException
+ */
+ public ScanResult scan(File in) throws IOException {
+ Socket socket = new Socket();
+ try {
+ socket.connect(new InetSocketAddress(getHost(), getPort()));
+ } catch (IOException e) {
+ log.error("could not connect to clamd server", e);
+ return new ScanResult(e);
+ }
+
+ try {
+ socket.setSoTimeout(getTimeout());
+ } catch (SocketException e) {
+ log.error("Could not set socket timeout to " + getTimeout() + "ms", e);
+ }
+
+ DataOutputStream dos = null;
+ String response = "";
+ try { // finally to close resources
+
+ try {
+ dos = new DataOutputStream(socket.getOutputStream());
+ } catch (IOException e) {
+ log.error("could not open socket OutputStream", e);
+ return new ScanResult(e);
+ }
+
+ try {
+ dos.write(SCAN);
+ } catch (IOException e) {
+ log.debug("error writing SCAN command", e);
+ return new ScanResult(e);
+ }
+
+ try {
+ dos.write(in.getAbsolutePath().getBytes());
+ } catch (IOException e) {
+ log.debug("error writing file path", e);
+ return new ScanResult(e);
+ }
+
+ try {
+ dos.writeInt(0);
+ dos.flush();
+ } catch (IOException e) {
+ log.debug("error writing zero-length chunk to socket", e);
+ }
+
+ int read = CHUNK_SIZE;
+ byte[] buffer = new byte[CHUNK_SIZE];
+ try {
+ read = socket.getInputStream().read(buffer);
+ } catch (IOException e) {
+ log.debug("error reading result from socket", e);
+ read = 0;
+ }
+
+ if (read > 0) response = new String(buffer, 0, read);
+
+ } finally {
+ if (dos != null) try {
+ dos.close();
+ } catch (IOException e) {
+ log.debug("exception closing DOS", e);
+ }
+ try {
+ socket.close();
+ } catch (IOException e) {
+ log.debug("exception closing socket", e);
+ }
+ }
+ if (log.isDebugEnabled()) log.debug("Response: " + response);
+ return new ScanResult(response.trim());
+ }
/**
* The preferred method to call. This streams the contents of the InputStream to clamd, so

0 comments on commit bc19051

Please sign in to comment.