Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports-kde
Path: blob/main/Mk/Scripts/do-users-groups.sh
16461 views
1
#!/bin/sh
2
#
3
# MAINTAINER: [email protected]
4
5
set -e
6
set -o pipefail
7
8
. "${dp_SCRIPTSDIR}/functions.sh"
9
10
validate_env dp_ECHO_MSG dp_GID_FILES dp_GID_OFFSET dp_GROUPS_BLACKLIST \
11
dp_INSTALL dp_OPSYS dp_OSVERSION dp_PREFIX dp_PW dp_SCRIPTSDIR \
12
dp_UG_DEINSTALL dp_UG_INSTALL dp_UID_FILES dp_UID_OFFSET \
13
dp_USERS_BLACKLIST
14
15
[ -n "${DEBUG_MK_SCRIPTS}" -o -n "${DEBUG_MK_SCRIPTS_DO_USERS_GROUPS}" ] && set -x
16
17
set -u
18
19
USERS=$1
20
GROUPS=$2
21
22
error() {
23
${dp_ECHO_MSG} "${1}"
24
25
exit 1
26
}
27
28
# Lines from GID and UID files both contain *. As we do not need any pathname
29
# expansion, disable globbing.
30
set -f
31
32
rm -f "${dp_UG_INSTALL}" "${dp_UG_DEINSTALL}" || :
33
34
if [ "${dp_OPSYS}" = FreeBSD ] ; then
35
cat >> "${dp_UG_INSTALL}" <<-eot
36
if [ -n "\${PKG_ROOTDIR}" ] && [ "\${PKG_ROOTDIR}" != "/" ]; then
37
PW="${dp_PW} -R \${PKG_ROOTDIR}"
38
else
39
PW=${dp_PW}
40
fi
41
eot
42
else
43
echo "PW=${dp_PW}" >> "${dp_UG_INSTALL}"
44
fi
45
46
# Both scripts need to start the same, so
47
cp -f "${dp_UG_INSTALL}" "${dp_UG_DEINSTALL}"
48
49
if [ -n "${GROUPS}" ]; then
50
for file in ${dp_GID_FILES}; do
51
if [ ! -f "${file}" ]; then
52
error "** ${file} doesn't exist. Exiting."
53
fi
54
done
55
${dp_ECHO_MSG} "===> Creating groups"
56
echo "echo \"===> Creating groups\"" >> "${dp_UG_INSTALL}"
57
for group in ${GROUPS}; do
58
# _bgpd:*:130:
59
if ! grep -q "^${group}:" ${dp_GID_FILES}; then \
60
error "** Cannot find any information about group \`${group}' in ${dp_GID_FILES}"
61
fi
62
while read -r line; do
63
# Do not change IFS for more than one command, if we
64
# changed IFS around the while read, it would mess up
65
# the string splitting in the heredoc command.
66
o_IFS=${IFS}
67
IFS=":"
68
set -- ${line}
69
IFS=${o_IFS}
70
group=$1
71
gid=$3
72
if [ -z "${gid}" ]; then
73
error "Group line for group ${group} has no gid"
74
fi
75
gid=$((gid+dp_GID_OFFSET))
76
cat >> "${dp_UG_INSTALL}" <<-eot2
77
if ! \${PW} groupshow $group >/dev/null 2>&1; then
78
echo "Creating group '$group' with gid '$gid'"
79
\${PW} groupadd $group -g $gid || exit \$?
80
else
81
echo "Using existing group '$group'"
82
fi
83
eot2
84
done <<-eot
85
$(grep -h "^${group}:" ${dp_GID_FILES} | head -n 1)
86
eot
87
done
88
fi
89
90
if [ -n "${USERS}" ]; then
91
for file in ${dp_UID_FILES}; do
92
if [ ! -f "${file}" ]; then
93
error "** ${file} doesn't exist. Exiting."
94
fi
95
done
96
97
${dp_ECHO_MSG} "===> Creating users"
98
echo "echo \"===> Creating users\"" >> "${dp_UG_INSTALL}"
99
100
for user in ${USERS}; do
101
# _bgpd:*:130:130:BGP Daemon:/var/empty:/sbin/nologin
102
if ! grep -q "^${user}:" ${dp_UID_FILES} ; then
103
error "** Cannot find any information about user \`${user}' in ${dp_UID_FILES}"
104
fi
105
while read -r line; do
106
# Do not change IFS for more than one command, if we
107
# changed IFS around the while read, it would mess up
108
# the string splitting in the heredoc command.
109
o_IFS=${IFS}
110
IFS=":"
111
set -- ${line}
112
IFS=${o_IFS}
113
login=$1
114
uid=$3
115
gid=$4
116
class=$5
117
gecos=$8
118
homedir=$9
119
shell=${10}
120
if [ -z "$uid" ] || [ -z "$gid" ] || [ -z "$homedir" ] || [ -z "$shell" ]; then
121
error "User line for ${user} is invalid"
122
fi
123
uid=$((uid+dp_UID_OFFSET))
124
gid=$((gid+dp_GID_OFFSET))
125
if [ -n "$class" ]; then
126
class="-L $class"
127
fi
128
homedir=$(echo "$homedir" | sed "s|^/usr/local|${dp_PREFIX}|")
129
cat >> "${dp_UG_INSTALL}" <<-eot2
130
if ! \${PW} usershow $login >/dev/null 2>&1; then
131
echo "Creating user '$login' with uid '$uid'"
132
\${PW} useradd $login -u $uid -g $gid $class -c "$gecos" -d $homedir -s $shell || exit \$?
133
else
134
echo "Using existing user '$login'"
135
fi
136
eot2
137
case $homedir in
138
/|/nonexistent|/var/empty)
139
;;
140
*)
141
echo "echo \"===> Creating homedir(s)\"" >> "${dp_UG_INSTALL}"
142
group=$(awk -F: -v gid=${gid} '$1 !~ /^#/ && $3 == gid { print $1 }' ${dp_GID_FILES})
143
cat >> "${dp_UG_INSTALL}" <<-blah
144
if [ -n "\${PKG_ROOTDIR}" ] && [ "\${PKG_ROOTDIR}" != "/" ]; then
145
HOMEDIR="\${PKG_ROOTDIR}/$homedir"
146
MDBDIR="-N \${PKG_ROOTDIR}/etc/"
147
else
148
HOMEDIR="$homedir"
149
MDBDIR=""
150
fi
151
${dp_INSTALL} \${MDBDIR} -d -g $group -o $login \${HOMEDIR}
152
blah
153
;;
154
esac
155
done <<-eot
156
$(grep -h "^${user}:" ${dp_UID_FILES} | head -n 1)
157
eot
158
done
159
fi
160
161
if [ -n "${GROUPS}" ]; then
162
for group in ${GROUPS}; do
163
# mail:*:6:postfix,clamav
164
while read -r line; do
165
# Do not change IFS for more than one command, if we
166
# changed IFS around the while read, it would mess up
167
# the string splitting in the heredoc command.
168
o_IFS=${IFS}
169
IFS=":"
170
# As some lines do not have a fourth argument, provide
171
# one so $4 always exists.
172
set -- ${line} ""
173
IFS=${o_IFS}
174
group=$1
175
gid=$3
176
members=$4
177
gid=$((gid+dp_GID_OFFSET))
178
o_IFS=${IFS}
179
IFS=","
180
set -- ${members}
181
IFS=${o_IFS}
182
for login in "$@"; do
183
for user in ${USERS}; do
184
if [ -n "${user}" ] && [ "${user}" = "${login}" ]; then
185
cat >> "${dp_UG_INSTALL}" <<-eot2
186
if ! \${PW} groupshow ${group} | grep -qw ${login}; then
187
echo "Adding user '${login}' to group '${group}'"
188
\${PW} groupmod ${group} -m ${login} || exit \$?
189
fi
190
eot2
191
fi
192
done
193
done
194
done <<-eot
195
$(grep -h "^${group}:" ${dp_GID_FILES} | head -n 1)
196
eot
197
done
198
fi
199
200
if [ -n "${USERS}" ]; then
201
for user in ${USERS}; do
202
if ! echo "${dp_USERS_BLACKLIST}" | grep -qw "${user}"; then
203
cat >> "${dp_UG_DEINSTALL}" <<-eot
204
if \${PW} usershow ${user} >/dev/null 2>&1; then
205
echo "==> You should manually remove the \"${user}\" user"
206
fi
207
eot
208
fi
209
done
210
fi
211
212
if [ -n "${GROUPS}" ]; then
213
for group in ${GROUPS}; do
214
if ! echo "${dp_GROUPS_BLACKLIST}" | grep -qw "${group}"; then
215
cat >> "${dp_UG_DEINSTALL}" <<-eot
216
if \${PW} groupshow ${group} >/dev/null 2>&1; then
217
echo "==> You should manually remove the \"${group}\" group"
218
fi
219
eot
220
fi
221
done
222
fi
223
224