diff --git a/.changeset/installer-headless-sudo.md b/.changeset/installer-headless-sudo.md new file mode 100644 index 00000000..87f69481 --- /dev/null +++ b/.changeset/installer-headless-sudo.md @@ -0,0 +1,5 @@ +--- +"aicodeman": patch +--- + +The installer no longer stops on "Failed to obtain sudo privileges" in a headless run with a working passwordless sudo (`su - user -c`, cron, a CI runner, cloud-init): it now checks whether `/dev/tty` can be opened before taking sudo's input from it, where it used to test only that the node exists. diff --git a/docs/wiki/Installation.md b/docs/wiki/Installation.md index ee6b76bb..8921a2c3 100644 --- a/docs/wiki/Installation.md +++ b/docs/wiki/Installation.md @@ -110,7 +110,9 @@ approve those steps. With nothing else said, that also installs the first agent catalogue when none is found; `--ai-cli` (or `CODEMAN_AI_CLI`) says which one, or none. `CODEMAN_TAILSCALE=1` preselects the Tailscale answer, and never installs Tailscale itself non-interactively; a non-interactive run never renames the -machine and never starts a service. Everything the unattended steps print goes to +machine and never starts a service. With no terminal there is nowhere to type a password, +so a step that needs `sudo` works only where `sudo` asks for none (a `NOPASSWD` entry, as +cloud images and CI runners have). Everything the unattended steps print goes to `~/.codeman/install.log`, and the last lines of it are shown when a step fails. ### Without administrator rights diff --git a/install.sh b/install.sh index dc226e79..bead8c0b 100755 --- a/install.sh +++ b/install.sh @@ -1185,9 +1185,20 @@ ensure_sudo() { if ! command -v sudo &>/dev/null; then die "sudo is required but not installed. Please install packages manually or run as root." fi + # A sudo that runs without a password (NOPASSWD, or the timestamp + # sudo_session_try keeps warm) needs no terminal and nothing validated. + # `sudo -v` is no substitute for this test: it asks for a password as soon as + # ONE sudoers entry of the account asks for one, so an account in the sudo + # group with a NOPASSWD entry of its own (the cloud-image layout) runs + # `sudo ` freely and is refused by `sudo -v` (measured on sudo 1.9.15). + if sudo -n true 2>/dev/null; then + return 0 + fi # Validate sudo access - # When piped (curl | bash), stdin is the pipe — redirect from /dev/tty so sudo can prompt - if [[ -e /dev/tty ]]; then + # When piped (curl | bash), stdin is the pipe: redirect from /dev/tty so sudo can prompt. + # Only when it can be opened (see tty_openable): with no terminal at all, sudo gets + # its chance as it is and fails by itself when it would have to ask. + if tty_openable; then if ! sudo -v 2>/dev/null < /dev/tty; then die "Failed to obtain sudo privileges." fi @@ -1217,8 +1228,9 @@ ensure_homebrew() { fi info "Installing Homebrew first..." - # When piped (curl | bash), stdin is the pipe — Homebrew needs TTY for sudo password prompt - if [[ -e /dev/tty ]]; then + # When piped (curl | bash), stdin is the pipe: Homebrew needs TTY for sudo password prompt. + # With no terminal to open (see tty_openable) it runs unattended instead. + if tty_openable; then /bin/bash -c "$(download_to_stdout https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" < /dev/tty else NONINTERACTIVE=1 /bin/bash -c "$(download_to_stdout https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" @@ -2019,9 +2031,21 @@ fetch_source_archive() { # the keyboard even though the user is sitting at a terminal. These helpers # prompt via /dev/tty whenever a real terminal is available, and only fall back # to defaults when there is genuinely none (CI, truly headless pipes). + +# True when /dev/tty can really be OPENED. The node is there on every Linux and +# macOS, also for a process with no controlling terminal (su -c, cron, a CI +# runner, cloud-init, a container started without -t): `[[ -e /dev/tty ]]` says +# yes there, and a `< /dev/tty` behind it fails before the command ever runs. +# That ended a headless install with a working passwordless sudo on "Failed to +# obtain sudo privileges". Opening the node is the only test, so every redirect +# from /dev/tty asks this first, never `-e`. +tty_openable() { + { : < /dev/tty; } 2>/dev/null +} + has_tty() { [[ -t 0 ]] && return 0 - { : < /dev/tty; } 2>/dev/null + tty_openable } read_reply() { @@ -2102,7 +2126,7 @@ sudo_session_try() { if ! sudo -n true 2>/dev/null; then echo "" >&2 info "Some steps need administrator rights. You will be asked for your password once." - if [[ -e /dev/tty ]]; then + if tty_openable; then sudo -v < /dev/tty || return 1 else sudo -v || return 1 @@ -2124,7 +2148,7 @@ sudo_session_start() { if sudo_session_try; then return 0 fi - if [[ -e /dev/tty ]]; then + if tty_openable; then die "Failed to obtain sudo privileges." fi die "Failed to obtain sudo privileges. Try running the script directly instead of piping." @@ -2920,8 +2944,9 @@ offer_install_tailscale() { sudo_session_start info "Running the official Tailscale installer..." # When piped (curl | bash), stdin is our pipe: give the child installer - # the real terminal so its own sudo prompt works. - if [[ -e /dev/tty ]]; then + # the real terminal so its own sudo prompt works. A run with no terminal + # to open (see tty_openable) hands it stdin as it is. + if tty_openable; then if ! sh -c "$(download_to_stdout https://tailscale.com/install.sh)" < /dev/tty; then warn "Tailscale installation failed." return 1 @@ -2966,7 +2991,7 @@ ensure_tailscale_login() { if "$ts_bin" up --timeout=300s; then up_ok="1"; fi else sudo_session_start - if [[ -e /dev/tty ]]; then + if tty_openable; then if run_as_root "$ts_bin" up --timeout=300s < /dev/tty; then up_ok="1"; fi else if run_as_root "$ts_bin" up --timeout=300s; then up_ok="1"; fi diff --git a/test/install-sh-invariants.test.ts b/test/install-sh-invariants.test.ts index ed3c0c01..f83bfe3f 100644 --- a/test/install-sh-invariants.test.ts +++ b/test/install-sh-invariants.test.ts @@ -197,6 +197,162 @@ describe('install.sh runtime safety', () => { }); }); +describe('install.sh and a /dev/tty that exists but cannot be opened', () => { + // The /dev/tty node is there on every Linux and macOS, also for a process with no controlling + // terminal (`su - user -c`, cron, a CI runner, cloud-init). `[[ -e /dev/tty ]]` says yes + // there, and the `< /dev/tty` behind it fails before the command runs. Measured 2026-11-27 in + // a fresh ubuntu:24.04 container: a headless run with a working passwordless sudo ended on + // "Failed to obtain sudo privileges", because `sudo -v < /dev/tty` never started sudo. + // The fix is one probe, `tty_openable`, which OPENS the node. + + /** A file test on the node (`[[ -e /dev/tty ]]`, `test -c /dev/tty`): true with no terminal behind it. */ + const NODE_TEST = /(?:\[\[?|\btest)\s+!?\s*-[a-zA-Z]\s+"?\/dev\/tty"?/; + + /** + * Every command that takes its stdin from /dev/tty, and the ones that do not sit directly + * under `if tty_openable; then`. The probe itself and the two readers are the exceptions: + * read_reply and read_secret run only after has_tty said yes, and with stdin not a terminal + * that IS tty_openable. + */ + function ttyRedirects(source: string): { sites: string[]; unguarded: string[] } { + const exempt = (line: string): boolean => + /^\s*\{ : < \/dev\/tty; \} 2>\/dev\/null$/.test(line) || /^\s*read -rs? "\$1" < \/dev\/tty$/.test(line); + const lines = source.split('\n'); + const indentOf = (line: string): number => line.length - line.trimStart().length; + const sites: string[] = []; + const unguarded: string[] = []; + lines.forEach((line, i) => { + if (/^\s*#/.test(line) || !/<\s*\/dev\/tty\b/.test(line) || exempt(line)) return; + sites.push(line.trim()); + // The enclosing statement: the nearest line above with less indentation. + let j = i - 1; + while (j >= 0 && (lines[j].trim() === '' || indentOf(lines[j]) >= indentOf(line))) j--; + if (j < 0 || lines[j].trim() !== 'if tty_openable; then') unguarded.push(`${i + 1}: ${line.trim()}`); + }); + return { sites, unguarded }; + } + + it('never tests the /dev/tty node, only whether it opens', () => { + const offenders = CODE_LINES.filter((line) => NODE_TEST.test(line)); + expect( + offenders, + `a file test on /dev/tty says nothing about a controlling terminal; use tty_openable:\n ${offenders.join('\n ')}` + ).toEqual([]); + expect(SOURCE).toMatch(/^tty_openable\(\) \{\n {4}\{ : < \/dev\/tty; \} 2>\/dev\/null\n\}$/m); + }); + + it('takes stdin from /dev/tty only directly under `if tty_openable; then`', () => { + const { sites, unguarded } = ttyRedirects(SOURCE); + expect(unguarded, `redirect from /dev/tty without the open probe:\n ${unguarded.join('\n ')}`).toEqual([]); + // Anti-vacuity: both sudo sites are among what was checked. + expect(sites.filter((line) => /\bsudo -v\b/.test(line)).length).toBe(2); + expect(sites.length).toBeGreaterThanOrEqual(5); + }); + + it('fails for a sudo site that redirects from /dev/tty behind a bare -e test (anti-vacuity)', () => { + // The shape ensure_sudo had when the headless install died. + const old = [ + 'ensure_sudo() {', + ' if [[ -e /dev/tty ]]; then', + ' if ! sudo -v 2>/dev/null < /dev/tty; then', + ' die "Failed to obtain sudo privileges."', + ' fi', + ' fi', + '}', + ]; + expect(old.filter((line) => NODE_TEST.test(line))).toEqual([' if [[ -e /dev/tty ]]; then']); + expect(ttyRedirects(old.join('\n')).unguarded).toEqual(['3: if ! sudo -v 2>/dev/null < /dev/tty; then']); + // And the same site behind the probe passes. + old[1] = ' if tty_openable; then'; + expect(ttyRedirects(old.join('\n'))).toEqual({ + sites: ['if ! sudo -v 2>/dev/null < /dev/tty; then'], + unguarded: [], + }); + }); + + /** + * Drive the sudo helpers in a real bash that has NO controlling terminal: `detached` puts + * the child in a session of its own, which is what `su -c`, cron and a CI runner do. `sudo` + * is a shell function that records its arguments and answers from the environment, so no + * real sudo runs. Each stub reads stdin's state the way the real one would. + */ + function driveSudo(body: string, sudo: { n: number; v: number }) { + const driver = ` + set -euo pipefail + export CODEMAN_INSTALL_SH_LIB=1 + . "$1" + [[ -e /dev/tty ]] && echo "node=present" + if { : < /dev/tty; } 2>/dev/null; then echo "tty=open"; else echo "tty=closed"; fi + sudo() { + echo "sudo $*" + case "$1" in + -n) return "$SUDO_N_RC" ;; + -v) return "$SUDO_V_RC" ;; + esac + return 0 + } + NO_ADMIN=0 + ${body} + `; + const env = { ...process.env, SUDO_N_RC: String(sudo.n), SUDO_V_RC: String(sudo.v) }; + return spawnSync('bash', ['-c', driver, 'bash', INSTALL_SH], { + encoding: 'utf-8', + timeout: 30_000, + env, + detached: true, + }); + } + + // As root the helpers return before they reach sudo, so there is nothing to drive. + const asUser = it.skipIf(process.getuid?.() === 0); + + asUser('reproduces the condition: the node is there and does not open', () => { + const result = driveSudo('true', { n: 0, v: 0 }); + expect(result.status, result.stderr).toBe(0); + expect(result.stdout).toContain('node=present'); + expect(result.stdout).toContain('tty=closed'); + }); + + asUser('ensure_sudo passes on a passwordless sudo with no terminal, without validating', () => { + const result = driveSudo('ensure_sudo; echo "ensure=ok"', { n: 0, v: 1 }); + expect(result.status, result.stderr).toBe(0); + expect(result.stdout).toContain('ensure=ok'); + expect(result.stdout).toContain('sudo -n true'); + // `sudo -v` wants a password wherever one sudoers entry of the account asks for one, + // even when `sudo ` does not, so a sudo that already runs is not validated. + expect(result.stdout).not.toContain('sudo -v'); + }); + + asUser('ensure_sudo falls back to a plain `sudo -v` when the terminal cannot be opened', () => { + const result = driveSudo('ensure_sudo; echo "ensure=ok"', { n: 1, v: 0 }); + expect(result.status, result.stderr).toBe(0); + expect(result.stdout).toContain('sudo -v'); + expect(result.stdout).toContain('ensure=ok'); + }); + + asUser('ensure_sudo names the missing terminal when sudo would have to ask', () => { + const result = driveSudo('ensure_sudo; echo "ensure=ok"', { n: 1, v: 1 }); + expect(result.status).toBe(1); + expect(result.stdout).not.toContain('ensure=ok'); + expect(result.stderr).toContain('Failed to obtain sudo privileges. Try running the script directly'); + }); + + asUser('sudo_session_try reaches sudo instead of failing on the redirect', () => { + // -n fails, so the keepalive loop it starts ends at once and the driver does not wait for it. + const result = driveSudo('sudo_session_try >/dev/null; echo "try=$?"', { n: 1, v: 0 }); + expect(result.status, result.stderr).toBe(0); + expect(result.stdout).toContain('try=0'); + expect(result.stderr).not.toContain('/dev/tty'); + }); + + asUser('sudo_session_start says to run it directly when there is no terminal to ask on', () => { + const result = driveSudo('sudo_session_start >/dev/null', { n: 1, v: 1 }); + expect(result.status).toBe(1); + expect(result.stderr).toContain('Failed to obtain sudo privileges. Try running the script directly'); + expect(result.stderr).not.toContain('/dev/tty'); + }); +}); + describe('install.sh DeepSeek identity probe', () => { it('greps for the same banner the registry identity regex demands', () => { // dsh_banner_probe is the ONE hand-written identity check left in the script (the