1 # $NetBSD: usergroup,v 1.3 2009/10/29 20:19:27 joerg Exp $
3 # Generate a +USERGROUP script that reference-counts users and groups
4 # that are required for the proper functioning of the package.
7 UNPACK,|UNPACK,+USERGROUP)
8 ${CAT} > ./+USERGROUP << 'EOF'
11 # +USERGROUP - users and groups management script
13 # Usage: ./+USERGROUP ADD|REMOVE [metadatadir]
14 # ./+USERGROUP CHECK-ADD|CHECK-REMOVE [metadatadir]
16 # This script supports two actions, ADD and REMOVE, that will add or
17 # remove the users and groups needed by the package associated with
18 # <metadatadir>. The CHECK-ADD action will check whether any users or
19 # groups needed by the package are missing, and print an informative
20 # message noting those users and groups. The CHECK-REMOVE action will
21 # check whether any users and groups needed by the package still exist,
22 # and print an informative message noting those users and groups. The
23 # CHECK-ADD and CHECK-REMOVE actions return non-zero if they detect
24 # either missing or existing users/groups, respectively.
26 # Lines starting with "# USER: " or "# GROUP: " are data read by this
27 # script that name the users and groups that this package requires to
28 # exist to function correctly, e.g.
30 # # USER: foo:foogrp::The Foomister
33 # The USER lines are of the form:
35 # user:group[:[userid][:[descr][:[home][:shell]]]]
37 # Only the user and group are required; everything else is optional,
38 # but the colons must be in the right places when specifying optional
41 # The GROUP lines are of the form:
45 # Only the group is required; the groupid is optional.
69 PKG_METADATA_DIR="${2-${CURDIR}}"
70 : ${PKGNAME=${PKG_METADATA_DIR##*/}}
71 : ${PKG_DBDIR=${PKG_METADATA_DIR%/*}}
72 : ${PKG_REFCOUNT_DBDIR=${PKG_DBDIR}.refcount}
74 PKG_REFCOUNT_USERS_DBDIR="${PKG_REFCOUNT_DBDIR}/users"
75 PKG_REFCOUNT_GROUPS_DBDIR="${PKG_REFCOUNT_DBDIR}/groups"
77 case "${PKG_CREATE_USERGROUP:-@PKG_CREATE_USERGROUP@}" in
78 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
79 _PKG_CREATE_USERGROUP=yes
81 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
82 _PKG_CREATE_USERGROUP=no
94 "") _buffer="$_word" ;;
95 *) _buffer="$_buffer $_word" ;;
97 if ${TEST} ${#_buffer} -gt $_length; then
105 *) ${ECHO} " $_buffer" ;;
109 # DO NOT CHANGE THE FOLLOWING LINE!
110 # platform-specific adduser/addgroup functions
115 ${SED} -n "/^\# GROUP: /{s/^\# GROUP: //;p;}" ${SELF} | ${SORT} -u |
116 { while read line; do
117 SAVEIFS="$IFS"; IFS=":"
119 group="$1"; groupid="$2"
124 shadow_dir="${PKG_REFCOUNT_GROUPS_DBDIR}/$group"
125 preexist="$shadow_dir/+PREEXISTING"
126 token="$shadow_dir/${PKGNAME}"
127 if ${TEST} ! -d "$shadow_dir"; then
129 group_exists $group $groupid &&
130 ${ECHO} "${PKGNAME}" > $preexist
132 if ${TEST} -f "$token" && \
133 ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
136 ${ECHO} "${PKG_METADATA_DIR}" >> $token
138 case ${_PKG_CREATE_USERGROUP} in
140 group_exists $group $groupid
143 1) addgroup "$group" "$groupid" ;;
148 ${SED} -n "/^\# USER: /{s/^\# USER: //;p;}" ${SELF} | ${SORT} -u |
149 { while read line; do
150 SAVEIFS="$IFS"; IFS=":"
152 user="$1"; group="$2"; userid="$3"
153 descr="$4"; home="$5" shell="$6"
161 shadow_dir="${PKG_REFCOUNT_USERS_DBDIR}/$user"
162 preexist="$shadow_dir/+PREEXISTING"
163 token="$shadow_dir/${PKGNAME}"
164 if ${TEST} ! -d "$shadow_dir"; then
166 user_exists $user $userid &&
167 ${ECHO} "${PKGNAME}" > $preexist
169 if ${TEST} -f "$token" && \
170 ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
173 ${ECHO} "${PKG_METADATA_DIR}" >> $token
175 case ${_PKG_CREATE_USERGROUP} in
177 group_exists $group || continue
178 user_exists $user $userid
181 1) adduser "$user" "$group" "$userid" \
182 "$descr" "$home" "$shell"
191 ${SED} -n "/^\# USER: /{s/^\# USER: //;p;}" ${SELF} | ${SORT} -u |
192 { while read line; do
193 SAVEIFS="$IFS"; IFS=":"
195 user="$1"; group="$2"; userid="$3"
196 descr="$4"; home="$5" shell="$6"
201 shadow_dir="${PKG_REFCOUNT_USERS_DBDIR}/$user"
202 preexist="$shadow_dir/+PREEXISTING"
203 token="$shadow_dir/${PKGNAME}"
204 tokentmp="$token.tmp.$$"
205 if ${TEST} -f "$token" && \
206 ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
207 ${CAT} "$token" | ${GREP} -v "^${PKG_METADATA_DIR}$" > $tokentmp
208 case `${CAT} $tokentmp | ${SED} -n "$="` in
210 ${RM} -f $preexist $token $token.tmp.*
211 ${RMDIR} -p $shadow_dir 2>/dev/null || ${TRUE}
214 ${MV} -f $tokentmp $token
219 ${SED} -n "/^\# GROUP: /{s/^\# GROUP: //;p;}" ${SELF} | ${SORT} -u |
220 { while read line; do
221 SAVEIFS="$IFS"; IFS=":"
223 group="$1"; groupid="$2"
228 shadow_dir="${PKG_REFCOUNT_GROUPS_DBDIR}/$group"
229 preexist="$shadow_dir/+PREEXISTING"
230 token="$shadow_dir/${PKGNAME}"
231 tokentmp="$token.tmp.$$"
232 if ${TEST} -f "$token" && \
233 ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
234 ${CAT} "$token" | ${GREP} -v "^${PKG_METADATA_DIR}$" > $tokentmp
235 case `${CAT} $tokentmp | ${SED} -n "$="` in
237 ${RM} -f $preexist $token $token.tmp.*
238 ${RMDIR} -p $shadow_dir 2>/dev/null || ${TRUE}
241 ${MV} -f $tokentmp $token
249 ${SED} -n "/^\# GROUP: /{s/^\# GROUP: //;p;}" ${SELF} | ${SORT} -u |
250 { while read line; do
251 SAVEIFS="$IFS"; IFS=":"
253 group="$1"; groupid="$2"
257 *) group_exists $group $groupid && continue ;;
259 case "$printed_header" in
261 *) printed_header=yes
262 ${ECHO} "==========================================================================="
263 ${ECHO} "The following groups need to be created for ${PKGNAME}:"
268 "") ${ECHO} " $group" ;;
269 *) ${ECHO} " $group (gid = $groupid)" ;;
272 case "$printed_header" in
274 ${ECHO} "==========================================================================="
278 ${TEST} $? -eq 0 || exitcode=1
279 ${SED} -n "/^\# USER: /{s/^\# USER: //;p;}" ${SELF} | ${SORT} -u |
280 { while read line; do
281 SAVEIFS="$IFS"; IFS=":"
283 user="$1"; group="$2"; userid="$3"
284 descr="$4"; home="$5" shell="$6"
288 *) user_exists $user $userid && continue ;;
290 case "$printed_header" in
292 *) printed_header=yes
293 ${ECHO} "==========================================================================="
294 ${ECHO} "The following users need to be created for ${PKGNAME}:"
298 : ${home:="@PKG_USER_HOME@"}
299 : ${shell:="@PKG_USER_SHELL@"}
301 "") ${ECHO} " $user: $group, $home, $shell" ;;
302 *) ${ECHO} " $user (uid = $userid): $group, $home, $shell" ;;
305 case "$printed_header" in
307 ${ECHO} "==========================================================================="
311 ${TEST} $? -eq 0 || exitcode=1
315 ${SED} -n "/^\# USER: /{s/^\# USER: //;p;}" ${SELF} | ${SORT} -u |
316 { while read line; do
317 SAVEIFS="$IFS"; IFS=":"
319 user="$1"; group="$2"; userid="$3"
320 descr="$4"; home="$5" shell="$6"
324 *) user_exists $user $userid || continue ;;
326 shadow_dir="${PKG_REFCOUNT_USERS_DBDIR}/$user"
327 ${TEST} ! -d "$shadow_dir" || continue # refcount isn't zero
328 existing_users="$existing_users $user"
330 case $existing_users in
332 *) ${ECHO} "==========================================================================="
333 ${ECHO} "The following users are no longer being used by ${PKGNAME},"
334 ${ECHO} "and they can be removed if no other software is using them:"
336 ${ECHO} "$existing_users" | listwrap 40
338 ${ECHO} "==========================================================================="
342 ${TEST} $? -eq 0 || exitcode=1
343 ${SED} -n "/^\# GROUP: /{s/^\# GROUP: //;p;}" ${SELF} | ${SORT} -u |
344 { while read line; do
345 SAVEIFS="$IFS"; IFS=":"
347 group="$1"; groupid="$2"
351 *) group_exists $group $groupid || continue ;;
353 shadow_dir="${PKG_REFCOUNT_GROUPS_DBDIR}/$group"
354 ${TEST} ! -d "$shadow_dir" || continue # refcount isn't zero
355 existing_groups="$existing_groups $group"
357 case $existing_groups in
359 *) ${ECHO} "==========================================================================="
360 ${ECHO} "The following groups are no longer being used by ${PKGNAME},"
361 ${ECHO} "and they can be removed if no other software is using them:"
363 ${ECHO} "$existing_groups" | listwrap 40
365 ${ECHO} "==========================================================================="
369 ${TEST} $? -eq 0 || exitcode=1
373 ${ECHO} "Usage: ./+USERGROUP ADD|REMOVE [metadatadir]"
374 ${ECHO} " ./+USERGROUP CHECK-ADD|CHECK-REMOVE [metadatadir]"
380 ${SED} -n "/^\# GROUP: /p" ${SELF} >> ./+USERGROUP
381 ${SED} -n "/^\# USER: /p" ${SELF} >> ./+USERGROUP
382 ${CHMOD} +x ./+USERGROUP