78 lines
3.3 KiB
Python
Executable File
78 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Validate and activate a frozen local APT repository (no external fallback)."""
|
|
import hashlib
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
|
|
def verify(bundle):
|
|
bundle = Path(bundle).resolve()
|
|
if not re.fullmatch(r'[A-Za-z0-9_./-]+', str(bundle)):
|
|
raise ValueError('Bundle path must not contain spaces or shell metacharacters')
|
|
entries = {}
|
|
for line in (bundle / 'SHA256SUMS').read_text().splitlines():
|
|
digest, relative = line.split(' ', 1)
|
|
path = bundle / relative
|
|
if not re.fullmatch('[a-f0-9]{64}', digest) or path.is_symlink() or not path.resolve().is_relative_to(bundle):
|
|
raise ValueError('Unsafe manifest entry: ' + relative)
|
|
if hashlib.sha256(path.read_bytes()).hexdigest() != digest:
|
|
raise ValueError('Checksum mismatch: ' + relative)
|
|
entries[relative] = digest
|
|
for required in ['platform', 'repo/Packages', 'package-versions.txt', 'requested-packages.txt']:
|
|
if required not in entries:
|
|
raise ValueError('Missing manifest entry: ' + required)
|
|
for path in (bundle / 'repo').glob('*.deb'):
|
|
if 'repo/' + path.name not in entries:
|
|
raise ValueError('Unlisted package: ' + path.name)
|
|
if (bundle / 'platform').read_text().splitlines() != ['bullseye', 'arm64']:
|
|
raise ValueError('Unsupported bundle platform')
|
|
return bundle
|
|
|
|
|
|
def activate(bundle):
|
|
bundle = verify(bundle)
|
|
if os.geteuid() != 0:
|
|
raise PermissionError('Run as root')
|
|
if subprocess.check_output(['dpkg', '--print-architecture'], text=True).strip() != 'arm64':
|
|
raise ValueError('This bundle requires Debian 11 arm64')
|
|
if 'VERSION_CODENAME=bullseye' not in Path('/etc/os-release').read_text():
|
|
raise ValueError('This bundle requires Debian 11 bullseye')
|
|
apt = Path('/etc/apt')
|
|
config = apt / 'apt.conf.d/99iomatex-offline'
|
|
if not config.exists():
|
|
backup = apt / ('iomatex-before-offline-' + time.strftime('%Y%m%d-%H%M%S'))
|
|
backup.mkdir(mode=0o700)
|
|
for name in ['sources.list', 'sources.list.d', 'apt.conf.d']:
|
|
src = apt / name
|
|
if src.is_dir():
|
|
shutil.copytree(src, backup / name)
|
|
elif src.exists():
|
|
shutil.copy2(src, backup / name)
|
|
# trusted=yes is restricted to this checksum-verified, local file repository.
|
|
source = apt / 'iomatex-offline.list'
|
|
source.write_text('deb [trusted=yes] file:' + str(bundle / 'repo') + ' ./\n')
|
|
cache = Path('/userdata/iomatex-apt-cache')
|
|
cache.mkdir(parents=True, exist_ok=True)
|
|
(cache / 'partial').mkdir(exist_ok=True)
|
|
config.write_text('Dir::Etc::sourcelist "' + str(source) + '";\n'
|
|
'Dir::Etc::sourceparts "-";\n'
|
|
'Dir::Cache::archives "' + str(cache) + '/";\n'
|
|
'APT::Install-Recommends "false";\n'
|
|
'APT::Install-Suggests "false";\n'
|
|
'Acquire::Languages "none";\n')
|
|
print('Local-only APT activated:', bundle)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) == 3 and sys.argv[1] == '--verify':
|
|
print('Verified:', verify(sys.argv[2]))
|
|
elif len(sys.argv) == 2:
|
|
activate(sys.argv[1])
|
|
else:
|
|
sys.exit('Usage: activate.py [--verify] /path/to/bundle')
|