A utility for downloading and verifying FreeBSD releases and snapshots
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

158 lines
3.8 KiB

  1. #!/bin/sh -
  2. #
  3. # Copyright 2018 John-Mark Gurney.
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. #
  27. # $Id$
  28. #
  29. set -e
  30. usage() {
  31. echo "Usage: $0 [ -m ] <file>"
  32. echo "Usage: $0 -c <date>"
  33. echo ''
  34. echo 'Options:'
  35. echo ' -m Do not check what files are available. This is useful for'
  36. echo ' bulk import.'
  37. echo ' -c Complete a bulk import (previously using -m), and assume that'
  38. echo ' any snapshots before specified data are unfetchable.'
  39. echo ''
  40. echo 'date is specified as YYYYMMDD'
  41. if [ x"$1" != x"" ]; then
  42. exit $1
  43. fi
  44. }
  45. args=`getopt cm $*`
  46. if [ $? -ne 0 ]; then
  47. usage 2
  48. fi
  49. set -- $args
  50. while :; do
  51. case "$1" in
  52. -c)
  53. complete=1
  54. shift
  55. ;;
  56. -m)
  57. more=1
  58. shift
  59. ;;
  60. --)
  61. shift; break
  62. ;;
  63. esac
  64. done
  65. if [ x"$complete" = x"1" -a x"$more" = x"1" ]; then
  66. echo '-m and -c cannot be specified at the same time.'
  67. usage 2
  68. elif [ x"$complete" = x"1" -a $# -ne 1 ]; then
  69. echo 'must only specify a date with -c'
  70. usage 2
  71. elif [ x"$complete" != x"1" -a $# -ne 1 ]; then
  72. echo 'must specify exactly one file'
  73. usage 2
  74. fi
  75. while ! mkdir "$0.running"; do
  76. sleep 1;
  77. done
  78. if [ x"$complete" = x"1" ]; then
  79. sort -u snapshot.complete.idx | xz > snapshot.complete.idx.xz
  80. awk '$5 >= "'"$1"'" {
  81. if (!system("wget --method=HEAD " $9))
  82. print
  83. }
  84. ' snapshot.idx | sort -u | xz > snapshot.idx.xz
  85. rm snapshot.idx snapshot.complete.idx
  86. rmdir "$0.running"
  87. exit 0
  88. fi
  89. # minimize file
  90. tmpfname="tmp.snapinf.asc"
  91. awk '
  92. output != 1 && tolower($1) == "message-id:" {
  93. print
  94. next
  95. }
  96. $0 == "-----BEGIN PGP SIGNED MESSAGE-----" {
  97. output = 1
  98. }
  99. output == 1 {
  100. print
  101. }
  102. $0 == "-----END PGP SIGNATURE-----" {
  103. output = 0
  104. }' "$1" > "$tmpfname"
  105. if ! gpg --verify "$tmpfname"; then
  106. echo 'failed verify'
  107. rm "$tmpfname"
  108. rmdir "$0.running"
  109. exit 1
  110. fi
  111. # process file
  112. awk -f ./mksnapidx.awk "$tmpfname" > additional
  113. rm "$tmpfname"
  114. # only check if there isn't more to come
  115. if [ x"$more" = x"1" ]; then
  116. (cat snapshot.idx || :; cat additional) > snapshot.idx.new
  117. (cat snapshot.complete.idx || :; cat additional) > snapshot.complete.idx.new
  118. else
  119. (xzcat snapshot.idx.xz; cat additional) | sort -u | awk '
  120. {
  121. if (!system("wget --method=HEAD " $9))
  122. print
  123. }
  124. ' > snapshot.idx.new
  125. xz snapshot.idx.new
  126. (xzcat snapshot.complete.idx.xz || :; cat additional) | sort -u > snapshot.complete.idx.new
  127. xz snapshot.complete.idx.new
  128. fi
  129. rm additional
  130. # install new indexes
  131. if [ x"$more" = x"1" ]; then
  132. mv snapshot.idx.new snapshot.idx
  133. mv snapshot.complete.idx.new snapshot.complete.idx
  134. else
  135. chmod 644 snapshot.idx.new.xz snapshot.complete.idx.new.xz
  136. mv snapshot.idx.new.xz snapshot.idx.xz
  137. mv snapshot.complete.idx.new.xz snapshot.complete.idx.xz
  138. fi
  139. rmdir "$0.running"