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