xref: /aosp_15_r20/external/ltp/IDcheck.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2#
3#    Copyright (c) International Business Machines  Corp., 2001
4#
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.
9#
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.
14#
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18#
19#   FILE        : IDcheck.sh
20#   DESCRIPTION : checks for req'd users/groups and will create them if requested.
21#   HISTORY     : see the cvs log
22#
23
24# Prompt user if ids/groups should be created
25echo "Checking for required user/group ids"
26echo ""
27
28# Check ids and create if needed.
29NO_ROOT_ID=1
30NO_NOBODY_ID=1
31NO_BIN_ID=1
32NO_DAEMON_ID=1
33NO_ROOT_GRP=1
34NO_NOBODY_GRP=1
35NO_BIN_GRP=1
36NO_DAEMON_GRP=1
37NO_USERS_GRP=1
38NO_SYS_GRP=1
39
40group="$DESTDIR/etc/group"
41passwd="$DESTDIR/etc/passwd"
42
43# find entry.
44fe() {
45    ID=$1
46    FILE=$2
47    [ -e "$FILE" ] || return $?
48    grep -q "^$ID:" "$FILE"
49}
50
51prompt_for_create() {
52	if [ -z "$CREATE_ENTRIES" ] ; then
53
54		if [ $NO_ROOT_ID -ne 0 -o $NO_NOBODY_ID -ne 0 -o $NO_BIN_ID -ne 0 -o $NO_DAEMON_ID -ne 0 -o $NO_ROOT_GRP -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
55			echo -n "If any required user ids and/or groups are missing, would you like these created? [y/N]"
56			read ans
57			case "$ans" in
58			[Yy]*) CREATE_ENTRIES=1 ;;
59			*)     CREATE_ENTRIES=0 ;;
60			esac
61		else
62			CREATE_ENTRIES=0
63		fi
64
65	fi
66}
67
68if [ -z ${EUID} ] ; then
69	EUID=$(id -u)
70fi
71
72for i in "$passwd" "$group"; do
73    if [ -e "$i" -a ! -r "$i" ] ; then
74	echo "$i not readable by uid $EUID"
75	exit 1
76    fi
77done
78
79fe root "$passwd"; NO_ROOT_ID=$?
80fe bin "$passwd"; NO_BIN_ID=$?
81fe daemon "$passwd"; NO_DAEMON_ID=$?
82fe nobody "$passwd"; NO_NOBODY_ID=$?
83
84fe root "$group"; NO_ROOT_GRP=$?
85fe bin "$group"; NO_BIN_GRP=$?
86fe daemon "$group"; NO_DAEMON_GRP=$?
87fe nobody "$group" || fe nogroup "$group"; NO_NOBODY_GRP=$?
88fe sys "$group"; NO_SYS_GRP=$?
89fe users "$group"; NO_USERS_GRP=$?
90
91prompt_for_create
92
93debug_vals() {
94
95echo "Missing the following group / user entries:"
96echo "Group file:		$group"
97echo "Password file:		$passwd"
98echo "root			$NO_ROOT_ID"
99echo "nobody:			$NO_NOBODY_ID"
100echo "bin:			$NO_BIN_ID"
101echo "daemon:			$NO_DAEMON_ID"
102echo "root grp:			$NO_ROOT_GRP"
103echo "nobody[/nogroup] grp:	$NO_NOBODY_GRP"
104echo "bin grp:			$NO_BIN_GRP"
105echo "daemon grp:		$NO_DAEMON_GRP"
106echo "sys grp:			$NO_SYS_GRP"
107echo "users grp:		$NO_USERS_GRP"
108echo ""
109
110}
111
112#debug_vals
113
114if [ $CREATE_ENTRIES -ne 0 ] ; then
115    if ! touch "$group" "$passwd" 2>/dev/null; then
116        echo "Failed to touch $group or $passwd"
117        exit 1
118    fi
119fi
120
121make_user_group() {
122	local name=$1 id=$2 no_id=$3 no_grp=$4
123
124	if [ $no_id -eq 0 -a $no_grp -eq 0 ] ; then
125		echo "'$name' user id and group found."
126	elif [ $CREATE_ENTRIES -ne 0 ] ; then
127		echo "Creating entries for $name"
128
129		# Avoid chicken and egg issue with id(1) call
130		# made above and below.
131		if ! fe "$name" "$passwd" && [ $no_id -ne 0 ] ; then
132			echo "${name}:x:${id}:${id}:${name}::" >> "$passwd"
133		fi
134		if [ $no_grp -ne 0 ] ; then
135			echo "${name}:x:$(id -u ${name}):" >> "$group"
136		fi
137	fi
138}
139make_user_group root 0 $NO_ROOT_ID $NO_ROOT_GRP
140make_user_group nobody 65534 $NO_NOBODY_ID $NO_NOBODY_GRP
141make_user_group bin 1 $NO_BIN_ID $NO_BIN_GRP
142make_user_group daemon 2 $NO_DAEMON_ID $NO_DAEMON_GRP
143
144if [ $NO_USERS_GRP -eq 0 ] ; then
145	echo "Users group found."
146elif [ $CREATE_ENTRIES -ne 0 ] ; then
147	echo 'users:x:100:' >> "$group"
148fi
149
150if [ $NO_SYS_GRP -eq 0 ] ; then
151	echo "Sys group found."
152elif [ $CREATE_ENTRIES -ne 0 ] ; then
153	echo 'sys:x:3:' >> "$group"
154fi
155
156MISSING_ENTRY=0
157
158# For entries that exist in both $group and $passwd.
159for i in root bin daemon; do
160    for file in "$group" "$passwd"; do
161        if ! fe "$i" "$file"; then
162            MISSING_ENTRY=1
163            break
164        fi
165    done
166    if [ $MISSING_ENTRY -ne 0 ]; then
167        break
168    fi
169done
170
171# nobody is a standard group on all distros, apart from debian based ones;
172# let's account for the fact that they use the nogroup group instead.
173if ! fe "nobody" "$passwd" || ! (fe "nogroup" "$group" || fe "nobody" "$group")
174then
175    MISSING_ENTRY=1
176fi
177
178# For entries that only exist in $group.
179for i in users sys; do
180    if ! fe "$i" "$group" ; then
181        MISSING_ENTRY=1
182    fi
183done
184
185if [ $MISSING_ENTRY -eq 0 ] ; then
186    echo "Required users/groups exist."
187    exit 0
188fi
189
190echo ""
191echo "*****************************************"
192echo "* Required users/groups do NOT exist!!! *"
193echo "*                                       *"
194echo "* Some kernel/syscall tests will FAIL!  *"
195echo "*****************************************"
196exit 1
197