blob: 9a941ddf76eccd44046ec1a41895890c33e9bcf6 [file] [log] [blame]
robbiewfec8b662004-04-21 16:51:54 +00001#!/bin/sh
2#
3# Copyright (c) International Business Machines Corp., 2003
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 pronram; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18#
19# FILE: /var/spool/cron/allow
20#
21# PURPOSE: Test that /var/spool/cron/allow , only allows those in the file to run cron jobs.
22#
23# HISTORY:
24# 04/03 Jerone Young (jyoung5@us.ibm.com)
25#
26
27iam=`whoami`
28
mridge8a9c7452004-07-29 21:12:43 +000029tvar=${MACHTYPE%-*}
30tvar=${tvar#*-}
31
subrata_modak97544a12009-10-26 15:07:57 +000032if [ "$tvar" = "redhat" -o "$tvar" = "redhat-linux" ]
mridge8a9c7452004-07-29 21:12:43 +000033then
34CRON_ALLOW="/etc/cron.allow"
35else
robbiewfec8b662004-04-21 16:51:54 +000036CRON_ALLOW="/var/spool/cron/allow"
mridge8a9c7452004-07-29 21:12:43 +000037fi
robbiewfec8b662004-04-21 16:51:54 +000038
39TEST_USER1="ca_user1"
40TEST_USER1_HOME="/home/$TEST_USER1"
41TEST_USER2="ca_user2"
42TEST_USER2_HOME="/home/$TEST_USER2"
43
44#-----------------------------------------------------------------------
45# FUNCTION: do_setup
46#-----------------------------------------------------------------------
47
48do_setup() {
49 #move any files that may get in the way
subrata_modak6d561072009-07-08 17:53:47 +000050 rm /tmp/cron_allow_test > /dev/null 2>&1
51 rm /tmp/cron_allow_test1 > /dev/null 2>&1
52 mv $CRON_ALLOW $CRON_ALLOW.old > /dev/null 2>&1
robbiewfec8b662004-04-21 16:51:54 +000053
54 #remove users for clean enviroment
subrata_modak261ad292009-02-23 07:20:05 +000055 su $TEST_USER1 -c "crontab -r"
56 su $TEST_USER2 -c "crontab -r"
robbiewfec8b662004-04-21 16:51:54 +000057 rm -rf /home/$TEST_USER1
58 rm -rf /home/$TEST_USER2
59 userdel $TEST_USER1
60 userdel $TEST_USER2
61 sleep 1
62
63#create 1st user
64 useradd -m -g users $TEST_USER1
65 if [ $? != 0 ]
66 then {
67 echo "Could not add test user $TEST_USER1 to system."
68 exit 1
69 }
70 fi
71
72#create 2nd user
73 useradd -m -g users $TEST_USER2
74 if [ $? != 0 ]
75 then {
76 echo "Could not add test user $TEST_USER2 to system."
77 exit 1
78 }
79 fi
80}
81
82#-----------------------------------------------------------------------
83# FUNCTION: do_cleanup
84#-----------------------------------------------------------------------
85do_cleanup(){
subrata_modak261ad292009-02-23 07:20:05 +000086 su $TEST_USER1 -c "crontab -r"
87 su $TEST_USER2 -c "crontab -r"
robbiewfec8b662004-04-21 16:51:54 +000088 rm -rf /home/$TEST_USER1
89 rm -rf /home/$TEST_USER2
90 userdel $TEST_USER1
91 userdel $TEST_USER2
92 rm $CRON_ALLOW
subrata_modak6d561072009-07-08 17:53:47 +000093 mv $CRON_ALLOW.old $CRON_ALLOW > /dev/null 2>&1
94 rm /tmp/cron_allow_test >/dev/null 2>&1
robbiewfec8b662004-04-21 16:51:54 +000095}
96
97#-----------------------------------------------------------------------
98# FUNCTION: run_test
99#-----------------------------------------------------------------------
100run_test() {
101
102if [ $iam = $TEST_USER1 ]
103then
104 echo "TEST: $CRON_ALLOW should only allow those in the file to
105run cron jobs."
106
107 echo "(1) TEST THAT PERSON IN $CRON_ALLOW IS ABLE TO RUN JOB."
108
109 echo "backup crontab...."
110 crontab -l | grep '^[^#]' > /tmp/crontab-cronallow-save-$iam
111
112 crontab - << EOF
subrata_modakeb599742008-09-05 08:43:26 +0000113 `date '+%M' | awk '{ORS=""; print ($1+2)%60 " * * * * "}'` echo "TEST JOB RAN" >> /tmp/cron_allow_test 2>&1
robbiewfec8b662004-04-21 16:51:54 +0000114EOF
115 if [ $? != 0 ]; then
116 echo Error while adding crontab for user $TEST_USER1
117 exit 1
118 fi
119
120 echo "sleeping for 130 seconds...."
121 sleep 130
122
123 EXIT_CODE=1
124 test -e /tmp/cron_allow_test && EXIT_CODE=0
125
126 if [ $EXIT_CODE = 1 ]; then
127 echo "Cron did not allow user to execute job , TEST FAILED"
128 else
subrata_modak7e525f62009-08-23 06:31:45 +0000129 echo "Cron allowed user to execute test job, TEST PASSED"
robbiewfec8b662004-04-21 16:51:54 +0000130 fi
131
132 echo "restore old crontab..."
133 crontab /tmp/crontab-cronallow-save-$iam
134 rm -f /tmp/crontab-cronallow-save-$iam
135
136
137 rm -f /tmp/cron_allow_test
138
139 exit $EXIT_CODE
140fi
141
142if [ $iam = $TEST_USER2 ]
143then
144 echo "(2) TEST THAT PERSON NOT IN $CRON_ALLOW IS NOT ABLE TO RUN JOB."
145
146 echo "backup crontab...."
147 crontab -l | grep '^[^#]' > /tmp/crontab-cronallow-save-$iam
148
149 crontab - << EOF
subrata_modakeb599742008-09-05 08:43:26 +0000150 `date '+%M' | awk '{ORS=""; print ($1+2)%60 " * * * * "}'` echo "TEST JOB RAN" >> /tmp/cron_allow_test1 2>&1
robbiewfec8b662004-04-21 16:51:54 +0000151EOF
152 if [ $? != 0 ]; then
153 echo Error while adding crontab for user $TEST_USER2
154 fi
155
156 echo "sleeping for 130 seconds...."
157 sleep 130
158
159 EXIT_CODE=0
160 test -e /tmp/cron_allow_test1 && EXIT_CODE=1
161
162 if [ $EXIT_CODE = 0 ]; then
163 echo "Cron did not allow user to execute job , TEST PASSED"
164 else
subrata_modak7e525f62009-08-23 06:31:45 +0000165 echo "Cron allowed user to execute test job, TEST FAILED"
robbiewfec8b662004-04-21 16:51:54 +0000166 fi
167
168 echo "restore old crontab..."
169 crontab /tmp/crontab-cronallow-save-$iam
170 rm -f /tmp/crontab-cronallow-save-$iam
171
172 rm -f /tmp/cron_allow_test1
173
174 exit $EXIT_CODE
175fi
176
177}
178
179#-----------------------------------------------------------------------
180# FUNCTION: main
181#-----------------------------------------------------------------------
182if [ $iam = "root" ]
183then
184 do_setup
185 echo $TEST_USER1 > $CRON_ALLOW
186 EXIT_CODE=0
187 su $TEST_USER1 -c "$0"
188 if [ $? != 0 ]
189 then
190 EXIT_CODE=1
191 fi
192 su $TEST_USER2 -c "$0"
193 if [ $? != 0 ]
194 then EXIT_CODE=1
195 fi
196 do_cleanup
197 exit $EXIT_CODE
198else
199 run_test
200fi