#!/bin/bash
################################################################################
# Script Name: version/2.1.27/UPDATE.sh
# Description: Turn on sender binding, and say what the edition is now.
# Company: hitechcloud.org
# Copyright (c) hitechcloud.org
################################################################################
#
# Two things need doing on a running server that a new binary cannot do for
# itself.
#
# The first is the sender-login map. 2.1.27 stops a mailbox being able to send
# as any domain on this server, and that fix is in two halves: a map saying who
# may send as what, and a Postfix restriction that consults it. The map has to
# exist first. A restriction that arrives before its map is a mail outage - so
# this generates the map, checks it, and only then re-renders the mail stack.
#
# The second is the edition. There are two now, and which one this server is
# depends on whether it holds a licence key. Every server that has one keeps
# Enterprise; this says so out loud rather than letting somebody discover it
# from a missing menu.
################################################################################

set -uo pipefail

GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
ok()   { echo -e "[2.1.27] ${GREEN}✓${NC} $*"; }
warn() { echo -e "[2.1.27] ${YELLOW}!${NC} $*" >&2; }

[[ $EUID -eq 0 ]] || { echo "[2.1.27] This update must run as root." >&2; exit 1; }

SENDERLOGIN_FILE="/etc/hitechcloud/mail/postfix-senderlogin"

# 1. The map first, then the restriction that reads it.
if command -v postconf >/dev/null 2>&1 && [[ -d /etc/hitechcloud/mail ]]; then
    if hitechcloudcli native-mail sync >/dev/null 2>&1; then
        if [[ -s "$SENDERLOGIN_FILE" ]]; then
            ok "Sender-login map written ($(wc -l < "$SENDERLOGIN_FILE") entries)"

            # The two parameters, set here rather than by re-running
            # `native-mail install`. The template carries them for a fresh
            # install; this is the same pair applied to a server that is
            # already serving mail, and it does not touch the twenty other
            # things a reinstall would rewrite underneath a running Postfix.
            #
            # Order inside the restriction list is not cosmetic. A list stops
            # at its first verdict, so permit_mynetworks in front of the check
            # permits everything originating on this machine - including every
            # account's PHP reaching localhost:25 - before the check is ever
            # consulted. That is one of the two bugs this release fixes; do
            # not "tidy" the order.
            postconf -e "smtpd_sender_login_maps=hash:${SENDERLOGIN_FILE}" >/dev/null 2>&1
            postconf -e "smtpd_sender_restrictions=reject_authenticated_sender_login_mismatch, permit_mynetworks, permit" >/dev/null 2>&1

            if postfix check >/dev/null 2>&1; then
                if systemctl reload postfix >/dev/null 2>&1; then
                    ok "Sender binding is in force: a mailbox can send as itself and as the aliases it answers"
                else
                    warn "Postfix would not reload - check: systemctl status postfix"
                fi
            else
                warn "Postfix rejected the new configuration; it has NOT been reloaded. Check: postfix check"
            fi
        else
            # No mailboxes is a legitimate state, and enabling a restriction
            # against an empty map would refuse mail nobody is sending.
            ok "No mailboxes on this server, so there is nothing to bind"
        fi
    else
        warn "Could not build the sender-login map. Mail is unaffected and the old behaviour stands; run: hitechcloudcli native-mail sync"
    fi
fi

# 2. Which edition this server is now.
key=$(awk -F= '/^\[LICENSE\]/{s=1;next} /^\[/{s=0} s&&/^[[:space:]]*key[[:space:]]*=/{gsub(/^[[:space:]]+|[[:space:]]+$/,"",$2);print $2;exit}' \
      /etc/hitechcloud/hitechcloud/conf/hitechcloud.config 2>/dev/null)
if [[ -n "$key" ]]; then
    ok "This server holds a licence key, so it is Enterprise - nothing changes for it"
else
    warn "This server has no licence key, so it is now Community: resellers are not available. Every hosting feature - accounts, websites, mail, DNS, databases - is unaffected."
fi

ok "2.1.27 update complete"
exit 0
