update.sh 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. #!/usr/bin/env bash
  2. ############## Begin Function Section ##############
  3. check_online_status() {
  4. CHECK_ONLINE_DOMAINS=('https://github.com' 'https://hub.docker.com')
  5. for domain in "${CHECK_ONLINE_DOMAINS[@]}"; do
  6. if timeout 6 curl --head --silent --output /dev/null ${domain}; then
  7. return 0
  8. fi
  9. done
  10. return 1
  11. }
  12. prefetch_images() {
  13. [[ -z ${BRANCH} ]] && { echo -e "\e[33m\nUnknown branch...\e[0m"; exit 1; }
  14. git fetch origin #${BRANCH}
  15. while read image; do
  16. if [[ "${image}" == "robbertkl/ipv6nat" ]]; then
  17. if ! grep -qi "ipv6nat-mailcow" docker-compose.yml || grep -qi "enable_ipv6: false" docker-compose.yml; then
  18. continue
  19. fi
  20. fi
  21. RET_C=0
  22. until docker pull "${image}"; do
  23. RET_C=$((RET_C + 1))
  24. echo -e "\e[33m\nError pulling $image, retrying...\e[0m"
  25. [ ${RET_C} -gt 3 ] && { echo -e "\e[31m\nToo many failed retries, exiting\e[0m"; exit 1; }
  26. sleep 1
  27. done
  28. done < <(git show "origin/${BRANCH}:docker-compose.yml" | grep "image:" | awk '{ gsub("image:","", $3); print $2 }')
  29. }
  30. docker_garbage() {
  31. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  32. IMGS_TO_DELETE=()
  33. declare -A IMAGES_INFO
  34. COMPOSE_IMAGES=($(grep -oP "image: \Kmailcow.+" "${SCRIPT_DIR}/docker-compose.yml"))
  35. for existing_image in $(docker images --format "{{.ID}}:{{.Repository}}:{{.Tag}}" | grep 'mailcow/'); do
  36. ID=$(echo "$existing_image" | cut -d ':' -f 1)
  37. REPOSITORY=$(echo "$existing_image" | cut -d ':' -f 2)
  38. TAG=$(echo "$existing_image" | cut -d ':' -f 3)
  39. if [[ " ${COMPOSE_IMAGES[@]} " =~ " ${REPOSITORY}:${TAG} " ]]; then
  40. continue
  41. else
  42. IMGS_TO_DELETE+=("$ID")
  43. IMAGES_INFO["$ID"]="$REPOSITORY:$TAG"
  44. fi
  45. done
  46. if [[ ! -z ${IMGS_TO_DELETE[*]} ]]; then
  47. echo "The following unused mailcow images were found:"
  48. for id in "${IMGS_TO_DELETE[@]}"; do
  49. echo " ${IMAGES_INFO[$id]} ($id)"
  50. done
  51. if [ ! $FORCE ]; then
  52. read -r -p "Do you want to delete them to free up some space? [y/N] " response
  53. if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  54. docker rmi ${IMGS_TO_DELETE[*]}
  55. else
  56. echo "OK, skipped."
  57. fi
  58. else
  59. echo "Running in forced mode! Force removing old mailcow images..."
  60. docker rmi ${IMGS_TO_DELETE[*]}
  61. fi
  62. echo -e "\e[32mFurther cleanup...\e[0m"
  63. echo "If you want to cleanup further garbage collected by Docker, please make sure all containers are up and running before cleaning your system by executing \"docker system prune\""
  64. fi
  65. }
  66. in_array() {
  67. local e match="$1"
  68. shift
  69. for e; do [[ "$e" == "$match" ]] && return 0; done
  70. return 1
  71. }
  72. migrate_docker_nat() {
  73. NAT_CONFIG='{"ipv6":true,"fixed-cidr-v6":"fd00:dead:beef:c0::/80","experimental":true,"ip6tables":true}'
  74. # Min Docker version
  75. DOCKERV_REQ=20.10.2
  76. # Current Docker version
  77. DOCKERV_CUR=$(docker version -f '{{.Server.Version}}')
  78. if grep -qi "ipv6nat-mailcow" docker-compose.yml && grep -qi "enable_ipv6: true" docker-compose.yml; then
  79. echo -e "\e[32mNative IPv6 implementation available.\e[0m"
  80. echo "This will enable experimental features in the Docker daemon and configure Docker to do the IPv6 NATing instead of ipv6nat-mailcow."
  81. echo '!!! This step is recommended !!!'
  82. echo "mailcow will try to roll back the changes if starting Docker fails after modifying the daemon.json configuration file."
  83. read -r -p "Should we try to enable the native IPv6 implementation in Docker now (recommended)? [y/N] " dockernatresponse
  84. if [[ ! "${dockernatresponse}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  85. echo "OK, skipping this step."
  86. return 0
  87. fi
  88. fi
  89. # Sort versions and check if we are running a newer or equal version to req
  90. if [ $(printf "${DOCKERV_REQ}\n${DOCKERV_CUR}" | sort -V | tail -n1) == "${DOCKERV_CUR}" ]; then
  91. # If Dockerd daemon json exists
  92. if [ -s /etc/docker/daemon.json ]; then
  93. IFS=',' read -r -a dockerconfig <<< $(cat /etc/docker/daemon.json | tr -cd '[:alnum:],')
  94. if ! in_array ipv6true "${dockerconfig[@]}" || \
  95. ! in_array experimentaltrue "${dockerconfig[@]}" || \
  96. ! in_array ip6tablestrue "${dockerconfig[@]}" || \
  97. ! grep -qi "fixed-cidr-v6" /etc/docker/daemon.json; then
  98. echo -e "\e[33mWarning:\e[0m You seem to have modified the /etc/docker/daemon.json configuration by yourself and not fully/correctly activated the native IPv6 NAT implementation."
  99. echo "You will need to merge your existing configuration manually or fix/delete the existing daemon.json configuration before trying the update process again."
  100. echo -e "Please merge the following content and restart the Docker daemon:\n"
  101. echo "${NAT_CONFIG}"
  102. return 1
  103. fi
  104. else
  105. echo "Working on IPv6 NAT, please wait..."
  106. echo "${NAT_CONFIG}" > /etc/docker/daemon.json
  107. ip6tables -F -t nat
  108. [[ -e /etc/rc.conf ]] && rc-service docker restart || systemctl restart docker.service
  109. if [[ $? -ne 0 ]]; then
  110. echo -e "\e[31mError:\e[0m Failed to activate IPv6 NAT! Reverting and exiting."
  111. rm /etc/docker/daemon.json
  112. if [[ -e /etc/rc.conf ]]; then
  113. rc-service docker restart
  114. else
  115. systemctl reset-failed docker.service
  116. systemctl restart docker.service
  117. fi
  118. return 1
  119. fi
  120. fi
  121. # Removing legacy container
  122. sed -i '/ipv6nat-mailcow:$/,/^$/d' docker-compose.yml
  123. if [ -s docker-compose.override.yml ]; then
  124. sed -i '/ipv6nat-mailcow:$/,/^$/d' docker-compose.override.yml
  125. if [[ "$(cat docker-compose.override.yml | sed '/^\s*$/d' | wc -l)" == "2" ]]; then
  126. mv docker-compose.override.yml docker-compose.override.yml_backup
  127. fi
  128. fi
  129. echo -e "\e[32mGreat! \e[0mNative IPv6 NAT is active.\e[0m"
  130. else
  131. echo -e "\e[31mPlease upgrade Docker to version ${DOCKERV_REQ} or above.\e[0m"
  132. return 0
  133. fi
  134. }
  135. remove_obsolete_nginx_ports() {
  136. # Removing obsolete docker-compose.override.yml
  137. for override in docker-compose.override.yml docker-compose.override.yaml; do
  138. if [ -s $override ] ; then
  139. if cat $override | grep nginx-mailcow > /dev/null 2>&1; then
  140. if cat $override | grep -E '(\[::])' > /dev/null 2>&1; then
  141. if cat $override | grep -w 80:80 > /dev/null 2>&1 && cat $override | grep -w 443:443 > /dev/null 2>&1 ; then
  142. echo -e "\e[33mBacking up ${override} to preserve custom changes...\e[0m"
  143. echo -e "\e[33m!!! Manual Merge needed (if other overrides are set) !!!\e[0m"
  144. sleep 3
  145. cp $override ${override}_backup
  146. sed -i '/nginx-mailcow:$/,/^$/d' $override
  147. echo -e "\e[33mRemoved obsolete NGINX IPv6 Bind from original override File.\e[0m"
  148. if [[ "$(cat $override | sed '/^\s*$/d' | wc -l)" == "2" ]]; then
  149. mv $override ${override}_empty
  150. echo -e "\e[31m${override} is empty. Renamed it to ensure mailcow is startable.\e[0m"
  151. fi
  152. fi
  153. fi
  154. fi
  155. fi
  156. done
  157. }
  158. detect_docker_compose_command(){
  159. if ! [[ "${DOCKER_COMPOSE_VERSION}" =~ ^(native|standalone)$ ]]; then
  160. if docker compose > /dev/null 2>&1; then
  161. if docker compose version --short | grep -e "^2." -e "^v2." > /dev/null 2>&1; then
  162. DOCKER_COMPOSE_VERSION=native
  163. COMPOSE_COMMAND="docker compose"
  164. echo -e "\e[33mFound Docker Compose Plugin (native).\e[0m"
  165. echo -e "\e[33mSetting the DOCKER_COMPOSE_VERSION Variable to native\e[0m"
  166. sed -i 's/^DOCKER_COMPOSE_VERSION=.*/DOCKER_COMPOSE_VERSION=native/' "$SCRIPT_DIR/mailcow.conf"
  167. sleep 2
  168. echo -e "\e[33mNotice: You'll have to update this Compose Version via your Package Manager manually!\e[0m"
  169. else
  170. echo -e "\e[31mCannot find Docker Compose with a Version Higher than 2.X.X.\e[0m"
  171. echo -e "\e[31mPlease update/install it manually regarding to this doc site: https://docs.mailcow.email/install/\e[0m"
  172. exit 1
  173. fi
  174. elif docker-compose > /dev/null 2>&1; then
  175. if ! [[ $(alias docker-compose 2> /dev/null) ]] ; then
  176. if docker-compose version --short | grep "^2." > /dev/null 2>&1; then
  177. DOCKER_COMPOSE_VERSION=standalone
  178. COMPOSE_COMMAND="docker-compose"
  179. echo -e "\e[33mFound Docker Compose Standalone.\e[0m"
  180. echo -e "\e[33mSetting the DOCKER_COMPOSE_VERSION Variable to standalone\e[0m"
  181. sed -i 's/^DOCKER_COMPOSE_VERSION=.*/DOCKER_COMPOSE_VERSION=standalone/' "$SCRIPT_DIR/mailcow.conf"
  182. sleep 2
  183. echo -e "\e[33mNotice: For an automatic update of docker-compose please use the update_compose.sh scripts located at the helper-scripts folder.\e[0m"
  184. else
  185. echo -e "\e[31mCannot find Docker Compose with a Version Higher than 2.X.X.\e[0m"
  186. echo -e "\e[31mPlease update/install regarding to this doc site: https://docs.mailcow.email/install/\e[0m"
  187. exit 1
  188. fi
  189. fi
  190. else
  191. echo -e "\e[31mCannot find Docker Compose.\e[0m"
  192. echo -e "\e[31mPlease install it regarding to this doc site: https://docs.mailcow.email/install/\e[0m"
  193. exit 1
  194. fi
  195. elif [ "${DOCKER_COMPOSE_VERSION}" == "native" ]; then
  196. COMPOSE_COMMAND="docker compose"
  197. # Check if Native Compose works and has not been deleted
  198. if ! $COMPOSE_COMMAND > /dev/null 2>&1; then
  199. # IF it not exists/work anymore try the other command
  200. COMPOSE_COMMAND="docker-compose"
  201. if ! $COMPOSE_COMMAND > /dev/null 2>&1 || ! $COMPOSE_COMMAND --version | grep "^2." > /dev/null 2>&1; then
  202. # IF it cannot find Standalone in > 2.X, then script stops
  203. echo -e "\e[31mCannot find Docker Compose or the Version is lower then 2.X.X.\e[0m"
  204. echo -e "\e[31mPlease install it regarding to this doc site: https://docs.mailcow.email/install/\e[0m"
  205. exit 1
  206. fi
  207. # If it finds the standalone Plugin it will use this instead and change the mailcow.conf Variable accordingly
  208. echo -e "\e[31mFound different Docker Compose Version then declared in mailcow.conf!\e[0m"
  209. echo -e "\e[31mSetting the DOCKER_COMPOSE_VERSION Variable from native to standalone\e[0m"
  210. sed -i 's/^DOCKER_COMPOSE_VERSION=.*/DOCKER_COMPOSE_VERSION=standalone/' "$SCRIPT_DIR/mailcow.conf"
  211. sleep 2
  212. fi
  213. elif [ "${DOCKER_COMPOSE_VERSION}" == "standalone" ]; then
  214. COMPOSE_COMMAND="docker-compose"
  215. # Check if Standalone Compose works and has not been deleted
  216. if ! $COMPOSE_COMMAND > /dev/null 2>&1 && ! $COMPOSE_COMMAND --version > /dev/null 2>&1 | grep "^2." > /dev/null 2>&1; then
  217. # IF it not exists/work anymore try the other command
  218. COMPOSE_COMMAND="docker compose"
  219. if ! $COMPOSE_COMMAND > /dev/null 2>&1; then
  220. # IF it cannot find Native in > 2.X, then script stops
  221. echo -e "\e[31mCannot find Docker Compose.\e[0m"
  222. echo -e "\e[31mPlease install it regarding to this doc site: https://docs.mailcow.email/install/\e[0m"
  223. exit 1
  224. fi
  225. # If it finds the native Plugin it will use this instead and change the mailcow.conf Variable accordingly
  226. echo -e "\e[31mFound different Docker Compose Version then declared in mailcow.conf!\e[0m"
  227. echo -e "\e[31mSetting the DOCKER_COMPOSE_VERSION Variable from standalone to native\e[0m"
  228. sed -i 's/^DOCKER_COMPOSE_VERSION=.*/DOCKER_COMPOSE_VERSION=native/' "$SCRIPT_DIR/mailcow.conf"
  229. sleep 2
  230. fi
  231. fi
  232. }
  233. detect_bad_asn() {
  234. echo -e "\e[33mDetecting if your IP is listed on Spamhaus Bad ASN List...\e[0m"
  235. response=$(curl --connect-timeout 15 --max-time 30 -s -o /dev/null -w "%{http_code}" "https://asn-check.mailcow.email")
  236. if [ "$response" -eq 503 ]; then
  237. if [ -z "$SPAMHAUS_DQS_KEY" ]; then
  238. echo -e "\e[33mYour server's public IP uses an AS that is blocked by Spamhaus to use their DNS public blocklists for Postfix.\e[0m"
  239. echo -e "\e[33mmailcow did not detected a value for the variable SPAMHAUS_DQS_KEY inside mailcow.conf!\e[0m"
  240. sleep 2
  241. echo ""
  242. echo -e "\e[33mTo use the Spamhaus DNS Blocklists again, you will need to create a FREE account for their Data Query Service (DQS) at: https://www.spamhaus.com/free-trial/sign-up-for-a-free-data-query-service-account\e[0m"
  243. echo -e "\e[33mOnce done, enter your DQS API key in mailcow.conf and mailcow will do the rest for you!\e[0m"
  244. echo ""
  245. sleep 2
  246. else
  247. echo -e "\e[33mYour server's public IP uses an AS that is blocked by Spamhaus to use their DNS public blocklists for Postfix.\e[0m"
  248. echo -e "\e[32mmailcow detected a Value for the variable SPAMHAUS_DQS_KEY inside mailcow.conf. Postfix will use DQS with the given API key...\e[0m"
  249. fi
  250. elif [ "$response" -eq 200 ]; then
  251. echo -e "\e[33mCheck completed! Your IP is \e[32mclean\e[0m"
  252. elif [ "$response" -eq 429 ]; then
  253. echo -e "\e[33mCheck completed! \e[31mYour IP seems to be rate limited on the ASN Check service... please try again later!\e[0m"
  254. else
  255. echo -e "\e[31mCheck failed! \e[0mMaybe a DNS or Network problem?\e[0m"
  256. fi
  257. }
  258. fix_broken_dnslist_conf() {
  259. # Fixing issue: #6143. To be removed in a later patch
  260. local file="${SCRIPT_DIR}/data/conf/postfix/dns_blocklists.cf"
  261. # Check if the file exists
  262. if [[ ! -f "$file" ]]; then
  263. return 1
  264. fi
  265. # Check if the file contains the autogenerated comment
  266. if grep -q "# Autogenerated by mailcow" "$file"; then
  267. # Ask the user if custom changes were made
  268. echo -e "\e[91mWARNING!!! \e[31mAn old version of dns_blocklists.cnf has been detected which may cause a broken postfix upon startup (see: https://github.com/mailcow/mailcow-dockerized/issues/6143)...\e[0m"
  269. echo -e "\e[31mIf you have any custom settings in there you might copy it away and adapt the changes after the file is regenerated...\e[0m"
  270. read -p "Do you want to delete the file now and let mailcow regenerate it properly? " response
  271. if [[ "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  272. rm "$file"
  273. echo -e "\e[32mdns_blocklists.cf has been deleted and will be properly regenerated"
  274. return 0
  275. else
  276. echo -e "\e[35mOk, not deleting it! Please make sure you take a look at postfix upon start then..."
  277. return 2
  278. fi
  279. fi
  280. }
  281. ############## End Function Section ##############
  282. # Check permissions
  283. if [ "$(id -u)" -ne "0" ]; then
  284. echo "You need to be root"
  285. exit 1
  286. fi
  287. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  288. # Run pre-update-hook
  289. if [ -f "${SCRIPT_DIR}/pre_update_hook.sh" ]; then
  290. bash "${SCRIPT_DIR}/pre_update_hook.sh"
  291. fi
  292. if [[ "$(uname -r)" =~ ^4\.15\.0-60 ]]; then
  293. echo "DO NOT RUN mailcow ON THIS UBUNTU KERNEL!";
  294. echo "Please update to 5.x or use another distribution."
  295. exit 1
  296. fi
  297. if [[ "$(uname -r)" =~ ^4\.4\. ]]; then
  298. if grep -q Ubuntu <<< "$(uname -a)"; then
  299. echo "DO NOT RUN mailcow ON THIS UBUNTU KERNEL!"
  300. echo "Please update to linux-generic-hwe-16.04 by running \"apt-get install --install-recommends linux-generic-hwe-16.04\""
  301. exit 1
  302. fi
  303. echo "mailcow on a 4.4.x kernel is not supported. It may or may not work, please upgrade your kernel or continue at your own risk."
  304. read -p "Press any key to continue..." < /dev/tty
  305. fi
  306. # Exit on error and pipefail
  307. set -o pipefail
  308. # Setting high dc timeout
  309. export COMPOSE_HTTP_TIMEOUT=600
  310. # Add /opt/bin to PATH
  311. PATH=$PATH:/opt/bin
  312. umask 0022
  313. # Unset COMPOSE_COMMAND and DOCKER_COMPOSE_VERSION Variable to be on the newest state.
  314. unset COMPOSE_COMMAND
  315. unset DOCKER_COMPOSE_VERSION
  316. for bin in curl docker git awk sha1sum grep cut; do
  317. if [[ -z $(command -v ${bin}) ]]; then
  318. echo "Cannot find ${bin}, exiting..."
  319. exit 1;
  320. fi
  321. done
  322. # Check Docker Version (need at least 24.X)
  323. docker_version=$(docker -v | grep -oP '\d+\.\d+\.\d+' | cut -d '.' -f 1 | head -1)
  324. if [[ $docker_version -lt 24 ]]; then
  325. echo -e "\e[31mCannot find Docker with a Version higher or equals 24.0.0\e[0m"
  326. echo -e "\e[33mmailcow needs a newer Docker version to work properly... continuing on your own risk!\e[0m"
  327. echo -e "\e[31mPlease update your Docker installation... sleeping 10s\e[0m"
  328. sleep 10
  329. fi
  330. export LC_ALL=C
  331. DATE=$(date +%Y-%m-%d_%H_%M_%S)
  332. BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
  333. while (($#)); do
  334. case "${1}" in
  335. --check|-c)
  336. echo "Checking remote code for updates..."
  337. LATEST_REV=$(git ls-remote --exit-code --refs --quiet https://github.com/mailcow/mailcow-dockerized "${BRANCH}" | cut -f1)
  338. if [ "$?" -ne 0 ]; then
  339. echo "A problem occurred while trying to fetch the latest revision from github."
  340. exit 99
  341. fi
  342. if [[ -z $(git log HEAD --pretty=format:"%H" | grep "${LATEST_REV}") ]]; then
  343. echo -e "Updated code is available.\nThe changes can be found here: https://github.com/mailcow/mailcow-dockerized/commits/master"
  344. git log --date=short --pretty=format:"%ad - %s" "$(git rev-parse --short HEAD)"..origin/master
  345. exit 0
  346. else
  347. echo "No updates available."
  348. exit 3
  349. fi
  350. ;;
  351. --check-tags)
  352. echo "Checking remote tags for updates..."
  353. LATEST_TAG_REV=$(git ls-remote --exit-code --quiet --tags origin | tail -1 | cut -f1)
  354. if [ "$?" -ne 0 ]; then
  355. echo "A problem occurred while trying to fetch the latest tag from github."
  356. exit 99
  357. fi
  358. if [[ -z $(git log HEAD --pretty=format:"%H" | grep "${LATEST_TAG_REV}") ]]; then
  359. echo -e "New tag is available.\nThe changes can be found here: https://github.com/mailcow/mailcow-dockerized/releases/latest"
  360. exit 0
  361. else
  362. echo "No updates available."
  363. exit 3
  364. fi
  365. ;;
  366. --ours)
  367. MERGE_STRATEGY=ours
  368. ;;
  369. --skip-start)
  370. SKIP_START=y
  371. ;;
  372. --skip-ping-check)
  373. SKIP_PING_CHECK=y
  374. ;;
  375. --stable)
  376. CURRENT_BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
  377. NEW_BRANCH="master"
  378. ;;
  379. --gc)
  380. echo -e "\e[32mCollecting garbage...\e[0m"
  381. docker_garbage
  382. exit 0
  383. ;;
  384. --nightly)
  385. CURRENT_BRANCH="$(cd "${SCRIPT_DIR}"; git rev-parse --abbrev-ref HEAD)"
  386. NEW_BRANCH="nightly"
  387. ;;
  388. --prefetch)
  389. echo -e "\e[32mPrefetching images...\e[0m"
  390. prefetch_images
  391. exit 0
  392. ;;
  393. -f|--force)
  394. echo -e "\e[32mRunning in forced mode...\e[0m"
  395. FORCE=y
  396. ;;
  397. -d|--dev)
  398. echo -e "\e[32mRunning in Developer mode...\e[0m"
  399. DEV=y
  400. ;;
  401. --help|-h)
  402. echo './update.sh [-c|--check, --check-tags, --ours, --gc, --nightly, --prefetch, --skip-start, --skip-ping-check, --stable, -f|--force, -d|--dev, -h|--help]
  403. -c|--check - Check for updates and exit (exit codes => 0: update available, 3: no updates)
  404. --check-tags - Check for newer tags and exit (exit codes => 0: newer tag available, 3: no newer tag)
  405. --ours - Use merge strategy option "ours" to solve conflicts in favor of non-mailcow code (local changes over remote changes), not recommended!
  406. --gc - Run garbage collector to delete old image tags
  407. --nightly - Switch your mailcow updates to the unstable (nightly) branch. FOR TESTING PURPOSES ONLY!!!!
  408. --prefetch - Only prefetch new images and exit (useful to prepare updates)
  409. --skip-start - Do not start mailcow after update
  410. --skip-ping-check - Skip ICMP Check to public DNS resolvers (Use it only if you'\''ve blocked any ICMP Connections to your mailcow machine)
  411. --stable - Switch your mailcow updates to the stable (master) branch. Default unless you changed it with --nightly.
  412. -f|--force - Force update, do not ask questions
  413. -d|--dev - Enables Developer Mode (No Checkout of update.sh for tests)
  414. '
  415. exit 0
  416. esac
  417. shift
  418. done
  419. [[ ! -f mailcow.conf ]] && { echo -e "\e[31mmailcow.conf is missing! Is mailcow installed?\e[0m"; exit 1;}
  420. chmod 600 mailcow.conf
  421. source mailcow.conf
  422. detect_docker_compose_command
  423. fix_broken_dnslist_conf
  424. DOTS=${MAILCOW_HOSTNAME//[^.]};
  425. if [ ${#DOTS} -lt 1 ]; then
  426. echo -e "\e[31mMAILCOW_HOSTNAME (${MAILCOW_HOSTNAME}) is not a FQDN!\e[0m"
  427. sleep 1
  428. echo "Please change it to a FQDN and redeploy the stack with $COMPOSE_COMMAND up -d"
  429. exit 1
  430. elif [[ "${MAILCOW_HOSTNAME: -1}" == "." ]]; then
  431. echo "MAILCOW_HOSTNAME (${MAILCOW_HOSTNAME}) is ending with a dot. This is not a valid FQDN!"
  432. exit 1
  433. elif [ ${#DOTS} -eq 1 ]; then
  434. echo -e "\e[33mMAILCOW_HOSTNAME (${MAILCOW_HOSTNAME}) does not contain a Subdomain. This is not fully tested and may cause issues.\e[0m"
  435. echo "Find more information about why this message exists here: https://github.com/mailcow/mailcow-dockerized/issues/1572"
  436. read -r -p "Do you want to proceed anyway? [y/N] " response
  437. if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  438. echo "OK. Proceeding."
  439. else
  440. echo "OK. Exiting."
  441. exit 1
  442. fi
  443. fi
  444. if grep --help 2>&1 | head -n 1 | grep -q -i "busybox"; then echo "BusyBox grep detected, please install gnu grep, \"apk add --no-cache --upgrade grep\""; exit 1; fi
  445. # This will also cover sort
  446. if cp --help 2>&1 | head -n 1 | grep -q -i "busybox"; then echo "BusyBox cp detected, please install coreutils, \"apk add --no-cache --upgrade coreutils\""; exit 1; fi
  447. if sed --help 2>&1 | head -n 1 | grep -q -i "busybox"; then echo "BusyBox sed detected, please install gnu sed, \"apk add --no-cache --upgrade sed\""; exit 1; fi
  448. CONFIG_ARRAY=(
  449. "SKIP_LETS_ENCRYPT"
  450. "SKIP_SOGO"
  451. "USE_WATCHDOG"
  452. "WATCHDOG_NOTIFY_EMAIL"
  453. "WATCHDOG_NOTIFY_WEBHOOK"
  454. "WATCHDOG_NOTIFY_WEBHOOK_BODY"
  455. "WATCHDOG_NOTIFY_BAN"
  456. "WATCHDOG_NOTIFY_START"
  457. "WATCHDOG_EXTERNAL_CHECKS"
  458. "WATCHDOG_SUBJECT"
  459. "SKIP_CLAMD"
  460. "SKIP_IP_CHECK"
  461. "ADDITIONAL_SAN"
  462. "AUTODISCOVER_SAN"
  463. "DOVEADM_PORT"
  464. "IPV4_NETWORK"
  465. "IPV6_NETWORK"
  466. "LOG_LINES"
  467. "SNAT_TO_SOURCE"
  468. "SNAT6_TO_SOURCE"
  469. "COMPOSE_PROJECT_NAME"
  470. "DOCKER_COMPOSE_VERSION"
  471. "SQL_PORT"
  472. "API_KEY"
  473. "API_KEY_READ_ONLY"
  474. "API_ALLOW_FROM"
  475. "MAILDIR_GC_TIME"
  476. "MAILDIR_SUB"
  477. "ACL_ANYONE"
  478. "SOLR_HEAP"
  479. "SKIP_SOLR"
  480. "ENABLE_SSL_SNI"
  481. "ALLOW_ADMIN_EMAIL_LOGIN"
  482. "SKIP_HTTP_VERIFICATION"
  483. "SOGO_EXPIRE_SESSION"
  484. "REDIS_PORT"
  485. "DOVECOT_MASTER_USER"
  486. "DOVECOT_MASTER_PASS"
  487. "MAILCOW_PASS_SCHEME"
  488. "ADDITIONAL_SERVER_NAMES"
  489. "ACME_CONTACT"
  490. "WATCHDOG_VERBOSE"
  491. "WEBAUTHN_ONLY_TRUSTED_VENDORS"
  492. "SPAMHAUS_DQS_KEY"
  493. "SKIP_UNBOUND_HEALTHCHECK"
  494. "DISABLE_NETFILTER_ISOLATION_RULE"
  495. )
  496. detect_bad_asn
  497. sed -i --follow-symlinks '$a\' mailcow.conf
  498. for option in "${CONFIG_ARRAY[@]}"; do
  499. if [[ ${option} == "ADDITIONAL_SAN" ]]; then
  500. if ! grep -q "${option}" mailcow.conf; then
  501. echo "Adding new option \"${option}\" to mailcow.conf"
  502. echo "${option}=" >> mailcow.conf
  503. fi
  504. elif [[ "${option}" == "COMPOSE_PROJECT_NAME" ]]; then
  505. if ! grep -q "${option}" mailcow.conf; then
  506. echo "Adding new option \"${option}\" to mailcow.conf"
  507. echo "COMPOSE_PROJECT_NAME=mailcowdockerized" >> mailcow.conf
  508. fi
  509. elif [[ "${option}" == "DOCKER_COMPOSE_VERSION" ]]; then
  510. if ! grep -q "${option}" mailcow.conf; then
  511. echo "Adding new option \"${option}\" to mailcow.conf"
  512. echo "# Used Docker Compose version" >> mailcow.conf
  513. echo "# Switch here between native (compose plugin) and standalone" >> mailcow.conf
  514. echo "# For more informations take a look at the mailcow docs regarding the configuration options." >> mailcow.conf
  515. echo "# Normally this should be untouched but if you decided to use either of those you can switch it manually here." >> mailcow.conf
  516. echo "# Please be aware that at least one of those variants should be installed on your maschine or mailcow will fail." >> mailcow.conf
  517. echo "" >> mailcow.conf
  518. echo "DOCKER_COMPOSE_VERSION=${DOCKER_COMPOSE_VERSION}" >> mailcow.conf
  519. fi
  520. elif [[ "${option}" == "DOVEADM_PORT" ]]; then
  521. if ! grep -q "${option}" mailcow.conf; then
  522. echo "Adding new option \"${option}\" to mailcow.conf"
  523. echo "DOVEADM_PORT=127.0.0.1:19991" >> mailcow.conf
  524. fi
  525. elif [[ "${option}" == "WATCHDOG_NOTIFY_EMAIL" ]]; then
  526. if ! grep -q "${option}" mailcow.conf; then
  527. echo "Adding new option \"${option}\" to mailcow.conf"
  528. echo "WATCHDOG_NOTIFY_EMAIL=" >> mailcow.conf
  529. fi
  530. elif [[ "${option}" == "LOG_LINES" ]]; then
  531. if ! grep -q "${option}" mailcow.conf; then
  532. echo "Adding new option \"${option}\" to mailcow.conf"
  533. echo '# Max log lines per service to keep in Redis logs' >> mailcow.conf
  534. echo "LOG_LINES=9999" >> mailcow.conf
  535. fi
  536. elif [[ "${option}" == "IPV4_NETWORK" ]]; then
  537. if ! grep -q "${option}" mailcow.conf; then
  538. echo "Adding new option \"${option}\" to mailcow.conf"
  539. echo '# Internal IPv4 /24 subnet, format n.n.n. (expands to n.n.n.0/24)' >> mailcow.conf
  540. echo "IPV4_NETWORK=172.22.1" >> mailcow.conf
  541. fi
  542. elif [[ "${option}" == "IPV6_NETWORK" ]]; then
  543. if ! grep -q "${option}" mailcow.conf; then
  544. echo "Adding new option \"${option}\" to mailcow.conf"
  545. echo '# Internal IPv6 subnet in fc00::/7' >> mailcow.conf
  546. echo "IPV6_NETWORK=fd4d:6169:6c63:6f77::/64" >> mailcow.conf
  547. fi
  548. elif [[ "${option}" == "SQL_PORT" ]]; then
  549. if ! grep -q "${option}" mailcow.conf; then
  550. echo "Adding new option \"${option}\" to mailcow.conf"
  551. echo '# Bind SQL to 127.0.0.1 on port 13306' >> mailcow.conf
  552. echo "SQL_PORT=127.0.0.1:13306" >> mailcow.conf
  553. fi
  554. elif [[ "${option}" == "API_KEY" ]]; then
  555. if ! grep -q "${option}" mailcow.conf; then
  556. echo "Adding new option \"${option}\" to mailcow.conf"
  557. echo '# Create or override API key for web UI' >> mailcow.conf
  558. echo "#API_KEY=" >> mailcow.conf
  559. fi
  560. elif [[ "${option}" == "API_KEY_READ_ONLY" ]]; then
  561. if ! grep -q "${option}" mailcow.conf; then
  562. echo "Adding new option \"${option}\" to mailcow.conf"
  563. echo '# Create or override read-only API key for web UI' >> mailcow.conf
  564. echo "#API_KEY_READ_ONLY=" >> mailcow.conf
  565. fi
  566. elif [[ "${option}" == "API_ALLOW_FROM" ]]; then
  567. if ! grep -q "${option}" mailcow.conf; then
  568. echo "Adding new option \"${option}\" to mailcow.conf"
  569. echo '# Must be set for API_KEY to be active' >> mailcow.conf
  570. echo '# IPs only, no networks (networks can be set via UI)' >> mailcow.conf
  571. echo "#API_ALLOW_FROM=" >> mailcow.conf
  572. fi
  573. elif [[ "${option}" == "SNAT_TO_SOURCE" ]]; then
  574. if ! grep -q "${option}" mailcow.conf; then
  575. echo "Adding new option \"${option}\" to mailcow.conf"
  576. echo '# Use this IPv4 for outgoing connections (SNAT)' >> mailcow.conf
  577. echo "#SNAT_TO_SOURCE=" >> mailcow.conf
  578. fi
  579. elif [[ "${option}" == "SNAT6_TO_SOURCE" ]]; then
  580. if ! grep -q "${option}" mailcow.conf; then
  581. echo "Adding new option \"${option}\" to mailcow.conf"
  582. echo '# Use this IPv6 for outgoing connections (SNAT)' >> mailcow.conf
  583. echo "#SNAT6_TO_SOURCE=" >> mailcow.conf
  584. fi
  585. elif [[ "${option}" == "MAILDIR_GC_TIME" ]]; then
  586. if ! grep -q "${option}" mailcow.conf; then
  587. echo "Adding new option \"${option}\" to mailcow.conf"
  588. echo '# Garbage collector cleanup' >> mailcow.conf
  589. echo '# Deleted domains and mailboxes are moved to /var/vmail/_garbage/timestamp_sanitizedstring' >> mailcow.conf
  590. echo '# How long should objects remain in the garbage until they are being deleted? (value in minutes)' >> mailcow.conf
  591. echo '# Check interval is hourly' >> mailcow.conf
  592. echo 'MAILDIR_GC_TIME=1440' >> mailcow.conf
  593. fi
  594. elif [[ "${option}" == "ACL_ANYONE" ]]; then
  595. if ! grep -q "${option}" mailcow.conf; then
  596. echo "Adding new option \"${option}\" to mailcow.conf"
  597. echo '# Set this to "allow" to enable the anyone pseudo user. Disabled by default.' >> mailcow.conf
  598. echo '# When enabled, ACL can be created, that apply to "All authenticated users"' >> mailcow.conf
  599. echo '# This should probably only be activated on mail hosts, that are used exclusivly by one organisation.' >> mailcow.conf
  600. echo '# Otherwise a user might share data with too many other users.' >> mailcow.conf
  601. echo 'ACL_ANYONE=disallow' >> mailcow.conf
  602. fi
  603. elif [[ "${option}" == "SOLR_HEAP" ]]; then
  604. if ! grep -q "${option}" mailcow.conf; then
  605. echo "Adding new option \"${option}\" to mailcow.conf"
  606. echo '# Solr heap size, there is no recommendation, please see Solr docs.' >> mailcow.conf
  607. echo '# Solr is a prone to run OOM on large systems and should be monitored. Unmonitored Solr setups are not recommended.' >> mailcow.conf
  608. echo '# Solr will refuse to start with total system memory below or equal to 2 GB.' >> mailcow.conf
  609. echo "SOLR_HEAP=1024" >> mailcow.conf
  610. fi
  611. elif [[ "${option}" == "SKIP_SOLR" ]]; then
  612. if ! grep -q "${option}" mailcow.conf; then
  613. echo "Adding new option \"${option}\" to mailcow.conf"
  614. echo '# Solr is disabled by default after upgrading from non-Solr to Solr-enabled mailcows.' >> mailcow.conf
  615. echo '# Disable Solr or if you do not want to store a readable index of your mails in solr-vol-1.' >> mailcow.conf
  616. echo "SKIP_SOLR=y" >> mailcow.conf
  617. fi
  618. elif [[ "${option}" == "ENABLE_SSL_SNI" ]]; then
  619. if ! grep -q "${option}" mailcow.conf; then
  620. echo "Adding new option \"${option}\" to mailcow.conf"
  621. echo '# Create seperate certificates for all domains - y/n' >> mailcow.conf
  622. echo '# this will allow adding more than 100 domains, but some email clients will not be able to connect with alternative hostnames' >> mailcow.conf
  623. echo '# see https://wiki.dovecot.org/SSL/SNIClientSupport' >> mailcow.conf
  624. echo "ENABLE_SSL_SNI=n" >> mailcow.conf
  625. fi
  626. elif [[ "${option}" == "SKIP_SOGO" ]]; then
  627. if ! grep -q "${option}" mailcow.conf; then
  628. echo "Adding new option \"${option}\" to mailcow.conf"
  629. echo '# Skip SOGo: Will disable SOGo integration and therefore webmail, DAV protocols and ActiveSync support (experimental, unsupported, not fully implemented) - y/n' >> mailcow.conf
  630. echo "SKIP_SOGO=n" >> mailcow.conf
  631. fi
  632. elif [[ "${option}" == "MAILDIR_SUB" ]]; then
  633. if ! grep -q "${option}" mailcow.conf; then
  634. echo "Adding new option \"${option}\" to mailcow.conf"
  635. echo '# MAILDIR_SUB defines a path in a users virtual home to keep the maildir in. Leave empty for updated setups.' >> mailcow.conf
  636. echo "#MAILDIR_SUB=Maildir" >> mailcow.conf
  637. echo "MAILDIR_SUB=" >> mailcow.conf
  638. fi
  639. elif [[ "${option}" == "WATCHDOG_NOTIFY_WEBHOOK" ]]; then
  640. if ! grep -q "${option}" mailcow.conf; then
  641. echo "Adding new option \"${option}\" to mailcow.conf"
  642. echo '# Send notifications to a webhook URL that receives a POST request with the content type "application/json".' >> mailcow.conf
  643. echo '# You can use this to send notifications to services like Discord, Slack and others.' >> mailcow.conf
  644. echo '#WATCHDOG_NOTIFY_WEBHOOK=https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' >> mailcow.conf
  645. fi
  646. elif [[ "${option}" == "WATCHDOG_NOTIFY_WEBHOOK_BODY" ]]; then
  647. if ! grep -q "${option}" mailcow.conf; then
  648. echo "Adding new option \"${option}\" to mailcow.conf"
  649. echo '# JSON body included in the webhook POST request. Needs to be in single quotes.' >> mailcow.conf
  650. echo '# Following variables are available: SUBJECT, BODY' >> mailcow.conf
  651. WEBHOOK_BODY='{"username": "mailcow Watchdog", "content": "**${SUBJECT}**\n${BODY}"}'
  652. echo "#WATCHDOG_NOTIFY_WEBHOOK_BODY='${WEBHOOK_BODY}'" >> mailcow.conf
  653. fi
  654. elif [[ "${option}" == "WATCHDOG_NOTIFY_BAN" ]]; then
  655. if ! grep -q "${option}" mailcow.conf; then
  656. echo "Adding new option \"${option}\" to mailcow.conf"
  657. echo '# Notify about banned IP. Includes whois lookup.' >> mailcow.conf
  658. echo "WATCHDOG_NOTIFY_BAN=y" >> mailcow.conf
  659. fi
  660. elif [[ "${option}" == "WATCHDOG_NOTIFY_START" ]]; then
  661. if ! grep -q "${option}" mailcow.conf; then
  662. echo "Adding new option \"${option}\" to mailcow.conf"
  663. echo '# Send a notification when the watchdog is started.' >> mailcow.conf
  664. echo "WATCHDOG_NOTIFY_START=y" >> mailcow.conf
  665. fi
  666. elif [[ "${option}" == "WATCHDOG_SUBJECT" ]]; then
  667. if ! grep -q "${option}" mailcow.conf; then
  668. echo "Adding new option \"${option}\" to mailcow.conf"
  669. echo '# Subject for watchdog mails. Defaults to "Watchdog ALERT" followed by the error message.' >> mailcow.conf
  670. echo "#WATCHDOG_SUBJECT=" >> mailcow.conf
  671. fi
  672. elif [[ "${option}" == "WATCHDOG_EXTERNAL_CHECKS" ]]; then
  673. if ! grep -q "${option}" mailcow.conf; then
  674. echo "Adding new option \"${option}\" to mailcow.conf"
  675. echo '# Checks if mailcow is an open relay. Requires a SAL. More checks will follow.' >> mailcow.conf
  676. echo '# No data is collected. Opt-in and anonymous.' >> mailcow.conf
  677. echo '# Will only work with unmodified mailcow setups.' >> mailcow.conf
  678. echo "WATCHDOG_EXTERNAL_CHECKS=n" >> mailcow.conf
  679. fi
  680. elif [[ "${option}" == "SOGO_EXPIRE_SESSION" ]]; then
  681. if ! grep -q "${option}" mailcow.conf; then
  682. echo "Adding new option \"${option}\" to mailcow.conf"
  683. echo '# SOGo session timeout in minutes' >> mailcow.conf
  684. echo "SOGO_EXPIRE_SESSION=480" >> mailcow.conf
  685. fi
  686. elif [[ "${option}" == "REDIS_PORT" ]]; then
  687. if ! grep -q "${option}" mailcow.conf; then
  688. echo "Adding new option \"${option}\" to mailcow.conf"
  689. echo "REDIS_PORT=127.0.0.1:7654" >> mailcow.conf
  690. fi
  691. elif [[ "${option}" == "DOVECOT_MASTER_USER" ]]; then
  692. if ! grep -q "${option}" mailcow.conf; then
  693. echo "Adding new option \"${option}\" to mailcow.conf"
  694. echo '# DOVECOT_MASTER_USER and _PASS must _both_ be provided. No special chars.' >> mailcow.conf
  695. echo '# Empty by default to auto-generate master user and password on start.' >> mailcow.conf
  696. echo '# User expands to DOVECOT_MASTER_USER@mailcow.local' >> mailcow.conf
  697. echo '# LEAVE EMPTY IF UNSURE' >> mailcow.conf
  698. echo "DOVECOT_MASTER_USER=" >> mailcow.conf
  699. fi
  700. elif [[ "${option}" == "DOVECOT_MASTER_PASS" ]]; then
  701. if ! grep -q "${option}" mailcow.conf; then
  702. echo "Adding new option \"${option}\" to mailcow.conf"
  703. echo '# LEAVE EMPTY IF UNSURE' >> mailcow.conf
  704. echo "DOVECOT_MASTER_PASS=" >> mailcow.conf
  705. fi
  706. elif [[ "${option}" == "MAILCOW_PASS_SCHEME" ]]; then
  707. if ! grep -q "${option}" mailcow.conf; then
  708. echo "Adding new option \"${option}\" to mailcow.conf"
  709. echo '# Password hash algorithm' >> mailcow.conf
  710. echo '# Only certain password hash algorithm are supported. For a fully list of supported schemes,' >> mailcow.conf
  711. echo '# see https://docs.mailcow.email/models/model-passwd/' >> mailcow.conf
  712. echo "MAILCOW_PASS_SCHEME=BLF-CRYPT" >> mailcow.conf
  713. fi
  714. elif [[ "${option}" == "ADDITIONAL_SERVER_NAMES" ]]; then
  715. if ! grep -q "${option}" mailcow.conf; then
  716. echo "Adding new option \"${option}\" to mailcow.conf"
  717. echo '# Additional server names for mailcow UI' >> mailcow.conf
  718. echo '#' >> mailcow.conf
  719. echo '# Specify alternative addresses for the mailcow UI to respond to' >> mailcow.conf
  720. echo '# This is useful when you set mail.* as ADDITIONAL_SAN and want to make sure mail.maildomain.com will always point to the mailcow UI.' >> mailcow.conf
  721. echo '# If the server name does not match a known site, Nginx decides by best-guess and may redirect users to the wrong web root.' >> mailcow.conf
  722. echo '# You can understand this as server_name directive in Nginx.' >> mailcow.conf
  723. echo '# Comma separated list without spaces! Example: ADDITIONAL_SERVER_NAMES=a.b.c,d.e.f' >> mailcow.conf
  724. echo 'ADDITIONAL_SERVER_NAMES=' >> mailcow.conf
  725. fi
  726. elif [[ "${option}" == "AUTODISCOVER_SAN" ]]; then
  727. if ! grep -q "${option}" mailcow.conf; then
  728. echo "Adding new option \"${option}\" to mailcow.conf"
  729. echo '# Obtain certificates for autodiscover.* and autoconfig.* domains.' >> mailcow.conf
  730. echo '# This can be useful to switch off in case you are in a scenario where a reverse proxy already handles those.' >> mailcow.conf
  731. echo '# There are mixed scenarios where ports 80,443 are occupied and you do not want to share certs' >> mailcow.conf
  732. echo '# between services. So acme-mailcow obtains for maildomains and all web-things get handled' >> mailcow.conf
  733. echo '# in the reverse proxy.' >> mailcow.conf
  734. echo 'AUTODISCOVER_SAN=y' >> mailcow.conf
  735. fi
  736. elif [[ "${option}" == "ACME_CONTACT" ]]; then
  737. if ! grep -q "${option}" mailcow.conf; then
  738. echo "Adding new option \"${option}\" to mailcow.conf"
  739. echo '# Lets Encrypt registration contact information' >> mailcow.conf
  740. echo '# Optional: Leave empty for none' >> mailcow.conf
  741. echo '# This value is only used on first order!' >> mailcow.conf
  742. echo '# Setting it at a later point will require the following steps:' >> mailcow.conf
  743. echo '# https://docs.mailcow.email/troubleshooting/debug-reset_tls/' >> mailcow.conf
  744. echo 'ACME_CONTACT=' >> mailcow.conf
  745. fi
  746. elif [[ "${option}" == "WEBAUTHN_ONLY_TRUSTED_VENDORS" ]]; then
  747. if ! grep -q "${option}" mailcow.conf; then
  748. echo "Adding new option \"${option}\" to mailcow.conf"
  749. echo "# WebAuthn device manufacturer verification" >> mailcow.conf
  750. echo '# After setting WEBAUTHN_ONLY_TRUSTED_VENDORS=y only devices from trusted manufacturers are allowed' >> mailcow.conf
  751. echo '# root certificates can be placed for validation under mailcow-dockerized/data/web/inc/lib/WebAuthn/rootCertificates' >> mailcow.conf
  752. echo 'WEBAUTHN_ONLY_TRUSTED_VENDORS=n' >> mailcow.conf
  753. fi
  754. elif [[ "${option}" == "SPAMHAUS_DQS_KEY" ]]; then
  755. if ! grep -q "${option}" mailcow.conf; then
  756. echo "Adding new option \"${option}\" to mailcow.conf"
  757. echo "# Spamhaus Data Query Service Key" >> mailcow.conf
  758. echo '# Optional: Leave empty for none' >> mailcow.conf
  759. echo '# Enter your key here if you are using a blocked ASN (OVH, AWS, Cloudflare e.g) for the unregistered Spamhaus Blocklist.' >> mailcow.conf
  760. echo '# If empty, it will completely disable Spamhaus blocklists if it detects that you are running on a server using a blocked AS.' >> mailcow.conf
  761. echo '# Otherwise it will work as usual.' >> mailcow.conf
  762. echo 'SPAMHAUS_DQS_KEY=' >> mailcow.conf
  763. fi
  764. elif [[ "${option}" == "WATCHDOG_VERBOSE" ]]; then
  765. if ! grep -q "${option}" mailcow.conf; then
  766. echo "Adding new option \"${option}\" to mailcow.conf"
  767. echo '# Enable watchdog verbose logging' >> mailcow.conf
  768. echo 'WATCHDOG_VERBOSE=n' >> mailcow.conf
  769. fi
  770. elif [[ "${option}" == "SKIP_UNBOUND_HEALTHCHECK" ]]; then
  771. if ! grep -q "${option}" mailcow.conf; then
  772. echo "Adding new option \"${option}\" to mailcow.conf"
  773. echo '# Skip Unbound (DNS Resolver) Healthchecks (NOT Recommended!) - y/n' >> mailcow.conf
  774. echo 'SKIP_UNBOUND_HEALTHCHECK=n' >> mailcow.conf
  775. fi
  776. elif [[ "${option}" == "DISABLE_NETFILTER_ISOLATION_RULE" ]]; then
  777. if ! grep -q "${option}" mailcow.conf; then
  778. echo "Adding new option \"${option}\" to mailcow.conf"
  779. echo '# Prevent netfilter from setting an iptables/nftables rule to isolate the mailcow docker network - y/n' >> mailcow.conf
  780. echo '# CAUTION: Disabling this may expose container ports to other neighbors on the same subnet, even if the ports are bound to localhost' >> mailcow.conf
  781. echo 'DISABLE_NETFILTER_ISOLATION_RULE=n' >> mailcow.conf
  782. fi
  783. elif ! grep -q "${option}" mailcow.conf; then
  784. echo "Adding new option \"${option}\" to mailcow.conf"
  785. echo "${option}=n" >> mailcow.conf
  786. fi
  787. done
  788. if [[ ("${SKIP_PING_CHECK}" == "y") ]]; then
  789. echo -e "\e[32mSkipping Ping Check...\e[0m"
  790. else
  791. echo -en "Checking internet connection... "
  792. if ! check_online_status; then
  793. echo -e "\e[31mfailed\e[0m"
  794. exit 1
  795. else
  796. echo -e "\e[32mOK\e[0m"
  797. fi
  798. fi
  799. if ! [ "$NEW_BRANCH" ]; then
  800. echo -e "\e[33mDetecting which build your mailcow runs on...\e[0m"
  801. sleep 1
  802. if [ "${BRANCH}" == "master" ]; then
  803. echo -e "\e[32mYou are receiving stable updates (master).\e[0m"
  804. echo -e "\e[33mTo change that run the update.sh Script one time with the --nightly parameter to switch to nightly builds.\e[0m"
  805. elif [ "${BRANCH}" == "nightly" ]; then
  806. echo -e "\e[31mYou are receiving unstable updates (nightly). These are for testing purposes only!!!\e[0m"
  807. sleep 1
  808. echo -e "\e[33mTo change that run the update.sh Script one time with the --stable parameter to switch to stable builds.\e[0m"
  809. else
  810. echo -e "\e[33mYou are receiving updates from an unsupported branch.\e[0m"
  811. sleep 1
  812. echo -e "\e[33mThe mailcow stack might still work but it is recommended to switch to the master branch (stable builds).\e[0m"
  813. echo -e "\e[33mTo change that run the update.sh Script one time with the --stable parameter to switch to stable builds.\e[0m"
  814. fi
  815. elif [ "$FORCE" ]; then
  816. echo -e "\e[31mYou are running in forced mode!\e[0m"
  817. echo -e "\e[31mA Branch Switch can only be performed manually (monitored).\e[0m"
  818. echo -e "\e[31mPlease rerun the update.sh Script without the --force/-f parameter.\e[0m"
  819. sleep 1
  820. elif [ "$NEW_BRANCH" == "master" ] && [ "$CURRENT_BRANCH" != "master" ]; then
  821. echo -e "\e[33mYou are about to switch your mailcow updates to the stable (master) branch.\e[0m"
  822. sleep 1
  823. echo -e "\e[33mBefore you do: Please take a backup of all components to ensure that no data is lost...\e[0m"
  824. sleep 1
  825. echo -e "\e[31mWARNING: Please see on GitHub or ask in the community if a switch to master is stable or not.
  826. In some rear cases an update back to master can destroy your mailcow configuration such as database upgrade, etc.
  827. Normally an upgrade back to master should be safe during each full release.
  828. Check GitHub for Database changes and update only if there similar to the full release!\e[0m"
  829. read -r -p "Are you sure you that want to continue upgrading to the stable (master) branch? [y/N] " response
  830. if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  831. echo "OK. If you prepared yourself for that please run the update.sh Script with the --stable parameter again to trigger this process here."
  832. exit 0
  833. fi
  834. BRANCH="$NEW_BRANCH"
  835. DIFF_DIRECTORY=update_diffs
  836. DIFF_FILE="${DIFF_DIRECTORY}/diff_before_upgrade_to_master_$(date +"%Y-%m-%d-%H-%M-%S")"
  837. mv diff_before_upgrade* "${DIFF_DIRECTORY}/" 2> /dev/null
  838. if ! git diff-index --quiet HEAD; then
  839. echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
  840. mkdir -p "${DIFF_DIRECTORY}"
  841. git diff "${BRANCH}" --stat > "${DIFF_FILE}"
  842. git diff "${BRANCH}" >> "${DIFF_FILE}"
  843. fi
  844. echo -e "\e[32mSwitching Branch to ${BRANCH}...\e[0m"
  845. git fetch origin
  846. git checkout -f "${BRANCH}"
  847. elif [ "$NEW_BRANCH" == "nightly" ] && [ "$CURRENT_BRANCH" != "nightly" ]; then
  848. echo -e "\e[33mYou are about to switch your mailcow Updates to the unstable (nightly) branch.\e[0m"
  849. sleep 1
  850. echo -e "\e[33mBefore you do: Please take a backup of all components to ensure that no Data is lost...\e[0m"
  851. sleep 1
  852. echo -e "\e[31mWARNING: A switch to nightly is possible any time. But a switch back (to master) isn't.\e[0m"
  853. read -r -p "Are you sure you that want to continue upgrading to the unstable (nightly) branch? [y/N] " response
  854. if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  855. echo "OK. If you prepared yourself for that please run the update.sh Script with the --nightly parameter again to trigger this process here."
  856. exit 0
  857. fi
  858. BRANCH=$NEW_BRANCH
  859. DIFF_DIRECTORY=update_diffs
  860. DIFF_FILE=${DIFF_DIRECTORY}/diff_before_upgrade_to_nightly_$(date +"%Y-%m-%d-%H-%M-%S")
  861. mv diff_before_upgrade* ${DIFF_DIRECTORY}/ 2> /dev/null
  862. if ! git diff-index --quiet HEAD; then
  863. echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
  864. mkdir -p ${DIFF_DIRECTORY}
  865. git diff "${BRANCH}" --stat > "${DIFF_FILE}"
  866. git diff "${BRANCH}" >> "${DIFF_FILE}"
  867. fi
  868. git fetch origin
  869. git checkout -f "${BRANCH}"
  870. fi
  871. if [ ! "$DEV" ]; then
  872. echo -e "\e[32mChecking for newer update script...\e[0m"
  873. SHA1_1="$(sha1sum update.sh)"
  874. git fetch origin #${BRANCH}
  875. git checkout "origin/${BRANCH}" update.sh
  876. SHA1_2=$(sha1sum update.sh)
  877. if [[ "${SHA1_1}" != "${SHA1_2}" ]]; then
  878. echo "update.sh changed, please run this script again, exiting."
  879. chmod +x update.sh
  880. exit 2
  881. fi
  882. fi
  883. if [ ! "$FORCE" ]; then
  884. read -r -p "Are you sure you want to update mailcow: dockerized? All containers will be stopped. [y/N] " response
  885. if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  886. echo "OK, exiting."
  887. exit 0
  888. fi
  889. migrate_docker_nat
  890. fi
  891. remove_obsolete_nginx_ports
  892. echo -e "\e[32mValidating docker-compose stack configuration...\e[0m"
  893. sed -i 's/HTTPS_BIND:-:/HTTPS_BIND:-/g' docker-compose.yml
  894. sed -i 's/HTTP_BIND:-:/HTTP_BIND:-/g' docker-compose.yml
  895. if ! $COMPOSE_COMMAND config -q; then
  896. echo -e "\e[31m\nOh no, something went wrong. Please check the error message above.\e[0m"
  897. exit 1
  898. fi
  899. echo -e "\e[32mChecking for conflicting bridges...\e[0m"
  900. MAILCOW_BRIDGE=$($COMPOSE_COMMAND config | grep -i com.docker.network.bridge.name | cut -d':' -f2)
  901. while read NAT_ID; do
  902. iptables -t nat -D POSTROUTING "$NAT_ID"
  903. done < <(iptables -L -vn -t nat --line-numbers | grep "$IPV4_NETWORK" | grep -E 'MASQUERADE.*all' | grep -v "${MAILCOW_BRIDGE}" | cut -d' ' -f1)
  904. DIFF_DIRECTORY=update_diffs
  905. DIFF_FILE=${DIFF_DIRECTORY}/diff_before_update_$(date +"%Y-%m-%d-%H-%M-%S")
  906. mv diff_before_update* ${DIFF_DIRECTORY}/ 2> /dev/null
  907. if ! git diff-index --quiet HEAD; then
  908. echo -e "\e[32mSaving diff to ${DIFF_FILE}...\e[0m"
  909. mkdir -p ${DIFF_DIRECTORY}
  910. git diff --stat > "${DIFF_FILE}"
  911. git diff >> "${DIFF_FILE}"
  912. fi
  913. echo -e "\e[32mPrefetching images...\e[0m"
  914. prefetch_images
  915. echo -e "\e[32mStopping mailcow...\e[0m"
  916. sleep 2
  917. MAILCOW_CONTAINERS=($($COMPOSE_COMMAND ps -q))
  918. $COMPOSE_COMMAND down
  919. echo -e "\e[32mChecking for remaining containers...\e[0m"
  920. sleep 2
  921. for container in "${MAILCOW_CONTAINERS[@]}"; do
  922. docker rm -f "$container" 2> /dev/null
  923. done
  924. [[ -f data/conf/nginx/ZZZ-ejabberd.conf ]] && rm data/conf/nginx/ZZZ-ejabberd.conf
  925. # Silently fixing remote url from andryyy to mailcow
  926. # git remote set-url origin https://github.com/mailcow/mailcow-dockerized
  927. DEFAULT_REPO="https://github.com/mailcow/mailcow-dockerized"
  928. CURRENT_REPO=$(git config --get remote.origin.url)
  929. if [ "$CURRENT_REPO" != "$DEFAULT_REPO" ]; then
  930. echo "The Repository currently used is not the default Mailcow Repository."
  931. echo "Currently Repository: $CURRENT_REPO"
  932. echo "Default Repository: $DEFAULT_REPO"
  933. if [ ! "$FORCE" ]; then
  934. read -r -p "Should it be changed back to default? [y/N] " repo_response
  935. if [[ "$repo_response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  936. git remote set-url origin $DEFAULT_REPO
  937. fi
  938. else
  939. echo "Running in forced mode... setting Repo to default!"
  940. git remote set-url origin $DEFAULT_REPO
  941. fi
  942. fi
  943. if [ ! "$DEV" ]; then
  944. echo -e "\e[32mCommitting current status...\e[0m"
  945. [[ -z "$(git config user.name)" ]] && git config user.name moo
  946. [[ -z "$(git config user.email)" ]] && git config user.email moo@cow.moo
  947. [[ ! -z $(git ls-files data/conf/rspamd/override.d/worker-controller-password.inc) ]] && git rm data/conf/rspamd/override.d/worker-controller-password.inc
  948. git add -u
  949. git commit -am "Before update on ${DATE}" > /dev/null
  950. echo -e "\e[32mFetching updated code from remote...\e[0m"
  951. git fetch origin #${BRANCH}
  952. echo -e "\e[32mMerging local with remote code (recursive, strategy: \"${MERGE_STRATEGY:-theirs}\", options: \"patience\"...\e[0m"
  953. git config merge.defaultToUpstream true
  954. git merge -X"${MERGE_STRATEGY:-theirs}" -Xpatience -m "After update on ${DATE}"
  955. # Need to use a variable to not pass return codes of if checks
  956. MERGE_RETURN=$?
  957. if [[ ${MERGE_RETURN} == 128 ]]; then
  958. echo -e "\e[31m\nOh no, what happened?\n=> You most likely added files to your local mailcow instance that were now added to the official mailcow repository. Please move them to another location before updating mailcow.\e[0m"
  959. exit 1
  960. elif [[ ${MERGE_RETURN} == 1 ]]; then
  961. echo -e "\e[93mPotential conflict, trying to fix...\e[0m"
  962. git status --porcelain | grep -E "UD|DU" | awk '{print $2}' | xargs rm -v
  963. git add -A
  964. git commit -m "After update on ${DATE}" > /dev/null
  965. git checkout .
  966. echo -e "\e[32mRemoved and recreated files if necessary.\e[0m"
  967. elif [[ ${MERGE_RETURN} != 0 ]]; then
  968. echo -e "\e[31m\nOh no, something went wrong. Please check the error message above.\e[0m"
  969. echo
  970. echo "Run $COMPOSE_COMMAND up -d to restart your stack without updates or try again after fixing the mentioned errors."
  971. exit 1
  972. fi
  973. elif [ "$DEV" ]; then
  974. echo -e "\e[33mDEVELOPER MODE: Not creating a git diff and commiting it to prevent development stuff within a backup diff...\e[0m"
  975. fi
  976. echo -e "\e[32mFetching new images, if any...\e[0m"
  977. sleep 2
  978. $COMPOSE_COMMAND pull
  979. # Fix missing SSL, does not overwrite existing files
  980. [[ ! -d data/assets/ssl ]] && mkdir -p data/assets/ssl
  981. cp -n -d data/assets/ssl-example/*.pem data/assets/ssl/
  982. echo -e "Checking IPv6 settings... "
  983. if grep -q 'SYSCTL_IPV6_DISABLED=1' mailcow.conf; then
  984. echo
  985. echo '!! IMPORTANT !!'
  986. echo
  987. echo 'SYSCTL_IPV6_DISABLED was removed due to complications. IPv6 can be disabled by editing "docker-compose.yml" and setting "enable_ipv6: true" to "enable_ipv6: false".'
  988. echo "This setting will only be active after a complete shutdown of mailcow by running $COMPOSE_COMMAND down followed by $COMPOSE_COMMAND up -d."
  989. echo
  990. echo '!! IMPORTANT !!'
  991. echo
  992. read -p "Press any key to continue..." < /dev/tty
  993. fi
  994. # Checking for old project name bug
  995. sed -i --follow-symlinks 's#COMPOSEPROJECT_NAME#COMPOSE_PROJECT_NAME#g' mailcow.conf
  996. # Fix Rspamd maps
  997. if [ -f data/conf/rspamd/custom/global_from_blacklist.map ]; then
  998. mv data/conf/rspamd/custom/global_from_blacklist.map data/conf/rspamd/custom/global_smtp_from_blacklist.map
  999. fi
  1000. if [ -f data/conf/rspamd/custom/global_from_whitelist.map ]; then
  1001. mv data/conf/rspamd/custom/global_from_whitelist.map data/conf/rspamd/custom/global_smtp_from_whitelist.map
  1002. fi
  1003. # Fix deprecated metrics.conf
  1004. if [ -f "data/conf/rspamd/local.d/metrics.conf" ]; then
  1005. if [ ! -z "$(git diff --name-only origin/master data/conf/rspamd/local.d/metrics.conf)" ]; then
  1006. echo -e "\e[33mWARNING\e[0m - Please migrate your customizations of data/conf/rspamd/local.d/metrics.conf to actions.conf and groups.conf after this update."
  1007. echo "The deprecated configuration file metrics.conf will be moved to metrics.conf_deprecated after updating mailcow."
  1008. fi
  1009. mv data/conf/rspamd/local.d/metrics.conf data/conf/rspamd/local.d/metrics.conf_deprecated
  1010. fi
  1011. # Set app_info.inc.php
  1012. if [ ${BRANCH} == "master" ]; then
  1013. mailcow_git_version=$(git describe --tags $(git rev-list --tags --max-count=1))
  1014. elif [ ${BRANCH} == "nightly" ]; then
  1015. mailcow_git_version=$(git rev-parse --short $(git rev-parse @{upstream}))
  1016. mailcow_last_git_version=""
  1017. else
  1018. mailcow_git_version=$(git rev-parse --short HEAD)
  1019. mailcow_last_git_version=""
  1020. fi
  1021. mailcow_git_commit=$(git rev-parse "origin/${BRANCH}")
  1022. mailcow_git_commit_date=$(git log -1 --format=%ci @{upstream} )
  1023. if [ $? -eq 0 ]; then
  1024. echo '<?php' > data/web/inc/app_info.inc.php
  1025. echo ' $MAILCOW_GIT_VERSION="'$mailcow_git_version'";' >> data/web/inc/app_info.inc.php
  1026. echo ' $MAILCOW_LAST_GIT_VERSION="";' >> data/web/inc/app_info.inc.php
  1027. echo ' $MAILCOW_GIT_OWNER="mailcow";' >> data/web/inc/app_info.inc.php
  1028. echo ' $MAILCOW_GIT_REPO="mailcow-dockerized";' >> data/web/inc/app_info.inc.php
  1029. echo ' $MAILCOW_GIT_URL="https://github.com/mailcow/mailcow-dockerized";' >> data/web/inc/app_info.inc.php
  1030. echo ' $MAILCOW_GIT_COMMIT="'$mailcow_git_commit'";' >> data/web/inc/app_info.inc.php
  1031. echo ' $MAILCOW_GIT_COMMIT_DATE="'$mailcow_git_commit_date'";' >> data/web/inc/app_info.inc.php
  1032. echo ' $MAILCOW_BRANCH="'$BRANCH'";' >> data/web/inc/app_info.inc.php
  1033. echo ' $MAILCOW_UPDATEDAT='$(date +%s)';' >> data/web/inc/app_info.inc.php
  1034. echo '?>' >> data/web/inc/app_info.inc.php
  1035. else
  1036. echo '<?php' > data/web/inc/app_info.inc.php
  1037. echo ' $MAILCOW_GIT_VERSION="'$mailcow_git_version'";' >> data/web/inc/app_info.inc.php
  1038. echo ' $MAILCOW_LAST_GIT_VERSION="";' >> data/web/inc/app_info.inc.php
  1039. echo ' $MAILCOW_GIT_OWNER="mailcow";' >> data/web/inc/app_info.inc.php
  1040. echo ' $MAILCOW_GIT_REPO="mailcow-dockerized";' >> data/web/inc/app_info.inc.php
  1041. echo ' $MAILCOW_GIT_URL="https://github.com/mailcow/mailcow-dockerized";' >> data/web/inc/app_info.inc.php
  1042. echo ' $MAILCOW_GIT_COMMIT="";' >> data/web/inc/app_info.inc.php
  1043. echo ' $MAILCOW_GIT_COMMIT_DATE="";' >> data/web/inc/app_info.inc.php
  1044. echo ' $MAILCOW_BRANCH="'$BRANCH'";' >> data/web/inc/app_info.inc.php
  1045. echo ' $MAILCOW_UPDATEDAT='$(date +%s)';' >> data/web/inc/app_info.inc.php
  1046. echo '?>' >> data/web/inc/app_info.inc.php
  1047. echo -e "\e[33mCannot determine current git repository version...\e[0m"
  1048. fi
  1049. if [[ ${SKIP_START} == "y" ]]; then
  1050. echo -e "\e[33mNot starting mailcow, please run \"$COMPOSE_COMMAND up -d --remove-orphans\" to start mailcow.\e[0m"
  1051. else
  1052. echo -e "\e[32mStarting mailcow...\e[0m"
  1053. sleep 2
  1054. $COMPOSE_COMMAND up -d --remove-orphans
  1055. fi
  1056. echo -e "\e[32mCollecting garbage...\e[0m"
  1057. docker_garbage
  1058. # Run post-update-hook
  1059. if [ -f "${SCRIPT_DIR}/post_update_hook.sh" ]; then
  1060. bash "${SCRIPT_DIR}/post_update_hook.sh"
  1061. fi
  1062. # echo "In case you encounter any problem, hard-reset to a state before updating mailcow:"
  1063. # echo
  1064. # git reflog --color=always | grep "Before update on "
  1065. # echo
  1066. # echo "Use \"git reset --hard hash-on-the-left\" and run $COMPOSE_COMMAND up -d afterwards."