1241862Seadler#!/bin/sh
2241862Seadler#-
3241862Seadler# Copyright (c) 2012 Eitan Adler
4241862Seadler# All rights reserved.
5241862Seadler#
6241862Seadler# Redistribution and use in source and binary forms, with or without
7241862Seadler# modification, are permitted provided that the following conditions
8241862Seadler# are met:
9241862Seadler# 1. Redistributions of source code must retain the above copyright
10241862Seadler#    notice, this list of conditions and the following disclaimer
11241862Seadler#    in this position and unchanged.
12241862Seadler# 2. Redistributions in binary form must reproduce the above copyright
13241862Seadler#    notice, this list of conditions and the following disclaimer in the
14241862Seadler#    documentation and/or other materials provided with the distribution.
15241862Seadler#
16241862Seadler# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17241862Seadler# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18241862Seadler# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19241862Seadler# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20241862Seadler# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21241862Seadler# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22241862Seadler# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23241862Seadler# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24241862Seadler# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25241862Seadler# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26241862Seadler# SUCH DAMAGE.
27241862Seadler#
28241862Seadler# $FreeBSD: releng/10.2/usr.bin/ssh-copy-id/ssh-copy-id.sh 284197 2015-06-10 01:17:14Z eadler $
29241862Seadler
30241862Seadlerusage() {
31262922Seadler	echo "usage: ssh-copy-id [-lv] [-i keyfile] [-o option] [-p port] [user@]hostname" >&2
32241862Seadler	exit 1
33241862Seadler}
34241862Seadler
35241862Seadlersendkey() {
36241862Seadler	local h="$1"
37242848Seadler	local k="$2"
38242848Seadler	printf "%s\n" "$k" | ssh $port -S none $options "$user$h" /bin/sh -c \'' \
39242848Seadler		set -e; \
40242848Seadler		umask 077; \
41242848Seadler		keyfile=$HOME/.ssh/authorized_keys ; \
42242848Seadler		mkdir -p -- "$HOME/.ssh/" ; \
43242848Seadler		while read alg key comment ; do \
44242848Seadler			[ -n "$key" ] || continue; \
45242848Seadler			if ! grep -sqwF "$key" "$keyfile"; then \
46242848Seadler				printf "$alg $key $comment\n" >> "$keyfile" ; \
47242848Seadler			fi ; \
48270257Seadler		done ; \
49262922Seadler		if [ -x /sbin/restorecon ]; then \
50262922Seadler			/sbin/restorecon -F "$HOME/.ssh/" "$keyfile" >/dev/null 2>&1 || true ; \
51284197Seadler		fi \
52241862Seadler	'\' 
53241862Seadler}
54241862Seadler
55241862SeadleragentKeys() {
56241862Seadler	keys="$(ssh-add -L | grep -v 'The agent has no identities.')$nl$keys"
57241862Seadler}
58241862Seadler
59241862Seadlerkeys=""
60241862Seadlerhost=""
61241862Seadlerhasarg=""
62241862Seadleruser=""
63241862Seadlerport=""
64241862Seadlernl="
65241862Seadler"
66241862Seadleroptions=""
67241862Seadler
68242848SeadlerIFS=$nl
69242848Seadler
70262922Seadlerwhile getopts 'i:lo:p:v' arg; do
71241862Seadler	case $arg in
72241862Seadler	i)	
73241862Seadler		hasarg="x"
74262922Seadler		if [ -r "${OPTARG}.pub" ]; then
75262922Seadler			keys="$(cat -- "${OPTARG}.pub")$nl$keys"
76262922Seadler		elif [ -r "$OPTARG" ]; then
77242848Seadler			keys="$(cat -- "$OPTARG")$nl$keys"
78242848Seadler		else
79242848Seadler			echo "File $OPTARG not found" >&2
80242848Seadler			exit 1
81241862Seadler		fi
82241862Seadler		;;
83241862Seadler	l)	
84241862Seadler		hasarg="x"
85241862Seadler		agentKeys
86241862Seadler		;;
87241862Seadler	p)	
88242848Seadler		port=-p$nl$OPTARG
89241862Seadler		;;
90241862Seadler	o)	
91242848Seadler		options=$options$nl-o$nl$OPTARG
92241862Seadler		;;
93262922Seadler	v)
94262922Seadler		options="$options$nl-v"
95262922Seadler		;;
96241862Seadler	*)	
97241862Seadler		usage
98241862Seadler		;;
99241862Seadler	esac
100241862Seadlerdone >&2
101241862Seadler
102241862Seadlershift $((OPTIND-1))
103241862Seadler
104241862Seadlerif [ -z "$hasarg" ]; then
105241862Seadler	agentKeys
106241862Seadlerfi
107242848Seadlerif [ -z "$keys" ] || [ "$keys" = "$nl" ]; then
108241862Seadler	echo "no keys found" >&2
109241862Seadler	exit 1
110241862Seadlerfi
111242848Seadlerif [ "$#" -eq 0 ]; then
112241862Seadler	usage
113241862Seadlerfi
114241862Seadler
115241862Seadlerfor host in "$@"; do
116241862Seadler	sendkey "$host" "$keys"
117241862Seadlerdone
118