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