~miconoco/keyring

ref: 1 keyring/keyring -rwxr-xr-x 3.2 KiB
6c442f08 — Christoph Polcin support -e and --eval as first argument 9 years ago
                                                                                
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/sh
# https://git.christoph-polcin.com/keyring/
# Simplified BSD License

[ -z $GPG_INFO_FILE ] && GPG_INFO_FILE="/tmp/gpg-agent-info.$USER"
[ -z $SSH_INFO_FILE ] && SSH_INFO_FILE="/tmp/ssh-agent-info.$USER"
[ -z $PASS_KEY_PREFIX ] && PASS_KEY_PREFIX='keyring'

# GPG & SSH Agent setup {{{
INIT=0
print_exports() {
    echo "GPG_AGENT_INFO=$GPG_AGENT_INFO; export GPG_AGENT_INFO;"
    echo "GPG_AGENT_PID=$GPG_AGENT_PID; export GPG_AGENT_PID;"
    echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK; export SSH_AUTH_SOCK;"
    echo "SSH_AGENT_PID=$SSH_AGENT_PID; export SSH_AGENT_PID;"
    echo "SSH_ASKPASS=$0; export SSH_ASKPASS;"
}

_source_gpg_info() {
    . "$GPG_INFO_FILE" > /dev/null 2>&1
    export GPG_AGENT_INFO
    export GPG_AGENT_PID
}

_source_ssh_info() {
    . "$SSH_INFO_FILE" > /dev/null 2>&1
    export SSH_AUTH_SOCK
    export SSH_AGENT_PID
    export SSH_ASKPASS
}

[ -e "$GPG_INFO_FILE" ] && _source_gpg_info

if [ -z $GPG_AGENT_PID ] || \
        [ "$(ps x | grep -s 'gpg-agent' | grep -sc $GPG_AGENT_PID)" -lt '1' ]
then
    INIT=1
    gpg-agent --daemon -q --write-env-file "$GPG_INFO_FILE" -s > /dev/null 2>&1
    chmod 640 "$GPG_INFO_FILE"
    GPG_AGENT_PID=`grep -o 'GPG_AGENT_INFO.*:\(.*\):' "$GPG_INFO_FILE" | cut -d: -f2`
    echo "GPG_AGENT_PID=$GPG_AGENT_PID; export GPG_AGENT_PID;" >> "$GPG_INFO_FILE"
    _source_gpg_info
fi


[ -e "$SSH_INFO_FILE" ] && _source_ssh_info

if [ -z $SSH_AGENT_PID ] || \
        [ "$(ps x | grep -s 'ssh-agent' | grep -sc $SSH_AGENT_PID)" -lt '1' ]
then
    INIT=1
    ssh-agent -s > "$SSH_INFO_FILE"
    chmod 640 "$SSH_INFO_FILE"
    echo "SSH_ASKPASS=$0; export SSH_ASKPASS;" >> "$SSH_INFO_FILE"
    _source_ssh_info
fi
[ $INIT -eq 1 ] && sleep 1
# }}}

[ $# -eq 0 ] && exit 0

# options {{{
if [ "$1" = '--eval' ] || [ "$1" = '-e' ]; then
    shift
    print_exports
fi

if [ "$1" = '--add' ] || [ "$1" = '-a' ]; then
    if [ $# -gt 1 ]; then
        shift
        echo "keyring +" >&2
        ssh-add $@ </dev/null >&2
        exit $?
    fi
    echo "ArgumentError: missing keys" >&2
    exit 1
fi

if [ "$1" = '--clear' ] || [ "$1" = '-c' ]; then
    ssh-add -D > /dev/null 2>&1
    pkill -SIGHUP gpg-agent > /dev/null 2>&1
    echo "All identities removed"
fi

if [ "$1" = '--kill' ] || [ "$1" = '-k' ]; then
    if [ $# -eq 2 -a "$2" = 'all' ]; then
        killall -9 ssh-agent
        killall -9 gpg-agent
        echo "All agents killed"
    else
        kill $SSH_AGENT_PID > /dev/null 2>&1
        kill $GPG_AGENT_PID > /dev/null 2>&1
        echo "My agents killed"
    fi
    exit 0
fi

if [ "$1" = '--list' ] || [ "$1" = '-l' ]; then
    echo "SSH-Agent pid: $SSH_AGENT_PID"
    ssh-add -l
    echo "GPG-Agent pid: $GPG_AGENT_PID"
fi

if [ "$1" = '--help' ] || [ "$1" = '-h' ]; then
cat <<EOF
keyring + https://git.christoph-polcin.com/keyring

Usage:
    keyring -e [--eval] [ARGS]
        foo
    keyring -a [--add] keys...
        foo
    keyring -c [--clear]
        foo
    keyring -h [--help]
        foo
    keyring -k [--kill] [all]
        foo
    keyring -l [--list]
        foo
    keyring KEY:
        bar
EOF
fi
# }}}

# retrieve passphrase for key (SSH_ASKPASS style) {{{
KEY=$( echo "$@" | sed -n 's/^.*[ \/]\([^\/].*\):\s*$/\1/p' )
if [ ! -z $KEY ]
then
    pass show "$PASS_KEY_PREFIX/$KEY"
else
    exit 1
fi
# }}}