#!/bin/sh # Installs the hotelbricks-access SSH key for the user running it. # curl -fsSL https://letmein.hotelbricks.dev | sh # curl -fsSL https://letmein.hotelbricks.dev | sh -s -- SHA256: # also check the key # The fingerprint check covers the key, not this script. set -eu url="https://letmein.hotelbricks.dev/key" want="${1:-}" fail() { echo "letmein: $*" >&2; exit 1; } if command -v curl >/dev/null 2>&1; then key=$(curl -fsSL "$url") || fail "could not download $url" elif command -v wget >/dev/null 2>&1; then key=$(wget -qO- "$url") || fail "could not download $url" else fail "needs curl or wget" fi lines=$(printf '%s\n' "$key" | wc -l) [ -n "$key" ] && [ $lines -eq 1 ] || fail "expected one key line, not installing" case "$key" in *" AAAA"*) ;; *) fail "downloaded data is not an SSH key, not installing" ;; esac fp="" if command -v ssh-keygen >/dev/null 2>&1; then tmp=$(mktemp) trap 'rm -f "$tmp"' EXIT printf '%s\n' "$key" > "$tmp" fp=$(ssh-keygen -lf "$tmp" 2>/dev/null | awk '{print $2}') || true [ -n "$fp" ] || fail "downloaded data is not an SSH key, not installing" fi if [ -n "$want" ]; then [ -n "$fp" ] || fail "ssh-keygen not found, cannot check the fingerprint" [ "$fp" = "$want" ] || fail "fingerprint mismatch: got $fp, expected $want" fi user=$(id -un) home=$(getent passwd "$user" 2>/dev/null | cut -d: -f6) || true [ -n "$home" ] || home="$HOME" dir="$home/.ssh" file="$dir/authorized_keys" mkdir -p "$dir" chmod 700 "$dir" touch "$file" chmod 600 "$file" if grep -qxF "$key" "$file"; then echo "letmein: already installed for $user${fp:+ ($fp)}" exit 0 fi # Start on a new line if the file does not end with one. if [ -s "$file" ] && [ -n "$(tail -c1 "$file")" ]; then echo >> "$file"; fi printf '%s\n' "$key" >> "$file" if command -v restorecon >/dev/null 2>&1; then restorecon -R "$dir" 2>/dev/null || true; fi echo "letmein: installed hotelbricks-access for $user${fp:+ ($fp)}"