This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathkr-beta
executable file
·211 lines (181 loc) · 7.02 KB
/
kr-beta
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#! /bin/sh
VERSION=2.0.3
install_darwin() {
command -v brew &>/dev/null && test "$1" = "brew"
if [ "$?" != "0" ]; then
say Installing Krypton...
install_darwin_manual
else
# Check if already installed with brew
ls -l `command -v kr` | grep Cellar &>/dev/null
if [ "$?" = "0" ]; then
say Upgrading Krypton with Homebrew...
ensure brew update -v
ensure brew upgrade kryptco/tap/kr && brew link --overwrite kryptco/tap/kr
else
say "Installing Krypton with Homebrew. If Homebrew is too slow or fails, try \"curl https://krypt.co/kr | sh\""
ensure brew update -v
ensure brew install kryptco/tap/kr && brew link --overwrite kryptco/tap/kr
if [ "$?" != "0" ]; then
echo
read -p "Krypton Homebrew install failed. Try manual installation?" -n 1 -r < /dev/tty
if [ $REPLY != ^[Yy]$ ]
then
install_darwin_manual
fi
fi
fi
fi
}
verify_bottle_hash() {
say OpenSSL command line found, verifying downloaded binary hash...
DOWNLOADED_HASH=`openssl dgst -sha256 -hex /tmp/$KR_FILE_PREFIX.tar.gz | awk '{print $2}'`
test "$DOWNLOADED_HASH" = "$BOTTLE_HASH" || (say "Downloaded binary hash incorrect. Aborting."; rm /tmp/$KR_FILE_PREFIX.tar.gz; exit 1) || exit 1
say "Binary verified."
}
install_darwin_manual() {
need_cmd mv
need_cmd cp
need_cmd curl
need_cmd rm
need_cmd tar
need_cmd launchctl
need_cmd perl
need_cmd mkdir
need_cmd touch
need_cmd printf
MAJOR_MAC_VERSION=$(sw_vers -productVersion | awk -F '.' '{print $1 "." $2}')
case $MAJOR_MAC_VERSION in
10.10) OS_NAME=yosemite; BOTTLE_HASH=dbcf28b9f00bd0b85d7670ad646aca0d5052bd0785e4f311211ff3bd82d061a8;;
10.11) OS_NAME=el_capitan; BOTTLE_HASH=aa5eba355ce043899911146b133be01a965a3e5302bd6e4e8a8b8136c49b4f7d;;
10.12) OS_NAME=sierra; BOTTLE_HASH=67daf0d9f68734afe0183091c6693c697865fac167afa2b303431047b2329767;;
*) say "Unsupported OS X version $MAJOR_MAC_VERSION. Krypton requires 10.10+" && exit 1 ;;
esac
say Downloading Krypton.
KR_FILE_PREFIX=kr-$VERSION.$OS_NAME
ensure curl -# -o /tmp/$KR_FILE_PREFIX.tar.gz -L https://github.com/KryptCo/bottles/raw/master/$KR_FILE_PREFIX.bottle.tar.gz
command -v openssl &>/dev/null && verify_bottle_hash
ensure mk_owned_dir_if_not_exists /usr/local/lib
ensure mk_owned_dir_if_not_exists /usr/local/bin
ensure mk_owned_dir_if_not_exists /usr/local/share/kr
ignore rm -rf /tmp/kr
ensure tar xf /tmp/$KR_FILE_PREFIX.tar.gz -C /tmp/
ensure mv_maybe_sudo "/tmp/kr/$VERSION/bin/*" /usr/local/bin/
ensure mv_maybe_sudo "/tmp/kr/$VERSION/lib/*" /usr/local/lib/
ensure mv_maybe_sudo "/tmp/kr/$VERSION/share/kr/*" /usr/local/share/kr/
perl -0777 -pi -e 's/\@\@HOMEBREW_PREFIX\@\@/\/usr\/local/' /usr/local/share/kr/co.krypt.krd.plist
ensure mkdir -p ~/Library/LaunchAgents/
ensure cp /usr/local/share/kr/co.krypt.krd.plist ~/Library/LaunchAgents/
ignore launchctl stop ~/Library/LaunchAgents/co.krypt.krd.plist &>/dev/null
ignore launchctl unload ~/Library/LaunchAgents/co.krypt.krd.plist &>/dev/null
ensure launchctl load ~/Library/LaunchAgents/co.krypt.krd.plist
ensure mkdir -p ~/.ssh
ensure touch ~/.ssh/config
# remove old ssh_configs
perl -0777 -p -i.bak1 -e 's/\n# Added by Krypton\nHost \*\n\tPKCS11Provider \/usr\/local\/lib\/kr-pkcs11.so\n\tProxyCommand \`find \/usr\/local\/bin\/krssh 2\>\/dev\/null \|\| which nc\` %h %p\n\tIdentityFile ~\/.ssh\/id_kryptonite\n\tIdentityFile ~\/.ssh\/id_ed25519\n\tIdentityFile ~\/.ssh\/id_rsa\n\tIdentityFile ~\/.ssh\/id_ecdsa\n\tIdentityFile ~\/.ssh\/id_dsa//g' ~/.ssh/config
perl -0777 -p -i.bak2 -e 's/\n# Added by Krypton\nHost \*\n\tPKCS11Provider \/usr\/local\/lib\/kr-pkcs11.so//g' ~/.ssh/config
perl -0777 -ne '/\n# Added by Krypton\nHost \*\n\tPKCS11Provider \/usr\/local\/lib\/kr-pkcs11.so\n\tProxyCommand \`find \/usr\/local\/bin\/krssh 2\>\/dev\/null \|\| which nc\` %h %p\n\tIdentityFile kryptonite/ || exit(1)' ~/.ssh/config || printf "\n# Added by Krypton\nHost *\n\tPKCS11Provider /usr/local/lib/kr-pkcs11.so\n\tProxyCommand \`find /usr/local/bin/krssh 2>/dev/null || which nc\` %%h %%p\n\tIdentityFile ~/.ssh/id_kryptonite\n\tIdentityFile ~/.ssh/id_ed25519\n\tIdentityFile ~/.ssh/id_rsa\n\tIdentityFile ~/.ssh/id_ecdsa\n\tIdentityFile ~/.ssh/id_dsa" >> ~/.ssh/config
say Krypton installed successfully. Type \"kr pair\" to pair with the Krypton mobile app.
kr restart &>/dev/null
}
install_linux() {
need_cmd apt-get
need_cmd apt-add-repository
need_cmd apt-key
need_cmd sleep
say Adding KryptCo signing key...
ensure sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C4A05888A1C4FA02E1566F859F2A29A569653940
which kr && say Removing old version of kr...
ignore sudo apt-get remove kr -y &>/dev/null
say Adding KryptCo repository...
ignore sudo add-apt-repository --remove "deb http://kryptco.github.io/deb kryptco main" &>/dev/null
sleep 1
ignore sudo add-apt-repository --remove "deb http://kryptco.github.io/deb beta beta" &>/dev/null
sleep 1
ensure sudo add-apt-repository "deb http://kryptco.github.io/deb beta beta"
sleep 1
ignore sudo apt-get update
say Installing kr...
ensure sudo apt-get install kr -y
}
install() {
unamestr=`uname`
if [ "$unamestr" = 'Linux' ]; then
install_linux "$@"
elif [ "$unamestr" = 'Darwin' ]; then
install_darwin "$@"
else
say "OS $unamestr Unsupported"
exit 1
fi
}
is_my_dir() {
test "`ls -ld $1 | awk 'NR==1 {print $3}'`" = "$USER"
}
# mv $1 to $2, using sudo if necessary
# $2 must be a directory, not the new file name
mv_maybe_sudo() {
(is_my_dir $2 && ensure mv -f $1 $2) || ensure warn_sudo mv -f $1 $2
}
warn_sudo() {
sudo -n true 2>/dev/null || say "sudo required for command $@"
sudo "$@"
}
mk_owned_dir_if_not_exists() {
if [ ! -d "$1" ]; then
mkdir -p $1 &> /dev/null || (ensure warn_sudo mkdir -p $1 && ensure warn_sudo chown $USER $1)
fi
}
# Copyright 2016 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
say() {
echo "kr: $@"
}
say_err() {
say "$@" >&2
}
err() {
say "$@" >&2
exit 1
}
need_cmd() {
if ! command -v "$1" > /dev/null 2>&1
then err "need '$1' (command not found)"
fi
}
need_ok() {
if [ $? != 0 ]; then err "$1"; fi
}
assert_nz() {
if [ -z "$1" ]; then err "assert_nz $2"; fi
}
# Run a command that should never fail. If the command fails execution
# will immediately terminate with an error showing the failing
# command.
ensure() {
"$@"
need_ok "command failed: $*"
}
# This is just for indicating that commands' results are being
# intentionally ignored. Usually, because it's being executed
# as part of error handling.
ignore() {
run "$@"
}
# Runs a command and prints it to stderr if it fails.
run() {
"$@"
local _retval=$?
if [ $_retval != 0 ]; then
say_err "command failed: $*"
fi
return $_retval
}
install "$@"