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.

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