]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4-check/main.go
OpenSSH signatures inclusion support
[meta4ra.git] / cmd / meta4-check / main.go
1 /*
2 meta4a -- Metalink 4.0 checker
3 Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Metalink 4.0 checker
19 package main
20
21 import (
22         "bufio"
23         "encoding/xml"
24         "flag"
25         "fmt"
26         "io"
27         "io/fs"
28         "log"
29         "os"
30         "strings"
31
32         "go.stargrave.org/meta4ra"
33 )
34
35 func main() {
36         hashes := flag.String("hashes", strings.Join(meta4ra.HashesDefault, ","), "hash-name:command-s")
37         extractSig := flag.Bool("extract-sig", false, "Extract signature files")
38         log.SetFlags(log.Lshortfile)
39         flag.Parse()
40         bad := false
41         for _, metaPath := range flag.Args() {
42                 data, err := os.ReadFile(metaPath)
43                 if err != nil {
44                         log.Fatalln(err)
45                 }
46                 var meta meta4ra.Metalink
47                 err = xml.Unmarshal(data, &meta)
48                 if err != nil {
49                         log.Fatalln(err)
50                 }
51                 for _, f := range meta.Files {
52                         for _, sig := range f.Signature {
53                                 if !*extractSig {
54                                         continue
55                                 }
56                                 var fn string
57                                 switch sig.MediaType {
58                                 case meta4ra.SigMediaTypePGP:
59                                         fn = f.Name + ".asc"
60                                 case meta4ra.SigMediaTypeSSH:
61                                         fn = f.Name + ".sig"
62                                 }
63                                 if fn == "" {
64                                         continue
65                                 }
66                                 if err = os.WriteFile(
67                                         fn,
68                                         []byte(strings.TrimPrefix(sig.Signature, "\n")),
69                                         fs.FileMode(0666),
70                                 ); err != nil {
71                                         fmt.Println("Error:", f.Name, "can not save signature:", err)
72                                         bad = true
73                                 }
74                         }
75                         hasher := meta4ra.NewHasher(*hashes)
76                         var hashTheir string
77                         var hashName string
78                         for i, name := range hasher.Names {
79                                 for _, h := range f.Hashes {
80                                         if h.Type == name {
81                                                 hasher.Names = []string{name}
82                                                 hasher.Cmds = append(hasher.Cmds[:0], hasher.Cmds[i])
83                                                 hasher.Ins = append(hasher.Ins[:0], hasher.Ins[i])
84                                                 hasher.Outs = append(hasher.Outs[:0], hasher.Outs[i])
85                                                 hashName = name
86                                                 hashTheir = h.Hash
87                                                 goto HashFound
88                                         }
89                                 }
90                         }
91                         log.Fatalln("no common hashes found for:", f.Name)
92                 HashFound:
93                         fd, err := os.Open(f.Name)
94                         if err != nil {
95                                 continue
96                         }
97                         hasher.Start()
98                         _, err = io.Copy(hasher, bufio.NewReaderSize(fd, 1<<20))
99                         fd.Close()
100                         sums := hasher.Sums()
101                         if err != nil {
102                                 fmt.Println("Error:", f.Name, err)
103                                 bad = true
104                                 continue
105                         }
106                         hashOur := sums[0].Hash
107                         if hashOur == hashTheir {
108                                 fmt.Println(f.Name, hashName, "good")
109                         } else {
110                                 fmt.Println(
111                                         "Hash does not match:", f.Name, hashName,
112                                         "our:", hashOur,
113                                         "their:", hashTheir,
114                                 )
115                                 bad = true
116                                 continue
117                         }
118                 }
119         }
120         if bad {
121                 os.Exit(1)
122         }
123 }