forked from dutchcoders/go-clamd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clamd.go
311 lines (260 loc) · 6.84 KB
/
clamd.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Open Source Initiative OSI - The MIT License (MIT):Licensing
The MIT License (MIT)
Copyright (c) 2013 DutchCoders <http://github.com/dutchcoders/>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package clamd
import (
"errors"
"fmt"
"io"
"net/url"
"strings"
)
const (
RES_OK = "OK"
RES_FOUND = "FOUND"
RES_ERROR = "ERROR"
RES_PARSE_ERROR = "PARSE ERROR"
)
type Clamd struct {
address string
}
type Stats struct {
Pools string
State string
Threads string
Memstats string
Queue string
}
type ScanResult struct {
Raw string
Description string
Path string
Hash string
Size int
Status string
}
var EICAR = []byte(`X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*`)
func (c *Clamd) newConnection() (conn *CLAMDConn, err error) {
var u *url.URL
if u, err = url.Parse(c.address); err != nil {
return
}
switch u.Scheme {
case "tcp":
conn, err = newCLAMDTcpConn(u.Host)
case "unix":
conn, err = newCLAMDUnixConn(u.Path)
default:
conn, err = newCLAMDUnixConn(c.address)
}
return
}
func (c *Clamd) simpleCommand(command string) (chan *ScanResult, error) {
conn, err := c.newConnection()
if err != nil {
return nil, err
}
err = conn.sendCommand(command)
if err != nil {
return nil, err
}
ch, wg, err := conn.readResponse()
go func() {
wg.Wait()
conn.Close()
}()
return ch, err
}
/*
Check the daemon's state (should reply with PONG).
*/
func (c *Clamd) Ping() error {
ch, err := c.simpleCommand("PING")
if err != nil {
return err
}
select {
case s := (<-ch):
switch s.Raw {
case "PONG":
return nil
default:
return errors.New(fmt.Sprintf("Invalid response, got %s.", s))
}
}
return nil
}
/*
Print program and database versions.
*/
func (c *Clamd) Version() (chan *ScanResult, error) {
dataArrays, err := c.simpleCommand("VERSION")
return dataArrays, err
}
/*
On this command clamd provides statistics about the scan queue, contents of scan
queue, and memory usage. The exact reply format is subject to changes in future
releases.
*/
func (c *Clamd) Stats() (*Stats, error) {
ch, err := c.simpleCommand("STATS")
if err != nil {
return nil, err
}
stats := &Stats{}
for s := range ch {
if strings.HasPrefix(s.Raw, "POOLS") {
stats.Pools = strings.Trim(s.Raw[6:], " ")
} else if strings.HasPrefix(s.Raw, "STATE") {
stats.State = s.Raw
} else if strings.HasPrefix(s.Raw, "THREADS") {
stats.Threads = s.Raw
} else if strings.HasPrefix(s.Raw, "QUEUE") {
stats.Queue = s.Raw
} else if strings.HasPrefix(s.Raw, "MEMSTATS") {
stats.Memstats = s.Raw
} else if strings.HasPrefix(s.Raw, "END") {
} else {
// return nil, errors.New(fmt.Sprintf("Unknown response, got %s.", s))
}
}
return stats, nil
}
/*
Reload the databases.
*/
func (c *Clamd) Reload() error {
ch, err := c.simpleCommand("RELOAD")
if err != nil {
return err
}
select {
case s := (<-ch):
switch s.Raw {
case "RELOADING":
return nil
default:
return errors.New(fmt.Sprintf("Invalid response, got %s.", s))
}
}
return nil
}
func (c *Clamd) Shutdown() error {
_, err := c.simpleCommand("SHUTDOWN")
if err != nil {
return err
}
return err
}
/*
Scan file or directory (recursively) with archive support enabled (a full path is
required).
*/
func (c *Clamd) ScanFile(path string) (chan *ScanResult, error) {
command := fmt.Sprintf("SCAN %s", path)
ch, err := c.simpleCommand(command)
return ch, err
}
/*
Scan file or directory (recursively) with archive and special file support disabled
(a full path is required).
*/
func (c *Clamd) RawScanFile(path string) (chan *ScanResult, error) {
command := fmt.Sprintf("RAWSCAN %s", path)
ch, err := c.simpleCommand(command)
return ch, err
}
/*
Scan file in a standard way or scan directory (recursively) using multiple threads
(to make the scanning faster on SMP machines).
*/
func (c *Clamd) MultiScanFile(path string) (chan *ScanResult, error) {
command := fmt.Sprintf("MULTISCAN %s", path)
ch, err := c.simpleCommand(command)
return ch, err
}
/*
Scan file or directory (recursively) with archive support enabled and don’t stop
the scanning when a virus is found.
*/
func (c *Clamd) ContScanFile(path string) (chan *ScanResult, error) {
command := fmt.Sprintf("CONTSCAN %s", path)
ch, err := c.simpleCommand(command)
return ch, err
}
/*
Scan file or directory (recursively) with archive support enabled and don’t stop
the scanning when a virus is found.
*/
func (c *Clamd) AllMatchScanFile(path string) (chan *ScanResult, error) {
command := fmt.Sprintf("ALLMATCHSCAN %s", path)
ch, err := c.simpleCommand(command)
return ch, err
}
/*
Scan a stream of data. The stream is sent to clamd in chunks, after INSTREAM,
on the same socket on which the command was sent. This avoids the overhead
of establishing new TCP connections and problems with NAT. The format of the
chunk is: <length><data> where <length> is the size of the following data in
bytes expressed as a 4 byte unsigned integer in network byte order and <data> is
the actual chunk. Streaming is terminated by sending a zero-length chunk. Note:
do not exceed StreamMaxLength as defined in clamd.conf, otherwise clamd will
reply with INSTREAM size limit exceeded and close the connection
*/
func (c *Clamd) ScanStream(r io.Reader, abort chan bool) (chan *ScanResult, error) {
conn, err := c.newConnection()
if err != nil {
return nil, err
}
go func() {
for {
_, allowRunning := <-abort
if !allowRunning {
break
}
}
conn.Close()
}()
conn.sendCommand("INSTREAM")
for {
buf := make([]byte, CHUNK_SIZE)
nr, err := r.Read(buf)
if nr > 0 {
conn.sendChunk(buf[0:nr])
}
if err != nil {
break
}
}
err = conn.sendEOF()
if err != nil {
return nil, err
}
ch, wg, err := conn.readResponse()
go func() {
wg.Wait()
conn.Close()
}()
return ch, nil
}
func NewClamd(address string) *Clamd {
clamd := &Clamd{address: address}
return clamd
}