f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# CDDL HEADER START
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# The contents of this file are subject to the terms of the
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# Common Development and Distribution License (the "License").
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# You may not use this file except in compliance with the License.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# or http://www.opensolaris.org/os/licensing.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# See the License for the specific language governing permissions
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# and limitations under the License.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# When distributing Covered Code, include this CDDL HEADER in each
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# If applicable, add the following below this CDDL HEADER, with the
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# fields enclosed by brackets "[]" replaced with your own identifying
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# information: Portions Copyright [yyyy] [name of copyright owner]
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# CDDL HEADER END
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy# Use is subject to license terms.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy#
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy. $STF_SUITE/include/libtest.shlib
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy. $STF_SUITE/tests/functional/history/history.cfg
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction run_and_verify
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset user pool
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy while getopts "p:u:" opt; do
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy case $opt in
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy p)
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy pool=$OPTARG
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy ;;
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy u)
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy user=$OPTARG
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy ;;
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy esac
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy done
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy shift $(($OPTIND - 1))
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy pool=${pool:-$TESTPOOL}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy user=${user:-"root"}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fullcmd="$1"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy flags="$2"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy histcmd=$(echo $fullcmd | sed 's/\/usr\/sbin\///g')
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy cmd=$(echo $histcmd | awk '{print $1}')
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy subcmd=$(echo $histcmd | awk '{print $2}')
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # If we aren't running zpool or zfs, something is wrong
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $cmd == "zpool" || $cmd == "zfs" ]] || \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_fail "run_and_verify called with \"$cmd ($fullcmd)\""
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # If this is a 'zfs receive' truncate the stdin redirect
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $subcmd == "receive" || $subcmd == "recv" ]] && \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy histcmd=${histcmd%% <*}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # Run the command as the specified user, and find the new history.
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy zpool history $flags $pool > $OLD_HISTORY 2>/dev/null
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $user == "root" ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_must eval "$fullcmd"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy else
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy log_must su $user -c "eval $fullcmd"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy zpool history $flags $pool > $TMP_HISTORY 2>/dev/null
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy diff $OLD_HISTORY $TMP_HISTORY | grep "^> " | sed 's/^> //g' \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy > $NEW_HISTORY
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # Verify what's common to every case, regardless of zpool history flags.
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "$histcmd" $NEW_HISTORY >/dev/null 2>&1 || \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_fail "Didn't find \"$histcmd\" in pool history"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # If 'zpool history' was called without any flags, then we're done.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ -z $flags ]] && return
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # Verify the new history in cases that are more interesting because
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # additional information is logged with -i or -l.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $flags =~ "i" ]] && log_must verify_$subcmd "$histcmd" "$subcmd" \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy "$flags"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $flags =~ "l" ]] && log_must verify_long "$histcmd" "$user" "$flags"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction verify_long
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset cmd=$1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset user=$2
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset flags=$3
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $flags =~ "l" ]] || return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy typeset uid=$(id -u $user)
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy typeset hname=$(hostname)
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if ! is_global_zone; then
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy hname=$hname:$(zonename)
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "$cmd \[user $uid ($user) on $hname\]" $NEW_HISTORY >/dev/null \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Couldn't find long information for \"$cmd\""
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction verify_hold
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset cmd=$1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset subcmd=$2
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset flags=$3
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $flags =~ "i" ]] || return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy typeset tag=$(echo $cmd | awk '{print $4}')
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset fullname=${cmd##* }
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset dsname=${fullname%%@*}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset snapname=${fullname##*@}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # This works whether or not the hold was recursive
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy for ds in $(zfs list -r -Ho name -t snapshot $dsname | \
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "@$snapname"); do
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "$subcmd $ds ([0-9]*) tag=$tag" $NEW_HISTORY \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Didn't find hold on $ds with $tag"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy done
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction verify_release
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # hold and release formats only differ by the subcommand name, so
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # simply reuse the hold function.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy verify_hold "$1" "release" "$3"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction verify_rollback
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset cmd=$1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset flags=$3
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $flags =~ "i" ]] || return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset fullname=${cmd##* }
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset dsname=${fullname%%@*}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset parent_fs=${dsname##*/}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset rb_fs=${dsname}/%rollback
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset snapname=${fullname##*@}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "clone swap $rb_fs ([0-9]*) parent=$parent_fs" $NEW_HISTORY \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Didn't find rollback clone swap in pool history"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "destroy $rb_fs" $NEW_HISTORY >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Didn't find rollback destroy in pool history"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction verify_inherit
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset cmd=$1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset flags=$3
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $flags =~ "i" ]] || return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset dsname=${cmd##* }
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset prop=${cmd% *}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy prop=${prop##* }
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # This works whether or not the inherit was recursive
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy for ds in $(zfs list -r -Ho name -t filesystem $dsname); do
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "$subcmd $ds ([0-9]*) ${prop}=" $NEW_HISTORY >/dev/null \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Didn't find inherit history for $ds"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy done
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction verify_allow
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset cmd=$1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset subcmd=$2
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset flags=$3
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $flags =~ "i" ]] || return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $subcmd == "allow" ]] && subcmd="update"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $subcmd == "unallow" ]] && subcmd="remove"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset is_set lflag dflag dsname gname gid uname uid opt str code tmp
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy #
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # Here, we determine three things:
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # - Whether we're operating on a set or an indivdual permission (which
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # dictates the case of the first character in the code)
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # - The name of the dataset we're operating on.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # - Whether the operation applies locally or to descendent datasets (or
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # both)
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy #
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy echo $cmd | awk '{i = NF - 1; print $i}' | grep '@' >/dev/null \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy 2>&1 && is_set=1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy dsname=${cmd##* }
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $cmd =~ "-l " ]] && lflag=1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $cmd =~ "-d " ]] && dflag=1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ -z $lflag && -z $dflag ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy lflag=1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy dflag=1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy #
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # For each of the five cases below, the operation is essentially the
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # same. First, use the command passed in to determine what the code at
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # the end of the pool history will be. The specifics of the code are
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # described in a block comment at the top of dsl_deleg.c. Once that's
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # been assembled, check for its presence in the history, and return
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # success or failure accordingly.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy #
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $cmd =~ "-s " ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy str="s-\$@"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ -n $is_set ]] && str="S-\$@"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy tmp=${cmd#*@}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy code="$str${tmp% *}"
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "permission $subcmd $dsname ([0-9]*) $code" \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy $NEW_HISTORY >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Couldn't find $code in $NEW_HISTORY"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy elif [[ $cmd =~ "-c " ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy str="c-\$"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ -n $is_set ]] && str="C-\$"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy tmp=${cmd#*-c}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy code="$str${tmp% *}"
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "permission $subcmd $dsname ([0-9]*) $code" \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy $NEW_HISTORY >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Couldn't find $code in $NEW_HISTORY"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy elif [[ $cmd =~ "-u " ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy str="u"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ -n $is_set ]] && str="U"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy tmp=${cmd##*-u }
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy opt=$(echo $tmp | awk '{print $2}')
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy uid=$(id -u ${tmp%% *})
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ -n $lflag ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy code="${str}l\$$uid $opt"
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "permission $subcmd $dsname ([0-9]*) $code" \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy $NEW_HISTORY >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Couldn't find $code in $NEW_HISTORY"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ -n $dflag ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy code="${str}d\$$uid $opt"
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "permission $subcmd $dsname ([0-9]*) $code" \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy $NEW_HISTORY >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Couldn't find $code in $NEW_HISTORY"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy elif [[ $cmd =~ "-g " ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy str="g"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ -n $is_set ]] && str="G"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy tmp=${cmd##*-g }
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy opt=$(echo $tmp | awk '{print $2}')
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy gid=$(awk -F: "/^${tmp%% *}:/ {print \$3}" /etc/group)
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ -n $lflag ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy code="${str}l\$$gid $opt"
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "permission $subcmd $dsname ([0-9]*) $code" \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy $NEW_HISTORY >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Couldn't find $code in $NEW_HISTORY"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ -n $dflag ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy code="${str}d\$$gid $opt"
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "permission $subcmd $dsname ([0-9]*) $code" \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy $NEW_HISTORY >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Couldn't find $code in $NEW_HISTORY"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy elif [[ $cmd =~ "-e " ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy str="e"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ -n $is_set ]] && str="E"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy opt=${cmd##*-e }
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy opt=${opt%% *}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ -n $lflag ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy code="${str}l\$ $opt"
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "permission $subcmd $dsname ([0-9]*) $code" \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy $NEW_HISTORY >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Couldn't find $code in $NEW_HISTORY"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ -n $dflag ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy code="${str}d\$ $opt"
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "permission $subcmd $dsname ([0-9]*) $code" \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy $NEW_HISTORY >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Couldn't find $code in $NEW_HISTORY"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy else
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Can't parse command \"$cmd\""
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction verify_unallow
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy #
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # The unallow and allow history have the same format, except the former
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # logs "permission removed" and the latter "permission updated" so
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # simply reuse the allow function.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy #
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy verify_allow "$1" "unallow" "$3"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction verify_destroy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset cmd=$1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset flags=$3
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # This function doesn't currently verifiy the zpool command.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ ${cmd%% *} == "zfs" ]] || return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $flags =~ "i" ]] || return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset dsname=${cmd##* }
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $dsname =~ "@" ]] && typeset is_snap=1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ -n $is_snap ]]; then
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "ioctl destroy_snaps" $NEW_HISTORY >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Didn't find ioctl while destroying $dsname"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # This should be present for datasets and snapshots alike
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "destroy $dsname" $NEW_HISTORY >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Didn't find \"destroy\" for $dsname"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedyfunction verify_snapshot
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy{
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset cmd=$1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset flags=$3
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy [[ $flags =~ "i" ]] || return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset fullname=${cmd##* }
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset dsname=${fullname%%@*}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy typeset snapname=${fullname##*@}
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "\[txg:[0-9]*\] $subcmd $fullname ([0-9]*)" $NEW_HISTORY \
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Didn't find snapshot command for $fullname"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy # This works whether or not the snapshot was recursive
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy for ds in $(zfs list -r -Ho name -t snapshot $dsname | \
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "@$snapname"); do
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy grep "^[ ]* $ds$" $NEW_HISTORY >/dev/null 2>&1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy if [[ $? != 0 ]]; then
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy log_note "Didn't find \"ioctl snapshot\" for $ds"
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 1
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy fi
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy done
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy return 0
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy}