#!/bin/bash
### Copyright 1999-2025. WebPros International GmbH. All rights reserved.
#

#
# Plesk script
#



#default values

product_default_conf()
{

PRODUCT_ROOT_D=/usr/local/psa
PRODUCT_RC_D=/etc/rc.d/init.d
PRODUCT_ETC_D=/usr/local/psa/etc
PLESK_LIBEXEC_DIR=/usr/lib64/plesk-9.0
HTTPD_VHOSTS_D=/var/www/vhosts
HTTPD_CONF_D=/etc/httpd/conf
HTTPD_INCLUDE_D=/etc/httpd/conf.d
HTTPD_BIN=/usr/sbin/httpd
HTTPD_LOG_D=/var/log/httpd
HTTPD_SERVICE=httpd
QMAIL_ROOT_D=/var/qmail
PLESK_MAILNAMES_D=/var/qmail/mailnames
RBLSMTPD=/usr/sbin/rblsmtpd
NAMED_RUN_ROOT_D=/var/named/chroot
WEB_STAT=/usr/bin/webalizer
MYSQL_VAR_D=/var/lib/mysql
MYSQL_BIN_D=/usr/bin
MYSQL_SOCKET=/var/lib/mysql/mysql.sock
PGSQL_DATA_D=/var/lib/pgsql/data
PGSQL_CONF_D=/var/lib/pgsql/data
PGSQL_BIN_D=/usr/bin
DUMP_D=/var/lib/psa/dumps
DUMP_TMP_D=/tmp
MAILMAN_ROOT_D=/usr/lib/mailman
MAILMAN_VAR_D=/var/lib/mailman
PYTHON_BIN=/usr/bin/python2
GPG_BIN=/usr/bin/gpg
TAR_BIN=/usr/lib64/plesk-9.0/sw-tar
AWSTATS_ETC_D=/etc/awstats
AWSTATS_BIN_D=/usr/share/awstats/wwwroot/cgi-bin
AWSTATS_TOOLS_D=/usr/share/awstats/tools
AWSTATS_DOC_D=/usr/share/awstats/wwwroot
OPENSSL_BIN=/usr/bin/openssl
LIB_SSL_PATH=/lib/libssl.so
LIB_CRYPTO_PATH=/lib/libcrypto.so
CLIENT_PHP_BIN=/usr/local/psa/bin/php-cli
SNI_SUPPORT=true
APS_DB_DRIVER_LIBRARY=/usr/lib64/sw/libmysqlserver.so.2.0
SA_MAX_MAIL_SIZE=256000

}

# echo message to product log, also to console in debug mode
p_echo()
{
    if [ -n "$product_log" ] ; then
        echo "$@" >> "$product_log" 2>&1
    fi
    if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" -o -z "$product_log" ] ; then
        echo "$@" >&2
    fi
}

# same as p_echo, but without new line
pnnl_echo()
{
	p_echo -n "$@"
}

echo_try()
{
	msg="$*"
	pnnl_echo " Trying to $msg... "
}

suc()
{
	p_echo "done"
}

get_default_locale()
{
	# Note that CentOS 7 typically doesn't have C.UTF-8
	for lc in "C.UTF-8" "en_US.UTF-8" "C"; do
		if [ -z "`LC_ALL=$lc locale 2>&1 >/dev/null`" ]; then
			echo "$lc"
			return 0
		fi
	done
	echo "C"
}

call_optional_function()
{
	local type_output="`LC_ALL=C type \"$1\" 2>/dev/null | head -n 1`"
	case "$type_output" in
		*function)
			"$@"
			;;
		*)
			return 0
			;;
	esac
}
### Copyright 1999-2025. WebPros International GmbH. All rights reserved.

#
# Support for runtime patching of shell scripts (including utilities and package scripts).
#

# --- Service functions ---

# Load and apply a patch in a relatively safe way
rp_safe_load_patch()
{
	local patch_file="$1"
	echo_try "load shell patch '$patch_file'"
	/bin/sh -n "$RP_BASEDIR/$patch_file" && 
	{
		. "$RP_BASEDIR/$patch_file"
		RP_LOADED_PATCHES="$RP_LOADED_PATCHES $patch_file"
	} &&
	suc
}

# Apply patches specific to the current context (e.g., depending on utility basename or package name)
# This is currently not implemented. This may be overriden by "spark".
rp_patch_runtime_context_specific()
{
	:
}

# --- Main entry points ---

rp_patch_runtime()
{
	# List of loaded patch files
	RP_LOADED_PATCHES=

	local RP_BASEDIR="$PRODUCT_BOOTSTRAPPER_DIR/rp"
	[ -d "$RP_BASEDIR" ] || return 0

	if [ -r "$RP_BASEDIR/spark" ]; then
		rp_safe_load_patch "spark"
	fi

	call_optional_function rp_patch_runtime_context_specific "$@"
}
### Copyright 1999-2025. WebPros International GmbH. All rights reserved.
# vim:ft=sh

initial_conf()
{
	PRODNAME="psa"
	PRODUCT_NAME="psa"
	product=${PRODNAME}
	PRODUCT_FULL_NAME="Plesk"

	product_etc="/etc/${PRODNAME}"
	prod_conf_t="/etc/psa/psa.conf"

	support_contact="https://support.plesk.com/"

	conceived_os_vendor=RedHat
	conceived_os_version="el9"

	clients_group="psacln"
	clients_GID="10001"

	services_group="psaserv"
	services_GID="10003"

	product_suff="saved_by_${product}".`date "+%m.%d;%H:%M"`
	product_suffo="saved_by_${product}"

	# plesk default password
	PRODUCT_DEFAULT_PASSWORD="setup"
}

read_conf()
{
	[ -n "$prod_conf_t" ] || prod_conf_t=/etc/psa/psa.conf

	if [ -s $prod_conf_t ]; then
		tmp_var=`perl -e 'undef $/; $_=<>; s/#.*$//gm;
		         s/^\s*(\S+)\s*/$1=/mg;
		         print' $prod_conf_t`
		eval $tmp_var
	else
		if ! is_product_installation; then
			p_echo "Unable to find product configuration file: $prod_conf_t. Default values will be used."
			return 1
		fi
	fi
	return 0
}

### Copyright 1999-2025. WebPros International GmbH. All rights reserved.
# vim:ft=sh:

#set_params

set_common_params()
{
	common_var=0

	PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
	LANG="`get_default_locale`"
	export PATH LANG
	unset GREP_OPTIONS
	umask 022
	ulimit -n 65535 2>/dev/null

	get_product_versions

	certificate_file="$PRODUCT_ETC_D/httpsd.pem"
	services="/etc/services"

	crontab="/usr/bin/crontab"

	SYSTEM_RC_D="/etc/rc.d/init.d"
	PLESK_LIBEXEC_DIR="/usr/lib64/plesk-9.0"
	PLESK_DB_DIR="/var/lib/plesk"
	PRODUCT_BOOTSTRAPPER_DIR="`printf "/usr/local/psa/bootstrapper/pp%s-bootstrapper" "$product_this_version"`"
	AUTOGENERATED_CONFIGS="#ATTENTION!\n#\n#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,\n#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.\n"
	AUTOGENERATED_CONFIGS_UPGRADE="#ATTENTION!\n#\n#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,\n#SO ALL YOUR CHANGES WILL BE LOST AFTER YOU UPGRADE PLESK.\n"
	PRODUCT_LOGS_D="/var/log/plesk"

	sendmail="/usr/sbin/sendmail"
	ps="ps axw"
	ifconfig="/sbin/ifconfig -a"

	machine="linux"
	if [ -f /etc/debian_version ]; then
		linux_distr="debian"
	else
		linux_distr="redhat"
	fi

	dummy_home="/"
	if [ -x /usr/sbin/nologin ]; then
		dummy_shell="/usr/sbin/nologin"
	else
		dummy_shell="/bin/false"
	fi

	rp_patch_runtime
}

get_product_versions()
{
	# Don't use global variables set elsewhere in this code. Use substitutions if needed.
	local prod_root_d="/usr/local/psa"

	product_name="psa"

	if [ -z "$product_this_version" ]; then
		# 1. Try to fetch version from file created by bootstrapper (should be 3-component).
		product_this_version="`cat "/var/lock/plesk-target-version" 2>/dev/null`"
		# 2. Fallback to $PRODUCT_ROOT_D/version (should be 3-component).
		if [ -z "$product_this_version" -a -r "$prod_root_d/version" ]; then
			product_this_version="`awk '{ print $1 }' "$prod_root_d/version"`"
		fi
		# 3. Fallback to hardcoded version (2-component). This may cause some other code to fail.
		if [ -z "$product_this_version" ]; then
			product_this_version="18.0"
			echo "Unable to determine \$product_this_version, will use less precise value '$product_this_version'" >&2
		fi
	fi

	product_version="$product_this_version"

	if [ -z "$product_prev_version" ]; then
		if [ -r "$prod_root_d/version.upg" ]; then
			product_prev_version=`awk '{ print $1 }' "$prod_root_d/version.upg"`
		elif [ -r "$prod_root_d/version" ]; then
			product_prev_version=`awk '{ print $1 }' "$prod_root_d/version"`
		else
			product_prev_version="$product_this_version"
		fi
	fi
}

# Clean installation of the product is being performed
is_product_installation()
{
	[ "X$do_upgrade" != "X1" -a ! -s "/usr/local/psa/version.upg" ]
}

### Copyright 1999-2025. WebPros International GmbH. All rights reserved.

### This is a compatibility wrapper for legacy monolithic mailmng.
### Serves only to avoid breaking any third-party code. Plesk does not and
### must not use this utility.
###
### Since this is a compatibility wrapper, adding *new* commands to it is not
### necessarry and even bad, since this will slow down the transition.

# --- command and utility definitions ---

MAILMNG_CORE_COMMANDS="
--add-mailname
--remove-mailname
--rename-mailname
--add-mailbox
--remove-mailbox
--set-password
--add-alias
--remove-alias
--add-domain
--remove-domain
--rename-domain
--turn-on-domain
--turn-off-domain
--add-domain-alias
--del-domain-alias
--rename-domain-alias
--get-mailbox-size
--set-mailbox-disk-quota
--set-domain-disk-quota
"

MAILMNG_BLACK_WHITE_COMMANDS="
--add-badmailfrom
--remove-badmailfrom
--add-whitelist
--remove-whitelist
"

MAILMNG_DOMAIN_COMMANDS="
--set-bounce
--set-catchall
--set-reject
--unset-reject
--set-domain-key
--remove-domain-key
--add-domain-to-sender-transport
--remove-domain-from-sender-transport
--remove-sender-transport
"

MAILMNG_MAILNAME_COMMANDS="
--add-redirect
--remove-redirect
--set-autoresponder
--reset-autoresponder
--list-attachments
--add-attachment
--remove-attachment
"

MAILMNG_SERVER_COMMANDS="
--set-maps-protection
--update-spf
--features
--set-max-letter-size
--get-max-letter-size
--set-mail-params
--get-mailbox-size-limit
--add-recipient-address-mapper
"

MAILMNG_SERVICE_COMMANDS="
--start-service
--stop-service
--restart-service
--reload-service
--status-service
"

HELP_COMMANDS="
-h
--help
--help-commands
--help-legacy
--help-stdin
"

DROPPED_COMMANDS="
--add-blacklist --remove-blacklist
--add-handler --update-handler --remove-handler --disable-handler --enable-handler --check-handler
--without-restart
--start-smtpd --stop-smtpd --restart-smtpd --status-smtpd 
--start-qmail-send --stop-qmail-send 
--start-maild --stop-maild --restart-maild --status-maild 
--start-milter --stop-milter --restart-milter --status-milter
"

UTILITIES="
mailmng-black-white
mailmng-core
mailmng-domain
mailmng-mailname
mailmng-server
mailmng-service
"

# --- initialization ---

product_log="/dev/stderr"
product_problems_log="$product_log"
problems_occured=0
bootstrapper_mode_file="/tmp/pp-bootstrapper-mode.flag"

product_default_conf
initial_conf
set_common_params
read_conf

command="$1"

# --- help ---

usage()
{
	if [ -n "$*" ]; then
		echo "ERROR: $*" >&2
		echo >&2
	fi

	echo "Usage: mailmng <COMMAND> <COMMAND OPTIONS...>" >&2
	echo >&2
	echo "WARNING: you are using legacy mailmng compatibility wrapper." >&2
	echo "Please use one of the following utilities instead in the future: `echo \"$UTILITIES\" | sed -e 's/^/    /'`" >&2
	echo "This wrapper suppresses any supplied STDIN. If you're getting errors like" >&2
	echo "'Failed to read and parse session mail data from STDIN.' then you should use one of the" >&2
	echo "utilities mentioned above and supply valid STDIN. See also --help-stdin option." >&2
	echo "Next time consider using Plesk CLI utilities or API instead of relying on its internals." >&2

	exit 2
}

[ -n "$command" ] || usage

for cmd in $HELP_COMMANDS; do
	if [ "$cmd" = "$command" ]; then
		usage
	fi
done

for cmd in $DROPPED_COMMANDS; do
	if [ "$cmd" = "$command" ]; then
		if [ -f "$bootstrapper_mode_file" ]; then
			echo "Command '$0 $@' was skipped as it is not supported anymore." >&2
			exit 0
		else
			usage "$command command is no longer supported. Please use other equivalents available among Plesk mail utilities."
		fi
	fi
done

# --- proxy call to appropriate utility ---

for utility in $UTILITIES; do
	eval "commands=$`echo $utility | tr 'a-z-' 'A-Z_'`_COMMANDS"
	for cmd in $commands; do
		if [ "$cmd" = "$command" ]; then
			exec "$PRODUCT_ROOT_D/admin/sbin/$utility" "$@" < /dev/null
		fi
	done
done

usage "Unknown command: $command"
