__user_groups: refactor, support FreeBSD

This commit is contained in:
Andres Erbsen 2016-09-11 22:23:06 -04:00
parent 7329b528f6
commit 493c8d61f4
2 changed files with 23 additions and 21 deletions

View file

@ -22,7 +22,8 @@ os="$($__explorer/os)"
if [ "$os" = "netbsd" ]; then if [ "$os" = "netbsd" ]; then
echo netbsd echo netbsd
exit elif [ "$os" = "freebsd" ]; then
fi echo freebsd
else
usermod --help | grep -q -- '-A group' && echo true || echo false usermod --help | grep -q -- '-A group' && echo true || echo false
fi

View file

@ -23,19 +23,6 @@ state_should="$(cat "$__object/parameter/state")"
oldusermod="$(cat "$__object/explorer/oldusermod")" oldusermod="$(cat "$__object/explorer/oldusermod")"
os=$(cat "$__global/explorer/os") os=$(cat "$__global/explorer/os")
if [ "$os" = "netbsd" ]; then
# NetBSD does not have a command to remove a user from a group
oldusermod="true"
addparam="-G"
delparam=";;#"
elif [ "$oldusermod" = "true" ]; then
addparam="-A"
delparam="-R"
else
addparam="-a"
delparam="-d"
fi
mkdir "$__object/files" mkdir "$__object/files"
# file has to be sorted for comparison with `comm` # file has to be sorted for comparison with `comm`
sort "$__object/parameter/group" > "$__object/files/group.sorted" sort "$__object/parameter/group" > "$__object/files/group.sorted"
@ -43,11 +30,9 @@ sort "$__object/parameter/group" > "$__object/files/group.sorted"
case "$state_should" in case "$state_should" in
present) present)
changed_groups="$(comm -13 "$__object/explorer/group" "$__object/files/group.sorted")" changed_groups="$(comm -13 "$__object/explorer/group" "$__object/files/group.sorted")"
action="$addparam"
;; ;;
absent) absent)
changed_groups="$(comm -12 "$__object/explorer/group" "$__object/files/group.sorted")" changed_groups="$(comm -12 "$__object/explorer/group" "$__object/files/group.sorted")"
action="$delparam"
;; ;;
esac esac
@ -57,9 +42,25 @@ if [ -z "$changed_groups" ]; then
fi fi
for group in $changed_groups; do for group in $changed_groups; do
if [ "$oldusermod" = "true" ]; then if [ "$os" = "netbsd" ]; then
echo "usermod $action \"$group\" \"$user\"" case "$state_should" in
present) echo "usermod -G \"$group\" \"$user\"" ;;
absent) echo 'NetBSD does not have a command to remove a user from a group' >&2 ; exit 1 ;;
esac
elif [ "$os" = "freebsd" ]; then
case "$state_should" in
present) echo "pw groupmod \"$group\" -m \"$user\"" ;;
absent) echo "pw groupmod \"$group\" -d \"$user\"" ;;
esac
elif [ "$oldusermod" = "true" ]; then
case "$state_should" in
present) echo "usermod -A \"$group\" \"$user\"" ;;
absent) echo "usermod -R \"$group\" \"$user\"" ;;
esac
else else
echo "gpasswd $action \"$user\" \"$group\"" case "$state_should" in
present) echo "gpasswd -a \"$group\" \"$user\"" ;;
absent) echo "gpasswd -d \"$group\" \"$user\"" ;;
esac
fi fi
done done