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