2013-03-27 08:34:52 +00:00
|
|
|
#!/bin/sh
|
2010-11-04 12:53:46 +00:00
|
|
|
#
|
|
|
|
# (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"
|
|
|
|
|
2010-12-28 10:22:58 +00:00
|
|
|
USERADD=usr/sbin/useradd
|
|
|
|
USERDEL=usr/sbin/userdel
|
|
|
|
GROUPADD=usr/sbin/groupadd
|
|
|
|
GROUPDEL=usr/sbin/groupdel
|
|
|
|
PASSWD=usr/bin/passwd
|
|
|
|
GETENT=usr/bin/getent
|
|
|
|
|
2013-03-27 08:34:52 +00:00
|
|
|
group_add() {
|
|
|
|
local _grname _gid use_gid
|
|
|
|
|
|
|
|
_grname="${1%:*}"
|
|
|
|
_gid="${1#*:}"
|
|
|
|
|
|
|
|
if [ "${_gid}" != "${_grname}" ]; then
|
|
|
|
use_gid="gid ${_gid}"
|
|
|
|
fi
|
2010-12-28 10:22:58 +00:00
|
|
|
|
2013-03-27 08:34:52 +00:00
|
|
|
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
|
2010-12-28 10:22:58 +00:00
|
|
|
if [ $? -eq 0 ]; then
|
2013-04-06 12:54:21 +00:00
|
|
|
echo "Created ${_grname} ($use_gid) system group."
|
2010-12-28 10:22:58 +00:00
|
|
|
else
|
2013-04-06 12:54:21 +00:00
|
|
|
echo "Failed to create ${_grname} ($use_gid) system group!"
|
2010-12-28 10:22:58 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
2010-11-04 12:53:46 +00:00
|
|
|
|
|
|
|
case "$ACTION" in
|
|
|
|
targets)
|
|
|
|
echo "post-install pre-remove"
|
|
|
|
;;
|
|
|
|
run)
|
2010-12-28 10:22:58 +00:00
|
|
|
if [ ! -x $USERADD -a ! -x $GROUPADD -a ! -x $PASSWD -a ! -x $GETENT ]; then
|
2010-11-04 12:53:46 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2010-12-28 10:22:58 +00:00
|
|
|
if [ -z "$system_accounts" -a -z "$system_groups" ]; then
|
2010-11-04 12:53:46 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2013-02-25 18:13:44 +00:00
|
|
|
HOST_ARCH=$(uname -m)
|
|
|
|
|
2013-04-06 13:01:42 +00:00
|
|
|
if [ -n "$XBPS_TARGET_ARCH" -a "$XBPS_TARGET_ARCH" != "$HOST_ARCH" ]; then
|
2013-02-25 18:13:44 +00:00
|
|
|
USERADD=useradd
|
|
|
|
USERDEL=userdel
|
|
|
|
GROUPADD=groupadd
|
|
|
|
GROUPDEL=groupdel
|
|
|
|
PASSWD=passwd
|
|
|
|
GETENT=getent
|
|
|
|
fi
|
|
|
|
|
2010-11-04 12:53:46 +00:00
|
|
|
case "$TARGET" in
|
|
|
|
post-install)
|
2010-12-28 12:03:04 +00:00
|
|
|
# System groups required by a package.
|
|
|
|
for grp in ${system_groups}; do
|
2013-04-06 12:54:21 +00:00
|
|
|
if [ ! -x "$GROUPADD" ]; then
|
|
|
|
echo "WARNING: cannot create ${grp} system group (missing groupadd)"
|
|
|
|
continue
|
|
|
|
fi
|
2010-12-28 12:03:04 +00:00
|
|
|
group_add $grp
|
|
|
|
done
|
|
|
|
|
2010-12-28 10:22:58 +00:00
|
|
|
# System user/group required by a package.
|
2010-11-04 12:53:46 +00:00
|
|
|
for acct in ${system_accounts}; do
|
2013-04-06 12:54:21 +00:00
|
|
|
if [ ! -x "$USERADD" ]; then
|
|
|
|
echo "WARNING: cannot create ${acct} system user/group (missing useradd)"
|
|
|
|
continue
|
|
|
|
fi
|
2010-11-04 12:53:46 +00:00
|
|
|
eval homedir="\$${acct}_homedir"
|
|
|
|
eval shell="\$${acct}_shell"
|
|
|
|
eval descr="\$${acct}_descr"
|
|
|
|
eval groups="\$${acct}_groups"
|
|
|
|
[ -z "$homedir" ] && homedir="/"
|
|
|
|
[ -z "$shell" ] && shell="/sbin/nologin"
|
|
|
|
[ -z "$descr" ] && descr="$acct unpriviledged user"
|
2010-12-28 12:03:04 +00:00
|
|
|
[ -n "$groups" ] && user_groups="-G $groups"
|
2010-11-04 12:53:46 +00:00
|
|
|
|
2010-12-28 12:03:04 +00:00
|
|
|
group_add $acct
|
2010-12-28 10:22:58 +00:00
|
|
|
|
|
|
|
if ! $GETENT passwd ${acct} >/dev/null; then
|
|
|
|
$USERADD -c "$descr" -d "$homedir" \
|
2010-12-28 12:03:04 +00:00
|
|
|
-s "$shell" -g ${acct} $user_groups \
|
2010-11-04 12:53:46 +00:00
|
|
|
-r ${acct} && \
|
2010-12-28 10:22:58 +00:00
|
|
|
$PASSWD -l ${acct} >/dev/null 2>&1
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo "Created ${acct} system user."
|
|
|
|
else
|
|
|
|
echo "Failed to create ${acct} system user!"
|
|
|
|
exit 1
|
|
|
|
fi
|
2010-11-04 12:53:46 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
pre-remove)
|
|
|
|
#
|
|
|
|
# Only unregister if we aren't updating a package.
|
|
|
|
#
|
|
|
|
if [ "$UPDATE" = "no" ]; then
|
|
|
|
for acct in ${system_accounts}; do
|
2013-04-06 12:54:21 +00:00
|
|
|
if [ ! -x "$USERDEL" ]; then
|
|
|
|
echo "WARNING: cannot remove ${acct} system user/group (missing userdel)"
|
|
|
|
continue
|
|
|
|
fi
|
2010-12-28 10:22:58 +00:00
|
|
|
$USERDEL ${acct} >/dev/null 2>&1
|
2010-11-04 12:53:46 +00:00
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo "Removed ${acct} system user/group."
|
|
|
|
fi
|
|
|
|
done
|
2010-12-28 10:22:58 +00:00
|
|
|
for grp in ${system_groups}; do
|
2013-04-06 12:54:21 +00:00
|
|
|
if [ ! -x "$GROUPDEL" ]; then
|
|
|
|
echo "WARNING: cannot remove ${acct} system group (missing groupdel)"
|
|
|
|
continue
|
|
|
|
fi
|
2010-12-28 10:22:58 +00:00
|
|
|
$GROUPDEL ${grp} >/dev/null 2>&1
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo "Removed ${grp} system group."
|
|
|
|
fi
|
|
|
|
done
|
2010-11-04 12:53:46 +00:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
exit 0
|