|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright (C) 2011 by Wayne Walker <wwalker@solid-constructs.com> |
| 4 | +# |
| 5 | +# Released under one of the versions of the MIT License. |
| 6 | +# |
| 7 | +# Copyright (C) 2011 by Wayne Walker <wwalker@solid-constructs.com> |
| 8 | +# |
| 9 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +# of this software and associated documentation files (the "Software"), to deal |
| 11 | +# in the Software without restriction, including without limitation the rights |
| 12 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +# copies of the Software, and to permit persons to whom the Software is |
| 14 | +# furnished to do so, subject to the following conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be included in |
| 17 | +# all copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +# THE SOFTWARE. |
| 26 | + |
| 27 | +sfa_init() { |
| 28 | + _ssh_agent_sockets=() |
| 29 | + _live_agent_list=() |
| 30 | + _live_agent_sock_list=() |
| 31 | + _sorted_live_agent_list=() |
| 32 | + |
| 33 | + # Set $sfa_path array to the dirs to search for ssh-agent sockets |
| 34 | + sfa_set_path |
| 35 | + |
| 36 | + if ! command -v 'timeout' &>/dev/null; then |
| 37 | + printf "ssh-find-agent.sh: 'timeout' command could not be found.\n" |
| 38 | + printf " Please install 'coreutils' via your system's package manager.\n" |
| 39 | + fi |
| 40 | +} |
| 41 | + |
| 42 | +# Allow users to override the default path to search for ssh-agent sockets |
| 43 | +# The first of the variable found is used to set the path: |
| 44 | +# SSH_FIND_AGENT_PATH (colon separated dir list) |
| 45 | +# _TMPDIR_OVERRIDE for legacy compatibility |
| 46 | +# TMPDIR (if set) (plus /tmp due to ssh bug) |
| 47 | +sfa_set_path() { |
| 48 | + sfa_path=() |
| 49 | + if [[ -n "$SSH_FIND_AGENT_PATH" ]]; then |
| 50 | + IFS=':' read -r -a sfa_path <<<"$SSH_FIND_AGENT_PATH" |
| 51 | + else |
| 52 | + # Maintain backwards compatibility with the old _TMPDIR_OVERRIDE variable |
| 53 | + if [[ -n "$_TMPDIR_OVERRIDE" ]]; then |
| 54 | + sfa_path=("$_TMPDIR_OVERRIDE") |
| 55 | + else |
| 56 | + if [[ -n "$TMPDIR" ]]; then |
| 57 | + sfa_path=("/tmp" "$TMPDIR") |
| 58 | + else |
| 59 | + sfa_path=("/tmp") |
| 60 | + fi |
| 61 | + fi |
| 62 | + fi |
| 63 | +} |
| 64 | + |
| 65 | +sfa_err() { |
| 66 | + # shellcheck disable=SC2059 |
| 67 | + printf "$@" 1>&2 |
| 68 | +} |
| 69 | + |
| 70 | +sfa_debug() { |
| 71 | + if ((_DEBUG > 0)); then |
| 72 | + sfa_err "$@" 1>&2 |
| 73 | + fi |
| 74 | +} |
| 75 | + |
| 76 | +sfa_find_all_agent_sockets() { |
| 77 | + _ssh_agent_sockets=$( |
| 78 | + find "${sfa_path[@]}" -maxdepth 2 -type s -name agent.\* 2>/dev/null | grep '/ssh-.*/agent.*' |
| 79 | + find "${sfa_path[@]}" -maxdepth 2 -type s -name S.gpg-agent.ssh 2>/dev/null | grep '/gpg-.*/S.gpg-agent.ssh' |
| 80 | + find "${sfa_path[@]}" -maxdepth 2 -type s -name ssh 2>/dev/null | grep '/keyring-.*/ssh$' |
| 81 | + find "${sfa_path[@]}" -maxdepth 2 -type s -regex '.*/ssh-.*/agent..*$' 2>/dev/null |
| 82 | + ) |
| 83 | + sfa_debug "$_ssh_agent_sockets" |
| 84 | +} |
| 85 | + |
| 86 | +sfa_test_agent_socket() { |
| 87 | + local socket=$1 |
| 88 | + local output |
| 89 | + output=$(SSH_AUTH_SOCK=$socket timeout 0.4 ssh-add -l 2>&1) |
| 90 | + result=$? |
| 91 | + |
| 92 | + [[ "$output" == "error fetching identities: communication with agent failed" ]] && result=2 |
| 93 | + sfa_debug $result |
| 94 | + |
| 95 | + case $result in |
| 96 | + 0 | 1 | 141) |
| 97 | + # contactible and has keys loaded |
| 98 | + { |
| 99 | + OIFS="$IFS" |
| 100 | + IFS=$'\n' |
| 101 | + # shellcheck disable=SC2207 |
| 102 | + _keys=($(SSH_AUTH_SOCK=$socket ssh-add -l 2>/dev/null)) |
| 103 | + IFS="$OIFS" |
| 104 | + } |
| 105 | + _live_agent_list+=("${#_keys[@]}:$socket") |
| 106 | + return 0 |
| 107 | + ;; |
| 108 | + 2 | 124) |
| 109 | + # socket is dead, delete it |
| 110 | + sfa_err 'socket (%s) is dead, removing it.\n' "$socket" |
| 111 | + sfa_debug "rm -rf ${socket%/*}" |
| 112 | + rm -rf "${socket%/*}" |
| 113 | + ;; |
| 114 | + 125 | 126 | 127) |
| 115 | + sfa_err 'timeout returned <%s>\n' "$result" 1>&2 |
| 116 | + ;; |
| 117 | + *) |
| 118 | + sfa_err 'Unknown failure timeout returned <%s>\n' "$result" 1>&2 |
| 119 | + ;; |
| 120 | + esac |
| 121 | + |
| 122 | + case $result in |
| 123 | + 0 | 1) |
| 124 | + _live_agent_list+=("$_key_count:$socket") |
| 125 | + return 0 |
| 126 | + ;; |
| 127 | + esac |
| 128 | + |
| 129 | + return 1 |
| 130 | +} |
| 131 | + |
| 132 | +sfa_verify_sockets() { |
| 133 | + for i in $_ssh_agent_sockets; do |
| 134 | + sfa_test_agent_socket "$i" |
| 135 | + done |
| 136 | +} |
| 137 | + |
| 138 | +sfa_fingerprints() { |
| 139 | + local file="$1" |
| 140 | + while read -r l; do |
| 141 | + [[ -n "$l" && ${l##\#} = "$l" ]] && ssh-keygen -l -f /dev/stdin <<<"$l" |
| 142 | + done <"$file" |
| 143 | +} |
| 144 | + |
| 145 | +sfa_print_choose_menu() { |
| 146 | + # find all the apparent socket files |
| 147 | + # the sockets go into $_ssh_agent_sockets[] |
| 148 | + sfa_find_all_agent_sockets |
| 149 | + |
| 150 | + # verify each socket, discarding if dead |
| 151 | + # the live sockets go into $_live_agent_list[] |
| 152 | + sfa_verify_sockets |
| 153 | + sfa_debug '<%s>\n' "${_live_agent_list[@]}" |
| 154 | + |
| 155 | + # shellcheck disable=SC2207 |
| 156 | + IFS=$'\n' _sorted_live_agent_list=($(sort -u <<<"${_live_agent_list[*]}")) |
| 157 | + unset IFS |
| 158 | + |
| 159 | + sfa_debug "SORTED:\n" |
| 160 | + sfa_debug ' <%s>\n' "${_sorted_live_agent_list[@]}" |
| 161 | + |
| 162 | + local i=0 |
| 163 | + local sock |
| 164 | + |
| 165 | + for agent in "${_sorted_live_agent_list[@]}"; do |
| 166 | + i=$((i + 1)) |
| 167 | + sock=${agent/*:/} |
| 168 | + if [[ "$1" = "-i" ]]; then |
| 169 | + _live_agent_sock_list[$i]=$sock |
| 170 | + |
| 171 | + printf '#%i)\n' "$i" |
| 172 | + printf ' export SSH_AUTH_SOCK=%s\n' "$sock" |
| 173 | + # Get all the forwarded keys for this agent, parse them and print them |
| 174 | + SSH_AUTH_SOCK=$sock ssh-add -l 2>&1 | |
| 175 | + grep -v 'error fetching identities for protocol 1: agent refused operation' | |
| 176 | + while IFS= read -r key; do |
| 177 | + parts=("$key") |
| 178 | + key_size="${parts[0]}" |
| 179 | + fingerprint="${parts[1]}" |
| 180 | + remote_name="${parts[2]}" |
| 181 | + key_type="${parts[3]}" |
| 182 | + printf ' %s %s\t%s\t%s\n' "$key_size" "$key_type" "$remote_name" "$fingerprint" |
| 183 | + done |
| 184 | + else |
| 185 | + printf '%s\n' "$sock" |
| 186 | + fi |
| 187 | + done |
| 188 | +} |
| 189 | + |
| 190 | +sfa_set_ssh_agent_socket() { |
| 191 | + case $1 in |
| 192 | + -c | --choose) |
| 193 | + sfa_print_choose_menu -i |
| 194 | + |
| 195 | + ((0 == ${#_live_agent_list[@]})) && { |
| 196 | + sfa_err 'No agents found.\n' |
| 197 | + return 1 |
| 198 | + } |
| 199 | + |
| 200 | + read -p "Choose (1-${#_live_agent_sock_list[@]})? " -r choice |
| 201 | + if [ "$choice" -eq "$choice" ]; then |
| 202 | + [[ -z "${_live_agent_sock_list[$choice]}" ]] && { |
| 203 | + sfa_err 'Invalid choice.\n' |
| 204 | + return 1 |
| 205 | + } |
| 206 | + printf 'Setting export SSH_AUTH_SOCK=%s\n' "${_live_agent_sock_list[$choice]}" |
| 207 | + export SSH_AUTH_SOCK=${_live_agent_sock_list[$choice]} |
| 208 | + fi |
| 209 | + ;; |
| 210 | + -a | --auto) |
| 211 | + # Choose the last one, as they are sorted numerically by how many keys they have |
| 212 | + sock=$(sfa_print_choose_menu | tail -n -1) |
| 213 | + [[ -z "$sock" ]] && return 1 |
| 214 | + sfa_debug 'export SSH_AUTH_SOCK=%s\n' "$sock" |
| 215 | + export SSH_AUTH_SOCK=$sock |
| 216 | + ;; |
| 217 | + *) |
| 218 | + sfa_usage |
| 219 | + ;; |
| 220 | + esac |
| 221 | + |
| 222 | + # set agent pid - this is unreliable as the pid may be of the child rather than the agent |
| 223 | + if [ -n "$SSH_AUTH_SOCK" ]; then |
| 224 | + export SSH_AGENT_PID=$(($(basename "$SSH_AUTH_SOCK" | cut -d. -f2) + 1)) |
| 225 | + fi |
| 226 | + |
| 227 | + return 0 |
| 228 | +} |
| 229 | + |
| 230 | +sfa_usage() { |
| 231 | + sfa_err 'ssh-find-agent <[-c|--choose|-a|--auto|-h|--help]>\n' |
| 232 | + return 1 |
| 233 | +} |
| 234 | + |
| 235 | +# Renamed for https://github.com/wwalker/ssh-find-agent/issues/12 |
| 236 | +ssh_find_agent() { |
| 237 | + sfa_init |
| 238 | + |
| 239 | + case $1 in |
| 240 | + -c | --choose | -a | --auto) |
| 241 | + sfa_set_ssh_agent_socket "$1" |
| 242 | + return $? |
| 243 | + ;; |
| 244 | + -l | --list) |
| 245 | + sfa_print_choose_menu -i |
| 246 | + ;; |
| 247 | + *) |
| 248 | + sfa_usage |
| 249 | + ;; |
| 250 | + esac |
| 251 | +} |
| 252 | + |
| 253 | +# Original function name is still supported. |
| 254 | +# https://github.com/wwalker/ssh-find-agent/issues/12 points out that I |
| 255 | +# should use ssh_find_agent() for best compatibility. |
| 256 | +ssh-find-agent() { |
| 257 | + ssh_find_agent "$@" |
| 258 | +} |
0 commit comments