v-add-sys-webmail 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/bin/bash
  2. # info: add webmail support for a domain
  3. # options: USER DOMAIN WEBMAIL [RESTART] [QUIET]
  4. # labels: hestia
  5. #
  6. # example: v-add-sys-webmail user domain.com
  7. #
  8. # this function adds support for webmail services
  9. # to a mail domain.
  10. #----------------------------------------------------------#
  11. # Variable&Function #
  12. #----------------------------------------------------------#
  13. # Argument definition
  14. user=$1
  15. domain=$2
  16. webmail=$3
  17. restart="$4"
  18. quiet=$5
  19. # Additional argument formatting
  20. if [[ "$domain" =~ [[:upper:]] ]]; then
  21. domain=$(echo "$domain" |tr '[:upper:]' '[:lower:]')
  22. fi
  23. if [[ "$domain" =~ ^www\..* ]]; then
  24. domain=$(echo "$domain" |sed -e "s/^www.//")
  25. fi
  26. if [[ "$domain" =~ .*\.$ ]]; then
  27. domain=$(echo "$domain" |sed -e "s/\.$//")
  28. fi
  29. domain_idn=$(idn -t --quiet -a "$domain")
  30. # Includes
  31. source $HESTIA/func/main.sh
  32. source $HESTIA/func/domain.sh
  33. source $HESTIA/func/ip.sh
  34. source $HESTIA/conf/hestia.conf
  35. # Additional argument formatting
  36. format_domain
  37. format_domain_idn
  38. #----------------------------------------------------------#
  39. # Verifications #
  40. #----------------------------------------------------------#
  41. if [ -z "$webmail" ]; then
  42. for client in ${WEBMAIL_SYSTEM//,/ };do
  43. webmail="$client"
  44. done
  45. fi
  46. check_args '3' "$#" 'USER DOMAIN WEBMAIL [RESTART]'
  47. is_format_valid 'user' 'domain'
  48. is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
  49. is_system_enabled "$IMAP_SYSTEM" 'IMAP_SYSTEM'
  50. is_type_valid "$WEBMAIL_SYSTEM" "$webmail"
  51. is_object_valid 'user' 'USER' "$user"
  52. is_object_unsuspended 'user' 'USER' "$user"
  53. is_object_valid 'mail' 'DOMAIN' "$domain"
  54. is_object_unsuspended 'mail' 'DOMAIN' "$domain"
  55. # Perform verification if read-only mode is enabled
  56. check_hestia_demo_mode
  57. #----------------------------------------------------------#
  58. # Action #
  59. #----------------------------------------------------------#
  60. # Inherit web domain local ip address
  61. domain_ip=$(get_object_value 'web' 'DOMAIN' "$domain" '$IP')
  62. if [ ! -z "$domain_ip" ]; then
  63. local_ip=$(get_real_ip "$domain_ip")
  64. is_ip_valid "$local_ip" "$user"
  65. ip=$local_ip
  66. nat_ip=$(get_ip_value '$NAT')
  67. if [ ! -z "$nat_ip" ]; then
  68. ip=$nat_ip
  69. fi
  70. else
  71. get_user_ip
  72. fi
  73. # Verify that webmail alias variable exists and create it if it does not
  74. if [ -z "$WEBMAIL_ALIAS" ]; then
  75. $BIN/v-change-sys-config-value 'WEBMAIL_ALIAS' "webmail"
  76. else
  77. # Ensure DNS record exists if Hestia is hosting DNS zones
  78. if [ ! -z "$DNS_SYSTEM" ]; then
  79. dns_domain=$($BIN/v-list-dns-domains $user | grep $domain | cut -d' ' -f1)
  80. webmail_record=$($BIN/v-list-dns-records $user $domain | grep -i $WEBMAIL_ALIAS | cut -d' ' -f1)
  81. if [ "$dns_domain" = "$domain" ]; then
  82. if [ -z "$webmail_record" ]; then
  83. $BIN/v-add-dns-record $user $domain $WEBMAIL_ALIAS A $ip
  84. else
  85. $BIN/v-delete-dns-record $user $domain $webmail_record
  86. $BIN/v-add-dns-record $user $domain $WEBMAIL_ALIAS A $ip
  87. fi
  88. fi
  89. fi
  90. if [ "$webmail" == "roundcube" ]; then
  91. WEBMAIL_TEMPLATE="default"
  92. if [ ! -z "$PROXY_SYSTEM" ]; then
  93. PROXY_TEMPLATE="default"
  94. fi
  95. # Add webmail configuration to mail domain
  96. WEBMAIL_TEMPLATE="default"
  97. if [ "$WEB_SYSTEM" = "nginx" ]; then
  98. WEBMAIL_TEMPLATE="web_system"
  99. fi
  100. else
  101. WEBMAIL_TEMPLATE="rainloop"
  102. if [ ! -z "$PROXY_SYSTEM" ]; then
  103. PROXY_TEMPLATE="default_rainloop"
  104. fi
  105. fi
  106. add_webmail_config "$WEB_SYSTEM" "${WEBMAIL_TEMPLATE}.tpl"
  107. if [ ! -z "$PROXY_SYSTEM" ]; then
  108. add_webmail_config "$PROXY_SYSTEM" "${PROXY_TEMPLATE}.tpl"
  109. fi
  110. # Enable SSL for webmail if available
  111. if [ -f $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt ] || [ "$SSL" = 'yes' ]; then
  112. add_webmail_config "$WEB_SYSTEM" "${WEBMAIL_TEMPLATE}.stpl"
  113. if [ ! -z "$PROXY_SYSTEM" ]; then
  114. add_webmail_config "$PROXY_SYSTEM" "${PROXY_TEMPLATE}.stpl"
  115. fi
  116. fi
  117. fi
  118. WEBMAIL=$(get_object_value 'web' 'DOMAIN' "$domain" "$WEBMAIL")
  119. if [ -z "$WEBMAIL" ]; then
  120. add_object_key 'mail' 'DOMAIN' "$domain" 'WEBMAIL' 'SSL'
  121. fi
  122. # Set SSL as enabled in configuration
  123. update_object_value 'mail' 'DOMAIN' "$domain" '$WEBMAIL' "$webmail"
  124. #----------------------------------------------------------#
  125. # Hestia #
  126. #----------------------------------------------------------#
  127. if [ "$restart" = 'yes' ]; then
  128. # Restarting web server
  129. $BIN/v-restart-web $restart
  130. check_result $? "Web restart failed" >/dev/null
  131. $BIN/v-restart-proxy $restart
  132. check_result $? "Proxy restart failed" >/dev/null
  133. fi
  134. # Logging
  135. if [ "$quiet" != 'yes' ]; then
  136. log_history "enabled webmail support for $domain"
  137. fi
  138. log_event "$OK" "$ARGUMENTS"
  139. exit