diff options
author | Costa Tsaousis (ktsaou) <costa@tsaousis.gr> | 2017-04-02 19:46:33 +0300 |
---|---|---|
committer | Costa Tsaousis (ktsaou) <costa@tsaousis.gr> | 2017-04-02 19:46:33 +0300 |
commit | a7ec7a43a51eed9c3e137a89b36f206331c21e69 (patch) | |
tree | 8b6f930dd07cf30b0f5fc63ae65f8ebedf5ff1b4 /installer | |
parent | 44f2478a962de1df7b627716a1d5b581cf8bf43f (diff) |
workaround about getent not being available
Diffstat (limited to 'installer')
-rw-r--r-- | installer/functions.sh | 52 |
1 files changed, 47 insertions, 5 deletions
diff --git a/installer/functions.sh b/installer/functions.sh index b0e01b0eeb..b590e19e99 100644 --- a/installer/functions.sh +++ b/installer/functions.sh @@ -178,10 +178,53 @@ run() { return ${ret} } +getent_cmd="$(which_cmd getent)" +portable_check_user_exists() { + local username="${1}" found= + + if [ ! -z "${getent_cmd}" ] + then + "${getent_cmd}" passwd "${username}" >/dev/null 2>&1 + return $? + fi + + found="$(cut -d ':' -f 1 </etc/passwd | grep "^${username}$")" + [ "${found}" = "${username}" ] && return 0 + return 1 +} + +portable_check_group_exists() { + local groupname="${1}" found= + + if [ ! -z "${getent_cmd}" ] + then + "${getent_cmd}" group "${groupname}" >/dev/null 2>&1 + return $? + fi + + found="$(cut -d ':' -f 1 </etc/group | grep "^${groupname}$")" + [ "${found}" = "${groupname}" ] && return 0 + return 1 +} + +portable_check_user_in_group() { + local username="${1}" groupname="${2}" users= + + if [ ! -z "${getent_cmd}" ] + then + users="$(getent group "${groupname}" | cut -d ':' -f 4)" + else + users="$(grep "^${groupname}:" </etc/group | cut -d ':' -f 4)" + fi + + [[ ",${users}," =~ ,${username}, ]] && return 0 + return 1 +} + portable_add_user() { local username="${1}" - getent passwd "${username}" > /dev/null 2>&1 + portable_check_user_exists "${username}" [ $? -eq 0 ] && echo >&2 "User '${username}' already exists." && return 0 echo >&2 "Adding ${username} user account ..." @@ -214,7 +257,7 @@ portable_add_user() { portable_add_group() { local groupname="${1}" - getent group "${groupname}" > /dev/null 2>&1 + portable_check_group_exists "${groupname}" [ $? -eq 0 ] && echo >&2 "Group '${groupname}' already exists." && return 0 echo >&2 "Adding ${groupname} user group ..." @@ -244,12 +287,11 @@ portable_add_group() { portable_add_user_to_group() { local groupname="${1}" username="${2}" - getent group "${groupname}" > /dev/null 2>&1 + portable_check_group_exists "${groupname}" [ $? -ne 0 ] && echo >&2 "Group '${groupname}' does not exist." && return 1 # find the user is already in the group - local users=$(getent group "${groupname}" | cut -d ':' -f 4) - if [[ ",${users}," =~ ,${username}, ]] + if portable_check_user_in_group "${username}" "${groupname}" then # username is already there echo >&2 "User '${username}' is already in group '${groupname}'." |