-
Notifications
You must be signed in to change notification settings - Fork 0
/
hcheck.go
144 lines (129 loc) · 3.67 KB
/
hcheck.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
package main
import (
"bufio"
"crypto/sha256"
"fmt"
flag "github.com/spf13/pflag"
"golang.org/x/sys/unix"
"io"
"log"
"os"
"path/filepath"
"strings"
)
func sha256File(path string) string {
fh, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer fh.Close()
h := sha256.New()
if _, err := io.Copy(h, fh); err != nil {
log.Fatal(err)
}
return fmt.Sprintf("%x", h.Sum(nil))
}
// Stolen from https://gist.github.com/sethamclean/9475737
func fileWalk(location string) chan string {
channel := make(chan string)
go func() {
filepath.Walk(location, func(path string, finfo os.FileInfo, walkErr error) (err error) {
if walkErr != nil {
log.Fatal(walkErr)
}
if !finfo.IsDir() {
channel <- path
}
return
})
defer close(channel)
}()
return channel
}
// Parse the sha256sum file into a map of map[filename] = hash
func parseHashfile(hashFilepath string) map[string]string {
file, err := os.Open(hashFilepath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
hashMap := make(map[string]string)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
splittedLine := strings.Split(scanner.Text(), " ")
if len(splittedLine) == 2 {
theHash := splittedLine[0]
theFilename := splittedLine[1]
hashMap[theFilename] = theHash
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return hashMap
}
func dirReadable(dir string) bool {
return unix.Access(dir, unix.R_OK|unix.X_OK) == nil
}
func checksumOk(filename string, hash string, hashes map[string]string) bool {
return hashes[filename] == hash
}
func checksumMismatch(filename string, hash string, hashes map[string]string) bool {
return hashes[filename] != hash && hashes[filename] != ""
}
func checksumNew(filename string, hash string, hashes map[string]string) bool {
return hashes[filename] == ""
}
func main() {
var hashFile = flag.StringP("hash-file", "f", "", "(required) List of hashes to check against.")
var checkDir = flag.StringP("check-dir", "c", "", "(required) Directory which is scanned and compared against hashes in the hash file.")
var excludeOK = flag.BoolP("exclude-ok", "o", false, "Exclude status OK lines. (default: false)")
var excludeMISMATCH = flag.BoolP("exclude-mismatch", "m", false, "Exclude status MISMATCH lines. (default: false)")
var excludeREMOVED = flag.BoolP("exclude-removed", "r", false, "Exclude status REMOVED lines. (default: false)")
var excludeNEW = flag.BoolP("exclude-new", "n", false, "Exclude status NEW lines. (default: false)")
flag.Parse()
if *excludeOK && *excludeMISMATCH && *excludeREMOVED && *excludeNEW {
return
}
if *hashFile == "" && *checkDir == "" {
flag.Usage()
return
}
if f, err := os.Open(*hashFile); err != nil {
log.Print("Unable to open --hash-file=" + *hashFile)
log.Fatal(err)
} else {
defer f.Close()
}
if !dirReadable(*checkDir) {
log.Fatal("Unable to traverse --check-dir=" + *checkDir)
return
}
hashes := parseHashfile(*hashFile)
for filename := range fileWalk(*checkDir) {
hash := sha256File(filename)
if checksumOk(filename, hash, hashes) {
if !*excludeOK {
fmt.Printf("%s %s: OK\n", hash, filename)
}
} else if checksumMismatch(filename, hash, hashes) {
if !*excludeMISMATCH {
fmt.Printf("%s %s: MISMATCH\n", hash, filename)
}
} else if checksumNew(filename, hash, hashes) {
if !*excludeNEW {
fmt.Printf("%s %s: NEW\n", hash, filename)
}
} else {
panic("How the hell did we end up here?")
}
}
// Check for hash file entry files which are missing on the filesystem.
if !*excludeREMOVED {
for filename, h := range hashes {
if _, err := os.Stat(filename); os.IsNotExist(err) {
fmt.Printf("%s %s: REMOVED\n", h, filename)
}
}
}
}