1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2012 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/tests/functional/acl/acl_common.kshlib
33
34#
35# DESCRIPTION:
36#	Verify chmod permission settings on files and directories, as both root
37#	and non-root users.
38#
39# STRATEGY:
40#	1. Loop root and $ZFS_ACL_STAFF1 as root and non-root users.
41#	2. Create test file and directory in zfs filesystem.
42#	3. Execute 'chmod' with specified options.
43#	4. Check 'ls -l' output and compare with expect results.
44#
45# NOTE:
46#	The test does not work for default "discard" aclmode property.
47#	The test is modified to run with "passthrough" aclmode property.
48
49verify_runnable "both"
50
51function cleanup
52{
53	# reset aclmode=discard
54	log_must $ZFS set aclmode=discard $TESTPOOL/$TESTFS
55}
56
57#	"init_map" "options" "expect_map"
58set -A argv \
59	"000" "a+rw"	"rw-rw-rw-"	"000" "a+rwx"	"rwxrwxrwx" \
60	"000" "u+xr"	"r-x------"	"000" "gu-xw"	"---------" \
61	"644" "a-r"	"-w-------"	"644" "augo-x"	"rw-r--r--" \
62	"644" "=x"	"--x--x--x"	"644" "u-rw"	"---r--r--" \
63	"644" "uo+x"	"rwxr--r-x"	"644" "ga-wr"	"---------" \
64	"777" "augo+x"	"rwxrwxrwx"	"777" "go-xr"	"rwx-w--w-" \
65	"777" "o-wx"	"rwxrwxr--"	"777" "ou-rx"	"-w-rwx-w-" \
66	"777" "a+rwx"	"rwxrwxrwx"	"777" "u=rw"	"rw-rwxrwx" \
67	"000" "123"	"--x-w--wx"	"000" "412"	"r----x-w-" \
68	"231" "562"	"r-xrw--w-"	"712" "000"	"---------" \
69	"777" "121"	"--x-w---x"	"123" "775"	"rwxrwxr-x"
70
71log_assert " Verify chmod permission settings on files and directories"
72log_onexit cleanup
73
74#
75# Verify file or directory have correct map after chmod
76#
77# $1 file or directory
78#
79function test_chmod_mapping #<file-dir>
80{
81	typeset node=$1
82	typeset -i i=0
83
84	while ((i < ${#argv[@]})); do
85		usr_exec $CHMOD ${argv[i]} $node
86		if (($? != 0)); then
87			log_note "usr_exec $CHMOD ${argv[i]} $node"
88			return 1
89		fi
90		usr_exec $CHMOD ${argv[((i + 1))]} $node
91		if (($? != 0)); then
92			log_note "usr_exec $CHMOD ${argv[((i + 1))]} $node"
93			return 1
94		fi
95
96		typeset mode
97		mode=$(get_mode ${node})
98
99		if [[ $mode != "-${argv[((i + 2))]}"* && \
100			$mode != "d${argv[((i + 2))]}"* ]]
101		then
102			log_fail "FAIL: '${argv[i]}' '${argv[((i + 1))]}' \
103				'${argv[((i + 2))]}'"
104		fi
105
106		((i += 3))
107	done
108
109	return 0
110}
111
112# set aclmode=passthrough
113log_must $ZFS set aclmode=passthrough $TESTPOOL/$TESTFS
114
115for user in root $ZFS_ACL_STAFF1; do
116	log_must set_cur_usr $user
117
118	# Test file
119	log_must usr_exec $TOUCH $testfile
120	log_must test_chmod_mapping $testfile
121	log_must usr_exec $CHMOD A+user:$ZFS_ACL_STAFF2:write_acl:allow $testfile
122
123	# Test directory
124	log_must usr_exec $MKDIR $testdir
125	log_must test_chmod_mapping $testdir
126	log_must usr_exec $CHMOD A+user:$ZFS_ACL_STAFF2:write_acl:allow $testdir
127
128	# Grant privileges of write_acl and retest the chmod commands.
129
130	log_must set_cur_usr $ZFS_ACL_STAFF2
131	log_must test_chmod_mapping $testfile
132	log_must test_chmod_mapping $testdir
133
134	log_must set_cur_usr $user
135
136	log_must usr_exec $RM $testfile
137	log_must usr_exec $RM -rf $testdir
138done
139
140log_pass "Setting permissions using 'chmod' completed successfully."
141