| 1 | // Build an isolated file repository and install its signed packages in a new SDK. |
| 2 | module main |
| 3 | |
| 4 | import encoding.base64 |
| 5 | import x.json2 |
| 6 | import net.urllib |
| 7 | import os |
| 8 | |
| 9 | fn output(argv []string) !string { |
| 10 | result := os.exec(argv) |
| 11 | if result.exit_code != 0 { |
| 12 | return error('${argv[0]} exited ${result.exit_code}: ${result.output}') |
| 13 | } |
| 14 | return result.output |
| 15 | } |
| 16 | |
| 17 | fn gpg(keyring string, args []string) !string { |
| 18 | mut cmd := ['gpg', '--homedir', keyring, '--batch', '--no-tty'] |
| 19 | cmd << args |
| 20 | return output(cmd) |
| 21 | } |
| 22 | |
| 23 | fn trust_key(state string, key string, fingerprint string) ! { |
| 24 | trust := os.join_path(state, 'verify') |
| 25 | os.mkdir(trust, mode: 0o700)! |
| 26 | gpg(trust, ['--import', key])! |
| 27 | records := gpg(trust, ['--with-colons', '--fingerprint', '--list-keys'])!.split_into_lines() |
| 28 | fingerprints := records.filter(it.starts_with('fpr:')) |
| 29 | if records.filter(it.starts_with('pub:')).len != 1 || fingerprints.len == 0 |
| 30 | || fingerprints[0].split(':')[9] != fingerprint { |
| 31 | return error('public key does not match the expected test signer fingerprint') |
| 32 | } |
| 33 | ownertrust := os.join_path(trust, 'fixture-ownertrust') |
| 34 | os.write_file(ownertrust, fingerprint + ':6:\n')! |
| 35 | gpg(trust, ['--import-ownertrust', ownertrust])! |
| 36 | } |
| 37 | |
| 38 | // Keep private signing material out of every repository and consumer root. |
| 39 | fn keys(state string) ! { |
| 40 | os.mkdir(state, mode: 0o700)! |
| 41 | signer := os.join_path(state, 'signing') |
| 42 | os.mkdir(signer, mode: 0o700)! |
| 43 | gpg(signer, ['--pinentry-mode', 'loopback', '--passphrase', '', '--quick-generate-key', |
| 44 | 'Vinix ports test signer <ports-test@example.invalid>', 'ed25519', 'sign', '0'])! |
| 45 | records := gpg(signer, ['--with-colons', '--fingerprint', '--list-keys'])!.split_into_lines() |
| 46 | fingerprints := records.filter(it.starts_with('fpr:')) |
| 47 | if fingerprints.len != 1 { |
| 48 | return error('expected one signing fingerprint in the new keyring') |
| 49 | } |
| 50 | fingerprint := fingerprints[0].split(':')[9] |
| 51 | os.write_file(os.join_path(state, 'signer.fingerprint'), fingerprint + '\n')! |
| 52 | public_key := os.join_path(state, 'signer.asc') |
| 53 | gpg(signer, ['--output', public_key, '--armor', '--export', fingerprint])! |
| 54 | trust_key(state, public_key, fingerprint)! |
| 55 | } |
| 56 | |
| 57 | fn metadata_value(raw string, key string) !string { |
| 58 | prefix := key + ' = ' |
| 59 | values := raw.split_into_lines().filter(it.starts_with(prefix)) |
| 60 | if values.len != 1 { |
| 61 | return error('expected one ${key} in .PKGINFO') |
| 62 | } |
| 63 | return values[0][prefix.len..] |
| 64 | } |
| 65 | |
| 66 | fn publish(state string, repo string, arch string, archives []string) ! { |
| 67 | fingerprint := os.read_file(os.join_path(state, 'signer.fingerprint'))!.trim_space() |
| 68 | signer := os.join_path(state, 'signing') |
| 69 | trust := os.join_path(state, 'verify') |
| 70 | os.mkdir(repo)! |
| 71 | stage := os.join_path(repo, '.staging') |
| 72 | os.mkdir(stage)! |
| 73 | mut names := map[string]bool{} |
| 74 | mut staged := []string{} |
| 75 | mut receipts := ['name\tversion\tarch\tarchive\tsha256'] |
| 76 | mut signatures := map[string]string{} |
| 77 | for archive in archives { |
| 78 | name := os.file_name(archive) |
| 79 | target := os.join_path(stage, name) |
| 80 | if !name.ends_with('.pkg.tar.zst') || os.exists(target) { |
| 81 | return error('expected a distinct .pkg.tar.zst archive, got ${archive}') |
| 82 | } |
| 83 | os.cp(archive, target)! |
| 84 | raw := output(['bsdtar', '-xOf', target, '.PKGINFO'])! |
| 85 | id := metadata_value(raw, 'pkgname')! |
| 86 | pkg_arch := metadata_value(raw, 'arch')! |
| 87 | if id in names || pkg_arch !in [arch, 'any'] { |
| 88 | return error('duplicate package name or incompatible architecture: ${name}') |
| 89 | } |
| 90 | names[id] = true |
| 91 | digest := output(['sha256sum', target])!.all_before(' ') |
| 92 | version := metadata_value(raw, 'pkgver')! |
| 93 | receipts << [id, version, pkg_arch, name, digest].join('\t') |
| 94 | gpg(signer, ['--pinentry-mode', 'loopback', '--passphrase', '', '--local-user', fingerprint, |
| 95 | '--output', target + '.sig', '--detach-sign', target])! |
| 96 | gpg(trust, ['--verify', target + '.sig', target])! |
| 97 | signatures['${id}-${version}/desc'] = base64.encode(os.read_bytes(target + '.sig')!) |
| 98 | staged << target |
| 99 | } |
| 100 | mut repo_add := ['env', 'GNUPGHOME=' + signer, 'repo-add', '--nocolor', '--sign'] |
| 101 | // Pacman 6 embeds signatures by default. Pacman 7 accepts an explicit flag. |
| 102 | if output(['repo-add', '--help'])!.contains('--include-sigs') { |
| 103 | repo_add << '--include-sigs' |
| 104 | } |
| 105 | db := os.join_path(stage, 'vinix.db.tar.gz') |
| 106 | repo_add << ['--key', fingerprint, db] |
| 107 | repo_add << staged |
| 108 | print(output(repo_add)!) |
| 109 | for member, signature in signatures { |
| 110 | if !output(['bsdtar', '-xOf', db, member])!.contains('\n%PGPSIG%\n' + signature + '\n') { |
| 111 | return error('signed database is missing the package signature in ${member}') |
| 112 | } |
| 113 | } |
| 114 | for index in ['vinix.db', 'vinix.files'] { |
| 115 | path := os.join_path(stage, index) |
| 116 | gpg(trust, ['--verify', path + '.sig', path])! |
| 117 | } |
| 118 | os.cp(os.join_path(state, 'signer.asc'), os.join_path(repo, 'test-signer.asc'))! |
| 119 | os.cp(os.join_path(state, 'signer.fingerprint'), os.join_path(repo, 'test-signer.fingerprint'))! |
| 120 | os.write_file(os.join_path(repo, 'packages.tsv'), receipts.join('\n') + '\n')! |
| 121 | os.rename(stage, os.join_path(repo, arch))! |
| 122 | } |
| 123 | |
| 124 | fn install(state string, repo string, arch string, client string, packages []string, fakeroot bool) ! { |
| 125 | if packages.any(it.len == 0 || it.starts_with('-') || it.contains('/')) { |
| 126 | return error('expected package names from the signed repository') |
| 127 | } |
| 128 | os.mkdir(client)! |
| 129 | for subdir in ['root', 'db/local', 'db/sync', 'cache', 'hooks'] { |
| 130 | os.mkdir_all(os.join_path(client, subdir))! |
| 131 | } |
| 132 | os.write_file(os.join_path(client, 'db/local/ALPM_DB_VERSION'), '9\n')! |
| 133 | config := os.join_path(client, 'config.vile') |
| 134 | url := 'file://' + os.join_path(repo, arch).split('/').map(urllib.path_escape(it)).join('/') |
| 135 | os.write_file(config, [ |
| 136 | 'path-root = ' + json2.encode(os.join_path(client, 'root') + '/'), |
| 137 | 'path-db = ' + json2.encode(os.join_path(client, 'db') + '/'), |
| 138 | 'path-cache = [' + json2.encode(os.join_path(client, 'cache') + '/') + ']', |
| 139 | 'path-gpg = ' + json2.encode(os.join_path(state, 'verify') + '/'), |
| 140 | 'path-log = ' + json2.encode(os.join_path(client, 'vpac.log')), |
| 141 | 'path-hook = [' + json2.encode(os.join_path(client, 'hooks') + '/') + ']', |
| 142 | 'pkg-arch = ' + json2.encode(arch), |
| 143 | 'network-siglevel = ["package", "database"]', |
| 144 | 'network-user = ""', |
| 145 | 'network-disable-sandbox = true', |
| 146 | 'repositories = list(sync-repo("vinix", [' + json2.encode(url) + ']))', |
| 147 | ].join('\n') + '\n')! |
| 148 | mut cmd := if fakeroot { ['fakeroot', '--'] } else { ['unshare', '-Urn'] } |
| 149 | cmd << [os.find_abs_path_of_executable('vpac')!, '--config', config, 'i', '--noconfirm'] |
| 150 | cmd << packages |
| 151 | result := os.exec(cmd) |
| 152 | os.write_file(os.join_path(client, 'install.log'), result.output)! |
| 153 | print(result.output) |
| 154 | if result.exit_code != 0 { |
| 155 | return error('vpac install exited ${result.exit_code}') |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | fn command(args []string) ! { |
| 160 | if args.len == 2 && args[0] == 'keys' { |
| 161 | keys(os.abs_path(args[1]))! |
| 162 | return |
| 163 | } |
| 164 | if args.len == 4 && args[0] == 'trust' { |
| 165 | state := os.abs_path(args[1]) |
| 166 | os.mkdir(state, mode: 0o700)! |
| 167 | trust_key(state, os.abs_path(args[2]), args[3])! |
| 168 | return |
| 169 | } |
| 170 | if args.len >= 5 && args[3] in ['x86_64', 'aarch64'] { |
| 171 | state, repo := os.abs_path(args[1]), os.abs_path(args[2]) |
| 172 | if args[0] == 'publish' { |
| 173 | publish(state, repo, args[3], args[4..])! |
| 174 | return |
| 175 | } |
| 176 | if args[0] == 'install' && args.len >= 6 { |
| 177 | fakeroot := args[5] == '--fakeroot' |
| 178 | first_package := if fakeroot { 6 } else { 5 } |
| 179 | packages := args[first_package..] |
| 180 | if packages.len == 0 { |
| 181 | return error('expected at least one package name') |
| 182 | } |
| 183 | install(state, repo, args[3], os.abs_path(args[4]), packages, fakeroot)! |
| 184 | return |
| 185 | } |
| 186 | } |
| 187 | eprintln('usage: feed keys NEW_KEY_STATE') |
| 188 | eprintln(' feed trust NEW_CLIENT_KEY_STATE PUBLIC_KEY EXPECTED_FINGERPRINT') |
| 189 | eprintln(' feed publish KEY_STATE NEW_REPO ARCH PACKAGE_ARCHIVE...') |
| 190 | eprintln(' feed install KEY_STATE REPO ARCH NEW_CLIENT [--fakeroot] PACKAGE_NAME...') |
| 191 | exit(2) |
| 192 | } |
| 193 | |
| 194 | fn main() { |
| 195 | command(os.args[1..]) or { |
| 196 | eprintln(err) |
| 197 | exit(1) |
| 198 | } |
| 199 | } |
| 200 | |