X-Git-Url: https://review.fuel-infra.org/gitweb?a=blobdiff_plain;f=cirros-testvm%2Fsrc-cirros%2Fsrc%2Fusr%2Fbin%2Fssh-add-key;fp=cirros-testvm%2Fsrc-cirros%2Fsrc%2Fusr%2Fbin%2Fssh-add-key;h=c867dc2e9431f6f196d0493c24af5d91f4fba042;hb=b0a0f15dfaa205161a7fcb20cf1b8cd4948c2ef3;hp=0000000000000000000000000000000000000000;hpb=c6ac3cd55ee2da956195eee393b0882105dfad4e;p=packages%2Ftrusty%2Fcirros-testvm.git diff --git a/cirros-testvm/src-cirros/src/usr/bin/ssh-add-key b/cirros-testvm/src-cirros/src/usr/bin/ssh-add-key new file mode 100755 index 0000000..c867dc2 --- /dev/null +++ b/cirros-testvm/src-cirros/src/usr/bin/ssh-add-key @@ -0,0 +1,87 @@ +#!/bin/sh + +error() { echo "$@" 1>&2; } +fail() { [ $# -eq 0 ] || error "$@"; exit 1; } + +Usage() { + cat <&2; [ $# -eq 0 ] || error "$@"; exit 1; } + +short_opts="hpru" +long_opts="help,prefix:,replace,user:" +getopt_out=$(getopt --name "${0##*/}" \ + --options "${short_opts}" --long "${long_opts}" -- "$@") && + eval set -- "${getopt_out}" || + bad_Usage + +user="" +prefix="" +key="" +replace=false + +while [ $# -ne 0 ]; do + cur=${1}; next=${2}; + case "$cur" in + -h|--help) Usage ; exit 0;; + -p|--prefix) prefix="$next"; shift;; + -r|--replace) replace=true;; + --) shift; break;; + esac + shift; +done + +[ $# -ge 1 ] || bad_Usage "must provide keys" + +cd ~/ || fail "failed to cd ~" +mkdir -p -m 0755 .ssh || fail "failed to make .ssh" +umask 066 +: >> .ssh/authorized_keys || + fail "can't write to ~/.ssh/authorized_keys" + +if [ "$1" = "-" ]; then + keys="" + key="" + # we check for success or non empty string. + # on final line with no EOF, it returns 1, but sets key + while read key || [ -n "$key" ]; do + keys="${keys}|${key}" + done + keys=${keys#|}; + oifs="$IFS" + IFS="|" + set -- $keys + IFS="$oifs" +fi + +if $replace; then + for key in "$@"; do + removekey ".ssh/authorized_keys" "$key" || fail "failed to remove: $key" + done +fi + +{ + for key in "$@"; do + echo "${prefix:+${prefix} }$key" + done +} >> .ssh/authorized_keys +chmod 600 .ssh/authorized_keys + +# vi: ts=4 noexpandtab