daandemeyer / rpms / systemd

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