diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-16 18:38:49 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-16 18:38:49 +1200 |
commit | 0b1c99ffb1c3019e8afb9dcc542fbc5e868baef1 (patch) | |
tree | 9712aa161c25232e0f6561ee181b82eb42af347a /bin/urlc.m4 | |
parent | Put "all" subtargets on their own line (diff) | |
download | dotfiles-0b1c99ffb1c3019e8afb9dcc542fbc5e868baef1.tar.gz dotfiles-0b1c99ffb1c3019e8afb9dcc542fbc5e868baef1.zip |
Template traps in scripts depending on mktd(1)
Diffstat (limited to 'bin/urlc.m4')
-rw-r--r-- | bin/urlc.m4 | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/bin/urlc.m4 b/bin/urlc.m4 new file mode 100644 index 00000000..c49c1ecf --- /dev/null +++ b/bin/urlc.m4 @@ -0,0 +1,64 @@ +#!/bin/sh +# Try to find erroneous or insecure URLs +self=urlc + +# cURL request timeout +tm=${URLCHECK_TIMEOUT:-8} + +include(`include/mktd.trap.sh') +# Create buffer files for the headers and body content, to be cleaned up on +# exit +list=$td/list head=$td/head body=$td/body + +# Iterate through input; ignore leading/trailing whitespace +# shellcheck disable=SC2002 +cat -- "${@:--}" >"$list" +while read -r url ; do + + # Skip anything that doesn't start with HTTP + case $url in + http*) ;; + *) continue ;; + esac + + # Make initial request, log head and body to files, cry and skip on error + if ! curl -A Mozilla -fHLsS -D "$head" -m "$tm" -o "$body" -- \ + "$url" ; then + printf >&2 '%s: %s raises error\n' \ + "$self" "$url" + ex=1 + continue + fi + + # Iterate through header file, cry about the first redirect we find + while IFS=': ' read -r header value ; do + [ "$header" = 'Location' ] || continue + printf >&2 '%s: %s redirects to %s\n' \ + "$self" "$url" "$value" >&2 + ex=1 + break + done < "$head" + + # Skip anything that's already secure + case $url in + https*) continue ;; + *) ;; + esac + + # Form a naïve attempt at a possible secure URL and try to request it, + # point it out if it actually works + burl=${url#http://} + surl=https://$burl + if curl -A Mozilla -fLsS -D "$head" -m "$tm" -o "$body" -- \ + "$surl" 2>/dev/null ; then + printf >&2 '%s: %s has a working secure version at %s\n' \ + "$self" "$url" "$surl" + ex=1 + fi +done <"$list" + +# Wait for the input process to finish +wait + +# Exit if any errors +exit "${ex:-0}" |