-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_client.sh
executable file
·209 lines (188 loc) · 6.96 KB
/
generate_client.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
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
#!/bin/bash
#This script generates openvpn client keys using the easy-rsa build-key tool and creates a single distributable .ovpn file.
#########################
# The command line help #
#########################
display_help() {
echo "Usage: $0 -c <client name> -b <base config>"
echo "Must be run as root."
echo
echo "Required"
echo " -c, --client-name name of the client. This will be used to generate key files and output .ovpn file. Takes the client name as an argument"
echo " -b, --base-config base client configuration file to be used to compile an output .ovpn for the client. Takes the base config file as an argument."
echo " -o, --output-directory All keys are stored in /etc/openvpn/easy-rsa/keys. If this parameter is provided the. Takes the desired output directory as an argument."
echo " output .ovpn will be copied to the directory and the ownership of the file will be changed "
echo " to whoever called sudo."
echo "Optional"
echo " -s, --silent-mode If this flag is provided then the key generation will run without user interaction. All inputs"
echo " will be set to their default. Takes no arguments."
echo " -x, --skip-keygen Only the client ovpn file will be built with pre-existing keys if -O is not enabled. The key generation will be skipped. Takes no arguments."
echo " NOTE: This will fail if there are no keys already generated."
echo " -O, --skip-output-file-generation Only the client keys will be generated provided -x is not enabled. The .ovpn output file generation will be skipped. Takes no arguments."
echo " -f, --force Overwrite existing client output ovpn file."
echo
echo "Example"
echo " sudo sh generate_client.sh -c client1 -o . -b base.conf"
echo " sudo sh generate_client.sh -c client1 -o . -b base.conf -s"
echo
echo "Note: This will fail if there is already a client with the provided name in the database"
echo "All generated client keys as well as ovpn files are stored in: $KEY_DIRECTORY"
exit 1
}
################################
# Default Parameters #
# #
################################
SILENT_MODE=false
SKIP_KEY_GENERATION=false
SKIP_OUTPUT_FILE_GENERATION=false
OVERWRITE_OUTPUT_FILE=false
KEY_DIRECTORY="/etc/openvpn/easy-rsa/keys"
OPENVPN_DIRECTORY="/etc/openvpn"
################################
# Check if parameters options #
# are given on the command line#
################################
if [ $# -eq 0 ]; then
display_help
fi
while :
do
case "$1" in
-h | --help)
display_help
exit 0
;;
-b | --base-config)
BASE_CONFIG="$2"
shift 2
;;
-c | --client-name)
CLIENT_NAME="$2"
shift 2
;;
-o | --output-directory)
OUTPUT_DIRECTORY="$2"
shift 2
;;
-s | --silent-mode)
SILENT_MODE=true
shift 1
;;
-x | --skip-key-generation)
SKIP_KEY_GENERATION=true
shift 1
;;
-O | --skip-output-file-generation)
SKIP_OUTPUT_FILE_GENERATION=true
shift 1
;;
-f | --force)
OVERWRITE_OUTPUT_FILE=true
shift 1
;;
--) # End of all options
shift
break
;;
-*)
echo "Error: Unknown option: $1" >&2
## or call function display_help
exit 1
;;
*) # No more options
break
;;
esac
done
################################
# Root Check #
# #
################################
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
################################
# Validate and sanitize input #
# #
################################
if [ -z "${CLIENT_NAME}" ]; then
>&2 echo "Invalid Arguments: A client name must be provided with -c"
exit 128;
fi
#CLIENT_NAME="${CLIENT_NAME// /_}"#Remove white space
if [ -z "${BASE_CONFIG}" ]; then
>&2 echo "Invalid Arguments: A base configuration file must be provided with -b"
exit 128;
fi
echo
echo "Base Configuration: $BASE_CONFIG"
cat $BASE_CONFIG
echo
################################
#Generate client Keys #
# #
################################
if [ "$SKIP_KEY_GENERATION" = true ] ; then
echo "Skipping key generation..."
else
echo "Generating Keys..."
if [ -f "$KEY_DIRECTORY/$CLIENT_NAME.key" ];
then
>&2 echo "Error: Key for client: $CLIENT_NAME already exists. Choose a different client name."
exit 126;
fi
CWD=$(pwd)
cd /etc/openvpn/easy-rsa/
. /etc/openvpn/easy-rsa/vars
if [ "$SILENT_MODE" = true ] ; then
./pkitool $CLIENT_NAME
else
exec /etc/openvpn/easy-rsa/build-key $CLIENT_NAME
fi
cd $CWD
fi
echo
################################
# Build Client .ovpn file #
# #
################################
if [ "$SKIP_OUTPUT_FILE_GENERATION" = true ] ; then
echo "Skipping output file generation..."
else
OUTPUT_FILE=$CLIENT_NAME.ovpn
if [ -f "$KEY_DIRECTORY/$OUTPUT_FILE" ] && [ "$OVERWRITE_OUTPUT_FILE" = false ];
then
>&2 echo "Error: Output File: $KEY_DIRECTORY/$OUTPUT_FILE already exists. Rerun with -f flag to overwrite."
exit 126;
fi
echo "Compiling output .ovpn file: $OUTPUT_FILE"
cat $BASE_CONFIG > $KEY_DIRECTORY/$OUTPUT_FILE
echo '<ca>' >> $KEY_DIRECTORY/$OUTPUT_FILE
cat $KEY_DIRECTORY/ca.crt >> $KEY_DIRECTORY/$OUTPUT_FILE
echo '</ca>' >> $KEY_DIRECTORY/$OUTPUT_FILE
echo '<cert>' >> $KEY_DIRECTORY/$OUTPUT_FILE
cat $KEY_DIRECTORY/$CLIENT_NAME.crt >> $KEY_DIRECTORY/$OUTPUT_FILE
echo '</cert>' >> $KEY_DIRECTORY/$OUTPUT_FILE
echo '<key>' >> $KEY_DIRECTORY/$OUTPUT_FILE
cat $KEY_DIRECTORY/$CLIENT_NAME.key >> $KEY_DIRECTORY/$OUTPUT_FILE
echo '</key>' >> $KEY_DIRECTORY/$OUTPUT_FILE
echo '<tls-auth>' >> $KEY_DIRECTORY/$OUTPUT_FILE
cat $OPENVPN_DIRECTORY/ta.key >> $KEY_DIRECTORY/$OUTPUT_FILE
echo '</tls-auth>' >> $KEY_DIRECTORY/$OUTPUT_FILE
echo 'key-direction 1' >> $KEY_DIRECTORY/$OUTPUT_FILE
echo '' >> $KEY_DIRECTORY/$OUTPUT_FILE
echo "Client ovpn created: $KEY_DIRECTORY/$OUTPUT_FILE"
if [ -z ${OUTPUT_DIRECTORY+x} ]; then
echo "No output directory provided skipping copy.";
else
echo "Output Directory set to: $OUTPUT_DIRECTORY";
echo "Copying $KEY_DIRECTORY/$OUTPUT_FILE to $OUTPUT_DIRECTORY/$OUTPUT_FILE"
cp $KEY_DIRECTORY/$OUTPUT_FILE $OUTPUT_DIRECTORY
#Set owner to whoever called this script
echo "Setting ownership of $OUTPUT_DIRECTORY/$OUTPUT_FILE to $SUDO_USER:"
chown $SUDO_USER $OUTPUT_DIRECTORY/$OUTPUT_FILE
fi
fi
echo