#!/bin/bash
# Every user of the phone gets ~/.ssh with an authorized_keys (the keys of computers that may log
# in go there) and an Ed25519 key of their own (logins from the phone to elsewhere, sftp, git).
# Run by plinius-ssh-userkey.service at a user's first login, never at the image's build: an
# image offered for download would hand everyone the same private key. Whatever is there stays.
set -euo pipefail

dir="${HOME:?}/.ssh"
umask 077
mkdir -p -- "$dir"
# sshd ignores authorized_keys in a folder others may write to (StrictModes)
chmod 700 -- "$dir"
[[ -e $dir/authorized_keys ]] || : >"$dir/authorized_keys"
if [[ ! -e $dir/id_ed25519 && ! -e $dir/id_ed25519.pub ]]; then
	ssh-keygen -q -t ed25519 -N '' -C "$(id -un)@$(uname -n)" -f "$dir/id_ed25519"
fi
