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.

126 lines
4.5 KiB

  1. #!/usr/bin/env sh
  2. #Here is a script to deploy cert to an AVM FRITZ!Box router.
  3. #returns 0 means success, otherwise error.
  4. #DEPLOY_FRITZBOX_USERNAME="username"
  5. #DEPLOY_FRITZBOX_PASSWORD="password"
  6. #DEPLOY_FRITZBOX_URL="https://fritz.box"
  7. # Kudos to wikrie at Github for his FRITZ!Box update script:
  8. # https://gist.github.com/wikrie/f1d5747a714e0a34d0582981f7cb4cfb
  9. ######## Public functions #####################
  10. #domain keyfile certfile cafile fullchain
  11. fritzbox_deploy() {
  12. _cdomain="$1"
  13. _ckey="$2"
  14. _ccert="$3"
  15. _cca="$4"
  16. _cfullchain="$5"
  17. _debug _cdomain "$_cdomain"
  18. _debug _ckey "$_ckey"
  19. _debug _ccert "$_ccert"
  20. _debug _cca "$_cca"
  21. _debug _cfullchain "$_cfullchain"
  22. if ! _exists iconv; then
  23. if ! _exists uconv; then
  24. if ! _exists perl; then
  25. _err "iconv or uconv or perl not found"
  26. return 1
  27. fi
  28. fi
  29. fi
  30. # Clear traces of incorrectly stored values
  31. _clearaccountconf DEPLOY_FRITZBOX_USERNAME
  32. _clearaccountconf DEPLOY_FRITZBOX_PASSWORD
  33. _clearaccountconf DEPLOY_FRITZBOX_URL
  34. # Read config from saved values or env
  35. _getdeployconf DEPLOY_FRITZBOX_USERNAME
  36. _getdeployconf DEPLOY_FRITZBOX_PASSWORD
  37. _getdeployconf DEPLOY_FRITZBOX_URL
  38. _debug DEPLOY_FRITZBOX_URL "$DEPLOY_FRITZBOX_URL"
  39. _debug DEPLOY_FRITZBOX_USERNAME "$DEPLOY_FRITZBOX_USERNAME"
  40. _secure_debug DEPLOY_FRITZBOX_PASSWORD "$DEPLOY_FRITZBOX_PASSWORD"
  41. if [ -z "$DEPLOY_FRITZBOX_USERNAME" ]; then
  42. _err "FRITZ!Box username is not found, please define DEPLOY_FRITZBOX_USERNAME."
  43. return 1
  44. fi
  45. if [ -z "$DEPLOY_FRITZBOX_PASSWORD" ]; then
  46. _err "FRITZ!Box password is not found, please define DEPLOY_FRITZBOX_PASSWORD."
  47. return 1
  48. fi
  49. if [ -z "$DEPLOY_FRITZBOX_URL" ]; then
  50. _err "FRITZ!Box url is not found, please define DEPLOY_FRITZBOX_URL."
  51. return 1
  52. fi
  53. # Save current values
  54. _savedeployconf DEPLOY_FRITZBOX_USERNAME "$DEPLOY_FRITZBOX_USERNAME"
  55. _savedeployconf DEPLOY_FRITZBOX_PASSWORD "$DEPLOY_FRITZBOX_PASSWORD"
  56. _savedeployconf DEPLOY_FRITZBOX_URL "$DEPLOY_FRITZBOX_URL"
  57. # Do not check for a valid SSL certificate, because initially the cert is not valid, so it could not install the LE generated certificate
  58. export HTTPS_INSECURE=1
  59. _info "Log in to the FRITZ!Box"
  60. _fritzbox_challenge="$(_get "${DEPLOY_FRITZBOX_URL}/login_sid.lua" | sed -e 's/^.*<Challenge>//' -e 's/<\/Challenge>.*$//')"
  61. if _exists iconv; then
  62. _fritzbox_hash="$(printf "%s-%s" "${_fritzbox_challenge}" "${DEPLOY_FRITZBOX_PASSWORD}" | iconv -f ASCII -t UTF16LE | _digest md5 hex)"
  63. elif _exists uconv; then
  64. _fritzbox_hash="$(printf "%s-%s" "${_fritzbox_challenge}" "${DEPLOY_FRITZBOX_PASSWORD}" | uconv -f ASCII -t UTF16LE | _digest md5 hex)"
  65. else
  66. _fritzbox_hash="$(printf "%s-%s" "${_fritzbox_challenge}" "${DEPLOY_FRITZBOX_PASSWORD}" | perl -p -e 'use Encode qw/encode/; print encode("UTF-16LE","$_"); $_="";' | _digest md5 hex)"
  67. fi
  68. _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>.*$//')"
  69. if [ -z "${_fritzbox_sid}" ] || [ "${_fritzbox_sid}" = "0000000000000000" ]; then
  70. _err "Logging in to the FRITZ!Box failed. Please check username, password and URL."
  71. return 1
  72. fi
  73. _info "Generate form POST request"
  74. _post_request="$(_mktemp)"
  75. _post_boundary="---------------------------$(date +%Y%m%d%H%M%S)"
  76. # _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!
  77. _CERTPASSWORD_=
  78. {
  79. printf -- "--"
  80. printf -- "%s\r\n" "${_post_boundary}"
  81. printf "Content-Disposition: form-data; name=\"sid\"\r\n\r\n%s\r\n" "${_fritzbox_sid}"
  82. printf -- "--"
  83. printf -- "%s\r\n" "${_post_boundary}"
  84. printf "Content-Disposition: form-data; name=\"BoxCertPassword\"\r\n\r\n%s\r\n" "${_CERTPASSWORD_}"
  85. printf -- "--"
  86. printf -- "%s\r\n" "${_post_boundary}"
  87. printf "Content-Disposition: form-data; name=\"BoxCertImportFile\"; filename=\"BoxCert.pem\"\r\n"
  88. printf "Content-Type: application/octet-stream\r\n\r\n"
  89. cat "${_ckey}" "${_cfullchain}"
  90. printf "\r\n"
  91. printf -- "--"
  92. printf -- "%s--" "${_post_boundary}"
  93. } >>"${_post_request}"
  94. _info "Upload certificate to the FRITZ!Box"
  95. export _H1="Content-type: multipart/form-data boundary=${_post_boundary}"
  96. _post "$(cat "${_post_request}")" "${DEPLOY_FRITZBOX_URL}/cgi-bin/firmwarecfg" | grep SSL
  97. retval=$?
  98. if [ $retval = 0 ]; then
  99. _info "Upload successful"
  100. else
  101. _err "Upload failed"
  102. fi
  103. rm "${_post_request}"
  104. return $retval
  105. }