aboutsummaryrefslogtreecommitdiff
path: root/tests/zfs-tests/tests/functional/acl/acl_common.kshlib
blob: ba08bcb48befb22272aec2f633c9db252a8570ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# Copyright (c) 2016, 2018 by Delphix. All rights reserved.
#

. $STF_SUITE/tests/functional/acl/acl.cfg
. $STF_SUITE/include/libtest.shlib

#
# Get the given file/directory access mode
#
# $1 object -- file or directory
#
function get_mode #<obj>
{
	typeset obj=$1
	if (( ${#obj} == 0 )); then
		return 1
	fi

	ls -ld $obj | awk '{print $1}'
}

#
# Get the given file/directory ACL
#
# $1 object -- file or directory
#
function get_acl #<obj>
{
        typeset obj=$1
	if (( ${#obj} == 0 )); then
		return 1
	fi

	ls -vd $obj | nawk '(NR != 1) {print $0}'
}

#
# Get the given file/directory ACL
#
# $1 object -- file or directory
#
function get_compact_acl #<obj>
{
        typeset obj=$1
	if (( ${#obj} == 0 )); then
		return 1
	fi

	ls -Vd $obj | nawk '(NR != 1) {print $0}'
}

#
# Check the given two files/directories have the same ACLs
#
# Return 0, if source object acl is equal to target object acl.
#
# $1 source object
# $2 target object
#
function compare_acls #<src> <tgt>
{
        typeset src=$1
        typeset tgt=$2

	(( ${#src} == 0 || ${#tgt} == 0 )) && return 1
	[[ $src == $tgt ]] && return 0

	typeset tmpsrc=$TEST_BASE_DIR/compare_acls.src.$$
	typeset tmptgt=$TEST_BASE_DIR/compare_acls.tgt.$$

	get_acl $src > $tmpsrc
	get_acl $tgt > $tmptgt
	typeset -i ret=0
	diff $tmpsrc $tmptgt > /dev/null 2>&1
	ret=$?
	rm -f $tmpsrc $tmptgt

	if (( ret != 0 )); then
		return $ret
	fi

	get_compact_acl $src > $tmpsrc
	get_compact_acl $tgt > $tmptgt
	diff $tmpsrc $tmptgt > /dev/null 2>&1
	ret=$?
	rm -f $tmpsrc $tmptgt

	return $ret
}

#
# Check that the given two objects have the same modes.
# Return 0, if their modes are equal with each other. Otherwise, return 1.
#
# $1 source object
# $2 target object
#
function compare_modes #<src> <tgt>
{
        typeset src=$1
        typeset tgt=$2
        typeset -i i=0
        set -A mode

	(( ${#src} == 0 || ${#tgt} == 0 )) && return 1
	[[ $src == $tgt ]] && return 0

	typeset obj
        for obj in $src $tgt
        do
                mode[i]=$(get_mode $obj)

                (( i = i + 1 ))
        done

        [[ ${mode[0]} != ${mode[1]} ]] && return 1

        return 0
}

#
# Check that the given two objects have the same xattrs.
# Return 0, if their xattrs are equal with each other. Otherwise, return 1.
#
# $1 source object
# $2 target object
#
function compare_xattrs #<src> <tgt>
{
        typeset src=$1
        typeset tgt=$2

	(( ${#src} == 0 || ${#tgt} == 0 )) && return 1
	[[ $src == $tgt ]] && return 0

	typeset tmpsrc=$TEST_BASE_DIR/compare_xattrs.src.$$
	typeset tmptgt=$TEST_BASE_DIR/compare_xattrs.tgt.$$

	get_xattr $src > $tmpsrc
	get_xattr $tgt > $tmptgt
	typeset -i ret=0
	diff $tmpsrc $tmptgt > /dev/null 2>&1
	ret=$?
	rm -f $tmpsrc $tmptgt

        return $ret
}

#
# Check '+' is set for a given file/directory with 'ls [-l]' command
#
# $1 object -- file or directory.
#
function plus_sign_check_l #<obj>
{
	typeset obj=$1
	if (( ${#obj} == 0 )); then
		return 1
	fi

	ls -ld $obj | awk '{print $1}' | grep "+$" > /dev/null

        return $?
}

#
# Check '+' is set for a given file/directory with 'ls [-v]' command
#
# $1 object -- file or directory.
#
function plus_sign_check_v #<obj>
{
	typeset obj=$1
	if (( ${#obj} == 0 )); then
		return 1
	fi

	ls -vd $obj | awk '(NR == 1) {print $1}' | grep "+$" > /dev/null

        return $?
}

#
# A wrapper function of c program
#
# $1 legal login name
# $2-n commands and options
#
function chgusr_exec #<login_name> <commands> [...]
{
	chg_usr_exec $@
	return $?
}

#
# Export the current user for the following usr_exec operating.
#
# $1 legal login name
#
function set_cur_usr #<login_name>
{
	export ZFS_ACL_CUR_USER=$1
}

#
# Run commands by $ZFS_ACL_CUR_USER
#
# $1-n commands and options
#
function usr_exec #<commands> [...]
{
	chg_usr_exec "$ZFS_ACL_CUR_USER" $@
	return $?
}

#
# Count how many ACEs for the specified file or directory.
#
# $1 file or directory name
#
function count_ACE #<file or dir name>
{
	if [[ ! -e $1 ]]; then
		log_note "Need input file or directory name."
		return 1
	fi

	ls -vd $1 | nawk 'BEGIN {count=0}
			(NR != 1)&&(/[0-9]:/) {count++}
			END {print count}'

	return 0
}

#
# Get specified number ACE content of specified file or directory.
#
# $1 file or directory name
# $2 specified number
#
function get_ACE #<file or dir name> <specified number> <verbose|compact>
{
	if [[ ! -e $1 || $2 -ge $(count_ACE $1) ]]; then
		return 1
	fi

	typeset file=$1
	typeset -i num=$2
	typeset format=${3:-verbose}
	typeset -i next_num=-1

        typeset tmpfile=$TEST_BASE_DIR/tmp_get_ACE.$$
        typeset line=""
	typeset args

	case $format in
		verbose) args="-vd"
			;;
		compact) args="-Vd"
			;;
		*) log_fail "Invalid parameter as ($format), " \
			"only verbose|compact is supported."
			;;
	esac

	ls $args $file > $tmpfile
	(( $? != 0 )) && log_fail "FAIL: ls $args $file > $tmpfile"
	while read line; do
		[[ -z $line ]] && continue
		if [[ $args == -vd ]]; then
			if [[ $line == "$num":* ]]; then
				(( next_num = num + 1 ))
			fi
			if [[ $line == "$next_num":* ]]; then
				break
			fi
			if (( next_num != -1 )); then
				print -n $line
			fi
		else
			if (( next_num == num )); then
				print -n $line
			fi
			(( next_num += 1 ))
		fi
	done < $tmpfile

	rm -f $tmpfile
	(( $? != 0 )) && log_fail "FAIL: rm -f $tmpfile"
}

#
# Cleanup exist user/group.
#
function cleanup_user_group
{
	del_user $ZFS_ACL_ADMIN

	del_user $ZFS_ACL_STAFF1
	del_user $ZFS_ACL_STAFF2
	del_group $ZFS_ACL_STAFF_GROUP

	del_user $ZFS_ACL_OTHER1
	del_user $ZFS_ACL_OTHER2
	del_group $ZFS_ACL_OTHER_GROUP

	return 0
}

#
# Clean up testfile and test directory
#
function cleanup
{
	if [[ -d $TESTDIR ]]; then
		cd $TESTDIR
		rm -rf $TESTDIR/*
	fi
}

#
# According to specified access or acl_spec, do relevant operating by using the
# specified user.
#
# $1 specified user
# $2 node
# $3 acl_spec or access
#
function rwx_node #user node acl_spec|access
{
	typeset user=$1
	typeset node=$2
	typeset acl_spec=$3

	if [[ $user == "" || $node == "" || $acl_spec == "" ]]; then
		log_note "node or acl_spec are not defined."
		return 1
	fi

	if [[ -d $node ]]; then
		case $acl_spec in
		*:read_data:*|read_data)
			chgusr_exec $user ls -l $node > /dev/null 2>&1
			return $? ;;
		*:write_data:*|write_data)
			if [[ -f ${node}/tmpfile ]]; then
				log_must rm -f ${node}/tmpfile
			fi
			chgusr_exec $user touch ${node}/tmpfile > \
				/dev/null 2>&1
			return $? ;;
		*"execute:"*|execute)
			chgusr_exec $user find $node > /dev/null 2>&1
			return $? ;;
		esac
	else
		case $acl_spec in
		*:read_data:*|read_data)
			chgusr_exec $user cat $node > /dev/null 2>&1
			return $? ;;
		*:write_data:*|write_data)
			chgusr_exec $user dd if=/usr/bin/ls of=$node > \
				/dev/null 2>&1
			return $? ;;
		*"execute:"*|execute)
			ZFS_ACL_ERR_STR=$(chgusr_exec $user $node 2>&1)
			return $? ;;
		esac
	fi
}

#
# Get the given file/directory xattr
#
# $1 object -- file or directory
#
function get_xattr #<obj>
{
        typeset obj=$1
	typeset xattr
	if (( ${#obj} == 0 )); then
		return 1
	fi

	for xattr in `runat $obj ls | \
		grep -E -v -e SUNWattr_ro -e SUNWattr_rw` ; do
		runat $obj sum $xattr
	done
}

#
# Get the owner of a file/directory
#
function get_owner #node
{
	typeset node=$1
	typeset value

	if [[ -z $node ]]; then
		log_fail "node are not defined."
	fi

	if [[ -d $node ]]; then
		value=$(ls -dl $node | awk '{print $3}')
	elif [[ -e $node ]]; then
		value=$(ls -l $node | awk '{print $3}')
	fi

	echo $value
}

#
# Get the group of a file/directory
#
function get_group #node
{
	typeset node=$1
	typeset value

	if [[ -z $node ]]; then
		log_fail "node are not defined."
	fi

	if [[ -d $node ]]; then
		value=$(ls -dl $node | awk '{print $4}')
	elif [[ -e $node ]]; then
		value=$(ls -l $node | awk '{print $4}')
	fi

	echo $value
}


#
# Get the group name that a UID belongs to
#
function get_user_group #uid
{
	typeset uid=$1
	typeset value

	if [[ -z $uid ]]; then
		log_fail "UID not defined."
	fi

	value=$(id $uid)

	if [[ $? -eq 0 ]]; then
		value=${value##*\(}
		value=${value%%\)*}
		echo $value
	else
		log_fail "Invalid UID (uid)."
	fi
}

#
# Get the specified item of the specified string
#
# $1:	Item number, count from 0.
# $2-n: strings
#
function getitem
{
	typeset -i n=$1
	shift

	(( n += 1 ))
	eval echo \${$n}
}

#
# This function calculate the specified directory files checksum and write
# to the specified array.
#
# $1 directory in which the files will be cksum.
# $2 file array name which was used to store file cksum information.
# $3 attribute array name which was used to store attribute information.
#
function cksum_files #<dir> <file_array_name> <attribute_array_name>
{
	typeset dir=$1
	typeset farr_name=$2
	typeset aarr_name=$3

	[[ ! -d $dir ]] && return
	typeset oldpwd=$PWD
	cd $dir
	typeset files=$(ls file*)

	typeset -i i=0
	typeset -i n=0
	while (( i < NUM_FILE )); do
		typeset f=$(getitem $i $files)
		eval $farr_name[$i]=\$\(\cksum $f\)

		typeset -i j=0
		while (( j < NUM_ATTR )); do
			eval $aarr_name[$n]=\$\(\runat \$f \cksum \
				attribute.$j\)

			(( j += 1 ))
			(( n += 1 ))
		done

		(( i += 1 ))
	done

	cd $oldpwd
}

#
# This function compare two cksum results array.
#
# $1 The array name which stored the cksum before operation.
# $2 The array name which stored the cksum after operation.
#
function compare_cksum #<array1> <array2>
{
	typeset before=$1
	typeset after=$2
	eval typeset -i count=\${#$before[@]}

	typeset -i i=0
	while (( i < count )); do
		eval typeset var1=\${$before[$i]}
		eval typeset var2=\${$after[$i]}

		if [[ $var1 != $var2 ]]; then
			return 1
		fi

		(( i += 1 ))
	done

	return 0
}

#
# This function calculate all the files cksum information in current directory
# and output them to the specified file.
#
# $1 directory from which the files will be cksum.
# $2 cksum output file
#
function record_cksum #<outfile>
{
	typeset dir=$1
	typeset outfile=$2

	[[ ! -d ${outfile%/*} ]] && usr_exec mkdir -p ${outfile%/*}

	usr_exec cd $dir ; find . -depth -type f -exec cksum {} \\\; | \
	    sort > $outfile
	usr_exec cd $dir ; find . -depth -type f -xattr -exec runat {} \
		cksum attribute* \\\; | sort >> $outfile
}

#
# The function create_files creates the directories and files that the script
# will operate on to test extended attribute functionality.
#
# $1 The base directory in which to create directories and files.
#
function create_files #<directory>
{
	typeset basedir=$1

	[[ ! -d $basedir ]] && usr_exec mkdir -m 777 $basedir
	[[ ! -d $RES_DIR  ]] && usr_exec mkdir -m 777 $RES_DIR
	[[ ! -d $INI_DIR ]] && usr_exec mkdir -m 777 $INI_DIR
	[[ ! -d $TST_DIR ]] && usr_exec mkdir -m 777 $TST_DIR
	[[ ! -d $TMP_DIR  ]] && usr_exec mkdir -m 777 $TMP_DIR

	#
	# Create the original file and its attribute files.
	#
	[[ ! -a $RES_DIR/file ]] && \
		usr_exec file_write -o create -f $RES_DIR/file \
			-b 1024 -d 0 -c 1
	[[ ! -a $RES_DIR/attribute ]] && \
		usr_exec cp $RES_DIR/file $RES_DIR/attribute

	typeset oldpwd=$PWD
	cd $INI_DIR

	typeset -i i=0
	while (( i < NUM_FILE )); do
		typeset dstfile=$INI_DIR/file.$$.$i
		usr_exec cp $RES_DIR/file $dstfile

		typeset -i j=0
		while (( j < NUM_ATTR )); do
			usr_exec runat $dstfile \
				cp $RES_DIR/attribute ./attribute.$j
			(( j += 1 ))
		done

		(( i += 1 ))
	done

	cd $oldpwd
}