class Ddr::Antivirus::ClamdScannerAdapter

Adapter for clamd client (clamdscan)

Constants

DEFAULT_MAX_FILE_SIZE
MAX_FILE_SIZE_RE

Attributes

config[R]

Public Class Methods

new() click to toggle source
# File lib/ddr/antivirus/adapters/clamd_scanner_adapter.rb, line 14
def initialize
  @config = `clamconf` rescue nil
end

Public Instance Methods

clamdscan(path) click to toggle source
# File lib/ddr/antivirus/adapters/clamd_scanner_adapter.rb, line 33
def clamdscan(path)
  output = IO.popen(["clamdscan", "--fdpass", path, err: [:child, :out]]) do |io|
    io.read
  end
  [ output, $?.exitstatus ]
end
max_file_size() click to toggle source
# File lib/ddr/antivirus/adapters/clamd_scanner_adapter.rb, line 44
def max_file_size
  @max_file_size ||= if config && (m = MAX_FILE_SIZE_RE.match(config))
                       m[1].to_i
                     else
                       DEFAULT_MAX_FILE_SIZE
                     end
end
scan(path) click to toggle source
# File lib/ddr/antivirus/adapters/clamd_scanner_adapter.rb, line 18
def scan(path)
  check_file_size(path) # raises Ddr::Antivirus::MaxFileSizeExceeded
  output, exitcode = clamdscan(path)
  # FIXME I don't like where the scanned_at time is set, but I'm nit-picking --DCS
  result = ScanResult.new(path, output, version: version, scanned_at: Time.now.utc)
  case exitcode
  when 0
    result
  when 1
    raise VirusFoundError.new(result)
  when 2
    raise ScannerError.new(result)
  end
end
version() click to toggle source
# File lib/ddr/antivirus/adapters/clamd_scanner_adapter.rb, line 40
def version
  @version ||= `clamdscan -V`.strip
end

Private Instance Methods

check_file_size(path) click to toggle source
# File lib/ddr/antivirus/adapters/clamd_scanner_adapter.rb, line 54
def check_file_size(path)
  if (file_size = File.size(path)) > max_file_size
    raise MaxFileSizeExceeded,
          "Unable to scan file at \"#{path}\" -- size (#{file_size}) " \
          "exceeds clamd MaxFileSize setting (#{max_file_size})."
  end
end