001/*
002 *  Copyright 2023 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package org.ametys.web;
018
019import java.io.BufferedInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.nio.charset.StandardCharsets;
023import java.util.List;
024
025import org.apache.avalon.framework.component.Component;
026import org.apache.commons.io.IOUtils;
027
028import org.ametys.runtime.config.Config;
029import org.ametys.runtime.plugin.component.AbstractLogEnabled;
030
031/**
032 * Helper that analyses a file for viruses
033 */
034public class AnalyseFileForVirusHelper extends AbstractLogEnabled implements Component
035{
036    /** The component role. */
037    public static final String ROLE = AnalyseFileForVirusHelper.class.getName();
038    
039    /** The result of the antivirus when no viruses is found */
040    public static final String ANTIVIRUS_RESULT_OK = "OK";
041    
042    /**
043     * Checks if the antivirus is enabled
044     * @return <code>true</code> if the antivirus is enabled, <code>false</code> otherwise 
045     */
046    public boolean isAntivirusEnabled()
047    {
048        return Config.getInstance().getValue("plugins.web.antivirus.activated");
049    }
050    
051    /**
052     * Antivirus analysis. Based on clamscan results.
053     * 
054     * @param absolutePath the absolute path of the file to analyse
055     * @return true if the file is correct, false if a malware was discovered in
056     *         the file
057     */
058    public boolean analysefile(String absolutePath)
059    {
060        if (!isAntivirusEnabled())
061        {
062            return true;
063        }
064        
065        boolean toReturn = false;
066        try
067        {
068            String command = Config.getInstance().getValue("plugins.web.antivirus.command");
069            String[] commandExcecuted = new String[] {command, absolutePath};
070            if (getLogger().isDebugEnabled())
071            {
072                getLogger().debug("Executing antivirus analysis : " + commandExcecuted);
073            }
074            // Execute command
075            Process child = Runtime.getRuntime().exec(commandExcecuted);
076
077            // Get the input stream and read from it
078            try (InputStream in = new BufferedInputStream(child.getInputStream()))
079            {
080                List<String> lines = IOUtils.readLines(in, StandardCharsets.UTF_8);
081                if (lines != null && lines.size() > 0)
082                {
083                    if (getLogger().isDebugEnabled())
084                    {
085                        getLogger().debug("Result of the command : ");
086                        StringBuilder builder = new StringBuilder();
087                        for (String line : lines)
088                        {
089                            builder.append(line);
090                        }
091                        getLogger().debug(builder.toString());
092                    }
093                    
094                    String firstLine = lines.get(0);
095                    if (firstLine.startsWith(absolutePath))
096                    {
097                        return ANTIVIRUS_RESULT_OK.equals(firstLine.substring(absolutePath.length() + 2));
098                    }
099                }
100            }
101        }
102        catch (IOException e)
103        {
104            getLogger().error("Unable to get to output from the command", e);
105        }
106
107        return toReturn;
108    }
109}