qubes-download-dom0-updates.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/bin/bash
  2. DOM0_UPDATES_DIR=/var/lib/qubes/dom0-updates
  3. GUI=1
  4. CLEAN=0
  5. CHECK_ONLY=0
  6. OPTS=(--installroot "$DOM0_UPDATES_DIR")
  7. if [ -f "$DOM0_UPDATES_DIR/etc/dnf/dnf.conf" ]; then
  8. OPTS+=("--config=$DOM0_UPDATES_DIR/etc/dnf/dnf.conf")
  9. elif [ -f "$DOM0_UPDATES_DIR/etc/yum.conf" ]; then
  10. OPTS+=("--config=$DOM0_UPDATES_DIR/etc/yum.conf")
  11. fi
  12. # DNF uses /etc/yum.repos.d, even when --installroot is specified
  13. OPTS+=("--setopt=reposdir=$DOM0_UPDATES_DIR/etc/yum.repos.d")
  14. # DNF verifies signatures implicitly, but yumdownloader does not.
  15. SIGNATURE_REGEX=""
  16. PKGLIST=()
  17. YUM_ACTION=
  18. export LC_ALL=C
  19. while [ -n "$1" ]; do
  20. case "$1" in
  21. --doit|--force-xen-upgrade|--console|--show-output)
  22. # ignore
  23. ;;
  24. --nogui)
  25. GUI=0
  26. ;;
  27. --gui)
  28. GUI=1
  29. ;;
  30. --clean)
  31. CLEAN=1
  32. ;;
  33. --check-only)
  34. CHECK_ONLY=1
  35. ;;
  36. --action=*)
  37. YUM_ACTION=${1#--action=}
  38. ;;
  39. -*)
  40. # we already add these options for DNF, and Yum doesn’t support them
  41. case $1 in (--best|--allowerasing) :;; (*) OPTS+=("$1");; esac
  42. ;;
  43. *)
  44. PKGLIST+=( "${1}" )
  45. if [ -z "$YUM_ACTION" ]; then
  46. YUM_ACTION=install
  47. fi
  48. ;;
  49. esac
  50. shift
  51. done
  52. if [ -z "$YUM_ACTION" ]; then
  53. YUM_ACTION=upgrade
  54. fi
  55. if type dnf >/dev/null 2>&1; then
  56. YUM=(dnf --best --allowerasing --noplugins)
  57. else
  58. YUM=(yum)
  59. fi
  60. if ! [ -d "$DOM0_UPDATES_DIR" ]; then
  61. echo "Dom0 updates dir does not exists: $DOM0_UPDATES_DIR" >&2
  62. exit 1
  63. fi
  64. mkdir -p $DOM0_UPDATES_DIR/etc
  65. if [ -e /etc/debian_version ]; then
  66. # Default rpm configuration on Debian uses ~/.rpmdb for rpm database (as
  67. # rpm isn't native package manager there)
  68. mkdir -p "$DOM0_UPDATES_DIR$HOME"
  69. rm -rf "$DOM0_UPDATES_DIR$HOME/.rpmdb"
  70. cp -r "$DOM0_UPDATES_DIR/var/lib/rpm" "$DOM0_UPDATES_DIR$HOME/.rpmdb"
  71. fi
  72. # Rebuild rpm database in case of different rpm version
  73. rm -f $DOM0_UPDATES_DIR/var/lib/rpm/__*
  74. rpm --root=$DOM0_UPDATES_DIR --rebuilddb
  75. if [ "$CLEAN" = "1" ]; then
  76. # shellcheck disable=SC2086
  77. "${YUM[@]}" "${OPTS[@]}" clean all
  78. rm -f "$DOM0_UPDATES_DIR"/packages/*
  79. rm -rf "$DOM0_UPDATES_DIR"/var/cache/*
  80. fi
  81. # just check for updates, but don't download any package
  82. if [ ${#PKGLIST[@]} -eq 0 ] && [ "$CHECK_ONLY" = "1" ]; then
  83. echo "Checking for dom0 updates..." >&2
  84. # shellcheck disable=SC2086
  85. UPDATES_FULL=$("${YUM[@]}" "${OPTS[@]}" check-update)
  86. check_update_retcode=$?
  87. if [ "$check_update_retcode" -eq 1 ]; then
  88. # Exit here if yum have reported an error. Exit code 100 isn't an
  89. # error, it's "updates available" info, so check specifically for exit code 1
  90. exit 1
  91. fi
  92. if [ $check_update_retcode -eq 100 ]; then
  93. echo "Available updates: "
  94. echo "$UPDATES_FULL"
  95. exit 100
  96. else
  97. echo "No new updates available"
  98. if [ "$GUI" = 1 ]; then
  99. zenity --info --text="No new updates available"
  100. fi
  101. exit 0
  102. fi
  103. fi
  104. # now, we will download something
  105. YUM_COMMAND=(fakeroot "${YUM[@]}" "$YUM_ACTION" -y --downloadonly)
  106. # check for --downloadonly option - if not supported (Debian), fallback to
  107. # yumdownloader
  108. if ! "${YUM[@]}" --help | grep -q downloadonly; then
  109. if dpkg --compare-versions \
  110. "$(dpkg-query --show --showformat='${version}' rpm)" gt 4.14; then
  111. SIGNATURE_REGEX="^[A-Za-z0-9._+-/]{1,128}\.rpm: digests signatures OK$"
  112. else
  113. SIGNATURE_REGEX="^[A-Za-z0-9._+-/]{1,128}\.rpm: [a-z0-9() ]* (pgp|gpg) [a-z0-9 ]* OK$"
  114. fi
  115. # setup environment for yumdownloader to be happy
  116. if [ ! -e "$DOM0_UPDATES_DIR/etc/yum.conf" ]; then
  117. ln -nsf dnf/dnf.conf "$DOM0_UPDATES_DIR/etc/yum.conf"
  118. fi
  119. if [ "$YUM_ACTION" = "install" ]; then
  120. YUM_COMMAND=(yumdownloader "--destdir=$DOM0_UPDATES_DIR/packages" --resolve)
  121. elif [ "$YUM_ACTION" = "upgrade" ]; then
  122. # shellcheck disable=SC2086
  123. UPDATES_FULL=$("${YUM[@]}" "${OPTS[@]}" check-update "${PKGLIST[@]}")
  124. check_update_retcode=$?
  125. UPDATES_FULL=$(echo "$UPDATES_FULL" | grep -v "^Loaded plugins:\|^Last metadata\|^$")
  126. mapfile -t PKGLIST < <(echo "$UPDATES_FULL" | grep -v "^Obsoleting\|Could not" | cut -f 1 -d ' ')
  127. if [ "$check_update_retcode" -eq 0 ]; then
  128. # exit code 0 means no updates available - regardless of stdout messages
  129. echo "No new updates available" >&2
  130. exit 0
  131. fi
  132. YUM_COMMAND=(yumdownloader "--destdir=$DOM0_UPDATES_DIR/packages" --resolve)
  133. elif [ "$YUM_ACTION" == "list" ] || [ "$YUM_ACTION" == "search" ]; then
  134. # those actions do not download any package, so lack of --downloadonly is irrelevant
  135. YUM_COMMAND=("${YUM[@]}" -y -- "$YUM_ACTION")
  136. elif [ "$YUM_ACTION" == "reinstall" ]; then
  137. # this is just approximation of 'reinstall' action...
  138. mapfile -t PKGLIST < <(rpm "--root=$DOM0_UPDATES_DIR" -q "${PKGLIST[@]}")
  139. YUM_COMMAND=(yumdownloader "--destdir=$DOM0_UPDATES_DIR/packages" --resolve)
  140. else
  141. echo "ERROR: yum version installed in VM $(hostname) does not suppport --downloadonly option" >&2
  142. echo "ERROR: only 'install' and 'upgrade' actions supported ($YUM_ACTION not)" >&2
  143. if [ "$GUI" = 1 ]; then
  144. zenity --error --text="yum version too old for '$YUM_ACTION' action, see console for details"
  145. fi
  146. exit 1
  147. fi
  148. fi
  149. mkdir -p "$DOM0_UPDATES_DIR/packages"
  150. set -e
  151. "${YUM_COMMAND[@]}" "${OPTS[@]}" "${PKGLIST[@]}"
  152. find "$DOM0_UPDATES_DIR/var/cache" -name '*.rpm' -print0 2>/dev/null |\
  153. xargs -0 -r ln -f -t "$DOM0_UPDATES_DIR/packages/"
  154. if ls "$DOM0_UPDATES_DIR"/packages/*.rpm > /dev/null 2>&1; then
  155. if [ -n "$SIGNATURE_REGEX" ]; then
  156. rpmkeys_error=0
  157. for pkg in "$DOM0_UPDATES_DIR"/packages/*.rpm; do
  158. rpmkeys_exit_code=0
  159. output="$(rpmkeys --root "$DOM0_UPDATES_DIR" --checksig "$pkg")" \
  160. || rpmkeys_exit_code="$?"
  161. if [ ! "$rpmkeys_exit_code" = "0" ]; then
  162. echo "ERROR: could not verify $pkg" >&2
  163. rpmkeys_error=1
  164. rm "$pkg"
  165. elif ! echo "$output" |grep -Pq "$SIGNATURE_REGEX"; then
  166. echo "ERROR: missing or invalid signature for $pkg" >&2
  167. rpmkeys_error=1
  168. rm "$pkg"
  169. else
  170. echo "Successfully verified $pkg" >&2
  171. fi
  172. done
  173. if [ ! "$rpmkeys_error" = "0" ]; then
  174. echo "ERROR: could not verify one or more packages" >&2
  175. exit 1
  176. fi
  177. fi
  178. cmd="/usr/lib/qubes/qrexec-client-vm dom0 qubes.ReceiveUpdates /usr/lib/qubes/qfile-agent"
  179. qrexec_exit_code=0
  180. $cmd "$DOM0_UPDATES_DIR"/packages/*.rpm || { qrexec_exit_code=$? ; true; };
  181. if [ ! "$qrexec_exit_code" = "0" ]; then
  182. echo "'$cmd $DOM0_UPDATES_DIR/packages/*.rpm' failed with exit code ${qrexec_exit_code}!" >&2
  183. exit "$qrexec_exit_code"
  184. fi
  185. else
  186. echo "No packages downloaded" >&2
  187. fi