Browse Source

Fix handling of users and groups on install. (#14961)

* Fix handling of users and groups on install.

- Use `getent` when possible to check if user exists.
- When adding a user, adding a group, or adding a user to a group, only
  try the first supported command we find, instead of trying each one we
  find in sequence until one works.

* Update packaging/installer/functions.sh

Co-authored-by: Ilya Mashchenko <ilya@netdata.cloud>

---------

Co-authored-by: Ilya Mashchenko <ilya@netdata.cloud>
Austin S. Hemmelgarn 1 year ago
parent
commit
f056d0db5d
1 changed files with 19 additions and 40 deletions
  1. 19 40
      packaging/installer/functions.sh

+ 19 - 40
packaging/installer/functions.sh

@@ -918,32 +918,29 @@ portable_add_user() {
   [ -z "${homedir}" ] && homedir="/tmp"
 
   # Check if user exists
-  if cut -d ':' -f 1 < /etc/passwd | grep "^${username}$" 1> /dev/null 2>&1; then
-    echo >&2 "User '${username}' already exists."
-    return 0
+  if command -v getent > /dev/null 2>&1; then
+    if getent passwd "${username}" > /dev/null 2>&1; then
+        echo >&2 "User '${username}' already exists."
+        return 0
+    fi
+  else
+    if cut -d ':' -f 1 < /etc/passwd | grep "^${username}$" 1> /dev/null 2>&1; then
+        echo >&2 "User '${username}' already exists."
+        return 0
+    fi
   fi
 
   echo >&2 "Adding ${username} user account with home ${homedir} ..."
 
   nologin="$(command -v nologin || echo '/bin/false')"
 
-  # Linux
   if command -v useradd 1> /dev/null 2>&1; then
     run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0
-  fi
-
-  # FreeBSD
-  if command -v pw 1> /dev/null 2>&1; then
+  elif command -v pw 1> /dev/null 2>&1; then
     run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0
-  fi
-
-  # BusyBox
-  if command -v adduser 1> /dev/null 2>&1; then
+  elif command -v adduser 1> /dev/null 2>&1; then
     run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0
-  fi
-
-  # mac OS
-  if command -v sysadminctl 1> /dev/null 2>&1; then
+  elif command -v sysadminctl 1> /dev/null 2>&1; then
     run sysadminctl -addUser "${username}" && return 0
   fi
 
@@ -966,20 +963,11 @@ portable_add_group() {
   # Linux
   if command -v groupadd 1> /dev/null 2>&1; then
     run groupadd -r "${groupname}" && return 0
-  fi
-
-  # FreeBSD
-  if command -v pw 1> /dev/null 2>&1; then
+  elif command -v pw 1> /dev/null 2>&1; then
     run pw groupadd "${groupname}" && return 0
-  fi
-
-  # BusyBox
-  if command -v addgroup 1> /dev/null 2>&1; then
+  elif command -v addgroup 1> /dev/null 2>&1; then
     run addgroup "${groupname}" && return 0
-  fi
-
-  # mac OS
-  if command -v dseditgroup 1> /dev/null 2>&1; then
+  elif command -v dseditgroup 1> /dev/null 2>&1; then
     dseditgroup -o create "${groupname}" && return 0
   fi
 
@@ -1010,20 +998,11 @@ portable_add_user_to_group() {
     # Linux
     if command -v usermod 1> /dev/null 2>&1; then
       run usermod -a -G "${groupname}" "${username}" && return 0
-    fi
-
-    # FreeBSD
-    if command -v pw 1> /dev/null 2>&1; then
+    elif command -v pw 1> /dev/null 2>&1; then
       run pw groupmod "${groupname}" -m "${username}" && return 0
-    fi
-
-    # BusyBox
-    if command -v addgroup 1> /dev/null 2>&1; then
+    elif command -v addgroup 1> /dev/null 2>&1; then
       run addgroup "${username}" "${groupname}" && return 0
-    fi
-
-    # mac OS
-    if command -v dseditgroup 1> /dev/null 2>&1; then
+    elif command -v dseditgroup 1> /dev/null 2>&1; then
       dseditgroup -u "${username}" "${groupname}" && return 0
     fi