#!/bin/bash
# nd-ssh-auth: SSH access settings for NDOS (used by rc.initial and nd-firstboot)
#
#   nd-ssh-auth status          -> prints "on" or "off" (SSH password login)
#   nd-ssh-auth password on|off -> allow / forbid SSH password login
#   nd-ssh-auth add-key         -> read a public key from stdin/prompt, add for ndadmin
#   nd-ssh-auth is-ec2          -> exit 0 on AWS EC2
#
# Key login is always allowed. The base sshd_config says PasswordAuthentication no;
# "password on" only adds the drop-in below (sshd_config includes it at the top).
set -u

ADMIN=ndadmin
PW_DROPIN=/etc/ssh/sshd_config.d/40-nd-password.conf

is_ec2() {
    grep -qi 'amazon' /sys/class/dmi/id/sys_vendor 2>/dev/null && return 0
    grep -q '^i-' /sys/class/dmi/id/board_asset_tag 2>/dev/null && return 0
    grep -qi 'amazon' /sys/class/dmi/id/bios_version 2>/dev/null && return 0
    grep -qi '^ec2' /sys/hypervisor/uuid 2>/dev/null && return 0
    return 1
}

reload_sshd() {
    if ! sshd -t; then
        echo "sshd configuration test failed; sshd not reloaded."
        return 1
    fi
    if systemctl is-active --quiet sshd; then
        systemctl reload-or-restart sshd
    fi
}

has_password() {
    local p
    p=$(getent shadow "$ADMIN" | cut -d: -f2)
    [[ $p == \$* ]]
}

case "${1:-}" in
status)
    if [ -f "$PW_DROPIN" ] && grep -qi '^PasswordAuthentication[[:space:]]\+yes' "$PW_DROPIN"; then
        echo on
    else
        echo off
    fi
    ;;
password)
    case "${2:-}" in
    on)
        if ! has_password; then
            echo "ndadmin has no password yet. Set one first (menu option 3)."
            exit 1
        fi
        if is_ec2; then
            echo "WARNING: this is an AWS instance. Password login over SSH is"
            echo "less secure than the EC2 key pair it was launched with."
            read -r -p "Type 'yes' to enable password login anyway: " a
            [ "$a" = yes ] || { echo "Cancelled."; exit 1; }
        fi
        install -d -m 0755 /etc/ssh/sshd_config.d
        printf 'PasswordAuthentication yes\n' > "$PW_DROPIN"
        chmod 0644 "$PW_DROPIN"
        reload_sshd && echo "SSH password login enabled."
        ;;
    off)
        rm -f "$PW_DROPIN"
        reload_sshd && echo "SSH password login disabled (key login only)."
        ;;
    *)
        echo "usage: nd-ssh-auth password on|off"; exit 2 ;;
    esac
    ;;
add-key)
    if [ -t 0 ]; then
        read -r -p "Paste the SSH public key for $ADMIN: " key
    else
        read -r key
    fi
    [ -n "${key:-}" ] || { echo "No key given."; exit 1; }
    if ! printf '%s\n' "$key" | ssh-keygen -l -f /dev/stdin >/dev/null 2>&1; then
        echo "That is not a valid SSH public key."
        exit 1
    fi
    h=$(getent passwd "$ADMIN" | cut -d: -f6)
    install -d -m 700 -o "$ADMIN" -g "$ADMIN" "$h/.ssh"
    touch "$h/.ssh/authorized_keys"
    if grep -qxF "$key" "$h/.ssh/authorized_keys"; then
        echo "Key already present."
    else
        printf '%s\n' "$key" >> "$h/.ssh/authorized_keys"
        echo "Key added: $(printf '%s\n' "$key" | ssh-keygen -l -f /dev/stdin)"
    fi
    chown "$ADMIN:$ADMIN" "$h/.ssh/authorized_keys"
    chmod 600 "$h/.ssh/authorized_keys"
    ;;
is-ec2)
    is_ec2
    ;;
*)
    echo "usage: nd-ssh-auth status | password on|off | add-key | is-ec2"
    exit 2
    ;;
esac
