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.

136 lines
4.9 KiB

  1. #!/usr/bin/env bash
  2. # Modified script from here: https://github.com/FarsetLabs/letsencrypt-helper-scripts/blob/master/letsencrypt-unifi.sh
  3. # Modified by: Brielle Bruns <bruns@2mbit.com>
  4. # Download URL: https://source.sosdg.org/brielle/lets-encrypt-scripts
  5. # Version: 1.2
  6. # Last Changed: 03/24/2016
  7. # 02/02/2016: Fixed some errors with key export/import, removed lame docker requirements
  8. # 02/27/2016: More verbose progress report
  9. # 03/08/2016: Add renew option, reformat code, command line options
  10. # 03/24/2016: More sanity checking, embedding cert
  11. PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  12. while getopts "ird:e:" opt; do
  13. case $opt in
  14. i) onlyinsert="yes";;
  15. r) renew="yes";;
  16. d) domains+=("$OPTARG");;
  17. e) email=("$OPTARG");;
  18. esac
  19. done
  20. # Location of LetsEncrypt binary we use
  21. LEBINARY="/usr/src/letsencrypt/certbot-auto"
  22. # Command line options depending on New or Renew.
  23. NEWCERT="--renew-by-default certonly"
  24. RENEWCERT="-n renew"
  25. if [[ ! -x ${LEBINARY} ]]; then
  26. echo "Error: LetsEncrypt binary not found in ${LEBINARY} !"
  27. echo "You'll need to do one of the following:"
  28. echo "1) Change LEBINARY variable in this script"
  29. echo "2) Install LE manually or via your package manager and do #1"
  30. echo "3) Use the included get-letsencrypt.sh script to install it"
  31. exit 1
  32. fi
  33. if [[ ! -z ${email} ]]; then
  34. email="--email ${email}"
  35. else
  36. email=""
  37. fi
  38. shift $((OPTIND -1))
  39. for val in "${domains[@]}"; do
  40. DOMAINS="${DOMAINS} -d ${val} "
  41. done
  42. MAINDOMAIN=${domains[0]}
  43. if [[ -z ${MAINDOMAIN} ]]; then
  44. echo "Error: At least one -d argument is required"
  45. exit 1
  46. fi
  47. if [[ ${renew} == "yes" ]]; then
  48. LEOPTIONS=${RENEWCERT}
  49. else
  50. LEOPTIONS="${email} ${DOMAINS} ${NEWCERT}"
  51. fi
  52. if [[ ${onlyinsert} != "yes" ]]; then
  53. echo "Firing up standalone authenticator on TCP port 443 and requesting cert..."
  54. ${LEBINARY} \
  55. --server https://acme-v01.api.letsencrypt.org/directory \
  56. --agree-tos \
  57. --standalone --standalone-supported-challenges tls-sni-01 \
  58. ${LEOPTIONS}
  59. fi
  60. if `md5sum -c /etc/letsencrypt/live/${MAINDOMAIN}/cert.pem.md5 &>/dev/null`; then
  61. echo "Cert has not changed, not updating controller."
  62. exit 0
  63. else
  64. TEMPFILE=$(mktemp)
  65. CERTTEMPFILE=$(mktemp)
  66. # Identrust cross-signed CA cert needed by the java keystore for import.
  67. # Can get original here: https://www.identrust.com/certificates/trustid/root-download-x3.html
  68. cat > ${CERTTEMPFILE} <<'_EOF'
  69. -----BEGIN CERTIFICATE-----
  70. MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/
  71. MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
  72. DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow
  73. PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD
  74. Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
  75. AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O
  76. rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq
  77. OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b
  78. xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw
  79. 7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD
  80. aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV
  81. HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG
  82. SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69
  83. ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr
  84. AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz
  85. R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5
  86. JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo
  87. Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ
  88. -----END CERTIFICATE-----
  89. _EOF
  90. echo "Cert has changed, updating controller..."
  91. md5sum /etc/letsencrypt/live/${MAINDOMAIN}/cert.pem > /etc/letsencrypt/live/${MAINDOMAIN}/cert.pem.md5
  92. echo "Using openssl to prepare certificate..."
  93. openssl pkcs12 -export -passout pass:aircontrolenterprise \
  94. -in /etc/letsencrypt/live/${MAINDOMAIN}/cert.pem \
  95. -inkey /etc/letsencrypt/live/${MAINDOMAIN}/privkey.pem \
  96. -out ${TEMPFILE} -name unifi \
  97. -CAfile /etc/letsencrypt/live/${MAINDOMAIN}/chain.pem -caname root
  98. echo "Stopping Unifi controller..."
  99. service unifi stop
  100. echo "Removing existing certificate from Unifi protected keystore..."
  101. keytool -delete -alias unifi -keystore /usr/lib/unifi/data/keystore \
  102. -deststorepass aircontrolenterprise
  103. echo "Inserting certificate into Unifi keystore..."
  104. keytool -trustcacerts -importkeystore \
  105. -deststorepass aircontrolenterprise \
  106. -destkeypass aircontrolenterprise \
  107. -destkeystore /usr/lib/unifi/data/keystore \
  108. -srckeystore ${TEMPFILE} -srcstoretype PKCS12 \
  109. -srcstorepass aircontrolenterprise \
  110. -alias unifi
  111. rm -f ${TEMPFILE}
  112. echo "Importing cert into Unifi database..."
  113. java -jar /usr/lib/unifi/lib/ace.jar import_cert \
  114. /etc/letsencrypt/live/${MAINDOMAIN}/cert.pem \
  115. /etc/letsencrypt/live/${MAINDOMAIN}/chain.pem \
  116. ${CERTTEMPFILE}
  117. rm -f ${CERTTEMPFILE}
  118. echo "Starting Unifi controller..."
  119. service unifi start
  120. echo "Done!"
  121. fi