java/src/org/openantivirus/daemon ScannerDaemon.java,NONE,1.1 RequestHandler.java,NONE,1.1

Kurt Huwig <[email protected]> Tue, 18 May 2004 09:03:30 +0000
Newsgroups gmane.comp.security.virus.openantivirus.cvs
Message-ID <[email protected]>
Update of /cvsroot/openantivirus/java/src/org/openantivirus/daemon
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8196/src/org/openantivirus/daemon

Added Files:
	ScannerDaemon.java RequestHandler.java 
Log Message:
Added '?'-wildcard pattern matching

--- NEW FILE: RequestHandler.java ---
/*
 * $Id: RequestHandler.java,v 1.1 2004/05/18 09:03:28 kurti Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is OAV.
 *
 * The Initial Developer of the Original Code is Kurt Huwig <[email protected]>.
 * Portions created by the Initial Developer are Copyright (C) 2001-2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK *****
 */

package org.openantivirus.daemon;

import java.io.*;
import java.net.*;
import java.util.*;

import org.openantivirus.engine.*;
import org.openantivirus.engine.censor.*;

/**
 * ScannerThread
 *
 * Handles a single scan request
 *
 * Pattern-Roles:
 * @author  Kurt Huwig
 * @version $Revision: 1.1 $
 */
public class RequestHandler implements Runnable {
    public static final String VERSION =
        "$Id: RequestHandler.java,v 1.1 2004/05/18 09:03:28 kurti Exp $";
    
    public static final String OK    = "OK\n",
                               ERROR = "ERROR\n";

    
    /** timeout for data connections in milliseconds; use whole seconds */
    private static final int DATA_TIMEOUT = 10000;
    
    private final Socket socket;
    private final ScanConfiguration scanConfiguration;
    private final ScanEngine scanner;
    
    public RequestHandler(Socket socket,
                          ScanEngine scanner,
                          ScanConfiguration scanConfiguration) {
        this.socket            = socket;
        this.scanner           = scanner;
        this.scanConfiguration = scanConfiguration;
    }
    
    public void run() {
        String sResult = ERROR;
        BufferedReader br = null;
        PrintWriter    pw = null;
        try {
            br = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
            pw = new PrintWriter(socket.getOutputStream());
            
            final String commandLine = br.readLine();
            
            if (commandLine.length() == 0) {
                System.err.println("No command found");
                return;
            }
            
            final int spacePos = commandLine.indexOf(' ');
            String sCommand;
            String argument;
            if (spacePos != -1) {
                sCommand = commandLine.substring(0, spacePos).toUpperCase();
                argument = commandLine.substring(spacePos + 1);
            } else {
                sCommand = commandLine;
                argument = "";
            }
            
            try {
                if (sCommand.equals("SCAN")) {
                    if (argument.length() == 0) {
                        System.err.println("Filename not found");
                        return;
                    }
                    scanner.scan(new File(argument), scanConfiguration);
                    sResult = OK;
/*                    
                } else if (sCommand.equals("POST")) {
                    ServerSocket dataInServerSocket = new ServerSocket(
                            0, 50,
                            InetAddress.getByName(ScannerDaemon.BINDNAME));
                    
                    dataInServerSocket.setSoTimeout(DATA_TIMEOUT);
                    pw.println("Send data to port '"
                               + dataInServerSocket.getLocalPort()
                               + "' within " + (DATA_TIMEOUT / 1000)
                               + " seconds.");
                    pw.flush();
                    Socket dataInSocket = null;
                    try {
                        dataInSocket  = dataInServerSocket.accept();
                        pw.println("Connected.");
                        pw.flush();
                        new SequentialStreamFilter(
                                scanConfiguration.getTrie()).filter(
                                        dataInSocket.getInputStream(), null);
                        sResult = OK;
                        
                    } catch (InterruptedIOException iie) {
                        // Client failed to connect to data port
                        sResult = ERROR;
                    } finally {
                        if (dataInSocket != null) {
                            dataInSocket.close();
                        }
                        dataInServerSocket.close();
                    }
                    
                } else if (sCommand.equals("FILTER")) {
                    ServerSocket dataInServerSocket = new ServerSocket(
                            0, 50,
                            InetAddress.getByName(ScannerDaemon.BINDNAME));
                    ServerSocket dataOutServerSocket = new ServerSocket(
                            0, 50,
                            InetAddress.getByName(ScannerDaemon.BINDNAME));
                    
                    dataInServerSocket.setSoTimeout(DATA_TIMEOUT);
                    dataOutServerSocket.setSoTimeout(DATA_TIMEOUT);
                    pw.println("Send/receive data to/from port '"
                               + dataInServerSocket.getLocalPort() + "/"
                               + dataOutServerSocket.getLocalPort()
                               + "' within " + (DATA_TIMEOUT / 1000)
                               + " seconds.");
                    pw.flush();
                    Socket dataInSocket = null, dataOutSocket = null;
                    try {
                        dataInSocket  = dataInServerSocket.accept();
                        dataOutSocket = dataOutServerSocket.accept();
                        pw.println("Connected.");
                        pw.flush();
                        new SequentialStreamFilter(
                                scanConfiguration.getTrie()).filter(
                                        dataInSocket.getInputStream(),
                                        sCommand.equals("FILTER") ?
                                        dataOutSocket.getOutputStream() : null);
                        sResult = OK;
                        
                    } catch (InterruptedIOException iie) {
                        // Client failed to connect to data port
                        sResult = ERROR;
                    } finally {
                        if (dataOutSocket != null) {
                            dataOutSocket.close();
                        }
                        if (dataInSocket != null) {
                            dataInSocket.close();
                        }
                        dataInServerSocket.close();
                        dataOutServerSocket.close();
                    }*/
                } else if (sCommand.equals("COMMAND")
                           && argument.length() > 0) {
                    final StringTokenizer st = new StringTokenizer(argument);
                    argument = st.nextToken().toUpperCase();
                    if (argument.equals("SHUTDOWN")) {
                        System.exit(0);
                        /*
                    } else if (argument.equals("CREDO")) {
                        final String subcommand = st.nextToken().toUpperCase();
                        if (subcommand.equals("RELOAD")) {
                            final ScannerConfiguration newConf =
                                    (ScannerConfiguration)
                                    scanConfiguration.clone();
                            new CredoParser(newConf).load();
                            newConf.getTrie().prepare();
                            scanConfiguration.setConfiguration(newConf);
                            sResult = OK;
                        } else {
                            sResult = ERROR + ": Unknown subcommand '"
                                            + argument + "'.";
                        }*/
                    } else {
                        sResult = ERROR + ": Unknown COMMAND '" + argument
                                        + "'.";
                    }
                } else {
                    sResult  = ERROR + ": Unknown command '" + sCommand + "'.";
                }
                
            } catch (MalwareFoundException mfe) {
                sResult = "FOUND '" + mfe.getName()
                        + "' in '" + mfe.getEntry().getName() + "'\n";
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pw != null ) {
                    pw.write(sResult);
                    pw.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}

--- NEW FILE: ScannerDaemon.java ---
/*
 * $Id: ScannerDaemon.java,v 1.1 2004/05/18 09:03:28 kurti Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is OAV.
 *
 * The Initial Developer of the Original Code is Kurt Huwig <[email protected]>.
 * Portions created by the Initial Developer are Copyright (C) 2001-2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK *****
 */ 

package org.openantivirus.daemon;

import java.io.*;
import java.net.*;

import org.openantivirus.engine.*;
import org.openantivirus.engine.credo.*;

/**
 * Listens on a port to incoming scan request. Request is of the form
 *
 *  SCAN <filename><newline>
 *
 * Answers with
 *
 *  OK<newline>
 *
 * or
 *
 *  FOUND: <virusname> <virusname> <virusname>...<newline>
 *
 * @author  Kurt Huwig
 * @version $Revision: 1.1 $
 */
public class ScannerDaemon {
    
    public static final String REVISION = "0.6.0";
    
    public static final String BINDNAME = "localhost";
    public static final int PORT = 8127;
    
    private static final int MAX_BACKLOG = 50;
    
    private boolean isRunning;
    
    private final ScanConfiguration scanConf;
    private final ScanEngine engine;
    
    public ScannerDaemon(ScanConfiguration scanConfiguration)
                throws CredoException, IOException {
        engine = new ScanEngine(scanConfiguration);
        this.scanConf = scanConfiguration;
    }
    
    public void start() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(PORT, MAX_BACKLOG,
                                            InetAddress.getByName(BINDNAME));
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return;
        }
        
        System.err.println("Listening for incoming requests");
        
        isRunning = true;
        while (isRunning) {
            try {
                new Thread(new RequestHandler(
                        serverSocket.accept(),
                        engine,
                        scanConf)).start();
            } catch(IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
        System.out.println("OpenAntivirus ScannerDaemon v" + REVISION + "\n"
                + "(c) 2001-2004 iKu Systemhaus AG http://www.iku-ag.de/\n"
                + "ScannerDaemon comes with ABSOLUTELY NO WARRANTY; for "
                + "details read 'COPYING'.\n"
                + "This is free software, and you are welcome to redistribute "
                + "it under certain\nconditions; for details read 'COPYING'.");
        
        final WriteableScanConfiguration scanConfiguration =
                new WriteableScanConfiguration(new DefaultScanConfiguration());
        
        for (int i = 0; i < args.length; i++) {
            if (args[i].charAt(0) == '-') {
                final String sParameter = args[i].substring(1);
                if ("nosignature".equals(sParameter)) {
                    scanConfiguration.putInt("credo.level",
                                             CredoParser.NO_VERIFY);
                } else if ("credolevel".equals(sParameter)) {
                    scanConfiguration.putInt("credo.level",
                                             Integer.parseInt(args[++i]));
                } else if ("followsymlinks".equals(sParameter)) {
//                    scanConfiguration.setFollowSymlinks(true);
                } else if ("tempdir".equals(sParameter)) {
//                    scanConfiguration.setTempDirectory(new File(args[++i]));
                } else {
                    System.err.println("Unknown parameter: " + args[i]);
                }
            } else {
//                scanConfiguration.setCredoPath(args[i]);
            }
        }
        
        try {
            new ScannerDaemon(scanConfiguration).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



-------------------------------------------------------
This SF.Net email is sponsored by: SourceForge.net Broadband
Sign-up now for SourceForge Broadband and get the fastest
6.0/768 connection for only $19.95/mo for the first 3 months!
http://ads.osdn.com/?ad_id=2562&alloc_id=6184&op=click