functions.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/bash
  2. log_f() {
  3. if [[ ${2} == "no_nl" ]]; then
  4. echo -n "$(date) - ${1}"
  5. elif [[ ${2} == "no_date" ]]; then
  6. echo "${1}"
  7. elif [[ ${2} != "redis_only" ]]; then
  8. echo "$(date) - ${1}"
  9. fi
  10. if [[ ${3} == "b64" ]]; then
  11. ${REDIS_CMDLINE} LPUSH ACME_LOG "{\"time\":\"$(date +%s)\",\"message\":\"base64,$(printf '%s' "${MAILCOW_HOSTNAME} - ${1}")\"}" > /dev/null
  12. else
  13. ${REDIS_CMDLINE} LPUSH ACME_LOG "{\"time\":\"$(date +%s)\",\"message\":\"$(printf '%s' "${MAILCOW_HOSTNAME} - ${1}" | \
  14. tr '%&;$"[]{}-\r\n' ' ')\"}" > /dev/null
  15. fi
  16. }
  17. verify_email(){
  18. regex="^(([A-Za-z0-9]+((\.|\-|\_|\+)?[A-Za-z0-9]?)*[A-Za-z0-9]+)|[A-Za-z0-9]+)@(([A-Za-z0-9]+)+((\.|\-|\_)?([A-Za-z0-9]+)+)*)+\.([A-Za-z]{2,})+$"
  19. if [[ $1 =~ ${regex} ]]; then
  20. return 0
  21. else
  22. return 1
  23. fi
  24. }
  25. verify_hash_match(){
  26. CERT_HASH=$(openssl x509 -in "${1}" -noout -pubkey | openssl md5)
  27. KEY_HASH=$(openssl pkey -in "${2}" -pubout | openssl md5)
  28. if [[ ${CERT_HASH} != ${KEY_HASH} ]]; then
  29. log_f "Certificate and key hashes do not match!"
  30. return 1
  31. else
  32. log_f "Verified hashes."
  33. return 0
  34. fi
  35. }
  36. get_ipv4(){
  37. local IPV4=
  38. local IPV4_SRCS=
  39. local TRY=
  40. IPV4_SRCS[0]="ip4.mailcow.email"
  41. IPV4_SRCS[1]="ip4.nevondo.com"
  42. until [[ ! -z ${IPV4} ]] || [[ ${TRY} -ge 10 ]]; do
  43. IPV4=$(curl --connect-timeout 3 -m 10 -L4s ${IPV4_SRCS[$RANDOM % ${#IPV4_SRCS[@]} ]} | grep -E "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")
  44. [[ ! -z ${TRY} ]] && sleep 1
  45. TRY=$((TRY+1))
  46. done
  47. echo ${IPV4}
  48. }
  49. get_ipv6(){
  50. local IPV6=
  51. local IPV6_SRCS=
  52. local TRY=
  53. IPV6_SRCS[0]="ip6.mailcow.email"
  54. IPV6_SRCS[1]="ip6.nevondo.com"
  55. until [[ ! -z ${IPV6} ]] || [[ ${TRY} -ge 10 ]]; do
  56. IPV6=$(curl --connect-timeout 3 -m 10 -L6s ${IPV6_SRCS[$RANDOM % ${#IPV6_SRCS[@]} ]} | grep "^\([0-9a-fA-F]\{0,4\}:\)\{1,7\}[0-9a-fA-F]\{0,4\}$")
  57. [[ ! -z ${TRY} ]] && sleep 1
  58. TRY=$((TRY+1))
  59. done
  60. echo ${IPV6}
  61. }
  62. check_domain(){
  63. DOMAIN=$1
  64. A_DOMAIN=$(dig A ${DOMAIN} +short | tail -n 1)
  65. AAAA_DOMAIN=$(dig AAAA ${DOMAIN} +short | tail -n 1)
  66. # Hard-fail on CAA errors for MAILCOW_HOSTNAME
  67. PARENT_DOMAIN=$(echo ${DOMAIN} | cut -d. -f2-)
  68. CAAS=( $(dig CAA ${PARENT_DOMAIN} +short | sed -n 's/\d issue "\(.*\)"/\1/p') )
  69. if [[ ! -z ${CAAS} ]]; then
  70. if [[ ${CAAS[@]} =~ "letsencrypt.org" ]]; then
  71. log_f "Validated CAA for parent domain ${PARENT_DOMAIN}"
  72. else
  73. log_f "Lets Encrypt disallowed for ${PARENT_DOMAIN} by CAA record"
  74. return 1
  75. fi
  76. fi
  77. # Check if CNAME without v6 enabled target
  78. if [[ ! -z ${AAAA_DOMAIN} ]] && [[ -z $(echo ${AAAA_DOMAIN} | grep "^\([0-9a-fA-F]\{0,4\}:\)\{1,7\}[0-9a-fA-F]\{0,4\}$") ]]; then
  79. AAAA_DOMAIN=
  80. fi
  81. if [[ ! -z ${AAAA_DOMAIN} ]]; then
  82. log_f "Found AAAA record for ${DOMAIN}: ${AAAA_DOMAIN} - skipping A record check"
  83. if [[ $(expand ${IPV6:-"0000:0000:0000:0000:0000:0000:0000:0000"}) == $(expand ${AAAA_DOMAIN}) ]] || [[ ${SKIP_IP_CHECK} == "y" ]] || [[ ${SNAT6_TO_SOURCE} != "n" ]]; then
  84. if verify_challenge_path "${DOMAIN}" 6; then
  85. log_f "Confirmed AAAA record with IP $(expand ${AAAA_DOMAIN})"
  86. return 0
  87. else
  88. log_f "Confirmed AAAA record with IP $(expand ${AAAA_DOMAIN}), but HTTP validation failed"
  89. fi
  90. else
  91. log_f "Cannot match your IP $(expand ${IPV6:-"0000:0000:0000:0000:0000:0000:0000:0000"}) against hostname ${DOMAIN} (DNS returned $(expand ${AAAA_DOMAIN}))"
  92. fi
  93. elif [[ ! -z ${A_DOMAIN} ]]; then
  94. log_f "Found A record for ${DOMAIN}: ${A_DOMAIN}"
  95. if [[ ${IPV4:-ERR} == ${A_DOMAIN} ]] || [[ ${SKIP_IP_CHECK} == "y" ]] || [[ ${SNAT_TO_SOURCE} != "n" ]]; then
  96. if verify_challenge_path "${DOMAIN}" 4; then
  97. log_f "Confirmed A record ${A_DOMAIN}"
  98. return 0
  99. else
  100. log_f "Confirmed A record with IP ${A_DOMAIN}, but HTTP validation failed"
  101. fi
  102. else
  103. log_f "Cannot match your IP ${IPV4} against hostname ${DOMAIN} (DNS returned ${A_DOMAIN})"
  104. fi
  105. else
  106. log_f "No A or AAAA record found for hostname ${DOMAIN}"
  107. fi
  108. return 1
  109. }
  110. verify_challenge_path(){
  111. if [[ ${SKIP_HTTP_VERIFICATION} == "y" ]]; then
  112. echo '(skipping check, returning 0)'
  113. return 0
  114. fi
  115. # verify_challenge_path URL 4|6
  116. RANDOM_N=${RANDOM}${RANDOM}${RANDOM}
  117. echo ${RANDOM_N} > /var/www/acme/${RANDOM_N}
  118. if [[ "$(curl --insecure -${2} -L http://${1}/.well-known/acme-challenge/${RANDOM_N} --silent)" == "${RANDOM_N}" ]]; then
  119. rm /var/www/acme/${RANDOM_N}
  120. return 0
  121. else
  122. rm /var/www/acme/${RANDOM_N}
  123. return 1
  124. fi
  125. }