From 67417a609a8720add3090e69f7e244fbf290cef5 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Sat, 22 Jan 2022 14:27:47 -0800 Subject: [PATCH] feat: refactored sshfs function. Now accepts -p, -o, -F, -u -n. by default will mount with the current user. use -u for different user, and -n for no user. -F for alternate ssh config file, -- for alternate port (22 is default) -o for additional sshfs options. --- all/modules/sshfs.sh | 66 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/all/modules/sshfs.sh b/all/modules/sshfs.sh index f9cf730..1625b38 100755 --- a/all/modules/sshfs.sh +++ b/all/modules/sshfs.sh @@ -8,23 +8,69 @@ function smount() { local HOST local PORT - local config + local CONFIG=$SSH_CONFIG + local PORT=22 + + declare SSHOPTS + declare OPTION + declare OPTARG + declare OPTIND + declare MNTUSER + while getopts 'u:np:o:F:' OPTION; do + # echo $OPTION $OPTARG + case "$OPTION" in + p) + PORT=$OPTARG + # echo option d: $DIRS + ;; + u) + MNTUSER=$OPTARG + ;; + n) + MNTUSER=_NONE_ + ;; + o) + echo "adding sshfs option: $OPTARG" + SSHOPTS="$SSHOPTS -o $OPTARG" + ;; + F) + echo "using SSH Config file at: $OPTARG" + CONFIG=$OPTARG + ;; + + *) + echo unknown option $OPTION + ;; + esac + done + + shift $((OPTIND - 1)) + + # first item is nowsource, second is local mount point, third is possbile local user HOST=$(sed 's/.*@\(.*\):.*/\1/' <<<"$1") - # TODO sed search for -p and extract port, below assumes 3 and 4 position - PORT=$([[ $3 = "-p" ]] && echo $4 || echo 22) + [[ ! $(host_reachable $HOST $PORT) ]] && echo host $HOST not reachable, aborting mount && return 1 if [[ $(mounted $2) ]]; then echo "aborting mount: $1 already mounted at $2" else - echo "SSHFS: mounting $1 at $2" mkdir -p $2 # can add any options after mount point directory like -o default_permissions - if [[ $SSH_CONFIG ]]; then - [[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG" - config=$([[ -f $SSH_CONFIG ]] && echo "-F $SSH_CONFIG") - fi - # echo sshfs "$*" "$config" - sshfs $* $config + config=$([[ -f $CONFIG ]] && echo "-F $CONFIG") + if [[ ! $MNTUSER == "_NONE_" ]]; then + MNTUSER=${MNTUSER:-$USER} + id=$(id -u ${MNTUSER}) + if [[ $id ]]; then + SSHOPTS="$SSHOPTS -o uid=$id -o allow_other" + else + echo no user ${MNTUSER} on this machine, aborting mount + return 1 + fi + else + MNTUSER="" + fi + args="-p $PORT $SSHOPTS $config $1 $2" + echo SSHFS $([[ $MNTUSER ]] && echo mounted as user ${MNTUSER}): $args + sshfs $args fi }