This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathwin-gpg-agent-relay
executable file
·212 lines (176 loc) · 6.11 KB
/
win-gpg-agent-relay
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash
# Script origins:
# https://gist.github.com/andsens/2ebd7b46c9712ac205267136dc677ac1
# https://gist.github.com/Nimamoh/e2df2ba0a99ef221d8cca360c931e5e6
# For debugging startup problems uncomment next line
# exec 2> >(tee -a -i "$HOME/error.log")
GNUPGHOME="$HOME/.gnupg"
if [ ! -d ${GNUPGHOME} ]; then
mkdir ${GNUPGHOME}
chmod 0700 ${GNUPGHOME}
fi
PIDFILE="$GNUPGHOME/win-gpg-agent-relay.pid"
SORELAY_BIN="${HOME}/winhome/.wsl/sorelay.exe"
WINAGENT_HOME_DIR="${WIN_AGENT_HOME}"
WINGPGAGENT_SOCKETS_DIR="${WIN_GNUPG_SOCKETS}"
GPG_AGENT_SOCK="$WINGPGAGENT_SOCKETS_DIR/S.gpg-agent"
GPG_AGENT_EXTRA_SOCK="$WINGPGAGENT_SOCKETS_DIR/S.gpg-agent.extra"
GPG_AGENT_SSH_SOCK="$WINAGENT_HOME_DIR/S.gpg-agent.ssh"
log() {
echo >&2 "$@"
}
is_pid_running() {
if [[ -z "$1" ]]; then
return 1
fi
ps -p "$1" >/dev/null
return $?
}
_cleanup() {
log "Cleaning up relay to $GPG_AGENT_SOCK..."
if is_pid_running "$SOCAT_GPG_AGENT_PID"; then
kill -SIGTERM "$SOCAT_GPG_AGENT_PID" || log "Failed."
fi
log "Cleaning up relay to $GPG_AGENT_EXTRA_SOCK..."
if is_pid_running "$SOCAT_GPG_AGENT_EXTRA_PID"; then
kill -SIGTERM "$SOCAT_GPG_AGENT_EXTRA_PID" || log "Failed."
fi
log "Cleaning up relay to $GPG_AGENT_SSH_SOCK..."
if is_pid_running "$SOCAT_GPG_AGENT_SSH_PID"; then
kill -SIGTERM "$SOCAT_GPG_AGENT_SSH_PID" || log "Failed."
fi
}
die() {
if [[ -n "$1" ]]; then
log "$1"
fi
log "Exiting."
exit 1
}
usage() {
log "Usage: win-gpg-agent-relay [OPTIONS] COMMAND"
log ""
log " SUMMARY: Relay local GPG sockets to win-gpg-agent's ones in order to integrate WSL2 and host."
log " Do debug use foreground command"
log ""
log " OPTIONS:"
log " -h|--help this page"
log ""
log " -v|--verbose verbose mode"
log ""
log " COMMAND: start, stop, foreground"
}
fg_opts() {
FG_OPTS=()
# Generate opts for passing it to foreground version
if [[ -n "$VERBOSE" ]]; then
FG_OPTS+=("-v")
fi
}
main() {
POSITIONAL=()
VERBOSE=""
while (($# > 0)); do
case "$1" in
-v | --verbose)
VERBOSE="ENABLED"
shift # shift once since flags have no values
;;
-h | --help)
usage
exit 0
;;
*) # unknown flag/switch
POSITIONAL+=("$1")
shift
if [[ "${#POSITIONAL[@]}" -gt 1 ]]; then
usage
die
fi
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional params
if [[ -z "$VERBOSE" ]]; then
QUIET="QUIET"
fi
case "${POSITIONAL[0]}" in
start)
fg_opts
start-stop-daemon --start --oknodo --pidfile "$PIDFILE" --name win-gpg-agent-r --make-pidfile --background --startas "$0" ${VERBOSE:+--verbose} ${QUIET:+--quiet} -- foreground "${FG_OPTS[@]}"
;;
stop)
start-stop-daemon --pidfile "$PIDFILE" --stop --remove-pidfile ${VERBOSE:+--verbose} ${QUIET:+--quiet}
;;
status)
start-stop-daemon --pidfile "$PIDFILE" --status ${VERBOSE:+--verbose} ${QUIET:+--quiet}
local result=$?
case $result in
0) log "$0 is running" ;;
1 | 3) log "$0 is not running" ;;
4) log "$0 unable to determine status" ;;
esac
return $result
;;
foreground)
relay
;;
*)
usage
die
;;
esac
}
relay() {
trap _cleanup EXIT
log "Using gpg-agent sockets in: $WINGPGAGENT_SOCKETS_DIR"
log "Using agent-gui sockets in: $WINAGENT_HOME_DIR"
[[ -f "${SORELAY_BIN}" ]] || die "Unable to access ${SORELAY_BIN}"
[[ -z "$WINGPGAGENT_SOCKETS_DIR" ]] && die "Wrong directory of gpg-agent sockets"
[[ -z "$WINAGENT_HOME_DIR" ]] && die "Wrong directory of agent-gui home"
if pgrep -fx "^gpg-agent\s.+" >/dev/null; then
log "Killing previously started local gpg-agent..."
echo "KILLAGENT" | gpg-connect-agent >/dev/null 2>&1
fi
if [ -e "$HOME/.gnupg/S.gpg-agent" ] || [ -e "$HOME/.gnupg/S.gpg-agent.extra" ] || [ -e "$HOME/.gnupg/S.gpg-agent.ssh" ]; then
log "WSL has been shutdown ungracefully, leaving garbage behind"
rm "$HOME/.gnupg/S.gpg-agent" "$HOME/.gnupg/S.gpg-agent.extra" "$HOME/.gnupg/S.gpg-agent.ssh"
fi
socat UNIX-LISTEN:"\"$HOME/.gnupg/S.gpg-agent\"",fork EXEC:"\"\'$SORELAY_BIN\' -a \'$GPG_AGENT_SOCK\'\"",nofork 1>/dev/null 2>&1 &
SOCAT_GPG_AGENT_PID="$!"
if ! is_pid_running "$SOCAT_GPG_AGENT_PID"; then
log "socat $SOCAT_GPG_AGENT_PID failed"
return 1
fi
log "socat running with PID: $SOCAT_GPG_AGENT_PID"
socat UNIX-LISTEN:"\"$HOME/.gnupg/S.gpg-agent.extra\"",fork EXEC:"\"\'$SORELAY_BIN\' -a \'$GPG_AGENT_EXTRA_SOCK\'\"",nofork 1>/dev/null 2>&1 &
SOCAT_GPG_AGENT_EXTRA_PID="$!"
if ! is_pid_running "$SOCAT_GPG_AGENT_EXTRA_PID"; then
log "socat $SOCAT_GPG_AGENT_EXTRA_PID failed"
return 1
fi
log "socat running with PID: $SOCAT_GPG_AGENT_EXTRA_PID"
socat UNIX-LISTEN:"\"$HOME/.gnupg/S.gpg-agent.ssh\"",fork EXEC:"\"\'$SORELAY_BIN\' \'$GPG_AGENT_SSH_SOCK\'\"",nofork 1>/dev/null 2>&1 &
SOCAT_GPG_AGENT_SSH_PID="$!"
if ! is_pid_running "$SOCAT_GPG_AGENT_SSH_PID"; then
log "socat $SOCAT_GPG_AGENT_SSH_PID failed"
return 1
fi
log "socat running with PID: $SOCAT_GPG_AGENT_SSH_PID"
log -n "Polling remote ssh-agent..."
res=`SSH_AUTH_SOCK="$HOME/.gnupg/S.gpg-agent.ssh" ssh-add -L 2>&1`
if [ "$?" -ne 0 ]; then
# When hardware smartcard is used to keep private keys gpg will report no identities here
if ! echo "${res}" | grep -q "The agent has no identities"; then
die "Failure communicating with ssh-agent"
fi
fi
log "OK"
log -n "Polling remote gpg-agent... "
gpg-connect-agent /bye >/dev/null 2>&1 || die "[$?] Failure communicating with gpg-agent"
log "OK"
# Everything checks, we are ready for actions
log "Entering wait..."
wait $SOCAT_GPG_AGENT_PID $SOCAT_GPG_AGENT_EXTRA_PID $SOCAT_GPG_AGENT_SSH_PID
}
main "$@"