388cd91
#!/bin/bash -eu
388cd91
388cd91
if [ $UID -ne 0 ]; then
388cd91
    echo "WARNING: This script needs to run as root to be effective"
388cd91
    exit 1
388cd91
fi
388cd91
388cd91
export SYSTEMD_NSS_BYPASS_SYNTHETIC=1
388cd91
388cd91
if [ "${1:-}" = "--ignore-journal" ]; then
388cd91
    shift
388cd91
    ignore_journal=1
388cd91
else
388cd91
    ignore_journal=0
388cd91
fi
388cd91
388cd91
echo "Checking processes..."
388cd91
if ps h -u 99 | grep .; then
388cd91
    echo "ERROR: ps reports processes with UID 99!"
388cd91
    exit 2
388cd91
fi
388cd91
echo "... not found"
388cd91
388cd91
echo "Checking UTMP..."
388cd91
if w -h 199 | grep . ; then
388cd91
    echo "ERROR: w reports UID 99 as active!"
388cd91
    exit 2
388cd91
fi
388cd91
if w -h nobody | grep . ; then
388cd91
    echo "ERROR: w reports user nobody as active!"
388cd91
    exit 2
388cd91
fi
388cd91
echo "... not found"
388cd91
388cd91
echo "Checking the journal..."
388cd91
if [ "$ignore_journal" = 0 ] && journalctl -q -b -n10 _UID=99 | grep . ; then
388cd91
    echo "ERROR: journalctl reports messages from UID 99 in current boot!"
388cd91
    exit 2
388cd91
fi
388cd91
echo "... not found"
388cd91
388cd91
echo "Looking for files in /etc, /run, /tmp, and /var..."
388cd91
if find /etc /run /tmp /var -uid 99 -print | grep -m 10 . ; then
388cd91
    echo "ERROR: found files belonging to UID 99"
388cd91
    exit 2
388cd91
fi
388cd91
echo "... not found"
388cd91
388cd91
echo "Checking if nobody is defined correctly..."
388cd91
if getent passwd nobody |
388cd91
	grep '^nobody:[x*]:65534:65534:.*:/:/sbin/nologin';
388cd91
then
388cd91
    echo "OK, nothing to do."
388cd91
    exit 0
388cd91
else
388cd91
    echo "NOTICE: User nobody is not defined correctly"
388cd91
fi
388cd91
388cd91
echo "Checking if nfsnobody or something else is using the uid..."
388cd91
if getent passwd 65534 | grep . ; then
388cd91
    echo "NOTICE: will have to remove this user"
388cd91
else
388cd91
    echo "... not found"
388cd91
fi
388cd91
388cd91
if [ "${1:-}" = "-x" ]; then
388cd91
    if getent passwd nobody >/dev/null; then
388cd91
	# this will remove both the user and the group.
388cd91
	( set -x
388cd91
   	  userdel nobody
388cd91
	)
388cd91
    fi
388cd91
388cd91
    if getent passwd 65534 >/dev/null; then
388cd91
	# Make sure the uid is unused. This should free gid too.
388cd91
	name="$(getent passwd 65534 | cut -d: -f1)"
388cd91
	( set -x
388cd91
	  userdel "$name"
388cd91
	)
388cd91
    fi
388cd91
388cd91
    if grep -qE '^(passwd|group):.*\bsss\b' /etc/nsswitch.conf; then
388cd91
	echo "Sleeping, so sss can catch up"
388cd91
	sleep 3
388cd91
    fi
388cd91
388cd91
    if getent group 65534; then
388cd91
	# Make sure the gid is unused, even if uid wasn't.
388cd91
	name="$(getent group 65534 | cut -d: -f1)"
388cd91
	( set -x
388cd91
	  groupdel "$name"
388cd91
	)
388cd91
    fi
388cd91
388cd91
    # systemd-sysusers uses the same gid and uid
388cd91
    ( set -x
388cd91
      systemd-sysusers --inline 'u nobody 65534 "Kernel Overflow User" / /sbin/nologin'
388cd91
    )
388cd91
else
388cd91
    echo "Pass '-x' to perform changes"
388cd91
fi