qubes-download-dom0-updates.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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="$OPTS --config=$DOM0_UPDATES_DIR/etc/dnf/dnf.conf"
  9. elif [ -f "$DOM0_UPDATES_DIR/etc/yum.conf" ]; then
  10. OPTS="$OPTS --config=$DOM0_UPDATES_DIR/etc/yum.conf"
  11. fi
  12. # DNF uses /etc/yum.repos.d, even when --installroot is specified
  13. OPTS="$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)
  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. OPTS="$OPTS $1"
  41. ;;
  42. *)
  43. PKGLIST+=( "${1}" )
  44. if [ -z "$YUM_ACTION" ]; then
  45. YUM_ACTION=install
  46. fi
  47. ;;
  48. esac
  49. shift
  50. done
  51. if [ -z "$YUM_ACTION" ]; then
  52. YUM_ACTION=upgrade
  53. fi
  54. YUM="yum"
  55. if type dnf >/dev/null 2>&1; then
  56. YUM="dnf --best --allowerasing --noplugins"
  57. else
  58. # salt in dom0 thinks it's using dnf but we only have yum so need to remove extra options
  59. OPTS="${OPTS/--best --allowerasing/}"
  60. fi
  61. if ! [ -d "$DOM0_UPDATES_DIR" ]; then
  62. echo "Dom0 updates dir does not exists: $DOM0_UPDATES_DIR" >&2
  63. exit 1
  64. fi
  65. mkdir -p $DOM0_UPDATES_DIR/etc
  66. if [ -e /etc/debian_version ]; then
  67. # Default rpm configuration on Debian uses ~/.rpmdb for rpm database (as
  68. # rpm isn't native package manager there)
  69. mkdir -p "$DOM0_UPDATES_DIR$HOME"
  70. ln -nsf "$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 $YUM_ACTION -y"
  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. if [ "$GUI" = 1 ]; then
  152. ( echo "1"
  153. # shellcheck disable=SC2086
  154. $YUM_COMMAND $OPTS "${PKGLIST[@]}"
  155. echo 100 ) | zenity --progress --pulsate --auto-close --auto-kill \
  156. --text="Downloading updates for Dom0, please wait..." --title="Qubes Dom0 updates"
  157. else
  158. # shellcheck disable=SC2086
  159. $YUM_COMMAND $OPTS "${PKGLIST[@]}"
  160. fi
  161. find "$DOM0_UPDATES_DIR/var/cache" -name '*.rpm' -print0 2>/dev/null |\
  162. xargs -0 -r ln -f -t "$DOM0_UPDATES_DIR/packages/"
  163. if ls "$DOM0_UPDATES_DIR"/packages/*.rpm > /dev/null 2>&1; then
  164. if [ -n "$SIGNATURE_REGEX" ]; then
  165. rpmkeys_error=0
  166. for pkg in "$DOM0_UPDATES_DIR"/packages/*.rpm; do
  167. rpmkeys_exit_code=0
  168. output="$(rpmkeys --root "$DOM0_UPDATES_DIR" --checksig "$pkg")" \
  169. || rpmkeys_exit_code="$?"
  170. if [ ! "$rpmkeys_exit_code" = "0" ]; then
  171. echo "ERROR: could not verify $pkg" >&2
  172. rpmkeys_error=1
  173. rm "$pkg"
  174. elif ! echo "$output" |grep -Pq "$SIGNATURE_REGEX"; then
  175. echo "ERROR: missing or invalid signature for $pkg" >&2
  176. rpmkeys_error=1
  177. rm "$pkg"
  178. else
  179. echo "Successfully verified $pkg" >&2
  180. fi
  181. done
  182. if [ ! "$rpmkeys_error" = "0" ]; then
  183. echo "ERROR: could not verify one or more packages" >&2
  184. exit 1
  185. fi
  186. fi
  187. cmd="/usr/lib/qubes/qrexec-client-vm dom0 qubes.ReceiveUpdates /usr/lib/qubes/qfile-agent"
  188. qrexec_exit_code=0
  189. $cmd "$DOM0_UPDATES_DIR"/packages/*.rpm || { qrexec_exit_code=$? ; true; };
  190. if [ ! "$qrexec_exit_code" = "0" ]; then
  191. echo "'$cmd $DOM0_UPDATES_DIR/packages/*.rpm' failed with exit code ${qrexec_exit_code}!" >&2
  192. exit "$qrexec_exit_code"
  193. fi
  194. else
  195. echo "No packages downloaded" >&2
  196. fi