File: sysusers.sh

package info (click to toggle)
rpm 4.20.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 9,832 kB
  • sloc: ansic: 70,093; sh: 1,843; python: 1,037; cpp: 324; makefile: 39
file content (189 lines) | stat: -rwxr-xr-x 4,032 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
# -*- mode: shell-script; indent-tabs-mode: true; tab-width: 4; -*-

# Creates system users and groups, based on files in the format described
# in sysusers.d(5). Replacement for systemd-sysusers(8) for the use of RPM
#
# USAGE:
#
#   sysusers.sh [--root ROOTDIR] [--replace CONFIGFILE] [CONFIGFILE...]
#
# SEE ALSO systemd-sysusers(8)
#
# Based on sysusers.generate-pre.sh from the Fedora systemd package

SYSTEMDIR=/usr/lib/sysusers.d
RUNTIMEDIR=/run/sysusers.d
SYSTEMCONFDIR=/etc/sysusers.d

ROOT=/
REPLACE=#

while test $# -gt 0 ; do
	case "${1}" in
		--root )
			shift
			ROOT=${1}
			shift
			;;
		--replace )
			shift
			REPLACE=${1}
			shift
			;;
		--help )
			cat <<EOF

sysusers.sh - Creates system users and groups, based on files in the
format described in sysusers.d(5). Replacement for systemd-sysusers(8)
for the use of RPM.

USAGE:

	sysusers.sh [--root ROOTDIR] [--replace CONFIGFILE] [CONFIGFILE...]

SEE ALSO systemd-sysusers(8)

EOF
			shift
			;;
		* )
			break
			;;
	esac
done

hasuser() {
	grep "^${1}:" "$ROOT"/etc/passwd >/dev/null
}

hasuid() {
	cut -d: -f3 "$ROOT"/etc/passwd | grep "${1}"  >/dev/null
}

hasgroup() {
	grep "^${1}:" "$ROOT"/etc/group >/dev/null
}

user() {
	local user="$1"
	local uid="$2"
	local desc="$3"
	local group="$4"
	local home="$5"
	local shell="$6"
	local expire="$7"

	[ "$desc" = '-' ] && desc=
	{ [ "$home" = '-' ] || [ "$home" = '' ]; } && home=/
	{ [ "$shell" = '-' ] || [ "$shell" = '' ]; } && shell=/sbin/nologin

	if [ "$uid" = '-' ] || [ "$uid" = '' ]; then
		if ! hasuser "$user" ; then
			useradd -R "$ROOT" -r -g "${group}" -d "${home}" -s "${shell}" -c "${desc}" "${user}" || :
		fi
	else
		if ! hasuser "${user}" ; then
			if ! hasuid "${uid}" ; then
				useradd -R "$ROOT" -r -u "${uid}" -g "${group}" -d "${home}" -s "${shell}" -c "${desc}" "${user}" || :
			else
				useradd -R "$ROOT" -r -g "${group}" -d "${home}" -s "${shell}" -c "${desc}" "${user}" || :
			fi
		fi
	fi

	if [[ $expire ]]; then
	    usermod -R "$ROOT" -e 1 "${user}"
	fi
}

group() {
	local group="$1"
	local gid="$2"

	if [ "$gid" = '-' ]; then
		hasgroup "${group}" || groupadd -R "$ROOT" -f -r "${group}" || :
	else
		hasgroup "${group}" || groupadd -R "$ROOT" -f -g "${gid}" -r "${group}" || :
	fi
}


addtogroup() {
	local user="$1"
	local group="$2"
	
	group "${group}" "-"
	user "${user}" "-" "" "${group}" "" "" ""

	usermod -R "$ROOT" -a -G "${group}" "$user" || :
}

parse() {
	local line arr expire

	while read -r line || [ -n "$line" ] ; do
		{ [ "${line:0:1}" = '#' ] || [ "${line:0:1}" = ';' ]; } && continue
		line="${line## *}"
		[ -z "$line" ] && continue
		mapfile -t arr < <(xargs -n1 <<<"$line")

		expire=""

		case "${arr[0]}" in
			('u' | 'u!')
				if [[ "${arr[0]}" == 'u!' ]]; then
					expire="1";
				fi

				if [[ "${arr[2]}" == *":"* ]]; then
					user "${arr[1]}" "${arr[2]%:*}" "${arr[3]}" "${arr[2]#*:}" "${arr[4]}" "${arr[5]}" $expire
				else
					group "${arr[1]}" "${arr[2]}"
					user "${arr[1]}" "${arr[2]}" "${arr[3]}" "${arr[1]}" "${arr[4]}" "${arr[5]}" $expire
				fi
				;;
			('g')
				group "${arr[1]}" "${arr[2]}"
				;;
			('m')
				addtogroup "${arr[1]}" "${arr[2]}"
				;;
		esac
	done
}


# Make sure etc and the user/group files exist
mkdir -p "$ROOT"/etc/
[ -e "$ROOT"/etc/passwd ] || touch "$ROOT"/etc/passwd
[ -e "$ROOT"/etc/shadow ] || touch "$ROOT"/etc/shadow
[ -e "$ROOT"/etc/group ] || touch "$ROOT"/etc/group

# read files from command line
for fn in "$@"; do
	# check if file is replaced by one with higher priority
	REPLACEDIR=$(dirname "$REPLACE")
	REPLACENAME=$(basename "$REPLACE")
	if [[ "$REPLACEDIR" == "${SYSTEMDIR}" ]] ; then
		if [ -f "${ROOT}${RUNTIMEDIR}/${REPLACENAME}" ] ; then
			continue
		fi
		if [ -f "${ROOT}${SYSTEMCONFDIR}/${REPLACENAME}" ] ; then
			continue
		fi
	fi

	if [[ "$REPLACEDIR" == "$RUNTIMEDIR" ]] ; then
		if [ -f "${ROOT}${SYSTEMCONFDIR}/${REPLACENAME}" ] ; then
			continue
		fi
	fi

	if [ "-" = "$fn" ]; then
		parse <&0
	else
		[ -r "$fn" ] || continue
		parse <"$fn"
	fi
done