Skip to content

Commit e058f5e

Browse files
committed
Add command line options
- Add support for command-line options for managing OpenVPN clients and removing OpenVPN.
1 parent 70ea744 commit e058f5e

File tree

1 file changed

+183
-29
lines changed

1 file changed

+183
-29
lines changed

openvpn-install.sh

Lines changed: 183 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,48 @@ TUN needs to be enabled before running this installer."
101101
fi
102102
}
103103

104+
set_client_name() {
105+
# Allow a limited set of characters to avoid conflicts
106+
client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<< "$unsanitized_client")
107+
}
108+
104109
parse_args() {
105110
while [ "$#" -gt 0 ]; do
106111
case $1 in
107112
--auto)
108113
auto=1
109114
shift
110115
;;
116+
--addclient)
117+
add_client=1
118+
unsanitized_client="$2"
119+
shift
120+
shift
121+
;;
122+
--exportclient)
123+
export_client=1
124+
unsanitized_client="$2"
125+
shift
126+
shift
127+
;;
128+
--listclients)
129+
list_clients=1
130+
shift
131+
;;
132+
--revokeclient)
133+
revoke_client=1
134+
unsanitized_client="$2"
135+
shift
136+
shift
137+
;;
138+
--uninstall)
139+
remove_ovpn=1
140+
shift
141+
;;
142+
-y|--yes)
143+
assume_yes=1
144+
shift
145+
;;
111146
-h|--help)
112147
show_usage
113148
;;
@@ -118,6 +153,43 @@ parse_args() {
118153
done
119154
}
120155

156+
check_args() {
157+
if [ "$auto" = 1 ] && [ -e "$OVPN_CONF" ]; then
158+
echo "Error: Invalid parameter '--auto'. OpenVPN is already set up on this server." >&2
159+
echo " To manage OpenVPN clients, re-run this script without '--auto'." >&2
160+
exit 1
161+
fi
162+
if [ "$((add_client + export_client + list_clients + revoke_client))" -gt 1 ]; then
163+
show_usage "Invalid parameters. Specify only one of '--addclient', '--exportclient', '--listclients' or '--revokeclient'."
164+
fi
165+
if [ "$remove_ovpn" = 1 ]; then
166+
if [ "$((add_client + export_client + list_clients + revoke_client + auto))" -gt 0 ]; then
167+
show_usage "Invalid parameters. '--uninstall' cannot be specified with other parameters."
168+
fi
169+
fi
170+
if [ ! -e "$OVPN_CONF" ]; then
171+
[ "$add_client" = 1 ] && exiterr "You must first set up OpenVPN before adding a client."
172+
[ "$export_client" = 1 ] && exiterr "You must first set up OpenVPN before exporting a client."
173+
[ "$list_clients" = 1 ] && exiterr "You must first set up OpenVPN before listing clients."
174+
[ "$revoke_client" = 1 ] && exiterr "You must first set up OpenVPN before revoking a client."
175+
[ "$remove_ovpn" = 1 ] && exiterr "Cannot remove OpenVPN because it has not been set up on this server."
176+
fi
177+
if [ "$add_client" = 1 ]; then
178+
set_client_name
179+
if [ -z "$client" ]; then
180+
exiterr "Invalid client name. Use one word only, no special characters except '-' and '_'."
181+
elif [ -e /etc/openvpn/server/easy-rsa/pki/issued/"$client".crt ]; then
182+
exiterr "$client: invalid name. Client already exists."
183+
fi
184+
fi
185+
if [ "$export_client" = 1 ] || [ "$revoke_client" = 1 ]; then
186+
set_client_name
187+
if [ -z "$client" ] || [ ! -e /etc/openvpn/server/easy-rsa/pki/issued/"$client".crt ]; then
188+
exiterr "Invalid client name, or client does not exist."
189+
fi
190+
fi
191+
}
192+
121193
check_nftables() {
122194
if [ "$os" = "centos" ]; then
123195
if grep -qs "hwdsl2 VPN script" /etc/sysconfig/nftables.conf \
@@ -183,6 +255,7 @@ cat <<'EOF'
183255
184256
Welcome to this OpenVPN server installer!
185257
GitHub: https://github.com/hwdsl2/openvpn-install
258+
186259
EOF
187260
}
188261

@@ -205,8 +278,14 @@ cat 1>&2 <<EOF
205278
Usage: bash $0 [options]
206279
207280
Options:
208-
--auto auto install OpenVPN using default options
209-
-h, --help show this help message and exit
281+
--auto auto install OpenVPN using default options
282+
--addclient [client name] add a new client
283+
--exportclient [client name] export configuration for an existing client
284+
--listclients list the names of existing clients
285+
--revokeclient [client name] revoke an existing client
286+
--uninstall remove OpenVPN and delete all configuration
287+
-y, --yes assume "yes" as answer to prompts when revoking a client or removing OpenVPN
288+
-h, --help show this help message and exit
210289
211290
To customize install options, run this script without arguments.
212291
EOF
@@ -216,7 +295,6 @@ EOF
216295
show_welcome() {
217296
if [ "$auto" = 0 ]; then
218297
show_header2
219-
echo
220298
echo 'I need to ask you a few questions before starting setup.'
221299
echo 'You can use the default options and just press enter if you are OK with them.'
222300
else
@@ -430,11 +508,6 @@ select_dns() {
430508
fi
431509
}
432510

433-
set_client_name() {
434-
# Allow a limited set of characters to avoid conflicts
435-
client=$(sed 's/[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-]/_/g' <<< "$unsanitized_client")
436-
}
437-
438511
enter_first_client_name() {
439512
if [ "$auto" = 0 ]; then
440513
echo
@@ -975,7 +1048,11 @@ enter_client_name() {
9751048
[ -z "$unsanitized_client" ] && abort_and_exit
9761049
set_client_name
9771050
while [[ -z "$client" || -e /etc/openvpn/server/easy-rsa/pki/issued/"$client".crt ]]; do
978-
echo "$client: invalid name."
1051+
if [ -z "$client" ]; then
1052+
echo "Invalid client name. Use one word only, no special characters except '-' and '_'."
1053+
else
1054+
echo "$client: invalid name. Client already exists."
1055+
fi
9791056
read -rp "Name: " unsanitized_client
9801057
[ -z "$unsanitized_client" ] && abort_and_exit
9811058
set_client_name
@@ -1005,7 +1082,7 @@ check_clients() {
10051082
if [[ "$num_of_clients" = 0 ]]; then
10061083
echo
10071084
echo "There are no existing clients!"
1008-
exit
1085+
exit 1
10091086
fi
10101087
}
10111088

@@ -1032,12 +1109,16 @@ select_client_to() {
10321109
}
10331110

10341111
confirm_revoke_client() {
1035-
echo
1036-
read -rp "Confirm $client revocation? [y/N]: " revoke
1037-
until [[ "$revoke" =~ ^[yYnN]*$ ]]; do
1038-
echo "$revoke: invalid selection."
1112+
if [ "$assume_yes" != 1 ]; then
1113+
echo
10391114
read -rp "Confirm $client revocation? [y/N]: " revoke
1040-
done
1115+
until [[ "$revoke" =~ ^[yYnN]*$ ]]; do
1116+
echo "$revoke: invalid selection."
1117+
read -rp "Confirm $client revocation? [y/N]: " revoke
1118+
done
1119+
else
1120+
revoke=y
1121+
fi
10411122
}
10421123

10431124
print_revoke_client() {
@@ -1054,7 +1135,7 @@ remove_client_conf() {
10541135
fi
10551136
}
10561137

1057-
revoke_client() {
1138+
revoke_client_ovpn() {
10581139
cd /etc/openvpn/server/easy-rsa/ || exit 1
10591140
(
10601141
set -x
@@ -1079,12 +1160,16 @@ print_client_revocation_aborted() {
10791160
}
10801161

10811162
confirm_remove_ovpn() {
1082-
echo
1083-
read -rp "Confirm OpenVPN removal? [y/N]: " remove
1084-
until [[ "$remove" =~ ^[yYnN]*$ ]]; do
1085-
echo "$remove: invalid selection."
1163+
if [ "$assume_yes" != 1 ]; then
1164+
echo
10861165
read -rp "Confirm OpenVPN removal? [y/N]: " remove
1087-
done
1166+
until [[ "$remove" =~ ^[yYnN]*$ ]]; do
1167+
echo "$remove: invalid selection."
1168+
read -rp "Confirm OpenVPN removal? [y/N]: " remove
1169+
done
1170+
else
1171+
remove=y
1172+
fi
10881173
}
10891174

10901175
print_remove_ovpn() {
@@ -1141,9 +1226,76 @@ check_tun
11411226
OVPN_CONF="/etc/openvpn/server/server.conf"
11421227

11431228
auto=0
1229+
assume_yes=0
1230+
add_client=0
1231+
export_client=0
1232+
list_clients=0
1233+
revoke_client=0
1234+
remove_ovpn=0
1235+
1236+
parse_args "$@"
1237+
check_args
1238+
1239+
if [ "$add_client" = 1 ]; then
1240+
show_header
1241+
echo
1242+
build_client_config
1243+
new_client
1244+
print_client_action added
1245+
exit 0
1246+
fi
1247+
1248+
if [ "$export_client" = 1 ]; then
1249+
show_header
1250+
new_client
1251+
print_client_action exported
1252+
exit 0
1253+
fi
1254+
1255+
if [ "$list_clients" = 1 ]; then
1256+
show_header
1257+
print_check_clients
1258+
check_clients
1259+
echo
1260+
show_clients
1261+
print_client_total
1262+
exit 0
1263+
fi
1264+
1265+
if [ "$revoke_client" = 1 ]; then
1266+
show_header
1267+
confirm_revoke_client
1268+
if [[ "$revoke" =~ ^[yY]$ ]]; then
1269+
print_revoke_client
1270+
revoke_client_ovpn
1271+
print_client_revoked
1272+
exit 0
1273+
else
1274+
print_client_revocation_aborted
1275+
exit 1
1276+
fi
1277+
fi
1278+
1279+
if [ "$remove_ovpn" = 1 ]; then
1280+
show_header
1281+
confirm_remove_ovpn
1282+
if [[ "$remove" =~ ^[yY]$ ]]; then
1283+
print_remove_ovpn
1284+
remove_firewall_rules
1285+
disable_ovpn_service
1286+
remove_sysctl_rules
1287+
remove_rclocal_rules
1288+
remove_pkgs
1289+
print_ovpn_removed
1290+
exit 0
1291+
else
1292+
print_ovpn_removal_aborted
1293+
exit 1
1294+
fi
1295+
fi
1296+
11441297
if [[ ! -e "$OVPN_CONF" ]]; then
11451298
check_nftables
1146-
parse_args "$@"
11471299
install_wget
11481300
install_iproute
11491301
show_welcome
@@ -1188,35 +1340,36 @@ else
11881340
build_client_config
11891341
new_client
11901342
print_client_action added
1191-
exit
1343+
exit 0
11921344
;;
11931345
2)
11941346
check_clients
11951347
select_client_to export
11961348
new_client
11971349
print_client_action exported
1198-
exit
1350+
exit 0
11991351
;;
12001352
3)
12011353
print_check_clients
12021354
check_clients
12031355
echo
12041356
show_clients
12051357
print_client_total
1206-
exit
1358+
exit 0
12071359
;;
12081360
4)
12091361
check_clients
12101362
select_client_to revoke
12111363
confirm_revoke_client
12121364
if [[ "$revoke" =~ ^[yY]$ ]]; then
12131365
print_revoke_client
1214-
revoke_client
1366+
revoke_client_ovpn
12151367
print_client_revoked
1368+
exit 0
12161369
else
12171370
print_client_revocation_aborted
1371+
exit 1
12181372
fi
1219-
exit
12201373
;;
12211374
5)
12221375
confirm_remove_ovpn
@@ -1228,13 +1381,14 @@ else
12281381
remove_rclocal_rules
12291382
remove_pkgs
12301383
print_ovpn_removed
1384+
exit 0
12311385
else
12321386
print_ovpn_removal_aborted
1387+
exit 1
12331388
fi
1234-
exit
12351389
;;
12361390
6)
1237-
exit
1391+
exit 0
12381392
;;
12391393
esac
12401394
fi

0 commit comments

Comments
 (0)