-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitgpg-server
More file actions
executable file
·145 lines (87 loc) · 2.29 KB
/
gitgpg-server
File metadata and controls
executable file
·145 lines (87 loc) · 2.29 KB
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
#!/bin/sh
set -eu
stderr() {
printf "%b\n" "$@" 1>&2
}
reportError() {
stderr "$2"
return "$1"
}
commandv() {
command -v "$1" || reportError "$?" "Executable '$1' not found"
}
# Check dependencies
gpg="$(commandv gpg)"
mkfifo="$(commandv mkfifo)"
timeout="$(commandv timeout)"
ARGS="$*"
# https://stackoverflow.com/a/8811800
# If ARGS does not contain '-bsau' then not signing request
if [ "${ARGS#*"-bsau"}" = "$ARGS" ]; then
exec "$gpg" "$@"
fi
if [ -z "${GITGPG_SSH_DIR+x}" ]; then
stderr "Missing environment GITGPG_SSH_DIR"
stderr "Reverting to gpg"
exec "$gpg" "$@"
fi
if [ ! -d "$GITGPG_SSH_DIR" ]; then
stderr "No such directory '$GITGPG_SSH_DIR'"
exit 1
fi
if [ -z "${GITGPG_SIGNING_REQUEST_KEY+x}" ]; then
stderr "Missing environment GITGPG_SIGNING_REQUEST_KEY"
exit 1
fi
checkForPipe() {
if [ ! -p "$1" ]; then
$mkfifo "$1"
fi
}
STDIN_PIPE="$GITGPG_SSH_DIR/stdin.pipe"
STDOUT_PIPE="$GITGPG_SSH_DIR/stdout.pipe"
STDERR_PIPE="$GITGPG_SSH_DIR/stderr.pipe"
EXITCODE_PIPE="$GITGPG_SSH_DIR/exitcode.pipe"
checkForPipe "$STDIN_PIPE"
checkForPipe "$STDOUT_PIPE"
checkForPipe "$STDERR_PIPE"
checkForPipe "$EXITCODE_PIPE"
$timeout 3s sh <<-EOF || reportError "$?" "Timeout: likely broken pipe.\nTry to restart session"
printf "%s" "BEGIN SIGNING REQUEST $GITGPG_SIGNING_REQUEST_KEY" > "$STDIN_PIPE"
EXITCODE="\$(cat "$EXITCODE_PIPE")"
if [ "\$EXITCODE" -ne 0 ]; then
printf "%s\n" "Failed to initiate signing request" 1>&2
exit "\$EXITCODE"
fi
EOF
# Ignore warning about unreachable statements, because this is triggered via trap
# shellcheck disable=SC2317
cleanupFd() {
exec 8<&-
exec 9<&-
exec 5<&-
exec 6<&-
kill "$PID_STDOUT_STEAMER" 2> /dev/null || true # Allow fail
kill "$PID_STDERR_STEAMER" 2> /dev/null || true # Allow fail
}
trap 'cleanupFd' EXIT INT
INPUT="$(cat)"
printf "%s" "$INPUT" > "$STDIN_PIPE"
# stderr "Setting up pipes descriptors"
exec 8<"$STDOUT_PIPE"
exec 9<"$STDERR_PIPE"
# Create FDs for current stdout and stderr
# so they can be used by background processes
exec 5>&1
exec 6>&2
# Stream content from pipe files into stdout and stderr
cat <&8 >&5 &
PID_STDOUT_STEAMER="$!"
cat <&9 >&6 &
PID_STDERR_STEAMER="$!"
wait
# stderr "waiting for exit code"
EXITCODE="$(cat "$EXITCODE_PIPE")"
exit "$EXITCODE"
# have a timeout
# timeout 2s