diff --git a/README.md b/README.md index 27257abb..e8820a15 100644 --- a/README.md +++ b/README.md @@ -1136,7 +1136,7 @@ Many users and organizations support setup-php via [GitHub Sponsors](https://git - [Node.js dependencies](https://github.com/shivammathur/setup-php/network/dependencies "Node.js dependencies") - [aaronparker/VcRedist](https://github.com/aaronparker/VcRedist "VcRedist PowerShell package") - [mlocati/powershell-phpmanager](https://github.com/mlocati/powershell-phpmanager "Package to handle PHP on windows") -- [ppa:ondrej/php](https://launchpad.net/~ondrej/+archive/ubuntu/php "Packaging active PHP packages") +- [Sury PHP packages](https://packages.sury.org/php/) and the [setup-php mirror](https://ppa.setup-php.com/sury/) - [shivammathur/cache-extensions](https://github.com/shivammathur/cache-extensions "GitHub action to help with caching PHP extensions") - [shivammathur/composer-cache](https://github.com/shivammathur/composer-cache "Cache composer releases") - [shivammathur/homebrew-extensions](https://github.com/shivammathur/homebrew-extensions "Tap for PHP extensions on MacOS") diff --git a/src/scripts/extensions/http.sh b/src/scripts/extensions/http.sh index 6880298e..0c50b990 100644 --- a/src/scripts/extensions/http.sh +++ b/src/scripts/extensions/http.sh @@ -75,7 +75,7 @@ add_http_latest() { if [ "$os" = "Linux" ]; then add_http_dependencies package="php$version-http" - add_ppa ondrej/php >/dev/null 2>&1 || update_ppa ondrej/php + add_ppa >/dev/null 2>&1 (check_package "$package" && install_packages "$package") || add_http_helper "$(get_http_version)" "$os" else if ! [[ "${version:?}" =~ ${old_versions:?} ]]; then diff --git a/src/scripts/extensions/phalcon.sh b/src/scripts/extensions/phalcon.sh index f28f482f..b0ac68b2 100644 --- a/src/scripts/extensions/phalcon.sh +++ b/src/scripts/extensions/phalcon.sh @@ -36,7 +36,7 @@ add_phalcon_helper() { add_brew_extension "$extension" extension else package="php${version:?}-$extension" - add_ppa ondrej/php >/dev/null 2>&1 || update_ppa ondrej/php + add_ppa >/dev/null 2>&1 [[ "$extension" =~ phalcon[4|5] ]] && (install_packages "php${version:?}-psr" || pecl_install psr || pecl_install psr-1.1.0) (check_package "$package" && install_packages "$package") || pecl_install phalcon-"$(get_phalcon_version)" || add_phalcon_from_repo fi diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index 821b748e..b1573d80 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -104,7 +104,7 @@ filter_packages() { add_extension_helper() { local extension=$1 packages=(php"$version"-"$extension") - add_ppa ondrej/php >/dev/null 2>&1 || update_ppa ondrej/php + add_ppa >/dev/null 2>&1 [ "${debug:?}" = "debug" ] && check_package php"$version"-"$extension"-dbgsym && packages+=(php"$version"-"$extension"-dbgsym) (check_package "${packages[0]}" && install_packages "${packages[@]}") || pecl_install "$extension" add_extension_log "$extension" "Installed and enabled" @@ -221,7 +221,7 @@ get_php_packages() { # Function to install packaged PHP add_packaged_php() { - add_ppa ondrej/php >/dev/null 2>&1 || update_ppa ondrej/php + add_ppa >/dev/null 2>&1 IFS=' ' read -r -a packages <<<"$(get_php_packages)" install_packages "${packages[@]}" } @@ -241,7 +241,9 @@ update_php() { # Function to install PHP. add_php() { if [ "${runner:?}" = "self-hosted" ] || [ "${use_builds_cache:-true}" = "false" ]; then - if [[ "$version" =~ ${php_builder_versions:?} || "$ts" = "zts" || -n "${asan:-}" ]]; then + # Sury's Ubuntu archive does not publish the PHP CLI debug symbols. + if [[ "$version" =~ ${php_builder_versions:?} || "$ts" = "zts" || -n "${asan:-}" || + ( "$debug" = "debug" && "$ID $ID_LIKE" == *ubuntu* ) ]]; then setup_php_builder else add_packaged_php diff --git a/src/scripts/tools/ppa.sh b/src/scripts/tools/ppa.sh index 15813ede..a73ce37e 100644 --- a/src/scripts/tools/ppa.sh +++ b/src/scripts/tools/ppa.sh @@ -37,10 +37,10 @@ set_base_version() { # Helper function to update package lists. update_lists_helper() { - list=$1 + local list=$1 command -v sudo >/dev/null && SUDO=sudo if [[ -n "$list" ]]; then - ${SUDO} apt-get update -o Dir::Etc::sourcelist="$list" -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0" + ${SUDO} apt-get update -o Dir::Etc::sourcelist="$list" -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0" "${@:2}" else ${SUDO} apt-get update fi @@ -50,22 +50,25 @@ update_lists_helper() { update_lists() { local ppa=${1:-} local ppa_search=${2:-} - local status_token=${3:-$ppa_search} local list= local status_file=/tmp/os_lists - local hash_cmd if [[ -n "$ppa" && -n "$ppa_search" ]]; then if [ -f "$ppa_search" ]; then list="$ppa_search" else list="$(grep -Elr "$ppa_search" "$list_dir" 2>/dev/null | head -n 1)" fi - hash_cmd="$(command -v sha256sum || command -v shasum)" - if [ -n "$status_token" ] && [ -n "$hash_cmd" ]; then - status_file=/tmp/os_lists_"$(echo -n "$status_token" | $hash_cmd | awk '{print $1}')" - elif [ -n "$status_token" ]; then - status_file=/tmp/os_lists_$(date +%s) - fi + [ -n "$list" ] || return 1 + status_file="/tmp/os_lists_refresh_${list##*/}" + [[ "${runner:-}" != self-hosted ]] && cmp -s "$list" "$status_file" && return 0 + local output rc + output=$(LC_ALL=C update_lists_helper "$list" -o APT::Update::Error-Mode=any 2>&1) || { + rc=$? + [[ "${3:-}" = --on-missing-key && "$output" =~ $missing_key ]] || return "$rc" + "$4" "$ppa" "$list" "${BASH_REMATCH[2]}" || return 1 + } + [[ "${runner:-}" = self-hosted ]] || cp "$list" "$status_file" + return elif [ -e "$list_file" ] && grep -Eq '^deb |^Types: *deb' "$list_file"; then list="$list_file" fi @@ -156,7 +159,7 @@ ubuntu_fingerprint() { ppa_uri="~${ppa%/*}/+archive/ubuntu/${ppa##*/}" get -s -n "" "${lp_api[0]}/$ppa_uri" | jq -er '.signing_key_fingerprint' 2>/dev/null \ || get -s -n "" "${lp_api[1]}/$ppa_uri" | jq -er '.signing_key_fingerprint' 2>/dev/null \ - || get -s -n "" "$ppa_sp/keys/$ppa.fingerprint" + || { [ -n "${4:-}" ] && get -s -n "" "${4%.gpg}.fingerprint"; } } @@ -170,26 +173,49 @@ debian_fingerprint() { gpg --list-packets "$release_pub" | grep -Eo 'fpr\sv4\s.*[a-zA-Z0-9]+' | head -n 1 | cut -d ' ' -f 3 } -# Function to add a GPG key. -add_key() { - ppa=${1:-ondrej/php} - ppa_url=$2 - package_dist=$3 - key_source=$4 - key_file=$5 - key_urls=("$key_source") - if [[ "$key_source" =~ launchpadcontent.net|debian.org ]]; then - fingerprint="$("${ID}"_fingerprint "$ppa" "$ppa_url" "$package_dist")" - sks_params="op=get&options=mr&exact=on&search=0x$fingerprint" - key_urls=("${sks[@]/%/\/pks\/lookup\?"$sks_params"}") - fi - key_urls+=("$ppa_sp/keys/$ppa.gpg") - [ ! -e "$key_source" ] && get -q -n "$key_file" "${key_urls[@]}" - if [[ "$(file "$key_file")" =~ .*('Public-Key (old)'|'Secret-Key') ]]; then - sudo gpg --batch --yes --dearmor "$key_file" >/dev/null 2>&1 && sudo mv "$key_file".gpg "$key_file" - fi +# Check for a binary OpenPGP public keyring readable by APT. +is_gpg_keyring() { + local keys + [ -s "$1" ] && LC_ALL=C file -Lb -- "$1" | grep -Eiq '^(OpenPGP public key|PGP(/GPG)? (public key|key public ring))' || return 1 + keys=$(gpg --batch --with-colons --show-keys "$1" 2>/dev/null) || return 1 + [[ -z "${2:-}" ]] || awk -F: -v signer="$2" '$1 == "fpr" && $10 ~ (signer "$") {found=1} END {exit !found}' <<< "$keys" } +# Function to add a GPG key. +add_key() ( + local ppa=$1 ppa_url=$2 package_dist=$3 key_source=$4 key_file=$5 + local mirror=${6:-} signer=${7:-} fingerprint sks_params + local key_urls=("$key_source") + if [[ -z "$key_source" || "$key_source" = "$ppa_url" ]]; then + key_urls=() + if fingerprint="$("${ID}"_fingerprint "$ppa" "$ppa_url" "$package_dist" "$mirror")" && [[ "$fingerprint" =~ ^[[:xdigit:]]+$ ]]; then + sks_params="op=get&options=mr&exact=on&search=0x$fingerprint" + key_urls=("${sks[@]/%/\/pks\/lookup\?"$sks_params"}") + fi + fi + [ -z "$mirror" ] || key_urls+=("$mirror") + local stage url input pending= + stage="$(mktemp -d)" || return 1 + trap 'rm -rf "$stage"; if [ -n "$pending" ]; then sudo rm -f -- "$pending"; fi' EXIT + for url in "${key_urls[@]}"; do + input=$url + if [ ! -f "$input" ]; then + input="$stage/download" + : > "$input" + (get -q -n "$input" "$url") || continue + fi + gpg --batch --yes --dearmor --output "$stage/key.gpg" "$input" >/dev/null 2>&1 || continue + is_gpg_keyring "$stage/key.gpg" "$signer" || continue + if [[ "$input" -ef "$key_file" ]] && { [[ "$key_file" = *.asc ]] || cmp -s "$input" "$stage/key.gpg"; }; then + return 0 + fi + pending="$(sudo mktemp "$key_file.XXXXXXXX")" || return 1 + sudo install -m 644 "$stage/key.gpg" "$pending" && sudo mv -f -- "$pending" "$key_file" + return + done + return 1 +) + # Function to handle existing list files. handle_existing_list() { local ppa=$1 @@ -217,48 +243,39 @@ write_list() { local components=$5 local key_file=$6 local list_basename="${ppa%%/*}"-"$ID"-"${ppa#*/}"-"$suite" - local arch + local arch by_hash= arch="$(dpkg --print-architecture)" + case "${url%/}" in + "$ppa_sp/sury"|"$ppa_sp/apache2") by_hash=' by-hash=force' ;; + esac sudo rm -f "$list_dir"/"${ppa/\//-}".list "$list_dir"/"${ppa/\//-}".sources "$list_dir"/"$list_basename".list "$list_dir"/"$list_basename".sources || true if [ "$type" = "deb822" ]; then - cat </dev/null -Types: deb -URIs: $url -Suites: $suite -Components: $components -Architectures: $arch -Signed-By: $key_file -EOF + { + printf '%s\n' "Types: deb" "URIs: $url" "Suites: $suite" "Components: $components" "Architectures: $arch" "Signed-By: $key_file" + if [ -n "$by_hash" ]; then printf '%s\n' 'By-Hash: force'; fi + } | sudo tee "$list_dir"/"$list_basename".sources >/dev/null else - echo "deb [arch=$arch signed-by=$key_file] $url $suite $components" | sudo tee "$list_dir"/"$list_basename".list >/dev/null 2>&1 + echo "deb [arch=$arch signed-by=$key_file$by_hash] $url $suite $components" | sudo tee "$list_dir"/"$list_basename".list >/dev/null 2>&1 fi } # Function to check if a PPA and its lists exist check_lists() { local ppa=$1 - local primary=${2:-} - local secondary=${3:-} - local status_token=${4:-$primary} + local pattern=${2:-${3:-}} local match_file= check_lists_file= - if [ -n "$primary" ]; then - match_file=$(grep -Elr "$primary" "$list_dir" 2>/dev/null | head -n 1) - fi - if [ -z "$match_file" ] && [ -n "$secondary" ]; then - local candidate - candidate=$(grep -Elr "$secondary" "$list_dir" 2>/dev/null | head -n 1) - if [ -n "$candidate" ] && { [ -z "$primary" ] || grep -Eq "$primary" "$candidate"; }; then - match_file="$candidate" - fi + if [ -n "$pattern" ]; then + match_file=$(grep -Elr "$pattern" "$list_dir" 2>/dev/null | head -n 1) fi if [ -n "$match_file" ]; then + check_lists_file="$match_file" + [[ "${4:-}" != --lookup-only ]] || return 0 local list_count list_count="$(sudo find /var/lib/apt/lists -type f -name "*${ppa/\//_}*" | wc -l)" if [ "$list_count" = "0" ]; then - update_lists "$ppa" "$match_file" "$status_token" + update_lists "$ppa" "$match_file" fi - check_lists_file="$match_file" return 0 fi return 1 @@ -266,21 +283,19 @@ check_lists() { # Function to add a sources list. add_list() { - ppa=${1-ondrej/php} + local ppa=${1-$default_ppa} ppa_url=${2:-"$lpc_ppa/$ppa/ubuntu"} - key_source=${3:-"$ppa_url"} + key_source=${3:-} package_dist=${4:-"$VERSION_CODENAME"} branches=${5:-main} local list_format local list_extension - local status_token local resolved_branches local list_path= list_format="$(get_sources_format)" list_extension="$(get_sources_extension "$list_format")" - status_token="${ppa_url}|${package_dist}|${branches}" IFS='|' read -r primary_pattern secondary_pattern <<< "$(get_repo_patterns "$list_format" "$ppa_url" "$package_dist" "$branches")" - if check_lists "$ppa" "$primary_pattern" "$secondary_pattern" "$status_token"; then + if check_lists "$ppa" "$primary_pattern" "$secondary_pattern" "${8:-}"; then list_path="$check_lists_file" if resolved_branches="$(handle_existing_list "$ppa" "$list_format" "$branches")"; then branches="$resolved_branches" @@ -288,14 +303,24 @@ add_list() { [ -n "$resolved_branches" ] && echo "$resolved_branches" && return 1 fi check_lists_file= - IFS='|' read -r primary_pattern secondary_pattern <<< "$(get_repo_patterns "$list_format" "$ppa_url" "$package_dist" "$branches")" - status_token="${ppa_url}|${package_dist}|${branches}" fi [ -e "$key_source" ] && key_file=$key_source || key_file="$key_dir"/"${ppa/\//-}"-keyring.gpg - add_key "$ppa" "$ppa_url" "$package_dist" "$key_source" "$key_file" - write_list "$list_format" "$ppa" "$ppa_url" "$package_dist" "$branches" "$key_file" + add_key "$ppa" "$ppa_url" "$package_dist" "$key_source" "$key_file" "${6:-}" || return 1 + write_list "$list_format" "$ppa" "$ppa_url" "$package_dist" "$branches" "$key_file" || return 1 list_path="$list_dir"/"${ppa%%/*}"-"$ID"-"${ppa#*/}"-"$package_dist"."$list_extension" - update_lists "$ppa" "$list_path" "$status_token" + # A caller requesting an index needs a successful refresh, even if APT has cached lists. + if [ -n "${7:-}" ]; then + update_lists_helper "$list_path" -o APT::Update::Error-Mode=any || return 1 + local index indexes + indexes=$(apt-get -o Dir::Etc::sourcelist="$list_path" -o Dir::Etc::sourceparts=- indextargets --format '$(FILENAME)' 'Identifier: Packages') || return 1 + while IFS= read -r index; do + [ -s "$index" ] || continue + printf '%s\n' "$index" > "$7" + return + done <<< "$indexes" + return 1 + fi + update_lists "$ppa" "$list_path" . /etc/os-release return 0; } @@ -309,66 +334,152 @@ check_ppa() { local list_format list_format="$(get_sources_format)" IFS='|' read -r primary_pattern secondary_pattern <<< "$(get_repo_patterns "$list_format" "$ppa_url" "$package_dist" "$branches")" - local status_token - status_token="${ppa_url}|${package_dist}|${branches}" - if check_lists "$ppa" "$primary_pattern" "$secondary_pattern" "$status_token"; then - return 0; - else - return 1; - fi + check_lists "$ppa" "$primary_pattern" "$secondary_pattern" || return 1 } # Function to remove a PPA. remove_list() { - ppa=${1-ondrej/php} + local ppa=${1-$default_ppa} list ppa_url + local -a ppa_urls [ -n "$2" ] && ppa_urls=("$2") || ppa_urls=("$lp_ppa/$ppa/ubuntu" "$lpc_ppa/$ppa/ubuntu") for ppa_url in "${ppa_urls[@]}"; do - grep -lr "$ppa_url" "$list_dir" | xargs -n1 sudo rm -f + while IFS= read -r -d '' list; do + sudo rm -f -- "$list" "/tmp/os_lists_refresh_${list##*/}" "${RUNNER_TEMP:-/tmp}/os_lists_${list##*/}" || return 1 + done < <(grep -lrZ --exclude="${3:-}" -- "$ppa_url" "$list_dir") done - sudo rm -f "$key_dir"/"${ppa/\//-}"-keyring /tmp/os_lists* || true + [[ ! -e "$key_dir/${ppa/\//-}-keyring" ]] || sudo rm -f "$key_dir/${ppa/\//-}-keyring" } # Function to check if ubuntu ppa is up is_ubuntu_ppa_up() { - ppa=${1:-ondrej/php} + ppa=${1:-$default_ppa} curl -s --connect-timeout 10 --max-time 10 --head --fail "$lpc_ppa/$ppa/ubuntu/dists/$VERSION_CODENAME/Release" > /dev/null } -# Function to add the PPA mirror. -add_ppa_sp_mirror() { - ppa=$1 - ppa_name="$(basename "$ppa")" - remove_list "$ppa" || true - [ "${debug:?}" = "debug" ] && add_list sp/"$ppa_name" "$ppa_sp/$ppa/ubuntu" "$ppa_sp/$ppa/ubuntu/key.gpg" "$VERSION_CODENAME" "main/debug" - add_list sp/"$ppa_name" "$ppa_sp/$ppa/ubuntu" "$ppa_sp/$ppa/ubuntu/key.gpg" +# Remove sources from caches that still have the old Launchpad mirrors. +remove_legacy_mirrors() { + local ppa old_ppa old_sp + for ppa in ondrej/php ondrej/apache2; do + old_ppa="//${ppa_sp#*://}/$ppa/ubuntu" + old_sp="//${sp#*://}/$ppa/ubuntu" + if grep -Fqr -e "$old_ppa" -e "$old_sp" "$list_dir"; then + remove_list "$ppa" "$old_ppa" && remove_list "$ppa" "$old_sp" || return 1 + fi + done } -add_sury_list() { - ppa=${1:-ondrej/php} - [ "${debug:?}" = "debug" ] && add_list "$ppa" "$sury"/"${ppa##*/}"/ "$sury"/"${ppa##*/}"/apt.gpg "$VERSION_CODENAME" "main/debug" - add_list "$ppa" "$sury"/"${ppa##*/}"/ "$sury"/"${ppa##*/}"/apt.gpg +# Function to add the Sury mirror. +add_ppa_sp_mirror() { + local ppa=$1 name=${1##*/} url="$ppa_sp/sury" + [[ "$ppa" =~ $mirrored_ppas ]] || return 1 + [ "$name" != apache2 ] || url="$ppa_sp/apache2" + local key_mirror="$ppa_sp/keys/sury/$name.gpg" + remove_list "$ppa" || true + remove_legacy_mirrors || return 1 + [ "${debug:?}" = "debug" ] && add_list "sp/$name" "$url" "$url/apt.gpg" "$VERSION_CODENAME" "main/debug" "$key_mirror" + add_list "sp/$name" "$url" "$url/apt.gpg" "$VERSION_CODENAME" main "$key_mirror" } +# Add a signed archive, using the mirror if APT cannot load a Packages index. +# Arguments: [--keyring path primary-key-url mirror-key-url] list ID, primary URL, mirror URL, keyring, suite, components, optional source URLs to replace. +add_list_with_fallback() ( + local managed_key='' key_url='' key_mirror='' + if [[ "$1" = --keyring ]]; then + managed_key=$2 key_url=$3 key_mirror=$4 + shift 4 + fi + local ppa=$1 primary=$2 mirror=$3 key=$4 suite=${5:-$VERSION_CODENAME} components=${6:-main} + local repo=${ppa_sources[$ID/$ppa]:-$ppa} source stamp request cached_request cached_key index + local stage='' old_url old_pattern + [[ -s "$key" || -n "$managed_key" ]] || return 1 + sources_format="$(get_sources_format)" + source="${repo%%/*}-$ID-${repo#*/}-$suite.$(get_sources_extension "$sources_format")" + stamp="${RUNNER_TEMP:-/tmp}/os_lists_$source" + printf -v request '%q ' "$primary" "$mirror" "$components" "${@:7}" + if [[ -s "$list_dir/$source" && -s "$stamp" && ! "$key" -nt "$stamp" ]] && + [[ -n "$(find "$stamp" -mtime -1 -print)" ]] && + { IFS= read -r cached_request && IFS= read -r index && IFS= read -r cached_key && cmp -s - "$list_dir/$source"; } < "$stamp" && + [[ "$cached_request" = "$request" && "$cached_key" = "$key" && -s "$index" ]] && + { [[ "${require_mirror_by_hash:-}" != true ]] || ! grep -Fq "${mirror%/}" "$list_dir/$source" || + grep -Eq '^By-Hash: force$| by-hash=force( |])' "$list_dir/$source"; }; then + # A cache can restore another source file after this archive was selected. + (( $# > 6 )) && grep -qrF --exclude="$source" -f <(printf '%s\n' "${@:7}") "$list_dir" || return 0 + else + stage=$(mktemp -d /tmp/setup-php-list.XXXXXXXX) || return 1 + trap 'rm -rf "$stage"' EXIT + chmod 755 "$stage" || return 1 + if [[ -z "$key" ]]; then + key="$stage/initial.gpg" + add_key "$repo" "$primary" "$suite" "$key_url" "$key" "$key_mirror" || return 1 + fi + local url attempt_key output signer='' candidate="$stage/rotated.gpg" + for url in "$primary" "$mirror"; do + for attempt_key in "$key" "$candidate"; do + if [[ "$attempt_key" = "$candidate" ]]; then + [[ -n "$key_url" && "$output" =~ $missing_key ]] || break + if [[ "$signer" != "${BASH_REMATCH[2]}" ]]; then + signer=${BASH_REMATCH[2]} + rm -f "$candidate" || return 1 + add_key "$repo" "$primary" "$suite" "$key_url" "$candidate" "$key_mirror" "$signer" || break + fi + [ -s "$candidate" ] || break + rm -f "$stage/$source" || return 1 + fi + if output=$(list_dir="$stage" add_list "$repo" "$url" "$attempt_key" "$suite" "$components" '' "$stage/index" 2>&1); then + [[ -z "$output" ]] || printf '%s\n' "$output" + key=$attempt_key + break 2 + fi + printf '%s\n' "$output" >&2 + done + done + [ -s "$stage/index" ] || return 1 + fi + for old_url in "${@:7}"; do + old_pattern=$(sed 's/[][\\.^$*]/\\&/g' <<< "${old_url%/}") + remove_list "$ppa" "$old_pattern/*\\([[:space:]]\\|$\\)" "$source" || return 1 + done + [ -n "$stage" ] || return 0 + sudo rm -f "/tmp/os_lists_refresh_$source" || return 1 + if [[ "$key" = "$stage/"* ]]; then + list_with_key "$stage/$source" "$managed_key" > "$stage/publish" && mv -f "$stage/publish" "$stage/$source" || return 1 + add_key "$repo" "$primary" "$suite" "$key" "$managed_key" || return 1 + key=$managed_key + fi + sudo install -m 644 "$stage/$source" "$list_dir/$source" || return 1 + { printf '%s\n' "$request" && cat "$stage/index" && printf '%s\n' "$key" && cat "$stage/$source"; } > "$stage/stamp" && mv -fT "$stage/stamp" "$stamp" +) + # Function to add a PPA. add_ppa() { set_base_version - ppa=${1:-ondrej/php} - if [[ "$ID" = "ubuntu" || "$ID_LIKE" =~ ubuntu ]] && [[ "$ppa" =~ "ondrej/" ]]; then - if [ "$VERSION_ID" = "26.04" ]; then - add_sury_list + ppa=${1:-$default_ppa} + if [[ "$ID ${ID_LIKE:-}" =~ ubuntu|debian && "$ppa" =~ $mirrored_ppas ]]; then + remove_legacy_mirrors || return 1 + local components=main name=${ppa##*/} primary="$sury/${ppa##*/}/" key_mirror="$ppa_sp/keys/sury/${ppa##*/}.gpg" + if [[ "$name" = php && "$ID ${ID_LIKE:-}" != *ubuntu* ]]; then + [ "${debug:?}" != debug ] || components="main/debug main" + add_list "$ppa" "$primary" "${primary}apt.gpg" "$VERSION_CODENAME" "$components" "$key_mirror" '' --lookup-only || + { [ -n "$check_lists_file" ] && update_lists "$ppa" "$check_lists_file" --on-missing-key retry_sury_update; } else - if is_ubuntu_ppa_up "$ppa" ; then - [ "${runner:?}" = "self-hosted" ] && find "$list_dir" -type f -name 'sp*' -exec grep -qF "${sp/https:\/\/}" {} \; -delete - [ "${debug:?}" = "debug" ] && add_list "$ppa" "$lpc_ppa/$ppa/ubuntu" "$lpc_ppa/$ppa/ubuntu" "$VERSION_CODENAME" "main/debug" - add_list "$ppa" - elif [ "$ppa" = "ondrej/php" ]; then - add_ppa_sp_mirror "$ppa" - else - add_log "${cross:?}" "$ppa" "PPA $ppa is not available" + local mirror="$ppa_sp/sury/" managed_key="$key_dir/sury-$name-keyring.gpg" key= + local -a curl_opts=("${curl_opts[@]}" --connect-timeout 5 --max-time 15) + if [ "$name" = apache2 ]; then + mirror="$ppa_sp/apache2/" + [ "${debug:-}" != debug ] || components="main/debug main" fi + local require_mirror_by_hash=true + local -a options=("$VERSION_CODENAME" "$components" "//${lp_ppa#*://}/$ppa/ubuntu" "//${lpc_ppa#*://}/$ppa/ubuntu" + "${primary%/}" "${mirror%/}") + [ "$name" != php ] || options+=(mirror+file:/etc/apt/setup-php-sury-mirrors.txt) + local packaged_key="$key_dir/debsuryorg-archive-keyring.gpg" + if is_gpg_keyring "$managed_key"; then + key="$managed_key" + elif is_gpg_keyring "$packaged_key"; then + key="$packaged_key" + fi + LC_ALL=C add_list_with_fallback --keyring "$managed_key" "${primary}apt.gpg" "$key_mirror" "$ppa" "$primary" "$mirror" "$key" "${options[@]}" fi - elif [[ "$ID" = "debian" || "$ID_LIKE" =~ debian ]] && [[ "$ppa" =~ "ondrej/" ]]; then - add_sury_list else add_list "$ppa" fi @@ -377,25 +488,70 @@ add_ppa() { return $exit_code } +# Replace a single Signed-By keyring, preserving the rest of a deb or deb822 source. +list_with_key() { + awk -v key="$2" ' + /^Signed-By:[[:blank:]]+\/[^[:space:]]+$/ { $0 = "Signed-By: " key; changed = 1 } + /^deb(-src)?[[:blank:]]/ && match($0, /[[:blank:][]signed-by=\/[^],[:space:]]+[][:blank:]]/) { + $0 = substr($0, 1, RSTART) "signed-by=" key substr($0, RSTART + RLENGTH - 1) + changed = 1 + } + { print } + END { exit !changed } + ' "$1" +} + +# Retry just the selected Sury source after APT reports a missing signer. +retry_sury_update() ( + local ppa=$1 list=$2 signer=$3 name=${1##*/} selected stage + [[ "$ppa" =~ $mirrored_ppas ]] || return 1 + local primary="$sury/$name/" mirror="$ppa_sp/sury/" key="$key_dir/sury-$name-keyring.gpg" + [ "$name" != apache2 ] || mirror="$ppa_sp/apache2/" + selected=$(apt-get -o Dir::Etc::sourcelist="$list" -o Dir::Etc::sourceparts=- indextargets \ + --no-release-info --format '$(REPO_URI)' | sort -u) || return 1 + [[ "${selected%/}" = "${primary%/}" || "${selected%/}" = "${mirror%/}" ]] || return 1 + + local source=${list##*/} request index old_key + local stamp="${RUNNER_TEMP:-/tmp}/os_lists_$source" + local -a curl_opts=("${curl_opts[@]}" --connect-timeout 5 --max-time 15) + stage=$(mktemp -d /tmp/setup-php-key.XXXXXXXX) || return 1 + trap 'if (( $? != 0 )); then rm -f -- "$stamp"; fi; rm -rf "$stage"' EXIT + # APT's signature verifier runs as an unprivileged user. + chmod 755 "$stage" || return 1 + list_with_key "$list" "$stage/key.gpg" > "$stage/$source" || return 1 + add_key "sury/$name" "$primary" "$VERSION_CODENAME" "${primary}apt.gpg" "$stage/key.gpg" "$ppa_sp/keys/sury/$name.gpg" "$signer" || return 1 + update_lists_helper "$stage/$source" -o APT::Update::Error-Mode=any || return 1 + + list_with_key "$list" "$key" > "$stage/$source" || return 1 + add_key "sury/$name" "$primary" "$VERSION_CODENAME" "$stage/key.gpg" "$key" || return 1 + if [[ -s "$stamp" ]] && { IFS= read -r request && IFS= read -r index && IFS= read -r old_key && [[ -n "$old_key" ]] && cmp -s - "$list"; } < "$stamp"; then + { printf '%s\n' "$request" "$index" "$key" && cat "$stage/$source"; } > "$stage/stamp" || return 1 + fi + sudo install -m 644 "$stage/$source" "$list" || return 1 + [[ ! -f "$stage/stamp" ]] || mv -fT "$stage/stamp" "$stamp" +) + # Function to update a PPA. update_ppa() { set_base_version - ppa=${1:-ondrej/php} + ppa=${1:-$default_ppa} ppa_url=${2:-"$lpc_ppa/$ppa/ubuntu"} package_dist=${4:-"$VERSION_CODENAME"} branches=${5:-main} local list_format list_format="$(get_sources_format)" - IFS='|' read -r primary_pattern secondary_pattern <<< "$(get_repo_patterns "$list_format" "$ppa_url" "$package_dist" "$branches")" + local repo=${ppa_sources[$ID/$ppa]:-$ppa} local list_path - list_path="$(grep -Elr "$primary_pattern" "$list_dir" 2>/dev/null | head -n 1)" - if [ -z "$list_path" ] && [ -n "$secondary_pattern" ]; then - list_path="$(grep -Elr "$secondary_pattern" "$list_dir" 2>/dev/null | head -n 1)" + list_path="$list_dir/${repo%%/*}-$ID-${repo#*/}-$package_dist.$(get_sources_extension "$list_format")" + if [[ -n "${2:-}" || ! -s "$list_path" ]]; then + IFS='|' read -r primary_pattern secondary_pattern <<< "$(get_repo_patterns "$list_format" "$ppa_url" "$package_dist" "$branches")" + list_path="$(grep -Elr "$primary_pattern" "$list_dir" 2>/dev/null | head -n 1)" + [ -n "$list_path" ] || return 1 fi - local status_token - status_token="${ppa_url}|${package_dist}|${branches}" - update_lists "$ppa" "${list_path:-$primary_pattern}" "$status_token" + local rc=0 + update_lists "$ppa" "$list_path" --on-missing-key retry_sury_update || rc=$? . /etc/os-release + return "$rc" } # Variables @@ -416,6 +572,10 @@ dist_info_dir='/usr/share/distro-info' sury='https://packages.sury.org' ppa_sp='https://ppa.setup-php.com' sp='https://setup-php.com' +default_ppa='ondrej/php' +missing_key='(NO_PUBKEY|Missing key) ([[:xdigit:]]{16,64})' +mirrored_ppas='^ondrej/(apache2|php)$' +declare -A ppa_sources=([ubuntu/ondrej/php]=setup-php/sury [ubuntu/ondrej/apache2]=setup-php/sury-apache2 [debian/ondrej/apache2]=setup-php/sury-apache2) sks=( 'https://keyserver.ubuntu.com' 'https://pgp.mit.edu'