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.

102 lines
3.7 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 wget; then
  23. _err "wget not found"
  24. return 1
  25. fi
  26. if ! _exists iconv; then
  27. _err "iconv not found"
  28. return 1
  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. _info "Log in to the FRITZ!Box"
  52. _fritzbox_challenge="$(wget --no-check-certificate -q -O - "${_fritzbox_url}/login_sid.lua" | sed -e 's/^.*<Challenge>//' -e 's/<\/Challenge>.*$//')"
  53. _fritzbox_hash="$(echo -n "${_fritzbox_challenge}-${_fritzbox_password}" | iconv -f ASCII -t UTF16LE | md5sum | awk '{print $1}')"
  54. _fritzbox_sid="$(wget --no-check-certificate -q -O - "${_fritzbox_url}/login_sid.lua?sid=0000000000000000&username=${_fritzbox_username}&response=${_fritzbox_challenge}-${_fritzbox_hash}" | sed -e 's/^.*<SID>//' -e 's/<\/SID>.*$//')"
  55. if [ -z "${_fritzbox_sid}" ] || [ "${_fritzbox_sid}" = "0000000000000000" ]; then
  56. _err "Logging in to the FRITZ!Box failed. Please check username, password and URL."
  57. return 1
  58. fi
  59. _info "Generate form POST request"
  60. _post_request="$(_mktemp)"
  61. _post_boundary="---------------------------$(date +%Y%m%d%H%M%S)"
  62. {
  63. printf -- "--%s\r\n" "${_post_boundary}"
  64. printf "Content-Disposition: form-data; name=\"sid\"\r\n\r\n%s\r\n" "${_fritzbox_sid}"
  65. printf -- "--%s\r\n" "${_post_boundary}"
  66. } >>"${_post_request}"
  67. # _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!
  68. _CERTPASSWORD_=
  69. {
  70. printf "Content-Disposition: form-data; name=\"BoxCertPassword\"\r\n\r\n%s\r\n" "${_CERTPASSWORD_}"
  71. printf -- "--%s\r\n" "${_post_boundary}"
  72. printf "Content-Disposition: form-data; name=\"BoxCertImportFile\"; filename=\"BoxCert.pem\"\r\n"
  73. printf "Content-Type: application/octet-stream\r\n\r\n"
  74. } >>"${_post_request}"
  75. cat "${_ckey}" "${_cfullchain}" >>"${_post_request}"
  76. {
  77. printf "\r\n"
  78. printf -- "--%s--" "${_post_boundary}"
  79. } >>"${_post_request}"
  80. _info "Upload certificate to the FRITZ!Box"
  81. wget --no-check-certificate -q -O - "${_fritzbox_url}/cgi-bin/firmwarecfg" --header="Content-type: multipart/form-data boundary=${_post_boundary}" --post-file "${_post_request}" | grep SSL
  82. _info "Upload successful"
  83. rm "${_post_request}"
  84. return 0
  85. }