aboutsummaryrefslogtreecommitdiff
path: root/passh
blob: 092e1242a67474b083438f311c5dbcccab5723b0 (plain) (blame)
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
#!/bin/sh

PASS_PREFIX="tc/ssh/"

host="$1"

# Open an "anonymous" fifo in fd 3
pipe="$(mktemp -u)" # get random path
mkfifo "$pipe" # create a fifo there
exec 3<> "$pipe" # open fifo rw at fd 3
rm "$pipe" # remove file

echo -n "Trying to get password from 'pass'... "
if pass show "${PASS_PREFIX}$host" 2> /dev/null >&3; then
    echo "FOUND ('${PASS_PREFIX}$host'). Connecting..."
    set -x
    exec sshpass -d3 ssh -v -o StrictHostKeyChecking=accept-new $@
else
    echo "not found. Trying pubkeys..."
    set -x
    ssh -o NumberOfPasswordPrompts=0 -v -o StrictHostKeyChecking=accept-new $@
    ok=$?
    set +x
    if [ "$ok" -ne 0 ]; then
        read -rp "Connection via pubkey failed, password? " passphrase
        echo "$passphrase" >&3
        echo "Connecting with passphrase..."
        set -x
        sshpass -d3 ssh -o StrictHostKeyChecking=accept-new $@
        ok=$?
        set +x
        if [ "$ok" -eq 0 ]; then
            echo "It worked! Storing password..."
            echo "$passphrase" | pass insert -e "${PASS_PREFIX}$host"
        else
            echo "Unable to connect with password either; giving up."
        fi
    fi
fi