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.

85 lines
3.2 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. _info "Log in in to the FRITZ!Box"
  47. _fritzbox_challenge="$(wget -q -O - ${_fritzbox_url}/login_sid.lua | sed -e 's/^.*<Challenge>//' -e 's/<\/Challenge>.*$//')"
  48. _fritzbox_hash="$(echo -n ${_fritzbox_challenge}-${_fritzbox_password} | iconv -f ASCII -t UTF16LE | md5sum | awk '{print $1}')"
  49. _fritzbox_sid="$(wget -q -O - ${_fritzbox_url}/login_sid.lua?sid=0000000000000000\&username=${_frithbox_username}\&response=${_fritzbox_challenge}-${_fritzbox_hash} | sed -e 's/^.*<SID>//' -e 's/<\/SID>.*$//')"
  50. _info "Generate form POST request"
  51. _post_request="$(_mktemp)"
  52. _post_boundary="---------------------------$(date +%Y%m%d%H%M%S)"
  53. printf -- "--${_post_boundary}\r\n" >> "${_post_request}"
  54. printf "Content-Disposition: form-data; name=\"sid\"\r\n\r\n${_fritzbox_sid}\r\n" >> "${_post_request}"
  55. printf -- "--${_post_boundary}\r\n" >> "${_post_request}"
  56. # _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!
  57. printf "Content-Disposition: form-data; name=\"BoxCertPassword\"\r\n\r\n${_CERTPASSWORD_}\r\n" >> "${_post_request}"
  58. printf -- "--${_post_boundary}\r\n" >> "${_post_request}"
  59. printf "Content-Disposition: form-data; name=\"BoxCertImportFile\"; filename=\"BoxCert.pem\"\r\n" >> "${_post_request}"
  60. printf "Content-Type: application/octet-stream\r\n\r\n" >> "${_post_request}"
  61. cat "${_ckey}" >> "${_post_request}"
  62. cat "${_cfullchain}" >> "${_post_request}"
  63. printf "\r\n" >> "${_post_request}"
  64. printf -- "--${_post_boundary}--" >> "${_post_request}"
  65. _info "Upload certificate to the FRITZ!Box"
  66. wget -q -O - "${_fritzbox_url}/cgi-bin/firmwarecfg" --header="Content-type: multipart/form-data boundary=${_post_boundary}" --post-file "${_post_request}"
  67. _info "Upload successful"
  68. rm "${_post_request}"
  69. return 0
  70. }