forked from rylena/sshdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·714 lines (664 loc) · 26.4 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·714 lines (664 loc) · 26.4 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#!/bin/sh
set -eu
REPOSITORY="rylena/sshdesk"
BRANCH="main"
TAILSCALE_INSTALL_URL="https://tailscale.com/install.sh"
TAILSCALE_MAC_URL="https://tailscale.com/download/mac"
HOMEBREW_INSTALL_URL="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
YDOTOOL_VERSION="1.0.4"
YDOTOOL_URL="https://github.com/ReimuNotMoe/ydotool/releases/download/v1.0.4/ydotool-release-ubuntu-latest"
YDOTOOLD_URL="https://github.com/ReimuNotMoe/ydotool/releases/download/v1.0.4/ydotoold-release-ubuntu-latest"
YDOTOOL_SHA256="daa83507a596d6839b7467540382dbdc6e4bf64ebfa4f7d6416e877d9a522c0c"
YDOTOOLD_SHA256="3f14f96308935214c0fb154507360f7632e7deda1935dc2d538259fd9986ed36"
YDOTOOL_SOURCE_URL="https://github.com/ReimuNotMoe/ydotool/archive/refs/tags/v1.0.4.tar.gz"
YDOTOOL_SOURCE_SHA256="ba075a43aa6ead51940e892ecffa4d0b8b40c241e4e2bc4bd9bd26b61fde23bd"
tailscale_choice="ask"
requested_user="${SSHDESK_USER-}"
temporary_directory=""
operating_system="$(uname -s)"
say() {
printf '%s\n' "$*"
}
fail() {
say "sshdesk-install: $*" >&2
exit 1
}
usage() {
cat <<'EOF'
Usage: install.sh [--user USER] [--tailscale | --no-tailscale]
Installs SSHDESK for the active Linux or macOS desktop user, configures
OpenSSH, and optionally installs and starts Tailscale at the end.
EOF
}
cleanup() {
if [ -n "${temporary_directory}" ] && [ -d "${temporary_directory}" ]; then
find "${temporary_directory}" -depth -mindepth 1 -delete 2>/dev/null || true
rmdir "${temporary_directory}" 2>/dev/null || true
fi
}
trap cleanup EXIT HUP INT TERM
while [ "$#" -gt 0 ]; do
case "$1" in
--user)
[ "$#" -ge 2 ] || fail "--user requires an account name"
requested_user="$2"
shift 2
;;
--tailscale)
tailscale_choice="yes"
shift
;;
--no-tailscale)
tailscale_choice="no"
shift
;;
-h|--help)
usage
exit 0
;;
*)
fail "unknown option: $1"
;;
esac
done
case "${operating_system}" in
Linux|Darwin) ;;
*) fail "unsupported operating system: ${operating_system}; use install.ps1 on Windows" ;;
esac
command -v curl >/dev/null 2>&1 || fail "curl is required"
command -v tar >/dev/null 2>&1 || fail "tar is required"
command -v sudo >/dev/null 2>&1 || [ "$(id -u)" -eq 0 ] || fail "sudo is required"
if [ "${operating_system}" = "Darwin" ]; then
[ -n "${requested_user}" ] || requested_user="$(id -un)"
else
if [ -z "${requested_user}" ]; then
requested_user="${SUDO_USER-}"
fi
if [ -z "${requested_user}" ] || [ "${requested_user}" = "root" ]; then
requested_user="${USER-}"
fi
if [ -z "${requested_user}" ] || [ "${requested_user}" = "root" ]; then
requested_user="$(logname 2>/dev/null || true)"
fi
fi
if [ -z "${requested_user}" ] || [ "${requested_user}" = "root" ]; then
fail "could not detect the desktop user; rerun with --user USER"
fi
case "${requested_user}" in
-*|*[!A-Za-z0-9_.-]*|'') fail "invalid desktop user: ${requested_user}" ;;
esac
if [ "${operating_system}" = "Linux" ]; then
getent passwd "${requested_user}" >/dev/null 2>&1 || \
fail "user does not exist: ${requested_user}"
else
id "${requested_user}" >/dev/null 2>&1 || fail "user does not exist: ${requested_user}"
[ "${requested_user}" = "$(id -un)" ] || \
fail "run the macOS installer while logged in as ${requested_user}"
fi
if [ "$(id -u)" -eq 0 ]; then
as_root=""
else
as_root="sudo"
fi
run_as_desktop_user() {
if [ "$(id -un)" = "${requested_user}" ]; then
"$@"
elif command -v runuser >/dev/null 2>&1; then
${as_root} runuser -u "${requested_user}" -- "$@"
elif command -v sudo >/dev/null 2>&1; then
sudo -u "${requested_user}" -- "$@"
else
fail "could not run a desktop access check as ${requested_user}"
fi
}
install_prerequisites() {
need_python=0
need_sshd=0
need_venv=0
command -v python3 >/dev/null 2>&1 || need_python=1
command -v sshd >/dev/null 2>&1 || [ -x /usr/sbin/sshd ] || need_sshd=1
if [ "${need_python}" -eq 0 ]; then
venv_check="${temporary_directory}/venv-check"
if ! python3 -m venv "${venv_check}" >/dev/null 2>&1; then
need_venv=1
fi
find "${venv_check}" -depth -mindepth 1 -delete 2>/dev/null || true
rmdir "${venv_check}" 2>/dev/null || true
fi
[ "${need_python}" -eq 0 ] && [ "${need_sshd}" -eq 0 ] && \
[ "${need_venv}" -eq 0 ] && return
say "Installing required Python and OpenSSH packages..."
if command -v apt-get >/dev/null 2>&1; then
${as_root} apt-get update
${as_root} apt-get install -y openssh-server python3 python3-venv
elif command -v dnf >/dev/null 2>&1; then
${as_root} dnf install -y openssh-server python3 python3-pip
elif command -v yum >/dev/null 2>&1; then
${as_root} yum install -y openssh-server python3 python3-pip
elif command -v pacman >/dev/null 2>&1; then
${as_root} pacman -Sy --needed --noconfirm openssh python python-pip
elif command -v zypper >/dev/null 2>&1; then
${as_root} zypper --non-interactive install openssh python3 python3-pip
elif command -v apk >/dev/null 2>&1; then
${as_root} apk add openssh-server python3 py3-pip py3-virtualenv
else
fail "install Python 3 with venv and OpenSSH server, then rerun this command"
fi
}
install_linux_capture_package() {
family="$1"
case "${family}" in
kde) executable="spectacle" ;;
wlroots) executable="grim" ;;
*) fail "unknown Wayland desktop family: ${family}" ;;
esac
command -v "${executable}" >/dev/null 2>&1 && return
say "Installing ${family} Wayland capture support..."
if command -v apt-get >/dev/null 2>&1; then
case "${family}" in
kde) package="kde-spectacle" ;;
wlroots) package="grim" ;;
esac
${as_root} apt-get update
${as_root} apt-get install -y "${package}"
elif command -v dnf >/dev/null 2>&1; then
case "${family}" in
kde) package="spectacle" ;;
wlroots) package="grim" ;;
esac
${as_root} dnf install -y "${package}"
elif command -v yum >/dev/null 2>&1; then
case "${family}" in
kde) package="spectacle" ;;
wlroots) package="grim" ;;
esac
${as_root} yum install -y "${package}"
elif command -v pacman >/dev/null 2>&1; then
case "${family}" in
kde) package="spectacle" ;;
wlroots) package="grim" ;;
esac
${as_root} pacman -Sy --needed --noconfirm "${package}"
elif command -v zypper >/dev/null 2>&1; then
case "${family}" in
kde) package="spectacle" ;;
wlroots) package="grim" ;;
esac
${as_root} zypper --non-interactive install "${package}"
elif command -v apk >/dev/null 2>&1; then
case "${family}" in
kde) package="spectacle" ;;
wlroots) package="grim" ;;
esac
${as_root} apk add "${package}"
else
fail "install ${executable} for Wayland capture, then rerun this command"
fi
command -v "${executable}" >/dev/null 2>&1 || \
fail "${executable} is unavailable after package installation"
}
gnome_streaming_is_ready() {
python3 -c '
import gi
gi.require_version("Gio", "2.0")
gi.require_version("Gst", "1.0")
gi.require_version("GstApp", "1.0")
gi.require_version("GstVideo", "1.0")
from gi.repository import Gst
Gst.init(None)
raise SystemExit(0 if Gst.ElementFactory.find("pipewiresrc") else 1)
' >/dev/null 2>&1
}
install_gnome_streaming_support() {
gnome_streaming_is_ready && return
say "Installing persistent GNOME PipeWire capture support..."
if command -v apt-get >/dev/null 2>&1; then
${as_root} apt-get update
${as_root} apt-get install -y \
python3-gi gir1.2-gstreamer-1.0 gir1.2-gst-plugins-base-1.0 \
gstreamer1.0-plugins-base gstreamer1.0-pipewire
elif command -v dnf >/dev/null 2>&1; then
${as_root} dnf install -y \
python3-gobject gstreamer1 gstreamer1-plugins-base pipewire-gstreamer
elif command -v yum >/dev/null 2>&1; then
${as_root} yum install -y \
python3-gobject gstreamer1 gstreamer1-plugins-base pipewire-gstreamer
elif command -v pacman >/dev/null 2>&1; then
${as_root} pacman -Sy --needed --noconfirm \
python-gobject gstreamer gst-plugins-base gst-plugin-pipewire
elif command -v zypper >/dev/null 2>&1; then
${as_root} zypper --non-interactive install \
python3-gobject gstreamer gstreamer-plugins-base gstreamer-plugin-pipewire
elif command -v apk >/dev/null 2>&1; then
${as_root} apk add \
py3-gobject3 gstreamer gst-plugins-base gst-plugin-pipewire
else
fail "install PyGObject, GStreamer, and its PipeWire plugin, then rerun"
fi
gnome_streaming_is_ready || \
fail "GStreamer PipeWire support is unavailable after package installation"
}
install_ydotool_binaries() {
helper_directory="/usr/local/libexec/sshdesk"
ydotool_cli="/usr/local/bin/ydotool"
ydotool_daemon="${helper_directory}/ydotoold"
if [ "$(uname -m)" = "x86_64" ] && ! command -v apk >/dev/null 2>&1; then
say "Installing the pinned ydotool ${YDOTOOL_VERSION} Wayland input helper..."
downloaded_cli="${temporary_directory}/ydotool"
downloaded_daemon="${temporary_directory}/ydotoold"
curl -fsSL "${YDOTOOL_URL}" -o "${downloaded_cli}"
curl -fsSL "${YDOTOOLD_URL}" -o "${downloaded_daemon}"
command -v sha256sum >/dev/null 2>&1 || fail "sha256sum is required"
printf '%s %s\n' "${YDOTOOL_SHA256}" "${downloaded_cli}" | sha256sum -c -
printf '%s %s\n' "${YDOTOOLD_SHA256}" "${downloaded_daemon}" | sha256sum -c -
chmod 0755 "${downloaded_cli}" "${downloaded_daemon}"
if "${downloaded_cli}" --help >/dev/null 2>&1 && \
"${downloaded_daemon}" --help 2>&1 | grep -q -- '--socket-own'; then
${as_root} install -d -m 0755 "${helper_directory}"
${as_root} install -m 0755 "${downloaded_cli}" "${ydotool_cli}"
${as_root} install -m 0755 "${downloaded_daemon}" "${ydotool_daemon}"
return
fi
say "The release binary is incompatible with this system; building ydotool locally..."
fi
say "Installing the ydotool build prerequisites..."
if command -v apt-get >/dev/null 2>&1; then
${as_root} apt-get update
${as_root} apt-get install -y build-essential cmake
elif command -v dnf >/dev/null 2>&1; then
${as_root} dnf install -y gcc make cmake
elif command -v yum >/dev/null 2>&1; then
${as_root} yum install -y gcc make cmake
elif command -v pacman >/dev/null 2>&1; then
${as_root} pacman -Sy --needed --noconfirm base-devel cmake
elif command -v zypper >/dev/null 2>&1; then
${as_root} zypper --non-interactive install gcc make cmake
elif command -v apk >/dev/null 2>&1; then
${as_root} apk add build-base cmake linux-headers
else
fail "install a C compiler, make, and CMake 3.4+, then rerun this command"
fi
source_archive="${temporary_directory}/ydotool-${YDOTOOL_VERSION}.tar.gz"
source_directory="${temporary_directory}/ydotool-source"
build_directory="${temporary_directory}/ydotool-build"
curl -fsSL "${YDOTOOL_SOURCE_URL}" -o "${source_archive}"
command -v sha256sum >/dev/null 2>&1 || fail "sha256sum is required"
printf '%s %s\n' "${YDOTOOL_SOURCE_SHA256}" "${source_archive}" | sha256sum -c -
mkdir -p "${source_directory}" "${build_directory}"
tar -xzf "${source_archive}" -C "${source_directory}" --strip-components=1
(
cd "${build_directory}"
cmake -DCMAKE_BUILD_TYPE=Release "${source_directory}"
cmake --build . --target ydotool ydotoold
)
${as_root} install -d -m 0755 "${helper_directory}"
${as_root} install -m 0755 "${build_directory}/ydotool" "${ydotool_cli}"
${as_root} install -m 0755 "${build_directory}/ydotoold" "${ydotool_daemon}"
}
configure_ydotool_service() {
command -v systemctl >/dev/null 2>&1 || \
fail "automatic Wayland input currently requires systemd"
if [ ! -c /dev/uinput ]; then
command -v modprobe >/dev/null 2>&1 || \
fail "/dev/uinput is missing and modprobe is unavailable"
${as_root} modprobe uinput
fi
[ -c /dev/uinput ] || fail "the Linux uinput device is unavailable"
modules_file="${temporary_directory}/sshdesk-uinput.conf"
printf 'uinput\n' > "${modules_file}"
${as_root} install -d -m 0755 /etc/modules-load.d
${as_root} install -m 0644 \
"${modules_file}" /etc/modules-load.d/sshdesk-uinput.conf
desktop_uid="$(id -u "${requested_user}")"
desktop_gid="$(id -g "${requested_user}")"
YDOTOOL_SOCKET="/run/sshdesk-ydotool/socket"
export YDOTOOL_SOCKET
service_file="${temporary_directory}/sshdesk-ydotoold.service"
cat > "${service_file}" <<EOF
[Unit]
Description=SSHDESK Wayland input helper
After=systemd-udevd.service
[Service]
Type=simple
ExecStart=${ydotool_daemon} --socket-path=${YDOTOOL_SOCKET} --socket-perm=0600 --socket-own=${desktop_uid}:${desktop_gid}
Restart=on-failure
RestartSec=1
RuntimeDirectory=sshdesk-ydotool
RuntimeDirectoryMode=0755
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
ProtectControlGroups=yes
ProtectKernelLogs=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
LockPersonality=yes
RestrictSUIDSGID=yes
RestrictAddressFamilies=AF_UNIX
DevicePolicy=closed
DeviceAllow=/dev/uinput rw
[Install]
WantedBy=multi-user.target
EOF
${as_root} install -m 0644 \
"${service_file}" /etc/systemd/system/sshdesk-ydotoold.service
${as_root} systemctl daemon-reload
${as_root} systemctl enable --now sshdesk-ydotoold.service
attempts=0
while [ ! -S "${YDOTOOL_SOCKET}" ] && [ "${attempts}" -lt 50 ]; do
attempts=$((attempts + 1))
sleep 0.1
done
[ -S "${YDOTOOL_SOCKET}" ] || \
fail "ydotoold did not create ${YDOTOOL_SOCKET}; check its systemd service"
if ! run_as_desktop_user env "YDOTOOL_SOCKET=${YDOTOOL_SOCKET}" \
"${ydotool_cli}" debug >/dev/null 2>&1; then
fail "${requested_user} cannot connect to the SSHDESK ydotoold socket"
fi
}
install_linux_desktop_dependencies() {
[ "${operating_system}" = "Linux" ] || return
if [ "${XDG_SESSION_TYPE-}" != "wayland" ] && [ -z "${WAYLAND_DISPLAY-}" ]; then
return
fi
desktop_name="$(printf '%s' "${XDG_CURRENT_DESKTOP-}" | tr '[:upper:]' '[:lower:]')"
case "${desktop_name}" in
*gnome*|*unity*|*cinnamon*|*budgie*) desktop_family="gnome" ;;
*kde*|*plasma*) desktop_family="kde" ;;
*) desktop_family="wlroots" ;;
esac
if [ "${desktop_family}" = "gnome" ]; then
install_gnome_streaming_support
return
fi
install_linux_capture_package "${desktop_family}"
install_ydotool_binaries
configure_ydotool_service
}
find_brew() {
if command -v brew >/dev/null 2>&1; then
command -v brew
elif [ -x /opt/homebrew/bin/brew ]; then
printf '%s\n' /opt/homebrew/bin/brew
elif [ -x /usr/local/bin/brew ]; then
printf '%s\n' /usr/local/bin/brew
else
return 1
fi
}
ensure_homebrew() {
if brew_binary="$(find_brew 2>/dev/null)"; then
printf '%s\n' "${brew_binary}"
return
fi
say "Installing Homebrew to provide missing macOS prerequisites..." >&2
homebrew_script="${temporary_directory}/homebrew-install.sh"
curl -fsSL "${HOMEBREW_INSTALL_URL}" -o "${homebrew_script}"
NONINTERACTIVE=1 /bin/bash "${homebrew_script}"
find_brew || fail "Homebrew installed but its executable could not be found"
}
install_macos_prerequisites() {
if command -v python3 >/dev/null 2>&1 && \
python3 -m venv "${temporary_directory}/venv-check" >/dev/null 2>&1; then
return
fi
find "${temporary_directory}/venv-check" -depth -mindepth 1 -delete 2>/dev/null || true
rmdir "${temporary_directory}/venv-check" 2>/dev/null || true
brew_binary="$(ensure_homebrew)"
"${brew_binary}" install python
brew_prefix="$("${brew_binary}" --prefix)"
PATH="${brew_prefix}/bin:${PATH}"
export PATH
command -v python3 >/dev/null 2>&1 || fail "Python 3 is unavailable after installation"
}
download_project() {
if [ -n "${SSHDESK_SOURCE_DIR-}" ]; then
project_directory="${SSHDESK_SOURCE_DIR}"
[ -f "${project_directory}/pyproject.toml" ] || fail "invalid SSHDESK_SOURCE_DIR"
return
fi
project_directory="${temporary_directory}/sshdesk"
mkdir -p "${project_directory}"
archive="${temporary_directory}/sshdesk.tar.gz"
curl -fsSL \
"https://github.com/${REPOSITORY}/archive/refs/heads/${BRANCH}.tar.gz" \
-o "${archive}"
tar -xzf "${archive}" -C "${project_directory}" --strip-components=1
}
configure_macos_openssh() {
desktop_home="$(dscl . -read "/Users/${requested_user}" NFSHomeDirectory | \
awk '{print $2}')"
[ -n "${desktop_home}" ] || fail "could not find the macOS home directory"
forced_command="${desktop_home}/.local/bin/sshdesk-forced-command"
[ -x "${forced_command}" ] || fail "portable forced command was not installed"
sshd_main="/etc/ssh/sshd_config"
sshd_directory="/etc/ssh/sshd_config.d"
sshd_snippet="${sshd_directory}/90-sshdesk-${requested_user}.conf"
added_include=0
${as_root} install -d -m 0755 "${sshd_directory}"
if ! ${as_root} grep -Eiq \
'^[[:space:]]*Include[[:space:]]+/etc/ssh/sshd_config\.d/\*' \
"${sshd_main}"; then
combined_config="${temporary_directory}/sshd_config-macos"
printf 'Include /etc/ssh/sshd_config.d/*\n' > "${combined_config}"
${as_root} cat "${sshd_main}" >> "${combined_config}"
${as_root} cp -p "${sshd_main}" "${sshd_main}.before-sshdesk"
${as_root} install -m 0644 "${combined_config}" "${sshd_main}"
added_include=1
fi
snippet="${temporary_directory}/90-sshdesk-macos.conf"
"${project_directory}/scripts/configure-sshd.sh" \
"${requested_user}" "${forced_command}" > "${snippet}"
had_previous=0
if ${as_root} test -f "${sshd_snippet}"; then
had_previous=1
${as_root} cp -p "${sshd_snippet}" "${temporary_directory}/previous-macos-snippet"
fi
${as_root} install -m 0644 "${snippet}" "${sshd_snippet}"
if ! ${as_root} /usr/sbin/sshd -t; then
if [ "${had_previous}" -eq 1 ]; then
${as_root} install -m 0644 \
"${temporary_directory}/previous-macos-snippet" "${sshd_snippet}"
else
${as_root} unlink "${sshd_snippet}"
fi
if [ "${added_include}" -eq 1 ]; then
${as_root} cp -p "${sshd_main}.before-sshdesk" "${sshd_main}"
fi
fail "OpenSSH rejected the macOS configuration; changes were rolled back"
fi
${as_root} /usr/sbin/systemsetup -setremotelogin on >/dev/null || \
fail "macOS could not enable Remote Login; grant Terminal Full Disk Access and rerun"
}
find_sshd() {
if command -v sshd >/dev/null 2>&1; then
command -v sshd
elif [ -x /usr/sbin/sshd ]; then
printf '%s\n' /usr/sbin/sshd
else
return 1
fi
}
start_openssh() {
if command -v systemctl >/dev/null 2>&1; then
if ${as_root} systemctl enable --now ssh.service >/dev/null 2>&1; then
${as_root} systemctl reload ssh.service
return
fi
if ${as_root} systemctl enable --now sshd.service >/dev/null 2>&1; then
${as_root} systemctl reload sshd.service
return
fi
fi
if command -v service >/dev/null 2>&1; then
if ${as_root} service ssh restart >/dev/null 2>&1; then
return
fi
if ${as_root} service sshd restart >/dev/null 2>&1; then
return
fi
fi
fail "OpenSSH is configured, but its service could not be started"
}
prompt_tailscale() {
if [ "${tailscale_choice}" != "ask" ]; then
return
fi
if [ ! -r /dev/tty ] || [ ! -w /dev/tty ]; then
say "No interactive terminal; skipping optional Tailscale installation."
tailscale_choice="no"
return
fi
printf 'Install and start Tailscale now? [y/N] ' >/dev/tty
IFS= read -r answer </dev/tty || answer=""
case "${answer}" in
y|Y|yes|YES|Yes) tailscale_choice="yes" ;;
*) tailscale_choice="no" ;;
esac
}
install_tailscale() {
[ "${tailscale_choice}" = "yes" ] || return 0
if [ "${operating_system}" = "Darwin" ]; then
if [ ! -d /Applications/Tailscale.app ]; then
say "Installing the Tailscale macOS app..."
brew_binary="$(ensure_homebrew)"
"${brew_binary}" install --cask tailscale
else
say "Tailscale is already installed."
fi
say "Opening Tailscale to finish its VPN permission and login prompts..."
open -a Tailscale || open "${TAILSCALE_MAC_URL}"
return
fi
if ! command -v tailscale >/dev/null 2>&1; then
say "Installing Tailscale from the official installer..."
tailscale_script="${temporary_directory}/tailscale-install.sh"
curl -fsSL "${TAILSCALE_INSTALL_URL}" -o "${tailscale_script}"
${as_root} sh "${tailscale_script}"
else
say "Tailscale is already installed."
fi
if command -v systemctl >/dev/null 2>&1; then
${as_root} systemctl enable --now tailscaled.service
elif command -v service >/dev/null 2>&1; then
${as_root} service tailscaled start
fi
say "Starting Tailscale login..."
if [ -r /dev/tty ] && [ -w /dev/tty ]; then
${as_root} tailscale up </dev/tty >/dev/tty
else
${as_root} tailscale up
fi
}
temporary_directory="$(mktemp -d "${TMPDIR:-/tmp}/sshdesk-install.XXXXXX")"
if [ "${operating_system}" = "Darwin" ]; then
install_macos_prerequisites
else
install_prerequisites
install_linux_desktop_dependencies
sshd_binary="$(find_sshd)" || fail "OpenSSH server is unavailable after installation"
command -v ssh-keygen >/dev/null 2>&1 && ${as_root} ssh-keygen -A
fi
download_project
if [ "${operating_system}" = "Darwin" ]; then
say "Installing SSHDESK for ${requested_user}..."
SSHDESK_BOOTSTRAP=1 "${project_directory}/scripts/install-macos.sh"
configure_macos_openssh
say "SSHDESK is installed and macOS Remote Login is running."
say "Connect with: ssh ${requested_user}@<server-address>"
say "Grant Screen Recording and Accessibility permission to the installed Python"
say "process in System Settings > Privacy & Security before connecting."
prompt_tailscale
install_tailscale
say "Installation complete."
exit 0
fi
desktop_home="$(getent passwd "${requested_user}" | cut -d: -f6)"
display_value="${DISPLAY-}"
if [ -z "${display_value}" ] && [ -z "${WAYLAND_DISPLAY-}" ]; then
display_value=":0"
fi
xauthority_value="${XAUTHORITY-}"
if [ -z "${xauthority_value}" ]; then
runtime_directory="${XDG_RUNTIME_DIR-}"
if [ -n "${runtime_directory}" ] && [ -r "${runtime_directory}/gdm/Xauthority" ]; then
xauthority_value="${runtime_directory}/gdm/Xauthority"
else
xauthority_value="${desktop_home}/.Xauthority"
fi
fi
say "Installing SSHDESK for ${requested_user}..."
${as_root} env \
"SSHDESK_BOOTSTRAP=1" \
"DISPLAY=${display_value}" \
"XAUTHORITY=${xauthority_value}" \
"WAYLAND_DISPLAY=${WAYLAND_DISPLAY-}" \
"XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR-}" \
"XDG_SESSION_TYPE=${XDG_SESSION_TYPE-}" \
"XDG_CURRENT_DESKTOP=${XDG_CURRENT_DESKTOP-}" \
"DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS-}" \
"YDOTOOL_SOCKET=${YDOTOOL_SOCKET-}" \
"${project_directory}/scripts/install-server.sh" \
"${requested_user}" "${display_value}" "${xauthority_value}"
say "Verifying graphical capture and input access..."
run_as_desktop_user env \
"DISPLAY=${display_value}" \
"XAUTHORITY=${xauthority_value}" \
"WAYLAND_DISPLAY=${WAYLAND_DISPLAY-}" \
"XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR-}" \
"XDG_SESSION_TYPE=${XDG_SESSION_TYPE-}" \
"XDG_CURRENT_DESKTOP=${XDG_CURRENT_DESKTOP-}" \
"DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS-}" \
"YDOTOOL_SOCKET=${YDOTOOL_SOCKET-}" \
/usr/local/bin/sshdesk-server --check
sshd_main="/etc/ssh/sshd_config"
sshd_directory="/etc/ssh/sshd_config.d"
sshd_snippet="${sshd_directory}/90-sshdesk-${requested_user}.conf"
added_include=0
${as_root} install -d -m 0755 "${sshd_directory}"
if ! ${as_root} grep -Eiq \
'^[[:space:]]*Include[[:space:]]+/etc/ssh/sshd_config\.d/\*\.conf' \
"${sshd_main}"; then
say "Enabling the OpenSSH configuration directory..."
combined_config="${temporary_directory}/sshd_config"
printf 'Include /etc/ssh/sshd_config.d/*.conf\n' > "${combined_config}"
${as_root} cat "${sshd_main}" >> "${combined_config}"
${as_root} cp -p "${sshd_main}" "${sshd_main}.before-sshdesk"
${as_root} install -m 0600 "${combined_config}" "${sshd_main}"
added_include=1
fi
snippet="${temporary_directory}/90-sshdesk.conf"
"${project_directory}/scripts/configure-sshd.sh" "${requested_user}" > "${snippet}"
had_previous=0
if ${as_root} test -f "${sshd_snippet}"; then
had_previous=1
${as_root} cp -p "${sshd_snippet}" "${temporary_directory}/previous-sshd-snippet"
fi
${as_root} install -m 0644 "${snippet}" "${sshd_snippet}"
if ! ${as_root} "${sshd_binary}" -t; then
if [ "${had_previous}" -eq 1 ]; then
${as_root} install -m 0644 \
"${temporary_directory}/previous-sshd-snippet" "${sshd_snippet}"
else
${as_root} unlink "${sshd_snippet}"
fi
if [ "${added_include}" -eq 1 ]; then
${as_root} cp -p "${sshd_main}.before-sshdesk" "${sshd_main}"
fi
fail "OpenSSH rejected the configuration; the SSHDESK snippet was rolled back"
fi
start_openssh
say "SSHDESK is installed and OpenSSH is running."
say "Connect with: ssh ${requested_user}@<server-address>"
# Tailscale is intentionally last so an optional network setup cannot interrupt
# SSHDESK package installation or leave an unvalidated sshd configuration.
prompt_tailscale
install_tailscale
if command -v tailscale >/dev/null 2>&1; then
tailscale_ip="$(tailscale ip -4 2>/dev/null | head -n 1 || true)"
if [ -n "${tailscale_ip}" ]; then
say "Tailscale SSHDESK address: ssh ${requested_user}@${tailscale_ip}"
fi
fi
say "Installation complete."