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