valeriyvdovin / rpms / systemd

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