]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4ra/check.go
Ability to download URLs
[meta4ra.git] / cmd / meta4ra / check.go
1 // meta4ra -- Metalink 4.0 utilities
2 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "bufio"
20         "encoding/xml"
21         "flag"
22         "fmt"
23         "io"
24         "io/fs"
25         "log"
26         "net/http"
27         "os"
28         "path"
29         "strings"
30
31         meta4ra "go.stargrave.org/meta4ra/internal"
32 )
33
34 func runCheck() {
35         pipe := flag.Bool("pipe", false, "Verify file data from stdin, copy to stdout")
36         allHashes := flag.Bool("all-hashes", false, "Check all hashes, not the first common one")
37         hashes := flag.String("hashes", meta4ra.HashesDefault,
38                 "hash-name:commandline[,...]")
39         extractSig := flag.Bool("extract-sig", false, "Extract signature files")
40         metaPath := flag.String("meta4", "file.meta4", "Metalink file")
41         dl := flag.Int("dl", -1, "URL index to download, instead of reading from stdin")
42         flag.Usage = func() {
43                 fmt.Fprintf(flag.CommandLine.Output(),
44                         "Usage: %s [options] [FILE ...]\n", os.Args[0])
45                 flag.PrintDefaults()
46                 fmt.Fprint(flag.CommandLine.Output(), `
47 If no FILEs are specified, then all <file>s from metalink are searched and
48 verified if they exist. Otherwise only specified FILEs are checked. If you
49 want to skip any <file> verification (for example only to validate the format
50 and -extract-sig, then you can just specify an empty ("") FILE.
51
52 If -dl (> 0) is specified, then it automatically implies -pipe, but
53 downloads data by specified URLs index, instead of reading from stdin.
54 That can be used as a downloading utility.
55 `)
56         }
57         flag.Parse()
58
59         if *showVersion {
60                 fmt.Println(meta4ra.Version())
61                 return
62         }
63         if *showWarranty {
64                 fmt.Println(meta4ra.Warranty)
65                 return
66         }
67
68         data, err := os.ReadFile(*metaPath)
69         if err != nil {
70                 log.Fatalln(err)
71         }
72         var meta meta4ra.Metalink
73         err = xml.Unmarshal(data, &meta)
74         if err != nil {
75                 log.Fatalln(err)
76         }
77
78         toCheck := make(map[string]string)
79         for _, fn := range flag.Args() {
80                 toCheck[path.Base(fn)] = fn
81         }
82         if *dl != -1 {
83                 *pipe = true
84         }
85         if *pipe && len(toCheck) != 1 {
86                 log.Fatalln("exactly single FILE must be specified when using -pipe")
87         }
88
89         bad := false
90         for _, f := range meta.Files {
91                 for _, sig := range f.Signature {
92                         if !*extractSig {
93                                 continue
94                         }
95                         var fn string
96                         switch sig.MediaType {
97                         case meta4ra.SigMediaTypePGP:
98                                 fn = f.Name + ".asc"
99                         case meta4ra.SigMediaTypeSSH:
100                                 fn = f.Name + ".sig"
101                         }
102                         if fn == "" {
103                                 continue
104                         }
105                         if err = os.WriteFile(
106                                 fn,
107                                 []byte(strings.TrimPrefix(sig.Signature, "\n")),
108                                 fs.FileMode(0666),
109                         ); err != nil {
110                                 log.Println("Error:", f.Name, "can not save signature:", err)
111                                 bad = true
112                         }
113                 }
114
115                 fullPath := toCheck[f.Name]
116                 delete(toCheck, f.Name)
117                 if !(len(toCheck) == 0 || fullPath != "") {
118                         continue
119                 }
120                 if fullPath == "" {
121                         fullPath = f.Name
122                 }
123                 if !*pipe {
124                         s, err := os.Stat(fullPath)
125                         if err != nil {
126                                 log.Println(err)
127                                 bad = true
128                                 continue
129                         }
130                         if uint64(s.Size()) != f.Size {
131                                 log.Println("size mismatch",
132                                         f.Name, "our:", s.Size(), "their:", f.Size)
133                                 bad = true
134                                 continue
135                         }
136                 }
137
138                 hasher, err := meta4ra.NewHasher(*hashes)
139                 if err != nil {
140                         log.Println(f.Name, err)
141                         bad = true
142                         continue
143                 }
144                 var hashTheir string
145                 var hashName string
146                 if *allHashes {
147                         goto HashFound
148                 }
149                 for i, name := range hasher.Names {
150                         for _, h := range f.Hashes {
151                                 if h.Type == name {
152                                         hasher.Names = []string{name}
153                                         hasher.Cmds = append(hasher.Cmds[:0], hasher.Cmds[i])
154                                         hasher.Ins = append(hasher.Ins[:0], hasher.Ins[i])
155                                         hasher.Outs = append(hasher.Outs[:0], hasher.Outs[i])
156                                         hashName = name
157                                         hashTheir = h.Hash
158                                         goto HashFound
159                                 }
160                         }
161                 }
162                 log.Println("no common hashes found for:", f.Name)
163                 bad = true
164                 continue
165         HashFound:
166
167                 var src io.ReadCloser
168                 src = os.Stdin
169                 if !*pipe {
170                         src, err = os.Open(fullPath)
171                         if err != nil {
172                                 log.Println("Error:", f.Name, err)
173                                 bad = true
174                                 continue
175                         }
176                 }
177                 if *dl != -1 {
178                         resp, err := http.Get(f.URLs[*dl].URL)
179                         if err != nil {
180                                 log.Println("Error:", f.Name, err)
181                                 bad = true
182                                 continue
183                         }
184                         log.Println("HTTP response:")
185                         for k := range resp.Header {
186                                 log.Println("\t"+k+":", resp.Header.Get(k))
187                         }
188                         if resp.StatusCode != http.StatusOK {
189                                 log.Println("Bad status code:", f.Name, resp.Status)
190                                 bad = true
191                                 continue
192                         }
193                         src = resp.Body
194                 }
195                 err = hasher.Start()
196                 if err != nil {
197                         if !*pipe {
198                                 src.Close()
199                         }
200                         hasher.Stop()
201                         log.Println("Error:", f.Name, err)
202                         bad = true
203                         continue
204                 }
205                 var w io.Writer
206                 if *pipe {
207                         w = io.MultiWriter(os.Stdout, hasher)
208                 } else {
209                         w = hasher
210                 }
211                 _, err = io.Copy(w, bufio.NewReaderSize(src, meta4ra.BufLen))
212                 if !*pipe || *dl != -1 {
213                         src.Close()
214                 }
215                 if err != nil {
216                         hasher.Stop()
217                         log.Println("Error:", f.Name, err)
218                         bad = true
219                         continue
220                 }
221                 sums, err := hasher.Sums()
222                 if err != nil {
223                         hasher.Stop()
224                         log.Println("Error:", f.Name, err)
225                         bad = true
226                         continue
227                 }
228                 if *allHashes {
229                         hashesOur := make(map[string]string, len(sums))
230                         for _, h := range sums {
231                                 hashesOur[h.Type] = h.Hash
232                         }
233                         for _, h := range f.Hashes {
234                                 hashOur := hashesOur[h.Type]
235                                 if h.Hash == hashOur {
236                                         if !*pipe {
237                                                 fmt.Fprintln(os.Stderr, f.Name, h.Type, "good")
238                                         }
239                                 } else {
240                                         log.Println(
241                                                 "hash mismatch:", f.Name, h.Type,
242                                                 "our:", hashOur,
243                                                 "their:", h.Hash,
244                                         )
245                                         bad = true
246                                 }
247                         }
248                 } else {
249                         hashOur := sums[0].Hash
250                         if hashOur == hashTheir {
251                                 if !*pipe {
252                                         fmt.Fprintln(os.Stderr, f.Name, hashName, "good")
253                                 }
254                         } else {
255                                 log.Println(
256                                         "hash mismatch:", f.Name, hashName,
257                                         "our:", hashOur,
258                                         "their:", hashTheir,
259                                 )
260                                 bad = true
261                                 continue
262                         }
263                 }
264         }
265         if len(toCheck) != 0 {
266                 if _, ok := toCheck["."]; !(len(toCheck) == 1 && ok) {
267                         log.Println("not all FILEs met")
268                         bad = true
269                 }
270         }
271         if bad {
272                 os.Exit(1)
273         }
274 }