Don't create a user if one with user ID exists

Programs that do things like getting the home dir end up getting the home dir of the other user by accident.

Use an existing user if the user ID and group ID match. Error out if the group ID matches, or if there are multiple
users with the same user ID.
This commit is contained in:
David Coppit
2017-08-26 12:03:05 -04:00
parent 502596a8da
commit c9e9c8b980

View File

@@ -42,9 +42,37 @@ function create_user {
USER="user_${USER_ID}_$GROUP_ID"
GROUP="group_${USER_ID}_$GROUP_ID"
if grep -q '^[^:]*:[^:]*:99:100:' /etc/passwd >/dev/null 2>&1
then
USER=$(grep '^[^:]*:[^:]*:99:100:' /etc/passwd | sed 's/:.*//')
if [[ $USER == *$'\n'* ]]
then
echo "$(ts) ERROR: Found multiple users with the proper user ID and group ID. Exiting..."
exit 1
fi
echo "$(ts) Found existing user \"$USER\" with the proper user ID and group ID. Skipping creation of user and group..."
return
fi
if grep -q '^[^:]*:[^:]*:99:' /etc/passwd >/dev/null 2>&1
then
USER=$(grep '^[^:]*:[^:]*:99:100:' /etc/passwd | sed 's/:.*//')
if [[ $USER == *$'\n'* ]]
then
echo "$(ts) ERROR: Found multiple users with the proper user ID and incorrect group ID. Refusing to modify the group ID. Exiting..."
else
echo "$(ts) ERROR: Found user \"$USER\" with the proper user ID but incorrect group ID. Refusing to modify the group ID. Exiting..."
fi
exit 1
fi
if id -u $USER >/dev/null 2>&1
then
echo "$(ts) User \"$USER\" already exists. Skipping creation of user and group..."
echo "$(ts) User \"$USER\" already exists. Skipping creation of new user and group..."
return
fi