Use "getent group" instead of reading "/etc/group" to get group information (#14316)
* Use getent group instead of /etc/group to search groups * Fallback to 'cat /etc/groups' if no getent exists * Use group_exists() in netdata-installer.sh * Rename group_exists() to get_group()
Dim-P committed
Jan 27, 2023 at 15:51 UTC
081dbc6cedbc7c035d8e3bc7bcb1845db656bacf
3 files changed
+14
-6
netdata-installer.sh
+2
-2
@@ -1200,8 +1200,8 @@ run chmod 770 "${NETDATA_CLAIMING_DIR}"
1200
if [ "$(id -u)" -eq 0 ]; then
1201
# find the admin group
1202
admin_group=
1203
- test -z "${admin_group}" && getent group root > /dev/null 2>&1 && admin_group="root"
1204
- test -z "${admin_group}" && getent group daemon > /dev/null 2>&1 && admin_group="daemon"
1203
+ test -z "${admin_group}" && get_group root > /dev/null 2>&1 && admin_group="root"
1204
+ test -z "${admin_group}" && get_group daemon > /dev/null 2>&1 && admin_group="daemon"
1205
test -z "${admin_group}" && admin_group="${NETDATA_GROUP}"
1206
1207
run chown "${NETDATA_USER}:${admin_group}" "${NETDATA_LOG_DIR}"
packaging/installer/functions.sh
+11
-3
@@ -396,6 +396,14 @@ get_os_key() {
396
fi
397
}
398
399
+get_group(){
400
+ if command -v getent > /dev/null 2>&1; then
401
+ getent group "${1:-""}"
402
+ else
403
+ cat /etc/group | grep "^${1}:"
404
+ fi
405
+}
406
+
407
issystemd() {
408
pids=''
409
p=''
@@ -933,7 +941,7 @@ portable_add_group() {
941
groupname="${1}"
942
943
# Check if group exist
936
- if cut -d ':' -f 1 < /etc/group | grep "^${groupname}$" 1> /dev/null 2>&1; then
944
+ if get_group "${groupname}" > /dev/null 2>&1; then
945
echo >&2 "Group '${groupname}' already exists."
946
return 0
947
fi
@@ -969,14 +977,14 @@ portable_add_user_to_group() {
977
username="${2}"
978
979
# Check if group exist
972
- if ! cut -d ':' -f 1 < /etc/group | grep "^${groupname}$" > /dev/null 2>&1; then
980
+ if ! get_group "${groupname}" > /dev/null 2>&1; then
981
echo >&2 "Group '${groupname}' does not exist."
982
# Don’t treat this as a failure, if the group does not exist we should not be trying to add the user to it.
983
return 0
984
fi
985
986
# Check if user is in group
979
- if expr ",$(grep "^${groupname}:" < /etc/group | cut -d ':' -f 4)," : ",""${username}"","; then
987
+ if get_group "${groupname}" | cut -d ':' -f 4 | grep -wq "${username}"; then
988
# username is already there
989
echo >&2 "User '${username}' is already in group '${groupname}'."
990
return 0
packaging/installer/netdata-uninstaller.sh
+1
-1
@@ -426,7 +426,7 @@ portable_del_group() {
426
427
# Linux
428
if command -v groupdel 1> /dev/null 2>&1; then
429
- if grep -q "${groupname}" /etc/group; then
429
+ if get_group "${groupname}" > /dev/null 2>&1; then
430
run groupdel "${groupname}" && return 0
431
else
432
info "Group ${groupname} already removed in a previous step."