#!/bin/sh _main() { opts="$@" if [ -z "$opts" ] ; then _help fi for o in $opts ; do case "$o" in g) ARG="${opts#* }" ; ARG="${ARG%% *}" ; if [ "$o" == "$ARG" ] ; then _err "Option usage: '-$o /path/to/pubkey'" return 1 fi GPG_PUBKEY="$ARG" _install_gpg_pubkey ;; G) ARG="${opts#* }" ; ARG="${ARG%% *}" ; if [ "$o" == "$ARG" ] ; then _err "Option usage: '-$o /path/to/seckey'" return 1 fi GPG_SECKEY="$ARG" _install_gpg_seckey ;; s) ARG="${opts#* }" ; ARG="${ARG%% *}" ; if [ "$o" == "$ARG" ] ; then _err "Option usage: '-$o /path/to/pubkey'" return 1 fi SSH_PUBKEY="$ARG" _install_ssh_pubkey ;; S) ARG="${opts#* }" ; ARG="${ARG%% *}" ; if [ "$o" == "$ARG" ] ; then _err "Option usage: '-$o /path/to/seckey'" return 1 fi SSH_SECKEY="$ARG" _install_ssh_seckey ;; h) _help ;; *) if [ "$o" != "$ARG" ] ; then _err "Unknown option '-$o'" return 1 fi ;; esac opts="${opts#* }" done } _log() { printf '\033[34mLog:\033[0m \033[2m%s\033[0m\n' "$1" } _warn() { printf '\033[33mWarn:\033[0m \033[2m%s\033[0m\n' "$1" } _err() { printf '\033[31mError:\033[0m \033[2m%s\033[0m\n' "$1" >&2 } _help() { printf '\033[2mUsage:\033[0m \033[36m%s\033[0m \033[34m[OPTION]\033[0m\033[2m...\033[0m \033[35m[ARGUMENT]\033[0m\033[2m...\033[0m -g /path/to/pubkey \033[2mInstall public gpg key\033[0m -G /path/to/seckey \033[2mInstall private gpg key\033[0m -s /path/to/pubkey \033[2mInstall public ssh key\033[0m -S /path/to/seckey \033[2mInstall private ssh key\033[0m ' "$0" } _prompt() { printf '%s [y/N]: ' "$1" read -r opt if [ "$opt" == "y" ] ; then return 0 else return 1 fi } _missing() { missing= for p in $(echo "$@" | tr ' ' '\n' | sort -u) ; do if ! command -v "$p" >&- 2>&- ; then missing="$missing $p" fi done if [ -n "$missing" ] ; then echo "${missing# }" return 127 else return 0 fi } _install_gpg_pubkey() { missing="$(_missing gpg)" if [ -n "$missing" ] ; then _err "The following commands are missing from PATH: $missing" return 127 fi if ! [ -f "$GPG_PUBKEY" ] ; then _err "Bad gpg pubkey path" return 1 fi if [ -z "$GNUPGHOME" ] ; then if ! _prompt '$GNUPGHOME is not defined, continue? (default to ~/.gnupg)' ; then return 1 else GNUPGHOME="$HOME/.gnupg" fi fi if ! [ -d "$GNUPGHOME" ] ; then mkdir -p "$GNUPGHOME" chmod 700 "$GNUPGHOME" fi key_info="$(gpg -q --show-keys "$GPG_PUBKEY")" if [ -z "${key_info//sec*/}" ] ; then _err "Failed to import public key $GPG_PUBKEY, appears to be secret key" return 1 fi fingerprint="$(echo "$key_info" | sed '/^ *[A-Za-z0-9]*/!d;s/ //g')" if [ "$?" != "0" ] ; then _err 'Could not get fingerprint' return 1 fi gpg -q --batch --import "$GPG_PUBKEY" if [ "$?" != "0" ] ; then _err 'Could not import public key' return 1 fi _log "Imported $GPG_PUBKEY" echo "$fingerprint:6:" | gpg -q --batch --import-ownertrust if [ "$?" != "0" ] ; then _err 'Could not trust public key ultimately' return 1 fi _log "Trusted $GPG_PUBKEY ultimately" } _install_gpg_seckey() { missing="$(_missing gpg)" if [ -n "$missing" ] ; then _err "The following commands are missing from PATH: $missing" return 127 fi if ! [ -f "$GPG_SECKEY" ] ; then _err "Bad gpg seckey path" return 1 fi if [ -z "$GNUPGHOME" ] ; then if ! _prompt '$GNUPGHOME is not defined, continue? (default to ~/.gnupg)' ; then return 1 else GNUPGHOME="$HOME/.gnupg" fi fi if ! [ -d "$GNUPGHOME" ] ; then mkdir -p "$GNUPGHOME" chmod 700 "$GNUPGHOME" fi key_info="$(gpg -q --show-keys "$GPG_SECKEY")" if [ -z "${key_info//pub*/}" ] ; then _err "Failed to import secret key $GPG_SECKEY, appears to be public key" return 1 fi gpg -q --batch --import "$GPG_SECKEY" if [ "$?" != "0" ] ; then _err 'Could not import secret key' return 1 fi _log "Imported $GPG_SECKEY" } _install_ssh_pubkey() { if ! [ -f "$SSH_PUBKEY" ] ; then _err "Bad ssh pubkey path" return 1 fi if [ -z "$SSH_DIR" ] ; then if ! _prompt '$SSH_DIR is not defined, continue? (default to ~/.ssh)' ; then return 1 else SSH_DIR="$HOME/.ssh" fi fi if ! [ -d "$SSH_DIR" ] ; then mkdir -p "$SSH_DIR" chmod 700 "$SSH_DIR" fi if [ -f "$SSH_DIR/${SSH_PUBKEY##*/}" ] ; then if ! _prompt "$SSH_DIR/${SSH_PUBKEY##*/} already exists, overwrite?" ; then return 1 fi fi cp "$SSH_PUBKEY" "$SSH_DIR/${SSH_PUBKEY##*/}" if [ "$?" != 0 ] ; then _err 'Could not install public ssh key' return 1 fi chmod 644 "$SSH_DIR/${SSH_PUBKEY##*/}" _log "Imported $SSH_PUBKEY" } _install_ssh_seckey() { if ! [ -f "$SSH_SECKEY" ] ; then _err "Bad ssh seckey path" return 1 fi if [ -z "$SSH_DIR" ] ; then if ! _prompt '$SSH_DIR is not defined, continue? (default to ~/.ssh)' ; then return 1 else SSH_DIR="$HOME/.ssh" fi fi if ! [ -d "$SSH_DIR" ] ; then mkdir -p "$SSH_DIR" chmod 700 "$SSH_DIR" fi if [ -f "$SSH_DIR/${SSH_SECKEY##*/}" ] ; then if ! _prompt "$SSH_DIR/${SSH_SECKEY##*/} already exists, overwrite?" ; then return 1 fi fi cp "$SSH_SECKEY" "$SSH_DIR/${SSH_SECKEY##*/}" if [ "$?" != 0 ] ; then _err 'Could not install public ssh key' return 1 fi chmod 600 "$SSH_DIR/${SSH_SECKEY##*/}" _log "Imported $SSH_SECKEY" } _opts() { opts= for o in $@ ; do if [ -z "${o%%-*}" ] ; then opts="$opts $(echo "$o" | sed 's/[A-Za-z0-9]/& /g;s/-//g')" else opts="$opts $o " fi done opts="${opts% }" opts="${opts# }" echo "$opts" } _main $(_opts $@) exit "$?"