diff --git a/.claude/memory/journal.md b/.claude/memory/journal.md index f136dcb..1354800 100644 --- a/.claude/memory/journal.md +++ b/.claude/memory/journal.md @@ -144,3 +144,6 @@ Done: `bind -n C-M-Up/Down/Left/Right select-pane` (root + copy-mode-vi, no vim ## 2026-10-11 — desktop + RDP offered, WSL default no Done: `offer_desktop()` (desktop [Y/n] → RDP [Y/n], WSL → [y/N] + hint), `confirm` default arg, `is_wsl()`. Trigger: WSL install lost DNS mid-run after ubuntu-desktop-minimal (hypothesis: NetworkManager/systemd-resolved rewrote /etc/resolv.conf), fail2ban/whois fetch failed. 8-case stub harness OK, shellcheck clean. Branch feature/desktop-rdp-prompt, commit 5d60269, pushed, not merged. + +## 2026-10-12 — WSL DNS confirmed + sshd hardening abort fixed +Done: WSL DNS root cause confirmed (ubuntu-desktop-minimal → systemd-resolved/NetworkManager rewrote resolv.conf); repair runbook (disable both, rm resolv.conf, `wsl --shutdown`) worked. Then install aborted at harden_sshd: `systemctl reload ssh` on inactive unit + `set -e`. Fix 64f06bb on bugfix/sshd-reload-inactive: `is-active` guard + `try-reload-or-restart`, `/run/sshd` created before `sshd -t` (Ubuntu 24.04: privsep dir exists only while ssh.service runs — challenge correctness MAJOR). Stub smoke both branches, shellcheck, semgrep PASS. Not merged. diff --git a/.claude/memory/learnings.md b/.claude/memory/learnings.md index 462fe10..db9a8cf 100644 --- a/.claude/memory/learnings.md +++ b/.claude/memory/learnings.md @@ -135,3 +135,15 @@ triggering pane as target: `set -pu @opt` needs no `-t`; `#{hook_pane}` quoting testable headless on a scratch pane (`new-window -d -P -F '#{pane_id}'`, assert `#{pane_in_mode}`); address by pane id, window-name plugin renames `-n` names. (5) `\;` inside tmux.conf '...' stays literal, then splits a bind's command list when the string is parsed by if-shell: works, no need to convert to `{ }` blocks. + +## LRN-018 — systemctl under `set -e`: guard reload, create /run/sshd before `sshd -t` +2026-10-12. Found fixing install.sh `harden_sshd` on WSL (commit 64f06bb). (1) Bare `systemctl reload ` +exits non-zero when unit inactive (" is not active, cannot reload.") → `set -e` kills installer. Guard: +`if systemctl is-active --quiet ; then sudo systemctl try-reload-or-restart ; else echo ...; fi`. +`try-reload-or-restart` exits 0 if unit stopped between check and call (no TOCTOU abort); `is-active` needs no +sudo, inside `if` never trips `set -e`. `restart` on inactive unit starts it, different semantic. (2) Ubuntu 24.04 +(systemd 255): `/run/sshd` created only by ssh.service `RuntimeDirectory=sshd`, no tmpfiles rule (systemd ≥ 256 +adds `20-systemd-ssh-generator.conf`). sshd never started → `sudo sshd -t` fails "Missing privilege separation +directory" → looks like config rejection. `sudo install -d -m 0755 /run/sshd` before `sshd -t`, idempotent. +(3) WSL: no sshd running by default, unit socket-activated or never started → any installer "validate + reload" +path must survive inactive unit. Stub `systemctl`/`sudo`/`sshd` on PATH in scratchpad to smoke both branches. diff --git a/.claude/tasks/contracts/2026-10-12-sshd-reload-inactive-0103.md b/.claude/tasks/contracts/2026-10-12-sshd-reload-inactive-0103.md new file mode 100644 index 0000000..1a17b94 --- /dev/null +++ b/.claude/tasks/contracts/2026-10-12-sshd-reload-inactive-0103.md @@ -0,0 +1,19 @@ +# CONTRACT — sshd-reload-inactive +- date: 2026-10-12 | flow: hotfix | branch: bugfix/sshd-reload-inactive +- status: active + +## REQUEST (verbatim — IMMUTABLE) +install.sh harden_sshd: `sudo systemctl reload ssh` fails under set -e when ssh.service is not active (WSL, socket-activated or never started) → "ssh.service is not active, cannot reload." aborts the whole install. Fix: reload only if `systemctl is-active --quiet ssh`, otherwise print that the drop-in is in place and applies at the next sshd start. Keep shellcheck clean, tabs. + +User message: "ok ca a fonctionne, par contre maintenant je bloque a Deploying sshd hardening drop-in +ssh.service is not active, cannot reload.. je sais pas si c'es tpour installer ssh-server ? Si oui demande aussi pour l'installer, si non il bloque pour quoi ?" + +## CLARIFICATIONS +none — request complete (openssh-server is already in the apt list; the block is the unconditional reload) + +## ACCEPTANCE CRITERIA +1. Symptom gone: harden_sshd no longer aborts the install when ssh.service is inactive (reload guarded by `systemctl is-active --quiet ssh`, else an informative echo). +2. Build/tests green: `bash -n install.sh` and `shellcheck install.sh` pass; indentation stays tabs. + +## FILE SCOPE +install.sh (harden_sshd only) diff --git a/.claude/tasks/plans/2026-10-12-sshd-reload-inactive-0103.md b/.claude/tasks/plans/2026-10-12-sshd-reload-inactive-0103.md new file mode 100644 index 0000000..3aaeaec --- /dev/null +++ b/.claude/tasks/plans/2026-10-12-sshd-reload-inactive-0103.md @@ -0,0 +1,55 @@ +# PLAN — sshd-reload-inactive (hotfix) + +## Root cause +`install.sh` runs under `set -euo pipefail`. `harden_sshd()` (lines ~226-236) ends with an +unconditional `sudo systemctl reload ssh`. On WSL (and any host where sshd is socket-activated +or simply not started) ssh.service is inactive, systemctl prints +"ssh.service is not active, cannot reload." and exits non-zero, so `set -e` aborts the whole +install at "Deploying sshd hardening drop-in". openssh-server itself IS installed (apt list, +line ~730); the drop-in has already been installed and validated with `sshd -t` at that point. + +## Exact edit (install.sh, harden_sshd, tabs) — revised after challenge +1. Before `sudo sshd -t`, add one line so the validation also works when ssh.service is + inactive (privsep dir normally created by the unit's RuntimeDirectory; absent on Ubuntu + 24.04 when sshd never ran — challenge correctness MAJOR + robustness MINOR 2): +``` + sudo install -d -m 0755 /run/sshd +``` +2. Replace the unconditional reload +``` + sudo systemctl reload ssh +} +``` +with +``` + if systemctl is-active --quiet ssh; then + sudo systemctl try-reload-or-restart ssh + else + echo "ssh.service not running — drop-in in place, applies at the next sshd start" + fi +} +``` +`try-reload-or-restart` exits 0 if the unit stopped between the check and the call +(robustness MINOR 1); the `is-active` guard stays for the informative echo. +3. Update the function's header comment (one or two lines): /run/sshd is created so `sshd -t` + works without a running sshd; the reload happens only when sshd is running. + +## Smoke (robustness MINOR 3 — exercise the else branch for real) +In the scratchpad, extract `harden_sshd` from install.sh into a temp script with +`SCRIPT_DIR` pointing at the repo, prepend a stub dir to PATH where `systemctl` exits 3 for +`is-active` (and 0 otherwise), `sudo` runs its arguments without privilege, `sshd` exits 0; +run under `set -euo pipefail` and assert exit 0 + the "drop-in in place" echo. Then flip the +stub (`is-active` exits 0) and assert `try-reload-or-restart ssh` is what gets called. +Plus `bash -n install.sh` and `shellcheck install.sh`. + +## Constraints +- No other change; `offer_ssh_memory_guard` uses `systemctl restart ssh`, which starts an inactive + unit and is out of scope. +- shellcheck clean, `bash -n` clean, tabs. + +## Challenge summary +- correctness MAJOR (/run/sshd missing → sshd -t false reject) → closed by edit 1. +- robustness MINOR 1 (check-then-act race) → closed by `try-reload-or-restart`. +- robustness MINOR 2 (same /run/sshd risk) → closed by edit 1. +- robustness MINOR 3 (gate never runs the else path) → closed by the Smoke section. +- simplicity: SOLID, no findings. diff --git a/CHANGELOG.md b/CHANGELOG.md index fd8c57f..b284f51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Format: [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). Versioning: se ### Fixed - `~/.gitconfig` was deployed with the literal `@USER@` / `@EMAIL@` placeholders: the installer read the identity from the repo bashrc template instead of the answers. - An `&` in the name no longer corrupts the rendered identity on bash 5.2 and later (`patsub_replacement` turned off). +- `install.sh`: the sshd hardening step no longer aborts the install when `ssh.service` is inactive (WSL, sshd never started): `/run/sshd` is created before `sshd -t`, so a valid drop-in is no longer removed as rejected, and the reload (`try-reload-or-restart`) runs only when sshd is active. ## [1.0.0] — 2026-10-06 diff --git a/README.md b/README.md index 0574349..861c32f 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ What it does: 10. On Linux, installs **code-server** (VS Code in the browser) via its vendor script — skipped if already present — and enables the `code-server@` systemd service (login from `id -un`: the bashrc overrides `$USER`). 11. On Linux, **offers** (`[Y/n]`, Enter = yes) to install **`ubuntu-desktop-minimal`** (GDM + GNOME Shell, ~1.5 GB): the RDP remote login below hands out a GNOME session, which a bare server install does not have. If accepted, then **offers** (`[Y/n]`) **RDP remote login** via `gnome-remote-desktop` (Wayland-native); no desktop means no RDP question. On **WSL** (`WSL_DISTRO_NAME` set or `microsoft` in `/proc/version`) both default to **no** (`[y/N]`): Windows owns the display, and the desktop drags in NetworkManager + systemd-resolved, which rewrite `/etc/resolv.conf` and break DNS for the rest of the install. With no terminal attached the default applies. The RDP setup installs the daemon + `openssl`, generates a self-signed TLS cert once, and prompts interactively for shared "gate" credentials (skipped when no terminal is attached, or already set). Disables `xrdp` if present; opens UFW port `3389` only when UFW is already active. Finally, when `lspci` sees an NVIDIA GPU, runs `ubuntu-drivers install` to put on the driver the distro recommends for the card (no version pinned; loads at the next reboot). Skipped on machines without an NVIDIA GPU. 12. On Linux, at the very end, **offers** (`[y/N]`) to install the **`cloudpex`** NAS mount helper to `/usr/local/bin` via `cloudpex/install.sh`, which prompts for the NAS host, share name, SMB user, mount point and SMB version and writes them to `/etc/cloudpex.conf` (root, `0600`; an existing config is shown and kept unless you say `n`; skipped when no terminal is attached). Nothing is mounted, no password stored, see [`cloudpex/README.md`](cloudpex/README.md). -13. On Linux, installs the **security baseline**, always, no prompt: **fail2ban** (+ `nftables`) with `etc/fail2ban/jail.d/local.conf` (sshd jail reading the journal, bans the offending IP on every port so the SSH port does not matter, 5 failures in 10 min → 1 h ban, loopback and private LAN ranges never banned); **unattended-upgrades** enabled through `etc/apt/apt.conf.d/20auto-upgrades`; and the **sshd drop-in** `etc/ssh/sshd_config.d/20-hardening.conf` (`PermitRootLogin no`, `MaxAuthTries 3`, `LoginGraceTime 20`), checked with `sshd -t` and removed again if sshd rejects it, then `reload ssh`. Authentication methods, port and user lists are left as they are. +13. On Linux, installs the **security baseline**, always, no prompt: **fail2ban** (+ `nftables`) with `etc/fail2ban/jail.d/local.conf` (sshd jail reading the journal, bans the offending IP on every port so the SSH port does not matter, 5 failures in 10 min → 1 h ban, loopback and private LAN ranges never banned); **unattended-upgrades** enabled through `etc/apt/apt.conf.d/20auto-upgrades`; and the **sshd drop-in** `etc/ssh/sshd_config.d/20-hardening.conf` (`PermitRootLogin no`, `MaxAuthTries 3`, `LoginGraceTime 20`), checked with `sshd -t` (`/run/sshd` is created first, so the check also works when sshd is not running) and removed again if sshd rejects it, then reloaded with `systemctl try-reload-or-restart ssh` when `ssh.service` is active; otherwise (WSL, sshd never started) the drop-in stays in place and applies at the next sshd start. Authentication methods, port and user lists are left as they are. 14. On Linux, at the very end, **offers** (`[y/N]`, skipped when no terminal is attached) to move **`/tmp` to disk**: Ubuntu mounts `/tmp` as a RAM-backed tmpfs capped at 50% of RAM, which agent runs fill, halving the RAM and breaking every shell with "No space left on device". Accepting masks `tmp.mount` and installs `etc/tmpfiles.d/tmp.conf` (wipe at boot, 10-day purge). Effective at the next reboot. 15. On Linux, at the very end, **offers** to keep **SSH reachable under memory pressure**: installs the `ssh.service` drop-in (`OOMScoreAdjust=-1000`, `MemoryMin=256M`) and `earlyoom` with `etc/default/earlyoom` (kills the largest process, `node`/`java` first and never `sshd`, once free RAM and swap both drop under 10%). Restarting `ssh` keeps open sessions. Note: `MemoryMin` protects the sshd daemon only; login sessions live in `user.slice`, so no setting can reserve RAM for a future shell. earlyoom acting in time is the real protection. 16. On Linux, at the very end, **offers** to set up **rootless Docker**: installs `uidmap dbus-user-session slirp4netns docker-ce-rootless-extras`, runs `dockerd-rootless-setuptool.sh install` as you (the apt package puts it in `/usr/bin`, so no `PATH` line is needed, unlike the `~/bin` install from `get.docker.com/rootless`), then `loginctl enable-linger` so the user daemon starts at boot. The tool creates `~/.config/systemd/user/docker.service` and switches the CLI to the `rootless` context; the deployed bashrc exports `DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock` whenever that socket exists, for tools that ignore contexts. The rootful daemon is disabled first (`systemctl disable --now docker.service docker.socket`, socket removed), as the docs recommend: `docker` then only talks to your user daemon. Needs `/etc/subuid` and `/etc/subgid` entries for the user (Ubuntu adds them at user creation). diff --git a/install.sh b/install.sh index 3e8e473..ac23fc2 100755 --- a/install.sh +++ b/install.sh @@ -220,19 +220,25 @@ install_unattended_upgrades() { # sshd hardening drop-in (PermitRootLogin, MaxAuthTries, LoginGraceTime): only # settings that cannot lock anyone out; authentication methods stay untouched. -# Validated with sshd -t before the reload. A rejected file is removed rather than -# left in place, so sshd keeps starting on the next boot; the install goes on and -# the warning tells you. +# Validated with sshd -t (/run/sshd is created first so the check works without a +# running sshd); the reload happens only when sshd is running. A rejected file is +# removed rather than left in place, so sshd keeps starting on the next boot; the +# install goes on and the warning tells you. harden_sshd() { echo "Deploying sshd hardening drop-in" sudo install -D -m 0644 "$SCRIPT_DIR/etc/ssh/sshd_config.d/20-hardening.conf" \ /etc/ssh/sshd_config.d/20-hardening.conf + sudo install -d -m 0755 /run/sshd if ! sudo sshd -t; then sudo rm -f /etc/ssh/sshd_config.d/20-hardening.conf echo "sshd rejected 20-hardening.conf — removed, sshd config unchanged" >&2 return 0 fi - sudo systemctl reload ssh + if systemctl is-active --quiet ssh; then + sudo systemctl try-reload-or-restart ssh + else + echo "ssh.service not running — drop-in in place, applies at the next sshd start" + fi } # Yes/no prompt for the optional system changes. $2 is the default taken on Enter: