summaryrefslogtreecommitdiff
path: root/contrib/ssh-copy-id
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2020-09-22 10:07:43 +1000
committerDamien Miller <djm@mindrot.org>2020-09-22 10:07:43 +1000
commit9bb8a303ce05ff13fb421de991b495930be103c3 (patch)
treef1a8391347358b2ffa313e35ff145a226bc60f78 /contrib/ssh-copy-id
parent0a4a5571ada76b1b012bec9cf6ad1203fc19ec8d (diff)
sync with upstream ssh-copy-id rev f0da1a1b7
Diffstat (limited to 'contrib/ssh-copy-id')
-rw-r--r--contrib/ssh-copy-id158
1 files changed, 80 insertions, 78 deletions
diff --git a/contrib/ssh-copy-id b/contrib/ssh-copy-id
index b83b8361..392f64f9 100644
--- a/contrib/ssh-copy-id
+++ b/contrib/ssh-copy-id
@@ -1,6 +1,7 @@
#!/bin/sh
-# Copyright (c) 1999-2016 Philip Hands <phil@hands.com>
+# Copyright (c) 1999-2020 Philip Hands <phil@hands.com>
+# 2017 Sebastien Boyron <seb@boyron.eu>
# 2013 Martin Kletzander <mkletzan@redhat.com>
# 2010 Adeodato =?iso-8859-1?Q?Sim=F3?= <asp16@alu.ua.es>
# 2010 Eric Moret <eric.moret@gmail.com>
@@ -33,13 +34,15 @@
# Shell script to install your public key(s) on a remote machine
# See the ssh-copy-id(1) man page for details
+# shellcheck shell=dash
+
# check that we have something mildly sane as our shell, or try to find something better
if false ^ printf "%s: WARNING: ancient shell, hunting for a more modern one... " "$0"
then
SANE_SH=${SANE_SH:-/usr/bin/ksh}
if printf 'true ^ false\n' | "$SANE_SH"
then
- printf "'%s' seems viable.\n" "$SANE_SH"
+ printf "'%s' seems viable.\\n" "$SANE_SH"
exec "$SANE_SH" "$0" "$@"
else
cat <<-EOF
@@ -51,16 +54,16 @@ then
a bug describing your setup, and the shell you used to make it work.
EOF
- printf "%s: ERROR: Less dimwitted shell required.\n" "$0"
+ printf '%s: ERROR: Less dimwitted shell required.\n' "$0"
exit 1
fi
fi
-most_recent_id="$(cd "$HOME" ; ls -t .ssh/id*.pub 2>/dev/null | grep -v -- '-cert.pub$' | head -n 1)"
-DEFAULT_PUB_ID_FILE="${most_recent_id:+$HOME/}$most_recent_id"
+# shellcheck disable=SC2010
+DEFAULT_PUB_ID_FILE=$(ls -t "${HOME}"/.ssh/id*.pub 2>/dev/null | grep -v -- '-cert.pub$' | head -n 1)
usage () {
- printf 'Usage: %s [-h|-?|-f|-n] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname\n' "$0" >&2
+ printf 'Usage: %s [-h|-?|-f|-n] [-i [identity_file]] [-p port] [-F alternative ssh_config file] [[-o <ssh -o options>] ...] [user@]hostname\n' "$0" >&2
printf '\t-f: force mode -- copy keys without trying to check if they are already installed\n' >&2
printf '\t-n: dry run -- no keys are actually copied\n' >&2
printf '\t-h|-?: print this help\n' >&2
@@ -69,18 +72,18 @@ usage () {
# escape any single quotes in an argument
quote() {
- printf "%s\n" "$1" | sed -e "s/'/'\\\\''/g"
+ printf '%s\n' "$1" | sed -e "s/'/'\\\\''/g"
}
use_id_file() {
local L_ID_FILE="$1"
if [ -z "$L_ID_FILE" ] ; then
- printf "%s: ERROR: no ID file found\n" "$0"
+ printf '%s: ERROR: no ID file found\n' "$0"
exit 1
fi
- if expr "$L_ID_FILE" : ".*\.pub$" >/dev/null ; then
+ if expr "$L_ID_FILE" : '.*\.pub$' >/dev/null ; then
PUB_ID_FILE="$L_ID_FILE"
else
PUB_ID_FILE="$L_ID_FILE.pub"
@@ -93,7 +96,7 @@ use_id_file() {
ErrMSG=$( { : < "$f" ; } 2>&1 ) || {
local L_PRIVMSG=""
[ "$f" = "$PRIV_ID_FILE" ] && L_PRIVMSG=" (to install the contents of '$PUB_ID_FILE' anyway, look at the -f option)"
- printf "\n%s: ERROR: failed to open ID file '%s': %s\n" "$0" "$f" "$(printf "%s\n%s\n" "$ErrMSG" "$L_PRIVMSG" | sed -e 's/.*: *//')"
+ printf "\\n%s: ERROR: failed to open ID file '%s': %s\\n" "$0" "$f" "$(printf '%s\n%s\n' "$ErrMSG" "$L_PRIVMSG" | sed -e 's/.*: *//')"
exit 1
}
done
@@ -105,80 +108,37 @@ if [ -n "$SSH_AUTH_SOCK" ] && ssh-add -L >/dev/null 2>&1 ; then
GET_ID="ssh-add -L"
fi
-while test "$#" -gt 0
+while getopts "i:o:p:F:fnh?" OPT
do
- [ "${SEEN_OPT_I}" ] && expr "$1" : "[-]i" >/dev/null && {
- printf "\n%s: ERROR: -i option must not be specified more than once\n\n" "$0"
- usage
- }
-
- OPT= OPTARG=
- # implement something like getopt to avoid Solaris pain
- case "$1" in
- -i?*|-o?*|-p?*)
- OPT="$(printf -- "$1"|cut -c1-2)"
- OPTARG="$(printf -- "$1"|cut -c3-)"
- shift
- ;;
- -o|-p)
- OPT="$1"
- OPTARG="$2"
- shift 2
- ;;
- -i)
- OPT="$1"
- test "$#" -le 2 || expr "$2" : "[-]" >/dev/null || {
- OPTARG="$2"
- shift
- }
- shift
- ;;
- -f|-n|-h|-\?)
- OPT="$1"
- OPTARG=
- shift
- ;;
- --)
- shift
- while test "$#" -gt 0
- do
- SAVEARGS="${SAVEARGS:+$SAVEARGS }'$(quote "$1")'"
- shift
- done
- break
- ;;
- -*)
- printf "\n%s: ERROR: invalid option (%s)\n\n" "$0" "$1"
- usage
- ;;
- *)
- SAVEARGS="${SAVEARGS:+$SAVEARGS }'$(quote "$1")'"
- shift
- continue
- ;;
- esac
case "$OPT" in
- -i)
+ i)
+ [ "${SEEN_OPT_I}" ] && {
+ printf '\n%s: ERROR: -i option must not be specified more than once\n\n' "$0"
+ usage
+ }
SEEN_OPT_I="yes"
use_id_file "${OPTARG:-$DEFAULT_PUB_ID_FILE}"
;;
- -o|-p)
- SSH_OPTS="${SSH_OPTS:+$SSH_OPTS }$OPT '$(quote "$OPTARG")'"
+ o|p|F)
+ SSH_OPTS="${SSH_OPTS:+$SSH_OPTS }-$OPT '$(quote "${OPTARG}")'"
;;
- -f)
+ f)
FORCED=1
;;
- -n)
+ n)
DRY_RUN=1
;;
- -h|-\?)
+ h|\?)
usage
;;
esac
done
+#shift all args to keep only USER_HOST
+shift $((OPTIND-1))
+
+
-eval set -- "$SAVEARGS"
if [ $# = 0 ] ; then
usage
@@ -189,16 +149,18 @@ if [ $# != 1 ] ; then
fi
# drop trailing colon
-USER_HOST=$(printf "%s\n" "$1" | sed 's/:$//')
+USER_HOST="$*"
# tack the hostname onto SSH_OPTS
SSH_OPTS="${SSH_OPTS:+$SSH_OPTS }'$(quote "$USER_HOST")'"
# and populate "$@" for later use (only way to get proper quoting of options)
eval set -- "$SSH_OPTS"
+# shellcheck disable=SC2086
if [ -z "$(eval $GET_ID)" ] && [ -r "${PUB_ID_FILE:=$DEFAULT_PUB_ID_FILE}" ] ; then
use_id_file "$PUB_ID_FILE"
fi
+# shellcheck disable=SC2086
if [ -z "$(eval $GET_ID)" ] ; then
printf '%s: ERROR: No identities found\n' "$0" >&2
exit 1
@@ -209,6 +171,7 @@ fi
populate_new_ids() {
local L_SUCCESS="$1"
+ # shellcheck disable=SC2086
if [ "$FORCED" ] ; then
NEW_IDS=$(eval $GET_ID)
return
@@ -218,17 +181,20 @@ populate_new_ids() {
eval set -- "$SSH_OPTS"
umask 0177
- local L_TMP_ID_FILE=$(mktemp ~/.ssh/ssh-copy-id_id.XXXXXXXXXX)
+ local L_TMP_ID_FILE
+ L_TMP_ID_FILE=$(mktemp ~/.ssh/ssh-copy-id_id.XXXXXXXXXX)
if test $? -ne 0 || test "x$L_TMP_ID_FILE" = "x" ; then
printf '%s: ERROR: mktemp failed\n' "$0" >&2
exit 1
fi
local L_CLEANUP="rm -f \"$L_TMP_ID_FILE\" \"${L_TMP_ID_FILE}.stderr\""
+ # shellcheck disable=SC2064
trap "$L_CLEANUP" EXIT TERM INT QUIT
printf '%s: INFO: attempting to log in with the new key(s), to filter out any that are already installed\n' "$0" >&2
+ # shellcheck disable=SC2086
NEW_IDS=$(
eval $GET_ID | {
- while read ID || [ "$ID" ] ; do
+ while read -r ID || [ "$ID" ] ; do
printf '%s\n' "$ID" > "$L_TMP_ID_FILE"
# the next line assumes $PRIV_ID_FILE only set if using a single id file - this
@@ -261,21 +227,52 @@ populate_new_ids() {
fi
if [ -z "$NEW_IDS" ] ; then
printf '\n%s: WARNING: All keys were skipped because they already exist on the remote system.\n' "$0" >&2
- printf '\t\t(if you think this is a mistake, you may want to use -f option)\n\n' "$0" >&2
+ printf '\t\t(if you think this is a mistake, you may want to use -f option)\n\n' >&2
exit 0
fi
printf '%s: INFO: %d key(s) remain to be installed -- if you are prompted now it is to install the new keys\n' "$0" "$(printf '%s\n' "$NEW_IDS" | wc -l)" >&2
}
+# installkey_sh [target_path]
+# produce a one-liner to add the keys to remote authorized_keys file
+# optionally takes an alternative path for authorized_keys
+installkeys_sh() {
+ local AUTH_KEY_FILE=${1:-.ssh/authorized_keys}
+
+ # In setting INSTALLKEYS_SH:
+ # the tr puts it all on one line (to placate tcsh)
+ # (hence the excessive use of semi-colons (;) )
+ # then in the command:
+ # cd to be at $HOME, just in case;
+ # the -z `tail ...` checks for a trailing newline. The echo adds one if was missing
+ # the cat adds the keys we're getting via STDIN
+ # and if available restorecon is used to restore the SELinux context
+ INSTALLKEYS_SH=$(tr '\t\n' ' ' <<-EOF)
+ cd;
+ umask 077;
+ mkdir -p $(dirname "${AUTH_KEY_FILE}") &&
+ { [ -z \`tail -1c ${AUTH_KEY_FILE} 2>/dev/null\` ] || echo >> ${AUTH_KEY_FILE}; } &&
+ cat >> ${AUTH_KEY_FILE} ||
+ exit 1;
+ if type restorecon >/dev/null 2>&1; then
+ restorecon -F .ssh ${AUTH_KEY_FILE};
+ fi
+EOF
+
+ # to defend against quirky remote shells: use 'exec sh -c' to get POSIX;
+ printf "exec sh -c '%s'" "${INSTALLKEYS_SH}"
+}
+
REMOTE_VERSION=$(ssh -v -o PreferredAuthentications=',' -o ControlPath=none "$@" 2>&1 |
sed -ne 's/.*remote software version //p')
+# shellcheck disable=SC2029
case "$REMOTE_VERSION" in
NetScreen*)
populate_new_ids 1
for KEY in $(printf "%s" "$NEW_IDS" | cut -d' ' -f2) ; do
- KEY_NO=$(($KEY_NO + 1))
- printf "%s\n" "$KEY" | grep ssh-dss >/dev/null || {
+ KEY_NO=$((KEY_NO + 1))
+ printf '%s\n' "$KEY" | grep ssh-dss >/dev/null || {
printf '%s: WARNING: Non-dsa key (#%d) skipped (NetScreen only supports DSA keys)\n' "$0" "$KEY_NO" >&2
continue
}
@@ -283,20 +280,25 @@ case "$REMOTE_VERSION" in
if [ $? = 255 ] ; then
printf '%s: ERROR: installation of key #%d failed (please report a bug describing what caused this, so that we can make this message useful)\n' "$0" "$KEY_NO" >&2
else
- ADDED=$(($ADDED + 1))
+ ADDED=$((ADDED + 1))
fi
done
if [ -z "$ADDED" ] ; then
exit 1
fi
;;
+ dropbear*)
+ populate_new_ids 0
+ [ "$DRY_RUN" ] || printf '%s\n' "$NEW_IDS" | \
+ ssh "$@" "$(installkeys_sh /etc/dropbear/authorized_keys)" \
+ || exit 1
+ ADDED=$(printf '%s\n' "$NEW_IDS" | wc -l)
+ ;;
*)
# Assuming that the remote host treats ~/.ssh/authorized_keys as one might expect
populate_new_ids 0
- # in ssh below - to defend against quirky remote shells: use 'exec sh -c' to get POSIX;
- # 'cd' to be at $HOME; add a newline if it's missing; and all on one line, because tcsh.
[ "$DRY_RUN" ] || printf '%s\n' "$NEW_IDS" | \
- ssh "$@" "exec sh -c 'cd ; umask 077 ; mkdir -p .ssh && { [ -z "'`tail -1c .ssh/authorized_keys 2>/dev/null`'" ] || echo >> .ssh/authorized_keys ; } && cat >> .ssh/authorized_keys || exit 1 ; if type restorecon >/dev/null 2>&1 ; then restorecon -F .ssh .ssh/authorized_keys ; fi'" \
+ ssh "$@" "$(installkeys_sh)" \
|| exit 1
ADDED=$(printf '%s\n' "$NEW_IDS" | wc -l)
;;