2017-08-29 21:13:29 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
#Here is a script to deploy cert to an AVM FRITZ!Box router.
|
|
|
|
|
|
|
|
#returns 0 means success, otherwise error.
|
|
|
|
|
|
|
|
#DEPLOY_FRITZBOX_USERNAME="username"
|
|
|
|
#DEPLOY_FRITZBOX_PASSWORD="password"
|
|
|
|
#DEPLOY_FRITZBOX_URL="https://fritz.box"
|
|
|
|
|
2017-08-29 22:24:31 +00:00
|
|
|
# Kudos to wikrie at Github for his FRITZ!Box update script:
|
|
|
|
# https://gist.github.com/wikrie/f1d5747a714e0a34d0582981f7cb4cfb
|
|
|
|
|
2017-08-29 21:13:29 +00:00
|
|
|
######## Public functions #####################
|
|
|
|
|
|
|
|
#domain keyfile certfile cafile fullchain
|
|
|
|
fritzbox_deploy() {
|
|
|
|
_cdomain="$1"
|
|
|
|
_ckey="$2"
|
|
|
|
_ccert="$3"
|
|
|
|
_cca="$4"
|
|
|
|
_cfullchain="$5"
|
|
|
|
|
|
|
|
_debug _cdomain "$_cdomain"
|
|
|
|
_debug _ckey "$_ckey"
|
|
|
|
_debug _ccert "$_ccert"
|
|
|
|
_debug _cca "$_cca"
|
|
|
|
_debug _cfullchain "$_cfullchain"
|
|
|
|
|
2017-08-29 21:53:41 +00:00
|
|
|
if ! _exists iconv; then
|
2020-11-28 14:22:14 +00:00
|
|
|
if ! _exists uconv; then
|
|
|
|
if ! _exists perl; then
|
|
|
|
_err "iconv or uconv or perl not found"
|
|
|
|
return 1
|
|
|
|
fi
|
2018-09-09 23:24:20 +00:00
|
|
|
fi
|
2017-08-29 21:13:29 +00:00
|
|
|
fi
|
|
|
|
|
2022-01-06 15:20:43 +00:00
|
|
|
# Clear traces of incorrectly stored values
|
|
|
|
_clearaccountconf DEPLOY_FRITZBOX_USERNAME
|
|
|
|
_clearaccountconf DEPLOY_FRITZBOX_PASSWORD
|
|
|
|
_clearaccountconf DEPLOY_FRITZBOX_URL
|
|
|
|
|
|
|
|
# Read config from saved values or env
|
|
|
|
_getdeployconf DEPLOY_FRITZBOX_USERNAME
|
|
|
|
_getdeployconf DEPLOY_FRITZBOX_PASSWORD
|
|
|
|
_getdeployconf DEPLOY_FRITZBOX_URL
|
|
|
|
|
|
|
|
_debug DEPLOY_FRITZBOX_URL "$DEPLOY_FRITZBOX_URL"
|
|
|
|
_debug DEPLOY_FRITZBOX_USERNAME "$DEPLOY_FRITZBOX_USERNAME"
|
|
|
|
_secure_debug DEPLOY_FRITZBOX_PASSWORD "$DEPLOY_FRITZBOX_PASSWORD"
|
|
|
|
|
|
|
|
if [ -z "$DEPLOY_FRITZBOX_USERNAME" ]; then
|
2017-08-29 21:13:29 +00:00
|
|
|
_err "FRITZ!Box username is not found, please define DEPLOY_FRITZBOX_USERNAME."
|
|
|
|
return 1
|
|
|
|
fi
|
2022-01-06 15:20:43 +00:00
|
|
|
if [ -z "$DEPLOY_FRITZBOX_PASSWORD" ]; then
|
2017-08-29 21:13:29 +00:00
|
|
|
_err "FRITZ!Box password is not found, please define DEPLOY_FRITZBOX_PASSWORD."
|
|
|
|
return 1
|
|
|
|
fi
|
2022-01-06 15:20:43 +00:00
|
|
|
if [ -z "$DEPLOY_FRITZBOX_URL" ]; then
|
2017-08-29 21:13:29 +00:00
|
|
|
_err "FRITZ!Box url is not found, please define DEPLOY_FRITZBOX_URL."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2022-01-06 15:20:43 +00:00
|
|
|
# Save current values
|
|
|
|
_savedeployconf DEPLOY_FRITZBOX_USERNAME "$DEPLOY_FRITZBOX_USERNAME"
|
|
|
|
_savedeployconf DEPLOY_FRITZBOX_PASSWORD "$DEPLOY_FRITZBOX_PASSWORD"
|
|
|
|
_savedeployconf DEPLOY_FRITZBOX_URL "$DEPLOY_FRITZBOX_URL"
|
2017-08-29 21:53:41 +00:00
|
|
|
|
2017-09-04 12:27:22 +00:00
|
|
|
# Do not check for a valid SSL certificate, because initially the cert is not valid, so it could not install the LE generated certificate
|
|
|
|
export HTTPS_INSECURE=1
|
|
|
|
|
2017-08-29 21:53:41 +00:00
|
|
|
_info "Log in to the FRITZ!Box"
|
2022-01-06 15:20:43 +00:00
|
|
|
_fritzbox_challenge="$(_get "${DEPLOY_FRITZBOX_URL}/login_sid.lua" | sed -e 's/^.*<Challenge>//' -e 's/<\/Challenge>.*$//')"
|
2018-09-09 23:24:20 +00:00
|
|
|
if _exists iconv; then
|
2022-01-06 15:20:43 +00:00
|
|
|
_fritzbox_hash="$(printf "%s-%s" "${_fritzbox_challenge}" "${DEPLOY_FRITZBOX_PASSWORD}" | iconv -f ASCII -t UTF16LE | _digest md5 hex)"
|
2020-11-28 14:22:14 +00:00
|
|
|
elif _exists uconv; then
|
2022-01-06 15:20:43 +00:00
|
|
|
_fritzbox_hash="$(printf "%s-%s" "${_fritzbox_challenge}" "${DEPLOY_FRITZBOX_PASSWORD}" | uconv -f ASCII -t UTF16LE | _digest md5 hex)"
|
2018-09-09 23:24:20 +00:00
|
|
|
else
|
2022-01-06 15:20:43 +00:00
|
|
|
_fritzbox_hash="$(printf "%s-%s" "${_fritzbox_challenge}" "${DEPLOY_FRITZBOX_PASSWORD}" | perl -p -e 'use Encode qw/encode/; print encode("UTF-16LE","$_"); $_="";' | _digest md5 hex)"
|
2018-09-09 23:24:20 +00:00
|
|
|
fi
|
2022-01-06 15:20:43 +00:00
|
|
|
_fritzbox_sid="$(_get "${DEPLOY_FRITZBOX_URL}/login_sid.lua?sid=0000000000000000&username=${DEPLOY_FRITZBOX_USERNAME}&response=${_fritzbox_challenge}-${_fritzbox_hash}" | sed -e 's/^.*<SID>//' -e 's/<\/SID>.*$//')"
|
2017-08-29 21:53:41 +00:00
|
|
|
|
2017-08-31 15:12:11 +00:00
|
|
|
if [ -z "${_fritzbox_sid}" ] || [ "${_fritzbox_sid}" = "0000000000000000" ]; then
|
2017-08-29 21:53:41 +00:00
|
|
|
_err "Logging in to the FRITZ!Box failed. Please check username, password and URL."
|
|
|
|
return 1
|
|
|
|
fi
|
2017-08-29 21:13:29 +00:00
|
|
|
|
|
|
|
_info "Generate form POST request"
|
|
|
|
_post_request="$(_mktemp)"
|
|
|
|
_post_boundary="---------------------------$(date +%Y%m%d%H%M%S)"
|
2017-09-04 12:07:10 +00:00
|
|
|
# _CERTPASSWORD_ is unset because Let's Encrypt certificates don't have a password. But if they ever do, here's the place to use it!
|
|
|
|
_CERTPASSWORD_=
|
2017-08-31 15:12:11 +00:00
|
|
|
{
|
2017-09-04 12:48:27 +00:00
|
|
|
printf -- "--"
|
|
|
|
printf -- "%s\r\n" "${_post_boundary}"
|
2017-08-31 15:25:08 +00:00
|
|
|
printf "Content-Disposition: form-data; name=\"sid\"\r\n\r\n%s\r\n" "${_fritzbox_sid}"
|
2017-09-04 12:48:27 +00:00
|
|
|
printf -- "--"
|
|
|
|
printf -- "%s\r\n" "${_post_boundary}"
|
2017-08-31 15:25:08 +00:00
|
|
|
printf "Content-Disposition: form-data; name=\"BoxCertPassword\"\r\n\r\n%s\r\n" "${_CERTPASSWORD_}"
|
2017-09-04 12:48:27 +00:00
|
|
|
printf -- "--"
|
|
|
|
printf -- "%s\r\n" "${_post_boundary}"
|
2017-08-31 15:25:08 +00:00
|
|
|
printf "Content-Disposition: form-data; name=\"BoxCertImportFile\"; filename=\"BoxCert.pem\"\r\n"
|
|
|
|
printf "Content-Type: application/octet-stream\r\n\r\n"
|
2017-09-04 12:40:28 +00:00
|
|
|
cat "${_ckey}" "${_cfullchain}"
|
2017-08-31 15:25:08 +00:00
|
|
|
printf "\r\n"
|
2017-09-04 12:48:27 +00:00
|
|
|
printf -- "--"
|
|
|
|
printf -- "%s--" "${_post_boundary}"
|
2017-08-31 15:12:11 +00:00
|
|
|
} >>"${_post_request}"
|
2017-08-29 21:13:29 +00:00
|
|
|
|
|
|
|
_info "Upload certificate to the FRITZ!Box"
|
|
|
|
|
2017-09-04 12:27:22 +00:00
|
|
|
export _H1="Content-type: multipart/form-data boundary=${_post_boundary}"
|
2022-01-06 15:20:43 +00:00
|
|
|
_post "$(cat "${_post_request}")" "${DEPLOY_FRITZBOX_URL}/cgi-bin/firmwarecfg" | grep SSL
|
2017-09-04 12:27:22 +00:00
|
|
|
|
|
|
|
retval=$?
|
2017-09-04 12:30:40 +00:00
|
|
|
if [ $retval = 0 ]; then
|
2017-09-04 12:27:22 +00:00
|
|
|
_info "Upload successful"
|
|
|
|
else
|
|
|
|
_err "Upload failed"
|
|
|
|
fi
|
2017-08-29 21:13:29 +00:00
|
|
|
rm "${_post_request}"
|
|
|
|
|
2017-09-04 12:27:22 +00:00
|
|
|
return $retval
|
2017-08-29 21:13:29 +00:00
|
|
|
}
|