1 #!/bin/bash 2 #********************************************************************* 3 # Copyright (c) International Business Machines Corp., 2003, 2004 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 : su 20 # 21 # PURPOSE: Tests the basic functionality of `su`. 22 # 23 # SETUP: The program `/usr/bin/expect' MUST be installed. 24 # 25 # HISTORY: 26 # 03/03 Dustin Kirkland (dkirklan (at] us.ibm.com) 27 # 03/03 Jerone Young (jeroney (at] us.ibm.com) 28 # 10/01/04 Kris Wilson Port to Red Hat 29 # 30 #********************************************************************* 31 32 echo "This script contains bashism that needs to be fixed!" 33 34 if [ -z ${TCbin} ] 35 then 36 export TCbin=$PWD 37 fi 38 39 export TEST_USER1="su_usr1" 40 41 tvar=${MACHTYPE%-*} 42 tvar=${tvar#*-} 43 44 # need to export tvar for su01_s1 45 export tvar 46 printf "Machine type is: $tvar\n\n" 47 48 if [ "$tvar" = "redhat" -o "$tvar" = "redhat-linux" ] 49 # Need to also set group for TEST_USER2 50 then 51 export TEST_USER1_GROUP="wheel" 52 export TEST_USER2_GROUP="wheel" 53 else 54 export TEST_USER1_GROUP="trusted" 55 export TEST_USER2_GROUP="trusted" 56 fi 57 export TEST_USER1_PASSWD="eal" 58 export TEST_USER1_ENCRYPTED_PASSWD="42VmxaOByKwlA" 59 export TEST_USER1_NEW_PASSWD="a_very_good_and_long_password" 60 export TEST_USER1_HOMEDIR="/home/$TEST_USER1" 61 62 export TEST_USER2="su_usr2" 63 # Group needs to be trusted for Red Hat. 64 #export TEST_USER2_GROUP="trusted" 65 export TEST_USER2_PASSWD="eal" 66 export TEST_USER2_ENCRYPTED_PASSWD="42VmxaOByKwlA" 67 export TEST_USER2_HOMEDIR="/home/$TEST_USER2" 68 69 #This is for enviroment test 70 export TEST_LINE="YOU_HAVE_THE_VARIABLE" 71 export TEST_ENV_FILE="/tmp/TEST_ENV_FILE_ROOT" 72 export TEST_ENV_FILE_USER="/tmp/TEST_ENV_FILE_USER" 73 export TEST_ENV_FILE2="/tmp/TEST_ENV_FILE_ROOT2" 74 #----------------------------------------------------------------------- 75 # FUNCTION: do_setup 76 #----------------------------------------------------------------------- 77 78 do_setup(){ 79 80 #REMOVE ANY TEMPOARY FILES THAT MAY STILL BE AROUND 81 rm -f $TEST_ENV_FILE_USER > /dev/null 2>&1 82 rm -f $TEST_ENV_FILE2 > /dev/null 2>&1 83 rm -f $TEST_ENV_FILE > /dev/null 2>&1 84 85 86 #Create 1st test user 87 #erase user if he may exist , so we can have a clean en 88 rm -rf /home/$TEST_USER1 89 getent passwd $TEST_USER1 > /dev/null 2>&1 && userdel $TEST_USER1 90 sleep 1 91 92 useradd -m -g users $TEST_USER1 93 if [ $? != 0 ] 94 then { 95 echo "Could not add test user $TEST_USER1." 96 exit 1 97 } 98 fi 99 100 usermod -G users,$TEST_USER1_GROUP $TEST_USER1 101 102 #create users home directory (SLES 8 does not do this, even when specified in adduser) 103 # Only do this if not RH; RH creates the directory. 104 if [ "$tvar" != "redhat" -a "$tvar" != "redhat-linux" ] 105 then { 106 USER_UID=`id -u $TEST_USER1` 107 USER_GID=`id -g $TEST_USER1` 108 mkdir -p $TEST_USER1_HOMEDIR 109 chown -R $USER_UID.$USER_GID $TEST_USER1_HOMEDIR 110 } 111 fi 112 113 usermod -p $TEST_USER1_ENCRYPTED_PASSWD $TEST_USER1 > /dev/null 2>&1 114 if [ $? != 0 ] 115 then { 116 echo "Could not set password for test user $TEST_USER1" 117 exit 1 118 } 119 fi 120 121 #Create 2nd test user 122 #erase user if he may exist , so we can have a clean en 123 rm -rf /home/$TEST_USER2 124 getent passwd $TEST_USER2 > /dev/null 2>&1 && userdel $TEST_USER2 125 sleep 1 126 127 useradd -m -g users $TEST_USER2 128 129 if [ $? != 0 ] 130 then { 131 echo "Could not add test user $TEST_USER2." 132 exit 1 133 } 134 fi 135 136 usermod -G users,$TEST_USER2_GROUP $TEST_USER2 137 138 #create users home diretory (SLES 8 does not do this, even when specified in adduser) 139 # Only do this if not RH; RH creates the directory. 140 if [ "$tvar" != "redhat" -a "$tvar" != "redhat-linux" ] 141 then { 142 USER_UID=`id -u $TEST_USER2` 143 USER_GID=`id -g $TEST_USER2` 144 mkdir -p $TEST_USER2_HOMEDIR 145 chown -R $USER_UID.$USER_GID $TEST_USER2_HOMEDIR 146 } 147 fi 148 149 usermod -p $TEST_USER2_ENCRYPTED_PASSWD $TEST_USER2 > /dev/null 2>&1 150 if [ $? != 0 ] 151 then { 152 echo "Could not set password for test user $TEST_USER2" 153 exit 1 154 } 155 fi 156 } 157 158 159 #----------------------------------------------------------------------- 160 # FUNCTION: do_cleanup 161 #----------------------------------------------------------------------- 162 163 do_cleanup() { 164 rm -rf /home/$TEST_USER1 165 rm -rf /home/$TEST_USER2 166 userdel $TEST_USER1 167 userdel $TEST_USER2 168 #REMOVE ANY TEMPOARY FILES THAT MAY STILL BE AROUND 169 rm -f $TEST_ENV_FILE_USER > /dev/null 2>&1 170 rm -f $TEST_ENV_FILE2 > /dev/null 2>&1 171 rm -f $TEST_ENV_FILE > /dev/null 2>&1 172 } 173 174 #----------------------------------------------------------------------- 175 # FUNCTION: MAIN 176 #----------------------------------------------------------------------- 177 do_setup 178 /bin/su $TEST_USER1 -c ${TCbin}/su01_s1 179 EXIT_CODE=$? 180 do_cleanup 181 exit $EXIT_CODE 182