Class: Clamby::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/clamby/command.rb

Overview

Interface with the system. Builds and runs the command.

Constant Summary collapse

EXECUTABLES =
%w(clamscan clamdscan freshclam)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandObject

Array containing the complete command line.



7
8
9
# File 'lib/clamby/command.rb', line 7

def command
  @command
end

Class Method Details

.clamscan_versionObject

Show the ClamAV version. Also acts as a quick check if ClamAV functions.



62
63
64
# File 'lib/clamby/command.rb', line 62

def self.clamscan_version
  new.run scan_executable, '--version'
end

.freshclamObject

Update the virus definitions.



55
56
57
58
59
# File 'lib/clamby/command.rb', line 55

def self.freshclam
  args = []
  args << "--datadir=#{Clamby.config[:datadir]}" if Clamby.config[:datadir]
  new.run 'freshclam', *args
end

.scan(path) ⇒ Object

Perform a ClamAV scan on the given path.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/clamby/command.rb', line 21

def self.scan(path)
  return nil unless file_exists?(path)

  args = [Shellwords.escape(path), '--no-summary']

  if Clamby.config[:daemonize]
    args << '--fdpass' if Clamby.config[:fdpass]
    args << '--stream' if Clamby.config[:stream]
    args << '--reload' if Clamby.config[:reload]
  end

  args << "-d #{Clamby.config[:datadir]}" if Clamby.config[:datadir]

  new.run scan_executable, *args

  case self.scan_status
  when 0
    return false
  when nil, 2
    # clamdscan returns 2 whenever error other than a detection happens
    if Clamby.config[:error_clamscan_client_error] && Clamby.config[:daemonize]
      raise Clamby::ClamscanClientError.new("Clamscan client error")
    end

    # returns true to maintain legacy behavior
    return true
  else
    return true unless Clamby.config[:error_file_virus]

    raise Clamby::VirusDetected.new("VIRUS DETECTED on #{Time.now}: #{path}")
  end
end

.scan_executableObject

Returns the appropriate scan executable, based on clamd being used.



10
11
12
13
# File 'lib/clamby/command.rb', line 10

def self.scan_executable
  return 'clamdscan' if Clamby.config[:daemonize]
  return 'clamscan'
end

.scan_statusObject

$CHILD_STATUS maybe nil if the execution itself (not the client process)



16
17
18
# File 'lib/clamby/command.rb', line 16

def self.scan_status
  $CHILD_STATUS && $CHILD_STATUS.exitstatus
end

Instance Method Details

#run(executable, *args) ⇒ Object

Run the given commands via a system call. The executable must be one of the permitted ClamAV executables. The arguments will be combined with default arguments if needed. The arguments are sorted alphabetically before being passed to the system.

Examples:

run('clamscan', file, '--verbose')
run('clamscan', '-V')


74
75
76
77
78
79
80
# File 'lib/clamby/command.rb', line 74

def run(executable, *args)
  executable_full = executable_path(executable)
  self.command = args | default_args
  self.command = command.sort.unshift(executable_full)

  system(self.command.join(' '), system_options)
end