summaryrefslogtreecommitdiff
path: root/Utilities/GitSetup/setup-ssh
blob: 8920a5bd338b046f9b9822a874df7c624da2f237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env bash
#=============================================================================
# Copyright 2010-2012 Kitware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================

# Run this script to set up ssh push access to the repository host.

# Project configuration instructions:
#
# - Populate adjacent "config" file with:
#    ssh.host = Repository host name
#    ssh.user = Username on host, if not "git"
#    ssh.key = Local ssh key name
#    ssh.request-url = Web page URL to request ssh access

egrep-q() {
	egrep "$@" >/dev/null 2>/dev/null
}

die() {
	echo 1>&2 "$@" ; exit 1
}

# Make sure we are inside the repository.
cd "${BASH_SOURCE%/*}" &&

# Load the project configuration.
host=$(git config -f config --get ssh.host) &&
user=$(git config -f config --get ssh.user || echo git) &&
key=$(git config -f config --get ssh.key) &&
request_url=$(git config -f config --get ssh.request-url) ||
die 'This project is not configured for ssh push access.'

# Check for existing configuration.
if test -r ~/.ssh/config &&
   egrep-q 'Host[= ]'"${host//\./\\.}" ~/.ssh/config; then
	echo 'Host "'"$host"'" is already in ~/.ssh/config' &&
	setup= &&
	question='Test'
else
	echo 'Host "'"$host"'" not found in ~/.ssh/config' &&
	setup=1 &&
	question='Setup and test'
fi &&

# Ask the user whether to make changes.
echo '' &&
read -ep "${question} push access by ssh to $user@$host? [y/N]: " access &&
if test "$access" != "y" -a "$access" != "Y"; then
	exit 0
fi &&

# Setup host configuration if necessary.
if test -n "$setup"; then
	if ! test -d ~/.ssh; then
		mkdir -p ~/.ssh &&
		chmod 700 ~/.ssh
	fi &&
	if ! test -f ~/.ssh/config; then
		touch ~/.ssh/config &&
		chmod 600 ~/.ssh/config
	fi &&
	ssh_config='Host='"$host"'
  IdentityFile ~/.ssh/'"$key" &&
	echo "Adding to ~/.ssh/config:

$ssh_config
" &&
	echo "$ssh_config" >> ~/.ssh/config &&
	if ! test -e ~/.ssh/"$key"; then
		if test -f ~/.ssh/id_rsa; then
			# Take care of the common case.
			ln -s id_rsa ~/.ssh/"$key"
			echo '
Assuming ~/.ssh/id_rsa is the private key corresponding to the public key for

  '"$user@$host"'

If this is incorrect place private key at "~/.ssh/'"$key"'".'
		else
			echo '
Place the private key corresponding to the public key registered for

  '"$user@$host"'

at "~/.ssh/'"$key"'".'
		fi
		read -e -n 1 -p 'Press any key to continue...'
	fi
fi || exit 1

# Test access configuration.
echo 'Testing ssh push access to "'"$user@$host"'"...' &&
if ! ssh "$user@$host" info; then
	die 'No ssh push access to "'"$user@$host"'".  You may need to request access at

  '"$request_url"'
'
fi