naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
d0811f
#!/bin/bash
09f814
# -*- mode: shell-script; indent-tabs-mode: true; tab-width: 4; -*-
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() {
09f814
	user="$1"
09f814
	uid="$2"
09f814
	desc="$3"
09f814
	group="$4"
09f814
	home="$5"
09f814
	shell="$6"
d0811f
09f814
	[ "$desc" = '-' ] && desc=
09f814
	{ [ "$home" = '-' ] || [ "$home" = '' ]; } && home=/
09f814
	{ [ "$shell" = '-' ] || [ "$shell" = '' ]; } && shell=/usr/sbin/nologin
d0811f
09f814
	if [ "$uid" = '-' ] || [ "$uid" = '' ]; then
09f814
		cat <<-EOF
09f814
		getent passwd '$user' >/dev/null || \\
09f814
			useradd -r -g '$group' -d '$home' -s '$shell' -c '$desc' '$user' || :
09f814
		EOF
09f814
	else
09f814
		cat <<-EOF
09f814
		if ! getent passwd '$user' >/dev/null; then
09f814
			if ! getent passwd '$uid' >/dev/null; then
09f814
			useradd -r -u '$uid' -g '$group' -d '$home' -s '$shell' -c '$desc' '$user' || :
09f814
			else
09f814
			useradd -r -g '$group' -d '$home' -s '$shell' -c '$desc' '$user' || :
09f814
			fi
09f814
		fi
d0811f
09f814
		EOF
09f814
	fi
d0811f
}
d0811f
d0811f
group() {
09f814
	group="$1"
09f814
	gid="$2"
09f814
09f814
	if [ "$gid" = '-' ]; then
09f814
		cat <<-EOF
09f814
		getent group '$group' >/dev/null || groupadd -r '$group' || :
09f814
		EOF
09f814
	else
09f814
		cat <<-EOF
09f814
		getent group '$group' >/dev/null || groupadd -f -g '$gid' -r '$group' || :
09f814
		EOF
09f814
	fi
09f814
}
09f814
09f814
usermod() {
09f814
	user="$1"
09f814
	group="$2"
09f814
09f814
	cat <<-EOF
09f814
	if getent group '$group' >/dev/null; then
09f814
		usermod -a -G '$group' '$user' || :
09f814
	fi
ae4880
	EOF
d0811f
}
d0811f
d0811f
parse() {
09f814
	while read -r line || [ -n "$line" ] ; do
09f814
		{ [ "${line:0:1}" = '#' ] || [ "${line:0:1}" = ';' ]; } && continue
09f814
		line="${line## *}"
09f814
		[ -z "$line" ] && continue
09f814
		eval "arr=( $line )"
09f814
		case "${arr[0]}" in
09f814
			('u')
09f814
				if [[ "${arr[2]}" == *":"* ]]; then
09f814
					user "${arr[1]}" "${arr[2]%:*}" "${arr[3]}" "${arr[2]#*:}" "${arr[4]}" "${arr[5]}"
09f814
				else
09f814
					group "${arr[1]}" "${arr[2]}"
09f814
					user "${arr[1]}" "${arr[2]}" "${arr[3]}" "${arr[1]}" "${arr[4]}" "${arr[5]}"
09f814
				fi
09f814
				;;
09f814
			('g')
09f814
				group "${arr[1]}" "${arr[2]}"
09f814
				;;
09f814
			('m')
09f814
				group "${arr[2]}" "-"
09f814
				user "${arr[1]}" "-" "" "${arr[1]}" "" ""
09f814
				usermod "${arr[1]}" "${arr[2]}"
09f814
				;;
09f814
		esac
09f814
	done
d0811f
}
d0811f
d0811f
for fn in "$@"; do
09f814
	[ -e "$fn" ] || continue
09f814
	echo "# generated from $(basename "$fn")"
09f814
	parse <"$fn"
d0811f
done