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.
 
 
 
 

494 lines
11 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. STOREDIR="$HOME/.snapaid"
  30. KEYS="0x524F0C37A0B946A3"
  31. KEY_URLS='https://svnweb.freebsd.org/doc/head/share/pgpkeys/gjb.key?view=co'
  32. setdefaults() {
  33. GPG=$(which gpg2)
  34. WGET=$(which wget)
  35. SHASUM=$(which shasum)
  36. }
  37. setdefaults
  38. if [ ! -x "$GPG" ]; then
  39. echo 'Failed to find gpg2 executable'
  40. exit 1
  41. fi
  42. if [ ! -x "$WGET" ]; then
  43. echo 'Failed to find wget executable'
  44. exit 1
  45. fi
  46. if [ ! -x "$SHASUM" ]; then
  47. echo 'Failed to find shasum executable'
  48. exit 1
  49. fi
  50. #wget:
  51. # -N for timestamps
  52. # --backups=x for backing up
  53. hostname=people.FreeBSD.org
  54. hostname=www.funkthat.com
  55. completeurl="https://${hostname}/~jmg/FreeBSD-snap/snapshot.complete.idx.xz"
  56. currenturl="https://${hostname}/~jmg/FreeBSD-snap/snapshot.idx.xz"
  57. # type release arch platform date svnrev xxx fname url mid
  58. # 1 2 3 4 5 6 7 8 9 10
  59. # iso 11.1-STABLE arm-armv6 BEAGLEBONE 20180315 r330998 xxx FreeBSD-11.1-STABLE-arm-armv6-BEAGLEBONE-20180315-r330998.img.xz https://download.freebsd.org/ftp/snapshots/ISO-IMAGES/11.1/FreeBSD-11.1-STABLE-arm-armv6-BEAGLEBONE-20180315-r330998.img.xz 20180316000842.GA7399@FreeBSD.org
  60. set -e
  61. # This is used for some testing functions
  62. copy_function() {
  63. declare -F "$1" > /dev/null || return 1
  64. local func="$(declare -f "$1")"
  65. eval "${2}(${func#*\(}"
  66. }
  67. # Test function to cause a bad input
  68. cmd_failure() {
  69. exit 1
  70. }
  71. # First time fails, second time run real command
  72. gpg_first_fails() {
  73. copy_function verifygpg_orig verifygpg
  74. return 1
  75. }
  76. # When first arg is --, just touch the file to fake a bad d/l
  77. bad_file_dl() {
  78. if [ x"$1" = x"--" ]; then
  79. touch $(basename "$2")
  80. else
  81. $WGET_orig "$@"
  82. fi
  83. }
  84. # Make sure that the storage directory is present
  85. mkstore() {
  86. mkdir "$STOREDIR" 2>/dev/null || :
  87. if ! [ -x "$STOREDIR" -a -w "$STOREDIR" -a -d "$STOREDIR" ]; then
  88. echo "$STOREDIR is not a writable directory."
  89. fi
  90. }
  91. check_keys() {
  92. for i in $KEYS; do
  93. if ! $GPG --list-keys "$i" >/dev/null 2>&1; then
  94. return 1
  95. fi
  96. done
  97. return 0
  98. }
  99. # Given a message id, get the raw body and store it.
  100. get_raw() {
  101. mkstore
  102. mid="$1"
  103. midfile="$STOREDIR/$mid".raw
  104. if [ ! -e "$midfile" ]; then
  105. # get the location, it's a database lookup
  106. loc=$($WGET --max-redirect=0 --method=HEAD -S -o - -O - 'https://docs.freebsd.org/cgi/mid.cgi?'"$mid" 2>/dev/null | awk 'tolower($1) == "location:" { print $2; exit }')
  107. # Some emails are sent to both -current and -snapshot,
  108. # we can't handle them for now
  109. # such as 20160529215940.GA11785@FreeBSD.org
  110. if [ x"$loc" = x"" ]; then
  111. return 1
  112. fi
  113. # if it's relative, add https
  114. if [ x"$loc" != x"${loc#//}" ]; then
  115. # add https
  116. loc="https:$loc"
  117. fi
  118. # get the raw part
  119. tmpfile="$STOREDIR/.tmp.$$.$mid".raw
  120. # strip out everything but message id and first signed part
  121. $WGET -O - "$loc"+raw 2>/dev/null | awk '
  122. tolower($1) == "message-id:" && check == 0 {
  123. print
  124. }
  125. $0 == "-----BEGIN PGP SIGNED MESSAGE-----" {
  126. sigbody = 1
  127. }
  128. sigbody {
  129. print
  130. }
  131. $0 == "-----END PGP SIGNATURE-----" {
  132. sigbody = 0
  133. }' > "$tmpfile"
  134. if verifygpg "$tmpfile"; then
  135. mv "$tmpfile" "$STOREDIR/$mid.raw"
  136. else
  137. rm "$tmpfile"
  138. echo Bad signature from mail archive.
  139. return 1
  140. fi
  141. else
  142. if ! verifygpg "$midfile"; then
  143. rm "$midfile"
  144. get_raw "$mid"
  145. return $?
  146. fi
  147. fi
  148. }
  149. fetch() {
  150. mkstore
  151. if ! (cd "$STOREDIR" && $WGET -N "$1" >/dev/null 2>&1); then
  152. return 1
  153. fi
  154. }
  155. getvermid() {
  156. xzcat "$STOREDIR"/snapshot.complete.idx.xz | awk '$8 == fname {
  157. print $10
  158. }' fname="$i"
  159. }
  160. # takes basename of arg, which much exist in STOREDIR, and verifies
  161. # that the signature is valid.
  162. verifygpg() {
  163. local fname
  164. fname=$(basename "$1")
  165. if ! (cd "$STOREDIR" && $GPG --verify "$fname" 2> /dev/null); then
  166. echo 'ERROR: PGP signature verification failed!'
  167. return 1
  168. fi
  169. }
  170. # Verifies the file
  171. verifyfile() {
  172. local fname
  173. local hashinfo
  174. local algo hash
  175. fname="$STOREDIR/${1}.raw"
  176. hashinfo=$(awk '
  177. check && $2 == "('"$2"')" {
  178. hashes[$1] = $4
  179. }
  180. $0 == "-----BEGIN PGP SIGNED MESSAGE-----" {
  181. check = 1
  182. }
  183. $0 == "-----BEGIN PGP SIGNATURE-----" {
  184. check = 0
  185. }
  186. END {
  187. if ("SHA512" in hashes)
  188. algo = "SHA512"
  189. else if ("SHA256" in hashes)
  190. algo = "SHA256"
  191. else {
  192. print "unkn BADHASH"
  193. exit 1
  194. }
  195. print algo " " hashes[algo]
  196. }
  197. ' "$fname")
  198. read algo hash <<-EOF
  199. ${hashinfo}
  200. EOF
  201. if [ x"$algo" == x"unkn" -o x"$algo" = x"" ]; then
  202. echo 'Unable to find hash for file.'
  203. exit 1
  204. fi
  205. echo "$hash $2" | $SHASUM -a "${algo#SHA}" -c -
  206. }
  207. dlverify() {
  208. fname="$8"
  209. dlurl="$9"
  210. vermid="${10}"
  211. # verify snap email
  212. if ! get_raw "$vermid"; then
  213. echo "Unable to fetch/verify snapshot email for: $fname"
  214. return 1
  215. fi
  216. if ! [ -f $(basename "$dlurl") ]; then
  217. # fetch link
  218. $WGET -- "$dlurl"
  219. else
  220. echo 'Image already exists, verifying...'
  221. fi
  222. if ! verifyfile "$vermid" "$fname"; then
  223. echo 'Removing bad file.'
  224. rm "$fname"
  225. return 1
  226. fi
  227. }
  228. if ! check_keys; then
  229. echo 'Necessary keys have not been imported into key ring.'
  230. echo "Please obtain they following keyid(s):"
  231. echo $KEYS
  232. echo ""
  233. echo "The keys may be obtained from the following URLs:"
  234. for i in $KEY_URLS; do
  235. echo "$i"
  236. done
  237. echo ""
  238. echo "and imported into GPG w/ the --import option."
  239. echo ""
  240. echo "For extra security, additional verification should be done, such"
  241. echo "as manually verifying finger prints."
  242. exit 3
  243. fi
  244. if [ x"$1" = x"verify" ]; then
  245. shift
  246. if ! fetch "$completeurl"; then
  247. echo Failed to fetch the complete index.
  248. exit 1
  249. fi
  250. for i in "$@"; do
  251. vermid=$(getvermid "$i")
  252. if [ x"$vermid" = x"" ]; then
  253. echo "Unable to find entry for: $i"
  254. continue
  255. fi
  256. if ! get_raw "$vermid"; then
  257. echo "Unable to fetch snapshot email for: $i"
  258. continue
  259. fi
  260. verifyfile "$vermid" "$i"
  261. done
  262. elif [ x"$1" = x"find" ]; then
  263. fetch "$currenturl"
  264. tmpdir=$(mktemp -d -t snapaid)
  265. trap "rm -r $tmpdir" 0
  266. ( cd "$tmpdir";
  267. xzcat "$STOREDIR"/snapshot.idx.xz | sort -r -k 5 > selection;
  268. while :; do
  269. # display current list
  270. cnt=$(wc -l < selection)
  271. awk '
  272. BEGIN {
  273. fmtstr = "%2s %-3s %-15s %-18s %-18s %-8s %-7s\n"
  274. printf(fmtstr, "#", "TYP", "RELEASE", "ARCH", "PLATFORM/TYPE", "DATE", "SVNREV")
  275. cnt = 1
  276. }
  277. {
  278. if ($4 == "xxx")
  279. plt=$7
  280. else
  281. plt=$4
  282. printf(fmtstr, cnt, $1, $2, $3, plt, $5, $6)
  283. if (cnt >= 20)
  284. exit 0
  285. cnt += 1
  286. }
  287. ' selection
  288. read -p 'Select image #, enter search term, reset, or quit: ' sel
  289. if [ x"$sel" = x"reset" ]; then
  290. xzcat "$STOREDIR"/snapshot.idx.xz | sort -r -k 5 > selection;
  291. continue
  292. elif [ x"$sel" = x"quit" ]; then
  293. echo "$sel" > sel
  294. break
  295. fi
  296. if [ "$cnt" -gt 20 ]; then
  297. cnt=20
  298. fi
  299. if [ "$sel" -ge 1 -a "$sel" -le "$cnt" ] 2>/dev/null; then
  300. echo $(tail -n +"$sel" selection | head -n 1) > sel
  301. break
  302. else
  303. # restrict
  304. if ! grep -- "$sel" selection > selection.new; then
  305. echo WARNING: Ignoring selection, no results.
  306. else
  307. mv selection.new selection
  308. fi
  309. fi
  310. done
  311. )
  312. sel=$(cat "$tmpdir"/sel)
  313. if [ x"$sel" = x"quit" ]; then
  314. exit 0
  315. fi
  316. set -- ${sel}
  317. echo selected image "$8"
  318. dlverify ${sel}
  319. elif [ x"$1" = x"test" ]; then
  320. # Setup test environment
  321. tmpdir=$(mktemp -d -t snapaid)
  322. trap "rm -r $tmpdir" 0
  323. cd "$tmpdir"
  324. STOREDIR="$tmpdir"/snapaid
  325. # Make sure that the check keys function works.
  326. echo 'Testing check_keys works...'
  327. # Prime the custom keyring
  328. GPG="gpg2 --no-default-keyring --keyring pubring.gpg"
  329. for i in $KEY_URLS; do
  330. $WGET -O - -- "$i" 2>/dev/null | $GPG --import 2>/dev/null
  331. done
  332. if ! check_keys; then
  333. echo failed
  334. exit 1
  335. fi
  336. KEYS_orig="$KEYS"
  337. KEYS="0x1384923867573928" # bogus key
  338. if check_keys; then
  339. echo failed
  340. exit 1
  341. fi
  342. echo passed
  343. # Test a bad download fails
  344. echo 'Testing dlverify...'
  345. WGET_orig="$WGET"
  346. WGET=bad_file_dl
  347. # if dlverify is successsful, then it's a failure
  348. if dlverify iso 13.0-CURRENT sparc64 xxx 20181026 r339752 bootonly FreeBSD-13.0-CURRENT-sparc64-20181026-r339752-bootonly.iso.xz https://download.freebsd.org/ftp/snapshots/ISO-IMAGES/13.0/FreeBSD-13.0-CURRENT-sparc64-20181026-r339752-bootonly.iso.xz 20181026184443.GD75350@FreeBSD.org; then
  349. echo 'failed'
  350. exit 1
  351. fi
  352. # Make sure that a bad d/l was not left behind
  353. if [ -e FreeBSD-13.0-CURRENT-sparc64-20181026-r339752-bootonly.iso.xz ]; then
  354. echo failed
  355. exit 1
  356. fi
  357. echo passed
  358. # Test getting the raw file
  359. echo 'Testing get_raw success...'
  360. mid='20160122055622.GA87581@FreeBSD.org'
  361. get_raw "$mid"
  362. # Verify resulsts
  363. (cd "$STOREDIR" && echo '6e53df5995b6cc423c7f2d63b6df52d5d7f70e8586c25f91433fd8a1a2466e77be6a38884bde8bedd9ff6e7deb0215a66e1c2a16e4955503c20445e649a5fb47 20160122055622.GA87581@FreeBSD.org.raw' | $SHASUM -a 512 -c)
  364. echo passed
  365. # If the file already exists, but fails verification, that
  366. # it will refetch and be correct
  367. echo 'Testing get_raw with file already present that fails verification...'
  368. copy_function verifygpg verifygpg_orig
  369. copy_function gpg_first_fails verifygpg
  370. get_raw "$mid"
  371. (cd "$STOREDIR" && echo '6e53df5995b6cc423c7f2d63b6df52d5d7f70e8586c25f91433fd8a1a2466e77be6a38884bde8bedd9ff6e7deb0215a66e1c2a16e4955503c20445e649a5fb47 20160122055622.GA87581@FreeBSD.org.raw' | $SHASUM -a 512 -c)
  372. echo passed
  373. # If the file already exists, a "broken" wget won't cause
  374. # a problem
  375. echo 'Testing get_raw with file already present...'
  376. WGET=cmd_failure
  377. get_raw "$mid"
  378. echo passed
  379. # Test failure
  380. echo 'Testing get_raw fails w/ bad data...'
  381. WGET=cmd_failure
  382. rm "$STOREDIR/$mid.raw"
  383. # it should fail
  384. ! get_raw "$mid"
  385. # and the desired file should not exist
  386. if [ -e "$STOREDIR/$mid.raw" ]; then
  387. echo 'Test failed!'
  388. exit 1;
  389. fi
  390. echo passed
  391. setdefaults
  392. echo tests completed!!!
  393. else
  394. if [ $# -gt 0 ]; then
  395. echo "Unknown verb: $1"
  396. fi
  397. echo "Usage:"
  398. echo " $0 verify file ..."
  399. echo " $0 find"
  400. echo ""
  401. echo "The verify option will attempt to verify each file specified."
  402. echo ""
  403. echo "The find option will start up an interactive session to find"
  404. echo "and select the snapshot to download and verify."
  405. fi