v-add-sys-webmail 4.9 KB

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