Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·464 lines (439 loc) · 16.1 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·464 lines (439 loc) · 16.1 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/bin/sh
# qsh installer for Linux, macOS and Termux:
# curl -fsSL https://github.com/VLOD-ZDOV/quic-ssh/releases/latest/download/install.sh | sh
# Asks what to install. Without a terminal, or to skip the questions:
# ... | sh -s -- --client | --server [--service | --no-service] [--share | --no-share]
# [--port N] [--totp off|optional|required] [--defaults]
# QSH_VERSION=v1.0.2 picks a release (default: the latest), QSH_BIN_DIR the install directory.
set -eu
REPO=VLOD-ZDOV/quic-ssh
say() { printf '%s\n' "$*"; }
die() { printf 'qsh install: %s\n' "$*" >&2; exit 1; }
usage() {
cat <<EOF
Usage: install.sh [--client | --server | --update] [--service | --no-service] [--share | --no-share]
[--port N] [--totp off|optional|required] [--defaults]
--client install qsh only
--update update what is installed in QSH_BIN_DIR (what qsh update runs)
--server install qsh and qshd
--service run qshd as a service that starts at boot
--no-service do not set up a service
--share one connection per host for 10 minutes (ControlMaster): later
qsh runs to it skip the login and its one-time code
--no-share leave the client config as it is
--port N qshd listens on port N (UDP and TCP; default 4422)
--totp MODE one-time codes: off, optional (for who sets them up, the
default) or required
--defaults ask nothing more: what is not given keeps its default
Without options it asks. QSH_VERSION picks a release, QSH_BIN_DIR the directory.
EOF
}
what='' service='' share='' port='' totp='' defaults=no
next=''
for a in "$@"; do
case $next in
port) port=$a; next=''; continue ;;
totp) totp=$a; next=''; continue ;;
esac
case $a in
--port) next=port ;;
--port=*) port=${a#--port=} ;;
--totp) next=totp ;;
--totp=*) totp=${a#--totp=} ;;
--defaults) defaults=yes ;;
--client) what=client ;;
--server) what=server ;;
--update) what=update ;;
--service) service=yes ;;
--no-service) service=no ;;
--share) share=yes ;;
--no-share) share=no ;;
-h | --help) usage; exit 0 ;;
*) die "unknown option: $a" ;;
esac
done
[ -z "$next" ] || die "--$next needs a value"
case $port in
"" | default) port='' ;;
*[!0-9]* | 0) die "--port: not a port number: $port" ;;
*) [ "$port" -le 65535 ] || die "--port: not a port number: $port" ;;
esac
case $totp in
"" | default | off | optional | required) [ "$totp" = default ] && totp='' ;;
*) die "--totp: off, optional or required, not $totp" ;;
esac
# Piped into sh, stdin is this script, so questions go to the terminal.
has_tty() { (exec </dev/tty) 2>/dev/null; }
ask() {
printf '%s' "$1" >/dev/tty
read -r REPLY </dev/tty || REPLY=
}
os=$(uname -s)
arch=$(uname -m)
case $arch in
x86_64 | amd64) arch=x86_64 ;;
aarch64 | arm64) arch=aarch64 ;;
*) die "no prebuilt binaries for $arch; build from source (see the README)" ;;
esac
case $os in
Linux)
case ${PREFIX:-} in
*com.termux*) plat=android ;;
*) plat=linux ;;
esac ;;
Darwin) plat=macos ;;
MINGW* | MSYS* | CYGWIN*) die "on Windows run in PowerShell: irm https://github.com/$REPO/releases/latest/download/install.ps1 | iex" ;;
*) die "no prebuilt binaries for $os; build from source (see the README)" ;;
esac
[ "$plat" = android ] && [ "$arch" != aarch64 ] && die "the Termux build is for aarch64 only"
if [ -z "$what" ]; then
if has_tty; then
say "qsh installer ($plat, $arch)"
say " 1) client only (qsh)"
say " 2) client and server (qsh + qshd)"
ask "Choose [1]: "
case $REPLY in
2) what=server ;;
"" | 1) what=client ;;
*) die "unknown choice: $REPLY" ;;
esac
else
what=client
fi
fi
if [ "$what" = server ] && [ -z "$service" ]; then
if has_tty; then
ask "Run qshd as a service that starts at boot? [Y/n]: "
case $REPLY in
"" | [Yy]*) service=yes ;;
*) service=no ;;
esac
else
service=no
fi
fi
# A few settings of qshd, asked once. Enter keeps the default, s skips the
# question; either way the config keeps that setting commented out.
if [ "$what" = server ] && [ "$defaults" = no ] && has_tty; then
say ""
say "qshd settings (Enter: the default in brackets, s: skip):"
while [ -z "$port" ]; do
ask " Port for UDP and TCP [4422]: "
case $REPLY in
"" | [Ss]) break ;;
*[!0-9]* | 0) say " A number from 1 to 65535, please." ;;
*) if [ "$REPLY" -le 65535 ]; then port=$REPLY; else say " A number from 1 to 65535, please."; fi ;;
esac
done
while [ -z "$totp" ]; do
ask " One-time codes: off, optional (for who sets them up), required [optional]: "
case $REPLY in
"" | [Ss]) break ;;
off | optional | required) totp=$REPLY ;;
*) say " off, optional or required, please." ;;
esac
done
fi
[ "$port" = 4422 ] && port=''
[ "$totp" = optional ] && totp=''
# root: how to run commands as root ("" when we are root, "none" when we cannot).
if [ "$(id -u)" = 0 ]; then
root=
elif command -v sudo >/dev/null 2>&1; then
root=sudo
elif command -v doas >/dev/null 2>&1; then
root=doas
else
root=none
fi
as_root() {
[ "$root" = none ] && die "need root for: $*"
$root "$@"
}
if [ "$plat" = android ]; then
bin=${QSH_BIN_DIR:-$PREFIX/bin}
elif [ -n "${QSH_BIN_DIR:-}" ]; then
bin=$QSH_BIN_DIR
elif [ "$root" != none ]; then
bin=/usr/local/bin
else
bin=$HOME/.local/bin
fi
update=no
if [ "$what" = update ]; then
update=yes service=no
[ -x "$bin/qsh" ] || die "no qsh in $bin"
if [ -x "$bin/qshd" ]; then what=server; else what=client; fi
have=$("$bin/qsh" --version 2>&1 | awk '{ print $2 }')
if [ -n "${QSH_VERSION:-}" ]; then
tag=$QSH_VERSION
elif command -v curl >/dev/null 2>&1; then
tag=$(curl -fsSLI -o /dev/null -w '%{url_effective}' "https://github.com/$REPO/releases/latest" 2>/dev/null) || tag=
tag=${tag##*/}
else
tag=
fi
if [ -n "$have" ] && [ "v$have" = "$tag" ]; then
say "qsh $have is up to date"
exit 0
fi
fi
fetch() {
if command -v curl >/dev/null 2>&1; then
curl -fsSL -o "$2" "$1"
elif command -v wget >/dev/null 2>&1; then
wget -qO "$2" "$1"
else
die "need curl or wget"
fi
}
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
trap 'exit 1' INT TERM
if [ -n "${QSH_VERSION:-}" ]; then
url=https://github.com/$REPO/releases/download/$QSH_VERSION
else
url=https://github.com/$REPO/releases/latest/download
fi
name=qsh-$arch-$plat
say "Downloading $name.tar.gz"
fetch "$url/$name.tar.gz" "$tmp/$name.tar.gz" || die "download failed: $url/$name.tar.gz"
# QSH_SUMS: a SHA256SUMS that `qsh update` has checked the release signature of.
if [ -n "${QSH_SUMS:-}" ]; then
cp "$QSH_SUMS" "$tmp/SHA256SUMS" || die "cannot read $QSH_SUMS"
else
fetch "$url/SHA256SUMS" "$tmp/SHA256SUMS" || die "download failed: $url/SHA256SUMS"
fi
want=$(awk -v f="$name.tar.gz" '$2 == f || $2 == "*" f { print $1 }' "$tmp/SHA256SUMS")
[ -n "$want" ] || die "$name.tar.gz is not in SHA256SUMS"
if command -v sha256sum >/dev/null 2>&1; then
got=$(sha256sum "$tmp/$name.tar.gz" | awk '{ print $1 }')
else
got=$(shasum -a 256 "$tmp/$name.tar.gz" | awk '{ print $1 }')
fi
[ "$got" = "$want" ] || die "checksum mismatch for $name.tar.gz"
tar xzf "$tmp/$name.tar.gz" -C "$tmp"
dist=$tmp/$name
files=$dist/qsh
[ "$what" = server ] && files="$files $dist/qshd"
if [ -d "$bin" ] && [ -w "$bin" ]; then
sudo_bin=
elif [ "$root" = none ]; then
mkdir -p "$bin"
sudo_bin=
else
sudo_bin=$root
$sudo_bin mkdir -p "$bin"
fi
# shellcheck disable=SC2086
$sudo_bin install -m755 $files "$bin/"
if [ "$what" = server ]; then say "Installed qsh and qshd to $bin"; else say "Installed qsh to $bin"; fi
qshd=$bin/qshd
# A root service must not run a binary that a user can replace.
if [ "$(id -u)" = 0 ] || [ -n "$sudo_bin" ]; then sys=yes; else sys=no; fi
# Units from before KillMode=process: a restart would end tmux and other
# jobs started from qsh sessions, which sshd keeps.
keep_jobs() { # unit drop-in-dir [root]
if ! grep -qs '^KillMode=' "$1" "$2"/*.conf; then
${3:-} mkdir -p "$2"
printf '[Service]\nKillMode=process\n' >"$tmp/killmode"
${3:-} cp "$tmp/killmode" "$2/killmode.conf"
say "Wrote $2/killmode.conf (a restart keeps tmux and nohup jobs, as with sshd)"
return 0
fi
return 1
}
# A unit that is already there may be edited by the admin: keep it.
put_unit() { # src dest [root]
if [ -e "$2" ]; then
say "Keeping the existing $2"
keep_jobs "$2" "$2.d" "${3:-}" || true
else
sed "s|/usr/local/bin/qshd|$qshd|g" "$1" >"$tmp/unit"
${3:-} mkdir -p "$(dirname "$2")"
${3:-} cp "$tmp/unit" "$2"
say "Wrote $2"
fi
}
service_linux() {
if [ ! -d /run/systemd/system ]; then
say "No systemd here: start '$qshd serve' with your init system (see the README)."
return
fi
if [ "$sys" = yes ]; then
put_unit "$dist/contrib/qshd.service" /etc/systemd/system/qshd.service "$root"
as_root systemctl daemon-reload
as_root systemctl enable --quiet qshd
as_root systemctl restart qshd
say "qshd runs as a system service (every user can log in): systemctl status qshd"
else
put_unit "$dist/contrib/qshd-user.service" "$HOME/.config/systemd/user/qshd.service"
systemctl --user daemon-reload
systemctl --user enable --quiet qshd
systemctl --user restart qshd
loginctl enable-linger "$(id -un)" 2>/dev/null ||
say "Run 'loginctl enable-linger' to keep qshd running after you log out."
say "qshd runs as a user service (only you can log in): systemctl --user status qshd"
fi
}
service_macos() {
[ "$sys" = yes ] || die "a launchd service needs qshd installed by root; $bin is writable by you"
put_unit "$dist/contrib/qshd.plist" /Library/LaunchDaemons/qshd.plist "$root"
if as_root launchctl print system/qshd >/dev/null 2>&1; then
as_root launchctl kickstart -k system/qshd
else
as_root launchctl bootstrap system /Library/LaunchDaemons/qshd.plist
fi
say "qshd runs as a launchd service; log: /var/log/qshd.log"
}
service_android() {
sv=$PREFIX/var/service/qshd
if [ -e "$sv/run" ]; then
say "Keeping the existing $sv/run"
else
mkdir -p "$sv"
printf '#!%s/bin/sh\nexec %s serve 2>&1\n' "$PREFIX" "$qshd" >"$sv/run"
chmod 755 "$sv/run"
say "Wrote $sv/run"
fi
if command -v sv-enable >/dev/null 2>&1 && sv-enable qshd 2>/dev/null; then
sv restart qshd >/dev/null 2>&1 || true
say "qshd runs as a Termux service: sv status qshd"
else
say "To run it as a service: pkg install termux-services, restart Termux, then sv-enable qshd"
fi
}
# On an update, restart a running service so it uses the new qshd.
restart_running() {
running=
case $plat in
linux)
if [ -d /run/systemd/system ]; then
if systemctl is-active --quiet qshd 2>/dev/null; then
running=system
elif systemctl --user is-active --quiet qshd 2>/dev/null; then
running=user
fi
fi ;;
macos)
if [ "$root" != none ] && $root launchctl print system/qshd >/dev/null 2>&1; then
running=launchd
fi ;;
android)
if command -v sv >/dev/null 2>&1 && sv status qshd 2>/dev/null | grep -q '^run'; then
running=runit
fi ;;
esac
[ -n "$running" ] || return 0
say "Restarting qshd closes the qsh sessions open on this machine."
if has_tty; then
ask "Restart the qshd service now? [Y/n]: "
case $REPLY in
"" | [Yy]*) ;;
*) say "Restart it later to use the new qshd."; return 0 ;;
esac
fi
case $running in
system)
# The update path keeps the unit too: give an old one KillMode=process.
if keep_jobs "$(systemctl show -P FragmentPath qshd)" /etc/systemd/system/qshd.service.d "$root"; then
as_root systemctl daemon-reload
fi
as_root systemctl restart qshd ;;
user)
if keep_jobs "$(systemctl --user show -P FragmentPath qshd)" "$HOME/.config/systemd/user/qshd.service.d"; then
systemctl --user daemon-reload
fi
systemctl --user restart qshd ;;
launchd) as_root launchctl kickstart -k system/qshd ;;
runit) sv restart qshd >/dev/null ;;
esac
say "Restarted the qshd service"
}
# Writes the settings asked for into qshd's config (created first, all
# commented out); one already set by hand is left as it is.
configure_qshd() {
[ -n "$port$totp" ] || return 0
if [ "$sys" = yes ]; then
as_root "$qshd" init >/dev/null
cfg=/etc/qsh/config.toml
else
"$qshd" init >/dev/null
cfg=$HOME/.config/qsh/qshd.toml
fi
if [ "$sys" = yes ]; then as_root cat "$cfg" >"$tmp/config"; else cat "$cfg" >"$tmp/config"; fi
for kv in ${port:+"listen=\"[::]:$port\""} ${totp:+"totp=\"$totp\""}; do
key=${kv%%=*} val=${kv#*=}
if grep -q "^${key}[[:space:]]*=" "$tmp/config"; then
say "$cfg already sets $key; left as it is"
continue
fi
# The commented-out default becomes the setting; else it goes at the top.
if grep -q "^# $key = " "$tmp/config"; then
sed "s|^# $key = .*|$key = $val|" "$tmp/config" >"$tmp/config.new"
else
{ printf '%s = %s\n' "$key" "$val"; cat "$tmp/config"; } >"$tmp/config.new"
fi
mv "$tmp/config.new" "$tmp/config"
say "Set $key = $val in $cfg"
done
# tee keeps the file's owner and mode.
if [ "$sys" = yes ]; then as_root tee "$cfg" <"$tmp/config" >/dev/null; else cat "$tmp/config" >"$cfg"; fi
}
if [ "$what" = server ]; then
[ "$update" = yes ] || configure_qshd
if [ "$service" = yes ]; then
"service_$plat"
else
restart_running
fi
fi
# Connection sharing for the client (`--share`), unless its config already has a say.
share_connections() {
dir="$HOME/.config/qsh"
cfg="$dir/config"
# qsh creates the config (all commented out) on its first start: the
# block below goes after it.
"$bin/qsh" -V >/dev/null 2>&1 || true
if grep -qi '^[[:space:]]*ControlMaster' "$cfg" 2>/dev/null; then
say "$cfg already sets ControlMaster; left as it is"
return 0
fi
[ -d "$dir" ] || { mkdir -p "$dir" && chmod 700 "$dir"; }
gap=''
[ -s "$cfg" ] && gap=yes
{
[ -n "$gap" ] && echo
echo "# One connection per host for 10 minutes after the last use: later qsh and"
echo "# qsh cp runs to it skip the login, and its one-time code. 'qsh -O exit HOST' ends it."
echo "Host *"
echo " ControlMaster auto"
echo " ControlPersist 10m"
} >>"$cfg"
chmod 600 "$cfg"
say "Turned on connection sharing in $cfg"
}
if [ "$update" != yes ] && [ -z "$share" ] && has_tty; then
ask "Share one connection per host for 10 minutes, so a one-time code is typed once? [y/N]: "
case $REPLY in
[Yy]*) share=yes ;;
*) share=no ;;
esac
fi
[ "$share" = yes ] && share_connections
if [ "$update" = yes ]; then
say "Updated to $("$bin/qsh" --version 2>&1 | awk '{ print $2 }')"
exit 0
fi
case :$PATH: in
*:"$bin":*) ;;
*) say "Add $bin to your PATH." ;;
esac
say ""
if [ "$what" = server ]; then
say "Open port ${port:-4422} for UDP and TCP in the firewall. To add a client, run 'qshd pair' on this"
say "machine as the user who will log in, then the 'qsh pair' command it prints on the client."
else
say "Next: run 'qshd pair' on the server, then 'qsh pair user@server' here and type the code."
say "With ssh access to the server already: 'qsh copy-id user@server' instead."
fi