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