This file is indexed.

/usr/sbin/simplesnapwrap is in simplesnap 1.0.3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/bin/bash

# Simple Snapshot Command Wrapper
# Copyright (c) 2014 John Goerzen
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e
ZFSCMD=/sbin/zfs
ZFS=runzfs
EXCLUDEPROP="org.complete.simplesnap:exclude"

logit () {
   logger -p info -t "`basename $0`[$$]" "$1"
}

exiterror () {
   logit "$1"
   exit 10
}

runzfs () {
  logit "Running: $ZFSCMD $*"
  if $ZFSCMD "$@"; then
      logit "zfs exited successfully."
      return 0
  else
      RETVAL="$?"
      logit "zfs exited with error: $RETVAL"
      return $RETVAL
  fi
}

DATE="gdate"
gdate &> /dev/null || DATE="date"
SED="gsed"
gsed &> /dev/null || SED="sed"
GREP="ggrep"
ggrep &> /dev/null || GREP="grep"


# template - $1
# dataset - $2
listsnaps () {
   $ZFS list -t snapshot -r -d 1 -H -o name "$2" | $GREP "@$1" || true
}

PATTERN="^[a-zA-Z0-9_/. -]\+\$"
if [ "$1" = "reinvoked" ]; then
  shift
  logit "Reinvoked with paramters \"$*\""
  shift # Drop the call to simplesnapwrap

  # Validate again.  Just to be sure.
  echo ".$*" | $GREP -q "${PATTERN}" || exiterror "Invalid characters found on re-validate; pattern is ${PATTERN}"
  echo ".$*" | $GREP -vq '\.\.' || exiterror "Found .. in input; aborting."

  # We don't want any paramters to contain a space or leading dash.
  for ARG; do
    echo ",${ARG}" | $GREP -vq '^,-' || exiterror "Found leading '-' in parameter \"${ARG}\"; aborting."
    echo ",${ARG}" | $GREP -vq ' ' || exiterror "Found space in parameters; aborting."
  done
  MODE="$1"
  shift || exiterror "Missing mode."
  case "$MODE" in
    "listfs")
       logit "Listing ZFS datasets without ${EXCLUDEPROP}=on"
       $ZFS list -t filesystem,volume -o "name,${EXCLUDEPROP}" -H | \
          $GREP -v $'\ton$' | $SED $'s/\t.*$//'
       ;;
    "sendback")
       # Next lines shared with reap
       TEMPLATE="__simplesnap_$1_"
       TEMPLATEPATTERN="^[a-zA-Z0-9]\+\$"
       echo "a$1" | $GREP -q "${TEMPLATEPATTERN}" || exiterror "Invalid characters in sendback template \"$1\"; pattern is ${TEMPLATEPATTERN}"
       DATASET="$2"
       echo "_${DATASET}" | $GREP -vq " " || exiterror "Space found in dataset name"
       [ -z "${DATASET}" ] && exiterror "No dataset given."
       [ -z "$1" ] && exiterror "No template given."

       logit "Listing snapshots on \"${DATASET}\" with template \"${TEMPLATE}\""

       OLDESTSNAP="`listsnaps \"${TEMPLATE}\" \"${DATASET}\" | head -n 1`"
       if [ -z "${OLDESTSNAP}" ]; then
         logit "Found no existing snapshot."
       else
         logit "Will use ${OLDESTSNAP} as basis."
       fi

       # Make a new snapshot.
       NEWSNAP="${TEMPLATE}`$DATE +%FT%T`__"
       NEWFULLSNAP="${DATASET}@${NEWSNAP}"
       logit "Making snapshot ${NEWFULLSNAP}"
       $ZFS snapshot "${NEWFULLSNAP}"

       if [ -z "${OLDESTSNAP}" ]; then
         # No existing snapshot.  Send the whole thing.
         logit "Sending non-incremental stream."
         $ZFS send "${NEWFULLSNAP}"
       else
         # Use the oldest existing snapshot.
         logit "Sending incremental stream back to ${OLDESTSNAP}"
         $ZFS send -I "${OLDESTSNAP}" "${NEWFULLSNAP}"
       fi
       ;;
    "reap")
       # Next lines shared with sendback
       TEMPLATE="__simplesnap_$1_"
       TEMPLATEPATTERN="^[a-zA-Z0-9_]\+\$"
       echo "_$TEMPLATE" | $GREP -q "${TEMPLATEPATTERN}" || exiterror "Invalid characters in reap template; pattern is ${TEMPLATEPATTERN}"
       DATASET="$2"
       echo "_${DATASET}" | $GREP -vq " " || exiterror "Space found in dataset name"
       [ -z "${DATASET}" ] && exiterror "No dataset given."
       [ -z "$1" ] && exiterror "No template given."
        
       # We always save the most recent.
       SNAPSTOREMOVE="`listsnaps \"${TEMPLATE}\" \"${DATASET}\" | head -n -1`"
       if [ -z "${SNAPSTOREMOVE}" ]; then
         logit "No snapshots to remove."
       else
         for REMOVAL in ${SNAPSTOREMOVE}; do
            logit "Destroying snapshot ${REMOVAL}"
            echo "_${REMOVAL}" | $GREP -q '@' || exiterror "PANIC: snapshot name doesn't contain '@'"
            $ZFS destroy "${REMOVAL}"
         done
       fi
       ;;
    *)
       exiterror "Invalid mode \"${MODE}\" specified."
       ;;
    esac
  logit "Exiting successfully."
  exit 0
fi

CMD="${SSH_ORIGINAL_COMMAND}"

logit "Invoked with parameters \"$*\" and SSH_ORIGINAL_COMMAND: \"${CMD}\""

if [ -z "${SSH_ORIGINAL_COMMAND}" ]; then
  echo "simplesnapwrap: This program is to be run from ssh."
  exiterror "Not run from ssh."
fi

# Sanitize input.  We don't want special characters here.

echo ".${CMD}" | $GREP -q "${PATTERN}" || exiterror "Invalid characters in input; pattern is: ${PATTERN}"
echo ".${CMD}" | $GREP -vq '\.\.' || exiterror "Found .. in input; aborting."

logit "Reinvoking to parse parameters"
exec "$0" reinvoked ${CMD}