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.

108 lines
3.8 KiB

7 years ago
  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. _err "iconv not found"
  24. return 1
  25. fi
  26. _fritzbox_username="${DEPLOY_FRITZBOX_USERNAME}"
  27. _fritzbox_password="${DEPLOY_FRITZBOX_PASSWORD}"
  28. _fritzbox_url="${DEPLOY_FRITZBOX_URL}"
  29. _debug _fritzbox_url "$_fritzbox_url"
  30. _debug _fritzbox_username "$_fritzbox_username"
  31. _secure_debug _fritzbox_password "$_fritzbox_password"
  32. if [ -z "$_fritzbox_username" ]; then
  33. _err "FRITZ!Box username is not found, please define DEPLOY_FRITZBOX_USERNAME."
  34. return 1
  35. fi
  36. if [ -z "$_fritzbox_password" ]; then
  37. _err "FRITZ!Box password is not found, please define DEPLOY_FRITZBOX_PASSWORD."
  38. return 1
  39. fi
  40. if [ -z "$_fritzbox_url" ]; then
  41. _err "FRITZ!Box url is not found, please define DEPLOY_FRITZBOX_URL."
  42. return 1
  43. fi
  44. _saveaccountconf DEPLOY_FRITZBOX_USERNAME "${_fritzbox_username}"
  45. _saveaccountconf DEPLOY_FRITZBOX_PASSWORD "${_fritzbox_password}"
  46. _saveaccountconf DEPLOY_FRITZBOX_URL "${_fritzbox_url}"
  47. # Do not check for a valid SSL certificate, because initially the cert is not valid, so it could not install the LE generated certificate
  48. export HTTPS_INSECURE=1
  49. _info "Log in to the FRITZ!Box"
  50. _fritzbox_challenge="$(_get "${_fritzbox_url}/login_sid.lua" | sed -e 's/^.*<Challenge>//' -e 's/<\/Challenge>.*$//')"
  51. _fritzbox_hash="$(printf "%s-%s" "${_fritzbox_challenge}" "${_fritzbox_password}" | iconv -f ASCII -t UTF16LE | md5sum | awk '{print $1}')"
  52. _fritzbox_sid="$(_get "${_fritzbox_url}/login_sid.lua?sid=0000000000000000&username=${_fritzbox_username}&response=${_fritzbox_challenge}-${_fritzbox_hash}" | sed -e 's/^.*<SID>//' -e 's/<\/SID>.*$//')"
  53. if [ -z "${_fritzbox_sid}" ] || [ "${_fritzbox_sid}" = "0000000000000000" ]; then
  54. _err "Logging in to the FRITZ!Box failed. Please check username, password and URL."
  55. return 1
  56. fi
  57. _info "Generate form POST request"
  58. _post_request="$(_mktemp)"
  59. _post_boundary="---------------------------$(date +%Y%m%d%H%M%S)"
  60. # _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!
  61. _CERTPASSWORD_=
  62. {
  63. printf -- "--"
  64. printf -- "%s\r\n" "${_post_boundary}"
  65. printf "Content-Disposition: form-data; name=\"sid\"\r\n\r\n%s\r\n" "${_fritzbox_sid}"
  66. printf -- "--"
  67. printf -- "%s\r\n" "${_post_boundary}"
  68. printf "Content-Disposition: form-data; name=\"BoxCertPassword\"\r\n\r\n%s\r\n" "${_CERTPASSWORD_}"
  69. printf -- "--"
  70. printf -- "%s\r\n" "${_post_boundary}"
  71. printf "Content-Disposition: form-data; name=\"BoxCertImportFile\"; filename=\"BoxCert.pem\"\r\n"
  72. printf "Content-Type: application/octet-stream\r\n\r\n"
  73. cat "${_ckey}" "${_cfullchain}"
  74. printf "\r\n"
  75. printf -- "--"
  76. printf -- "%s--" "${_post_boundary}"
  77. } >>"${_post_request}"
  78. _info "Upload certificate to the FRITZ!Box"
  79. export _H1="Content-type: multipart/form-data boundary=${_post_boundary}"
  80. _post "$(cat "${_post_request}")" "${_fritzbox_url}/cgi-bin/firmwarecfg" | grep SSL
  81. retval=$?
  82. if [ $retval = 0 ]; then
  83. _info "Upload successful"
  84. else
  85. _err "Upload failed"
  86. fi
  87. rm "${_post_request}"
  88. return $retval
  89. }