Skip to content

Commit

Permalink
NOJ - Initial flight checks
Browse files Browse the repository at this point in the history
Checking for age of clamav virus definition file
  • Loading branch information
gr211 committed Jun 25, 2024
1 parent 462d8cc commit f76fb15
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
61 changes: 59 additions & 2 deletions systemd/checks.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package systemd

import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"time"
)

Expand All @@ -18,10 +22,16 @@ type ClamavDbFile struct {
}

func DefaultDbAgeCheck() error {
return DbAgeCheck(clamavDbFile)
details, clamavError := GetClamConfDetails()

if clamavError != nil {
return clamavError
}

return DbConfigAgeCheck(details)
}

func DbAgeCheck(clamavDbFile ClamavDbFile) error {
func DbFileAgeCheck(clamavDbFile ClamavDbFile) error {
hoursInWeek := 24 * 7

fi, err := os.Stat(clamavDbFile.path)
Expand All @@ -40,3 +50,50 @@ func DbAgeCheck(clamavDbFile ClamavDbFile) error {
return nil
}
}

func DbConfigAgeCheck(details ClamConfDetails) error {
weekAgo := time.Now().Add(-time.Hour * 24 * 7)

tooOld := details.DailyCld.Before(weekAgo)

if tooOld {
return errors.New(fmt.Sprintf("virus definition is more than 7 days old: %s", details.DailyCld.String()))
}

return nil
}

type ClamConfDetails struct {
version string
sigs string
DailyCld time.Time
}

func GetClamConfDetails() (ClamConfDetails, error) {
cmd := exec.Command("clamconf")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return ClamConfDetails{}, errors.New(fmt.Sprintf("aaa"))
}

const layout = "Mon Jan 02 15:04:05 2006"
lines := strings.Split(out.String(), "\n")
re := regexp.MustCompile(`^daily.cld: version (.*), sigs: (.*), built on (.*)`)

for i := range lines {
line := lines[i]
finds := re.FindStringSubmatch(line)

if len(finds) > 0 {
cd := ClamConfDetails{}
cd.DailyCld, _ = time.Parse(layout, finds[3])
cd.version = finds[1]
cd.sigs = finds[2]
return cd, nil
}
}

return ClamConfDetails{}, errors.New(fmt.Sprintf("bbb"))
}
24 changes: 23 additions & 1 deletion systemd/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ func TestFileIs7DaysOld(t *testing.T) {
}
}

func TestDbConfigIsAlmost7DaysOld(t *testing.T) {
weekAgo := time.Now().Add(-time.Hour*24*7 + time.Hour)
details := ClamConfDetails{version: "1.0", sigs: "2.0", DailyCld: weekAgo}

err := DbConfigAgeCheck(details)

if err != nil {
t.Errorf("DailyCld should have been recent enough: \n%v", err)
}
}

func TestDbConfigIsOver7DaysOld(t *testing.T) {
weekAgo := time.Now().Add(-time.Hour*24*7 - time.Hour)
details := ClamConfDetails{version: "1.0", sigs: "2.0", DailyCld: weekAgo}

err := DbConfigAgeCheck(details)

if err == nil {
t.Errorf("DailyCld should have been too old")
}
}

func TestFileIsAlmost7DaysOld(t *testing.T) {
clamavError := fileErrorCheck(t, time.Now().Add(-time.Hour*24*7+time.Hour))

Expand Down Expand Up @@ -46,5 +68,5 @@ func fileErrorCheck(t *testing.T, when time.Time) error {

clamavDbFile := ClamavDbFile{path: nowFilePath}

return DbAgeCheck(clamavDbFile)
return DbFileAgeCheck(clamavDbFile)
}
1 change: 1 addition & 0 deletions systemd/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func GetClamDetails() (ClamDetails, error) {
cd.Year = strings.Split(cleanout, " ")[5][:4]
return cd, nil
}

func FindClamdProcess() ctypes.Prod {
cmd := exec.Command("ps", "aux")
var out bytes.Buffer
Expand Down

0 comments on commit f76fb15

Please sign in to comment.