#!/bin/sh
# preinst script for salt-common.
#
# See: dh_installdeb(1).

set -e

# Summary of how this script can be called:
#        * <new-preinst> 'install'
#        * <new-preinst> 'install' <old-version>
#        * <new-preinst> 'upgrade' <old-version>
#        * <old-preinst> 'abort-upgrade' <new-version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package.

# Source setup configuration if present. The DEB-conventional location is
# /etc/default/salt-setup; /etc/sysconfig/salt-minion-setup is honored as
# well for cross-distro parity with the RPM packaging. Either file may
# pre-set SALT_HOME, SALT_USER, SALT_GROUP, SALT_NAME, SALT_SHELL, or
# SALT_EXTRAS_DIR. Values in the sourced files win over inherited env
# vars; the [ -n ... ] || guards below preserve whatever was set,
# falling back to the hardcoded defaults only when nothing was supplied.
if [ -f /etc/default/salt-setup ]; then
    . /etc/default/salt-setup
fi
if [ -f /etc/sysconfig/salt-minion-setup ]; then
    . /etc/sysconfig/salt-minion-setup
fi

# SALT_ONEDIR_HARDEN=1 opts in to relocating the salt user's writable
# state out of /opt/saltstack/salt into per-daemon
# /var/lib/salt/<daemon>/. On 3006.x the default is unset (legacy
# blanket-chown of /opt/saltstack/salt preserved); operators opt in
# explicitly to get the hardened layout early on LTS. The default flips
# to hardened on 3009.0. See issue #70198.

[ -n "$SALT_USER" ] || SALT_USER=salt
[ -n "$SALT_NAME" ] || SALT_NAME="Salt"
[ -n "$SALT_GROUP" ] || SALT_GROUP=salt
[ -n "$SALT_SHELL" ] || SALT_SHELL=/usr/sbin/nologin

# SALT_HOME default depends on SALT_ONEDIR_HARDEN. In hardened mode the
# salt user's account home moves under /var/lib/salt so
# /opt/saltstack/salt can stay root-owned and 0755. The per-daemon
# preinst scripts may reassign SALT_HOME to /var/lib/salt/<daemon>/home
# so whichever daemon's preinst runs last owns the passwd home entry;
# this is intentional -- see issue #70198.
if [ -z "$SALT_HOME" ]; then
    if [ "$SALT_ONEDIR_HARDEN" = "1" ]; then
        SALT_HOME=/var/lib/salt/home
    else
        SALT_HOME=/opt/saltstack/salt
    fi
fi

case "$1" in
  install|upgrade)
    # create user to avoid running server as root
    # 1. create group if not existing
    if ! getent group | grep -q "^$SALT_GROUP:" ; then
       echo -n "Adding group $SALT_GROUP.."
       groupadd --system $SALT_GROUP 2>/dev/null ||true
       echo "..done"
    fi
    # 2. create homedir if not existing
    test -d $SALT_HOME || mkdir -p $SALT_HOME
    # 3. create user if not existing
    if ! getent passwd | grep -q "^$SALT_USER:"; then
      echo -n "Adding system user $SALT_USER.."
      useradd --system \
              --no-create-home \
              -s $SALT_SHELL \
              -g $SALT_GROUP \
              $SALT_USER 2>/dev/null || true
      echo "..done"
    fi
    # 4. adjust passwd entry
    usermod -c "$SALT_NAME" \
            -d $SALT_HOME   \
            -g $SALT_GROUP  \
             $SALT_USER

    # Remove incorrectly installed logrotate config - issue 65231
    test -d /etc/logrotate.d/salt && rm -r /etc/logrotate.d/salt || /bin/true

  ;;

  abort-upgrade)
  ;;

  *)
    echo "preinst called with unknown argument '$1'" >&2
    exit 1
  ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.



exit 0
