dotfiles/unprocessed/config/scripts/machines

528 lines
14 KiB
Plaintext
Raw Normal View History

2016-12-10 17:44:55 +01:00
#!/usr/bin/env bash
# Handles indexing and SSH keys of machines I
# have access on
MACHINES_HOME=$HOME
MACHINES_CONFIG=$HOME/.config/machines
MACHINES_API=https://machines.frogeye.fr
2019-04-22 18:29:28 +02:00
mkdir -p "$MACHINES_HOME" &> /dev/null
mkdir -p "$MACHINES_CONFIG" &> /dev/null
2016-12-10 17:44:55 +01:00
# COMMON
function prompt { # text
2019-04-22 18:29:28 +02:00
while true
do
read -r -p "$1 [yn] " yn
2016-12-10 17:44:55 +01:00
case $yn in
[Yy]* ) return 1;;
[Nn]* ) return 0;;
* ) echo "Please answer y or n.";;
esac
done
}
# From https://gist.github.com/cdown/1163649
urlencode() { # string
old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
2019-04-22 18:29:28 +02:00
[a-zA-Z0-9.~_-]) printf "%s" "$c" ;;
2016-12-10 17:44:55 +01:00
*) printf '%%%02X' "'$c" ;;
esac
done
LC_COLLATE=$old_lc_collate
}
urldecode() { # string
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
# API ACCESS
function _machines-api {
route=$1
shift
temp=$(mktemp)
wget "$MACHINES_API/$route" --content-on-error --quiet --output-document=$temp "$@"
result=$?
if [ $result != 0 ]; then
2018-10-08 23:52:53 +02:00
echo "[ERROR] wget returned $result for route $route" 1>&2;
cat $temp 1>&2;
rm $temp
exit 2
fi
cat $temp
rm $temp
2016-12-10 17:44:55 +01:00
}
function _machines-apiToken {
2019-04-22 18:29:28 +02:00
read -r -p 'TOTP token: ' token
2016-12-10 17:44:55 +01:00
_machines-api "$@" --header="X-TOTP: $token"
}
function _machines-apiSigned {
_machines-ensureAdmin
2019-04-22 18:29:28 +02:00
_machines-api "$@" --certificate="$MACHINES_CONFIG/machines.crt" --private-key="$MACHINES_CONFIG/machines.key"
2016-12-10 17:44:55 +01:00
}
# APPLICATION KEYS & CERTIFICATE
function _machines-pubFromCrt {
2019-04-22 18:29:28 +02:00
openssl x509 -in "$MACHINES_CONFIG/machines.crt" -pubkey -noout > "$MACHINES_CONFIG/machines.pub"
2016-12-10 17:44:55 +01:00
}
2017-01-14 18:34:14 +01:00
function _machines-verifyCertificate {
2017-09-01 18:39:06 +02:00
return
2019-04-22 18:29:28 +02:00
if openssl verify "$MACHINES_CONFIG/machines.crt" | grep -v 'error 18' | grep 'error' --quiet; then
echo "[ERROR] Invalid certificate" 1>&2;
exit 1
2017-01-14 18:34:14 +01:00
fi
}
2016-12-10 17:44:55 +01:00
function _machines-ensurePub {
2019-04-22 18:29:28 +02:00
if [ ! -f "$MACHINES_CONFIG/machines.crt" ]; then
2016-12-10 17:44:55 +01:00
CERT_FILE=$(mktemp)
echo "[INFO] Downloading certificate..."
2019-04-22 18:29:28 +02:00
_machines-api cert > "$CERT_FILE"
openssl x509 -fingerprint -in "$CERT_FILE" | grep Fingerprint --color=never
2016-12-10 17:44:55 +01:00
prompt "Is this correct ?"
if [ $? == 1 ]; then
2019-04-22 18:29:28 +02:00
mv "$CERT_FILE" "$MACHINES_CONFIG/machines.crt" &> /dev/null
2016-12-10 17:44:55 +01:00
else
echo "[ERROR] Certificate rejected." 1>&2;
exit 1
2016-12-10 17:44:55 +01:00
fi
fi
2017-01-14 18:34:14 +01:00
_machines-verifyCertificate
2019-04-22 18:29:28 +02:00
if [ ! -f "$MACHINES_CONFIG/machines.pub" ]; then
2017-01-14 18:34:14 +01:00
_machines-pubFromCrt
fi
2017-01-14 19:13:01 +01:00
return 0
2016-12-10 17:44:55 +01:00
}
function _machines-ensureAdmin {
2019-04-22 18:29:28 +02:00
if [ ! -f "$MACHINES_CONFIG/machines.key" ]; then
echo "[ERROR] You need have to have the private key to do that" 1>&2;
exit 1
fi
}
2016-12-10 17:44:55 +01:00
# SSH ACCESS KEYS
function _machines-signAkey { # network
KEY_FILE=$(mktemp)
SIGN_FILE=$(mktemp)
2019-04-22 18:29:28 +02:00
_machines-apiSigned "akey/$1?unsigned" > "$KEY_FILE"
openssl dgst -sha256 -sign "$MACHINES_CONFIG/machines.key" -out "$SIGN_FILE" "$KEY_FILE"
_machines-apiSigned "akey/$1" --method=PUT --body-file="$SIGN_FILE"
rm "$KEY_FILE" "$SIGN_FILE" &> /dev/null
2016-12-10 17:44:55 +01:00
}
function _machines-getAkey { # network
2017-01-14 18:34:14 +01:00
_machines-ensurePub
2016-12-10 17:44:55 +01:00
KEY_FILE=$(mktemp)
SIGN_FILE=$(mktemp)
2019-04-22 18:29:28 +02:00
_machines-api "akey/$1" > "$KEY_FILE"
_machines-api "akey/$1?signature" > "$SIGN_FILE"
if openssl dgst -sha256 -verify "$MACHINES_CONFIG/machines.pub" -signature "$SIGN_FILE" "$KEY_FILE" &> /dev/null
then
cat "$KEY_FILE"
\rm "$KEY_FILE" "$SIGN_FILE"
2016-12-10 17:44:55 +01:00
return 0
else
2019-04-22 18:29:28 +02:00
\rm "$KEY_FILE" "$SIGN_FILE"
exit 1
2016-12-10 17:44:55 +01:00
fi
}
function _machines-updateAkey {
2017-10-07 13:20:10 +02:00
MYKEY_FILE=$(mktemp)
2019-04-22 18:29:28 +02:00
network=$(grep '^network=' "$MACHINES_CONFIG/this" | cut -d '=' -f 2)
if _machines-getAkey "$network" > "$MYKEY_FILE"
then
\mv -f "$MYKEY_FILE" "$MACHINES_HOME/.ssh/authorized_keys"
2019-06-03 21:03:41 +02:00
if [ -f "$MACHINES_HOME/.ssh/authorized_keys.tail" ]
then
cat "$MACHINES_HOME/.ssh/authorized_keys.tail" >> "$MACHINES_HOME/.ssh/authorized_keys"
fi
2016-12-10 17:44:55 +01:00
return 0
else
2019-04-22 18:29:28 +02:00
cat "$MYKEY_FILE"
echo "[ERROR] Authorized keys are not properly signed" 1>&2;
2019-04-22 18:29:28 +02:00
\rm "$MYKEY_FILE"
exit 1
2016-12-10 17:44:55 +01:00
fi
}
function _machines-postFile { # filename
2019-04-22 18:29:28 +02:00
cat $1 | while read -r line; do
parameter=$(echo "$line" | cut -d '=' -f 1)
value="$(echo "$line" | sed 's/^[a-zA-Z0-9]\+\(\[\]\)\?=//')"
echo -n "&$parameter=$(urlencode "$value")"
done
}
function _machines-addElement { # element elementType default
FILE=$(mktemp)
2019-04-22 18:29:28 +02:00
echo -e "$3" > "$FILE"
$EDITOR "$FILE"
data=$(_machines-postFile "$FILE")
\rm "$FILE"
_machines-apiSigned "$2" --post-data "name=$1$data"
}
function _machines-viewElement { # element elementType
_machines-apiSigned "$2/$1"
}
function _machines-editElement { # element elementType
FILE=$(mktemp)
2019-04-22 18:29:28 +02:00
_machines-apiSigned "$2/$1" > "$FILE"
$EDITOR "$FILE"
data=$(_machines-postFile "$FILE")
rm "$FILE" &> /dev/null
err=$(_machines-apiSigned "$2/$1" --post-data "$data")
}
function _machines-deleteElement { # element elementType
2019-04-22 18:29:28 +02:00
err=$(_machines-apiSigned "$2/$1" --method=DELETE)
}
2016-12-10 17:44:55 +01:00
# USER ADMIN FUNCTIONS
function machines_history {
2019-04-22 18:29:28 +02:00
if [ -f "$MACHINES_CONFIG/lastVerifiedLog" ]; then
2016-12-10 17:44:55 +01:00
from=$(<"$MACHINES_CONFIG/lastVerifiedLog")
else
from=0
fi
d=$(date +%s)
_machines-apiSigned log?from=$from | less
2019-04-22 18:29:28 +02:00
if prompt "Is this OK?"
then
exit 1
2019-04-22 18:29:28 +02:00
else
echo "$d" > "$MACHINES_CONFIG/lastVerifiedLog"
return 0
2016-12-10 17:44:55 +01:00
fi
}
function machines_sign {
machines_history
2016-12-10 17:44:55 +01:00
echo "Signing default network authorized_keys..."
_machines-signAkey
2019-04-22 18:29:28 +02:00
_machines-apiSigned network | while read -r network; do
2016-12-10 17:44:55 +01:00
echo "Signing network $network authorized_keys..."
_machines-signAkey $network
done
}
function machines_machine_list {
2016-12-10 20:04:26 +01:00
_machines-apiSigned machine
}
function machines_network_list {
2016-12-10 20:04:26 +01:00
_machines-apiSigned network
}
function machines_machine_add_help {
echo "Usage: $0 machine|mac|m add MACHINE"
echo
echo "Arguments:"
echo " MACHINE machine to add"
return 0
2016-12-10 17:44:55 +01:00
}
function machines_machine_add { # machine
2019-04-22 18:29:28 +02:00
if [ -z "$1" ]; then
machines_machine_add_help
exit 1
2016-12-10 17:44:55 +01:00
fi
2019-04-22 18:29:28 +02:00
_machines-addElement "$1" machine "host[]=\nnetwork=\nuserkey=\nhostkey=\nuser="
2016-12-10 17:44:55 +01:00
}
function machines_network_add_help {
echo "Usage: $0 network|net|n add NETWORK"
echo
echo "Arguments:"
echo " NETWORK Network to add"
return 0
2016-12-10 17:44:55 +01:00
}
2019-04-22 18:29:28 +02:00
function machines_network_add { # network
2019-04-22 18:29:28 +02:00
if [ -z "$1" ]; then
machines_network_add_help
exit 1
2016-12-10 17:44:55 +01:00
fi
2019-04-22 18:29:28 +02:00
_machines-addElement "$1" network "allowed[]=\nsecure=false"
}
function machines_machine_view_help {
echo "Usage: $0 machine|mac|m view MACHINE"
echo
echo "Arguments:"
echo " MACHINE machine to view"
return 0
}
function machines_machine_view { # machine
if [ -z "$1" ]; then
machines_machine_view_help
exit 1
fi
_machines-viewElement "$1" machine
}
function machines_network_view_help {
echo "Usage: $0 network|net|n view NETWORK"
echo
echo "Arguments:"
echo " NETWORK Network to view"
return 0
}
function machines_network_view { # network
if [ -z "$1" ]; then
machines_network_view_help
exit 1
fi
_machines-viewElement "$1" network
2016-12-10 17:44:55 +01:00
}
function machines_machine_edit_help {
echo "Usage: $0 machine|mac|m edit MACHINE"
echo
echo "Arguments:"
2019-04-22 18:29:28 +02:00
echo " MACHINE machine to edit"
return 0
}
2019-04-22 18:29:28 +02:00
function machines_machine_edit { # machine
2019-04-22 18:29:28 +02:00
if [ -z "$1" ]; then
machines_machine_edit_help
exit 1
fi
2019-04-22 18:29:28 +02:00
_machines-editElement "$1" machine
2016-12-10 17:44:55 +01:00
}
function machines_network_edit_help {
echo "Usage: $0 network|net|n edit NETWORK"
echo
echo "Arguments:"
2019-04-22 18:29:28 +02:00
echo " NETWORK Network to edit"
return 0
2016-12-10 17:44:55 +01:00
}
function machines_network_edit { # network
2019-04-22 18:29:28 +02:00
if [ -z "$1" ]; then
machines_network_edit_help
exit 1
2016-12-10 17:44:55 +01:00
fi
2019-04-22 18:29:28 +02:00
_machines-editElement "$1" network
2016-12-10 17:44:55 +01:00
}
function machines_machine_delete_help {
echo "Usage: $0 machine|mac|m delete machine"
echo
echo "Arguments:"
echo " MACHINE machine to remove"
return 0
}
function machines_machine_delete { # machine
2019-04-22 18:29:28 +02:00
if [ -z "$1" ]; then
machines_machine_delete_help
exit 1
fi
2019-04-22 18:29:28 +02:00
_machines-deleteElement "$1" machine
2016-12-10 17:44:55 +01:00
}
function machines_network_delete_help {
echo "Usage: $0 network|net|n delete NETWORK"
echo
echo "Arguments:"
echo " NETWORK Network to remove"
return 0
}
function machines_network_delete { # network
2019-04-22 18:29:28 +02:00
if [ -z "$1" ]; then
machines_network_delete_help
exit 1
fi
2019-04-22 18:29:28 +02:00
_machines-deleteElement "$1" network
2016-12-10 17:44:55 +01:00
}
function machines_machine_help {
echo "Usage: $0 machine|mac|m COMMAND"
echo
echo "Commands:"
echo " list List all machines"
echo " add Interactively add a machine"
2019-04-22 18:29:28 +02:00
echo " view Display a machine"
echo " edit Interactively edit a specified machine"
echo " delete Remove a specified machine"
echo " help Get help with commands"
return 0
}
function machines_machine {
command="$1"
shift
if type "machines_machine_$command" &> /dev/null; then
"machines_machine_$command" "$@"
else
machines_machine_help
fi
}
function machines_network_help {
echo "Usage: $0 network|net|n COMMAND"
echo
echo "Commands:"
echo " list List all networks"
echo " add Interactively add a network"
2019-04-22 18:29:28 +02:00
echo " view Display a network"
echo " edit Interactively edit a specified network"
echo " delete Remove a specified network"
echo " help Get help with commands"
return 0
}
function machines_network {
command="$1"
shift
if type "machines_network_$command" &> /dev/null; then
"machines_network_$command" "$@"
else
machines_network_help
fi
}
machines_mac() { machines_machine "$@"; }
machines_m() { machines_machine "$@"; }
machines_net() { machines_network "$@"; }
machines_n() { machines_network "$@"; }
machines_mac_help() { machines_machine_help "$@"; }
machines_m_help() { machines_machine_help "$@"; }
machines_net_help() { machines_network_help "$@"; }
machines_n_help() { machines_network_help "$@"; }
function machines_update-all {
2019-04-22 18:29:28 +02:00
machines_machine_list | while read -r machine; do
2016-12-11 08:12:15 +01:00
echo "Updating $machine..."
2020-08-20 21:38:48 +02:00
if [ $machine = $(cat "$MACHINES_CONFIG/this.name") ]; then
machines_update
continue
fi
ssh -n "$machine" 'cd .dotfiles; git pull; ./config/scripts/machines update'
2016-12-11 08:12:15 +01:00
done
}
function machines_regen-keys {
if [[ -e $MACHINES_CONFIG/machines.key || -e $MACHINES_CONFIG/machines.pub || -e $MACHINES_CONFIG/machines.crt ]]; then
echo "[ERROR] Please delete the pem files manually to prove you know what you're doing." 1>&2;
exit 1
else
2019-04-22 18:29:28 +02:00
openssl genrsa -out "$MACHINES_CONFIG/machines.key" 4096
chmod 600 "$MACHINES_CONFIG/machines.key"
openssl req -key "$MACHINES_CONFIG/machines.key" -new -out "$MACHINES_CONFIG/machines.csr"
openssl x509 -req -days 1826 -in "$MACHINES_CONFIG/machines.csr" -signkey "$MACHINES_CONFIG/machines.key" -out "$MACHINES_CONFIG/machines.crt"
_machines-pubFromCrt
fi
2016-12-10 17:44:55 +01:00
}
# USER FUNCTIONS
function machines_setup {
2019-04-22 18:29:28 +02:00
if [ -e "$MACHINES_CONFIG/this.name" ]; then
echo "[ERROR] This machine is already set up" 1>&2;
exit 1
2016-12-10 17:44:55 +01:00
fi
_machines-ensurePub
# Variables
2019-04-22 18:29:28 +02:00
read -r -p 'Machine name? ' name
read -r -p 'Hosts (separated by spaces)? ' hosts
2016-12-10 17:44:55 +01:00
# User key
2019-04-22 18:29:28 +02:00
mkdir -p "$MACHINES_HOME/.ssh" &> /dev/null
2016-12-10 17:44:55 +01:00
if [[ ! -f $MACHINES_HOME/.ssh/id_rsa || ! -f $MACHINES_HOME/.ssh/id_rsa.pub ]]; then
2019-04-22 18:29:28 +02:00
ssh-keygen -b 4096 -C "$name@machines.frogeye.fr" -f "$MACHINES_HOME/.ssh/id_rsa" -t rsa
2016-12-10 17:44:55 +01:00
fi
userkey=$(<"$MACHINES_HOME/.ssh/id_rsa.pub")
# Host key
for type in ecdsa ed25519 rsa dsa; do
if [ -f "/etc/ssh/ssh_host_${type}_key.pub" ]; then
hostkey=$(<"/etc/ssh/ssh_host_${type}_key.pub")
break
fi
done
# Subscription
data="name=$(urlencode "$name")&userkey=$(urlencode "$userkey")&hostkey=$(urlencode "$hostkey")&user=$(urlencode "$USER")"
for host in $hosts; do
data="$data&host[]=$(urlencode "$host")"
done
_machines-apiToken machine --post-data "$data"
2016-12-10 17:44:55 +01:00
2019-04-22 18:29:28 +02:00
echo "$name" > "$MACHINES_CONFIG/this.name"
2017-09-01 18:39:06 +02:00
machines_update
2016-12-10 17:44:55 +01:00
}
function machines_update {
2019-04-22 18:29:28 +02:00
_machines-api "machine/$(cat "$MACHINES_CONFIG/this.name")" > "$MACHINES_CONFIG/this"
_machines-updateAkey
}
2017-10-07 13:20:10 +02:00
function machines_totp {
url=$(_machines-apiSigned totp)
echo "URL : $url"
echo "$url" | qrencode -o - | feh -
}
function machines_help {
command="$1"
if [ -n "$command" ]; then
if type "machines_${command}_help" &> /dev/null; then
shift
"machines_${command}_help" "$@"
return $?
fi
fi
echo "Usage: $0 COMMAND"
echo
echo "User commands:"
echo " setup Interactive initial setup for new machine"
echo " update Update this machine"
echo " help Get help with commands"
echo
echo "Admin commands:"
echo " machine|mac|m Modify machines"
echo " network|net|n Modify networks"
echo " update-all Update all machines available via SSH"
echo " regen-keys Regenerate system keys"
2017-10-07 13:20:10 +02:00
echo " sign Sign recent transactions for propagation"
echo " totp Get TOTP generating QR code / URL"
return 0
}
# MAIN
command="$1"
shift
if type "machines_$command" &> /dev/null; then
"machines_$command" "$@"
else
2019-04-22 18:29:28 +02:00
machines_help "$@"
fi