xref: /freebsd/usr.bin/ssh-copy-id/ssh-copy-id.sh (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
12beb7085SEitan Adler#!/bin/sh
22beb7085SEitan Adler#-
3*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
41de7b4b8SPedro F. Giffuni#
52beb7085SEitan Adler# Copyright (c) 2012 Eitan Adler
62beb7085SEitan Adler#
72beb7085SEitan Adler# Redistribution and use in source and binary forms, with or without
82beb7085SEitan Adler# modification, are permitted provided that the following conditions
92beb7085SEitan Adler# are met:
102beb7085SEitan Adler# 1. Redistributions of source code must retain the above copyright
112beb7085SEitan Adler#    notice, this list of conditions and the following disclaimer
122beb7085SEitan Adler#    in this position and unchanged.
132beb7085SEitan Adler# 2. Redistributions in binary form must reproduce the above copyright
142beb7085SEitan Adler#    notice, this list of conditions and the following disclaimer in the
152beb7085SEitan Adler#    documentation and/or other materials provided with the distribution.
162beb7085SEitan Adler#
172beb7085SEitan Adler# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
182beb7085SEitan Adler# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192beb7085SEitan Adler# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
202beb7085SEitan Adler# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
212beb7085SEitan Adler# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
222beb7085SEitan Adler# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
232beb7085SEitan Adler# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
242beb7085SEitan Adler# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
252beb7085SEitan Adler# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
262beb7085SEitan Adler# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272beb7085SEitan Adler# SUCH DAMAGE.
282beb7085SEitan Adler#
292beb7085SEitan Adler
302beb7085SEitan Adlerusage() {
31df54ea80SEitan Adler	echo "usage: ssh-copy-id [-lv] [-i keyfile] [-o option] [-p port] [user@]hostname" >&2
322beb7085SEitan Adler	exit 1
332beb7085SEitan Adler}
342beb7085SEitan Adler
352beb7085SEitan Adlersendkey() {
362beb7085SEitan Adler	local h="$1"
37076ea53eSEitan Adler	local k="$2"
38076ea53eSEitan Adler	printf "%s\n" "$k" | ssh $port -S none $options "$user$h" /bin/sh -c \'' \
39076ea53eSEitan Adler		set -e; \
40076ea53eSEitan Adler		umask 077; \
41076ea53eSEitan Adler		keyfile=$HOME/.ssh/authorized_keys ; \
42076ea53eSEitan Adler		mkdir -p -- "$HOME/.ssh/" ; \
43076ea53eSEitan Adler		while read alg key comment ; do \
44076ea53eSEitan Adler			[ -n "$key" ] || continue; \
45076ea53eSEitan Adler			if ! grep -sqwF "$key" "$keyfile"; then \
46076ea53eSEitan Adler				printf "$alg $key $comment\n" >> "$keyfile" ; \
47076ea53eSEitan Adler			fi ; \
48377d232dSEitan Adler		done ; \
490f4d0972SEitan Adler		if [ -x /sbin/restorecon ]; then \
500f4d0972SEitan Adler			/sbin/restorecon -F "$HOME/.ssh/" "$keyfile" >/dev/null 2>&1 || true ; \
516f55e69eSEitan Adler		fi \
522beb7085SEitan Adler	'\'
532beb7085SEitan Adler}
542beb7085SEitan Adler
552beb7085SEitan AdleragentKeys() {
562beb7085SEitan Adler	keys="$(ssh-add -L | grep -v 'The agent has no identities.')$nl$keys"
572beb7085SEitan Adler}
582beb7085SEitan Adler
592beb7085SEitan Adlerkeys=""
602beb7085SEitan Adlerhost=""
612beb7085SEitan Adlerhasarg=""
622beb7085SEitan Adleruser=""
632beb7085SEitan Adlerport=""
642beb7085SEitan Adlernl="
652beb7085SEitan Adler"
662beb7085SEitan Adleroptions=""
672beb7085SEitan Adler
68076ea53eSEitan AdlerIFS=$nl
69076ea53eSEitan Adler
70df54ea80SEitan Adlerwhile getopts 'i:lo:p:v' arg; do
712beb7085SEitan Adler	case $arg in
722beb7085SEitan Adler	i)
732beb7085SEitan Adler		hasarg="x"
74df54ea80SEitan Adler		if [ -r "${OPTARG}.pub" ]; then
75df54ea80SEitan Adler			keys="$(cat -- "${OPTARG}.pub")$nl$keys"
76df54ea80SEitan Adler		elif [ -r "$OPTARG" ]; then
77076ea53eSEitan Adler			keys="$(cat -- "$OPTARG")$nl$keys"
78076ea53eSEitan Adler		else
79076ea53eSEitan Adler			echo "File $OPTARG not found" >&2
80076ea53eSEitan Adler			exit 1
812beb7085SEitan Adler		fi
822beb7085SEitan Adler		;;
832beb7085SEitan Adler	l)
842beb7085SEitan Adler		hasarg="x"
852beb7085SEitan Adler		agentKeys
862beb7085SEitan Adler		;;
872beb7085SEitan Adler	p)
88076ea53eSEitan Adler		port=-p$nl$OPTARG
892beb7085SEitan Adler		;;
902beb7085SEitan Adler	o)
91076ea53eSEitan Adler		options=$options$nl-o$nl$OPTARG
922beb7085SEitan Adler		;;
93df54ea80SEitan Adler	v)
94df54ea80SEitan Adler		options="$options$nl-v"
95df54ea80SEitan Adler		;;
962beb7085SEitan Adler	*)
972beb7085SEitan Adler		usage
982beb7085SEitan Adler		;;
992beb7085SEitan Adler	esac
1002beb7085SEitan Adlerdone >&2
1012beb7085SEitan Adler
1022beb7085SEitan Adlershift $((OPTIND-1))
1032beb7085SEitan Adler
1042beb7085SEitan Adlerif [ -z "$hasarg" ]; then
1052beb7085SEitan Adler	agentKeys
1062beb7085SEitan Adlerfi
107076ea53eSEitan Adlerif [ -z "$keys" ] || [ "$keys" = "$nl" ]; then
1082beb7085SEitan Adler	echo "no keys found" >&2
1092beb7085SEitan Adler	exit 1
1102beb7085SEitan Adlerfi
111076ea53eSEitan Adlerif [ "$#" -eq 0 ]; then
1122beb7085SEitan Adler	usage
1132beb7085SEitan Adlerfi
1142beb7085SEitan Adler
1152beb7085SEitan Adlerfor host in "$@"; do
1162beb7085SEitan Adler	sendkey "$host" "$keys"
1172beb7085SEitan Adlerdone
118