-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathssh-manager.sh
executable file
·165 lines (133 loc) · 3.84 KB
/
ssh-manager.sh
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
#!/bin/bash
# Set the version
VERSION="v1.5.4"
# Get the source directory
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
# Set the script root path
PATH_ROOT="$DIR"
# Set the library root path
LIBRARY_PATH_ROOT="$PATH_ROOT/libs"
# Include all libraries in the libs directory
for f in "$LIBRARY_PATH_ROOT"/*.sh; do
# Include the directory
source "$f"
done
# Setup the global variables
KEY_MIN_LENGH=1024
KEY_MIN_RECOM=2048
KEY_MIN_DEFAULT=4096
KEY_PERM_DIR="0700"
KEY_PERM_PUB="0644"
KEY_PERM_PRIV="0600"
# Set the key base root
KEY_PATH_ROOT="$HOME/.ssh"
# Define the executables to use
EXEC_KEYGEN="ssh-keygen"
EXEC_COPYID="ssh-copy-id"
EXEC_OPENSSL="openssl"
EXEC_COPY="xclip"
args=" $@ "
regexArgAction='^ ([^ -]*) ?([^ ]*) '
[[ $args =~ $regexArgAction ]]
ACTION="${BASH_REMATCH[1]}"
CONNECTION_STRING="${BASH_REMATCH[2]}"
regexArgComment=' -(-comment|c) ([^ ]+) '
[[ $args =~ $regexArgComment ]]
if [ "${BASH_REMATCH[2]}" != "" ]; then
KEY_COMMENT="${BASH_REMATCH[2]}"
else
KEY_COMMENT=""
fi
regexArgType=' -(-type|t) ([^ ]+) '
[[ $args =~ $regexArgType ]]
if [ "${BASH_REMATCH[2]}" != "" ]; then
KEY_TYPE="${BASH_REMATCH[2]}"
else
KEY_TYPE="rsa"
fi
regexArgBits=' -(-bits|b) ([0-9]+) '
[[ $args =~ $regexArgBits ]]
if [ "${BASH_REMATCH[2]}" != "" ]; then
KEY_BITS="${BASH_REMATCH[2]}"
else
KEY_BITS=$KEY_MIN_DEFAULT
fi
regexArgPasswd=' -(-passwd|p) '
[[ $args =~ $regexArgPasswd ]]
if [ "${BASH_REMATCH[1]}" != "" ]; then
KEY_PASS_PROMPT=true
else
KEY_PASS_PROMPT=false
fi
regexArgPaths=' -(-paths) '
[[ $args =~ $regexArgPaths ]]
if [ "${BASH_REMATCH[1]}" != "" ]; then
KEY_PATH_SHOW=true
else
KEY_PATH_SHOW=false
fi
regexArgRemovalProgram=' -(-remove-with|d) ([^ ]+) '
[[ $args =~ $regexArgRemovalProgram ]]
if [ "${BASH_REMATCH[2]}" != "" ]; then
KEY_REMOVAL_PROG="${BASH_REMATCH[2]}"
else
KEY_REMOVAL_PROG="shred"
fi
regexArgPasswd=' -(-simple) '
[[ $args =~ $regexArgPasswd ]]
if [ "${BASH_REMATCH[1]}" != "" ]; then
KEY_FINGERPRINT_SIMPLE=true
else
KEY_FINGERPRINT_SIMPLE=false
fi
# Show the banner
if [ ! $KEY_FINGERPRINT_SIMPLE = true ]; then
echo -e "-----------------------------------------"
echo -e "$COLOUR_GRN Multi SSH Key Manager $VERSION$COLOUR_RST"
echo -e "-----------------------------------------"
echo
fi
# If no parameters were passed show the usage
if [ $# = 0 ]; then
usage
fi
#echo "ACTION $ACTION"
#echo "CONNECTION_STRING $CONNECTION_STRING"
#echo "KEY_PASS_PROMPT $KEY_PASS_PROMPT"
#echo "KEY_COMMENT $KEY_COMMENT"
#echo "KEY_BITS $KEY_BITS"
#echo "KEY_TYPE $KEY_TYPE"
#echo "KEY_PATH_SHOW $KEY_PATH_SHOW"
# Warn if the requested key length is <= the minimum
if [ $KEY_BITS -lt $KEY_MIN_LENGH ]; then
echo -e "$COLOUR_RED""WARNING:$COLOUR_RST Requested key length is short! Please use key lengths greater than $KEY_MIN_RECOM bits"
fi
# Split the connection string into the hostname and the username
IFS='@' read KEY_USER KEY_HOSTNAME <<< "$CONNECTION_STRING"
# Only throw an error if the connection string is missing for actions that require them
if [ "$ACTION" = "create" ] || [ "$ACTION" = "remove" ] || [ "$ACTION" = "authorise" ] || [ "$ACTION" = "fingerprint" ] || [ "$ACTION" = "copy" ]; then
# Check both the username and hostname have been found correctly
if [ "$KEY_USER" = "" ]; then
echo "ERROR: Please specify a username"
exit
fi
if [ "$KEY_HOSTNAME" = "" ]; then
echo "ERROR: Please specify a hostname"
exit
fi
# Set the path the key should be written to
KEY_PATH_DIR="$KEY_PATH_ROOT/$KEY_TYPE/$KEY_HOSTNAME" #Must be an absolute path!
# Set the path of the keys
KEY_PATH_PRIV="$KEY_PATH_DIR/$KEY_USER"
KEY_PATH_PUB="$KEY_PATH_DIR/$KEY_USER.pub"
fi
# Set the library path
UTIL_PATH="$PATH_ROOT/utils/$ACTION.sh"
# Check the library exists
if [ ! -f "$UTIL_PATH" ]; then
echo "ERROR: Unknown action '$ACTION'"
exit
fi
# Include the library for handling this action
. "$UTIL_PATH"