void-packages/srcpkgs/xbps-triggers/files/system-accounts

156 lines
3.4 KiB
Bash
Executable File

#!/bin/sh
#
# (Un)registers systems accounts (users/groups).
#
# Arguments: $ACTION = [run/targets]
# $TARGET = [post-install/pre-remove]
# $PKGNAME
# $VERSION
# $UPDATE = [yes/no]
#
ACTION="$1"
TARGET="$2"
PKGNAME="$3"
VERSION="$4"
UPDATE="$5"
USERADD=usr/sbin/useradd
USERDEL=usr/sbin/userdel
GROUPADD=usr/sbin/groupadd
GROUPDEL=usr/sbin/groupdel
PASSWD=usr/bin/passwd
GETENT=usr/bin/getent
group_add() {
local _grname _gid use_gid
_grname="${1%:*}"
_gid="${1#*:}"
if [ "${_gid}" != "${_grname}" ]; then
use_gid="gid ${_gid}"
fi
if ! $GETENT group ${_grname} >/dev/null; then
if [ -n "$use_gid" ]; then
$GROUPADD -r ${_grname} -g ${_gid} >/dev/null 2>&1
else
$GROUPADD -r ${_grname} >/dev/null 2>&1
fi
if [ $? -eq 0 ]; then
echo "Created ${_grname} ($use_gid) system group."
else
echo "Failed to create ${_grname} ($use_gid) system group!"
exit 1
fi
fi
}
case "$ACTION" in
targets)
echo "post-install pre-remove"
;;
run)
if [ ! -x $USERADD -a ! -x $GROUPADD -a ! -x $PASSWD -a ! -x $GETENT ]; then
exit 0
fi
if [ -z "$system_accounts" -a -z "$system_groups" ]; then
exit 0
fi
HOST_ARCH=$(uname -m)
if [ -n "$XBPS_TARGET_ARCH" -a "$XBPS_TARGET_ARCH" != "$HOST_ARCH" ]; then
USERADD=useradd
USERDEL=userdel
GROUPADD=groupadd
GROUPDEL=groupdel
PASSWD=passwd
GETENT=getent
fi
case "$TARGET" in
post-install)
# System groups required by a package.
for grp in ${system_groups}; do
if [ ! -x "$GROUPADD" ]; then
echo "WARNING: cannot create ${grp} system group (missing groupadd)"
continue
fi
group_add $grp
done
# System user/group required by a package.
for acct in ${system_accounts}; do
_uname="${acct%:*}"
_uid="${acct#*:}"
[ "${_uid}" != "${_uname}" ] && use_id="-u ${_uid} -g ${_uid}"
if [ ! -x "$USERADD" ]; then
echo "WARNING: cannot create ${acct} system user/group (missing useradd)"
continue
fi
eval homedir="\$${_uname}_homedir"
eval shell="\$${_uname}_shell"
eval descr="\$${_uname}_descr"
eval groups="\$${_uname}_groups"
[ -z "$homedir" ] && homedir="/"
[ -z "$shell" ] && shell="/sbin/nologin"
[ -z "$descr" ] && descr="${_uname} unpriviledged user"
[ -n "$groups" ] && user_groups="-G $groups"
group_add ${acct}
if ! $GETENT passwd ${_uname} >/dev/null; then
$USERADD -c "$descr" -d "$homedir" -s "$shell" ${user_groups} ${use_id:=-g ${_uname}} -r ${_uname} && \
$PASSWD -l ${_uname} >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Created ${_uname} (${_uid}) system user."
else
echo "Failed to create ${acct} system user!"
exit 1
fi
fi
done
;;
pre-remove)
#
# Only unregister if we aren't updating a package.
#
if [ "$UPDATE" = "no" ]; then
for acct in ${system_accounts}; do
_uname="${acct%:*}"
_uid="${acct#*:}"
if [ ! -x "$USERDEL" ]; then
echo "WARNING: cannot remove ${acct} system user/group (missing userdel)"
continue
fi
$USERDEL ${_uname} >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Removed ${_uname} (${_uid}) system user/group."
fi
done
for grp in ${system_groups}; do
_uname="${acct%:*}"
_uid="${acct#*:}"
if [ ! -x "$GROUPDEL" ]; then
echo "WARNING: cannot remove ${acct} system group (missing groupdel)"
continue
fi
$GROUPDEL ${_uname} >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Removed ${_uname} (${_uid}) system group."
fi
done
fi
;;
esac
;;
*)
exit 1
;;
esac
exit 0