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