module vmakepkg import crypto.blake2b import crypto.sha256 import os fn file_digests(path string) !(string, string) { mut f := os.open(path)! defer { f.close() } mut sha := sha256.new() mut b2 := blake2b.new512()! mut buf := []u8{len: 65536} for { n := f.read(mut buf) or { if err is os.Eof { break } return err } if n == 0 { break } sha.write(buf[..n])! b2.write(buf[..n])! } return sha.sum([]).hex(), b2.checksum().hex() } fn verify_source_signature(sig string, keys []string) ! { data := sig.all_before_last('.') if !os.is_file(data) { return error('missing signed source for ${sig}') } // keep machine-readable signature records apart from human diagnostics. dir := private_dir(os.dir(sig))! defer { os.rmdir_all(dir) or { eprintln('remove signature status: ${err}') } } file := os.join_path(dir, 'status') run(['gpg', '--batch', '--status-file', file, '--verify', '--', sig, data])! status := os.read_file(file)! for line in status.split_into_lines() { if keys.len == 0 { if line.starts_with('[GNUPG:] TRUST_FULLY ') || line.starts_with('[GNUPG:] TRUST_ULTIMATE ') { return } } else if line.starts_with('[GNUPG:] VALIDSIG ') { fields := line.fields() if fields.len >= 3 && (fields[2] in keys || fields.last() in keys) { return } } } return error('source signature has no trusted signing key: ${sig}') } fn native_sources(spec Spec, start string, src string) ! { if spec.source.len == 0 && spec.b2sums.len == 0 && spec.sha256sums.len == 0 { return } if spec.sha256sums.len == 0 && spec.b2sums.len == 0 { return error('sources require sha256sums or b2sums') } for sums in [spec.sha256sums, spec.b2sums] { if sums.len > 0 && sums.len != spec.source.len { return error('source and checksum counts differ') } } store := os.join_path(os.dir(src), 'sources') os.mkdir(store)! mut names := []string{} mut signed := []string{} for i, entry in spec.source { uri := entry.all_after('::') name := if entry.contains('::') { entry.all_before('::') } else { os.base(uri.all_before('?').all_before('#')).trim_string_right(if uri.starts_with('git+') { '.git' } else { '' }) } if name in ['', '.', '..'] || name.contains('/') || name.bytes().any(it < 32) || name in names { return error('invalid or duplicate source filename: ${name}') } names << name path := os.join_path(store, name) if uri.starts_with('git+') { if (spec.sha256sums.len > 0 && spec.sha256sums[i] != 'SKIP') || (spec.b2sums.len > 0 && spec.b2sums[i] != 'SKIP') { return error('git sources require SKIP checksums') } url := uri[4..].all_before('#') if !url.starts_with('https://') && !url.starts_with('file://') { return error('unsupported git transport: ${url}') } run(['git', 'clone', '--', url, os.join_path(src, name)])! if uri.contains('#') { ref := uri.all_after('#') if ref.all_before('=') !in ['tag', 'branch', 'commit'] || ref.all_after('=') == '' || ref.all_after('=').starts_with('-') { return error('invalid git source reference: ${ref}') } run(['git', '-C', os.join_path(src, name), 'checkout', '--detach', ref.all_after('='), '--'])! } continue } if uri.starts_with('https://') || uri.starts_with('http://') { run(['curl', '--fail', '--location', '--proto', '=http,https', '--proto-redir', '=http,https', '--output', path, '--', uri])! } else { if uri.contains('://') { return error('unsupported source URL: ${uri}') } os.cp(os.join_path(start, uri), path)! } sha, b2 := file_digests(path)! if spec.sha256sums.len > 0 && spec.sha256sums[i] != 'SKIP' && spec.sha256sums[i].to_lower() != sha { return error('sha256 mismatch for ${name}') } if spec.b2sums.len > 0 && spec.b2sums[i] != 'SKIP' && spec.b2sums[i].to_lower() != b2 { return error('b2sum mismatch for ${name}') } if name.ends_with('.sig') || name.ends_with('.asc') { signed << path } } for sig in signed { verify_source_signature(sig, spec.validpgpkeys)! } for name in names { path := os.join_path(store, name) if !os.exists(path) { continue } os.cp(path, os.join_path(src, name))! } for name in names { path := os.join_path(store, name) if !os.exists(path) { continue } if ['.tar', '.tgz', '.tbz2', '.txz', '.zip'].any(name.ends_with(it)) || name.contains('.tar.') { run(['bsdtar', '-xf', path, '-C', src])! } } }