Whamcloud - gitweb
b=23049 set path to truncate
[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
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10 # bug number for skipped test: 19430 19967 19967
11 ALWAYS_EXCEPT="                2     5     6    $SANITY_SEC_EXCEPT"
12 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
13
14 [ "$ALWAYS_EXCEPT$EXCEPT" ] && \
15     echo "Skipping tests: $ALWAYS_EXCEPT $EXCEPT"
16
17 SRCDIR=`dirname $0`
18 export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/../utils:$PATH:/sbin
19 export NAME=${NAME:-local}
20
21 LUSTRE=${LUSTRE:-`dirname $0`/..} 
22 . $LUSTRE/tests/test-framework.sh
23 init_test_env $@
24 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
25 init_logging
26
27 RUNAS="runas"
28
29 WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
30
31 CONFDIR=/etc/lustre
32 PERM_CONF=$CONFDIR/perm.conf
33 SANITYSECLOG=${TESTSUITELOG:-$TMP/$(basename $0 .sh).log}
34 FAIL_ON_ERROR=false
35
36 require_dsh_mds || exit 0
37 require_dsh_ost || exit 0
38
39 ID0=${ID0:-500}
40 ID1=${ID1:-501}
41 USER0=`cat /etc/passwd|grep :$ID0:$ID0:|cut -d: -f1`
42 USER1=`cat /etc/passwd|grep :$ID1:$ID1:|cut -d: -f1`
43
44 [ -z "$USER0" ] && \
45         echo "Please add user0 (uid=$ID0 gid=$ID0)! Skip sanity-sec" && exit 0
46
47 [ -z "$USER1" ] && \
48         echo "Please add user1 (uid=$ID1 gid=$ID1)! Skip sanity-sec" && exit 0
49
50 check_and_setup_lustre
51
52 sec_cleanup() {
53         if [ "$I_MOUNTED" = "yes" ]; then
54                 cleanupall -f || error "sec_cleanup"
55         fi
56 }
57
58 DIR=${DIR:-$MOUNT}
59 [ -z "`echo $DIR | grep $MOUNT`" ] && \
60         error "$DIR not in $MOUNT" && sec_cleanup && exit 1
61
62 [ `echo $MOUNT | wc -w` -gt 1 ] && \
63         echo "NAME=$MOUNT mounted more than once" && sec_cleanup && exit 0
64
65 [ $MDSCOUNT -gt 1 ] && \
66         echo "skip multi-MDS test" && sec_cleanup && exit 0
67
68 # for GSS_SUP
69 GSS_REF=$(lsmod | grep ^ptlrpc_gss | awk '{print $3}')
70 if [ ! -z "$GSS_REF" -a "$GSS_REF" != "0" ]; then
71         GSS_SUP=1
72         echo "with GSS support"
73 else
74         GSS_SUP=0
75         echo "without GSS support"
76 fi
77
78 MDT="`do_facet $SINGLEMDS "lctl get_param -N mdt.\*MDT\*.stats 2>/dev/null | cut -d"." -f2" || true`"
79 [ -z "$MDT" ] && error "fail to get MDT device" && exit 1
80 do_facet $SINGLEMDS "mkdir -p $CONFDIR"
81 IDENTITY_FLUSH=mdt.$MDT.identity_flush
82 MDSCAPA=mdt.$MDT.capa
83 CAPA_TIMEOUT=mdt.$MDT.capa_timeout
84 MDSSECLEVEL=mdt.$MDT.sec_level
85
86 # for CLIENT_TYPE
87 if [ -z "$(lctl get_param -n llite.*.client_type | grep remote 2>/dev/null)" ]; then
88         CLIENT_TYPE="local"
89         echo "local client"
90 else
91         CLIENT_TYPE="remote"
92         echo "remote client"
93 fi
94
95 SAVE_PWD=$PWD
96
97 build_test_filter
98
99 sec_login() {
100         local user=$1
101         local group=$2
102
103         if ! $RUNAS -u $user krb5_login.sh; then
104                 error "$user login kerberos failed."
105                 exit 1
106         fi
107
108         if ! $RUNAS -u $user -g $group ls $DIR > /dev/null 2>&1; then
109                 $RUNAS -u $user lfs flushctx -k
110                 $RUNAS -u $user krb5_login.sh
111                 if ! $RUNAS -u $user -g $group ls $DIR > /dev/null 2>&1; then
112                         error "init $user $group failed."
113                         exit 2
114                 fi
115         fi
116 }
117
118 declare -a identity_old
119
120 sec_setup() {
121         for num in `seq $MDSCOUNT`; do
122                 switch_identity $num true || identity_old[$num]=$?
123         done
124
125         if ! $RUNAS -u $ID0 ls $DIR > /dev/null 2>&1; then
126                 sec_login $USER0 $USER0
127         fi
128
129         if ! $RUNAS -u $ID1 ls $DIR > /dev/null 2>&1; then
130                 sec_login $USER1 $USER1
131         fi
132 }
133 sec_setup
134
135 # run as different user
136 test_0() {
137         umask 0022
138
139         chmod 0755 $DIR || error "chmod (1)"
140         rm -rf $DIR/$tdir || error "rm (1)"
141         mkdir -p $DIR/$tdir || error "mkdir (1)"
142
143         if [ "$CLIENT_TYPE" = "remote" ]; then
144                 do_facet $SINGLEMDS "echo '* 0 normtown' > $PERM_CONF"
145                 do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
146                 chown $USER0 $DIR/$tdir && error "chown (1)"
147                 do_facet $SINGLEMDS "echo '* 0 rmtown' > $PERM_CONF"
148                 do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
149         else
150                 chown $USER0 $DIR/$tdir || error "chown (2)"
151         fi
152
153         $RUNAS -u $ID0 ls $DIR || error "ls (1)"
154         rm -f $DIR/f0 || error "rm (2)"
155         $RUNAS -u $ID0 touch $DIR/f0 && error "touch (1)"
156         $RUNAS -u $ID0 touch $DIR/$tdir/f1 || error "touch (2)"
157         $RUNAS -u $ID1 touch $DIR/$tdir/f2 && error "touch (3)"
158         touch $DIR/$tdir/f3 || error "touch (4)"
159         chown root $DIR/$tdir || error "chown (3)"
160         chgrp $USER0 $DIR/$tdir || error "chgrp (1)"
161         chmod 0775 $DIR/$tdir || error "chmod (2)"
162         $RUNAS -u $ID0 touch $DIR/$tdir/f4 || error "touch (5)"
163         $RUNAS -u $ID1 touch $DIR/$tdir/f5 && error "touch (6)"
164         touch $DIR/$tdir/f6 || error "touch (7)"
165         rm -rf $DIR/$tdir || error "rm (3)"
166
167         if [ "$CLIENT_TYPE" = "remote" ]; then
168                 do_facet $SINGLEMDS "rm -f $PERM_CONF"
169                 do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
170         fi
171 }
172 run_test 0 "uid permission ============================="
173
174 # setuid/gid
175 test_1() {
176         [ $GSS_SUP = 0 ] && skip "without GSS support." && return
177
178         if [ "$CLIENT_TYPE" = "remote" ]; then
179                 do_facet $SINGLEMDS "echo '* 0 rmtown' > $PERM_CONF"
180                 do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
181         fi
182
183         rm -rf $DIR/$tdir
184         mkdir -p $DIR/$tdir
185
186         chown $USER0 $DIR/$tdir || error "chown (1)"
187         $RUNAS -u $ID1 -v $ID0 touch $DIR/$tdir/f0 && error "touch (2)"
188         echo "enable uid $ID1 setuid"
189         do_facet $SINGLEMDS "echo '* $ID1 setuid' >> $PERM_CONF"
190         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
191         $RUNAS -u $ID1 -v $ID0 touch $DIR/$tdir/f1 || error "touch (3)"
192
193         chown root $DIR/$tdir || error "chown (4)"
194         chgrp $USER0 $DIR/$tdir || error "chgrp (5)"
195         chmod 0770 $DIR/$tdir || error "chmod (6)"
196         $RUNAS -u $ID1 -g $ID1 touch $DIR/$tdir/f2 && error "touch (7)"
197         $RUNAS -u $ID1 -g $ID1 -j $ID0 touch $DIR/$tdir/f3 && error "touch (8)"
198         echo "enable uid $ID1 setuid,setgid"
199         do_facet $SINGLEMDS "echo '* $ID1 setuid,setgid' > $PERM_CONF"
200         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
201         $RUNAS -u $ID1 -g $ID1 -j $ID0 touch $DIR/$tdir/f4 || error "touch (9)"
202         $RUNAS -u $ID1 -v $ID0 -g $ID1 -j $ID0 touch $DIR/$tdir/f5 || error "touch (10)"
203
204         rm -rf $DIR/$tdir
205
206         do_facet $SINGLEMDS "rm -f $PERM_CONF"
207         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
208 }
209 run_test 1 "setuid/gid ============================="
210
211 run_rmtacl_subtest() {
212     $SAVE_PWD/rmtacl/run $SAVE_PWD/rmtacl/$1.test
213     return $?
214 }
215
216 # remote_acl
217 # for remote client only
218 test_2 () {
219         [ "$CLIENT_TYPE" = "local" ] && \
220                 skip "remote_acl for remote client only" && return
221         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep ^acl)" ] && \
222                 skip "must have acl enabled" && return
223         [ -z "$(which setfacl 2>/dev/null)" ] && \
224                 skip "could not find setfacl" && return
225         [ "$UID" != 0 ] && skip "must run as root" && return
226
227         do_facet $SINGLEMDS "echo '* 0 rmtacl,rmtown' > $PERM_CONF"
228         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
229
230         sec_login root root
231         sec_login bin bin
232         sec_login daemon daemon
233         sec_login games users
234
235         SAVE_UMASK=`umask`
236         umask 0022
237         cd $DIR
238
239         echo "performing cp ..."
240         run_rmtacl_subtest cp || error "cp"
241         echo "performing getfacl-noacl..."
242         run_rmtacl_subtest getfacl-noacl || error "getfacl-noacl"
243         echo "performing misc..."
244         run_rmtacl_subtest misc || error "misc"
245         echo "performing permissions..."
246         run_rmtacl_subtest permissions || error "permissions"
247         echo "performing setfacl..."
248         run_rmtacl_subtest setfacl || error "setfacl"
249
250         # inheritance test got from HP
251         echo "performing inheritance..."
252         cp $SAVE_PWD/rmtacl/make-tree .
253         chmod +x make-tree
254         run_rmtacl_subtest inheritance || error "inheritance"
255         rm -f make-tree
256
257         cd $SAVE_PWD
258         umask $SAVE_UMASK
259
260         do_facet $SINGLEMDS "rm -f $PERM_CONF"
261         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
262 }
263 run_test 2 "rmtacl ============================="
264
265 # rootsquash
266 # root_squash will be redesigned in Lustre 1.7
267 test_3() {
268         skip "root_squash will be redesigned in Lustre 1.7" && return
269 }
270 run_test 3 "rootsquash ============================="
271
272 # bug 3285 - supplementary group should always succeed.
273 # NB: the supplementary groups are set for local client only,
274 # as for remote client, the groups of the specified uid on MDT
275 # will be obtained by upcall /sbin/l_getidentity and used.
276 test_4() {
277         if [ "$CLIENT_TYPE" = "remote" ]; then
278                 do_facet $SINGLEMDS "echo '* 0 rmtown' > $PERM_CONF"
279                 do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
280         fi
281
282         rm -rf $DIR/$tdir
283         mkdir -p $DIR/$tdir
284         chmod 0771 $DIR/$tdir
285         chgrp $ID0 $DIR/$tdir
286         $RUNAS -u $ID0 ls $DIR/$tdir || error "setgroups (1)"
287         if [ "$CLIENT_TYPE" = "local" ]; then
288                 do_facet $SINGLEMDS "echo '* $ID1 setgrp' > $PERM_CONF"
289                 do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
290                 $RUNAS -u $ID1 -G1,2,$ID0 ls $DIR/$tdir || error "setgroups (2)"
291         fi
292         $RUNAS -u $ID1 -G1,2 ls $DIR/$tdir && error "setgroups (3)"
293         rm -rf $DIR/$tdir
294
295         do_facet $SINGLEMDS "rm -f $PERM_CONF"
296         do_facet $SINGLEMDS "lctl set_param -n $IDENTITY_FLUSH=-1"
297 }
298 run_test 4 "set supplementary group ==============="
299
300 mds_capability_timeout() {
301         [ $# -lt 1 ] && echo "Miss mds capability timeout value" && return 1
302
303         echo "Set mds capability timeout as $1 seconds"
304         do_facet $SINGLEMDS "lctl set_param -n $CAPA_TIMEOUT=$1"
305         return 0
306 }
307
308 mds_sec_level_switch() {
309         [ $# -lt 1 ] && echo "Miss mds sec level switch value" && return 1
310
311         case $1 in
312                 0) echo "Disable capa for all clients";;
313                 1) echo "Enable capa for remote client";;
314                 3) echo "Enable capa for all clients";;
315                 *) echo "Invalid mds sec level switch value" && return 2;;
316         esac
317
318         do_facet $SINGLEMDS "lctl set_param -n $MDSSECLEVEL=$1"
319         return 0
320 }
321
322 oss_sec_level_switch() {
323         [ $# -lt 1 ] && echo "Miss oss sec level switch value" && return 1
324
325         case $1 in
326                 0) echo "Disable capa for all clients";;
327                 1) echo "Enable capa for remote client";;
328                 3) echo "Enable capa for all clients";;
329                 *) echo "Invalid oss sec level switch value" && return 2;;
330         esac
331
332         for i in `seq $OSTCOUNT`; do
333                 local j=`expr $i - 1`
334                 local OST="`do_facet ost$i "lctl get_param -N obdfilter.\*OST\*$j/stats 2>/dev/null | cut -d"." -f2" || true`"
335                 [ -z "$OST" ] && return 3
336                 do_facet ost$i "lctl set_param -n obdfilter.$OST.sec_level=$1"
337         done
338         return 0
339 }
340
341 mds_capability_switch() {
342         [ $# -lt 1 ] && echo "Miss mds capability switch value" && return 1
343
344         case $1 in
345                 0) echo "Turn off mds capability";;
346                 3) echo "Turn on mds capability";;
347                 *) echo "Invalid mds capability switch value" && return 2;;
348         esac
349
350         do_facet $SINGLEMDS "lctl set_param -n $MDSCAPA=$1"
351         return 0
352 }
353
354 oss_capability_switch() {
355         [ $# -lt 1 ] && echo "Miss oss capability switch value" && return 1
356
357         case $1 in
358                 0) echo "Turn off oss capability";;
359                 1) echo "Turn on oss capability";;
360                 *) echo "Invalid oss capability switch value" && return 2;;
361         esac
362
363         for i in `seq $OSTCOUNT`; do
364                 local j=`expr $i - 1`
365                 local OST="`do_facet ost$i "lctl get_param -N obdfilter.\*OST\*$j/stats 2>/dev/null | cut -d"." -f2" || true`"
366                 [ -z "$OST" ] && return 3
367                 do_facet ost$i "lctl set_param -n obdfilter.$OST.capa=$1"
368         done
369         return 0
370 }
371
372 turn_mds_capa_on() {
373         mds_capability_switch 3 || return 1
374         mds_sec_level_switch 3  || return 2
375         return 0
376 }
377
378 turn_oss_capa_on() {
379         oss_capability_switch 1 || return 1
380         oss_sec_level_switch 3  || return 2
381         return 0
382 }
383
384 turn_capability_on() {
385         local capa_timeout=${1:-"1800"}
386
387         # To turn on fid capability for the system,
388         # there is a requirement that fid capability
389         # is turned on on all MDS/OSS servers before
390         # client mount.
391
392         turn_mds_capa_on || return 1
393         turn_oss_capa_on || return 2
394         mds_capability_timeout $capa_timeout || return 3
395         remount_client $MOUNT || return 4
396         return 0
397 }
398
399 turn_mds_capa_off() {
400         mds_sec_level_switch 0  || return 1
401         mds_capability_switch 0 || return 2
402         return 0
403 }
404
405 turn_oss_capa_off() {
406         oss_sec_level_switch 0  || return 1
407         oss_capability_switch 0 || return 2
408         return 0
409 }
410
411 turn_capability_off() {
412         # to turn off fid capability, you can just do
413         # it in a live system. But, please turn off
414         # capability of all OSS servers before MDS servers.
415
416         turn_oss_capa_off || return 1
417         turn_mds_capa_off || return 2
418         return 0
419 }
420
421 # We demonstrate that access to the objects in the filesystem are not
422 # accessible without supplying secrets from the MDS by disabling a
423 # proc variable on the mds so that it does not supply secrets. We then
424 # try and access objects which result in failure.
425 test_5() {
426         local file=$DIR/f5
427
428         [ $GSS_SUP = 0 ] && skip "without GSS support." && return
429         if ! remote_mds; then
430                 skip "client should be separated from server."
431                 return
432         fi
433
434         rm -f $file
435
436         turn_capability_off
437         if [ $? != 0 ]; then
438                 error "turn_capability_off"
439                 return 1
440         fi
441
442         turn_oss_capa_on
443         if [ $? != 0 ]; then
444                 error "turn_oss_capa_on"
445                 return 2
446         fi
447
448         if [ "$CLIENT_TYPE" = "remote" ]; then
449                 remount_client $MOUNT && return 3
450                 turn_oss_capa_off
451                 return 0
452         else
453                 remount_client $MOUNT || return 4
454         fi
455
456         # proc variable disabled -- access to the objects in the filesystem
457         # is not allowed 
458         echo "Should get Write error here : (proc variable are disabled "\
459              "-- access to the objects in the filesystem is denied."
460         $WTL $file 30
461         if [ $? == 0 ]; then
462                 error "Write worked well even though secrets not supplied."
463                 return 5
464         fi
465
466         turn_capability_on
467         if [ $? != 0 ]; then
468                 error "turn_capability_on"
469                 return 6
470         fi
471
472         sleep 5
473
474         # proc variable enabled, secrets supplied -- write should work now
475         echo "Should not fail here : (proc variable enabled, secrets supplied "\
476              "-- write should work now)."
477         $WTL $file 30
478         if [ $? != 0 ]; then
479                 error "Write failed even though secrets supplied."
480                 return 7
481         fi
482
483         turn_capability_off
484         if [ $? != 0 ]; then
485                 error "turn_capability_off"
486                 return 8
487         fi
488         rm -f $file
489 }
490 run_test 5 "capa secrets ========================="
491
492 # Expiry: A test program is performing I/O on a file. It has credential
493 # with an expiry half a minute later. While the program is running the
494 # credentials expire and no automatic extensions or renewals are
495 # enabled. The program will demonstrate an I/O failure.
496 test_6() {
497         local file=$DIR/f6
498
499         [ $GSS_SUP = 0 ] && skip "without GSS support." && return
500         if ! remote_mds; then
501                 skip "client should be separated from server."
502                 return
503         fi
504
505         turn_capability_off
506         if [ $? != 0 ]; then
507                 error "turn_capability_off"
508                 return 1
509         fi
510
511         rm -f $file
512
513         turn_capability_on 30
514         if [ $? != 0 ]; then
515                 error "turn_capability_on 30"
516                 return 2
517         fi
518
519         # Token expiry
520         $WTL $file 60
521         if [ $? != 0 ]; then
522                 error "$WTL $file 60"
523                 return 3
524         fi
525
526         # Reset MDS capability timeout
527         mds_capability_timeout 30
528         if [ $? != 0 ]; then
529                 error "mds_capability_timeout 30"
530                 return 4
531         fi
532
533         $WTL $file 60 &
534         local PID=$!
535         sleep 5
536
537         # To disable automatic renew, only need turn capa off on MDS.
538         turn_mds_capa_off
539         if [ $? != 0 ]; then
540                 error "turn_mds_capa_off"
541                 return 5
542         fi
543
544         echo "We expect I/O failure."
545         wait $PID
546         if [ $? == 0 ]; then
547                 echo "no I/O failure got."
548                 return 6
549         fi
550
551         turn_capability_off
552         if [ $? != 0 ]; then
553                 error "turn_capability_off"
554                 return 7
555         fi
556         rm -f $file
557 }
558 run_test 6 "capa expiry ========================="
559
560 log "cleanup: ======================================================"
561
562 sec_unsetup() {
563         for num in `seq $MDSCOUNT`; do
564                 if [ "${identity_old[$num]}" = 1 ]; then
565                         switch_identity $num false || identity_old[$num]=$?
566                 fi
567         done
568
569         $RUNAS -u $ID0 ls $DIR
570         $RUNAS -u $ID1 ls $DIR
571 }
572 sec_unsetup
573
574 sec_cleanup
575
576 echo '=========================== finished ==============================='
577 [ -f "$SANITYSECLOG" ] && \
578         cat $SANITYSECLOG && grep -q FAIL $SANITYSECLOG && exit 1 || true
579 echo "$0 completed"