xref: /freebsd/usr.bin/ssh-copy-id/ssh-copy-id.sh (revision 0957b409a90fd597c1e9124cbaf3edd2b488f4ac)
1#!/bin/sh
2#-
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4#
5# Copyright (c) 2012 Eitan Adler
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer
12#    in this position and unchanged.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29# $FreeBSD$
30
31usage() {
32	echo "usage: ssh-copy-id [-lv] [-i keyfile] [-o option] [-p port] [user@]hostname" >&2
33	exit 1
34}
35
36sendkey() {
37	local h="$1"
38	local k="$2"
39	printf "%s\n" "$k" | ssh $port -S none $options "$user$h" /bin/sh -c \'' \
40		set -e; \
41		umask 077; \
42		keyfile=$HOME/.ssh/authorized_keys ; \
43		mkdir -p -- "$HOME/.ssh/" ; \
44		while read alg key comment ; do \
45			[ -n "$key" ] || continue; \
46			if ! grep -sqwF "$key" "$keyfile"; then \
47				printf "$alg $key $comment\n" >> "$keyfile" ; \
48			fi ; \
49		done ; \
50		if [ -x /sbin/restorecon ]; then \
51			/sbin/restorecon -F "$HOME/.ssh/" "$keyfile" >/dev/null 2>&1 || true ; \
52		fi \
53	'\'
54}
55
56agentKeys() {
57	keys="$(ssh-add -L | grep -v 'The agent has no identities.')$nl$keys"
58}
59
60keys=""
61host=""
62hasarg=""
63user=""
64port=""
65nl="
66"
67options=""
68
69IFS=$nl
70
71while getopts 'i:lo:p:v' arg; do
72	case $arg in
73	i)
74		hasarg="x"
75		if [ -r "${OPTARG}.pub" ]; then
76			keys="$(cat -- "${OPTARG}.pub")$nl$keys"
77		elif [ -r "$OPTARG" ]; then
78			keys="$(cat -- "$OPTARG")$nl$keys"
79		else
80			echo "File $OPTARG not found" >&2
81			exit 1
82		fi
83		;;
84	l)
85		hasarg="x"
86		agentKeys
87		;;
88	p)
89		port=-p$nl$OPTARG
90		;;
91	o)
92		options=$options$nl-o$nl$OPTARG
93		;;
94	v)
95		options="$options$nl-v"
96		;;
97	*)
98		usage
99		;;
100	esac
101done >&2
102
103shift $((OPTIND-1))
104
105if [ -z "$hasarg" ]; then
106	agentKeys
107fi
108if [ -z "$keys" ] || [ "$keys" = "$nl" ]; then
109	echo "no keys found" >&2
110	exit 1
111fi
112if [ "$#" -eq 0 ]; then
113	usage
114fi
115
116for host in "$@"; do
117	sendkey "$host" "$keys"
118done
119