|
| 1 | +#!/bin/bash |
| 2 | +# info: Install SnappyMail webmail client |
| 3 | +# options: [MODE] |
| 4 | +# |
| 5 | +# This function installs the SnappyMail webmail client. |
| 6 | + |
| 7 | +#----------------------------------------------------------# |
| 8 | +# Variables & Functions # |
| 9 | +#----------------------------------------------------------# |
| 10 | + |
| 11 | +# Includes |
| 12 | +# shellcheck source=/etc/hestiacp/hestia.conf |
| 13 | +source /etc/hestiacp/hestia.conf |
| 14 | +# shellcheck source=/usr/local/hestia/func/main.sh |
| 15 | +source $HESTIA/func/main.sh |
| 16 | +# load config file |
| 17 | +source_conf "$HESTIA/conf/hestia.conf" |
| 18 | +# upgrade config file |
| 19 | +source "$HESTIA/install/upgrade/upgrade.conf" |
| 20 | + |
| 21 | +MODE=$1 |
| 22 | +UPDATE="no" |
| 23 | +# Version and Download paths |
| 24 | +# Version to be moved to upgrade script |
| 25 | +SM_FILE="snappymail-latest.tar.gz" |
| 26 | +# For removal of folder |
| 27 | +SM_EXTRACT_MAIN="snappymail" |
| 28 | + |
| 29 | +# Downloading full version |
| 30 | +SM_URL="https://snappymail.eu/repository/latest.tar.gz" |
| 31 | + |
| 32 | +# Folder paths |
| 33 | +SM_INSTALL_DIR="/var/lib/snappymail" |
| 34 | +SM_CONFIG_DIR="/etc/snappymail" |
| 35 | +SM_LOG="/var/log/snappymail" |
| 36 | + |
| 37 | +#----------------------------------------------------------# |
| 38 | +# Verifications # |
| 39 | +#----------------------------------------------------------# |
| 40 | + |
| 41 | +# Checking root permissions |
| 42 | +if [ "x$(id -u)" != 'x0' ]; then |
| 43 | + echo "ERROR: v-add-sys-snappymail can only be executed by the root user" |
| 44 | + exit 10 |
| 45 | +fi |
| 46 | + |
| 47 | +# Ensure that $HESTIA (/usr/local/hestia/) and other variables are valid. |
| 48 | +if [ -z "$HESTIA" ]; then |
| 49 | + HESTIA="/usr/local/hestia" |
| 50 | +fi |
| 51 | + |
| 52 | +if [ -z "$HOMEDIR" ] || [ -z "$HESTIA_INSTALL_DIR" ]; then |
| 53 | + echo "ERROR: Environment variables not present, installation aborted." |
| 54 | + exit 2 |
| 55 | +fi |
| 56 | + |
| 57 | +if [ -z "$(echo "$DB_SYSTEM" | grep -w 'mysql')" ]; then |
| 58 | + echo "ERROR: Mysql not available. Installation aborted" |
| 59 | + exit 2 |
| 60 | +fi |
| 61 | + |
| 62 | +# Get current version |
| 63 | +if [ -f "/var/lib/snappymail/data/VERSION" ]; then |
| 64 | + version=$(cat $SM_INSTALL_DIR/data/VERSION) |
| 65 | + if [ "$version" == "$sm_v" ]; then |
| 66 | + echo "Error: Installed version ($version) is equal to the available version ($sm_v)" |
| 67 | + exit 2 |
| 68 | + else |
| 69 | + UPDATE="yes" |
| 70 | + fi |
| 71 | +fi |
| 72 | + |
| 73 | +# Perform verification if read-only mode is enabled |
| 74 | +check_hestia_demo_mode |
| 75 | + |
| 76 | +#----------------------------------------------------------# |
| 77 | +# Action # |
| 78 | +#----------------------------------------------------------# |
| 79 | + |
| 80 | +if [ "$UPDATE" == "no" ]; then |
| 81 | + rm -f -r $SM_INSTALL_DIR |
| 82 | + rm -f -r $SM_CONFIG_DIR |
| 83 | + |
| 84 | + mkdir $SM_INSTALL_DIR |
| 85 | + mkdir $SM_CONFIG_DIR |
| 86 | + |
| 87 | + cd "$SM_INSTALL_DIR" |
| 88 | + [ ! -f "${SM_INSTALL_DIR}/${SM_FILE}" ] && wget "$SM_URL" --retry-connrefused --quiet -O "${SM_INSTALL_DIR}/${SM_FILE}" |
| 89 | + |
| 90 | + if [ ! -f "${SM_INSTALL_DIR}/${SM_FILE}" ]; then |
| 91 | + echo "ERROR: Download failed, installation aborted." |
| 92 | + exit 2 |
| 93 | + fi |
| 94 | + |
| 95 | + # Get current version |
| 96 | + |
| 97 | + key=$(openssl rand -hex 4) |
| 98 | + |
| 99 | + admin_account="admin_$key" |
| 100 | + admin_password=$(generate_password) |
| 101 | + |
| 102 | + echo "Username: admin_$key" > ~/.snappymail |
| 103 | + echo "Password: $admin_password" >> ~/.snappymail |
| 104 | + echo "Secret key: admin_$key" >> ~/.snappymail |
| 105 | + |
| 106 | + tar -xzf ${SM_INSTALL_DIR}/${SM_FILE} |
| 107 | + |
| 108 | + mv ./data $SM_CONFIG_DIR/ |
| 109 | + ln -s $SM_CONFIG_DIR/data/ ./data |
| 110 | + |
| 111 | + if [ -f '/usr/bin/mariadb' ]; then |
| 112 | + mariadb -e "CREATE DATABASE snappymail" 2>&1 |
| 113 | + r=$(generate_password) |
| 114 | + mariadb -e "GRANT ALL ON snappymail.* |
| 115 | + TO snappymail@localhost IDENTIFIED BY '$r'" |
| 116 | + else |
| 117 | + mysql -e "CREATE DATABASE snappymail" 2>&1 |
| 118 | + r=$(generate_password) |
| 119 | + mysql -e "GRANT ALL ON snappymail.* |
| 120 | + TO snappymail@localhost IDENTIFIED BY '$r'" |
| 121 | + fi |
| 122 | + php -f $HESTIA_COMMON_DIR/snappymail/install.php "admin_$key" "$admin_password" "$r" "$BACKEND_PORT" |
| 123 | + |
| 124 | + chown -R www-data:www-data ./data |
| 125 | + chown -R www-data:www-data $SM_CONFIG_DIR/ |
| 126 | + |
| 127 | + rm ${SM_INSTALL_DIR}/${SM_FILE} |
| 128 | + # Add robots.txt |
| 129 | + echo "User-agent: *" > $SM_INSTALL_DIR/robots.txt |
| 130 | + echo "Disallow: /" >> $SM_INSTALL_DIR/robots.txt |
| 131 | + |
| 132 | + # Updating hestia.conf |
| 133 | + if [ -z "$(grep WEBMAIL_SYSTEM $HESTIA/conf/hestia.conf)" ]; then |
| 134 | + $BIN/v-change-sys-config-value 'WEBMAIL_SYSTEM' 'snappymail' |
| 135 | + else |
| 136 | + if [ -z "$(echo "$WEBMAIL_SYSTEM" | grep -w 'snappymail')" ]; then |
| 137 | + if [ -n "$WEBMAIL_SYSTEM" ]; then |
| 138 | + $BIN/v-change-sys-config-value 'WEBMAIL_SYSTEM' "snappymail,$WEBMAIL_SYSTEM" |
| 139 | + else |
| 140 | + $BIN/v-change-sys-config-value 'WEBMAIL_SYSTEM' "snappymail" |
| 141 | + fi |
| 142 | + fi |
| 143 | + fi |
| 144 | + |
| 145 | +else |
| 146 | + [ ! -f "${SM_INSTALL_DIR}/${SM_FILE}" ] && wget "$SM_URL" --quiet -O "${SM_INSTALL_DIR}/${SM_FILE}" |
| 147 | + version=$(cat $SM_INSTALL_DIR/data/VERSION) |
| 148 | + |
| 149 | + tar -xzf snappymail-latest.tar.gz "data/VERSION" $SM_INSTALL_DIR/ |
| 150 | + version_source=$(cat $SM_INSTALL_DIR/VERSION) |
| 151 | + |
| 152 | + # Check version inside .tar.gz file in case hestia didn't update yet |
| 153 | + if [ "$version" != "$version_source" ]; then |
| 154 | + tar -xzf ${SM_INSTALL_DIR}/${SM_FILE} |
| 155 | + rm $SM_INSTALL_DIR/$SM_FILE |
| 156 | + fi |
| 157 | + rm ${SM_INSTALL_DIR}/VERSION |
| 158 | +fi |
| 159 | + |
| 160 | +#----------------------------------------------------------# |
| 161 | +# Hestia # |
| 162 | +#----------------------------------------------------------# |
| 163 | + |
| 164 | +if [ "$UPDATE" = "yes" ]; then |
| 165 | + $BIN/v-log-action "system" "Info" "Plugins" "SnappyMail updated (Version: $version)." |
| 166 | +else |
| 167 | + $BIN/v-log-action "system" "Info" "Plugins" "SnappyMail enabled (Version: $version)." |
| 168 | +fi |
| 169 | +log_event "$OK" "$ARGUMENTS" |
0 commit comments