#!/bin/bash # install-console-wrapper.sh — one-shot remediation for existing hosts # that installed before the monsys-console-launch wrapper existed. Run as root: # # curl -fsSL https://get.monsys.ai/install-console-wrapper.sh | sudo bash # # After this finishes, restart the agent (sudo systemctl restart monsys-agent) # and the emergency console will work on its next session attempt. set -e if [[ $EUID -ne 0 ]]; then echo "must run as root" >&2 exit 1 fi # Ensure monsys-console user exists (was created by install.sh). if ! id monsys-console >/dev/null 2>&1; then echo "creating monsys-console user" useradd -r -s /bin/bash -d /var/lib/monsys/console -c "Monsys Emergency Console" monsys-console install -d -o monsys-console -g monsys-console -m 0750 /var/lib/monsys/console fi echo "installing /usr/local/sbin/monsys-console-launch" install -m 0755 -o root -g root /dev/stdin /usr/local/sbin/monsys-console-launch <<'WRAPPER' #!/bin/bash set -e SESSION_ID="${1:?session_id required}" if ! [[ "$SESSION_ID" =~ ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ ]]; then echo "monsys-console-launch: invalid session_id" >&2 exit 2 fi if ! id monsys-console >/dev/null 2>&1; then echo "monsys-console-launch: monsys-console user missing" >&2 exit 3 fi export MONSYS_SESSION_ID="$SESSION_ID" export TERM="${TERM:-xterm-256color}" export HOME="/var/lib/monsys/console" export PATH="/usr/bin:/usr/sbin:/bin:/sbin" export SHELL="/bin/bash" export PS1="monsys-console:\w\$ " exec setpriv --reuid=monsys-console --regid=monsys-console --init-groups --inh-caps=-all -- \ /bin/bash --rcfile /var/lib/monsys/console/.bashrc --restricted -i WRAPPER echo "installing /etc/sudoers.d/monsys-console" install -m 0440 -o root -g root /dev/stdin /etc/sudoers.d/monsys-console <<'SUDOERS' monsys ALL=(root) NOPASSWD: /usr/local/sbin/monsys-console-launch * Defaults!/usr/local/sbin/monsys-console-launch !requiretty SUDOERS visudo -c -f /etc/sudoers.d/monsys-console >/dev/null # Old CAP_SETUID drop-in (from the earlier wrong fix) is harmless but no # longer needed. Remove it for cleanliness if present. if [[ -f /etc/systemd/system/monsys-agent.service.d/console-caps.conf ]]; then echo "removing obsolete console-caps.conf drop-in" rm /etc/systemd/system/monsys-agent.service.d/console-caps.conf systemctl daemon-reload fi echo "✓ wrapper installed. restart the agent to pick up the new binary + flow:" echo " sudo systemctl restart monsys-agent"