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