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.

94 lines
3.6 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. ######## Public functions #####################
  8. #domain keyfile certfile cafile fullchain
  9. fritzbox_deploy() {
  10. _cdomain="$1"
  11. _ckey="$2"
  12. _ccert="$3"
  13. _cca="$4"
  14. _cfullchain="$5"
  15. _debug _cdomain "$_cdomain"
  16. _debug _ckey "$_ckey"
  17. _debug _ccert "$_ccert"
  18. _debug _cca "$_cca"
  19. _debug _cfullchain "$_cfullchain"
  20. if ! _exists wget; then
  21. _err "wget not found"
  22. return 1
  23. fi
  24. if ! _exists iconv; then
  25. _err "iconv not found"
  26. return 1
  27. fi
  28. _fritzbox_username="${DEPLOY_FRITZBOX_USERNAME}"
  29. _fritzbox_password="${DEPLOY_FRITZBOX_PASSWORD}"
  30. _fritzbox_url="${DEPLOY_FRITZBOX_URL}"
  31. _debug _fritzbox_url "$_fritzbox_url"
  32. _debug _fritzbox_usename "$_fritzbox_username"
  33. _secure_debug _fritzbox_password "$_fritzbox_password"
  34. if [ -z "$_fritzbox_username" ]; then
  35. _err "FRITZ!Box username is not found, please define DEPLOY_FRITZBOX_USERNAME."
  36. return 1
  37. fi
  38. if [ -z "$_fritzbox_password" ]; then
  39. _err "FRITZ!Box password is not found, please define DEPLOY_FRITZBOX_PASSWORD."
  40. return 1
  41. fi
  42. if [ -z "$_fritzbox_url" ]; then
  43. _err "FRITZ!Box url is not found, please define DEPLOY_FRITZBOX_URL."
  44. return 1
  45. fi
  46. _saveaccountconf DEPLOY_FRITZBOX_USERNAME "${_fritzbox_username}"
  47. _saveaccountconf DEPLOY_FRITZBOX_PASSWORD "${_fritzbox_password}"
  48. _saveaccountconf DEPLOY_FRITZBOX_URL "${_fritzbox_url}"
  49. _info "Log in to the FRITZ!Box"
  50. _fritzbox_challenge="$(wget -q -O - ${_fritzbox_url}/login_sid.lua | sed -e 's/^.*<Challenge>//' -e 's/<\/Challenge>.*$//')"
  51. _fritzbox_hash="$(echo -n ${_fritzbox_challenge}-${_fritzbox_password} | iconv -f ASCII -t UTF16LE | md5sum | awk '{print $1}')"
  52. _fritzbox_sid="$(wget -q -O - ${_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}" -o "${_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. printf -- "--${_post_boundary}\r\n" >> "${_post_request}"
  61. printf "Content-Disposition: form-data; name=\"sid\"\r\n\r\n${_fritzbox_sid}\r\n" >> "${_post_request}"
  62. printf -- "--${_post_boundary}\r\n" >> "${_post_request}"
  63. # _CERTPASSWORD_ is unset because Let's Encrypt certificates don't have a passwort. But if they ever do, here's the place to use it!
  64. _CERTPASSWORD_=
  65. printf "Content-Disposition: form-data; name=\"BoxCertPassword\"\r\n\r\n${_CERTPASSWORD_}\r\n" >> "${_post_request}"
  66. printf -- "--${_post_boundary}\r\n" >> "${_post_request}"
  67. printf "Content-Disposition: form-data; name=\"BoxCertImportFile\"; filename=\"BoxCert.pem\"\r\n" >> "${_post_request}"
  68. printf "Content-Type: application/octet-stream\r\n\r\n" >> "${_post_request}"
  69. cat "${_ckey}" >> "${_post_request}"
  70. cat "${_cfullchain}" >> "${_post_request}"
  71. printf "\r\n" >> "${_post_request}"
  72. printf -- "--${_post_boundary}--" >> "${_post_request}"
  73. _info "Upload certificate to the FRITZ!Box"
  74. wget -q -O - "${_fritzbox_url}/cgi-bin/firmwarecfg" --header="Content-type: multipart/form-data boundary=${_post_boundary}" --post-file "${_post_request}" | grep SSL
  75. _info "Upload successful"
  76. rm "${_post_request}"
  77. return 0
  78. }