#!/bin/sh
### Copyright 1999-2014. Parallels IP Holdings GmbH. All Rights Reserved.
#

#
# Plesk script
#


### Copyright 1999-2014. Parallels IP Holdings GmbH. All Rights Reserved.
# Migration manager tables will be managed by plesk


### Copyright 1999-2014. Parallels IP Holdings GmbH. All Rights Reserved.
# vim:ft=sh
# Usage:  pleskrc <service> <action>
pleskrc()
{
	[ 2 -le $# ] || die "Not enough arguments"

	local service_name=$1
	local action=$2
	local ret=0
	local inten
	local service_script=""
	shift
	shift

	# Now check redefined functions
	if test "$machine" = "linux" && is_function "${service_name}_${action}_${machine}_${linux_distr}"; then
		"${service_name}_${action}_${machine}_${linux_distr}" $@
		return $?
	elif is_function "${service_name}_${action}_${machine}"; then
		"${service_name}_${action}_${machine}" $@
		return $?
	elif is_function "${service_name}_${action}"; then
		"${service_name}_${action}" $@
		return $?
	fi

	# Not redefined - call default action
	eval "service=\$${service_name}_service"
	[ -n "$service" ] || die "$action $service_name service (Empty service name for '$service_name')"

	inten="$action service $service"
	[ "$action" = "status" -o "$action" = "exists" ] || echo_try "$inten"

	service_ctl $action $service $service_name

	ret="$?"
	if [ "$action" != "status" -a "${action}" != "exists" ]; then
		[ "$ret" -eq 0 ] && suc || warn $inten
	fi

	return $ret
}

# NOTE:
# 	Function service_ctl in both variations are just helper for pleskrc().
# 	Do not call it directly, use pleskrc()!!!
service_ctl()
{
	local action=$1
	local service=$2
	#local service_name=$3 - ignored now, just unification with sysv service_ctl

	case "${action}" in
		exists)
			#systemd is compatible with sysV init scripts, so check also rc_d
			test -f /usr/lib/systemd/system/${service}.service || test -f ${SYSTEM_RC_D}/${service}
			return
			;;
		status)
			action="is-active"
			;;
		reload)
			action='reload-or-try-restart'
			;;
	esac

	/bin/systemctl ${action} ${service}.service >> $product_log 2>&1
}

is_function()
{
	local type_output="`type \"$1\" 2>/dev/null | head -n1 | awk '{print $NF}'`"
	test "X${type_output}" = "Xfunction"
}

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

# echo message to product log without new line, unless debug
pnnl_echo()
{
    if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" -o -z "$product_log" ] ; then
        echo -n "$*"
    else
        echo -n "$*" >> "$product_log" 2>&1
    fi
}

die()
{
	PACKAGE_SCRIPT_FAILED="$*"

	printf "\a\a"
	report_problem \
		"ERROR while trying to $*" \
		"Check the error reason(see log file: ${product_log}), fix and try again"

	selinux_close

	exit 1
}

simply_die()
{
	report_problem "$@"
	exit 1
}

warn()
{
	local inten
	inten="$1"
	p_echo
	p_echo "WARNING!"
	pnnl_echo "Some problems are found during $inten"
	p_echo "(see log file: ${product_log})"
	p_echo
	p_echo "Continue..."
	p_echo

	product_log_tail | send_error_report_with_input "Warning: $inten"

	[ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ] || \
	product_log_tail
}

# Use this function to report failed actions.
# Typical report should contain
# - reason or problem description (example: file copying failed)
# - how to resolve or investigate problem (example: check file permissions, free disk space)
# - how to re-run action (example: perform specific command, restart bootstrapper script, run installation again)
report_problem()
{
	[ -n "$product_problems_log" ] || product_problems_log="/dev/stderr"

	p_echo
	if [ "0$problems_occured" -eq 0 ]; then
		echo "***** $process problem report *****" >> "$product_problems_log" 2>&1
	fi
	for problem_message in "$@"; do
		p_echo "$problem_message"
		echo "$problem_message" >> "$product_problems_log" 2>&1
	done
	p_echo

	product_log_tail | send_error_report_with_input "Problem: $@"

	[ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ] || \
		product_log_tail

	problems_occured=1
}

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

suc()
{
	p_echo "done"
}

# do not call it w/o input! Use send_error_report in these cases.
send_error_report_with_input()
{
	{
		echo $@
		echo ""
		if [ -n "$error_report_context" ]; then
			echo "Context: $error_report_context"
			echo ""
		fi
		if [ -n "$RP_LOADED_PATCHES" ]; then
			echo "Loaded runtime patches: $RP_LOADED_PATCHES"
			echo ""
		fi
		cat -
	} | $PRODUCT_ROOT_D/admin/bin/send-error-report "install" >/dev/null 2>&1
}

detect_vz()
{
	PLESK_VZ=0
	PLESK_VE_HW_NODE=0
	PLESK_VZ_TYPE=

	local issue_file="/etc/issue"
	local vzcheck_file="/proc/self/status"
	[ -f "$vzcheck_file" ] || return 1

	local env_id=`sed -ne 's|^envID\:[[:space:]]*\([[:digit:]]\+\)$|\1|p' "$vzcheck_file"`
	[ -n "$env_id" ] || return 1
	if [ "$env_id" = "0" ]; then
		# Either VZ/OpenVZ HW node or unjailed CloudLinux
		PLESK_VE_HW_NODE=1
		return 1
	fi

	if grep -q "CloudLinux" "$issue_file" >/dev/null 2>&1 ; then
		return 1
	fi

	if [ -f "/proc/vz/veredir" ]; then
		PLESK_VZ_TYPE="vz"
	elif [ -d "/proc/vz" ]; then
		PLESK_VZ_TYPE="openvz"
	fi

	PLESK_VZ=1
	return 0
}

call_optional_function()
{
	export LANG=C LC_MESSAGES=C LC_ALL=C
	local type_output="`type \"$1\" 2>/dev/null | head -n 1`"
	case "$type_output" in
		*function)
			"$@"
			;;
		*)
			return 0
			;;
	esac
}

product_log_tail()
{
	[ -f "$product_log" ] || return 0
	tac "$product_log" | awk '/^START/ { exit } { print }' | tac
}
### Copyright 1999-2014. Parallels IP Holdings 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 "$@"
}

get_groupID()
{
# try to get GID, id -g doesn't show groups without users
	common_var=`cat /etc/group | awk -F':' '$1 == "'$1'" {print $3}'` 

# We have non-empty value if success
	test -n "$common_var"
}

selinux_close()
{
	if [ -z "$SELINUX_ENFORCE" -o "$SELINUX_ENFORCE" = "Disabled" ]; then
		return
	fi

	setenforce "$SELINUX_ENFORCE"
}
### Copyright 1999-2014. Parallels IP Holdings 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=C
	export PATH LANG
	umask 022
	ulimit -n 65535 2>/dev/null

	K_HUP="/bin/kill -HUP"
	K_KILL="/bin/kill -KILL"
	K_TERM="/bin/kill -TERM"
	K_USR2="/bin/kill -USR2"
	K_TEST="/bin/kill -0"

	users_created=""
	groups_created=""

	certificate_file="$PRODUCT_ETC_D/httpsd.pem"
	services="/etc/services"
	mtab="/etc/mtab"
	get_hostname="hostname"
	get_domainname="domainname"

	#VZP used to determine that we're inside SVE
	vza_file="/var/vzagent"

	#default parameters
	tar="tar"
	crontab="/usr/bin/crontab"

	cp_preserve="cp -p"
	SYSTEM_RC_D=/etc/init.d
	PLESK_LIBEXEC_DIR="/usr/lib/plesk-9.0"
	PLESK_DB_DIR="/var/lib/plesk"
	POSTFIX_LIBEXEC_DIR="/usr/lib/postfix"
	PRODUCT_BOOTSTRAPPER_DIR="/usr/local/psa/bootstrapper/pp12.0.18-bootstrapper"
	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 PARALLELS PLESK PANEL.\n"
	PRODUCT_LOGS_D="/var/log/plesk"

	set_common_params_linux 

	rp_patch_runtime

	detect_vz
}

set_common_params_linux()
{
	get_hostname="hostname -f"
	fstab="/etc/fstab"
	cp_preserve="cp --preserve=all --remove-destination"
	machine="linux"
	sendmail="/usr/sbin/sendmail"
	ps="ps axw"
	ps_long="ps axuw"
	false_shell="/bin/false"
	dummy_home="/"
	compress="gzip -9 -c"
	uncompress="gunzip -c"
	uudecode="uudecode -o /dev/stdout"
	ifconfig="/sbin/ifconfig -a"
	inet_str="inet addr"

	useradd_options="-r"
	if [ -f /etc/SuSE-release ]; then
		linux_distr="suse"
	elif [ -f /etc/debian_version ]; then
		linux_distr="debian"
		get_domainname="dnsdomainname"
	else
		linux_distr="redhat"
	fi

	sndml_ini="/etc/init.d/sendmail"
	mail_local="/usr/libexec/mail.local"
	if [ -x /sbin/nologin ]; then
		dummy_shell="/sbin/nologin"
	else
		dummy_shell="/bin/false"
	fi
	bash_shell="/bin/bash"
	rbash_shell="/bin/rbash"
	uudecode_full="/usr/bin/uudecode"
	named_osrelease=`cat /proc/sys/kernel/osrelease | perl -F"/[.-]/" -n -a  -e 'printf "%02u%02u%02u\n", $F[0],$F[1],$F[2]'`

	return 0
}

# vim:ft=sh:
### Copyright 1999-2014. Parallels IP Holdings GmbH. All Rights Reserved.
# -*- shell-script -*-
# configures Mailman to use a given mailserver
# Currently Postfix and QMail, both as "Manual" (see comments below)
set_mailman_mailsrv()
{
    local cfg server

    cfg=$1
    server=$2

    if [ -z "$cfg" ]; then
        simply_die "Usage: $0 /path/to/mm_cfg.py (qmail|postfix)"
    fi

    case "$server" in
        qmail)
            server="Manual"
            ;;
        postfix)
            # Although Mailman supports Postfix "directly" (i.e. it has
            # a code to control Postfix system aliases (typically, /etc/aliases)
            # we keep Mailman related system aliases in an additional hashed
            # database (hash:/var/spool/postfix/plesk/aliases)
            # (for various reasons), so we have to manage Mailman-specific alias
            # manually as with QMail
            server="Manual"
            ;;
        *)
            simply_die "Currently only qmail and postfix are supported"
            ;;
    esac

    if [ ! -f "$cfg" ]; then
        simply_die "Usage: no such file or directory \"$cfg\""
    fi

    #declare pymod pydir curmta
    local pydir pymod
    pydir=$(dirname "$cfg")
    [ -z "$pydir" ] && pydir="."
    pymod=$(basename "$cfg")
    pymod=${pymod%%.py}

    local python_bin
    python_bin="/usr/bin/python"

    local currentServer
    currentServer=$(PYTHONPATH="${PYTHONPATH}:$pydir" "${python_bin}" -c "import $pymod; print $pymod.MTA")
    if [ $? -ne 0 ]; then
        simply_die "Unable to load current settings"
    fi

    if [ "x${currentServer}" = "x${server}" ]; then
        # nothing to do
        return 0
    fi

    local tmpcfg bakcfg

    bakcfg="${cfg}.saved_by_plesk"
    tmpcfg=$(mktemp "${cfg}.XXXXXX")
    if [ $? != 0 ]; then
        simply_die "Unable to creare a temporary file for ${cfg}"
    fi
    trap "mailman_trap_handler \"$tmpcfg\"" HUP PIPE INT TERM EXIT
    awk -v server="$server" 'BEGIN { found=0; }
/^[[:space:]]*MTA[[:space:]]*=/ {
    print "### ", $0;
    if (!found) {
        str=$0;
        sub(/=.*/, "='"'"'" server "'"'"'", str);
        $0=str;
        found=1;
    }
}
{ print $0; }
END {
    if (!found) {
        print "MTA='"'"'" server "'"'"'";
    };
    exit 0;
}' "$cfg" >>"$tmpcfg"
    rc=$?
    if [ 0 -eq $rc ]; then
        p_echo "updating configuration file $cfg"
        rm -f "$bakcfg" 2>/dev/null; # errors will be reported later
        ln "$cfg" "$bakcfg" &&             mv -f "$tmpcfg" "$cfg" || simply_die "unable to save new configuration"
        rm -f "$tmpcfg"
        trap - HUP PIPE INT TERM EXIT
    else
        simply_die "unable to process \"$cfg\""
    fi
    return 0
}

# This function tries to configure Plesk-specific
# wrapper (mm_wrapper) which ensures that
# /usr/lib/mailman/mail/mailman will be called with
# the correct group.
# The problem is that on RH* and derivatives
# /usr/lib/mailman/mail/mailman should be called
# from within a process with one of the following groups:
# mail, nobody, postfix, daemon
# while on SuSE this should be a group whose gid is
# stored in either /etc/mailman/mailman.mail-gid or
# /etc/mailman/mailman.cgi-gid. This group must to be
# coincide with GID for postfix-mailman process from
# postfix master.cf config file.
# So if the file /etc/mailman/mailman.mail-gid exist
# we ensure that it contains an appropriate gid and
# change the file if needed (preserving a backup)
# If the file is missing then we make sure that
# mm_wrapper is SGID mail.
#
# Requirements: coreutils, grep, sed, perl


#last update:
#on CE/RH we ship mm_wrapper with correct owner/perms
#on SUSE tune mailman's GID config
#on deb-based OS do nothing
set_mailman_wrapper_perms()
{
	local gidfile="/etc/mailman/mailman.mail-gid"
	local backup=.saved_by_psa
	local mailman_group=$1

	if [ -z "${mailman_group}" ]; then
		warn "unspecified group for maillist delivery"
		return 1
	fi

	if [ ! -r ${gidfile} ]; then
		p_echo "mailman GID file '${gidfile}' doesn't exist (possibly mailman is not installed), skip tuning"
		return
	fi


	if ! get_groupID "${mailman_group}" ; then
		warn "detect group ID of ${mailman_group}"
		return 1
	fi

	cp -pr "$gidfile" "$gidfile$backup" || simply_die "Unable to create a backup file";
	echo -n "$common_var" > "$gidfile";	
}

mailman_trap_handler()
{
    trap - EXIT
    rm -f -- "$1"
    exit
}

### Copyright 1999-2014. Parallels IP Holdings GmbH. All Rights Reserved.

set_postfix_params()
{
	postfix_service="postfix"
	pc_remote_service="pc-remote"
}


postfix_status()
{
	# here be dragons.
	# the practical experience shows that simple checking of status of
	# Postfix "master" process is not enough. So we read Postfix master
	# process pid file if any, then try to look for a process with
	# name ``qmgr'' and parent pid being equal to
	# the pid read from the pidfile. If pgrep finds such a process
	# it returns 0, if not its exit status is non-zero.
	# pgrep is portable enough to prefer it to "hand-made" alternatives
	# such as famous ``ps | grep $name | grep -v grep...'' pipes
	# bug 147822. do not interrupt installation for FreeBSD

	[ -f "/var/spool/postfix/pid/master.pid" ] || return 1

	local ppid

	read ppid </var/spool/postfix/pid/master.pid 2>/dev/null
	if [ $? -ne 0 -o -z "$ppid" ]; then
		# not found or other error
		return 1;
	fi
	pgrep -P $ppid qmgr >/dev/null 2>/dev/null
}
### Copyright 1999-2014. Parallels IP Holdings GmbH. All Rights Reserved.
# -*- shell-script -*-
# A quick script to fix MTA configuration setting in
# Mailman configuration file /path/to/mm_cfg.py

# Usage: mailman_conf_init /path/to/mm_cfg.py
# Requirements: coreutils, awk, python


# Until logging setup for standalone utilities is implemented
# we do not log to file (only to console)
init_log_stub()
{
	product_log=/dev/null
	product_problems_log=/dev/null
	problems_occured=0
}

[ "X${PLESK_INSTALLER_DEBUG}" = "X" ] || set -x
[ "X${PLESK_INSTALLER_STRICT_MODE}" = "X" ] || set -e

init_log_stub

if [ -z "$1" ]; then
	echo "Usage: $0 /path/to/mm_cfg.py"
	exit 1
fi

if ! set_mailman_mailsrv "$1" postfix; then
    echo "unable to configure Mailman to work with Postfix"
	exit 1
fi

# Hopefully, Postfix doesn't need mm_wrapper at all and
# thus don't need to do black magic with its permissions

# Unfortunately need. bug #148309
set_common_params
if ! set_mailman_wrapper_perms mailman; then
    echo "unable to set Mailman wrapper permissions"
	exit 1
fi

# We also need to adjust Mailman delivery transport 
# recipient limit as wrapper does not understand 
# multiple maillists.
# Some systems do not support '/etc/init.d/postfix status',
# so we use ours.
set_postfix_params
true postfix_status
if ! /usr/sbin/postconf -e mailman_destination_recipient_limit=1; then
	echo "unable to adjust Mailman delivery transport recipient limit for Postfix"
	exit 1
fi
pleskrc postfix reload
