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 committed
Apr 25, 2023 at 14:29 UTC
f056d0db5d0a3270d681086ff714c477b1a4ed58
1 file changed
+19
-40
packaging/installer/functions.sh
+19
-40
@@ -918,32 +918,29 @@ portable_add_user() {
918
[ -z "${homedir}" ] && homedir="/tmp"
919
920
# Check if user exists
921
- if cut -d ':' -f 1 < /etc/passwd | grep "^${username}$" 1> /dev/null 2>&1; then
922
- echo >&2 "User '${username}' already exists."
923
- return 0
921
+ if command -v getent > /dev/null 2>&1; then
922
+ if getent passwd "${username}" > /dev/null 2>&1; then
923
+ echo >&2 "User '${username}' already exists."
924
+ return 0
925
+ fi
926
+ else
927
+ if cut -d ':' -f 1 < /etc/passwd | grep "^${username}$" 1> /dev/null 2>&1; then
928
+ echo >&2 "User '${username}' already exists."
929
+ return 0
930
+ fi
931
fi
932
933
echo >&2 "Adding ${username} user account with home ${homedir} ..."
934
935
nologin="$(command -v nologin || echo '/bin/false')"
936
930
- # Linux
937
if command -v useradd 1> /dev/null 2>&1; then
938
run useradd -r -g "${username}" -c "${username}" -s "${nologin}" --no-create-home -d "${homedir}" "${username}" && return 0
933
- fi
934
-
935
- # FreeBSD
936
- if command -v pw 1> /dev/null 2>&1; then
939
+ elif command -v pw 1> /dev/null 2>&1; then
940
run pw useradd "${username}" -d "${homedir}" -g "${username}" -s "${nologin}" && return 0
938
- fi
939
-
940
- # BusyBox
941
- if command -v adduser 1> /dev/null 2>&1; then
941
+ elif command -v adduser 1> /dev/null 2>&1; then
942
run adduser -h "${homedir}" -s "${nologin}" -D -G "${username}" "${username}" && return 0
943
- fi
944
-
945
- # mac OS
946
- if command -v sysadminctl 1> /dev/null 2>&1; then
943
+ elif command -v sysadminctl 1> /dev/null 2>&1; then
944
run sysadminctl -addUser "${username}" && return 0
945
fi
946
@@ -966,20 +963,11 @@ portable_add_group() {
963
# Linux
964
if command -v groupadd 1> /dev/null 2>&1; then
965
run groupadd -r "${groupname}" && return 0
969
- fi
970
-
971
- # FreeBSD
972
- if command -v pw 1> /dev/null 2>&1; then
966
+ elif command -v pw 1> /dev/null 2>&1; then
967
run pw groupadd "${groupname}" && return 0
974
- fi
975
-
976
- # BusyBox
977
- if command -v addgroup 1> /dev/null 2>&1; then
968
+ elif command -v addgroup 1> /dev/null 2>&1; then
969
run addgroup "${groupname}" && return 0
979
- fi
980
-
981
- # mac OS
982
- if command -v dseditgroup 1> /dev/null 2>&1; then
970
+ elif command -v dseditgroup 1> /dev/null 2>&1; then
971
dseditgroup -o create "${groupname}" && return 0
972
fi
973
@@ -1010,20 +998,11 @@ portable_add_user_to_group() {
998
# Linux
999
if command -v usermod 1> /dev/null 2>&1; then
1000
run usermod -a -G "${groupname}" "${username}" && return 0
1013
- fi
1014
-
1015
- # FreeBSD
1016
- if command -v pw 1> /dev/null 2>&1; then
1001
+ elif command -v pw 1> /dev/null 2>&1; then
1002
run pw groupmod "${groupname}" -m "${username}" && return 0
1018
- fi
1019
-
1020
- # BusyBox
1021
- if command -v addgroup 1> /dev/null 2>&1; then
1003
+ elif command -v addgroup 1> /dev/null 2>&1; then
1004
run addgroup "${username}" "${groupname}" && return 0
1023
- fi
1024
-
1025
- # mac OS
1026
- if command -v dseditgroup 1> /dev/null 2>&1; then
1005
+ elif command -v dseditgroup 1> /dev/null 2>&1; then
1006
dseditgroup -u "${username}" "${groupname}" && return 0
1007
fi
1008