Browse Source

Merge pull request #4349 from Anuril/fix-v-sys-add-snappymail

Fixes handling of snappymail
Jaap Marcus 1 year ago
parent
commit
a0821ae4fe
3 changed files with 107 additions and 10 deletions
  1. 14 7
      bin/v-add-sys-snappymail
  2. 89 0
      bin/v-delete-sys-snappymail
  3. 4 3
      install/common/snappymail/install.php

+ 14 - 7
bin/v-add-sys-snappymail

@@ -92,22 +92,26 @@ if [ "$UPDATE" == "no" ]; then
 		exit 2
 	fi
 
-	# Get current version
-
 	key=$(openssl rand -hex 4)
-
+	suffix=$(openssl rand -hex 4)
 	admin_account="admin_$key"
 	admin_password=$(generate_password)
 
 	echo "Username: admin_$key" > ~/.snappymail
 	echo "Password: $admin_password" >> ~/.snappymail
-	echo "Secret key: admin_$key" >> ~/.snappymail
+	echo "Admin Panel key: admin_$suffix" >> ~/.snappymail
 
 	tar -xzf ${SM_INSTALL_DIR}/${SM_FILE}
+	# Get current version
+	version=$(cat ./data/VERSION)
 
 	mv ./data $SM_CONFIG_DIR/
 	ln -s $SM_CONFIG_DIR/data/ ./data
 
+	# Create cache folder and set permissions
+	mkdir -p "$SM_CONFIG_DIR/data/_data_/_default_/cache/"
+	chown -R www-data:www-data ./data
+
 	if [ -f '/usr/bin/mariadb' ]; then
 		mariadb -e "CREATE DATABASE snappymail" 2>&1
 		r=$(generate_password)
@@ -119,11 +123,14 @@ if [ "$UPDATE" == "no" ]; then
 		mysql -e "GRANT ALL ON snappymail.*
 		 TO snappymail@localhost IDENTIFIED BY '$r'"
 	fi
-	php -f $HESTIA_COMMON_DIR/snappymail/install.php "admin_$key" "$admin_password" "$r" "$BACKEND_PORT"
-
-	chown -R www-data:www-data ./data
+	# Temporarily set the permissions to www-data
 	chown -R www-data:www-data $SM_CONFIG_DIR/
+	php -f $HESTIA_COMMON_DIR/snappymail/install.php "admin_$key" "admin_$suffix" "$admin_password" "$r" "$BACKEND_PORT"
+
+	# Set the permissions back to hestiamail
+	chown -R hestiamail:www-data $SM_CONFIG_DIR/
 
+	# Remove the downloaded file
 	rm ${SM_INSTALL_DIR}/${SM_FILE}
 	# Add robots.txt
 	echo "User-agent: *" > $SM_INSTALL_DIR/robots.txt

+ 89 - 0
bin/v-delete-sys-snappymail

@@ -0,0 +1,89 @@
+#!/bin/bash
+# info: Delete SnappyMail webmail client
+# options: None
+#
+# This function removes the SnappyMail webmail client.
+
+#----------------------------------------------------------#
+#                Variables & Functions                     #
+#----------------------------------------------------------#
+
+# Includes
+# shellcheck source=/etc/hestiacp/hestia.conf
+source /etc/hestiacp/hestia.conf
+# shellcheck source=/usr/local/hestia/func/main.sh
+source $HESTIA/func/main.sh
+# load config file
+source_conf "$HESTIA/conf/hestia.conf"
+# upgrade config file
+source "$HESTIA/install/upgrade/upgrade.conf"
+
+# Folder paths
+SM_INSTALL_DIR="/var/lib/snappymail"
+SM_CONFIG_DIR="/etc/snappymail"
+SM_LOG="/var/log/snappymail"
+
+#----------------------------------------------------------#
+#                    Verifications                         #
+#----------------------------------------------------------#
+
+# Checking root permissions
+if [ "x$(id -u)" != 'x0' ]; then
+	echo "ERROR: v-delete-sys-snappymail can only be executed by the root user"
+	exit 10
+fi
+
+# Ensure that $HESTIA (/usr/local/hestia/) and other variables are valid.
+if [ -z "$HESTIA" ]; then
+	HESTIA="/usr/local/hestia"
+fi
+
+if [ -z "$HOMEDIR" ] || [ -z "$HESTIA_INSTALL_DIR" ]; then
+	echo "ERROR: Environment variables not present, uninstallation aborted."
+	exit 2
+fi
+
+if [ -z "$(echo "$DB_SYSTEM" | grep -w 'mysql')" ]; then
+	echo "ERROR: Mysql not available. Uninstallation aborted"
+	exit 2
+fi
+
+# Get current version
+if [ -f "/var/lib/snappymail/data/VERSION" ]; then
+	version=$(cat /var/lib/snappymail/data/VERSION)
+else
+	echo "Error: SnappyMail is not installed"
+	exit 2
+fi
+
+# Perform verification if read-only mode is enabled
+check_hestia_demo_mode
+
+#----------------------------------------------------------#
+#                       Action                             #
+#----------------------------------------------------------#
+
+rm -f -r $SM_INSTALL_DIR
+rm -f -r $SM_CONFIG_DIR
+rm ~/.snappymail
+
+if [ -f '/usr/bin/mariadb' ]; then
+	mariadb -e "DROP DATABASE snappymail" 2>&1
+	mariadb -e "DROP USER snappymail@localhost"
+else
+	mysql -e "DROP DATABASE snappymail" 2>&1
+	mysql -e "DROP USER snappymail@localhost"
+fi
+# Updating hestia.conf
+if [ -z "$(echo "$WEBMAIL_SYSTEM" | grep -w 'snappymail')" ]; then
+	# remove snappymail from webmail list and make sure the string doesn't start with a comma
+	$BIN/v-change-sys-config-value 'WEBMAIL_SYSTEM' "$(echo "$WEBMAIL_SYSTEM" | sed "s/snappymail//g" | sed 's/^,//g')"
+fi
+
+#----------------------------------------------------------#
+#                       Hestia                             #
+#----------------------------------------------------------#
+
+$BIN/v-log-action "system" "Info" "Plugins" "SnappyMail removed (Version: $version)."
+
+log_event "$OK" "$ARGUMENTS"

+ 4 - 3
install/common/snappymail/install.php

@@ -7,8 +7,9 @@ $oConfig = \RainLoop\Api::Config();
 
 // Change default login data / key
 $oConfig->Set("security", "admin_login", $argv[1]);
-$oConfig->Set("security", "admin_panel_key", $argv[1]);
-$oConfig->SetPassword($argv[2]);
+$oConfig->Set("security", "admin_panel_key", $argv[2]);
+$newPassword = new \SnappyMail\SensitiveString($argv[3]);
+$oConfig->SetPassword($newPassword);
 
 // Allow Contacts to be saved in database
 $oConfig->Set("contacts", "enable", "On");
@@ -16,7 +17,7 @@ $oConfig->Set("contacts", "allow_sync", "On");
 $oConfig->Set("contacts", "type", "mysql");
 $oConfig->Set("contacts", "pdo_dsn", "mysql:host=127.0.0.1;port=3306;dbname=snappymail");
 $oConfig->Set("contacts", "pdo_user", "snappymail");
-$oConfig->Set("contacts", "pdo_password", $argv[3]);
+$oConfig->Set("contacts", "pdo_password", $argv[4]);
 
 // Plugins
 $oConfig->Set("plugins", "enable", "On");