|
Zbigniew Jędrzejewski-Szmek |
ced923 |
#!/bin/bash
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
# This script turns sysuser.d files into scriptlets mandated by Fedora
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
# packaging guidelines. The general idea is to define users using the
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
# declarative syntax but to turn this into traditional scriptlets.
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
user() {
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
user="$1"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
uid="$2"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
desc="$3"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
group="$4"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
home="$5"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
shell="$6"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
[ "$desc" = '-' ] && desc=
|
|
Zbigniew Jędrzejewski-Szmek |
975bf6 |
{ [ "$home" = '-' ] || [ "$home" = '' ]; } && home=/
|
|
Luca BRUNO |
d00b46 |
{ [ "$shell" = '-' ] || [ "$shell" = '' ]; } && shell=/usr/sbin/nologin
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
|
|
Zbigniew Jędrzejewski-Szmek |
975bf6 |
if [ "$uid" = '-' ] || [ "$uid" = '' ]; then
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
cat <
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
getent passwd '$user' >/dev/null || \\
|
|
Zbigniew Jędrzejewski-Szmek |
a65bd0 |
useradd -r -g '$group' -d '$home' -s '$shell' -c '$desc' '$user' || :
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
EOF
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
else
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
cat <
|
|
Zbigniew Jędrzejewski-Szmek |
a65bd0 |
if ! getent passwd '$user' >/dev/null; then
|
|
Zbigniew Jędrzejewski-Szmek |
a65bd0 |
if ! getent passwd '$uid' >/dev/null; then
|
|
Luca BRUNO |
d00b46 |
useradd -r -u '$uid' -g '$group' -d '$home' -s '$shell' -c '$desc' '$user' || :
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
else
|
|
Luca BRUNO |
d00b46 |
useradd -r -g '$group' -d '$home' -s '$shell' -c '$desc' '$user' || :
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
fi
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
fi
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
EOF
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
fi
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
group() {
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
group="$1"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
gid="$2"
|
|
Luca BRUNO |
f27d46 |
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
if [ "$gid" = '-' ]; then
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
cat <<-EOF
|
|
Zbigniew Jędrzejewski-Szmek |
a65bd0 |
getent group '$group' >/dev/null || groupadd -r '$group' || :
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
EOF
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
else
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
cat <<-EOF
|
|
Zbigniew Jędrzejewski-Szmek |
a65bd0 |
getent group '$group' >/dev/null || groupadd -f -g '$gid' -r '$group' || :
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
EOF
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
fi
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
|
|
Luca BRUNO |
f27d46 |
usermod() {
|
|
Luca BRUNO |
f27d46 |
user="$1"
|
|
Luca BRUNO |
f27d46 |
group="$2"
|
|
Luca BRUNO |
f27d46 |
|
|
Luca BRUNO |
f27d46 |
cat <<-EOF
|
|
Luca BRUNO |
f27d46 |
if getent group '$group' >/dev/null; then
|
|
Luca BRUNO |
f27d46 |
usermod -a -G '$group' '$user' || :
|
|
Luca BRUNO |
f27d46 |
fi
|
|
Luca BRUNO |
f27d46 |
EOF
|
|
Luca BRUNO |
f27d46 |
}
|
|
Luca BRUNO |
f27d46 |
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
parse() {
|
|
Zbigniew Jędrzejewski-Szmek |
975bf6 |
while read -r line || [ -n "$line" ] ; do
|
|
Zbigniew Jędrzejewski-Szmek |
975bf6 |
{ [ "${line:0:1}" = '#' ] || [ "${line:0:1}" = ';' ]; } && continue
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
line="${line## *}"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
[ -z "$line" ] && continue
|
|
Zbigniew Jędrzejewski-Szmek |
975bf6 |
eval "arr=( $line )"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
case "${arr[0]}" in
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
('u')
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
group "${arr[1]}" "${arr[2]}"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
user "${arr[1]}" "${arr[2]}" "${arr[3]}" "${arr[1]}" "${arr[4]}" "${arr[5]}"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
# TODO: user:group support
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
;;
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
('g')
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
group "${arr[1]}" "${arr[2]}"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
;;
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
('m')
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
group "${arr[2]}" "-"
|
|
Luca BRUNO |
f27d46 |
user "${arr[1]}" "-" "" "${arr[1]}" "" ""
|
|
Luca BRUNO |
f27d46 |
usermod "${arr[1]}" "${arr[2]}"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
;;
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
esac
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
done
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
for fn in "$@"; do
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
[ -e "$fn" ] || continue
|
|
Zbigniew Jędrzejewski-Szmek |
975bf6 |
echo "# generated from $(basename "$fn")"
|
|
Zbigniew Jędrzejewski-Szmek |
157506 |
parse <"$fn"
|
|
Zbigniew Jędrzejewski-Szmek |
ced923 |
done
|