| 1 |
#!/bin/bash |
| 2 |
set -eu |
| 3 |
|
| 4 |
# Copyright (C) 2019, IONOS SE |
| 5 |
# Author: Benjamin Drung <[email protected]> |
| 6 |
# |
| 7 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
# purpose with or without fee is hereby granted, provided that the above |
| 9 |
# copyright notice and this permission notice appear in all copies. |
| 10 |
# |
| 11 |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 12 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 13 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 14 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 15 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 16 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 17 |
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 |
|
| 19 |
# This sudo helper exists, because sudo does not support regular expressions |
| 20 |
# and the supported wildcards cannot be secured. Example: |
| 21 |
# `/tmp/image-factory/../../etc/shadow` matches `/tmp/image-factory*` |
| 22 |
|
| 23 |
MOUNTDIR=/tmp/image-factory |
| 24 |
passed= |
| 25 |
|
| 26 |
if [[ "$*" =~ ^chmod( -R)?\ o[+-]r?w?x?\ ${MOUNTDIR}\.(\.?[A-Za-z0-9_/-]+)*$ ]]; then |
| 27 |
passed=yes |
| 28 |
elif [[ "$*" =~ ^mount\ /dev/loop[0-9]*\ ${MOUNTDIR}\.[A-Za-z0-9_/-]*$ ]]; then |
| 29 |
passed=yes |
| 30 |
elif [[ "$*" =~ ^umount\ ${MOUNTDIR}\.[A-Za-z0-9_/-]*$ ]]; then |
| 31 |
passed=yes |
| 32 |
fi |
| 33 |
|
| 34 |
if test -z "$passed"; then |
| 35 |
echo "${0##*/}: Command '$*' not allowed." >&2 |
| 36 |
exit 1 |
| 37 |
fi |
| 38 |
|
| 39 |
if test -n "${DRYRUN:-}"; then |
| 40 |
echo "${0##*/}: $*" |
| 41 |
else |
| 42 |
exec "$@" |
| 43 |
fi |