clamd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ source 'https://rubygems.org'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 soundarapandian rathinasamy
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,89 @@
1
+ # Clamd
2
+
3
+ Ruby gem to speak with Clamd daemon
4
+
5
+ NOTE: Still under development
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'clamd'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install clamd
20
+
21
+ ## Configuration
22
+
23
+ Refer the below code to configure the Clamd gem
24
+
25
+ Clamd.configure do |config|
26
+ config.host = "localhost" #mandatory
27
+ config.port = 9321 #mandatory
28
+ config.open_timeout = 5 #optional
29
+ config.read_timeout = 20 #optional
30
+ config.chunk_size = 10240 #optional
31
+ end
32
+
33
+ ## Usage
34
+
35
+ To check ClamAV version
36
+
37
+ Clamd.version
38
+
39
+ Response will be like below
40
+
41
+ =>"ClamAV 0.97.5/15468/Wed Oct 17 01:13:58 2012\n"
42
+
43
+ To check Clamd is alive or not.
44
+
45
+ Clamd.ping
46
+
47
+ Response from Clamd daemon will be "PONG" if it is alive
48
+
49
+ =>"PONG"
50
+
51
+ To reload Clamd virus signature database
52
+
53
+ Clamd.reload
54
+
55
+ Response from Clamd daemon will be "RELOADING"
56
+
57
+ =>"RELOADING"
58
+
59
+ To shutdown Clamd daemon
60
+
61
+ Clamd.shutdown
62
+
63
+ Response will be blank if shutdown success
64
+
65
+ =>""
66
+
67
+ To scan a file
68
+
69
+ Clamd.scan("/file/path")
70
+ =>"/file/path: OK"
71
+
72
+ To use CONTSCAN facility of Clamd
73
+
74
+ Clamd.contscan("/file/path")
75
+ =>"/file/path: OK"
76
+
77
+ To use MULTISCAN facility of Clamd
78
+
79
+ Clamd.multiscan("/file/path")
80
+ =>"/file/path: OK"
81
+
82
+ To use INSTREAM facility of Clamd
83
+
84
+ Clamd.instream("/file/path/to/stream/to/clamd")
85
+ =>"stream: OK"
86
+
87
+ To know Clamd scan queue status
88
+
89
+ Clamd.stats
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'clamd/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "clamd"
8
+ gem.version = Clamd::VERSION
9
+ gem.authors = ["soundarapandian rathinasamy"]
10
+ gem.email = ["soundar.rathinasamy@gmail.com"]
11
+ gem.description = %q{Ruby gem to interact with ClamAV daemon(Clamd)}
12
+ gem.summary = %q{Clamd gem enables you to feed the simple VERSION command to
13
+ complex INSTREAM command to ClamAV antivirus daemon(Clamd)}
14
+ gem.homepage = "https://github.com/soundarapandian/clamd"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,7 @@
1
+ require "clamd/command"
2
+ require "clamd/configuration"
3
+ require "clamd/version"
4
+
5
+ module Clamd
6
+ extend Command
7
+ end
@@ -0,0 +1,100 @@
1
+ require 'timeout'
2
+ require 'clamd/socket_utility'
3
+ require 'clamd/instream_helper'
4
+
5
+ module Clamd
6
+ module Command
7
+ COMMAND = {
8
+ :ping => "PING",
9
+ :version => "VERSION",
10
+ :reload => "RELOAD",
11
+ :shutdown => "SHUTDOWN",
12
+ :scan => "SCAN",
13
+ :contscan => "CONTSCAN",
14
+ :multiscan => "MULTISCAN",
15
+ :instream => "zINSTREAM\0",
16
+ :stats => "zSTATS\0" }.freeze
17
+
18
+ include SocketUtility
19
+ include InstreamHelper
20
+
21
+ def exec(command, path=nil)
22
+ begin
23
+ return "ERROR: Please configure Clamd first" unless Clamd.configured?
24
+ socket = Timeout::timeout(Clamd.configuration.open_timeout) { open_socket }
25
+ write_socket(socket, command, path)
26
+ Timeout::timeout(Clamd.configuration.read_timeout) { read_socket(socket, command) }
27
+ rescue Errno::ECONNREFUSED
28
+ "ERROR: Failed to connect to Clamd daemon"
29
+ rescue Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE
30
+ "ERROR: Connection with Clamd daemon closed unexpectedly"
31
+ rescue Timeout::Error
32
+ "ERROR: Timeout error occurred"
33
+ ensure
34
+ close_socket(socket) if socket
35
+ end
36
+ end
37
+
38
+ def read_socket(socket, command)
39
+ socket.recv(clamd_response_size(command)).gsub(/(\u0000)|(\n)/, "")
40
+ end
41
+
42
+ def write_socket(socket, command, path)
43
+ if path && command != COMMAND[:instream]
44
+ socket.write("#{command} #{path}")
45
+ else
46
+ socket.write(command)
47
+ end
48
+ stream_to_clamd(socket, path) if command == COMMAND[:instream]
49
+ end
50
+
51
+ def clamd_response_size(command)
52
+ case command
53
+ when COMMAND[:ping]
54
+ 4
55
+ when COMMAND[:reload]
56
+ 9
57
+ when COMMAND[:shutdown]
58
+ 1
59
+ else
60
+ 1024
61
+ end
62
+ end
63
+
64
+ def ping
65
+ exec(COMMAND[:ping])
66
+ end
67
+
68
+ def version
69
+ exec(COMMAND[:version])
70
+ end
71
+
72
+ def reload
73
+ exec(COMMAND[:reload])
74
+ end
75
+
76
+ def shutdown
77
+ exec(COMMAND[:shutdown])
78
+ end
79
+
80
+ def scan(path)
81
+ exec(COMMAND[:scan], path)
82
+ end
83
+
84
+ def contscan(path)
85
+ exec(COMMAND[:contscan], path)
86
+ end
87
+
88
+ def multiscan(path)
89
+ exec(COMMAND[:multiscan], path)
90
+ end
91
+
92
+ def instream(path)
93
+ exec(COMMAND[:instream], path)
94
+ end
95
+
96
+ def stats
97
+ exec(COMMAND[:stats])
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,35 @@
1
+ module Clamd
2
+ class Configuration
3
+ attr_accessor :host, :port, :open_timeout, :read_timeout, :chunk_size
4
+ DEFAULT_CONFIGURATION = { :open_timeout => 5, :read_timeout => 30, :chunk_size => 10240 }
5
+
6
+ def initialize
7
+ @open_timeout = DEFAULT_CONFIGURATION[:open_timeout]
8
+ @read_timeout = DEFAULT_CONFIGURATION[:read_timeout]
9
+ @chunk_size = DEFAULT_CONFIGURATION[:chunk_size]
10
+ end
11
+ end
12
+
13
+ def self.configure
14
+ @configuration ||= Configuration.new
15
+ yield(@configuration) if block_given?
16
+ missing = []
17
+ missing << "host" unless @configuration.host
18
+ missing << "port" unless @configuration.port
19
+ raise ConfigurationError,
20
+ "Missing configuration: #{missing.join(",")}" unless missing.empty?
21
+ @configuration
22
+ end
23
+
24
+ def self.configuration
25
+ return unless @configuration
26
+ @configuration
27
+ end
28
+
29
+ def self.configured?
30
+ return true if configuration
31
+ false
32
+ end
33
+
34
+ class ConfigurationError < StandardError;end
35
+ end
@@ -0,0 +1,26 @@
1
+ module Clamd
2
+ module InstreamHelper
3
+ def stream_to_clamd(socket, path)
4
+ begin
5
+ file = File.open(path, "rb")
6
+ read = file.read(Clamd.configuration.chunk_size)
7
+ while read
8
+ write_chunk(socket, read)
9
+ read = file.read(10240)
10
+ end
11
+ stop_streaming(socket)
12
+ ensure
13
+ file.close if file
14
+ end
15
+ end
16
+
17
+ def write_chunk(socket, chunk)
18
+ socket.write([chunk.size].pack("N"))
19
+ socket.write(chunk)
20
+ end
21
+
22
+ def stop_streaming(socket)
23
+ socket.write([0].pack("N"))
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ require 'socket'
2
+
3
+ module Clamd
4
+ module SocketUtility
5
+ def open_socket
6
+ TCPSocket.open(Clamd.configuration.host, Clamd.configuration.port)
7
+ end
8
+
9
+ def close_socket(socket)
10
+ socket.close
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Clamd
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clamd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - soundarapandian rathinasamy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Ruby gem to interact with ClamAV daemon(Clamd)
15
+ email:
16
+ - soundar.rathinasamy@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - clamd.gemspec
27
+ - lib/clamd.rb
28
+ - lib/clamd/command.rb
29
+ - lib/clamd/configuration.rb
30
+ - lib/clamd/instream_helper.rb
31
+ - lib/clamd/socket_utility.rb
32
+ - lib/clamd/version.rb
33
+ homepage: https://github.com/soundarapandian/clamd
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.24
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Clamd gem enables you to feed the simple VERSION command to complex INSTREAM
57
+ command to ClamAV antivirus daemon(Clamd)
58
+ test_files: []