/
engine.go
150 lines (125 loc) · 2.82 KB
/
engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// +build !windows
package clam
/*
#cgo pkg-config: libclamav
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <clamav.h>
*/
import "C"
import (
"errors"
"fmt"
"os"
"runtime"
"sync"
"sync/atomic"
)
var (
ErrAlreadyCompiled = errors.New("engine already compiled")
)
type Engine interface {
// Compile compiles the engine using the virus definitions in cvdDir.
Compile(cvdDir string) error
// IsCompiled returns true if then engine is compiled.
IsCompiled() bool
// Scan scans the file for viruses, returns error if virus is found.
Scan(file *os.File) error
// Destroy destroys the engine.
Destroy()
}
func init() {
// Prepare libclamav
var ret C.int
ret = C.cl_init(C.CL_INIT_DEFAULT)
if ret != C.CL_SUCCESS {
panic("cannot initialize clamav:" + C.GoString(C.cl_strerror(ret)))
}
}
func New() Engine {
e := &clEngine{}
runtime.SetFinalizer(e, func(e2 *clEngine) {
e2.Destroy()
})
return e
}
type clEngine struct {
compiled int32
engine *C.struct_cl_engine
mtx sync.Mutex
}
func (e *clEngine) Compile(cvdDir string) error {
e.mtx.Lock()
defer e.mtx.Unlock()
if e.IsCompiled() {
return ErrAlreadyCompiled
}
// Create new engine
e.engine = C.cl_engine_new()
if e.engine == nil {
return errors.New("failed to create new clamav engine")
}
// Load signatures
var ret C.int
var sigs C.uint = 0
ret = C.cl_load(C.CString(cvdDir), e.engine, &sigs, C.CL_DB_STDOPT)
if ret != C.CL_SUCCESS {
return fmt.Errorf("could not load vcds: %s", C.GoString(C.cl_strerror(ret)))
}
// Compile engine
ret = C.cl_engine_compile(e.engine)
if ret != C.CL_SUCCESS {
return fmt.Errorf("could not compile engine: %s", C.GoString(C.cl_strerror(ret)))
}
atomic.StoreInt32(&e.compiled, 1)
return nil
}
func (e *clEngine) IsCompiled() bool {
return (atomic.LoadInt32(&e.compiled) == 1)
}
func (e *clEngine) Scan(file *os.File) error {
e.mtx.Lock()
defer e.mtx.Unlock()
var ret C.int
var fd C.int = C.int(file.Fd())
var virname *C.char
var size C.ulong = 0
// Scan file
var virName string
ret = C.cl_scandesc(fd, &virname, &size, e.engine, C.CL_SCAN_STDOPT)
if ret == C.CL_VIRUS {
virName = C.GoString(virname)
} else if ret == C.CL_CLEAN {
// do nothing
} else {
return fmt.Errorf("error scanning file: %s", C.GoString(C.cl_strerror(ret)))
}
if len(virName) > 0 {
return &VirusError{
VirusName: virName,
BytesScanned: uint64(size * C.CL_COUNT_PRECISION),
}
}
return nil
}
func (e *clEngine) Destroy() {
e.mtx.Lock()
defer e.mtx.Unlock()
if e.engine != nil {
C.cl_engine_free(e.engine)
e.engine = nil
e.compiled = 0
}
}
type VirusError struct {
VirusName string
BytesScanned uint64
}
func (e *VirusError) Error() string {
return fmt.Sprintf("found virus %s", e.VirusName)
}