From c9e9c8b980d3a5ba4abfe7c1b069f684a56be6d2 Mon Sep 17 00:00:00 2001 From: David Coppit Date: Sat, 26 Aug 2017 12:03:05 -0400 Subject: [PATCH] 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. --- runas.sh | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/runas.sh b/runas.sh index 9aa9898..39bcb55 100755 --- a/runas.sh +++ b/runas.sh @@ -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