iop: ssh_(install_key): Unify scripts and rework

Unify the iop-subcommands install_key and ssh_install_key to one script.
In addition:

* Support new key types such as ssh-ed25519 which is supported by
  Dropbear nowadays. Also add unsupported ones to make them work
  automatically in the future.
* For the install_key command, do not re-add keys already added
* Read keys loaded into SSH Agent as well, which is useful when working
  with agent forwarding on a remote host or in a Docker container.
* For the ssh_install_key command also add keys added manually to the
  build dir.
This commit is contained in:
Andreas Gnau 2021-07-08 12:46:56 +02:00
parent aadbbda3e9
commit b84033c641
2 changed files with 44 additions and 23 deletions

View file

@ -1,12 +1,29 @@
# this is a developer helper script to install the public ssh key in the created image # this is a developer helper script to install the public ssh key in the created image
DROPBEAR_AUTHORIZED_KEYS_FILE=/etc/dropbear/authorized_keys
function get_ssh_public_keys {
(
shopt -s nullglob
# home directory, not all of those are supported by dropbear,
# but let's include them now already to decrease future maintenance
cat /dev/null ~/.ssh/{id_rsa,id_dsa,id_ecdsa,id_ecdsa_sk,id_ed25519,id_ed25519_sk,id_xmss}.pub 2> /dev/null
# keys added manually or automatically to the build dir
cat "files${DROPBEAR_AUTHORIZED_KEYS_FILE}" 2>/dev/null
# keys in the agent (useful when using SSH agent forwarding)
ssh-add -L 2> /dev/null
) | sort | uniq
}
function install_key { function install_key {
local build_dir_dropbear_authorized_keys_file="files${DROPBEAR_AUTHORIZED_KEYS_FILE}"
mkdir -p files/etc/dropbear mkdir -p "$(dirname "$build_dir_dropbear_authorized_keys_file")"
test -e ~/.ssh/id_dsa.pub && cat ~/.ssh/id_dsa.pub >>files/etc/dropbear/authorized_keys get_ssh_public_keys > "$build_dir_dropbear_authorized_keys_file"
test -e ~/.ssh/id_rsa.pub && cat ~/.ssh/id_rsa.pub >>files/etc/dropbear/authorized_keys chmod 0644 "$build_dir_dropbear_authorized_keys_file"
chmod 0644 files/etc/dropbear/authorized_keys echo "Keys in "$build_dir_dropbear_authorized_keys_file" are now:"
cat "$build_dir_dropbear_authorized_keys_file"
echo
echo "Disabling login on serial console..."
echo "::sysinit:/etc/init.d/rcS S boot" >files/etc/inittab echo "::sysinit:/etc/init.d/rcS S boot" >files/etc/inittab
echo "::shutdown:/etc/init.d/rcS K shutdown" >>files/etc/inittab echo "::shutdown:/etc/init.d/rcS K shutdown" >>files/etc/inittab
echo "console::askconsolelate:/bin/cttyhack /bin/ash --login" >>files/etc/inittab echo "console::askconsolelate:/bin/cttyhack /bin/ash --login" >>files/etc/inittab
@ -14,4 +31,25 @@ function install_key {
echo Done echo Done
} }
function ssh_install_key_help() {
echo Usage: $0 ssh_install_key HOSTNAME
echo
echo "Installs SSH public keys to a device's authorized_keys file"
}
# this is a developer helper script to install the public ssh key on host running dropbear
function ssh_install_key {
if [ $# -ne 1 ] || [ "$1" == '--help' ]; then
ssh_install_key_help
[ $# -eq 1 ]; return
fi
host="$1"
local keys="$(get_ssh_public_keys)"
echo "Adding the following keys to $DROPBEAR_AUTHORIZED_KEYS_FILE on $host:"
echo "$keys"
ssh root@$host "echo '$keys' >> '$DROPBEAR_AUTHORIZED_KEYS_FILE'" && echo ok
}
register_command "ssh_install_key" "Install the users public ssh key on host running dropbear"
register_command "install_key" "Install the user's public ssh key in the created image" register_command "install_key" "Install the user's public ssh key in the created image"

View file

@ -1,17 +0,0 @@
# this is a developer helper script to install the public ssh key on host running dropbear
function ssh_install_key {
if [ -e ~/.ssh/id_rsa.pub ]; then
echo "Adding public RSA key to $1"
KEY=`cat ~/.ssh/id_rsa.pub`
elif [ -e ~/.ssh/id_dsa.pub ]; then
echo "Adding public DSA key to $1"
KEY=`cat ~/.ssh/id_dsa.pub`
else
echo "No public key found"
exit 1
fi
ssh root@$1 "echo '$KEY' >> /etc/dropbear/authorized_keys" && echo ok
}
register_command "ssh_install_key" "Install the users public ssh key on host running dropbear"