ipv6_controller.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/usr/bin/env bash
  2. # _modules/scripts/ipv6_controller.sh
  3. # THIS SCRIPT IS DESIGNED TO BE RUNNING BY MAILCOW SCRIPTS ONLY!
  4. # DO NOT, AGAIN, NOT TRY TO RUN THIS SCRIPT STANDALONE!!!!!!
  5. # 1) Check if the host supports IPv6
  6. get_ipv6_support() {
  7. # ---- helper: probe external IPv6 connectivity without DNS ----
  8. _probe_ipv6_connectivity() {
  9. # Use literal, always-on IPv6 echo responders (no DNS required)
  10. local PROBE_IPS=("2001:4860:4860::8888" "2606:4700:4700::1111")
  11. local ip rc=1
  12. for ip in "${PROBE_IPS[@]}"; do
  13. if command -v ping6 &>/dev/null; then
  14. ping6 -c1 -W2 "$ip" &>/dev/null || ping6 -c1 -w2 "$ip" &>/dev/null
  15. rc=$?
  16. elif command -v ping &>/dev/null; then
  17. ping -6 -c1 -W2 "$ip" &>/dev/null || ping -6 -c1 -w2 "$ip" &>/dev/null
  18. rc=$?
  19. else
  20. rc=1
  21. fi
  22. [[ $rc -eq 0 ]] && return 0
  23. done
  24. return 1
  25. }
  26. if [[ ! -f /proc/net/if_inet6 ]] || grep -qs '^1' /proc/sys/net/ipv6/conf/all/disable_ipv6 2>/dev/null; then
  27. DETECTED_IPV6=false
  28. echo -e "${YELLOW}IPv6 not detected on host – ${LIGHT_RED}IPv6 is administratively disabled${YELLOW}.${NC}"
  29. return
  30. fi
  31. if ip -6 route show default 2>/dev/null | grep -qE '^default'; then
  32. echo -e "${YELLOW}Default IPv6 route found – testing external IPv6 connectivity...${NC}"
  33. if _probe_ipv6_connectivity; then
  34. DETECTED_IPV6=true
  35. echo -e "IPv6 detected on host – ${LIGHT_GREEN}leaving IPv6 support enabled${YELLOW}.${NC}"
  36. else
  37. DETECTED_IPV6=false
  38. echo -e "${YELLOW}Default IPv6 route present but external IPv6 connectivity failed – ${LIGHT_RED}disabling IPv6 support${YELLOW}.${NC}"
  39. fi
  40. return
  41. fi
  42. if ip -6 addr show scope global 2>/dev/null | grep -q 'inet6'; then
  43. DETECTED_IPV6=false
  44. echo -e "${YELLOW}Global IPv6 address present but no default route – ${LIGHT_RED}disabling IPv6 support${YELLOW}.${NC}"
  45. return
  46. fi
  47. if ip -6 addr show scope link 2>/dev/null | grep -q 'inet6'; then
  48. echo -e "${YELLOW}Only link-local IPv6 addresses found – testing external IPv6 connectivity...${NC}"
  49. if _probe_ipv6_connectivity; then
  50. DETECTED_IPV6=true
  51. echo -e "External IPv6 connectivity available – ${LIGHT_GREEN}leaving IPv6 support enabled${YELLOW}.${NC}"
  52. else
  53. DETECTED_IPV6=false
  54. echo -e "${YELLOW}Only link-local IPv6 present and no external connectivity – ${LIGHT_RED}disabling IPv6 support${YELLOW}.${NC}"
  55. fi
  56. return
  57. fi
  58. DETECTED_IPV6=false
  59. echo -e "${YELLOW}IPv6 not detected on host – ${LIGHT_RED}disabling IPv6 support${YELLOW}.${NC}"
  60. }
  61. # 2) Ensure Docker daemon.json has (or create) the required IPv6 settings
  62. docker_daemon_edit(){
  63. DOCKER_DAEMON_CONFIG="/etc/docker/daemon.json"
  64. DOCKER_MAJOR=$(docker version --format '{{.Server.Version}}' 2>/dev/null | cut -d. -f1)
  65. MISSING=()
  66. _has_kv() { grep -Eq "\"$1\"[[:space:]]*:[[:space:]]*$2" "$DOCKER_DAEMON_CONFIG" 2>/dev/null; }
  67. if [[ -f "$DOCKER_DAEMON_CONFIG" ]]; then
  68. # reject empty or whitespace-only file immediately
  69. if [[ ! -s "$DOCKER_DAEMON_CONFIG" ]] || ! grep -Eq '[{}]' "$DOCKER_DAEMON_CONFIG"; then
  70. echo -e "${RED}ERROR: $DOCKER_DAEMON_CONFIG exists but is empty or contains no JSON braces – please initialize it with valid JSON (e.g. {}).${NC}"
  71. exit 1
  72. fi
  73. # Validate JSON if jq is present
  74. if command -v jq &>/dev/null && ! jq empty "$DOCKER_DAEMON_CONFIG" &>/dev/null; then
  75. echo -e "${RED}ERROR: Invalid JSON in $DOCKER_DAEMON_CONFIG – please correct manually.${NC}"
  76. exit 1
  77. fi
  78. # Gather missing keys
  79. ! _has_kv ipv6 true && MISSING+=("ipv6: true")
  80. # For Docker < 28, keep requiring fixed-cidr-v6 (default bridge needs it on old engines)
  81. if [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 28 ]]; then
  82. ! grep -Eq '"fixed-cidr-v6"[[:space:]]*:[[:space:]]*".+"' "$DOCKER_DAEMON_CONFIG" \
  83. && MISSING+=('fixed-cidr-v6: "fd00:dead:beef:c0::/80"')
  84. fi
  85. # For Docker < 27, ip6tables needed and was tied to experimental in older releases
  86. if [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 27 ]]; then
  87. _has_kv ipv6 true && ! _has_kv ip6tables true && MISSING+=("ip6tables: true")
  88. ! _has_kv experimental true && MISSING+=("experimental: true")
  89. fi
  90. # Fix if needed
  91. if ((${#MISSING[@]}>0)); then
  92. echo -e "${MAGENTA}Your daemon.json is missing: ${YELLOW}${MISSING[*]}${NC}"
  93. if [[ -n "$FORCE" ]]; then
  94. ans=Y
  95. else
  96. read -p "Would you like to update $DOCKER_DAEMON_CONFIG now? [Y/n] " ans
  97. ans=${ans:-Y}
  98. fi
  99. if [[ $ans =~ ^[Yy]$ ]]; then
  100. cp "$DOCKER_DAEMON_CONFIG" "${DOCKER_DAEMON_CONFIG}.bak"
  101. if command -v jq &>/dev/null; then
  102. TMP=$(mktemp)
  103. # Base filter: ensure ipv6 = true
  104. JQ_FILTER='.ipv6 = true'
  105. # Add fixed-cidr-v6 only for Docker < 28
  106. if [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 28 ]]; then
  107. JQ_FILTER+=' | .["fixed-cidr-v6"] = (.["fixed-cidr-v6"] // "fd00:dead:beef:c0::/80")'
  108. fi
  109. # Add ip6tables/experimental only for Docker < 27
  110. if [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 27 ]]; then
  111. JQ_FILTER+=' | .ip6tables = true | .experimental = true'
  112. fi
  113. jq "$JQ_FILTER" "$DOCKER_DAEMON_CONFIG" >"$TMP" && mv "$TMP" "$DOCKER_DAEMON_CONFIG"
  114. echo -e "${LIGHT_GREEN}daemon.json updated. Restarting Docker...${NC}"
  115. (command -v systemctl &>/dev/null && systemctl restart docker) || service docker restart
  116. echo -e "${YELLOW}Docker restarted.${NC}"
  117. else
  118. echo -e "${RED}Please install jq or manually update daemon.json and restart Docker.${NC}"
  119. exit 1
  120. fi
  121. else
  122. echo -e "${YELLOW}User declined Docker update – skipping Docker daemon configuration.${NC}"
  123. echo -e "${YELLOW}IPv6 will be disabled for mailcow.${NC}"
  124. echo ""
  125. echo -e "${YELLOW}If you change your mind later, please insert these changes manually to $DOCKER_DAEMON_CONFIG:${NC}"
  126. echo "${MISSING[*]}"
  127. echo ""
  128. return 1
  129. fi
  130. fi
  131. else
  132. # Create new daemon.json if missing
  133. if [[ -n "$FORCE" ]]; then
  134. ans=Y
  135. else
  136. read -p "$DOCKER_DAEMON_CONFIG not found. Create it with IPv6 settings? [Y/n] " ans
  137. ans=${ans:-Y}
  138. fi
  139. if [[ $ans =~ ^[Yy]$ ]]; then
  140. mkdir -p "$(dirname "$DOCKER_DAEMON_CONFIG")"
  141. if [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 27 ]]; then
  142. cat > "$DOCKER_DAEMON_CONFIG" <<EOF
  143. {
  144. "ipv6": true,
  145. "fixed-cidr-v6": "fd00:dead:beef:c0::/80",
  146. "ip6tables": true,
  147. "experimental": true
  148. }
  149. EOF
  150. elif [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 28 ]]; then
  151. cat > "$DOCKER_DAEMON_CONFIG" <<EOF
  152. {
  153. "ipv6": true,
  154. "fixed-cidr-v6": "fd00:dead:beef:c0::/80"
  155. }
  156. EOF
  157. else
  158. # Docker 28+: ipv6 works without fixed-cidr-v6
  159. cat > "$DOCKER_DAEMON_CONFIG" <<EOF
  160. {
  161. "ipv6": true
  162. }
  163. EOF
  164. fi
  165. echo -e "${GREEN}Created $DOCKER_DAEMON_CONFIG with IPv6 settings.${NC}"
  166. echo "Restarting Docker..."
  167. (command -v systemctl &>/dev/null && systemctl restart docker) || service docker restart
  168. echo "Docker restarted."
  169. else
  170. echo -e "${YELLOW}User declined to create daemon.json – skipping Docker daemon configuration.${NC}"
  171. echo -e "${YELLOW}IPv6 will be disabled for mailcow.${NC}"
  172. echo ""
  173. echo -e "${YELLOW}If you change your mind later, please create $DOCKER_DAEMON_CONFIG with these settings:${NC}"
  174. if [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 27 ]]; then
  175. echo ' "ipv6": true,'
  176. echo ' "fixed-cidr-v6": "fd00:dead:beef:c0::/80",'
  177. echo ' "ip6tables": true,'
  178. echo ' "experimental": true'
  179. elif [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 28 ]]; then
  180. echo ' "ipv6": true,'
  181. echo ' "fixed-cidr-v6": "fd00:dead:beef:c0::/80"'
  182. else
  183. echo ' "ipv6": true'
  184. fi
  185. echo ""
  186. return 1
  187. fi
  188. fi
  189. }
  190. # 3) Main wrapper for generate_config.sh and update.sh
  191. configure_ipv6() {
  192. # detect manual override if mailcow.conf is present
  193. if [[ -n "$MAILCOW_CONF" && -f "$MAILCOW_CONF" ]] && grep -q '^ENABLE_IPV6=' "$MAILCOW_CONF"; then
  194. MANUAL_SETTING=$(grep '^ENABLE_IPV6=' "$MAILCOW_CONF" | cut -d= -f2)
  195. elif [[ -z "$MAILCOW_CONF" ]] && [[ -n "${ENABLE_IPV6:-}" ]]; then
  196. MANUAL_SETTING="$ENABLE_IPV6"
  197. else
  198. MANUAL_SETTING=""
  199. fi
  200. get_ipv6_support
  201. # if user manually set it, check for mismatch
  202. if [[ "$DETECTED_IPV6" != "true" ]]; then
  203. if [[ -n "$MAILCOW_CONF" && -f "$MAILCOW_CONF" ]]; then
  204. if grep -q '^ENABLE_IPV6=' "$MAILCOW_CONF"; then
  205. sed -i 's/^ENABLE_IPV6=.*/ENABLE_IPV6=false/' "$MAILCOW_CONF"
  206. else
  207. echo "ENABLE_IPV6=false" >> "$MAILCOW_CONF"
  208. fi
  209. else
  210. export IPV6_BOOL=false
  211. fi
  212. echo "Skipping Docker IPv6 configuration because host does not support IPv6."
  213. echo "Make sure to check if your docker daemon.json does not include \"enable_ipv6\": true if you do not want IPv6."
  214. echo "IPv6 configuration complete: ENABLE_IPV6=false"
  215. sleep 2
  216. return
  217. fi
  218. if ! docker_daemon_edit; then
  219. # User declined Docker daemon configuration
  220. # When called from update.sh, MAILCOW_CONF is set and we modify the existing file
  221. # When called from generate_config.sh, MAILCOW_CONF is not set and we export IPV6_BOOL
  222. if [[ -n "$MAILCOW_CONF" && -f "$MAILCOW_CONF" ]]; then
  223. if grep -q '^ENABLE_IPV6=' "$MAILCOW_CONF"; then
  224. sed -i 's/^ENABLE_IPV6=.*/ENABLE_IPV6=false/' "$MAILCOW_CONF"
  225. else
  226. echo "ENABLE_IPV6=false" >> "$MAILCOW_CONF"
  227. fi
  228. else
  229. export IPV6_BOOL=false
  230. fi
  231. echo "IPv6 configuration complete: ENABLE_IPV6=false"
  232. return 0
  233. fi
  234. if [[ -n "$MAILCOW_CONF" && -f "$MAILCOW_CONF" ]]; then
  235. if grep -q '^ENABLE_IPV6=' "$MAILCOW_CONF"; then
  236. sed -i 's/^ENABLE_IPV6=.*/ENABLE_IPV6=true/' "$MAILCOW_CONF"
  237. else
  238. echo "ENABLE_IPV6=true" >> "$MAILCOW_CONF"
  239. fi
  240. else
  241. export IPV6_BOOL=true
  242. fi
  243. echo "IPv6 configuration complete: ENABLE_IPV6=true"
  244. }