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
|
# /bin/sh -n
# This file is part of Epoptes, https://epoptes.org
# Copyright (C) 2010-2023 the Epoptes team, see AUTHORS
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Implements the client side of the epoptes communications protocol.
# The daemon reads this file when it starts, and sends it to clients when they
# connect. The clients actually source it and then wait for further commands.
# Dash handles signals a bit differently from bash.
# We need to trap both 0 and 15 otherwise the trap function isn't called at all.
# 0 is normal program termination, it's called on `service epoptes restart`.
# 15 is SIGTERM, it's called when socat reaches the -T timeout.
# We don't always want to relaunch though.
# For user sessions, ping() aborts when X isn't running.
# Root connections abort when the system is shutting down.
# Also, epoptes-client kills -QUIT all the other epoptes-client processes.
on_signal() {
# local code # we can't do that, it resets $?
code="$?"
trap - 0 15
# Don't relaunch if the system is shutting down
if [ "$UID" -eq 0 ] && [ -d /run/systemd/system ] && [ -x /bin/systemctl ] &&
systemctl list-jobs | grep -q 'shutdown.target[[:space:]]*start'; then
echo "epoptes-client got signal: $1, but system is shutting down" >&5
else
echo "epoptes-client got signal: $1, relaunching..." >&5
# Restore the stdio descriptors to the previously saved 5
(
sleep 10
ping
exec epoptes-client $SERVER $PORT >&5 2>&1
) &
fi
exit "$code"
}
# Output a message and exit with an error.
# Parameters:
# $1..$N = The message.
die() {
echo "epoptes-client ERROR: $*" >&5
trap - 0 15
exit 1
}
# Some actions like broadcasting need access to the display even if they
# run as root. Xorg might have been restarted though, so detect DISPLAY.
ensure_display() {
local vars
test "$UID" -eq 0 || return 0
if timeout 1 xprop -root EPOPTES_CLIENT >/dev/null 2>&1; then
return 0
elif vars=$(./get-display); then
export $vars
else
return 1
fi
}
xprop_set() {
xprop -root -f "$1" 8u -set "$1" "$2"
}
xprop_get() {
# xprop -root UNSET_VAR echoes an error message to stdout instead of stderr
xprop -root "$1" 2>/dev/null | sed 's/^[^=]*//;s/^= "//;s/"$//;s/\\"/"/'
}
xprop_unset() {
xprop -root -remove "$1"
}
# Get the main MAC address, avoiding bonds, MAC randomization etc (#178).
main_mac() {
local resi resm resp line iface priority
# Result interface, mac and priority
unset resi resm
resp=0
while read -r line; do
set -o noglob
# shellcheck disable=SC2086
set -- $line
set +o noglob
iface=${2%:}
shift 2
case "$iface" in
# "Child" interfaces like enp3s0.5@enp3s0
*[.@]*) priority=1 ;;
# Possibly ethernet
e*) priority=4 ;;
# Possibly wireless
w*) priority=3 ;;
# Anything else, bond, tap, vmbr, fwbr
*) priority=2 ;;
esac
unset mac
while [ $# -gt 1 ]; do
case "$1" in
link/ether) mac=$2 ;;
# Bonds, MAC randomization and in general "Cloned MAC"
permaddr) mac=$2 ;;
esac
shift
done
if [ -n "$mac" ] && [ "$priority" -gt "$resp" ]; then
resi=$iface
resm=$mac
resp=$priority
fi
done <<EOF
$(ip -oneline -family inet link show | LANG=C sort -k 2,2)
EOF
echo "$resm $resi"
}
# Calculate, export and return a collection of useful variables.
info() {
local server_ip def_iface
if [ -z "$cached_info" ]; then
VERSION=${VERSION:-0.4.3} # Just in case the client wasn't updated
test -n "$USER" || USER=$(whoami)
NAME=$(getent passwd "$UID" | cut -d':' -f5 | cut -d',' -f1)
test -n "$HOME" || HOME=$(getent passwd "$UID" | cut -d: -f6)
if [ -n "$LTSP_CLIENT_HOSTNAME" ]; then
HOSTNAME="$LTSP_CLIENT_HOSTNAME"
else
HOSTNAME=$(hostname)
test -n "$HOSTNAME" || die "Empty hostname"
fi
if [ -n "$LTSP_CLIENT" ] && [ -n "$LTSP_CLIENT_MAC" ]; then
# LTSP exports those vars, use them if available.
MAC=$(echo "$LTSP_CLIENT_MAC" | awk '{ print tolower($1) }')
IP="$LTSP_CLIENT"
else
server_ip=$(getent hosts "$SERVER" | awk '{ print $1; exit }')
server_ip=${server_ip:-$SERVER}
read -r def_iface IP <<EOF
$(ip route get "$server_ip" | sed -n 's/.*dev *\([^ ]*\).*src *\([^ ]*\).*/\1 \2/p')
EOF
test "${def_iface:-lo}" != "lo" || read -r def_iface IP <<EOF
$(ip route get 192.168.67.0 | sed -n 's/.*dev *\([^ ]*\).*src *\([^ ]*\).*/\1 \2/p')
EOF
test "${def_iface:-lo}" != "lo" || die "Empty def_iface"
test -n "$IP" || die "Empty IP"
MAC=$(main_mac)
MAC=${MAC%% *}
test -n "$MAC" || die "Empty MAC"
fi
CPU=$(awk -F': ' '/^(model name|Model)/ { print $2; exit }' /proc/cpuinfo)
# Avoid the name "GROUPS" as it's an internal bash variable, see
# https://github.com/ltsp/ltsp/issues/389#issuecomment-1144856545
MEMBEROF=$(groups | tr ' ' ',')
RAM=$(awk '/^MemTotal:/ { print int($2/1024) }' /proc/meminfo)
# Use grep to avoid syntax errors like e.g. this on rpi4:
# MODALIAS=of:NgpuT(null)Cbrcm,bcm2711-vc5
VGA=$(
DRIVER=
PCI_ID=
eval "$(grep -shE '^(DRIVER|PCI_ID)=' /sys/class/graphics/fb?/../../uevent /sys/class/drm/card?/../../uevent || true)"
echo "$DRIVER${PCI_ID:+" [$PCI_ID]"}"
)
OS=$(uname -o)
export VERSION USER NAME HOME MEMBEROF HOSTNAME IP MAC CPU RAM VGA OS
cached_info=true
fi
cat <<EOF
uid=$UID
type=$TYPE
version=$VERSION
user=$USER
name=$NAME
home=$HOME
memberof=$MEMBEROF
hostname=$HOSTNAME
mac=$MAC
ip=$IP
cpu=$CPU
ram=$RAM
vga=$VGA
os=$OS
EOF
}
# Execute a command in the background and optionally print its pid.
# For internal use. Parameters:
# [-p] = print the pid.
# $1..$N = the command and its parameters.
background() {
local print_pid
ensure_display
if [ "$1" = "-p" ]; then
print_pid=true
shift
fi
# The command is run on a subshell with stdin and stdout redirected to
# /dev/null, so that it doesn't interfere with the output of other commands.
# stderr isn't changed, i.e. ~/.xsession-errors will be used.
# See issues #58 and #103 for explanation of the following syntax.
(
unset DESKTOP_AUTOSTART_ID
exec 0</dev/null >/dev/null
"$@"
) &
test -n "$print_pid" && echo $!
}
# Execute a command in the background.
# Parameters:
# $1 = the command.
execute() {
local launcher
# Do some logging, either in ~/.xsession-errors or on the console.
echo "$(LANG=C date '+%c'), epoptes-client executing: $1" >&5
case "$1" in
'')
echo "Can't execute an empty command." >&5
;;
www.*)
set "http://$1"
launcher="xdg-open"
;;
http:* | https:* | ftp:* | file:* | mailto:*)
launcher="xdg-open"
;;
*)
if [ -e "$1" ] && { [ ! -x "$1" ] || [ -d "$1" ]; }; then
launcher="xdg-open"
elif which -- "$1" >/dev/null; then
# Don't waste RAM for sh if it's just an executable.
launcher=""
fi
;;
esac
# In all unhandled cases, run the command with sh -c.
launcher=${launcher-sh -c}
background $launcher "$1"
}
# Log out the connected user.
logout() {
./endsession --logout
}
# Reboot the client.
reboot() {
./endsession --reboot
}
# Shut down the client.
shutdown() {
./endsession --shutdown
}
# Create a thumbshot of the user screen.
# Parameters:
# $1 = thumbshot width.
# $2 = thumbshot height.
thumbshot() {
# TODO: remove compatibility fallback after a few versions
local executable
test -z "$WAYLAND_DISPLAY" || return 0
if [ -f "./thumbshot.py" ]; then
executable=./thumbshot.py
else
executable=./screenshot
fi
if ! $executable "$1" "$2"; then
BAD_THUMBSHOTS=$((BAD_THUMBSHOTS + 1))
echo "$BAD_THUMBSHOTS failed thumbshots so far!" >&5
fi
}
# Lock the screen.
# Parameters:
# $1 = seconds to keep screen locked, 0 means forever - currently ignored.
# $2 = message to display to the user.
lock_screen() {
local pid
unlock_screen
# TODO: remove compatibility fallback after a few versions
if [ -f "./lock_screen.py" ]; then
pid=$(background -p exec ./lock_screen.py "$2")
else
# exec is needed to return the correct pid when /bin/sh -> bash
pid=$(background -p exec ./lock-screen "$2")
fi
xprop_set EPOPTES_LOCK_SCREEN_PID "$pid"
}
# Unlock a locked screen.
unlock_screen() {
local pid
ensure_display
pid=$(xprop_get EPOPTES_LOCK_SCREEN_PID)
if [ -n "$pid" ]; then
kill "$pid"
xprop_unset EPOPTES_LOCK_SCREEN_PID
fi
}
# Mute the system sound.
# Parameters:
# $1 = seconds to keep sound muted, 0 means forever - currently ignored.
mute_sound() {
if [ -x /usr/bin/pactl ]; then
background pactl set-sink-mute @DEFAULT_SINK@ 1
elif [ -x /usr/bin/amixer ]; then
background amixer -c 0 -q sset Master mute
fi
}
# Unute the system sound.
unmute_sound() {
if [ -x /usr/bin/pactl ]; then
background pactl set-sink-mute @DEFAULT_SINK@ 0
elif [ -x /usr/bin/amixer ]; then
background amixer -c 0 -q sset Master unmute
fi
}
# Start a network benchmark and print its pid.
# Stop it automatically after a while.
# Before starting, also stop any previous benchmarks that are still running.
# Parameters:
# $1 = host
# $2 = seconds to run (default=10).
start_benchmark() {
# benchmark_pid can't be unset in the background, but no harm done
benchmark_pid=$(background -p stop_start_benchmark "$@")
{
sleep $((2 * ${2:-10} + 5))
stop_benchmark "$benchmark_pid"
} &
echo "$benchmark_pid"
}
# Helper function to avoid running stop_benchmark in foreground.
# Parameters:
# $1 = host
# $2 = seconds to run.
stop_start_benchmark() {
test -n "$benchmark_pid" && stop_benchmark "$benchmark_pid"
# exec is needed to return the correct pid when /bin/sh -> bash
exec iperf -c "$1" --full-duplex ${1:+-t "$2"}
}
# Manually stop a previously running benchmark.
# Parameters:
# $1 = pid
stop_benchmark() {
if [ -n "$1" ]; then
kill "$1" || return 0
sleep 0.2
# For some reason, iperf -c <unknown host> needs 2 kills!
kill "$1" || return 0
sleep 0.2
# If it's not killed by now, it's hanged
kill -9 "$1" && sleep 0.2
fi 2>/dev/null
}
# Display some text to the user.
# Parameters:
# $1 = text.
# $2 = title.
# $3 = True/False, whether the text contains pango markup.
# $4 = icon name.
message() {
# TODO: remove compatibility fallback after a few versions
if [ -f "./message.py" ]; then
background ./message.py "$@"
else
background ./message "$@"
fi
}
# Connect to the server to be monitored.
# Parameters:
# $1 = host:port.
get_monitored() {
background x11vnc -q -nopw -noshm -24to32 -viewonly -connect_or_exit "$1"
}
# Connect to the server to get assistance.
# Parameters:
# $1 = host:port.
# $2 = grab keyboard and mouse.
get_assisted() {
background x11vnc -q -nopw -noshm -24to32 ${2:+-grabptr -grabkbd} -connect_or_exit "$1"
}
# Deactivate the screensaver, in order for the users to watch a broadcast.
reset_screensaver() {
if [ -x /usr/bin/xdg-screensaver ]; then
xdg-screensaver reset
else
xset s reset
fi
}
# Receive a broadcasted screen from the server.
# Parameters:
# $1 = host:port.
# $2 = password (encrypted as in ~/.vnc/passwd and octal-escaped).
# $3 = fullscreen.
receive_broadcast() {
receiving_broadcast=true
stop_receptions
reset_screensaver
# See https://epoptes.org/documentation/vnc/
if [ -z "$VNCVIEWER" ]; then
if [ -f /usr/share/applications/realvnc-vncviewer.desktop ]; then
VNCVIEWER=realvnc-vnc-viewer
elif [ -x /usr/bin/ssvncviewer ]; then
VNCVIEWER=ssvncviewer
else
VNCVIEWER=$(readlink -f /usr/bin/vncviewer)
VNCVIEWER=${VNCVIEWER##*/}
fi
fi
# shellcheck disable=SC2059 # $2 is octal-escaped
printf "$2" | {
sleep 0.$(($(od -An -N2 -tu2 /dev/urandom) % 50 + 50))
case "$VNCVIEWER" in
realvnc-vnc-viewer)
if [ -f "/etc/epoptes/vncviewer" ] &&
[ ! -f "$HOME/.vnc/config.d/vncviewer" ]; then
mkdir -pm 700 "$HOME/.vnc"
mkdir -pm 700 "$HOME/.vnc/config.d"
cp /etc/epoptes/vncviewer "$HOME/.vnc/config.d/vncviewer"
fi
exec vncviewer -shared -viewonly -securitynotificationtimeout=0 \
-scaling=aspectfit -uselocalcursor=0 \
-enabletoolbar=0 -warnunencrypted=0 \
-hideclosealert=true -autoreconnect=false \
-passwd /dev/stdin ${3:+-fullscreen -menukey=} "$1"
;;
ssvncviewer)
exec ssvncviewer -shared -viewonly -scale auto -escape never \
-passwd /dev/stdin ${3:+-fullscreen} "$1"
;;
xtigervncviewer)
exec xtigervncviewer -shared -viewonly \
-passwd /dev/stdin ${3:+-fullscreen -menukey=} "$1"
;;
xtightvncviewer)
exec xtightvncviewer -shared -viewonly \
-passwd /dev/stdin ${3:+-fullscreen} "$1"
;;
xvnc4viewer)
exec xvnc4viewer -shared -viewonly -uselocalcursor=0 \
-passwd /dev/stdin ${3:+-fullscreen -menukey=} "$1"
;;
*)
# A generic vncviewer, e.g. tigervnc on Arch/Fedora/SUSE
exec "$VNCVIEWER" -shared -viewonly \
-passwd /dev/stdin ${3:+-fullscreen} "$1"
;;
esac
} >/dev/null 2>&1 &
xprop_set EPOPTES_VNCVIEWER_PID "$!"
}
# The vnc clients should automatically exit when the server is killed.
# Unfortunately, that isn't always true, so try to kill them anyway.
stop_receptions() {
local pid
unset receiving_broadcast
ensure_display
pid=$(xprop_get EPOPTES_VNCVIEWER_PID)
if [ -n "$pid" ]; then
kill "$pid"
xprop_unset EPOPTES_VNCVIEWER_PID
fi
}
# Open a root terminal inside the X session.
root_term() {
background xterm -e bash -l
}
# Send a screen session to a host using socat.
# Parameters:
# $1 = host:port.
remote_term() {
local screen_params
if [ "$UID" -eq 0 ]; then
screen_params="bash -l"
else
screen_params="-l"
fi
background sh -c "
cd
sleep 1
TERM=xterm exec socat EXEC:'screen $screen_params',pty,stderr tcp:$1"
}
# Ping is called every few seconds just to make sure the connection is alive.
# But currently we use it as a workaround to kill stale clients too:
# Epoptes-client isn't registered as an X session client, and it doesn't
# exit automatically, so tell it to exit as soon as X is unavailable.
ping() {
if [ "$UID" -gt 0 ]; then
xprop -root -f EPOPTES_CLIENT 32c -set EPOPTES_CLIENT $$ || die "No X"
fi
if [ "$receiving_broadcast" = true ]; then
reset_screensaver
fi
}
# Display a message.
# Parameters:
# $1..$N = The message.
# echo()
# No need to implement it in the shell, it's embedded.
# Main
if [ -z "$UID" ] || [ -z "$TYPE" ] || [ -z "$SERVER" ]; then
die "Required environment variables are missing."
fi
echo " ...done" >&5
info
# Prevent dbus from getting autolaunched (issue #66)
export "DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS:-unix:path=/dev/null}"
for i in 0 15; do
trap "on_signal $i" $i
done
|