diff --git a/connectors/host/host.go b/connectors/host/host.go index 4f47fbb..493aa6f 100644 --- a/connectors/host/host.go +++ b/connectors/host/host.go @@ -23,6 +23,9 @@ type Target struct { // HostKey is the host's public key in authorized_keys form, as // ssh-keyscan prints it. A host presenting any other key is refused. HostKey string `json:"host_key"` + // Helpers runs the lookups that walk directories through the root-owned + // helpers in target/, so they see what the account alone can't read. + Helpers bool `json:"helpers"` key ssh.PublicKey } @@ -107,7 +110,7 @@ func (h *host) Lookup(ctx context.Context, name string, raw jsontext.Value) (jso if limit <= 0 { limit = 20 } - command, parse, err := lookupCommand(name, p, limit, in.OlderThanHours) + command, parse, err := lookupCommand(name, p, limit, in.OlderThanHours, target.Helpers) if err != nil { return nil, false, err } @@ -129,13 +132,16 @@ func (h *host) Lookup(ctx context.Context, name string, raw jsontext.Value) (jso // lookupCommand builds the fixed command for a lookup. Every argument from // the input is shell-quoted, because ssh hands the command to the remote // shell. -func lookupCommand(name, p string, limit int, olderThan *int) (string, func([]byte) (any, error), error) { +func lookupCommand(name, p string, limit int, olderThan *int, helpers bool) (string, func([]byte) (any, error), error) { q := quote(p) n := strconv.Itoa(limit) switch name { case "filesystem_usage": return "LC_ALL=C df -P -B1 -- " + q + " && LC_ALL=C df -P -i -- " + q, parseDF, nil case "directory_sizes": + if helpers { + return helperDir + "directory-sizes " + q + " " + n, parseDU, nil + } return "LC_ALL=C du -x -B1 --max-depth=1 -- " + q + " 2>/dev/null | sort -rn | head -n " + n, parseDU, nil case "old_files": hours := 24 @@ -143,12 +149,18 @@ func lookupCommand(name, p string, limit int, olderThan *int) (string, func([]by hours = *olderThan } minutes := strconv.Itoa(hours * 60) + if helpers { + return helperDir + "old-files " + q + " " + minutes + " " + n, parseFind, nil + } return "LC_ALL=C find " + q + " -xdev -type f -mmin +" + minutes + " -printf '%s\\t%T@\\t%u\\t%p\\n' 2>/dev/null | sort -rn | head -n " + n, parseFind, nil } return "", nil, fmt.Errorf("no lookup named %q", name) } +// helperDir is where target/install.sh puts the helpers, run through sudo. +const helperDir = "sudo -n /usr/local/libexec/archie/" + // checkPath accepts an absolute path with no control characters, cleaned. func checkPath(p string) (string, error) { if !path.IsAbs(p) { diff --git a/connectors/host/target/README.md b/connectors/host/target/README.md new file mode 100644 index 0000000..60be7be --- /dev/null +++ b/connectors/host/target/README.md @@ -0,0 +1,12 @@ +# Host connector: what a target host needs + +- An unprivileged account (`archie`) whose `authorized_keys` holds the public + half of the machine credential the connector is accepted with. +- `df`, `du`, `find`, `sort` and `head` (GNU coreutils and findutils). +- For lookups to see files the account can't read, run `install.sh` as root. + It installs two helpers under `/usr/local/libexec/archie` and a sudo rule + allowing exactly those, and then set `"helpers": true` on the host in the + connector's `hosts` setting. The helpers only read, check every argument, + and run fixed commands: sudo can't safely allow `du` or `find` themselves, + since `find` can delete and execute. +- The host's public key, as `ssh-keyscan` prints it, in the `hosts` setting. diff --git a/connectors/host/target/directory-sizes b/connectors/host/target/directory-sizes new file mode 100755 index 0000000..073547a --- /dev/null +++ b/connectors/host/target/directory-sizes @@ -0,0 +1,9 @@ +#!/bin/sh +# directory-sizes PATH LIMIT: the largest entries directly under PATH, in +# bytes, staying on its filesystem. Read-only; run by the host connector +# through sudo, so it trusts none of its arguments. +set -eu +[ "$#" -eq 2 ] || { echo "usage: directory-sizes PATH LIMIT" >&2; exit 2; } +case "$1" in /*) ;; *) echo "path must be absolute" >&2; exit 2 ;; esac +case "$2" in '' | *[!0-9]*) echo "limit must be a number" >&2; exit 2 ;; esac +LC_ALL=C du -x -B1 --max-depth=1 -- "$1" 2>/dev/null | sort -rn | head -n "$2" diff --git a/connectors/host/target/install.sh b/connectors/host/target/install.sh new file mode 100755 index 0000000..2fa64e8 --- /dev/null +++ b/connectors/host/target/install.sh @@ -0,0 +1,9 @@ +#!/bin/sh +# Installs the host connector's read-only helpers and their sudo rule. Run as +# root on each host the connector reaches. +set -eu +here=$(dirname "$0") +install -d -m 755 /usr/local/libexec/archie +install -m 755 -o root -g root "$here/directory-sizes" "$here/old-files" /usr/local/libexec/archie/ +install -m 440 -o root -g root "$here/sudoers" /etc/sudoers.d/archie-host +visudo -cf /etc/sudoers.d/archie-host diff --git a/connectors/host/target/old-files b/connectors/host/target/old-files new file mode 100755 index 0000000..43a4adf --- /dev/null +++ b/connectors/host/target/old-files @@ -0,0 +1,11 @@ +#!/bin/sh +# old-files PATH MINUTES LIMIT: the largest files under PATH not modified for +# MINUTES, staying on its filesystem, as size, mtime, owner and path. +# Read-only; run by the host connector through sudo, so it trusts none of its +# arguments. +set -eu +[ "$#" -eq 3 ] || { echo "usage: old-files PATH MINUTES LIMIT" >&2; exit 2; } +case "$1" in /*) ;; *) echo "path must be absolute" >&2; exit 2 ;; esac +case "$2" in '' | *[!0-9]*) echo "minutes must be a number" >&2; exit 2 ;; esac +case "$3" in '' | *[!0-9]*) echo "limit must be a number" >&2; exit 2 ;; esac +LC_ALL=C find "$1" -xdev -type f -mmin +"$2" -printf '%s\t%T@\t%u\t%p\n' 2>/dev/null | sort -rn | head -n "$3" diff --git a/connectors/host/target/sudoers b/connectors/host/target/sudoers new file mode 100644 index 0000000..3ad67ec --- /dev/null +++ b/connectors/host/target/sudoers @@ -0,0 +1,2 @@ +# The host connector's read-only helpers, and nothing else, as root. +archie ALL=(root) NOPASSWD: /usr/local/libexec/archie/directory-sizes, /usr/local/libexec/archie/old-files