Automatically scan Lotus Notes database document attachments for viruses

SearchDomino.com member Darren Wright provides a Java agent that will automatically scan document attachments in a Lotus Notes database to check for viruses. If it finds an infected file, it will detach it, clean it and then reattach the decontaminated file.

I recently came across some older Lotus Notes databases that had email attachments from the late 1990's. Upon detaching

the attachments, McAfee found several viruses that have been sitting and waiting to infect someone's computer.

Rather than having to detach, clean, and reattach each file separately, I did what any lazy programmer would do -- automate it! I was fortunate enough to have a McAfee's Enterprise VirusScan, which includes the command-line scan tool. I created the Java agent below to run against selected documents.

It detaches the attached files one at a time, scans, and waits for McAfee's response. It then re-attaches the clean copy if infected, and saved the document. This agent should work with most command-line scanners. The scanner's response can be filtered for keywords, with appropriate responses coded based on the response given.

import lotus.domino.*;

import java.io.*;

import java.util.*;


public class VirusScan extends AgentBase {
public void NotesMain()
{
Session nsess = null;
boolean docupdated = false;

try
{
nsess = getSession();

AgentContext aContext = nsess.getAgentContext();
DocumentCollection dc = 
aContext.getUnprocessedDocuments();
Document doc = dc.getFirstDocument();

while (doc != null)
{
Vector vecItemList = doc.getItems();
Iterator itItemList = vecItemList.iterator();

while (itItemList.hasNext())
{
Item itemCurrent = (Item) itItemList.next();

if (itemCurrent.getType() == Item.RICHTEXT)
{
RichTextItem itmBody = (RichTextItem) itemCurrent;
Vector v = itmBody.getEmbeddedObjects();
Enumeration e = v.elements();

while (e.hasMoreElements())
{
EmbeddedObject eo = (EmbeddedObject) 
e.nextElement();

if (eo.getType() == EmbeddedObject.
EMBED_ATTACHMENT)
{
String eoName = eo.getName();
String eoSource = eo.getSource();
String filepath = "c:\Temp\";
eo.extractFile(filepath + eoSource);
System.out.println("Checking file '" + 
eoName + "' for virus.");

if (CheckForVirus(filepath) == true)
{
System.out.println("File '" + eoName + "' contained a virus.");
eo.remove();
itmBody.embedObject
(EmbeddedObject.EMBED_ATTACHMENT, 
null, filepath + eoSource, eoName);
docupdated = true;
}

boolean success = (new File(filepath + 
eoSource)).delete();

if (!success)
{
// Deletion failed
System.out.println("File " + eoName + " 
could not be deleted.");
}

eoName = "";
eoSource = "";
filepath = "";
}

eo.recycle();
}


itmBody.recycle();
}
}

if (docupdated = true)
{
doc.save(true, false);
docupdated = false;
}

doc = dc.getNextDocument(doc);
}

// doc.recycle();
dc.recycle();
nsess.recycle();
}
catch (NotesException ne)
{
System.out.println("NotesException " + 
ne.id + " - " + ne.text);
ne.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
System.out.println("Process complete . . .");

try
{
nsess.recycle();
}
catch (NotesException ne)
{
System.out.println("NotesException " + 
ne.id + " - " + ne.text);
ne.printStackTrace();
}
}
}

public boolean CheckForVirus(String filepath)
{
boolean virusfound = false;
int rc = -1;

try
{
Runtime rt = Runtime.getRuntime();

//Process p = rt.exec(args[0]);
Process p = rt.exec
("C:\Program Files\
Network Associates\VirusScan\
csscan.exe 
/Target " + filepath + " /clean");
int bufSize = 4096;
BufferedInputStream bis = new BufferedInputStream
(p.getInputStream(), bufSize);
int len;
byte[] buffer = new byte[bufSize];
String strBuffer = null;

// Echo back what the program spit out
while ((len = bis.read(buffer, 0, bufSize)) != -1)
{
 //System.out.write(buffer, 0, len);
//convert bytes to string so it can be parsed.
strBuffer = toAsciiString(buffer, 0, len);
}

System.out.println(strBuffer);

//System.out.println(strBuffer.indexOf("(Cleaned )"));
if (strBuffer.indexOf("(Cleaned )") > 0)
{
System.out.println("Virus Detected");
virusfound = true;
}
else
{
System.out.println("No Virus Detected");
virusfound = false;
}

rc = p.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
rc = -1;
}
finally
{
System.out.println("completed");
}

return virusfound;
}

public static final String toAsciiString
(byte[] buffer, 
int startPos, int length)
{
byte[] stringBytes = new byte[length];
int endIndex = startPos + length;
int pos = 0;

for (int i = startPos; i < endIndex; i++)
{
stringBytes[pos++] = buffer[i];
}

return new String(stringBytes);
}
}

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip library by member Darren Wright. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.



This was first published in July 2006

Dig deeper on Java for Lotus Notes Domino

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchWinIT

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchVirtualDataCentre.co.UK

Close