Whamcloud - gitweb
add full support for remote operations.
[fs/lustre-release.git] / lustre / tests / sanity-sec.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # TODO: support rootsquash test
7 set -e
8
9 SRCDIR=`dirname $0`
10 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/../utils:$PATH:/sbin
11
12 ONLY=${ONLY:-"$*"}
13 ALWAYS_EXCEPT=${ALWAYS_EXCEPT:-""}
14 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
15
16 [ "$ALWAYS_EXCEPT$EXCEPT" ] && \
17         echo "Skipping tests: `echo $ALWAYS_EXCEPT $EXCEPT`"
18
19 TMP=${TMP:-/tmp}
20
21 LFS=${LFS:-lfs}
22 LCTL=${LCTL:-lctl}
23 RUNAS=${RUNAS:-runas}
24
25 log() {
26         echo "$*"
27         $LCTL mark "$*" 2> /dev/null || true
28 }
29
30 run_one() {
31         BEFORE=`date +%s`
32         log "== test $2= `date +%H:%M:%S` ($BEFORE)"
33         export TESTNAME=test_$1
34         test_$1 || error "exit with rc=$?"
35         unset TESTNAME
36         pass "($((`date +%s` - $BEFORE))s)"
37 }
38
39 build_test_filter() {
40         for O in $ONLY; do
41             eval ONLY_${O}=true
42         done
43         for E in $EXCEPT $ALWAYS_EXCEPT; do
44             eval EXCEPT_${E}=true
45         done
46 }
47
48 _basetest() {
49         echo $*
50 }
51
52 basetest() {
53         IFS=abcdefghijklmnopqrstuvwxyz _basetest $1
54 }
55
56 run_test() {
57          base=`basetest $1`
58          if [ "$ONLY" ]; then
59                  testname=ONLY_$1
60                  if [ ${!testname}x != x ]; then
61                         run_one $1 "$2"
62                         return $?
63                  fi
64                  testname=ONLY_$base
65                  if [ ${!testname}x != x ]; then
66                          run_one $1 "$2"
67                          return $?
68                  fi
69                  echo -n "."
70                  return 0
71         fi
72         testname=EXCEPT_$1
73         if [ ${!testname}x != x ]; then
74                  echo "skipping excluded test $1"
75                  return 0
76         fi
77         testname=EXCEPT_$base
78         if [ ${!testname}x != x ]; then
79                  echo "skipping excluded test $1 (base $base)"
80                  return 0
81         fi
82         run_one $1 "$2"
83         return $?
84 }
85
86 SANITYSECLOG=${SANITYSECLOG:-/tmp/sanity-sec.log}
87
88 [ "$SANITYSECLOG" ] && rm -f $SANITYSECLOG || true
89
90 error() { 
91         sysctl -w lustre.fail_loc=0
92         log "FAIL: $TESTNAME $@"
93         if [ "$SANITYSECLOG" ]; then
94                 echo "FAIL: $TESTNAME $@" >> $SANITYSECLOG
95         else
96                 exit 1
97         fi
98 }
99
100 pass() { 
101         echo PASS $@
102 }
103
104 mounted_lustre_filesystems() {
105         awk '($3 ~ "lustre" && $1 ~ ":") { print $2 }' /proc/mounts
106 }
107 MOUNT="`mounted_lustre_filesystems`"
108 if [ -z "$MOUNT" ]; then
109         formatall
110         setupall
111         MOUNT="`mounted_lustre_filesystems`"
112         [ -z "$MOUNT" ] && error "NAME=$NAME not mounted"
113         S_MOUNTED=yes
114 fi
115
116 [ `echo $MOUNT | wc -w` -gt 1 ] && error "NAME=$NAME mounted more than once"
117
118 DIR=${DIR:-$MOUNT}
119 [ -z "`echo $DIR | grep $MOUNT`" ] && echo "$DIR not in $MOUNT" && exit 99
120
121 if [ -z "`lsmod|grep mdt`" ]; then
122         echo "skipping $TESTNAME (remote MDT)"
123         exit 0
124 fi
125
126 LPROC=/proc/fs/lustre
127 ENABLE_IDENTITY=/usr/sbin/l_getidentity
128 DISABLE_IDENTITY=NONE
129 LOVNAME=`cat $LPROC/llite/*/lov/common_name | tail -n 1`
130 MDT=$(\ls $LPROC/mdt 2> /dev/null | grep -v num_refs | tail -n 1)
131 TSTDIR="$MOUNT/remote_user_dir"
132 LUSTRE_CONF_DIR=/etc/lustre
133 SETXID_CONF=$LUSTRE_CONF_DIR/setxid.conf
134 SETXID_CONF_BAK=$LUSTRE_CONF_DIR/setxid.conf.bak
135 IDENTITY_UPCALL=$LPROC/mdt/$MDT/identity_upcall
136 IDENTITY_UPCALL_BAK=`more $IDENTITY_UPCALL`
137 IDENTITY_FLUSH=$LPROC/mdt/$MDT/identity_flush
138 ROOTSQUASH_UID=$LPROC/mdt/$MDT/rootsquash_uid
139 ROOTSQUASH_GID=$LPROC/mdt/$MDT/rootsquash_gid
140 NOSQUASH_NIDS=$LPROC/mdt/$MDT/nosquash_nids
141 KRB5_REALM=`cat /etc/krb5.conf |grep default_realm| awk '{ print $3 }'`
142 USER1=`cat /etc/passwd|grep :500:|cut -d: -f1`
143 USER2=`cat /etc/passwd|grep :501:|cut -d: -f1`
144
145 if [ ! "$USER1" ]
146 then
147         echo "==========Please add user1 (uid=500)!=========="
148         error "==========Please add user1 (uid=500)!=========="
149         exit 0
150 fi
151
152 if [ ! "$USER2" ]
153 then
154         echo "==========Please add user2 (uid=501)!=========="
155         error "==========Please add user2 (uid=501)!=========="
156         exit 0
157 fi
158
159 build_test_filter
160
161 setup() {
162         if [ -f "$SETXID_CONF" ]
163         then
164                 mv -f $SETXID_CONF $SETXID_CONF_BAK
165         else
166                 rm -f $SETXID_CONF_BAK
167         fi
168         echo $ENABLE_IDENTITY > $IDENTITY_UPCALL
169         echo 1 > $IDENTITY_FLUSH
170         $RUNAS -u 500 ls $DIR
171         $RUNAS -u 501 ls $DIR
172 }
173 setup
174
175 # run as different user
176 test_0() {
177         rm -rf $DIR/d0
178         mkdir $DIR/d0
179
180         chown $USER1 $DIR/d0 || error
181         $RUNAS -u 500 ls $DIR || error
182         $RUNAS -u 500 touch $DIR/f0 && error
183         $RUNAS -u 500 touch $DIR/d0/f1 || error
184         $RUNAS -u 501 touch $DIR/d0/f2 && error
185         touch $DIR/d0/f3 || error
186         chown root $DIR/d0
187         chgrp $USER1 $DIR/d0
188         chmod 775 $DIR/d0
189         $RUNAS -u 500 touch $DIR/d0/f4 || error
190         $RUNAS -u 501 touch $DIR/d0/f5 && error
191         touch $DIR/d0/f6 || error
192
193         rm -rf $DIR/d0
194 }
195 run_test 0 "uid permission ============================="
196
197 # setuid/gid
198 test_1() {
199         rm -rf $DIR/d1
200         mkdir $DIR/d1
201
202         chown $USER1 $DIR/d1 || error
203         $RUNAS -u 501 -v 500 touch $DIR/d1/f0 && error
204         echo "* 501 setuid" > $SETXID_CONF
205         echo "enable uid 501 setuid"
206         echo 1 > $IDENTITY_FLUSH
207         $RUNAS -u 501 -v 500 touch $DIR/d1/f1 || error
208
209         chown root $DIR/d1
210         chgrp $USER1 $DIR/d1
211         chmod 770 $DIR/d1
212         $RUNAS -u 501 -g 501 touch $DIR/d1/f2 && error
213         echo "* 501 setuid,setgid" > $SETXID_CONF
214         echo "enable uid 501 setuid,setgid"
215         echo 1 > $IDENTITY_FLUSH
216         $RUNAS -u 501 -g 501 -j 500 touch $DIR/d1/f3 || error
217         $RUNAS -u 501 -v 500 -g 501 -j 500 touch $DIR/d1/f4 || error
218
219         rm -f $SETXID_CONF
220         rm -rf $DIR/d1
221         echo 1 > $IDENTITY_FLUSH
222 }
223 run_test 1 "setuid/gid ============================="
224
225 # lfs getfacl/setfacl
226 test_2() {
227         rm -rf $DIR/d2
228         mkdir $DIR/d2
229         chmod 755 $DIR/d2
230         echo xxx > $DIR/d2/f0
231         chmod 644 $DIR/d2/f0
232
233         $LFS getfacl $DIR/d2/f0 || error
234         $RUNAS -u 500 cat $DIR/d2/f0 || error
235         $RUNAS -u 500 touch $DIR/d2/f0 && error
236
237         $LFS setfacl -m u:$USER1:w $DIR/d2/f0 || error
238         $LFS getfacl $DIR/d2/f0 || error
239         echo "set user $USER1 write permission on file $DIR/d2/fo"
240         $RUNAS -u 500 touch $DIR/d2/f0 || error
241         $RUNAS -u 500 cat $DIR/d2/f0 && error
242
243         rm -rf $DIR/d2
244 }
245 run_test 2 "lfs getfacl/setfacl ============================="
246
247 # rootsquash
248 test_3() {
249         [ -n "$SEC" ] && echo "ignore rootsquash test for single node" && return
250
251         $LCTL conf_param $MDT.mdt.nosquash_nids=none
252         while grep LNET_NID_ANY $NOSQUASH_NIDS > /dev/null; do sleep 1; done
253         $LCTL conf_param $MDT.mdt.rootsquash_uid=0
254         while [ "`cat $ROOTSQUASH_UID`" -ne 0 ]; do sleep 1; done
255         $LCTL conf_param $MDT.mdt.rootsquash_gid=0
256         while [ "`cat $ROOTSQUASH_GID`" -ne 0 ]; do sleep 1; done
257
258         rm -rf $DIR/d3
259         mkdir $DIR/d3
260         chown $USER1 $DIR/d3
261         chmod 700 $DIR/d3
262         $LCTL conf_param $MDT.mdt.rootsquash_uid=500
263         echo "set rootsquash uid = 500"
264         while [ "`cat $ROOTSQUASH_UID`" -ne 500 ]; do sleep 1; done
265         touch $DIR/f3_0 && error
266         touch $DIR/d3/f3_1 || error
267
268         $LCTL conf_param $MDT.mdt.rootsquash_uid=0
269         echo "disable rootsquash"
270         while [ "`cat $ROOTSQUASH_UID`" -ne 0 ]; do sleep 1; done
271         chown root $DIR/d3
272         chgrp $USER2 $DIR/d3
273         chmod 770 $DIR/d3
274
275         $LCTL conf_param $MDT.mdt.rootsquash_uid=500
276         echo "set rootsquash uid = 500"
277         while [ "`cat $ROOTSQUASH_UID`" -ne 500 ]; do sleep 1; done
278         touch $DIR/d3/f3_2 && error
279         $LCTL conf_param $MDT.mdt.rootsquash_gid=501
280         echo "set rootsquash gid = 501"
281         while [ "`cat $ROOTSQUASH_GID`" -ne 501 ]; do sleep 1; done
282         touch $DIR/d3/f3_3 || error
283
284         $LCTL conf_param $MDT.mdt.nosquash_nids=*
285         echo "add host in rootsquash skip list"
286         while ! grep LNET_NID_ANY $NOSQUASH_NIDS > /dev/null;
287                 do sleep 1;
288         done
289         touch $DIR/f3_4 || error
290
291         $LCTL conf_param $MDT.mdt.rootsquash_uid=0
292         while [ "`cat $ROOTSQUASH_UID`" -ne 0 ]; do sleep 1; done
293         $LCTL conf_param $MDT.mdt.rootsquash_gid=0
294         while [ "`cat $ROOTSQUASH_GID`" -ne 0 ]; do sleep 1; done
295         $LCTL conf_param $MDT.mdt.nosquash_nids=none
296         rm -rf $DIR/d3
297         rm -f $DIR/f3_?
298 }
299 run_test 3 "rootsquash ============================="
300
301 # bug 3285 - supplementary group should always succeed (see do_init_ucred),
302 # NB: the supplementary groups are set for local client only, as for remote
303 # client, the groups of the specified uid on MDT will be obtained by
304 # upcall /sbin/l_getidentity and used.
305 test_4() {
306         mkdir $DIR/d4
307         chmod 771 $DIR/d4
308         chgrp 500 $DIR/d4
309         $RUNAS -u 500 -G1,2,500 ls $DIR/d4 || error "setgroups failed"
310         rm -rf $DIR/d4
311 }
312 run_test 4 "set supplementary group ==============="
313
314 log "cleanup: ======================================================"
315
316 unsetup() {
317         if [ -f "$SETXID_CONF_BAK" ]
318         then
319                 mv -f $SETXID_CONF_BAK $SETXID_CONF
320         fi
321         echo $IDENTITY_UPCALL_BAK > $IDENTITY_UPCALL
322         echo 1 > $IDENTITY_FLUSH
323         $RUNAS -u 500 ls $DIR
324         $RUNAS -u 501 ls $DIR
325 }
326 unsetup
327
328 if [ "$S_MOUNTED" = "yes" ]; then
329         cleanupall -f || error "cleanup failed"
330 fi
331
332 echo '=========================== finished ==============================='