From f7c7d8c5991d78e5666de3b69ee63844266eaf21 Mon Sep 17 00:00:00 2001 From: Lukas Schauer Date: Sat, 16 Jan 2016 21:40:12 +0100 Subject: [PATCH] =?utf8?q?use=20different=20sed=20version=20for=20differen?= =?utf8?q?t=20system=20types=20(mostly=20for=20compatibility=20with=20anci?= =?utf8?q?ent=20linux=20distros=20nobody=20should=20be=20using=20anyway=20?= =?utf8?q?=C2=AF\=5F(=E3=83=84)=5F/=C2=AF)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- letsencrypt.sh | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/letsencrypt.sh b/letsencrypt.sh index 6657f39..fa4e156 100755 --- a/letsencrypt.sh +++ b/letsencrypt.sh @@ -12,7 +12,7 @@ BASEDIR="${SCRIPTDIR}" check_dependencies() { curl -V > /dev/null 2>&1 || _exiterr "This script requires curl." openssl version > /dev/null 2>&1 || _exiterr "This script requires an openssl binary." - sed -E "" < /dev/null > /dev/null 2>&1 || _exiterr "This script requires sed with support for extended (modern) regular expressions." + _sed "" < /dev/null > /dev/null 2>&1 || _exiterr "This script requires sed with support for extended (modern) regular expressions." grep -V > /dev/null 2>&1 || _exiterr "This script requires grep." mktemp -u -t XXXXXX > /dev/null 2>&1 || _exiterr "This script requires mktemp." } @@ -134,6 +134,15 @@ init_system() { fi } +# Different sed version for different os types... +_sed() { + if [ "${OSTYPE}" = "Linux" ]; then + sed -r "${@}" + else + sed -E "${@}" + fi +} + # Print error message and exit with error _exiterr() { echo "ERROR: ${1}" >&2 @@ -143,13 +152,13 @@ _exiterr() { # Encode data as url-safe formatted base64 urlbase64() { # urlbase64: base64 encoded string with '+' replaced with '-' and '/' replaced with '_' - openssl base64 -e | tr -d '\n\r' | sed -e 's:=*$::g' -e 'y:+/:-_:' + openssl base64 -e | tr -d '\n\r' | _sed -e 's:=*$::g' -e 'y:+/:-_:' } # Convert hex string to binary data hex2bin() { # Remove spaces, add leading zero, escape as hex string and parse with printf - printf -- "$(cat | sed -E -e 's/[[:space:]]//g' -e 's/^(.(.{2})*)$/0\1/' -e 's/(.{2})/\\x\1/g')" + printf -- "$(cat | _sed -e 's/[[:space:]]//g' -e 's/^(.(.{2})*)$/0\1/' -e 's/(.{2})/\\x\1/g')" } # Get string value from json dictionary @@ -280,7 +289,7 @@ sign_domain() { challenges="$(printf '%s\n' "${response}" | grep -Eo '"challenges":[^\[]*\[[^]]*]')" repl=$'\n''{' # fix syntax highlighting in Vim challenge="$(printf "%s" "${challenges//\{/${repl}}" | grep \""${CHALLENGETYPE}"\")" - challenge_token="$(printf '%s' "${challenge}" | get_json_string_value token | sed 's/[^A-Za-z0-9_\-]/_/g')" + challenge_token="$(printf '%s' "${challenge}" | get_json_string_value token | _sed 's/[^A-Za-z0-9_\-]/_/g')" challenge_uri="$(printf '%s' "${challenge}" | get_json_string_value uri)" if [[ -z "${challenge_token}" ]] || [[ -z "${challenge_uri}" ]]; then @@ -381,7 +390,7 @@ command_sign_domains() { fi # Generate certificates for all domains found in domains.txt. Check if existing certificate are about to expire - <"${DOMAINS_TXT}" sed -E -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*$//g' -e 's/[[:space:]]+/ /g' | (grep -vE '^(#|$)' || true) | while read -r line; do + <"${DOMAINS_TXT}" _sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*$//g' -e 's/[[:space:]]+/ /g' | (grep -vE '^(#|$)' || true) | while read -r line; do domain="$(printf '%s\n' "${line}" | cut -d' ' -f1)" morenames="$(printf '%s\n' "${line}" | cut -s -d' ' -f2-)" cert="${BASEDIR}/certs/${domain}/cert.pem" @@ -397,8 +406,8 @@ command_sign_domains() { if [[ -e "${cert}" ]]; then printf " + Checking domain name(s) of existing cert..." - certnames="$(openssl x509 -in "${cert}" -text -noout | grep DNS: | sed 's/DNS://g' | tr -d ' ' | tr ',' '\n' | sort -u | tr '\n' ' ' | sed 's/ $//')" - givennames="$(echo "${domain}" "${morenames}"| tr ' ' '\n' | sort -u | tr '\n' ' ' | sed 's/ $//' | sed 's/^ //')" + certnames="$(openssl x509 -in "${cert}" -text -noout | grep DNS: | _sed 's/DNS://g' | tr -d ' ' | tr ',' '\n' | sort -u | tr '\n' ' ' | _sed 's/ $//')" + givennames="$(echo "${domain}" "${morenames}"| tr ' ' '\n' | sort -u | tr '\n' ' ' | _sed 's/ $//' | _sed 's/^ //')" if [[ "${certnames}" = "${givennames}" ]]; then echo " unchanged." @@ -502,6 +511,8 @@ command_env() { # Main method (parses script arguments and calls command_* methods) main() { + OSTYPE="$(uname)" + COMMAND="" set_command() { [[ -z "${COMMAND}" ]] || _exiterr "Only one command can be executed at a time. See help (-h) for more information." -- 2.39.5