dsa-4371-update 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. # Log everthing to stdout.
  3. # Use qrexc output only to communicate success/failure
  4. exec {qrexec_output}>&1
  5. exec 1>&2
  6. set -eu -o pipefail
  7. tmp=
  8. error() {
  9. printf "Error: $1\n" "${@:2}"
  10. exit
  11. }
  12. exit_ok() {
  13. printf "Ok: $2\n" "${@:3}"
  14. printf "$1\n" >&$qrexec_output
  15. exit
  16. }
  17. cleanup() {
  18. if [ -n "$tmp" ]; then
  19. rm -rf "$tmp"
  20. fi
  21. }
  22. check_apt_version() {
  23. local pkg="$1"
  24. local fixed_version="$2"
  25. if [ -z "$fixed_version" ] || [ -z "$pkg" ]; then
  26. error "Bug: Invalid argument!"
  27. fi
  28. installed_version="$(dpkg -s $pkg | grep '^Version: ' | cut -d ' ' -f 2)"
  29. if [ -z "$installed_version" ]; then
  30. error "Failed to get apt version."
  31. fi
  32. rc=0
  33. dpkg --compare-versions "$installed_version" ge "$fixed_version" || rc=$?
  34. if [ "$rc" -gt 1 ]; then
  35. error "Bug: Failed to compare versions!"
  36. fi
  37. return $rc
  38. }
  39. main() {
  40. if [ ! -e /etc/debian_version ]; then
  41. exit_ok 'changed=no' 'Not a Debian.'
  42. fi
  43. trap cleanup EXIT
  44. tmp="$(mktemp -d --tmpdir)"
  45. codename="$(cat /etc/debian_version)"
  46. case "$codename" in
  47. */sid|10.*|kali-*)
  48. # We will treat testing as sid here. This hopefully won't break
  49. # anything ...
  50. codename="sid"
  51. pkg="libapt-pkg5.0"
  52. fixed_version="1.8.0~alpha3.1"
  53. ;;
  54. 8.*)
  55. codename="jessie"
  56. pkg="libapt-pkg4.12"
  57. fixed_version="1.0.9.8.5"
  58. ;;
  59. 9.*)
  60. codename="stretch"
  61. pkg="libapt-pkg5.0"
  62. fixed_version="1.4.9"
  63. ;;
  64. *)
  65. exit_ok 'changed=no' 'Unrecognized debian variant, but probably ok by now'
  66. esac
  67. if check_apt_version "$pkg" "$fixed_version"; then
  68. exit_ok 'changed=no' 'Nothing to do, apt already fixed.'
  69. fi
  70. : > "$tmp/sources.list"
  71. mkdir "$tmp/sources.list.d"
  72. # Make sure that any old (maybe bogus) list is removed.
  73. apt-get \
  74. -o "Acquire::http::AllowRedirect=false" \
  75. -o "Dir::Etc::SourceList=$tmp/sources.list" \
  76. -o "Dir::Etc::SourceParts=$tmp/sources.list.d" \
  77. --list-cleanup \
  78. update
  79. printf 'deb http://cdn-fastly.deb.debian.org/debian %s main\n' "$codename" > "$tmp/sources.list"
  80. if [ "$codename" != "sid" ]; then
  81. printf 'deb http://cdn-fastly.deb.debian.org/debian-security %s/updates main\n' "$codename" >> "$tmp/sources.list"
  82. fi
  83. # Don't fetch Translation and Contents file. We don't need them and we will
  84. # throw them away later anyway.
  85. apt-get \
  86. -o "Acquire::http::AllowRedirect=false" \
  87. -o "Acquire::Languages=none" \
  88. -o "Acquire::IndexTargets::deb::Contents-deb::DefaultEnabled=false" \
  89. -o "Dir::Etc::SourceList=$tmp/sources.list" \
  90. -o "Dir::Etc::SourceParts=$tmp/sources.list.d" \
  91. update
  92. apt-get \
  93. -o "Acquire::http::AllowRedirect=false" \
  94. -o "Dir::Etc::SourceList=$tmp/sources.list" \
  95. -o "Dir::Etc::SourceParts=$tmp/sources.list.d" \
  96. --no-remove \
  97. --only-upgrade \
  98. -y \
  99. install "$pkg"
  100. if ! check_apt_version "$pkg" "$fixed_version"; then
  101. error 'apt version is still not fixed!'
  102. fi
  103. # Run update again to restore normal package sources.
  104. apt-get update
  105. exit_ok 'changed=yes' "Done."
  106. }
  107. main