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$
29241862Seadler
30241862Seadlerusage() {
31241862Seadler	echo "usage: ssh-copy-id [-l] [-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 ; \
48242848Seadler		done \
49241862Seadler	'\' 
50241862Seadler}
51241862Seadler
52241862SeadleragentKeys() {
53241862Seadler	keys="$(ssh-add -L | grep -v 'The agent has no identities.')$nl$keys"
54241862Seadler}
55241862Seadler
56241862Seadlerkeys=""
57241862Seadlerhost=""
58241862Seadlerhasarg=""
59241862Seadleruser=""
60241862Seadlerport=""
61241862Seadlernl="
62241862Seadler"
63241862Seadleroptions=""
64241862Seadler
65242848SeadlerIFS=$nl
66242848Seadler
67241862Seadlerwhile getopts 'i:lo:p:' arg; do
68241862Seadler	case $arg in
69241862Seadler	i)	
70241862Seadler		hasarg="x"
71242848Seadler		if [ -r "$OPTARG" ]; then
72242848Seadler			keys="$(cat -- "$OPTARG")$nl$keys"
73242848Seadler		else
74242848Seadler			echo "File $OPTARG not found" >&2
75242848Seadler			exit 1
76241862Seadler		fi
77241862Seadler		;;
78241862Seadler	l)	
79241862Seadler		hasarg="x"
80241862Seadler		agentKeys
81241862Seadler		;;
82241862Seadler	p)	
83242848Seadler		port=-p$nl$OPTARG
84241862Seadler		;;
85241862Seadler	o)	
86242848Seadler		options=$options$nl-o$nl$OPTARG
87241862Seadler		;;
88241862Seadler	*)	
89241862Seadler		usage
90241862Seadler		;;
91241862Seadler	esac
92241862Seadlerdone >&2
93241862Seadler
94241862Seadlershift $((OPTIND-1))
95241862Seadler
96241862Seadlerif [ -z "$hasarg" ]; then
97241862Seadler	agentKeys
98241862Seadlerfi
99242848Seadlerif [ -z "$keys" ] || [ "$keys" = "$nl" ]; then
100241862Seadler	echo "no keys found" >&2
101241862Seadler	exit 1
102241862Seadlerfi
103242848Seadlerif [ "$#" -eq 0 ]; then
104241862Seadler	usage
105241862Seadlerfi
106241862Seadler
107241862Seadlerfor host in "$@"; do
108241862Seadler	sendkey "$host" "$keys"
109241862Seadlerdone
110