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