Ejemplo n.º 1
0
string ClamdScanner::Scan( const char *FileName )
{
    bool SockAnswer;

    if ( UseSocket )
    {
        SockAnswer = CLAMDSocket.ConnectToSocket( Params::GetConfigString("CLAMDSOCKET"), 1 );
    }
    else
    {
        SockAnswer = CLAMDSocket.ConnectToServer();
    }

    if ( SockAnswer == false )
    {
        //Prevent log flooding, show error only once per minute
        if ( (LastError == 0) || (LastError + 60 < time(NULL)) )
        {
            LogFile::ErrorMessage("Clamd: Could not connect to scanner! Scanner down?\n");
            LastError = time(NULL);
        }

        ScannerAnswer = "2Could not connect to scanner socket";
        return ScannerAnswer;
    }

    //Construct command for scanner
    ScannerCmd = "SCAN ";
    ScannerCmd += FileName;
    ScannerCmd += "\n";

    //Send command
    if ( CLAMDSocket.Send( ScannerCmd ) == false )
    {
        CLAMDSocket.Close();

        //Try to reconnect after 1 second
        sleep(1);

        if ( UseSocket )
        {
            SockAnswer = CLAMDSocket.ConnectToSocket( Params::GetConfigString("CLAMDSOCKET"), 1 );
        }
        else
        {
            SockAnswer = CLAMDSocket.ConnectToServer();
        }

        if ( SockAnswer == false )
        {
            //Prevent log flooding, show error only once per minute
            if ( (LastError == 0) || (LastError + 60 < time(NULL)) )
            {
                LogFile::ErrorMessage("Clamd: Could not connect to scanner! Scanner down?\n");
                LastError = time(NULL);
            }

            ScannerAnswer = "2Could not connect to scanner socket";
            return ScannerAnswer;
        }

        if ( CLAMDSocket.Send( ScannerCmd ) == false )
        {
            CLAMDSocket.Close();

            LogFile::ErrorMessage("Clamd: Could not write command to scanner\n");
            ScannerAnswer = "2Scanner connection failed";
            return ScannerAnswer;
        }
    }

    string Response;

    //Get response
    if ( CLAMDSocket.GetLine( Response, "\n", 600 ) == false )
    {
        CLAMDSocket.Close();

        ScannerAnswer = "2Could not read from scanner socket";
        return ScannerAnswer;
    }
    
    //Connection will be closed
    CLAMDSocket.Close();

    //Clean?
    if ( MatchSubstr( Response, " OK", -1 ) )
    {
        ScannerAnswer = "0Clean";
        return ScannerAnswer;
    }

    string::size_type Position;

    //Virus?
    if ( (Position = Response.find(" FOUND")) != string::npos )
    {
        string::size_type PositionStart = Response.find(": ") + 2;

        ScannerAnswer = "1" + Response.substr( PositionStart, Position - PositionStart );
        return ScannerAnswer;
    }

    //Error?
    if ( (Position = Response.find(" ERROR")) != string::npos )
    {
        //Ignore RAR errors
        if ( Response.find("RAR module failure") != string::npos )
        {
            ScannerAnswer = "0Clean";
            return ScannerAnswer;
        }

        string::size_type PositionStart = Response.find(": ") + 2;

        ScannerAnswer = "2" + Response.substr( PositionStart, Position - PositionStart );
        return ScannerAnswer;
    }

    //Unknown answer..
    LogFile::ErrorMessage("Clamd: Unknown response from scanner: %s\n", Response.c_str());
    ScannerAnswer = "2Unknown response from scanner";
    return ScannerAnswer;
}
Ejemplo n.º 2
0
int ConnectionToHTTP::AnalyseHeaderLine( string &RequestT )
{
	//~ if (LL > 0) LogFile::AccessMessage("(1)*****Analyse Header Line : %s\n",RequestT.c_str());
    //Optimize checks.. no need to match if header line not long enough

    //"Content-Length: x" needs atleast 17 chars
    //"Connection: Keep-Alive" needs atleast 22 chars

    if ( RequestT.length() > 16 )
    {
        //Uppercase for matching
        string RequestU = UpperCase(RequestT);

        if ( MatchBegin( RequestU, "CONTENT-LENGTH: ", 16 ) )
        {
            if ( RequestU.find_first_not_of("0123456789", 16) != string::npos )
            {
                //Invalid Content-Length
                return 0;
            }

            string LengthToken = RequestT.substr( 16 );

            //Sanity check for invalid huge Content-Length
            if ( LengthToken.length() > 18 ) return 0;

            if ( sscanf(LengthToken.c_str(), LLD, &ContentLength) != 1 )
            {
                ContentLength = -1;
            }

            return 0;
        }

        if ( MatchSubstr( RequestU, "CONNECTION: KEEP-ALIVE", -1 ) )
        {
            IsKeepAlive = true;

            return 0;
        }

        if ( MatchBegin( RequestU, "CONTENT-TYPE: IMAGE/", 20 ) )
        {
            IsImage = true;

            return 0;
        }

        if ( MatchBegin( RequestU, "TRANSFER-ENCODING: CHUNKED", 26 ) )
        {
            IsChunked = true;

            return 0;
        }

        if ( MatchBegin( RequestU, "TRANSFER-ENCODING: ", 19 ) )
        {
            //Not allowed on HTTP/1.0
            return -232;
        }

    }//End >16 check

    return 0;
}