teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/sysusers.generate-pre.sh

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