You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

179 lines
3.8 KiB

  1. /*
  2. Open Source Initiative OSI - The MIT License (MIT):Licensing
  3. The MIT License (MIT)
  4. Copyright (c) 2013 DutchCoders <http://github.com/dutchcoders/>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. this software and associated documentation files (the "Software"), to deal in
  7. the Software without restriction, including without limitation the rights to
  8. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  9. of the Software, and to permit persons to whom the Software is furnished to do
  10. so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. package clamd
  22. import (
  23. "bufio"
  24. "fmt"
  25. "io"
  26. "net"
  27. "regexp"
  28. "strconv"
  29. "strings"
  30. "sync"
  31. "time"
  32. )
  33. const CHUNK_SIZE = 1024
  34. const TCP_TIMEOUT = time.Second * 2
  35. var resultRegex = regexp.MustCompile(
  36. `^(?P<path>[^:]+): ((?P<desc>[^:]+)(\((?P<virhash>([^:]+)):(?P<virsize>\d+)\))? )?(?P<status>FOUND|ERROR|OK)$`,
  37. )
  38. type CLAMDConn struct {
  39. net.Conn
  40. }
  41. func (conn *CLAMDConn) sendCommand(command string) error {
  42. commandBytes := []byte(fmt.Sprintf("n%s\n", command))
  43. _, err := conn.Write(commandBytes)
  44. return err
  45. }
  46. func (conn *CLAMDConn) sendEOF() error {
  47. _, err := conn.Write([]byte{0, 0, 0, 0})
  48. return err
  49. }
  50. func (conn *CLAMDConn) sendChunk(data []byte) error {
  51. var buf [4]byte
  52. lenData := len(data)
  53. buf[0] = byte(lenData >> 24)
  54. buf[1] = byte(lenData >> 16)
  55. buf[2] = byte(lenData >> 8)
  56. buf[3] = byte(lenData >> 0)
  57. a := buf
  58. b := make([]byte, len(a))
  59. for i := range a {
  60. b[i] = a[i]
  61. }
  62. conn.Write(b)
  63. _, err := conn.Write(data)
  64. return err
  65. }
  66. func (c *CLAMDConn) readResponse() (chan *ScanResult, *sync.WaitGroup, error) {
  67. var wg sync.WaitGroup
  68. wg.Add(1)
  69. reader := bufio.NewReader(c)
  70. ch := make(chan *ScanResult)
  71. go func() {
  72. defer func() {
  73. close(ch)
  74. wg.Done()
  75. }()
  76. for {
  77. line, err := reader.ReadString('\n')
  78. if err == io.EOF {
  79. return
  80. }
  81. if err != nil {
  82. return
  83. }
  84. line = strings.TrimRight(line, " \t\r\n")
  85. ch <- parseResult(line)
  86. }
  87. }()
  88. return ch, &wg, nil
  89. }
  90. func parseResult(line string) *ScanResult {
  91. res := &ScanResult{}
  92. res.Raw = line
  93. matches := resultRegex.FindStringSubmatch(line)
  94. if len(matches) == 0 {
  95. res.Description = "Regex had no matches"
  96. res.Status = RES_PARSE_ERROR
  97. return res
  98. }
  99. for i, name := range resultRegex.SubexpNames() {
  100. switch name {
  101. case "path":
  102. res.Path = matches[i]
  103. case "desc":
  104. res.Description = matches[i]
  105. case "virhash":
  106. res.Hash = matches[i]
  107. case "virsize":
  108. i, err := strconv.Atoi(matches[i])
  109. if err == nil {
  110. res.Size = i
  111. }
  112. case "status":
  113. switch matches[i] {
  114. case RES_OK:
  115. case RES_FOUND:
  116. case RES_ERROR:
  117. break
  118. default:
  119. res.Description = "Invalid status field: " + matches[i]
  120. res.Status = RES_PARSE_ERROR
  121. return res
  122. }
  123. res.Status = matches[i]
  124. }
  125. }
  126. return res
  127. }
  128. func newCLAMDTcpConn(address string) (*CLAMDConn, error) {
  129. conn, err := net.DialTimeout("tcp", address, TCP_TIMEOUT)
  130. if err != nil {
  131. if nerr, isOk := err.(net.Error); isOk && nerr.Timeout() {
  132. return nil, nerr
  133. }
  134. return nil, err
  135. }
  136. return &CLAMDConn{Conn: conn}, err
  137. }
  138. func newCLAMDUnixConn(address string) (*CLAMDConn, error) {
  139. conn, err := net.Dial("unix", address)
  140. if err != nil {
  141. return nil, err
  142. }
  143. return &CLAMDConn{Conn: conn}, err
  144. }