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.
 
 
 
 

155 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. mkdir "$0.running"
  76. if [ x"$complete" = x"1" ]; then
  77. sort -u snapshot.complete.idx | xz > snapshot.complete.idx.xz
  78. awk '$5 >= "'"$1"'" {
  79. if (!system("wget --method=HEAD " $9))
  80. print
  81. }
  82. ' snapshot.idx | sort -u | xz > snapshot.idx.xz
  83. rm snapshot.idx snapshot.complete.idx
  84. rmdir "$0.running"
  85. exit 0
  86. fi
  87. # minimize file
  88. tmpfname="tmp.snapinf.asc"
  89. awk '
  90. output != 1 && tolower($1) == "message-id:" {
  91. print
  92. next
  93. }
  94. $0 == "-----BEGIN PGP SIGNED MESSAGE-----" {
  95. output = 1
  96. }
  97. output == 1 {
  98. print
  99. }
  100. $0 == "-----END PGP SIGNATURE-----" {
  101. output = 0
  102. }' "$1" > "$tmpfname"
  103. if ! gpg --verify "$tmpfname"; then
  104. echo 'failed verify'
  105. rm "$tmpfname"
  106. rmdir "$0.running"
  107. exit 1
  108. fi
  109. # process file
  110. awk -f ./mksnapidx.awk "$tmpfname" > additional
  111. rm "$tmpfname"
  112. # only check if there isn't more to come
  113. if [ x"$more" = x"1" ]; then
  114. (cat snapshot.idx || :; cat additional) > snapshot.idx.new
  115. (cat snapshot.complete.idx || :; cat additional) > snapshot.complete.idx.new
  116. else
  117. (xzcat snapshot.idx.xz; cat additional) | sort -u | awk '
  118. {
  119. if (!system("wget --method=HEAD " $9))
  120. print
  121. }
  122. ' > snapshot.idx.new
  123. xz snapshot.idx.new
  124. (xzcat snapshot.complete.idx.xz || :; cat additional) | sort -u > snapshot.complete.idx.new
  125. xz snapshot.complete.idx.new
  126. fi
  127. rm additional
  128. # install new indexes
  129. if [ x"$more" = x"1" ]; then
  130. mv snapshot.idx.new snapshot.idx
  131. mv snapshot.complete.idx.new snapshot.complete.idx
  132. else
  133. mv snapshot.idx.new.xz snapshot.idx.xz
  134. mv snapshot.complete.idx.new.xz snapshot.complete.idx.xz
  135. fi
  136. rmdir "$0.running"