Fix the creation of the dumpdir directory in stress_floppy Makefile
[ltp-debian.git] / testcases / commands / cron / cron_deny01
blob89ca66c895c61c90a9b2ba28469ab656d66dd23c
1 #!/bin/sh
3 # Copyright (c) International Business Machines Corp., 2003
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.
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.
15 # You should have received a copy of the GNU General Public License
16 # along with this pronram; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # FILE: /var/spool/cron/allow
21 # PURPOSE: Test that /var/spool/cron/deny , does not allow those in the file to run cron jobs.
23 # HISTORY:
24 # 04/03 Jerone Young (jyoung5@us.ibm.com)
27 iam=`whoami`
29 tvar=${MACHTYPE%-*}
30 tvar=${tvar#*-}
32 if [ "$tvar" = "redhat" -o "$tvar" = "redhat-linux" ]
33 then
34 CRON_DENY="/etc/cron.deny"
35 CRON_ALLOW="/etc/cron.allow"
36 else
37 CRON_DENY="/var/spool/cron/deny"
38 CRON_ALLOW="/var/spool/cron/allow"
41 TEST_USER1="cd_user1"
42 TEST_USER1_HOME="/home/$TEST_USER1"
43 TEST_USER2="cd_user2"
44 TEST_USER2_HOME="/home/$TEST_USER2"
46 #-----------------------------------------------------------------------
47 # FUNCTION: do_setup
48 #-----------------------------------------------------------------------
50 do_setup() {
51 #move any files that may get in the way
52 rm /tmp/cron_deny_test > /dev/null 2>&1
53 rm /tmp/cron_deny_test1 > /dev/null 2>&1
55 mv $CRON_DENY $CRON_DENY.old > /dev/null 2>&1
56 mv $CRON_ALLOW $CRON_ALLOW.old > /dev/null 2>&1
58 #remove users for clean enviroment
59 su $TEST_USER1 -c "crontab -r"
60 su $TEST_USER2 -c "crontab -r"
61 rm -rf /home/$TEST_USER1
62 rm -rf /home/$TEST_USER2
63 userdel $TEST_USER1
64 userdel $TEST_USER2
65 sleep 1
67 #create 1st user
68 useradd -m -g users $TEST_USER1
69 if [ $? != 0 ]
70 then {
71 echo "Could not add test user $TEST_USER1 to system."
72 exit 1
76 #create 2nd user
77 useradd -m -g users $TEST_USER2
78 if [ $? != 0 ]
79 then {
80 echo "Could not add test user $TEST_USER2 to system."
81 exit 1
86 #-----------------------------------------------------------------------
87 # FUNCTION: do_cleanup
88 #-----------------------------------------------------------------------
89 do_cleanup(){
90 su $TEST_USER1 -c "crontab -r"
91 su $TEST_USER2 -c "crontab -r"
92 rm -rf /home/$TEST_USER1
93 rm -rf /home/$TEST_USER2
94 userdel $TEST_USER1
95 userdel $TEST_USER2
96 rm $CRON_DENY
97 mv $CRON_DENY.old $CRON_DENY > /dev/null 2>&1
98 mv $CRON_ALLOW.old $CRON_ALLOW > /dev/null 2>&1
99 rm /tmp/cron_allow_test >/dev/null 2>&1
102 #-----------------------------------------------------------------------
103 # FUNCTION: run_test
104 #-----------------------------------------------------------------------
105 run_test() {
107 if [ $iam = $TEST_USER1 ]
108 then
109 echo "TEST: $CRON_DENY should allow only allow those who are not in the file to
110 run cron jobs."
112 echo "(1) TEST THAT PERSON NOT IN $CRON_DENY IS ABLE TO RUN JOB."
114 crontab - << EOF
115 `date '+%M %H' | awk '{ORS=""; print ($1+2)%60" "$2" * * * "}'` echo "TEST JOB RAN" >> /tmp/cron_deny_test 2>&1
117 if [ $? != 0 ]; then
118 echo Error while adding crontab for user $TEST_USER1
119 exit 1
122 echo "sleeping for 130 seconds...."
123 sleep 130
125 EXIT_CODE=1
126 test -e /tmp/cron_deny_test && EXIT_CODE=0
128 if [ $EXIT_CODE = 1 ]; then
129 echo "Cron did not allow user to execute job , TEST FAILED"
130 else
131 echo "Cron allowed user to execute test job, TEST PASSED"
134 rm -f /tmp/cron_deny_test
136 exit $EXIT_CODE
139 if [ $iam = $TEST_USER2 ]
140 then
141 echo "(2) TEST THAT PERSON IN $CRON_DENY IS NOT ABLE TO RUN JOB."
143 crontab - << EOF
144 `date '+%M' | awk '{ORS=""; print ($1+2)%60 " * * * * "}'` echo "TEST JOB RAN" >> /tmp/cron_deny_test 2>&1
146 if [ $? != 0 ]; then
147 echo Error while adding crontab for user $TEST_USER2
150 echo "sleeping for 130 seconds...."
151 sleep 130
153 EXIT_CODE=0
154 test -e /tmp/cron_deny_test1 && EXIT_CODE=1
156 if [ $EXIT_CODE = 0 ]; then
157 echo "Cron did not allow user to execute job , TEST PASSED"
158 else
159 echo "Cron allowed user to execute test job, TEST FAILED"
162 rm -f /tmp/cron_deny_test1
164 exit $EXIT_CODE
169 #-----------------------------------------------------------------------
170 # FUNCTION: main
171 #-----------------------------------------------------------------------
172 if [ $iam = "root" ]
173 then
174 do_setup
175 echo $TEST_USER2 > $CRON_DENY
176 EXIT_CODE=0
177 su $TEST_USER1 -c "$0"
178 if [ $? != 0 ]
179 then
180 EXIT_CODE=1
182 su $TEST_USER2 -c "$0"
183 if [ $? != 0 ]
184 then EXIT_CODE=1
186 do_cleanup
187 exit $EXIT_CODE
188 else
189 run_test