-echo >&2 "Fixing permissions ..."
-
-check_cmd() {
- which "${1}" >/dev/null 2>&1 && return 0
- command -v "${1}" >/dev/null 2>&1 && return 0
- return 1
-}
-
-portable_add_user() {
- local username="${1}"
-
- getent passwd "${username}" > /dev/null 2>&1
- [ $? -eq 0 ] && return 0
-
- echo >&2 "Adding ${username} user account ..."
-
- local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')"
-
- # Linux
- if check_cmd useradd
- then
- run useradd -r -g "${username}" -c "${username}" -s "${nologin}" -d / "${username}" && return 0
- fi
-
- # FreeBSD
- if check_cmd pw
- then
- run pw useradd "${username}" -d / -g "${username}" -s "${nologin}" && return 0
- fi
-
- # BusyBox
- if check_cmd adduser
- then
- run adduser -D -G "${username}" "${username}" && return 0
- fi
-
- echo >&2 "Failed to add ${username} user account !"
-
- return 1
-}
-
-portable_add_group() {
- local groupname="${1}"
-
- getent group "${groupname}" > /dev/null 2>&1
- [ $? -eq 0 ] && return 0
-
- echo >&2 "Adding ${groupname} user group ..."
-
- # Linux
- if check_cmd groupadd
- then
- run groupadd -r "${groupname}" && return 0
- fi
-
- # FreeBSD
- if check_cmd pw
- then
- run pw groupadd "${groupname}" && return 0
- fi
-
- # BusyBox
- if check_cmd addgroup
- then
- run addgroup "${groupname}" && return 0
- fi
-
- echo >&2 "Failed to add ${groupname} user group !"
- return 1
-}
-
-portable_add_user_to_group() {
- local groupname="${1}" username="${2}"
-
- getent group "${groupname}" > /dev/null 2>&1
- [ $? -ne 0 ] && return 1
-
- # find the user is already in the group
- local users=$(getent group "${groupname}" | cut -d ':' -f 4)
- if [[ ",${users}," =~ ,${username}, ]]
- then
- # username is already there
- return 0
- else
- # username is not in group
- echo >&2 "Adding ${username} user to the ${groupname} group ..."
-
- # Linux
- if check_cmd usermod
- then
- run usermod -a -G "${groupname}" "${username}" && return 0
- fi
-
- # FreeBSD
- if check_cmd pw
- then
- run pw groupmod "${groupname}" -m "${username}" && return 0
- fi
-
- # BusyBox
- if check_cmd addgroup
- then
- run addgroup "${username}" "${groupname}" && return 0
- fi
-
- echo >&2 "Failed to add user ${username} to group ${groupname} !"
- return 1
- fi
-}
-
-iscontainer() {
- # man systemd-detect-virt
- local cmd=$(which systemd-detect-virt 2>/dev/null || command -v systemd-detect-virt 2>/dev/null)
- if [ ! -z "${cmd}" -a -x "${cmd}" ]
- then
- "${cmd}" --container >/dev/null 2>&1 && return 0
- fi
-
- # /proc/1/sched exposes the host's pid of our init !
- # http://stackoverflow.com/a/37016302
- local pid=$( cat /proc/1/sched | head -n 1 | { IFS='(),#:' read name pid th threads; echo $pid; } )
- local p=$(( pid + 0 ))
- [ ${pid} -ne 1 ] && return 0