| 1 | module vmakepkg |
| 2 | |
| 3 | import crypto.blake2b |
| 4 | import crypto.sha256 |
| 5 | import os |
| 6 | |
| 7 | fn file_digests(path string) !(string, string) { |
| 8 | mut f := os.open(path)! |
| 9 | defer { f.close() } |
| 10 | mut sha := sha256.new() |
| 11 | mut b2 := blake2b.new512()! |
| 12 | mut buf := []u8{len: 65536} |
| 13 | for { |
| 14 | n := f.read(mut buf) or { |
| 15 | if err is os.Eof { break } |
| 16 | return err |
| 17 | } |
| 18 | if n == 0 { break } |
| 19 | sha.write(buf[..n])! |
| 20 | b2.write(buf[..n])! |
| 21 | } |
| 22 | return sha.sum([]).hex(), b2.checksum().hex() |
| 23 | } |
| 24 | |
| 25 | fn verify_source_signature(sig string, keys []string) ! { |
| 26 | data := sig.all_before_last('.') |
| 27 | if !os.is_file(data) { return error('missing signed source for ${sig}') } |
| 28 | // keep machine-readable signature records apart from human diagnostics. |
| 29 | dir := private_dir(os.dir(sig))! |
| 30 | defer { os.rmdir_all(dir) or { eprintln('remove signature status: ${err}') } } |
| 31 | file := os.join_path(dir, 'status') |
| 32 | run(['gpg', '--batch', '--status-file', file, '--verify', '--', sig, data])! |
| 33 | status := os.read_file(file)! |
| 34 | for line in status.split_into_lines() { |
| 35 | if keys.len == 0 { |
| 36 | if line.starts_with('[GNUPG:] TRUST_FULLY ') || line.starts_with('[GNUPG:] TRUST_ULTIMATE ') { |
| 37 | return |
| 38 | } |
| 39 | } else if line.starts_with('[GNUPG:] VALIDSIG ') { |
| 40 | fields := line.fields() |
| 41 | if fields.len >= 3 && (fields[2] in keys || fields.last() in keys) { return } |
| 42 | } |
| 43 | } |
| 44 | return error('source signature has no trusted signing key: ${sig}') |
| 45 | } |
| 46 | |
| 47 | fn native_sources(spec Spec, start string, src string) ! { |
| 48 | if spec.source.len == 0 && spec.b2sums.len == 0 && spec.sha256sums.len == 0 { return } |
| 49 | if spec.sha256sums.len == 0 && spec.b2sums.len == 0 { |
| 50 | return error('sources require sha256sums or b2sums') |
| 51 | } |
| 52 | for sums in [spec.sha256sums, spec.b2sums] { |
| 53 | if sums.len > 0 && sums.len != spec.source.len { |
| 54 | return error('source and checksum counts differ') |
| 55 | } |
| 56 | } |
| 57 | store := os.join_path(os.dir(src), 'sources') |
| 58 | os.mkdir(store)! |
| 59 | mut names := []string{} |
| 60 | mut signed := []string{} |
| 61 | for i, entry in spec.source { |
| 62 | uri := entry.all_after('::') |
| 63 | name := if entry.contains('::') { |
| 64 | entry.all_before('::') |
| 65 | } else { |
| 66 | os.base(uri.all_before('?').all_before('#')).trim_string_right(if uri.starts_with('git+') { |
| 67 | '.git' |
| 68 | } else { |
| 69 | '' |
| 70 | }) |
| 71 | } |
| 72 | if name in ['', '.', '..'] || name.contains('/') || name.bytes().any(it < 32) || name in names { |
| 73 | return error('invalid or duplicate source filename: ${name}') |
| 74 | } |
| 75 | names << name |
| 76 | path := os.join_path(store, name) |
| 77 | if uri.starts_with('git+') { |
| 78 | if (spec.sha256sums.len > 0 && spec.sha256sums[i] != 'SKIP') |
| 79 | || (spec.b2sums.len > 0 && spec.b2sums[i] != 'SKIP') { |
| 80 | return error('git sources require SKIP checksums') |
| 81 | } |
| 82 | url := uri[4..].all_before('#') |
| 83 | if !url.starts_with('https://') && !url.starts_with('file://') { |
| 84 | return error('unsupported git transport: ${url}') |
| 85 | } |
| 86 | run(['git', 'clone', '--', url, os.join_path(src, name)])! |
| 87 | if uri.contains('#') { |
| 88 | ref := uri.all_after('#') |
| 89 | if ref.all_before('=') !in ['tag', 'branch', 'commit'] || ref.all_after('=') == '' || ref.all_after('=').starts_with('-') { |
| 90 | return error('invalid git source reference: ${ref}') |
| 91 | } |
| 92 | run(['git', '-C', os.join_path(src, name), 'checkout', '--detach', ref.all_after('='), |
| 93 | '--'])! |
| 94 | } |
| 95 | continue |
| 96 | } |
| 97 | if uri.starts_with('https://') || uri.starts_with('http://') { |
| 98 | run(['curl', '--fail', '--location', '--proto', '=http,https', '--proto-redir', |
| 99 | '=http,https', '--output', path, '--', uri])! |
| 100 | } else { |
| 101 | if uri.contains('://') { return error('unsupported source URL: ${uri}') } |
| 102 | os.cp(os.join_path(start, uri), path)! |
| 103 | } |
| 104 | sha, b2 := file_digests(path)! |
| 105 | if spec.sha256sums.len > 0 && spec.sha256sums[i] != 'SKIP' && spec.sha256sums[i].to_lower() != sha { |
| 106 | return error('sha256 mismatch for ${name}') |
| 107 | } |
| 108 | if spec.b2sums.len > 0 && spec.b2sums[i] != 'SKIP' && spec.b2sums[i].to_lower() != b2 { |
| 109 | return error('b2sum mismatch for ${name}') |
| 110 | } |
| 111 | if name.ends_with('.sig') || name.ends_with('.asc') { signed << path } |
| 112 | } |
| 113 | for sig in signed { verify_source_signature(sig, spec.validpgpkeys)! } |
| 114 | for name in names { |
| 115 | path := os.join_path(store, name) |
| 116 | if !os.exists(path) { continue } |
| 117 | os.cp(path, os.join_path(src, name))! |
| 118 | } |
| 119 | for name in names { |
| 120 | path := os.join_path(store, name) |
| 121 | if !os.exists(path) { continue } |
| 122 | if ['.tar', '.tgz', '.tbz2', '.txz', '.zip'].any(name.ends_with(it)) || name.contains('.tar.') { |
| 123 | run(['bsdtar', '-xf', path, '-C', src])! |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |