Add new and missing, remove deleted files
[ltp-debian.git] / IDcheck.sh
blob38f6d322143db5c85cdbbc368aad033bb1aaebf4
1 #!/bin/sh
3 # Copyright (c) International Business Machines Corp., 2001
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 # the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # FILE : IDcheck.sh
20 # DESCRIPTION : checks for req'd users/groups and will create them if requested.
21 # HISTORY : see the cvs log
24 # debian is good enough
26 exit 0
28 # Prompt user if ids/groups should be created
29 echo "Checking for required user/group ids"
30 echo ""
32 # Check ids and create if needed.
33 NO_NOBODY_ID=1
34 NO_BIN_ID=1
35 NO_DAEMON_ID=1
36 NO_NOBODY_GRP=1
37 NO_BIN_GRP=1
38 NO_DAEMON_GRP=1
39 NO_USERS_GRP=1
40 NO_SYS_GRP=1
42 group="$DESTDIR/etc/group"
43 passwd="$DESTDIR/etc/passwd"
45 # find entry.
46 fe() {
47 ID=$1; shift
48 FILE=$1; shift
49 [ -e "$FILE" ] || return $?
50 awk "/^$ID:/ { FOUND=1 } END { if (\$FOUND == 1) { exit 1; } exit 0; }" \
51 "$FILE"
54 prompt_for_create() {
55 if [ -z "$CREATE_ENTRIES" ] ; then
57 if [ $NO_NOBODY_ID -ne 0 -o $NO_BIN_ID -ne 0 -o $NO_DAEMON_ID -ne 0 -o $NO_NOBODY_GRP -ne 0 -o $NO_BIN_GRP -ne 0 -o $NO_DAEMON_GRP -ne 0 -o $NO_USERS_GRP -ne 0 -o $NO_SYS_GRP -ne 0 ] ; then
58 echo -n "If any required user ids and/or groups are missing, would you like these created? [y/N]"
59 read ans
60 case "$ans" in
61 Y*|y*) CREATE_ENTRIES=1 ;;
62 *) CREATE_ENTRIES=0 ;;
63 esac
64 else
65 CREATE_ENTRIES=0
71 if [ -z ${EUID} ] ; then
72 EUID=$(id -u)
75 for i in "$passwd" "$group"; do
76 if [ -e "$i" -a ! -r "$i" ] ; then
77 echo "$i not readable by uid $EUID"
78 exit 1
80 done
82 fe bin "$passwd"; NO_BIN_ID=$?
83 fe daemon "$passwd"; NO_DAEMON_ID=$?
84 fe nobody "$passwd"; NO_NOBODY_ID=$?
86 fe bin "$group"; NO_BIN_GRP=$?
87 fe daemon "$group"; NO_DAEMON_GRP=$?
88 fe nobody "$group"; NO_NOBODY_GRP=$?
89 fe sys "$group"; NO_SYS_GRP=$?
90 fe users "$group"; NO_USERS_GRP=$?
92 prompt_for_create
94 debug_vals() {
96 echo "Missing the following group / user entries:"
97 echo "Group file: $group"
98 echo "Password file: $passwd"
99 echo "nobody: $NO_NOBODY_ID"
100 echo "bin: $NO_BIN_ID"
101 echo "daemon: $NO_DAEMON_ID"
102 echo "nobody grp: $NO_NOBODY_GRP"
103 echo "bin grp: $NO_BIN_GRP"
104 echo "daemon grp: $NO_DAEMON_GRP"
105 echo "sys grp: $NO_SYS_GRP"
106 echo "users grp: $NO_USERS_GRP"
107 echo ""
111 #debug_vals
113 if [ $CREATE_ENTRIES -ne 0 ] ; then
114 if ! touch "$group" "$passwd" 2>/dev/null; then
115 echo "Failed to touch $group or $passwd"
116 exit 1
120 make_user_group() {
121 local name=$1 id=$2 no_id=$3 no_grp=$4
123 if [ $no_id -eq 0 -a $no_grp -eq 0 ] ; then
124 echo "'$name' user id and group found."
125 elif [ $CREATE_ENTRIES -ne 0 ] ; then
126 echo "Creating entries for $name"
128 # Avoid chicken and egg issue with id(1) call
129 # made above and below.
130 if ! fe "$name" "$passwd" && [ $no_id -ne 0 ] ; then
131 echo "${name}:x:${id}:${id}:${name}::" >> "$passwd"
133 if [ $no_grp -ne 0 ] ; then
134 echo "${name}:x:$(id -u ${name}):" >> "$group"
138 make_user_group nobody 99 $NO_NOBODY_ID $NO_NOBODY_GRP
139 make_user_group bin 1 $NO_BIN_ID $NO_BIN_GRP
140 make_user_group daemon 2 $NO_DAEMON_ID $NO_DAEMON_GRP
142 if [ $NO_USERS_GRP -eq 0 ] ; then
143 echo "Users group found."
144 elif [ $CREATE_ENTRIES -ne 0 ] ; then
145 echo 'users:x:100:' >> "$group"
148 if [ $NO_SYS_GRP -eq 0 ] ; then
149 echo "Sys group found."
150 elif [ $CREATE_ENTRIES -ne 0 ] ; then
151 echo 'sys:x:3:' >> "$group"
154 MISSING_ENTRY=0
156 # For entries that exist in both $group and $passwd.
157 for i in nobody bin daemon; do
158 for file in "$group" "$passwd"; do
159 if ! fe "$i" "$file"; then
160 MISSING_ENTRY=1
161 break
163 done
164 if [ $MISSING_ENTRY -ne 0 ]; then
165 break
167 done
169 # For entries that only exist in $group.
170 for i in users sys; do
171 if ! fe "$i" "$file"; then
172 MISSING_ENTRY=1
174 done
176 if [ $MISSING_ENTRY -eq 0 ] ; then
177 echo "Required users/groups exist."
178 exit 0
181 echo ""
182 echo "*****************************************"
183 echo "* Required users/groups do NOT exist!!! *"
184 echo "* *"
185 echo "* Some kernel/syscall tests will FAIL! *"
186 echo "*****************************************"
187 exit 1