Last active
August 8, 2022 22:29
-
-
Save Zoddo/035784d640ecf9156fa471534deb1e1f to your computer and use it in GitHub Desktop.
A script to use as an authorized_keys forced command to allow only SCP within a defined directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [[ -z "$SSH_ORIGINAL_COMMAND" || "${SSH_ORIGINAL_COMMAND%% *}" != "scp" ]]; then | |
echo "Error: You don't have access to any shell" >&2 | |
exit 1 | |
fi | |
#set -- $SSH_ORIGINAL_COMMAND | |
umask 377 | |
while getopts "vrdt:f:" opt ${SSH_ORIGINAL_COMMAND#* }; do | |
case $opt in | |
v) | |
# verbose | |
OPT_V="-v" | |
;; | |
r) | |
# recusrive | |
OPT_R="-r" | |
;; | |
d) | |
# directory | |
OPT_D="-d" | |
;; | |
t) | |
# Transfer from the source | |
CANONICAL_TARGET=$(readlink -m "$1$OPTARG") | |
if [[ "${CANONICAL_TARGET}" != $(readlink -m "$1")* ]]; then | |
echo "Error: Unsafe target" >&2 | |
exit 2 | |
fi | |
OPT_T="-t ${CANONICAL_TARGET}" | |
mkdir -pm 700 "${CANONICAL_TARGET}" | |
;; | |
f) | |
# Transfer to the source | |
CANONICAL_TARGET=$(readlink -m "$1$OPTARG") | |
if [[ "${CANONICAL_TARGET}" != $(readlink -m "$1")* ]]; then | |
echo "Error: Unsafe target" >&2 | |
exit 2 | |
fi | |
OPT_T="-f ${CANONICAL_TARGET}" | |
;; | |
esac | |
done | |
scp $OPT_V $OPT_R $OPT_D $OPT_T |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment