#!/usr/bin/env bash # Runs inside the sign image (build/Dockerfile.sign), started by scripts/release.sh. # Adds the packages of /dist to the repositories of /site (deb, rpm, arch), keeps the newest KEEP # versions of each package, rebuilds the indexes and signs everything with the key in /keys. # Mounts: /site (the gh-pages tree, read-write), /keys (private.asc, read-only), # /dist (the checked release files, read-write: SHA256SUMS gets a detached signature). set -Eeuo pipefail SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd -P) SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")" # shellcheck source=scripts/lib.sh source "${SCRIPT_DIR}/lib.sh" SITE_DIR="${SITE_DIR:-/site}" KEY_FILE="${KEY_FILE:-/keys/private.asc}" DIST_DIR="${DIST_DIR:-/dist}" KEEP="${KEEP:-3}" SUITE="${SUITE:-stable}" COMPONENT="${COMPONENT:-main}" ORIGIN="${ORIGIN:-r4ven.me}" LABEL="${LABEL:-r4ven pkgs}" REPO_NAME="${REPO_NAME:-r4ven}" PUBLIC_KEY_NAME="${PUBLIC_KEY_NAME:-r4ven-pkgs.asc}" TAB=$'\t' usage() { cat << EOF Usage: ${SCRIPT_NAME} Runs in the sign container. Reads ${KEY_FILE} and the packages of ${DIST_DIR}, updates the deb, rpm and arch repositories of ${SITE_DIR} (newest ${KEEP} versions per package and architecture) and signs them. Writes ${SITE_DIR}/catalog.json for the landing page. EOF } if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then usage exit 0 fi WORK_DIR="" cleanup() { trap - SIGINT SIGHUP SIGTERM SIGQUIT ERR EXIT # A subshell (process substitution, pipeline) inherits the traps; only the main shell may remove the work dir. [[ "$BASHPID" == "$$" ]] || return 0 [[ -n "$WORK_DIR" && -d "$WORK_DIR" ]] && rm -rf "$WORK_DIR" return 0 } # shellcheck disable=SC2154 # rc is assigned inside the trap string itself trap 'rc=$?; cleanup; exit $rc' SIGINT SIGHUP SIGTERM SIGQUIT ERR EXIT [[ -f "$KEY_FILE" ]] || log_error "Private key ${KEY_FILE} not found" [[ -f "${SITE_DIR}/${PUBLIC_KEY_NAME}" ]] || log_error "Public key ${SITE_DIR}/${PUBLIC_KEY_NAME} not found" WORK_DIR=$(mktemp -d) export GNUPGHOME="${WORK_DIR}/sign" mkdir -m 700 "$GNUPGHOME" gpg --batch --quiet --import "$KEY_FILE" fingerprint=$(gpg --batch --list-secret-keys --with-colons | awk -F: '/^fpr:/ {print $10; exit}') [[ -n "$fingerprint" ]] || log_error "No secret key in ${KEY_FILE}" log_info "Signing key: ${fingerprint}" # The public key of the repositories must belong to the key that signs. public_fingerprint=$(gpg --batch --show-keys --with-colons "${SITE_DIR}/${PUBLIC_KEY_NAME}" | awk -F: '/^fpr:/ {print $10; exit}') [[ "$public_fingerprint" == "$fingerprint" ]] \ || log_error "Public key ${PUBLIC_KEY_NAME} (${public_fingerprint}) is not the signing key (${fingerprint})" # Package metadata comes from files the maintainer does not control, so names and architectures are # checked before they become paths or commands on the landing page. check_name() { [[ "$2" =~ ^[A-Za-z0-9][A-Za-z0-9+._-]*$ ]] || log_error "Invalid $1 in package metadata: '$2'" } check_arch() { [[ "$1" =~ ^[A-Za-z0-9_]+$ ]] || log_error "Invalid architecture in package metadata: '$1'" } # The binary (dearmored) form of the public key, for tools that want a .gpg file. gpg --batch --yes --dearmor --output "${SITE_DIR}/${PUBLIC_KEY_NAME%.asc}.gpg" "${SITE_DIR}/${PUBLIC_KEY_NAME}" gpg_sign() { gpg --batch --yes --local-user "$fingerprint" "$@"; } # Scanners: one TSV line per package file: name, version, arch, path (relative to /site), size, summary, url. scan_deb() { local f while IFS= read -r -d '' f; do dpkg-deb -W --showformat="\${Package}${TAB}\${Version}${TAB}\${Architecture}${TAB}${f}${TAB}\${Installed-Size}${TAB}" "$f" dpkg-deb -f "$f" Description Homepage | awk ' /^Description:/ {sub(/^Description: */, ""); d = $0} /^Homepage:/ {sub(/^Homepage: */, ""); u = $0} END {printf "%s\t%s\n", d, u}' done < <(find deb/pool -name '*.deb' -print0 2> /dev/null | sort -z || true) } scan_rpm() { local f while IFS= read -r -d '' f; do rpm -qp --nosignature --qf "%{NAME}${TAB}%{VERSION}-%{RELEASE}${TAB}%{ARCH}${TAB}${f}${TAB}%{SIZE}${TAB}%{SUMMARY}${TAB}%{URL}\n" "$f" | sed 's/(none)//' done < <(find rpm -name '*.rpm' -print0 2> /dev/null | sort -z || true) } scan_arch() { local f while IFS= read -r -d '' f; do bsdtar -xOf "$f" .PKGINFO | awk -F' = ' -v f="$f" -v OFS='\t' ' $1 == "pkgname" {n = $2} $1 == "pkgver" {v = $2} $1 == "arch" {a = $2} $1 == "size" {s = $2} $1 == "pkgdesc" {d = $2} $1 == "url" {u = $2} END {print n, v, a, f, s, d, u}' done < <(find arch -name '*.pkg.tar.zst' -print0 2> /dev/null | sort -z || true) } # Keeps the newest KEEP versions of each name+arch. Reads a scan on stdin. prune() { sort -t "$TAB" -k1,1 -k3,3 -k2,2Vr \ | awk -F'\t' -v keep="$KEEP" '{k = $1 FS $3} ++n[k] > keep {print $4}' \ | while IFS= read -r old; do log_info "Removed old version: ${old}" rm -f -- "$old" "${old}.sig" "${old}.sha256" done } cd "$SITE_DIR" shopt -s nullglob # 1. Put the release files into the trees: deb/pool/main///, rpm//, arch//. for f in "$DIST_DIR"/*.deb; do package=$(dpkg-deb -f "$f" Package) check_name "deb package name" "$package" check_arch "$(dpkg-deb -f "$f" Architecture)" case "$package" in lib*) prefix="${package:0:4}" ;; *) prefix="${package:0:1}" ;; esac install -Dm644 "$f" "deb/pool/${COMPONENT}/${prefix}/${package}/$(basename "$f")" log_info "Added $(basename "$f") to deb" done new_rpms=() for f in "$DIST_DIR"/*.rpm; do arch=$(rpm -qp --nosignature --qf '%{ARCH}' "$f") check_arch "$arch" check_name "rpm package name" "$(rpm -qp --nosignature --qf '%{NAME}' "$f")" install -Dm644 "$f" "rpm/${arch}/$(basename "$f")" new_rpms+=("rpm/${arch}/$(basename "$f")") log_info "Added $(basename "$f") to rpm" done for f in "$DIST_DIR"/*.pkg.tar.zst; do arch=$(bsdtar -xOf "$f" .PKGINFO | awk -F' = ' '$1 == "arch" {print $2}') check_arch "$arch" check_name "arch package name" "$(bsdtar -xOf "$f" .PKGINFO | awk -F' = ' '$1 == "pkgname" {print $2}')" [[ "$arch" == "any" ]] && arch=x86_64 install -Dm644 "$f" "arch/${arch}/$(basename "$f")" log_info "Added $(basename "$f") to arch" done # 2. Drop the old versions. scan_deb | prune scan_rpm | prune scan_arch | prune # 3. deb: indexes and the signed Release. if [[ -d deb/pool ]]; then ( cd deb dists="dists/${SUITE}" rm -rf "$dists" mapfile -t archs < <(find pool -name '*.deb' -exec dpkg-deb -f {} Architecture \; | sort -u | grep -vx all || true) [[ "${#archs[@]}" -gt 0 ]] || archs=(amd64) for arch in "${archs[@]}"; do index="${dists}/${COMPONENT}/binary-${arch}" mkdir -p "$index" apt-ftparchive --arch "$arch" packages pool > "${index}/Packages" gzip -9kn "${index}/Packages" xz -9k "${index}/Packages" log_info "deb ${index}: $(grep -c '^Package:' "${index}/Packages") package(s)" done apt-ftparchive \ -o "APT::FTPArchive::Release::Origin=${ORIGIN}" \ -o "APT::FTPArchive::Release::Label=${LABEL}" \ -o "APT::FTPArchive::Release::Suite=${SUITE}" \ -o "APT::FTPArchive::Release::Codename=${SUITE}" \ -o "APT::FTPArchive::Release::Architectures=${archs[*]}" \ -o "APT::FTPArchive::Release::Components=${COMPONENT}" \ -o "APT::FTPArchive::Release::Description=Packages of r4ven.me" \ release "$dists" > "${WORK_DIR}/Release" mv "${WORK_DIR}/Release" "${dists}/Release" gpg_sign --clearsign --output "${dists}/InRelease" "${dists}/Release" gpg_sign --armor --detach-sign --output "${dists}/Release.gpg" "${dists}/Release" ) fi # 4. rpm: sign the new packages, build repodata/ per architecture, sign repomd.xml. for dir in rpm/*/; do dir="${dir%/}" for rpm in "${new_rpms[@]}"; do [[ "$(dirname "$rpm")" == "$dir" && -f "$rpm" ]] || continue rpmsign --define "_gpg_name ${fingerprint}" --addsign "$rpm" > /dev/null done rm -rf "${dir}/repodata" createrepo_c --quiet "$dir" gpg_sign --armor --detach-sign --output "${dir}/repodata/repomd.xml.asc" "${dir}/repodata/repomd.xml" log_info "rpm ${dir}: indexed and signed" done # 5. arch: a detached signature per package, then the signed database. Pages cannot serve # symlinks reliably, so the names pacman asks for (.db, .files) are plain copies. for dir in arch/*/; do dir="${dir%/}" rm -f "${dir}/${REPO_NAME}".db* "${dir}/${REPO_NAME}".files* for pkg in "$dir"/*.pkg.tar.zst; do [[ -f "${pkg}.sig" ]] || gpg_sign --detach-sign --output "${pkg}.sig" "$pkg" done (cd "$dir" && repo-add --quiet --sign --key "$fingerprint" "${REPO_NAME}.db.tar.zst" ./*.pkg.tar.zst) for kind in db files; do rm -f "${dir}/${REPO_NAME}.${kind}" "${dir}/${REPO_NAME}.${kind}.sig" cp "${dir}/${REPO_NAME}.${kind}.tar.zst" "${dir}/${REPO_NAME}.${kind}" cp "${dir}/${REPO_NAME}.${kind}.tar.zst.sig" "${dir}/${REPO_NAME}.${kind}.sig" done rm -f "${dir}/${REPO_NAME}".*.old "${dir}/${REPO_NAME}".*.old.sig log_info "arch ${dir}: database built and signed" done # 5b. A checksum file next to every package (published bytes, after signing), for those who download by hand. while IFS= read -r -d '' pkg; do (cd "$(dirname "$pkg")" && sha256sum "$(basename "$pkg")" > "$(basename "$pkg").sha256") done < <({ find deb rpm arch -type f \( -name '*.deb' -o -name '*.rpm' -o -name '*.pkg.tar.zst' \) -print0 2> /dev/null || true; }) # 6. The release gets the same bytes as the site: the signed packages, the .sig of Arch packages, # a new SHA256SUMS over them and its signature. The result is in ${DIST_DIR}/signed. signed_dir="${DIST_DIR}/signed" if [[ -f "${DIST_DIR}/SHA256SUMS" ]]; then rm -rf "$signed_dir" mkdir "$signed_dir" for src in "$DIST_DIR"/*.deb "$DIST_DIR"/*.rpm "$DIST_DIR"/*.pkg.tar.zst "$DIST_DIR"/*.apk; do [[ -f "$src" ]] || continue name=$(basename "$src") found=() while IFS= read -r -d '' path; do found+=("$path") done < <({ find deb rpm arch apk -type f -name "$name" -print0 2> /dev/null || true; }) if [[ "${#found[@]}" -eq 0 ]]; then log_warn "${name} is no longer in the repository (older than the newest ${KEEP} versions): not added to the release" continue fi [[ "${#found[@]}" -eq 1 ]] || log_error "${name} is in the repository more than once" cp "${found[0]}" "${signed_dir}/${name}" [[ ! -f "${found[0]}.sig" ]] || cp "${found[0]}.sig" "${signed_dir}/${name}.sig" done (cd "$signed_dir" && find . -type f ! -name SHA256SUMS -printf '%f\n' | LC_ALL=C sort | xargs -r sha256sum > SHA256SUMS) gpg_sign --armor --detach-sign --output "${signed_dir}/SHA256SUMS.asc" "${signed_dir}/SHA256SUMS" log_info "Release files and SHA256SUMS signed" fi # 7. The catalog for the landing page. { scan_deb | sed "s/^/deb${TAB}/" scan_rpm | sed "s/^/rpm${TAB}/" scan_arch | sed "s/^/arch${TAB}/" } | jq -R -s --arg fp "$fingerprint" --arg key "$PUBLIC_KEY_NAME" ' {fingerprint: $fp, key: $key, packages: [ split("\n")[] | select(length > 0) | split("\t") | {format: .[0], name: .[1], version: .[2], arch: .[3], file: .[4], size: (.[5] | tonumber? // 0), summary: .[6], url: .[7]} ]}' > catalog.json # 8. Check the result the way a client does: only the published public key, a clean keyring. verify_home="${WORK_DIR}/verify" mkdir -m 700 "$verify_home" verify() { GNUPGHOME="$verify_home" gpg --batch "$@" 2> /dev/null; } verify --quiet --import "${SITE_DIR}/${PUBLIC_KEY_NAME}" if [[ -f "deb/dists/${SUITE}/InRelease" ]]; then verify --verify "deb/dists/${SUITE}/InRelease" || log_error "deb InRelease does not verify with the published key" verify --verify "deb/dists/${SUITE}/Release.gpg" "deb/dists/${SUITE}/Release" || log_error "deb Release.gpg does not verify" fi for dir in rpm/*/; do verify --verify "${dir}repodata/repomd.xml.asc" "${dir}repodata/repomd.xml" || log_error "rpm ${dir} repomd.xml does not verify" done rpm_db="${WORK_DIR}/rpmdb" for rpm in rpm/*/*.rpm; do [[ -d "$rpm_db" ]] || rpm --dbpath "$rpm_db" --import "${SITE_DIR}/${PUBLIC_KEY_NAME}" rpm --dbpath "$rpm_db" -K "$rpm" | grep -q 'signatures OK' || log_error "rpm ${rpm} does not verify with the published key" done for dir in arch/*/; do for f in "${dir}${REPO_NAME}.db" "$dir"*.pkg.tar.zst; do verify --verify "${f}.sig" "$f" || log_error "arch ${f} does not verify with the published key" done done if [[ -f "${signed_dir}/SHA256SUMS.asc" ]]; then verify --verify "${signed_dir}/SHA256SUMS.asc" "${signed_dir}/SHA256SUMS" || log_error "SHA256SUMS.asc does not verify" (cd "$signed_dir" && sha256sum --check --quiet SHA256SUMS) || log_error "The release files do not match the new SHA256SUMS" # Every release file is a byte copy of a site file, so a site .sha256 must equal the release hash. while read -r hash name; do site_file=$(find deb rpm arch apk -type f -name "${name}.sha256" 2> /dev/null | head -n 1 || true) [[ -z "$site_file" || "$(cut -d' ' -f1 "$site_file")" == "$hash" ]] || log_error "${name}: the release hash differs from the site" done < "${signed_dir}/SHA256SUMS" fi log_info "Done: the repositories are signed and verified"