-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopenvpn-server-setup
executable file
·2059 lines (1791 loc) · 77 KB
/
openvpn-server-setup
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
##
# openvpn-server-setup designed to help OpenVPN server setup on CentOS 6, CentOS 7,
# Ubuntu 16.04 and experimental support of Ubuntu 18.04.
# It can be used for stock openvpn server configuration.
# For customizations you still need manually tune configuration.
#
# You can execute this script on remote host via ssh:
# ssh user@host 'sudo bash -s' -- <'/local/path/to/openvpn-server-setup'
# Or copy to remote host and run:
# sudo bash /path/to/openvpn-server-setup [-i IFNAME] [--mssfix [VAL]] [--fragment [VAL]]
#
# By default this script creates configurations with default `mssfix` and
# `fragment` options which is fine in most cases. BTW you can set the value
# you want by commandline option, particularly `mssfix 0` which can lead to
# high packets fragmentation which by-turn can lead to bandwidth and latency
# issues, but it's much more resistant to VPN-tracking by external web-sites
# and do not requre manual TAP adapter MTU fixing on Windows (which isrequired
# by tun-mtu or link-mtu).
#
# In all cases you can change this behavior after creation by modifying server
# and client configs.
#
# openvpn-manage configuration tool embedded to this script and will be
# available after setup. It helps you to manage openvpn users.
##
## Defaults
SERVER_LOCAL_IF='eth0'
mssfix=';mssfix 1450'
fragment=';fragment 1450'
## The content of openvpn-manage script
read -rd '' OPENVPN_MANAGE <<'EOF_OPENVPN_MANAGE'
#!/bin/bash
SCRIPT_VERSION='0.7.4'
read -rd '' HELP <<'EOF'
Description:
This tool created to support user management for specific openvpn server setup
by openvpn-server-setup tool.
Some script terms:
- host: the name of specific host (specific host can have several clients/certs).
- client: specific vpn-client/cert for the host.
Usage:
openvpn-manage ACTION { PARAMS } [ OPTIONS ]
ACTION := { usage | help | create | append | enable | disable | revoke | list | show }
OPTIONS := { -f CONF | -t CLIENT_TYPE | -s IP/CIDR | -c COUNT | -o CLIENT_OS | -e | -d | -v }
CLIENT_TYPE := { user | router }
CLIENT_OS := { windows | nix }
openvpn-manage { create | append } HOST_NAME [ OPTIONS ]
openvpn-manage { enable | disable | revoke } { host HOST_NAME | client CLIENT_NAME }
openvpn-manage list { host[s] | client[s] } [ HOST_NAME ] { -t CLIENT_TYPE | -s CLIENT_SUBNET_BEHIND | -e | -d }
openvpn-manage show { host HOST_NAME | client CLIENT_NAME }
Actions:
create host HOST_NAME
append client HOST_NAME
Used for create/append clients to HOST_NAME.
Difference is:
validation - you can't create host if HOST_NAME already exist and you can't
append clients to non-existent host.
subnet applying for type router - for "append" if -s not specified additional clients of
the host will have the same subnet as first available iroute option
in first available client config.
enable, disable, revoke
You can enable/disable/revoke specific client (client CLIENT_NAME) or all clients
(host HOST_NAME) of the host.
Enable and disable just delete/add 'disable' line to client config, so client can't
connect. Host/client can be enabled/disabled any time.
revoke adds client[s] cert[s] to revocation list, so it could be treated as
persistent client deactivation.
Note: revoked cert still can be unrevoked manually, but it's not supported
by openvpn-manage.
list
List hosts or clients depends on filter options.
For HOST_NAME grep standard regexp's supported.
Examples:
List all hosts:
list hosts
List all routers clients:
list clients -t router
List enabled clients of specific host with specified internal subnet:
list clients HOST_NAME -e -s 10.10.0.8/29
Options:
-f CONF
Configuration file of openvpn-manage
Default: /etc/openvpn/server/scripts/openvpn-manage.conf
-t CLIENT_TYPE
Could be "user" (default) or "router". Difference is client config options.
User config contain push "route ..." to the routers internal subnets.
Router config contain iroute to internal subnet behind it.
-s CLIENT_SUBNET_BEHIND
Format: IP/CIDR. Subnet behind the router. Valid only if CLIENT_TYPE is "router".
If no subnet specified subnet will be incremented from the last saved subnet.
Subnet will be saved to LAST_IROUTE_SUBNET_FILE. If no file exist
INITIAL_IROUTE_SUBNET will be used and then saved to LAST_IROUTE_SUBNET_FILE.
Subnet will be saved only if -s option omitted, so manually specified subnet
will not be saved in LAST_IROUTE_SUBNET_FILE. It's allow manually specify
subnet without interrupt existing subnet chain.
-c COUNT
Default is 1. Allow to create COUNT clients/certs for the host with single
command. Additional host clients/certs could be added via action "append"
next time.
-e, -d
List enabled/disabled only.
-z {0 | 1}
If 1 create zip packs (default: 0).
-r REMOTE_IP[:REMOTE_PORT]
Overwrite config REMOTE_IP and optionally REMOTE_PORT variables
-v {default | debug | none | conf}
Set stdout verbosity:
debug: verbose output
default: normal output
none: completely quite, only errrors can be print to stderr
conf: print only single generated config content for action "create"
EOF
##
# Exit with optional message to stderr
##
quit() {
[[ $2 ]] && echo "$2" >&2
exit "${1:-0}"
}
##
# Convert decimal number to IPv4 address
##
dec2ip() {
local _var _res _e _octet _dec
if [[ $1 == '-v' ]]; then _var=$2; shift 2; fi
_dec=$1
for _e in {3..0}; do
(( _octet = _dec/256**_e ))
(( _dec -= _octet*256**_e ))
_res+=.${_octet}
done
_res=${_res:1}
if [[ ${_var} ]]; then printf -v "${_var}" '%s' "${_res}"; else echo "${_res}"; fi
}
##
# Convert IPv4 address to decimal number
##
ip2dec() {
local _var _res _a _b _c _d
if [[ $1 == '-v' ]]; then _var=$2; shift 2; fi
IFS=. read -r _a _b _c _d <<<"$1"
(( _res = _a*256**3 + _b*256**2 + _c*256 + _d ))
if [[ ${_var} ]]; then printf -v "${_var}" '%s' "${_res}"; else echo "${_res}"; fi
}
##
# Convert IPv4 MASK to CIDR
# Assumes that MASK have no gaps.
##
mask2cidr() {
local _var _res _x
if [[ $1 == '-v' ]]; then _var=$2; shift 2; fi
_x=${1##*255.}
set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1}-${#_x})*2 )) "${_x%%.*}"
_x=${1%%$3*}
(( _res = $2 + ${#_x}/4 ))
if [[ ${_var} ]]; then printf -v "${_var}" '%s' "${_res}"; else echo "${_res}"; fi
}
##
# Convert CIDR to IPv4 MASK
##
cidr2mask() {
local _var _res
if [[ $1 == '-v' ]]; then _var=$2; shift 2; fi
[[ -z $1 ]] && set 0
set -- $(( 5-($1/8) )) 255 255 255 255 $(( (255<<(8 - ($1%8)))&255 )) 0 0 0
if (($1 > 1)); then shift "$1"; else shift; fi
_res=${1:-0}.${2:-0}.${3:-0}.${4:-0}
if [[ ${_var} ]]; then printf -v "${_var}" '%s' "${_res}"; else echo "${_res}"; fi
}
##
# Return next subnet with the same size
# Subnet format is IPv4/CIDR
##
increment_subnet() {
local _var _res _ip _cidr _size _ip_dec _ip_next
if [[ $1 == '-v' ]]; then _var=$2; shift 2; fi
_ip=${1%/*}
_cidr=${1#*/}
(( _size = 2**(32-_cidr) )) # all IPs count inside subnet e.g. /29 size is 8
ip2dec -v _ip_dec "${_ip}"
dec2ip -v _ip_next $((_size+_ip_dec))
_res=${_ip_next}/${_cidr}
if [[ ${_var} ]]; then printf -v "${_var}" '%s' "${_res}"; else echo "${_res}"; fi
}
##
# Validate IPv4 address
# Note: this is invalid address: 192.168.000.001
##
validate_ip4() {
local _re
_re='^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$'
[[ "$1" =~ ${_re} ]] && return 0 || return 1
}
##
# Validate IPv4/CIDR
##
validate_ip4cidr() {
local _re
_re='^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\/([0-9]|[1-2][0-9]|3[0-2])$'
[[ "$1" =~ ${_re} ]] && return 0 || return 1
}
##
# Usage: validate_host_name host_name [regex]
##
validate_host_name() {
local _re
if [[ $2 ]]; then
_re=$2
elif [[ ${HOST_NAME_ALLOWED_REGEX} ]]; then
_re=${HOST_NAME_ALLOWED_REGEX}
else
_re='^[A-Za-z0-9_-]+$' # Default regex
fi
[[ "$1" =~ ${_re} ]] || quit 1 "Host name restricted to match the following regex: ${_re}"
return 0
}
##
# Validate that client name starts with 5 digits and '-' then.
# Values '00000-' and '00001-' treated as invalid.
# Include host name validation.
##
validate_client_name() {
local _re _N
_re='^[0-9]+-$'
_N=${1::6}
[[ "${_N}" =~ ${_re} && "${_N}" != '00000-' && "${_N}" != '00001-' ]] || quit 1 "First 6 symbols of client name must match the following regex: ${_re}"
validate_host_name "${1:6}" || return 1
return 0
}
##
# Attempt to kill client connection on both tcp and udp servers if management interface is enabled
##
kill_existing_client_connection() {
local _cn
_cn=$1
if [[ -n "${TCP_MANAGEMENT_SOCKET}" && -S "${TCP_MANAGEMENT_SOCKET}" ]]; then
# Kill existing connection if any
sudo -u "${TCP_MANAGEMENT_USER}" -g "${TCP_MANAGEMENT_GROUP}" nc -U "${TCP_MANAGEMENT_SOCKET}" <<<"kill ${_cn}" >/dev/null
# TODO: implement for TCP_MANAGEMENT_IP:TCP_MANAGEMENT_PORT
fi
if [[ -n "${UDP_MANAGEMENT_SOCKET}" && -S "${UDP_MANAGEMENT_SOCKET}" ]]; then
# Kill existing connection if any
sudo -u "${UDP_MANAGEMENT_USER}" -g "${UDP_MANAGEMENT_GROUP}" nc -U "${UDP_MANAGEMENT_SOCKET}" <<<"kill ${_cn}" >/dev/null
# TODO: implement for UDP_MANAGEMENT_IP:UDP_MANAGEMENT_PORT
fi
return 0 # Always return 0
}
revoke_cert() {
local _cn
_cn=$1
[[ -f "${EASYRSA}/pki/issued/${_cn}.crt" ]] || quit 1 "No such cert: ${_cn}"
if ! grep -qE "^R[[:space:]]*[[:digit:]]*Z.*/CN=${_cn}(/.*)?$" "${EASYRSA}/pki/index.txt"; then
./easyrsa --batch revoke "${_cn}" &>"${DESCRIPTOR}"
if (($?)); then
quit 1 "${_cn}: failed to revoke cert"
else
# TODO: after killing connection crl-verify file is still not updated so client may have enough time to reconnect (?)
kill_existing_client_connection "${_cn}"
echo "${_cn}: cert revoked"
fi
else
echo "${_cn}: cert already revoked!"
fi
}
disable_client() {
local _cn
_cn=$1
if ! grep -q '^disable$' "${CLIENT_CONFIG_DIR}/${_cn}"; then
echo disable >>"${CLIENT_CONFIG_DIR}/${_cn}"
kill_existing_client_connection "${_cn}"
echo "${_cn}: disabled"
else
echo "${_cn}: was already disabled!"
fi
}
enable_client() {
local _cn
_cn=$1
if grep -q '^disable$' "${CLIENT_CONFIG_DIR}/${_cn}"; then
sed -i '/^disable$/d' "${CLIENT_CONFIG_DIR}/${_cn}"
echo "${_cn}: enabled"
else
echo "${_cn}: was already enabled!"
fi
}
populate_subnet_var() {
local _cn CLIENT_SUBNET_BEHIND_IP CLIENT_SUBNET_BEHIND_CIDR ROUTERS_LIST FIRST_ROUTER
_cn=$1
ROUTERS_LIST=$(exec -c grep -l '^iroute ' "${CLIENT_CONFIG_DIR}/"*"-${_cn}")
if (($? == 0)); then # Other routers exists for this host
# Get subnet from first available router config of this host!
read -r FIRST_ROUTER <<<"${ROUTERS_LIST}"
CLIENT_SUBNET_BEHIND=$(exec -c grep -m1 '^iroute ' "${FIRST_ROUTER}")
CLIENT_SUBNET_BEHIND=${CLIENT_SUBNET_BEHIND#iroute }
CLIENT_SUBNET_BEHIND_IP=${CLIENT_SUBNET_BEHIND%% *}
mask2cidr -v CLIENT_SUBNET_BEHIND_CIDR ${CLIENT_SUBNET_BEHIND#* }
CLIENT_SUBNET_BEHIND=${CLIENT_SUBNET_BEHIND_IP}/${CLIENT_SUBNET_BEHIND_CIDR}
else # It's only router for this host: autoincrement subnet
# Increment last saved subnet
if [[ -f "${LAST_IROUTE_SUBNET_FILE}" ]]; then
increment_subnet -v CLIENT_SUBNET_BEHIND "$(< "${LAST_IROUTE_SUBNET_FILE}")"
else
CLIENT_SUBNET_BEHIND=${INITIAL_IROUTE_SUBNET}
fi
[[ -f "${LAST_IROUTE_SUBNET_FILE}" ]] && mv "${LAST_IROUTE_SUBNET_FILE}"{,.old}
echo "${CLIENT_SUBNET_BEHIND}" >"${LAST_IROUTE_SUBNET_FILE}"
echo "Note: LAST_IROUTE_SUBNET was incremented to ${CLIENT_SUBNET_BEHIND}" >&2
fi
}
update_client() {
# Update client type and/or subnet
local _cn OLD_CLIENT_TYPE OLD_IROUTE CLIENT_SUBNET_BEHIND_IP CLIENT_SUBNET_BEHIND_MASK OLD_CLIENT_SUBNET_BEHIND OLD_CLIENT_SUBNET_BEHIND_IP OLD_CLIENT_SUBNET_BEHIND_CIDR
_cn=$1
if grep -q '^iroute ' "${CLIENT_CONFIG_DIR}/${_cn}"; then
OLD_CLIENT_TYPE=router
OLD_IROUTE=$(exec -c grep -m1 '^iroute ' "${CLIENT_CONFIG_DIR}/${_cn}")
OLD_CLIENT_SUBNET_BEHIND=${OLD_IROUTE#iroute }
OLD_CLIENT_SUBNET_BEHIND_IP=${OLD_CLIENT_SUBNET_BEHIND%% *}
mask2cidr -v OLD_CLIENT_SUBNET_BEHIND_CIDR ${OLD_CLIENT_SUBNET_BEHIND#* }
OLD_CLIENT_SUBNET_BEHIND=${OLD_CLIENT_SUBNET_BEHIND_IP}/${OLD_CLIENT_SUBNET_BEHIND_CIDR}
else
OLD_CLIENT_TYPE=user
fi
case "${CLIENT_TYPE}" in
user)
if [[ "${CLIENT_TYPE}" != "${OLD_CLIENT_TYPE}" ]]; then # router -> user
sed -i -e '/^iroute /d' -e "1 a config ${USERS_CONF_IN_CHROOT}" "${CLIENT_CONFIG_DIR}/${_cn}"
echo "${_cn}: type changed to ${CLIENT_TYPE}"
else # user -> user: nothing to change
echo "${_cn}: type is already ${CLIENT_TYPE}!"
fi
;;
router)
if [[ -z "${CLIENT_SUBNET_BEHIND}" ]]; then
populate_subnet_var "${_cn:6}"
fi
CLIENT_SUBNET_BEHIND_IP=${CLIENT_SUBNET_BEHIND%%/*}
cidr2mask -v CLIENT_SUBNET_BEHIND_MASK ${CLIENT_SUBNET_BEHIND#*/}
if [[ "${CLIENT_TYPE}" != "${OLD_CLIENT_TYPE}" ]]; then # user -> router
sed -i -e '/^config /d' -e "1 a iroute ${CLIENT_SUBNET_BEHIND_IP} ${CLIENT_SUBNET_BEHIND_MASK}" "${CLIENT_CONFIG_DIR}/${_cn}"
echo "${_cn}: internal subnet changed to ${CLIENT_SUBNET_BEHIND}"
else # router -> router: change router subnet only
if [[ "${OLD_CLIENT_SUBNET_BEHIND}" != "${CLIENT_SUBNET_BEHIND}" ]]; then
sed -i "s/${OLD_IROUTE}/iroute ${CLIENT_SUBNET_BEHIND_IP} ${CLIENT_SUBNET_BEHIND_MASK}/" "${CLIENT_CONFIG_DIR}/${_cn}"
echo "${_cn}: internal subnet changed to ${CLIENT_SUBNET_BEHIND}"
else
echo "${_cn}: already has internal subnet ${CLIENT_SUBNET_BEHIND}!"
fi
fi
;;
esac
# Enable/disable if -e|-d specified
[[ "${ENABLED}" == yes ]] && enable_client "${_cn}"
[[ "${ENABLED}" == no ]] && disable_client "${_cn}"
}
show_client() {
local CLIENT_NAME ENABLED VALID_CERT CLIENT_TYPE IROUTES IROUTE IP CIDR USERS_CONF_LINE line n N SUBNET CLIENT_CONFIG CCC
SUBNET=''
CLIENT_NAME=$1
CLIENT_CONFIG="${CLIENT_CONFIG_DIR}/${CLIENT_NAME}"
CCC=$(< "${CLIENT_CONFIG}")
if grep -q '^disable$' <<<"${CCC}"; then
ENABLED=no
else
ENABLED=yes
fi
IROUTES=$(exec -c grep '^iroute ' <<<"${CCC}")
if (($? == 0)); then
CLIENT_TYPE=router
while read IROUTE; do
IROUTE=${IROUTE#iroute }
IP=${IROUTE%% *}
mask2cidr -v CIDR ${IROUTE#* }
[[ ${SUBNET} ]] && SUBNET+=','
SUBNET+="${IP}/${CIDR}"
done <<<"${IROUTES}"
fi
USERS_CONF_LINE=$(exec -c grep "^config ${USERS_CONF_IN_CHROOT}$" <<<"${CCC}")
if (($? == 0)); then
CLIENT_TYPE=user
fi
if [[ -n "${IROUTES}" && -n "${USERS_CONF_LINE}" ]]; then
CLIENT_TYPE=mixed
elif [[ -z "${IROUTES}" && -z "${USERS_CONF_LINE}" ]]; then
CLIENT_TYPE=unspecified
fi
(( N = 10#${CLIENT_NAME::5} ))
n=0
while read line; do
((n++))
if [[ ${n} -eq ${N} ]]; then
if [[ "${line::1}" == 'R' ]]; then
VALID_CERT=no
else
VALID_CERT=yes
fi
break
fi
done <"${EASYRSA}/pki/index.txt"
echo "${CLIENT_NAME}:
Server side config: ${CLIENT_CONFIG}
Enabled: ${ENABLED}
Valid cert: ${VALID_CERT}
Type: ${CLIENT_TYPE}"
if [[ "${CLIENT_TYPE}" == 'router' || "${CLIENT_TYPE}" == 'mixed' ]]; then
echo " Internal subnets: ${SUBNET}"
fi
if [[ "${CLIENT_TYPE}" == 'user' || "${CLIENT_TYPE}" == 'mixed' ]]; then
echo " Users shared config: ${USERS_CONF}"
fi
}
add_client() {
local HOST_NAME FLNUMBER NUMBER USER CLIENT_CONFIG_NAME DEC_IP srv TMP_USER_DIR OVPN_CONF_CONTENT
HOST_NAME=$1
printf -v FLNUMBER '%05d' $(( 16#$(< "${EASYRSA}/pki/serial") ))
(( NUMBER = 10#${FLNUMBER} ))
USER="${FLNUMBER}-${HOST_NAME}"
case "${CLIENT_OS}" in
windows)
CLIENT_CONFIG_NAME="${USER}.ovpn"
eval "read -rd '' OS_SPECIFIC_OPTIONS <<EOFOSO${BR}# ${NIX_SPECIFIC_OPTIONS//${BR}/${BR}# }${BR}${BR}${WIN_SPECIFIC_OPTIONS}${BR}EOFOSO"
;;
*)
CLIENT_CONFIG_NAME="${USER}.conf"
eval "read -rd '' OS_SPECIFIC_OPTIONS <<EOFOSO${BR}${NIX_SPECIFIC_OPTIONS}${BR}${BR}# ${WIN_SPECIFIC_OPTIONS//${BR}/${BR}# }${BR}EOFOSO"
esac
cd "${EASYRSA}"
./easyrsa --batch --req-cn="${USER}" gen-req "${USER}" nopass &>"${DESCRIPTOR}" || quit 1 "Easyrsa error: $?"
./easyrsa --batch --req-cn="${USER}" sign-req client "${USER}" &>"${DESCRIPTOR}" || quit 1 "Easyrsa error: $?"
# Change CN in Authority Key Identifier DirName for compatibility with Netcomm routers openvpn interface
sed -i -e "s|/CN=server/|/CN=${USER}/|" -e "s|/CN=server$|/CN=${USER}|" "${EASYRSA}/pki/issued/${USER}.crt"
# Calculate client OVPN IP and NETMASK
srv=$(exec -c grep -m1 '^server ' "${SERVER_CONF}")
srv=${srv#server }
srv=${srv% nopool*}
ip2dec -v DEC_IP ${srv%% *}
dec2ip -v CLIENT_OVPN_IP $((DEC_IP+NUMBER))
NETMASK=${srv#* }
## Number for ethN and tunN, first host cert will have N=0, 2nd - N=1 etc.
N=$(ls -1 "${CLIENT_CONFIG_DIR}"/*-"${HOST_NAME}" 2>/dev/null | wc -l)
# Create symlinks if possible, copy otherwise
if ln -s "${EASYRSA}/pki/issued/${USER}.crt" "${CLIENT_KEYS_DIR}" >/dev/null; then
ln -s "${EASYRSA}/pki/private/${USER}.key" "${CLIENT_KEYS_DIR}"
else
cp "${EASYRSA}/pki/issued/${USER}.crt" "${EASYRSA}/pki/private/${USER}.key" "${CLIENT_KEYS_DIR}"
fi
echo "ifconfig-push ${CLIENT_OVPN_IP} ${NETMASK}" >"${CLIENT_CONFIG_DIR}/${USER}"
case "${CLIENT_TYPE}" in
user)
echo "config ${USERS_CONF_IN_CHROOT}" >>"${CLIENT_CONFIG_DIR}/${USER}"
;;
router)
CLIENT_SUBNET_BEHIND_IP=${CLIENT_SUBNET_BEHIND%%/*}
cidr2mask -v CLIENT_SUBNET_BEHIND_MASK ${CLIENT_SUBNET_BEHIND#*/}
echo "iroute ${CLIENT_SUBNET_BEHIND_IP} ${CLIENT_SUBNET_BEHIND_MASK}" >>"${CLIENT_CONFIG_DIR}/${USER}"
ROUTER_CONF_STR='# On config creation time vpn-server used the following internal subnet for this client:'
ROUTER_CONF_STR+="# ${CLIENT_SUBNET_BEHIND}"
ROUTER_CONF_STR+='# It should match real internal subnet if you want allow vpn-users access it.'
ROUTER_CONF_STR+='# Forwarding should be enabled for vpn<->subnet interfaces.'
;;
esac
if [[ ${D} ]]; then
disable_client "${USER}"
fi
chown :"${OPENVPN_GROUP}" "${CLIENT_CONFIG_DIR}/${USER}"
chmod 640 "${CLIENT_CONFIG_DIR}/${USER}"
# chown :${OPENVPN_GROUP} "${EASYRSA}/pki/index.txt"
# chmod 640 "${EASYRSA}/pki/index.txt"
# chmod og+x "${EASYRSA}/pki"{,/issued,/private}
# cp "${EASYRSA}/pki/issued/${USER}.crt" "${EASYRSA}/pki/private/${USER}.key" "${KEYS_DIR}/ca.crt" "${KEYS_DIR}/ta.key" temp
mkdir -p "${CLIENT_PACKS_DIR}/${HOST_NAME}"
# Write client config
eval "read -rd '' CLIENT_CONFIG <<EOF${BR}${CLIENT_CONFIG_TEMPLATE}${BR}EOF"
echo "${CLIENT_CONFIG}" >"${CLIENT_PACKS_DIR}/${HOST_NAME}/${CLIENT_CONFIG_NAME}"
chown -R :sudo "${CLIENT_PACKS_DIR}/${HOST_NAME}"
# (set -o posix; set) >&2
# .zip pack beside all-in-one config
# Include:
# ${USER}.tgz with ca.crt ${USER}.key ${USER}.crt
# ta.key
# ${USER} all-in-one conf
if ((CREATE_ZIP)); then
TMP_USER_DIR="/tmp/openvpn-manage/${USER}"
mkdir -p "${TMP_USER_DIR}"
tar --transform 's/.*\///g' -zchf "${TMP_USER_DIR}/${USER}.tgz" "./pki/private/${USER}.key" "./pki/issued/${USER}.crt" ./pki/ca.crt &>"${DESCRIPTOR}"
cp -l ./pki/ta.key "${CLIENT_PACKS_DIR}/${HOST_NAME}/${CLIENT_CONFIG_NAME}" "${TMP_USER_DIR}/" &>"${DESCRIPTOR}" || \
cp ./pki/ta.key "${CLIENT_PACKS_DIR}/${HOST_NAME}/${CLIENT_CONFIG_NAME}" "${TMP_USER_DIR}/" &>"${DESCRIPTOR}"
zip -rjD "${CLIENT_PACKS_DIR}/${HOST_NAME}/${USER}.zip" "${TMP_USER_DIR}/" &>"${DESCRIPTOR}"
rm "${TMP_USER_DIR}/"{"${USER}.tgz",ta.key,"${CLIENT_CONFIG_NAME}"} &>"${DESCRIPTOR}" && rm -r "${TMP_USER_DIR}/" &>"${DESCRIPTOR}"
[[ ${DESCRIPTOR} == /dev/null ]] || echo "${BR}${BR}"
fi
if [[ "${VERB}" == conf ]]; then
read -rd '' OVPN_CONF_CONTENT <"${CLIENT_PACKS_DIR}/${HOST_NAME}/${CLIENT_CONFIG_NAME}"
echo "${OVPN_CONF_CONTENT}"
elif [[ "${VERB}" == none ]]; then
:
else
echo "${USER}: config created:${BR}${CLIENT_PACKS_DIR}/${HOST_NAME}/${CLIENT_CONFIG_NAME}"
fi
cd - >/dev/null
}
create_host() {
local HOST_NAME COUNT INCSUB
HOST_NAME=$1
[[ $2 ]] && COUNT=$2 || COUNT=1
if (( COUNT <= 0 )); then
quit 1 "Invalid COUNT: ${COUNT}"
fi
for ((n=1;n<=COUNT;++n)); do
INCSUB=no # Is subnet was incremented?
if [[ "${CLIENT_TYPE}" == router ]]; then
if [[ -z "${CLIENT_SUBNET_BEHIND}" ]]; then
# Increment last saved subnet
if [[ -f "${LAST_IROUTE_SUBNET_FILE}" ]]; then
increment_subnet -v CLIENT_SUBNET_BEHIND "$(< "${LAST_IROUTE_SUBNET_FILE}")"
else
CLIENT_SUBNET_BEHIND=${INITIAL_IROUTE_SUBNET}
fi
INCSUB=yes
fi
if [[ -z "${S}" ]]; then # If not specified in command line
validate_ip4cidr "${CLIENT_SUBNET_BEHIND}" || quit 1 "Invalid subnet: ${CLIENT_SUBNET_BEHIND}"
fi
fi
add_client "${HOST_NAME}"
if [[ $? -eq 0 && "${INCSUB}" == yes ]]; then
[[ -f "${LAST_IROUTE_SUBNET_FILE}" ]] && mv "${LAST_IROUTE_SUBNET_FILE}"{,.old}
echo "${CLIENT_SUBNET_BEHIND}" >"${LAST_IROUTE_SUBNET_FILE}"
echo "Note: LAST_IROUTE_SUBNET was incremented to ${CLIENT_SUBNET_BEHIND}" >&2
fi
if [[ "${CLIENT_TYPE}" == router ]]; then
echo "Router internal subnet set to ${CLIENT_SUBNET_BEHIND}"
fi
echo
done
if ((CREATE_ZIP)); then
zip --exclude \*.zip -rjD "${CLIENT_PACKS_DIR}/${HOST_NAME}.zip" "${CLIENT_PACKS_DIR}/${HOST_NAME}/" >/dev/null
[[ "${VERB}" == default || "${VERB}" == debug ]] && \
echo "${BR}Host pack with client configs created:${BR}${CLIENT_PACKS_DIR}/${HOST_NAME}.zip"
fi
}
find_client_names() {
# Return array of client names for specified host_name
local _var _res _host_name _filename
if [[ $1 == '-v' ]]; then _var=$2; shift 2; fi
_res=()
_host_name=$1
for _filename in "${CLIENT_PACKS_DIR}/${_host_name}/"*.{ovpn,conf}; do
_filename=${_filename##*/}
_filename=${_filename%.ovpn}
_filename=${_filename%.conf}
[[ "${_filename}" == '*' ]] && continue
_res+=("${_filename}")
done
if [[ ${_var} ]]; then eval "${_var}=(\${_res[@]})"; else echo "${_res[@]}"; fi
}
BR=$'\n'
# Main case: Parse positional arguments
case "$1" in
''|help) quit 255 "${HELP}";;
*) # Parse args, verify, apply defaults.
# Really not for all actions root required, but to be sure...
# ((UID)) && quit 2 'Root privileges required'
[[ $2 ]] || quit 1 "Action requires parameter!"
unset HELP
## Defaults
DESCRIPTOR=/dev/null # Default if no -v option specified
CONF='/etc/openvpn/server/scripts/openvpn-manage.conf'
CREATE_ZIP=0 # Do not create zip archives
# Options below can be overwrited in CONF file
# CA_PASS=no # 'yes' if CA is password protected
SERVER_DIR='/etc/openvpn/server'
KEYS_DIR="${SERVER_DIR}/keys"
CLIENT_KEYS_DIR="${SERVER_DIR}/client-keys"
CLIENT_PACKS_DIR="${SERVER_DIR}/client-packs"
CLIENT_CONFIG_DIR="${SERVER_DIR}/client-configs"
USERS_CONF="${SERVER_DIR}/users-config"
UDP_SERVER_CONF='/etc/openvpn/udp.server.conf'
TCP_SERVER_CONF='/etc/openvpn/tcp.server.conf'
if [[ -f "${UDP_SERVER_CONF}" ]]; then
SERVER_CONF="${UDP_SERVER_CONF}"
elif [[ -f "${TCP_SERVER_CONF}" ]]; then
SERVER_CONF="${TCP_SERVER_CONF}"
else
SERVER_CONF='/etc/openvpn/server.conf'
fi
EASYRSA='/etc/openvpn/easy-rsa/easyrsa3'
CLIENT_OS_DEFAULT='nix' # 'nix' for Unix-based OS, 'windows' for Windows
OPENVPN_USER=openvpn
OPENVPN_GROUP=openvpn
INITIAL_IROUTE_SUBNET='10.10.0.0/29'
LAST_IROUTE_SUBNET_FILE="${SERVER_DIR}/last_iroute_subnet"
HOST_NAME_ALLOWED_REGEX='^[A-Za-z0-9_-]+$'
ALLOW_DIFFERENT_CLIENT_TYPES_FOR_HOST=no
## END Defaults
## Parse options
# Calculate first option position
for ((i=1;i<=$#;++i)); do
if [[ "${!i::1}" == '-' ]]; then
OPTIND=$i
break
fi
done
while getopts ":edt:o:s:c:f:v:z:r:" OPT; do
[[ "${OPTARG::1}" = '-' ]] && quit 1 "Option argument can not start with '-'"
case "$OPT" in
v) VERB=${OPTARG}; V='-v';
[[ "${VERB}" == debug ]] && DESCRIPTOR=/dev/stdout
;;
e) ENABLED=yes; E='-e';;
d) ENABLED=no; D='-d';;
t) CLIENT_TYPE=${OPTARG}; T='-t';;
o) CLIENT_OS=${OPTARG}; O='-o';
case "${CLIENT_OS}" in
linux|mac|bsd) CLIENT_OS=nix;;
windows|nix) ;;
*) quit 1 "Invalid OS: ${CLIENT_OS}${BR}"'Have to be "windows" or "nix" (for Linux/Mac/BSD/etc). "linux", "mac", "bsd" translated to "nix".'
esac
;;
s) CLIENT_SUBNET_BEHIND=${OPTARG}; S='-s';
validate_ip4cidr "${CLIENT_SUBNET_BEHIND}" || quit 1 "Invalid subnet: ${CLIENT_SUBNET_BEHIND}"
;;
c) COUNT=${OPTARG};;
f) CONF=${OPTARG};;
z) [[ "${OPTARG}" == 1 ]] && CREATE_ZIP=1 || CREATE_ZIP=0;;
r) REMOTE=${OPTARG}
# It also could be domain, so
# validate_ip4 "${REMOTE%%:*}" || quit 1 "Invalid IP on -r option value: ${REMOTE}"
;;
:) quit 1 "Option -$OPTARG requires an argument";;
*) quit 1 "Invalid option: -$OPTARG";;
esac
done
# OPTIND=1
# Import configuration file
. "${CONF}"
(($?)) && quit 1 "Failed to import configuration file: ${CONF}"
# echo "${CLIENT_CONFIG_TEMPLATE}" >&2
# Set variables of for -r option
if [[ -n ${REMOTE} ]]; then
[[ ${REMOTE%%:*} ]] && REMOTE_IP=${REMOTE%%:*}
[[ ${REMOTE##*:} =~ ^[0-9]+$ ]] && REMOTE_PORT=${REMOTE##*:}
fi
CHROOT=$(exec -c grep -m1 '^chroot ' "${SERVER_CONF}")
CHROOT=${CHROOT#chroot }
if [[ -n "${CHROOT}" && "${CHROOT}" != '/' ]]; then
USERS_CONF_IN_CHROOT=${USERS_CONF##*/} # E.g. users-config
else
USERS_CONF_IN_CHROOT=${USERS_CONF}
fi
# Verification of config options
[[ -d "${CHROOT}" ]] || quit 1 "Chroot server dir have to exist for current configuration: ${CHROOT}"
[[ -d "${SERVER_DIR}" ]] || quit 1 "Server dir does not exist: ${SERVER_DIR}"
[[ -d "${KEYS_DIR}" ]] || quit 1 "Keys dir does not exist: ${KEYS_DIR}"
[[ -d "${CLIENT_KEYS_DIR}" ]] || quit 1 "Clients keys dir does not exist: ${CLIENT_KEYS_DIR}"
[[ -d "${CLIENT_PACKS_DIR}" ]] || quit 1 "Clients packages dir dir does not exist: ${CLIENT_PACKS_DIR}"
[[ -d "${CLIENT_CONFIG_DIR}" ]] || quit 1 "Clients config dir dir does not exist: ${CLIENT_CONFIG_DIR}"
[[ -f "${USERS_CONF}" ]] || quit 1 "Users shared config file does not exist: ${USERS_CONF}"
[[ -f "${SERVER_CONF}" ]] || quit 1 "OpenVPN server config file does not exist: ${SERVER_CONF}"
[[ -d "${EASYRSA}" ]] || quit 1 "EASYRSA dir does not exist: ${EASYRSA}"
[[ -f "${EASYRSA}/pki/crl.pem" ]] || quit 1 "Certificate revocation list file does not exist: ${EASYRSA}/pki/crl.pem"
getent passwd "${OPENVPN_USER}" &>/dev/null || quit 1 "No such user: ${OPENVPN_USER}"
getent group "${OPENVPN_GROUP}" &>/dev/null || quit 1 "No such group: ${OPENVPN_GROUP}"
[[ -z ${CLIENT_OS} ]] && CLIENT_OS=${CLIENT_OS_DEFAULT}
if [[ -n $E && -n $D ]]; then
quit 1 "Options -e and -d can not be specified simultaneously!"
fi
case "${CLIENT_TYPE}" in
'') CLIENT_TYPE=user;;
user|router) ;;
*) quit 1 "Invalid argument for -t option: ${CLIENT_TYPE}";;
esac
if [[ -n "${S}" && "${CLIENT_TYPE}" != router ]]; then
quit 1 "Subnet can be specified for type \"router\" only"
fi
case "${COUNT}" in
'') COUNT=1;;
*[^0-9]*) quit 1 "Invalid COUNT: ${COUNT}";;
esac
# Set openvpn management interface variables
UDP_MANAGEMENT_SOCKET=''
UDP_MANAGEMENT_IP=''
UDP_MANAGEMENT_PORT=''
UDP_MANAGEMENT_PWFILE=''
UDP_MANAGEMENT_USER='root'
UDP_MANAGEMENT_GROUP='root'
if [[ -f "${UDP_SERVER_CONF}" ]]; then
while read -a cflarr || [[ -n "${cflarr}" ]]; do
if [[ "${cflarr[0]}" == 'management' ]]; then
UDP_MANAGEMENT_PWFILE=${cflarr[3]}
if [[ "${cflarr[2]}" == unix ]]; then
UDP_MANAGEMENT_SOCKET=${cflarr[1]}
else
UDP_MANAGEMENT_IP=${cflarr[1]}
UDP_MANAGEMENT_PORT=${cflarr[2]}
fi
elif [[ "${cflarr[0]}" == 'management-client-user' ]]; then
UDP_MANAGEMENT_USER=${cflarr[1]}
elif [[ "${cflarr[0]}" == 'management-client-group' ]]; then
UDP_MANAGEMENT_GROUP=${cflarr[1]}
fi
done <"${UDP_SERVER_CONF}"
fi
TCP_MANAGEMENT_SOCKET=''
TCP_MANAGEMENT_IP=''
TCP_MANAGEMENT_PORT=''
TCP_MANAGEMENT_PWFILE=''
TCP_MANAGEMENT_USER='root'
TCP_MANAGEMENT_GROUP='root'
if [[ -f "${TCP_SERVER_CONF}" ]]; then
while read -a cflarr || [[ -n "${cflarr}" ]]; do
if [[ "${cflarr[0]}" == 'management' ]]; then
TCP_MANAGEMENT_PWFILE=${cflarr[3]}
if [[ "${cflarr[2]}" == unix ]]; then
TCP_MANAGEMENT_SOCKET=${cflarr[1]}
else
TCP_MANAGEMENT_IP=${cflarr[1]}
TCP_MANAGEMENT_PORT=${cflarr[2]}
fi
elif [[ "${cflarr[0]}" == 'management-client-user' ]]; then
TCP_MANAGEMENT_USER=${cflarr[1]}
elif [[ "${cflarr[0]}" == 'management-client-group' ]]; then
TCP_MANAGEMENT_GROUP=${cflarr[1]}
fi
done <"${TCP_SERVER_CONF}"
fi
;;&
create)
HOST_NAME=$2
[[ ${HOST_NAME} ]] || quit 1 "You have to specify HOST_NAME"
validate_host_name "${HOST_NAME}"
if [[ -d "${CLIENT_PACKS_DIR}/${HOST_NAME}" ]]; then
quit 1 "Host already exist: ${HOST_NAME}${BR}Use \"append\" to append clients to existing host."
fi
;;&
append)
HOST_NAME=$2
[[ ${HOST_NAME} ]] || quit 1 "You have to specify HOST_NAME"
validate_host_name "${HOST_NAME}"
if ! [[ -d "${CLIENT_PACKS_DIR}/${HOST_NAME}" ]]; then
quit 1 "Host does not exist: ${HOST_NAME}${BR}Use 'create' to create new host."
fi
for i in "${CLIENT_CONFIG_DIR}"/*-"${HOST_NAME}"; do FIRST_CLIENT_CONFIG_FILE=$i; break; done
if grep -q '^iroute ' "${FIRST_CLIENT_CONFIG_FILE}"; then
# Use the same client type as other clients has
CLIENT_TYPE_PREVIOUS=router
else
CLIENT_TYPE_PREVIOUS=user
fi
if [[ "${CLIENT_TYPE_PREVIOUS}" != "${CLIENT_TYPE}" ]]; then
if [[ "${ALLOW_DIFFERENT_CLIENT_TYPES_FOR_HOST}" == yes ]]; then
if [[ ${T} ]]; then # Specified in command line explicitly
[[ ${V} ]] && echo "Warning: Client type specified in command line doesn't match the type of previous clients for this host!" &>"${DESCRIPTOR}"
else
CLIENT_TYPE=${CLIENT_TYPE_PREVIOUS}
fi
else
[[ $T ]] && quit 1 "Client type specified in command line doesn't match the type of previous clients for this host!${BR}Remove it or specify proper type."
CLIENT_TYPE=${CLIENT_TYPE_PREVIOUS}
fi
fi
if [[ "${CLIENT_TYPE}" == router && -z "${S}" ]]; then
populate_subnet_var "${HOST_NAME}"
fi
# Reuse previous CLIENT_OS if commandline option '-o' ommited
for i in "${CLIENT_PACKS_DIR}/${HOST_NAME}/"*[np][fn]; do FIRST_EXT=${i##*.}; break; done
if [[ -z "$O" ]]; then
if [[ "${FIRST_EXT}" == 'ovpn' ]]; then
CLIENT_OS=windows
else
CLIENT_OS=nix
fi
fi
;;&
create|append)
[[ -z ${CLIENT_CONFIG_TEMPLATE} ]] && quit 1 'CLIENT_CONFIG_TEMPLATE variable required, you have to set it in configuration file.'
create_host "${HOST_NAME}" "${COUNT}"
;;
enable|disable|revoke|update|show)
case "$2" in
client)
CLIENT_NAME=$3
[[ ${CLIENT_NAME} ]] || quit 1 "You have to specify CLIENT_NAME"
validate_client_name "${CLIENT_NAME}"
if ! [[ -f "${CLIENT_CONFIG_DIR}/${CLIENT_NAME}" ]]; then
quit 1 "No such client or client config is absent: ${CLIENT_NAME}"
fi
;;
host)
HOST_NAME=$3
[[ ${HOST_NAME} ]] || quit 1 "You have to specify HOST_NAME"
validate_host_name "${HOST_NAME}"
if ! [[ -d "${CLIENT_PACKS_DIR}/${HOST_NAME}" ]]; then
quit 1 "No such host: ${HOST_NAME}"
fi
;;
*) quit 1 "Invalid parameter: $2"
esac
;;&
enable)
case "$2" in
client)
enable_client "${CLIENT_NAME}"
;;
host)
find_client_names -v client_names "${HOST_NAME}"
for cn in "${client_names[@]}"; do enable_client "${cn}"; done
echo "${BR}All clients of ${HOST_NAME} was enabled"
;;
esac
;;
disable)
case "$2" in
client)
disable_client "${CLIENT_NAME}"
;;
host)
find_client_names -v client_names "${HOST_NAME}"
for cn in "${client_names[@]}"; do disable_client "${cn}"; done
echo "${BR}All clients of ${HOST_NAME} was disabled"
;;
esac
;;
revoke)
cd "${EASYRSA}"
case "$2" in
client)
revoke_cert "${CLIENT_NAME}"
;;
host)
find_client_names -v client_names "${HOST_NAME}"
for cn in "${client_names[@]}"; do revoke_cert "${cn}"; done
echo "${BR}All certs of ${HOST_NAME} was revoked"
;;
esac
if ./easyrsa --batch gen-crl &>"${DESCRIPTOR}"; then
echo "Revocation list was updated"
else
quit 1 "Failed to update revocation list!"
fi
chown :"${OPENVPN_GROUP}" "${EASYRSA}/pki/crl.pem"
chmod 440 "${EASYRSA}/pki/crl.pem"
if [[ -d /etc/openvpn/chroot ]]; then
rm -f /etc/openvpn/chroot/crl.pem # Workaround for CentOS which ignore -f option for cp
cp -pfT "${EASYRSA}/pki/crl.pem" /etc/openvpn/chroot/crl.pem
fi
# chown :${OPENVPN_GROUP} "${EASYRSA}/pki/index.txt"
# chmod 640 "${EASYRSA}/pki/index.txt"
# chmod g+x "${EASYRSA}/pki"
cd - >/dev/null
;;
update) # Update type and/or subnet
case "$2" in
client)
update_client "${CLIENT_NAME}"
;;
host)
# quit 254 "Update all host clients feature does not implemented yet. Use it with single client."
find_client_names -v client_names "${HOST_NAME}"
for cn in "${client_names[@]}"; do update_client "${cn}"; done
echo "${BR}All clients of ${HOST_NAME} was updated"
;;
esac
;;
list) # TODO: finish
case "$2" in
clients|client)
OUT=$(ls -1 "${CLIENT_CONFIG_DIR}" | grep -v '^DEFAULT$') # All clients/certs