aboutsummaryrefslogtreecommitdiffstats
path: root/modules/scripts/gopass-ssh-load/script.sh
blob: e8bc0eea941b3ae674e1bb70f17e41af400515df (plain)
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
export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"

if [ -z "$SSH_AUTH_SOCK" ] || [ ! -S "$SSH_AUTH_SOCK" ]; then
  echo "Error: SSH_AUTH_SOCK is not set or valid." >&2
  exit 1
fi

if ssh-add -l 2>/dev/null | grep -qE "(ED25519|RSA|ECDSA)"; then
  exit 0
fi

for key in github gitlab bitbucket codeberg sourcehut; do
  if gopass cat "ssh/$key" > /dev/null 2>&1; then
    tmpdir=$(mktemp -d)
    keyfile="$tmpdir/key"
    gopass cat "ssh/$key" > "$keyfile" 2>/dev/null
    chmod 600 "$keyfile"

    if ! ssh-add "$keyfile" 2>/dev/null; then
      passphrase=$(gopass cat "ssh/$key/passphrase" 2>/dev/null)
      if [ -n "$passphrase" ]; then
        tmpcopy=$(mktemp)
        cp "$keyfile" "$tmpcopy"
        chmod 600 "$tmpcopy"
        if ssh-keygen -p -P "$passphrase" -N "" -f "$tmpcopy" 2>/dev/null; then
          ssh-add "$tmpcopy" 2>/dev/null
        fi
        rm -f "$tmpcopy"
      fi
    fi
    rm -rf "$tmpdir"
  fi
done