blob: 7c3ef1db24d24c43a657fbe5e86757895d2ded18 [file] [log] [blame]
robbiew3f9a8802001-10-12 20:39:17 +00001#!/bin/sh
vapier4fdf37f2008-04-28 01:36:44 +00002#
robbiew599ddd72001-11-28 21:31:41 +00003# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
vapier4fdf37f2008-04-28 01:36:44 +000018#
robbiew599ddd72001-11-28 21:31:41 +000019# FILE : IDcheck.sh
20# DESCRIPTION : checks for req'd users/groups and will create them if requested.
vapier4fdf37f2008-04-28 01:36:44 +000021# HISTORY : see the cvs log
22#
robbiew599ddd72001-11-28 21:31:41 +000023
robbiewd23f7242001-11-21 17:33:40 +000024# Prompt user if ids/groups should be created
robbiewc0f37e62001-11-27 19:14:19 +000025echo "Checking for required user/group ids"
26echo ""
robbiewd23f7242001-11-21 17:33:40 +000027
vapier4fdf37f2008-04-28 01:36:44 +000028# Check ids and create if needed.
29NO_NOBODY_ID=1
30NO_BIN_ID=1
31NO_DAEMON_ID=1
32NO_NOBODY_GRP=1
33NO_BIN_GRP=1
34NO_DAEMON_GRP=1
35NO_USERS_GRP=1
36NO_SYS_GRP=1
robbiewd23f7242001-11-21 17:33:40 +000037
subrata_modakd9f329e2009-07-07 14:30:27 +000038group="$DESTDIR/etc/group"
39passwd="$DESTDIR/etc/passwd"
vapier4fdf37f2008-04-28 01:36:44 +000040
subrata_modakd9f329e2009-07-07 14:30:27 +000041# find entry.
42fe() {
yaberauneya3ab801e2009-07-10 23:01:27 +000043 ID=$1; shift
44 FILE=$1; shift
45 [ -e "$FILE" ] || return $?
46 awk "/^$ID:/ { FOUND=1 } END { if (\$FOUND == 1) { exit 1; } exit 0; }" \
47 "$FILE"
vapier4fdf37f2008-04-28 01:36:44 +000048}
49
50prompt_for_create() {
subrata_modakd9f329e2009-07-07 14:30:27 +000051 if [ -z "$CREATE_ENTRIES" ] ; then
vapier4fdf37f2008-04-28 01:36:44 +000052
subrata_modakd9f329e2009-07-07 14:30:27 +000053 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
subrata_modak4ede9d02008-09-29 18:12:16 +000054 echo -n "If any required user ids and/or groups are missing, would you like these created? [y/N]"
vapier4fdf37f2008-04-28 01:36:44 +000055 read ans
56 case "$ans" in
57 Y*|y*) CREATE_ENTRIES=1 ;;
58 *) CREATE_ENTRIES=0 ;;
59 esac
60 else
61 CREATE_ENTRIES=0
62 fi
63
64 fi
65}
66
subrata_modak694c3f02008-08-29 18:03:48 +000067if [ -z ${EUID} ] ; then
68 EUID=$(id -u)
69fi
70
yaberauneya3ab801e2009-07-10 23:01:27 +000071for i in "$passwd" "$group"; do
72 if [ -e "$i" -a ! -r "$i" ] ; then
73 echo "$i not readable by uid $EUID"
vapier4fdf37f2008-04-28 01:36:44 +000074 exit 1
yaberauneya3ab801e2009-07-10 23:01:27 +000075 fi
76done
robbiewd23f7242001-11-21 17:33:40 +000077
subrata_modakd9f329e2009-07-07 14:30:27 +000078fe bin "$passwd"; NO_BIN_ID=$?
79fe daemon "$passwd"; NO_DAEMON_ID=$?
80fe nobody "$passwd"; NO_NOBODY_ID=$?
vapier4fdf37f2008-04-28 01:36:44 +000081
subrata_modakd9f329e2009-07-07 14:30:27 +000082fe bin "$group"; NO_BIN_GRP=$?
83fe daemon "$group"; NO_DAEMON_GRP=$?
84fe nobody "$group"; NO_NOBODY_GRP=$?
85fe sys "$group"; NO_SYS_GRP=$?
86fe users "$group"; NO_USERS_GRP=$?
robbiewd23f7242001-11-21 17:33:40 +000087
vapier4fdf37f2008-04-28 01:36:44 +000088prompt_for_create
89
90debug_vals() {
91
92echo "Missing the following group / user entries:"
subrata_modakd9f329e2009-07-07 14:30:27 +000093echo "Group file: $group"
94echo "Password file: $passwd"
vapier4fdf37f2008-04-28 01:36:44 +000095echo "nobody: $NO_NOBODY_ID"
96echo "bin: $NO_BIN_ID"
97echo "daemon: $NO_DAEMON_ID"
98echo "nobody grp: $NO_NOBODY_GRP"
99echo "bin grp: $NO_BIN_GRP"
100echo "daemon grp: $NO_DAEMON_GRP"
101echo "sys grp: $NO_SYS_GRP"
102echo "users grp: $NO_USERS_GRP"
103echo ""
vapier4fdf37f2008-04-28 01:36:44 +0000104
105}
106
107#debug_vals
108
109if [ $CREATE_ENTRIES -ne 0 ] ; then
yaberauneya3ab801e2009-07-10 23:01:27 +0000110 if ! touch "$group" "$passwd" 2>/dev/null; then
111 echo "Failed to touch $group or $passwd"
112 exit 1
113 fi
robbiewd23f7242001-11-21 17:33:40 +0000114fi
115
vapier4fdf37f2008-04-28 01:36:44 +0000116make_user_group() {
117 local name=$1 id=$2 no_id=$3 no_grp=$4
118
119 if [ $no_id -eq 0 -a $no_grp -eq 0 ] ; then
120 echo "'$name' user id and group found."
121 elif [ $CREATE_ENTRIES -ne 0 ] ; then
122 echo "Creating entries for $name"
123
124 # Avoid chicken and egg issue with id(1) call
125 # made above and below.
subrata_modakd9f329e2009-07-07 14:30:27 +0000126 if ! fe "$name" "$passwd" && [ $no_id -ne 0 ] ; then
127 echo "${name}:x:${id}:${id}:${name}::" >> "$passwd"
vapier4fdf37f2008-04-28 01:36:44 +0000128 fi
129 if [ $no_grp -ne 0 ] ; then
subrata_modakd9f329e2009-07-07 14:30:27 +0000130 echo "${name}:x:$(id -u ${name}):" >> "$group"
vapier4fdf37f2008-04-28 01:36:44 +0000131 fi
132 fi
133}
134make_user_group nobody 99 $NO_NOBODY_ID $NO_NOBODY_GRP
135make_user_group bin 1 $NO_BIN_ID $NO_BIN_GRP
136make_user_group daemon 2 $NO_DAEMON_ID $NO_DAEMON_GRP
137
138if [ $NO_USERS_GRP -eq 0 ] ; then
139 echo "Users group found."
140elif [ $CREATE_ENTRIES -ne 0 ] ; then
subrata_modakd9f329e2009-07-07 14:30:27 +0000141 echo 'users:x:100:' >> "$group"
robbiewd23f7242001-11-21 17:33:40 +0000142fi
143
vapier4fdf37f2008-04-28 01:36:44 +0000144if [ $NO_SYS_GRP -eq 0 ] ; then
145 echo "Sys group found."
146elif [ $CREATE_ENTRIES -ne 0 ] ; then
subrata_modakd9f329e2009-07-07 14:30:27 +0000147 echo 'sys:x:3:' >> "$group"
robbiewd23f7242001-11-21 17:33:40 +0000148fi
149
subrata_modakd9f329e2009-07-07 14:30:27 +0000150MISSING_ENTRY=0
151
152# For entries that exist in both $group and $passwd.
153for i in nobody bin daemon; do
154 for file in "$group" "$passwd"; do
155 if ! fe "$i" "$file"; then
156 MISSING_ENTRY=1
157 break
158 fi
159 done
160 if [ $MISSING_ENTRY -ne 0 ]; then
161 break
162 fi
163done
164
165# For entries that only exist in $group.
166for i in users sys; do
167 if ! fe "$i" "$file"; then
168 MISSING_ENTRY=1
169 fi
170done
171
172if [ $MISSING_ENTRY -eq 0 ] ; then
173 echo "Required users/groups exist."
174 exit 0
robbiew06e3bdf2003-04-08 15:23:33 +0000175fi
176
vapier4fdf37f2008-04-28 01:36:44 +0000177echo ""
robbiewd23f7242001-11-21 17:33:40 +0000178echo "*****************************************"
179echo "* Required users/groups do NOT exist!!! *"
180echo "* *"
181echo "* Some kernel/syscall tests will FAIL! *"
182echo "*****************************************"
183exit 1