#!/bin/sh
# =============================================================================
# autopkgtest "users": self-test of the users module (temporary local users
# and groups, with an optional known clear-text password).
#
# Needs root.  The accounts are created for the test and removed again at
# teardown, also when the test fails.  Names are prefixed "fxt" and nothing
# that existed before is touched.
#
# HOW TO ADAPT THIS FILE
#  * Typical use: an unprivileged local user that runs the software under test
#        fx_load users
#        fx_user_add alice --password 'Sup3r-secret'
#        su -s /bin/sh -c 'sudo -S -l' alice <<<'Sup3r-secret'
#  * Call fx_user_add BEFORE starting daemons: teardown runs in reverse order,
#    so the daemons are stopped before userdel has to remove the account.
#  * Run from a checkout:  FX_LIBDIR=$PWD/lib sh debian/tests/users
# =============================================================================
set -eu

LIB="${FX_LIBDIR:-/usr/share/autopkgtest-helper}"
export FX_LIBDIR="$LIB"
. "$LIB/fixtures.sh"
fx_need_root
fx_init
fx_load users

T=$FX_ROOT/selftest
mkdir "$T"

# Pick names that are certainly unused; a testbed with such users is odd.
for n in fxtuser fxtother fxtgrp fxtnopw; do
    if getent passwd "$n" >/dev/null || getent group "$n" >/dev/null; then
        fx_skip "account or group '$n' exists on this testbed"
    fi
done

# The password deliberately contains characters that are special to the
# shell, to chpasswd (the colon) and to sed.
PW='p@ss:w0rd &|$x'

# ---------------------------------------------------------------------------
fx_step "fx_group_add, fx_user_add with password and supplementary group"
fx_group_add fxtgrp
getent group fxtgrp >/dev/null || fx_fail "group not created"
fx_user_add fxtuser --password "$PW" --groups fxtgrp --comment "Test User"
getent passwd fxtuser >"$T/passwd"
fx_assert_contains "$T/passwd" "fxtuser:" ":Test User:" ":/bin/sh"
# The default home is below FX_ROOT and belongs to the user.
home=$(getent passwd fxtuser | cut -d: -f6)
case $home in "$FX_ROOT"/*) ;; *) fx_fail "home $home not below FX_ROOT" ;; esac
[ -d "$home" ] || fx_fail "home directory not created"
[ "$(stat -c %U "$home")" = fxtuser ] || fx_fail "home not owned by the user"
# private primary group plus the supplementary one
id -Gn fxtuser >"$T/groups"
fx_assert_contains "$T/groups" fxtuser fxtgrp
# The account works: commands run as the user.
[ "$(su -s /bin/sh -c 'id -un' fxtuser)" = fxtuser ] || fx_fail "su to fxtuser failed"

# ---------------------------------------------------------------------------
fx_step "the clear-text password is the one in effect"
fx_user_pw_ok fxtuser "$PW" || fx_fail "correct password rejected"
# Negative checks, so that the positive one proves something.
fx_user_pw_ok fxtuser "${PW}x" && fx_fail "wrong password accepted"
fx_user_pw_ok fxtuser "" && fx_fail "empty password accepted"
fx_user_pw_ok fxtnosuch "$PW" && fx_fail "password accepted for unknown user"

# ---------------------------------------------------------------------------
fx_step "without --password the account is locked"
fx_user_add fxtnopw
fx_user_pw_ok fxtnopw "" && fx_fail "empty password works for fxtnopw"
fx_user_pw_ok fxtnopw "anything" && fx_fail "password works for fxtnopw"

# ---------------------------------------------------------------------------
fx_step "existing home directory is kept, existing accounts are not adopted"
mkdir "$T/oldhome"
echo precious >"$T/oldhome/file"
cat >"$T/keephome.sh" <<SUB
set -eu
. "$LIB/fixtures.sh"
fx_init
fx_load users
fx_user_add fxtother --home "$T/oldhome" --group fxtgrp
SUB
sh "$T/keephome.sh"
[ "$(cat "$T/oldhome/file")" = precious ] || fx_fail "pre-existing home was removed"
getent passwd fxtother >/dev/null && fx_fail "fxtother not removed"
# adopting root, an existing group, or a bad password must be refused
for bad in "fx_user_add root" "fx_group_add fxtgrp" \
           "fx_user_add fxtother --password ''" \
           "fx_user_add fxtother --password 'a
b'" \
           "fx_user_add fxtother --group no-such-group" \
           "fx_user_add fxtother --frobnicate"; do
    printf 'set -eu\n. "%s/fixtures.sh"\nfx_init\nfx_load users\n%s\n' "$LIB" "$bad" >"$T/bad.sh"
    rc=0
    sh "$T/bad.sh" >/dev/null 2>"$T/bad.err" || rc=$?
    [ "$rc" -eq 1 ] || fx_fail "'$bad' exit status $rc, expected 1"
    fx_assert_contains "$T/bad.err" "ERROR"
done
getent passwd root >/dev/null || fx_fail "root vanished"
getent group fxtgrp >/dev/null || fx_fail "fxtgrp was removed by a failed call"

# ---------------------------------------------------------------------------
fx_step "teardown after a failing test removes users, groups and homes"
# The child creates an account, starts a process owned by it (userdel refuses
# to remove a user with running processes, so the process must be stopped
# first) and then fails.  Afterwards nothing may be left behind and the exit
# status must be kept.  Escaped variables are expanded by the child.
cat >"$T/fail.sh" <<SUB
set -eu
. "$LIB/fixtures.sh"
fx_init
fx_load users
fx_user_add fxtother --password x
echo "\$FX_ROOT" >"$T/child.root"
getent passwd fxtother | cut -d: -f6 >"$T/child.home"
fx_start owned setpriv --reuid fxtother --regid fxtother --clear-groups sleep 300
cat "\$FX_RUNDIR/owned.pid" >"$T/child.pid"
exit 3
SUB
rc=0
sh "$T/fail.sh" >/dev/null 2>"$T/fail.err" || rc=$?
[ "$rc" -eq 3 ] || fx_fail "exit status $rc, expected 3 ($(cat "$T/fail.err"))"
fx_alive "$(cat "$T/child.pid")" && fx_fail "process of the user survived"
getent passwd fxtother >/dev/null && fx_fail "user left behind after failed test"
getent group fxtother >/dev/null && fx_fail "private group left behind"
[ ! -e "$(cat "$T/child.home")" ] || fx_fail "home of removed user left behind"
[ ! -e "$(cat "$T/child.root")" ] || fx_fail "child FX_ROOT not removed"

echo "users: all tests passed"
