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