2020-02-07 15:34:30 +00:00
|
|
|
#!/bin/bash
|
2022-10-09 13:12:53 +00:00
|
|
|
# -*- mode: shell-script; indent-tabs-mode: true; tab-width: 4; -*-
|
2020-02-07 15:34:30 +00:00
|
|
|
|
|
|
|
# This script turns sysuser.d files into scriptlets mandated by Fedora
|
|
|
|
# packaging guidelines. The general idea is to define users using the
|
|
|
|
# declarative syntax but to turn this into traditional scriptlets.
|
|
|
|
|
|
|
|
user() {
|
2022-10-09 13:12:53 +00:00
|
|
|
user="$1"
|
|
|
|
uid="$2"
|
|
|
|
desc="$3"
|
|
|
|
group="$4"
|
|
|
|
home="$5"
|
|
|
|
shell="$6"
|
2020-02-07 15:34:30 +00:00
|
|
|
|
2022-10-09 13:12:53 +00:00
|
|
|
[ "$desc" = '-' ] && desc=
|
|
|
|
{ [ "$home" = '-' ] || [ "$home" = '' ]; } && home=/
|
|
|
|
{ [ "$shell" = '-' ] || [ "$shell" = '' ]; } && shell=/usr/sbin/nologin
|
2020-02-07 15:34:30 +00:00
|
|
|
|
2022-10-09 13:12:53 +00:00
|
|
|
if [ "$uid" = '-' ] || [ "$uid" = '' ]; then
|
|
|
|
cat <<-EOF
|
|
|
|
getent passwd '$user' >/dev/null || \\
|
|
|
|
useradd -r -g '$group' -d '$home' -s '$shell' -c '$desc' '$user' || :
|
|
|
|
EOF
|
|
|
|
else
|
|
|
|
cat <<-EOF
|
|
|
|
if ! getent passwd '$user' >/dev/null; then
|
|
|
|
if ! getent passwd '$uid' >/dev/null; then
|
|
|
|
useradd -r -u '$uid' -g '$group' -d '$home' -s '$shell' -c '$desc' '$user' || :
|
|
|
|
else
|
|
|
|
useradd -r -g '$group' -d '$home' -s '$shell' -c '$desc' '$user' || :
|
|
|
|
fi
|
|
|
|
fi
|
2020-02-07 15:34:30 +00:00
|
|
|
|
2022-10-09 13:12:53 +00:00
|
|
|
EOF
|
|
|
|
fi
|
2020-02-07 15:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
group() {
|
2022-10-09 13:12:53 +00:00
|
|
|
group="$1"
|
|
|
|
gid="$2"
|
2022-09-01 12:51:38 +00:00
|
|
|
|
2022-10-09 13:12:53 +00:00
|
|
|
if [ "$gid" = '-' ]; then
|
|
|
|
cat <<-EOF
|
|
|
|
getent group '$group' >/dev/null || groupadd -r '$group' || :
|
|
|
|
EOF
|
|
|
|
else
|
|
|
|
cat <<-EOF
|
|
|
|
getent group '$group' >/dev/null || groupadd -f -g '$gid' -r '$group' || :
|
|
|
|
EOF
|
|
|
|
fi
|
2020-02-07 15:34:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 12:51:38 +00:00
|
|
|
usermod() {
|
2022-10-09 13:12:53 +00:00
|
|
|
user="$1"
|
|
|
|
group="$2"
|
2022-09-01 12:51:38 +00:00
|
|
|
|
2022-10-09 13:12:53 +00:00
|
|
|
cat <<-EOF
|
|
|
|
if getent group '$group' >/dev/null; then
|
|
|
|
usermod -a -G '$group' '$user' || :
|
|
|
|
fi
|
|
|
|
EOF
|
2022-09-01 12:51:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:34:30 +00:00
|
|
|
parse() {
|
2022-10-09 13:12:53 +00:00
|
|
|
while read -r line || [ -n "$line" ] ; do
|
|
|
|
{ [ "${line:0:1}" = '#' ] || [ "${line:0:1}" = ';' ]; } && continue
|
|
|
|
line="${line## *}"
|
|
|
|
[ -z "$line" ] && continue
|
|
|
|
eval "arr=( $line )"
|
|
|
|
case "${arr[0]}" in
|
|
|
|
('u')
|
|
|
|
group "${arr[1]}" "${arr[2]}"
|
|
|
|
user "${arr[1]}" "${arr[2]}" "${arr[3]}" "${arr[1]}" "${arr[4]}" "${arr[5]}"
|
|
|
|
# TODO: user:group support
|
|
|
|
;;
|
|
|
|
('g')
|
|
|
|
group "${arr[1]}" "${arr[2]}"
|
|
|
|
;;
|
|
|
|
('m')
|
|
|
|
group "${arr[2]}" "-"
|
|
|
|
user "${arr[1]}" "-" "" "${arr[1]}" "" ""
|
|
|
|
usermod "${arr[1]}" "${arr[2]}"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2020-02-07 15:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for fn in "$@"; do
|
2022-10-09 13:12:53 +00:00
|
|
|
[ -e "$fn" ] || continue
|
|
|
|
echo "# generated from $(basename "$fn")"
|
|
|
|
parse <"$fn"
|
2020-02-07 15:34:30 +00:00
|
|
|
done
|