blob: fc2c9a0e803d3ea61d4ff55a814fd0bb63198510 [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 +000025clear
26echo "Checking for required user/group ids"
27echo ""
robbiewd23f7242001-11-21 17:33:40 +000028
vapier4fdf37f2008-04-28 01:36:44 +000029# Check ids and create if needed.
30NO_NOBODY_ID=1
31NO_BIN_ID=1
32NO_DAEMON_ID=1
33NO_NOBODY_GRP=1
34NO_BIN_GRP=1
35NO_DAEMON_GRP=1
36NO_USERS_GRP=1
37NO_SYS_GRP=1
robbiewcefffa72003-03-03 17:06:24 +000038I_AM_ROOT=0
robbiewd23f7242001-11-21 17:33:40 +000039
vapier4fdf37f2008-04-28 01:36:44 +000040#
41# id(1) for entry.
42#
43ife() {
44 id "$@" > /dev/null
45}
46
47#
48# grep(1) for entry.
49#
50gfe() {
51 grep -q "$@"
52}
53
54prompt_for_create() {
55 if [ -n "$CREATE_ENTRIES" ] ; then
56
57 if [ $I_AM_ROOT -eq 0 ] ; then
58 echo "Not root; can't create user / group entries on local machine".
59 CREATE_ENTRIES=0
60 fi
61 echo "CREATE_ENTRIES variable set to $CREATE_ENTRIES ..."
62 echo
63
64 else
65
66 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 -a $I_AM_ROOT -ne 0 ] ; then
subrata_modak4ede9d02008-09-29 18:12:16 +000067 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 +000068 read ans
69 case "$ans" in
70 Y*|y*) CREATE_ENTRIES=1 ;;
71 *) CREATE_ENTRIES=0 ;;
72 esac
73 else
74 CREATE_ENTRIES=0
75 fi
76
77 fi
78}
79
subrata_modak694c3f02008-08-29 18:03:48 +000080if [ -z ${EUID} ] ; then
81 EUID=$(id -u)
82fi
83
vapier4fdf37f2008-04-28 01:36:44 +000084if [ -e /etc/passwd -a ! -r /etc/passwd ] ; then
85 echo "/etc/passwd not readable by uid $EUID"
86 exit 1
87elif [ -e /etc/group -a ! -r /etc/group ] ; then
88 echo "/etc/group not readable by uid $EUID"
89 exit 1
robbiewd23f7242001-11-21 17:33:40 +000090fi
91
vapier4fdf37f2008-04-28 01:36:44 +000092ife bin; NO_BIN_ID=$?
93ife daemon; NO_DAEMON_ID=$?
94ife nobody; NO_NOBODY_ID=$?
95
96gfe '^bin:' /etc/group; NO_BIN_GRP=$?
97gfe '^daemon:' /etc/group; NO_DAEMON_GRP=$?
98gfe '^nobody:' /etc/group; NO_NOBODY_GRP=$?
99gfe '^sys:' /etc/group; NO_SYS_GRP=$?
100gfe '^users:' /etc/group; NO_USERS_GRP=$?
101
102if [ $EUID -eq 0 ] ; then
103 I_AM_ROOT=1
robbiewd23f7242001-11-21 17:33:40 +0000104fi
105
vapier4fdf37f2008-04-28 01:36:44 +0000106prompt_for_create
107
108debug_vals() {
109
110echo "Missing the following group / user entries:"
111echo "nobody: $NO_NOBODY_ID"
112echo "bin: $NO_BIN_ID"
113echo "daemon: $NO_DAEMON_ID"
114echo "nobody grp: $NO_NOBODY_GRP"
115echo "bin grp: $NO_BIN_GRP"
116echo "daemon grp: $NO_DAEMON_GRP"
117echo "sys grp: $NO_SYS_GRP"
118echo "users grp: $NO_USERS_GRP"
119echo ""
120echo "i am root: $I_AM_ROOT"
121echo ""
122
123}
124
125#debug_vals
126
127if [ $CREATE_ENTRIES -ne 0 ] ; then
128 if ! touch /etc/group ; then
129 echo "Couldn't touch /etc/group"
130 exit 1
131 fi
robbiewd23f7242001-11-21 17:33:40 +0000132fi
133
vapier4fdf37f2008-04-28 01:36:44 +0000134make_user_group() {
135 local name=$1 id=$2 no_id=$3 no_grp=$4
136
137 if [ $no_id -eq 0 -a $no_grp -eq 0 ] ; then
138 echo "'$name' user id and group found."
139 elif [ $CREATE_ENTRIES -ne 0 ] ; then
140 echo "Creating entries for $name"
141
142 # Avoid chicken and egg issue with id(1) call
143 # made above and below.
144 if ! gfe "^${name}:" /etc/passwd && [ $no_id -ne 0 ] ; then
145 echo "${name}:x:${id}:${id}:${name}::" >> /etc/passwd
146 fi
147 if [ $no_grp -ne 0 ] ; then
148 echo "${name}:x:$(id -u ${name}):" >> /etc/group
149 fi
150 fi
151}
152make_user_group nobody 99 $NO_NOBODY_ID $NO_NOBODY_GRP
153make_user_group bin 1 $NO_BIN_ID $NO_BIN_GRP
154make_user_group daemon 2 $NO_DAEMON_ID $NO_DAEMON_GRP
155
156if [ $NO_USERS_GRP -eq 0 ] ; then
157 echo "Users group found."
158elif [ $CREATE_ENTRIES -ne 0 ] ; then
159 echo 'users:x:100:' >> /etc/group
robbiewd23f7242001-11-21 17:33:40 +0000160fi
161
vapier4fdf37f2008-04-28 01:36:44 +0000162if [ $NO_SYS_GRP -eq 0 ] ; then
163 echo "Sys group found."
164elif [ $CREATE_ENTRIES -ne 0 ] ; then
165 echo 'sys:x:3:' >> /etc/group
robbiewd23f7242001-11-21 17:33:40 +0000166fi
167
vapier4fdf37f2008-04-28 01:36:44 +0000168if ife nobody && ife bin && ife daemon &&
169 ife -g nobody && ife -g bin && ife -g daemon &&
subrata_modak4ede9d02008-09-29 18:12:16 +0000170 gfe '^users:' /etc/group && gfe '^sys:' /etc/group &&
171 gfe '^nobody:' /etc/group
vapier4fdf37f2008-04-28 01:36:44 +0000172then
173 echo ""
174 echo "Required users/groups exist."
175 exit 0
robbiew06e3bdf2003-04-08 15:23:33 +0000176fi
177
vapier4fdf37f2008-04-28 01:36:44 +0000178echo ""
robbiewd23f7242001-11-21 17:33:40 +0000179echo "*****************************************"
180echo "* Required users/groups do NOT exist!!! *"
181echo "* *"
182echo "* Some kernel/syscall tests will FAIL! *"
183echo "*****************************************"
184exit 1