fix(system_baseline): keep tmux autostart from ending or swallowing logins
Review of #122. - A failed attach no longer closes the connection: the snippet does not exec tmux, it prints a line and leaves the plain shell (a client TERM without terminfo made every login fail). A clean detach still ends the connection. - The snippet acts only for an interactive shell (case $-): a login shell that is not interactive, such as `ssh -t host 'bash -lc cmd'`, is left alone. Docs corrected. - Validation anchors with \Z, so a trailing newline from a YAML block scalar is refused. It moves to validate.yml (tag always); argument_specs already covers types. - Install keys on tmux being in the resolved package list, not a second profile list. - verify.yml: the snippet does nothing without a terminal, the trailing-newline refusals, and the removal path. The vacuous pve assert is gone. - README and argument_specs: window commands start with a minimal environment (use absolute paths), zsh and fish do not read /etc/profile.d, detach ends the connection.
This commit is contained in:
1 parent
d3d39097b0
commit
da3389eac1
7 files changed
+137
-40
No files matched your search
@@ -106,9 +106,11 @@ system_baseline_tmux_windows:
|
|||||||
- { name: logs, command: "journalctl -f" }
|
- { name: logs, command: "journalctl -f" }
|
||||||
```
|
```
|
||||||
|
|
||||||
- It is `/etc/profile.d/tmux-autostart.sh`, read only by an interactive login shell, so `scp`, `sftp`, `ssh host command` and Ansible never trigger it. sshd's `ForceCommand` is not used: it applies to every session type and a broken tmux would lock all of them out. It only acts for an SSH login with a terminal, outside tmux, and without `~/.no-tmux`.
|
- It is `/etc/profile.d/tmux-autostart.sh`. Every login shell reads that directory, so the snippet itself acts only for an interactive shell (`case $-`) with a terminal and an SSH connection, outside tmux, and without `~/.no-tmux`. `scp`, `sftp`, `ssh host command`, Ansible and `ssh -t host 'bash -lc cmd'` never trigger it. sshd's `ForceCommand` is not used: it applies to every session type and a broken tmux would lock all of them out. Users whose login shell is zsh or fish are not affected, because those shells do not read `/etc/profile.d`.
|
||||||
|
- The snippet is installed when autostart is on and the resolved package list contains `tmux` (the `ubuntu_lxc` and `ubuntu_vm` profiles). If you override `system_baseline_essential_packages` without it, nothing is installed.
|
||||||
|
- A failed attach (for example a client `TERM` such as `xterm-kitty` or `wezterm` without a terminfo entry on the host) prints a line and leaves a plain shell. Detaching (`Ctrl-b d`) ends the SSH connection, as `exit` would.
|
||||||
- Turn it off for one login with `ssh -t host bash` (a non-login shell does not read `/etc/profile.d`), for one user with `touch ~/.no-tmux`, and for the host by setting the variable to `false` and running the role (it removes the snippet).
|
- Turn it off for one login with `ssh -t host bash` (a non-login shell does not read `/etc/profile.d`), for one user with `touch ~/.no-tmux`, and for the host by setting the variable to `false` and running the role (it removes the snippet).
|
||||||
- The windows are made when the session is created; a login that re-attaches adds none, so a change applies from the next new session. A window closes when its command exits.
|
- The windows are made when the session is created; a login that re-attaches adds none, so a change applies from the next new session. A window closes when its command exits. A window command runs with `sh -c` from the tmux server of the first login, before `~/.profile` is read, so it does not see `~/.local/bin` and other additions made there: use absolute paths.
|
||||||
- Security: the name and commands are validated (`A-Za-z0-9_-`, one line) and shell-quoted, and come from the inventory, never from a user. The file is root-owned and not writable by its users. The commands run as the user who logs in, are readable by every user and show in `ps`: no secrets in a command. A session outlives the SSH connection, so a root session stays open until it is closed.
|
- Security: the name and commands are validated (`A-Za-z0-9_-`, one line) and shell-quoted, and come from the inventory, never from a user. The file is root-owned and not writable by its users. The commands run as the user who logs in, are readable by every user and show in `ps`: no secrets in a command. A session outlives the SSH connection, so a root session stays open until it is closed.
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ argument_specs:
|
|||||||
short_description: Apply the OS baseline for a system type
|
short_description: Apply the OS baseline for a system type
|
||||||
description:
|
description:
|
||||||
- >-
|
- >-
|
||||||
Validates C(system_baseline_system_type), then runs the update, packages, unattended-upgrades, locale, timezone, journald,
|
Validates C(system_baseline_system_type) and, when C(system_baseline_tmux_autostart) is true, the tmux session name and
|
||||||
|
windows, then runs the update, packages, unattended-upgrades, locale, timezone, journald,
|
||||||
sysctl, tmpfs, tmux and reboot task files, in that order.
|
sysctl, tmpfs, tmux and reboot task files, in that order.
|
||||||
- Needs gathered facts, because the reboot check reads C(ansible_os_family).
|
- Needs gathered facts, because the reboot check reads C(ansible_os_family).
|
||||||
- >-
|
- >-
|
||||||
@@ -48,8 +49,10 @@ argument_specs:
|
|||||||
description:
|
description:
|
||||||
- >-
|
- >-
|
||||||
Attach an interactive SSH login to a tmux session, creating it when it does not exist. Installs C(/etc/profile.d/tmux-autostart.sh),
|
Attach an interactive SSH login to a tmux session, creating it when it does not exist. Installs C(/etc/profile.d/tmux-autostart.sh),
|
||||||
read only by an interactive login shell, so scp, sftp, C(ssh host command) and automation are unaffected. A user opts out with
|
which acts only for an interactive login shell with a terminal, so scp, sftp, C(ssh host command) and automation are unaffected.
|
||||||
C(~/.no-tmux). Only the C(ubuntu_lxc) and C(ubuntu_vm) profiles install it; with false, or on C(pve), the file is removed.
|
Shells that do not read C(/etc/profile.d) (zsh, fish) are unaffected too. A user opts out with C(~/.no-tmux). Installed only when
|
||||||
|
C(tmux) is in the resolved package list (the C(ubuntu_lxc) and C(ubuntu_vm) profiles); with false, or without tmux, the file is
|
||||||
|
removed. A failed attach leaves a plain shell; detaching ends the SSH connection.
|
||||||
system_baseline_tmux_session:
|
system_baseline_tmux_session:
|
||||||
type: str
|
type: str
|
||||||
default: main
|
default: main
|
||||||
@@ -63,7 +66,8 @@ argument_specs:
|
|||||||
- >-
|
- >-
|
||||||
Windows made when the session is created (a login that only re-attaches adds none), next to a plain shell. A window closes when its
|
Windows made when the session is created (a login that only re-attaches adds none), next to a plain shell. A window closes when its
|
||||||
command exits. The commands come from the inventory and are shell-quoted, but they run as the user who logs in, are readable by
|
command exits. The commands come from the inventory and are shell-quoted, but they run as the user who logs in, are readable by
|
||||||
every user on the host and show in C(ps): put no secret in one.
|
every user on the host and show in C(ps): put no secret in one. They run with C(sh -c) from the tmux server started by the first
|
||||||
|
login, before C(~/.profile) is read, so use absolute paths for anything outside the default C(PATH).
|
||||||
options:
|
options:
|
||||||
name:
|
name:
|
||||||
type: str
|
type: str
|
||||||
|
|||||||
@@ -49,7 +49,6 @@
|
|||||||
that:
|
that:
|
||||||
- "'ufw' not in system_baseline_essential_packages"
|
- "'ufw' not in system_baseline_essential_packages"
|
||||||
- "'tmux' not in system_baseline_essential_packages"
|
- "'tmux' not in system_baseline_essential_packages"
|
||||||
- not system_baseline_tmux_autostart
|
|
||||||
- "'unattended-upgrades' in system_baseline_essential_packages"
|
- "'unattended-upgrades' in system_baseline_essential_packages"
|
||||||
- system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot'] == false
|
- system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot'] == false
|
||||||
- system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot-Time'] == '03:00'
|
- system_baseline_unattended_upgrade_settings['Unattended-Upgrade::Automatic-Reboot-Time'] == '03:00'
|
||||||
|
|||||||
@@ -127,3 +127,85 @@
|
|||||||
ansible.builtin.file:
|
ansible.builtin.file:
|
||||||
path: /var/run/reboot-required
|
path: /var/run/reboot-required
|
||||||
state: absent
|
state: absent
|
||||||
|
|
||||||
|
# The snippet must do nothing without a terminal: SSH_CONNECTION is set, stdin is not a tty. It must reach the line after it
|
||||||
|
# and leave no tmux server behind.
|
||||||
|
- name: Source the snippet with an SSH connection and no terminal
|
||||||
|
ansible.builtin.command:
|
||||||
|
argv: [/bin/sh, -c, '. /etc/profile.d/tmux-autostart.sh; echo reached']
|
||||||
|
environment:
|
||||||
|
SSH_CONNECTION: 192.0.2.1 50000 192.0.2.2 22
|
||||||
|
register: system_baseline_verify_snippet
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: It continued to the next line and started no tmux session
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that: system_baseline_verify_snippet.stdout == 'reached'
|
||||||
|
quiet: true
|
||||||
|
|
||||||
|
- name: Ask tmux for sessions
|
||||||
|
ansible.builtin.command: tmux list-sessions
|
||||||
|
register: system_baseline_verify_sessions
|
||||||
|
changed_when: false
|
||||||
|
failed_when: system_baseline_verify_sessions.rc == 0
|
||||||
|
|
||||||
|
# The inputs reject a trailing newline (a YAML block scalar adds one): the check anchors with \Z, not $.
|
||||||
|
- name: Run the validation with a session name that ends in a newline
|
||||||
|
block:
|
||||||
|
- name: Include the validation
|
||||||
|
ansible.builtin.include_role:
|
||||||
|
name: mortis.kamino.system_baseline
|
||||||
|
tasks_from: validate
|
||||||
|
vars:
|
||||||
|
system_baseline_system_type: ubuntu_lxc
|
||||||
|
system_baseline_tmux_autostart: true
|
||||||
|
system_baseline_tmux_session: "main\n"
|
||||||
|
- name: Unreachable when validation works
|
||||||
|
ansible.builtin.fail:
|
||||||
|
msg: a session name with a trailing newline was accepted
|
||||||
|
rescue:
|
||||||
|
- name: The validation refused it
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that: "'system_baseline_tmux_session must be' in ansible_failed_result.msg | default('')"
|
||||||
|
quiet: true
|
||||||
|
|
||||||
|
- name: Run the validation with a window command that ends in a newline
|
||||||
|
block:
|
||||||
|
- name: Include the validation
|
||||||
|
ansible.builtin.include_role:
|
||||||
|
name: mortis.kamino.system_baseline
|
||||||
|
tasks_from: validate
|
||||||
|
vars:
|
||||||
|
system_baseline_system_type: ubuntu_lxc
|
||||||
|
system_baseline_tmux_autostart: true
|
||||||
|
system_baseline_tmux_windows:
|
||||||
|
- name: logs
|
||||||
|
command: "journalctl -f\n"
|
||||||
|
- name: Unreachable when validation works
|
||||||
|
ansible.builtin.fail:
|
||||||
|
msg: a window command with a trailing newline was accepted
|
||||||
|
rescue:
|
||||||
|
- name: The validation refused it
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that: "'system_baseline_tmux_session must be' in ansible_failed_result.msg | default('')"
|
||||||
|
quiet: true
|
||||||
|
|
||||||
|
# The removal path runs last, because it deletes the snippet the checks above read. It lives here and not in converge: a
|
||||||
|
# converge that installs and then removes the file would not be idempotent.
|
||||||
|
- name: Run the tmux tasks with autostart off
|
||||||
|
ansible.builtin.include_role:
|
||||||
|
name: mortis.kamino.system_baseline
|
||||||
|
tasks_from: tmux
|
||||||
|
vars:
|
||||||
|
system_baseline_system_type: ubuntu_lxc
|
||||||
|
system_baseline_tmux_autostart: false
|
||||||
|
|
||||||
|
- name: Stat the snippet again
|
||||||
|
ansible.builtin.stat:
|
||||||
|
path: /etc/profile.d/tmux-autostart.sh
|
||||||
|
register: system_baseline_verify_removed
|
||||||
|
|
||||||
|
- name: The snippet is gone
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that: not system_baseline_verify_removed.stat.exists
|
||||||
|
quiet: true
|
||||||
@@ -1,26 +1,13 @@
|
|||||||
---
|
---
|
||||||
# Attach an interactive SSH login to a tmux session, created with the configured windows when it does not exist.
|
# Attach an interactive SSH login to a tmux session, created with the configured windows when it does not exist.
|
||||||
# /etc/profile.d is read only by an interactive login shell, so scp, sftp, `ssh host command` and automation never trigger it.
|
# /etc/profile.d is read by every login shell: the snippet itself acts only for an interactive one with a terminal and an SSH
|
||||||
|
# connection, so scp, sftp, `ssh host command` and automation never trigger it.
|
||||||
# sshd's ForceCommand is deliberately not used: it applies to every session type and a broken tmux would lock out all of them.
|
# sshd's ForceCommand is deliberately not used: it applies to every session type and a broken tmux would lock out all of them.
|
||||||
#
|
#
|
||||||
# Security: the session name, window names and commands are role inputs (inventory), never read from a user or a fact. They are
|
# Installed when autostart is on and the resolved package list holds tmux (the ubuntu_lxc and ubuntu_vm profiles); the inputs are
|
||||||
# validated here and shell-quoted in the template. The script is root-owned and not writable by the users it runs for. It is
|
# checked in validate.yml. Security: the session name, window names and commands are role inputs (inventory), never read from a user
|
||||||
# world-readable and the commands show in `ps`, so a command must hold no secret. Commands run as the user who logs in.
|
# or a fact. They are validated and shell-quoted in the template. The script is root-owned and not writable by the users it runs for.
|
||||||
- name: Check the tmux inputs
|
# It is world-readable and the commands show in `ps`, so a command must hold no secret. Commands run as the user who logs in.
|
||||||
ansible.builtin.assert:
|
|
||||||
that:
|
|
||||||
- system_baseline_tmux_session is match('^[A-Za-z0-9_-]{1,32}$')
|
|
||||||
- system_baseline_tmux_windows is sequence and system_baseline_tmux_windows is not string
|
|
||||||
- system_baseline_tmux_windows | map(attribute='name', default='') | map('regex_search', '^[A-Za-z0-9_-]{1,32}$') | select | list | length
|
|
||||||
== system_baseline_tmux_windows | length
|
|
||||||
- system_baseline_tmux_windows | map(attribute='command', default='') | select('match', '^[^\n\r]+$') | list | length
|
|
||||||
== system_baseline_tmux_windows | length
|
|
||||||
fail_msg: >-
|
|
||||||
system_baseline_tmux_session must be 1-32 characters of A-Z a-z 0-9 _ - ; every system_baseline_tmux_windows entry needs
|
|
||||||
a name (same characters) and a one-line command.
|
|
||||||
quiet: true
|
|
||||||
when: system_baseline_tmux_autostart | bool
|
|
||||||
|
|
||||||
- name: Install the tmux auto-start snippet
|
- name: Install the tmux auto-start snippet
|
||||||
ansible.builtin.template:
|
ansible.builtin.template:
|
||||||
src: tmux-autostart.sh.j2
|
src: tmux-autostart.sh.j2
|
||||||
@@ -29,14 +16,12 @@
|
|||||||
group: root
|
group: root
|
||||||
mode: "0644"
|
mode: "0644"
|
||||||
validate: /bin/sh -n %s
|
validate: /bin/sh -n %s
|
||||||
when:
|
when: system_baseline_tmux_autostart | bool and 'tmux' in system_baseline_essential_packages
|
||||||
- system_baseline_tmux_autostart | bool
|
|
||||||
- system_baseline_system_type in ['ubuntu_lxc', 'ubuntu_vm']
|
|
||||||
become: true
|
become: true
|
||||||
|
|
||||||
- name: Remove the tmux auto-start snippet
|
- name: Remove the tmux auto-start snippet
|
||||||
ansible.builtin.file:
|
ansible.builtin.file:
|
||||||
path: /etc/profile.d/tmux-autostart.sh
|
path: /etc/profile.d/tmux-autostart.sh
|
||||||
state: absent
|
state: absent
|
||||||
when: not (system_baseline_tmux_autostart | bool and system_baseline_system_type in ['ubuntu_lxc', 'ubuntu_vm'])
|
when: not (system_baseline_tmux_autostart | bool and 'tmux' in system_baseline_essential_packages)
|
||||||
become: true
|
become: true
|
||||||
@@ -17,3 +17,20 @@
|
|||||||
fail_msg: "system_baseline_system_type '{{ system_baseline_system_type }}' is invalid. Must be: ubuntu_vm, ubuntu_lxc, pve."
|
fail_msg: "system_baseline_system_type '{{ system_baseline_system_type }}' is invalid. Must be: ubuntu_vm, ubuntu_lxc, pve."
|
||||||
success_msg: "system_baseline_system_type '{{ system_baseline_system_type }}' is valid."
|
success_msg: "system_baseline_system_type '{{ system_baseline_system_type }}' is valid."
|
||||||
tags: always
|
tags: always
|
||||||
|
|
||||||
|
# \Z, not $: Python's `$` also matches before a final newline, which a YAML block scalar would add. Not inside a double-quoted
|
||||||
|
# YAML scalar, where \Z is an invalid escape. The types and required keys are checked by meta/argument_specs.yml.
|
||||||
|
- name: Validate the tmux session and windows
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- system_baseline_tmux_session is match('^[A-Za-z0-9_-]{1,32}\Z')
|
||||||
|
- system_baseline_tmux_windows | map(attribute='name') | select('match', '^[A-Za-z0-9_-]{1,32}\Z') | list | length
|
||||||
|
== system_baseline_tmux_windows | length
|
||||||
|
- system_baseline_tmux_windows | map(attribute='command') | select('match', '^[^\n\r]+\Z') | list | length
|
||||||
|
== system_baseline_tmux_windows | length
|
||||||
|
fail_msg: >-
|
||||||
|
system_baseline_tmux_session must be 1-32 characters of A-Z a-z 0-9 _ - ; every system_baseline_tmux_windows entry needs
|
||||||
|
a name (same characters) and a one-line command.
|
||||||
|
quiet: true
|
||||||
|
when: system_baseline_tmux_autostart | bool
|
||||||
|
tags: always
|
||||||
@@ -1,14 +1,22 @@
|
|||||||
# Managed by Ansible (mortis.kamino.system_baseline, tasks/tmux.yml). Do not edit: the next run overwrites it.
|
# Managed by Ansible (mortis.kamino.system_baseline, tasks/tmux.yml). Do not edit: the next run overwrites it.
|
||||||
# Interactive SSH logins only (profile.d is not read by scp, sftp, `ssh host command` or Ansible).
|
# Interactive login shells with a terminal and an SSH connection only: scp, sftp, `ssh host command` and Ansible have no terminal
|
||||||
|
# or no interactive shell, and `ssh -t host 'bash -lc cmd'` is a login shell that is not interactive (the case below keeps it out).
|
||||||
# Off for one user: touch ~/.no-tmux
|
# Off for one user: touch ~/.no-tmux
|
||||||
if [ -n "$SSH_CONNECTION" ] && [ -z "$TMUX" ] && [ -t 0 ] && [ ! -e "$HOME/.no-tmux" ] && command -v tmux >/dev/null 2>&1; then
|
case $- in
|
||||||
# new-session -d fails when the session exists, so the windows below are made only when this login creates it (once, even
|
*i*)
|
||||||
# for two logins at the same moment). Windows close when their command exits; window 0 is always a plain shell.
|
if [ -n "$SSH_CONNECTION" ] && [ -z "$TMUX" ] && [ -t 0 ] && [ ! -e "$HOME/.no-tmux" ] && command -v tmux >/dev/null 2>&1; then
|
||||||
if tmux new-session -d -s {{ system_baseline_tmux_session | quote }} 2>/dev/null; then
|
# new-session -d fails when the session exists, so the windows below are made only when this login creates it (once, even
|
||||||
:
|
# for two logins at the same moment). Windows close when their command exits; window 0 is always a plain shell.
|
||||||
|
if tmux new-session -d -s {{ system_baseline_tmux_session | quote }} 2>/dev/null; then
|
||||||
|
:
|
||||||
{% for window in system_baseline_tmux_windows %}
|
{% for window in system_baseline_tmux_windows %}
|
||||||
tmux new-window -d -t {{ ('=' ~ system_baseline_tmux_session ~ ':') | quote }} -n {{ window.name | quote }} {{ window.command | quote }}
|
tmux new-window -d -t {{ ('=' ~ system_baseline_tmux_session ~ ':') | quote }} -n {{ window.name | quote }} {{ window.command | quote }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
fi
|
fi
|
||||||
exec tmux attach-session -t {{ ('=' ~ system_baseline_tmux_session) | quote }}
|
# Not exec: a failed attach (for example a client TERM this host has no terminfo for) must leave a usable shell instead of
|
||||||
fi
|
# closing the connection on every login. A clean detach exits, so detaching ends the SSH connection.
|
||||||
|
tmux attach-session -t {{ ('=' ~ system_baseline_tmux_session) | quote }} && exit
|
||||||
|
echo 'tmux-autostart: could not attach to tmux, continuing with a plain shell (touch ~/.no-tmux to stop trying)' >&2
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
Reference in new issue
Block a user