This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
versa.sh
executable file
·2061 lines (1683 loc) · 60.8 KB
/
versa.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
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 Very Easy RSA Usermanagement Script
# Author: Rene Fa
# Version: 1.6.8
# Date: 12.05.2018
# Licence: GNU Lesser General Public Licence
source /etc/versa/config.sh
# These values must not be changed
FP="/tmp/versa-v-stdout" # Path for the verbose named pipe
# OpenVPN config
OPENVPN_CONF() {
cat <<EOF
port 1194
proto udp
dev tun
ca versa/rootCA.crt
cert versa/server.crt
key versa/server.key # This file should be kept secret
dh versa/dh.pem
crl-verify versa/crl.pem
topology subnet
# Configure your desired subnet here
server $SUBNET_IP $SUBNET_MASK
ifconfig-pool-persist ipp.txt
#Push Traffic through VPN
#push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS $SERVER_IP"
push "dhcp-option DNS 80.80.80.80"
client-config-dir ccd
push "route 10.250.0.0 255.255.255.0"
#client-to-client
auth-user-pass-verify versa/verify.sh via-file
script-security 2
keepalive 10 120
cipher AES-256-CBC # AES
comp-lzo
user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
verb 3
EOF
}
OPENVPN_CLIENT_CONF() {
PUBLIC_IP="$(cat $OPENVPN_PATH/versa/publicip)"
cat <<EOF
#daemon 1
client
dev tun
proto udp
remote $PUBLIC_IP 1194
resolv-retry infinite
nobind
user nobody
group nogroup
persist-key
persist-tun
mute-replay-warnings
ns-cert-type server
cipher AES-256-CBC
comp-lzo
verb 3
## If you use Linux
#script-security 2
#up /etc/openvpn/update-resolv-conf
#down /etc/openvpn/update-resolv-conf
## If you use Linux with systemd-resolved (eg. >= Ubuntu 16.10 )
#script-security 2
#setenv PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#up /etc/openvpn/scripts/update-systemd-resolved
#down /etc/openvpn/scripts/update-systemd-resolved
#down-pre
auth-user-pass
#auth-user-pass authfile
EOF
}
OPENSSL_CONF() {
cat <<EOF
HOME = .
RANDFILE = \$ENV::HOME/.rnd
oid_section = new_oids
[ new_oids ]
tsa_policy1 = 1.2.3.4.1
tsa_policy2 = 1.2.3.4.5.6
tsa_policy3 = 1.2.3.4.5.7
[ ca ]
default_ca = CA_default # The default ca section
[ CA_default ]
dir = . # Where everything is kept
certs = \$dir/certs # Where the issued certs are kept
crl_dir = \$dir/crl # Where the issued crl are kept
database = \$dir/index.txt # database index file.
# several ctificates with same subject.
new_certs_dir = \$dir/newcerts # default place for new certs.
certificate = \$dir/rootCA.crt # The CA certificate
serial = \$dir/serial # The current serial number
crlnumber = \$dir/crlnumber # the current crl number
# must be commented out to leave a V1 CRL
crl = \$dir/crl.pem # The current CRL
private_key = \$dir/private/rootCA.key # The private key
RANDFILE = \$dir/private/.rand # private random number file
x509_extensions = usr_cert # The extentions to add to the cert
name_opt = ca_default # Subject Name options
cert_opt = ca_default # Certificate field options
default_days = 365 # how long to certify for
default_crl_days= 30 # how long before next CRL
default_md = default # use public key default MD
preserve = no # keep passed DN ordering
policy = policy_match
[ policy_match ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[ policy_anything ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[ req ]
default_bits =
default_keyfile = privkey.pem
distinguished_name = req_distinguished_name
attributes = req_attributes
x509_extensions = v3_ca # The extentions to add to the self signed cert
string_mask = utf8only
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = $ENV::KEY_COUNTRY
countryName_min = 2
countryName_max = 2
stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = $ENV::KEY_PROVINCE
localityName = Locality Name (eg, city)
localityName_defailt = $ENV::KEY_CITY
0.organizationName = Organization Name (eg, company)
0.organizationName_default = $ENV::KEY_ORG
organizationalUnitName = Organizational Unit Name (eg, section)
commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_max = 64
emailAddress = Email Address
emailAddress_default = $ENV::KEY_EMAIL
emailAddress_max = 64
[ req_attributes ]
challengePassword = A challenge password
challengePassword_min = 4
challengePassword_max = 20
unstructuredName = An optional company name
[ usr_cert ]
basicConstraints=CA:FALSE
nsComment = "OpenSSL Generated Certificate"
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
[ v3_ca ]
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always,issuer
basicConstraints = CA:true
[ crl_ext ]
authorityKeyIdentifier=keyid:always
[ proxy_cert_ext ]
basicConstraints=CA:FALSE
nsComment = "OpenSSL Generated Certificate"
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
proxyCertInfo=critical,language:id-ppl-anyLanguage,pathlen:3,policy:foo
[ tsa ]
default_tsa = tsa_config1 # the default TSA section
[ tsa_config1 ]
dir = ./demoCA # TSA root directory
serial = \$dir/tsaserial # The current serial number (mandatory)
crypto_device = builtin # OpenSSL engine to use for signing
signer_cert = \$dir/tsacert.pem # The TSA signing certificate
# (optional)
certs = \$dir/rootCA.crt # Certificate chain to include in reply
# (optional)
signer_key = \$dir/private/rootCA.key # The TSA private key (optional)
default_policy = tsa_policy1 # Policy if request did not specify it
# (optional)
other_policies = tsa_policy2, tsa_policy3 # acceptable policies (optional)
digests = md5, sha1 # Acceptable message digests (mandatory)
accuracy = secs:1, millisecs:500, microsecs:100 # (optional)
clock_precision_digits = 0 # number of digits after dot. (optional)
ordering = yes # Is ordering defined for timestamps?
# (optional, default: no)
tsa_name = yes # Must the TSA name be included in the reply?
# (optional, default: no)
ess_cert_id_chain = no # Must the ESS cert id chain be included?
# optional, default: no)
[ server ]
basicConstraints = CA:FALSE
nsCertType = server
nsComment = "OpenSSL Generated Server Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
extendedKeyUsage = serverAuth
keyUsage = digitalSignature, keyEncipherment
EOF
}
OPENVPN_VERIFY() {
cat <<EOF
#!/bin/bash
genhash() {
HASHPASS=\`echo -n "\$1\$2" | md5sum | sed s'/\ -//'\`
i=0
while [ \$i -lt 10 ]; do
HASHPASS=\`echo -n \$HASHPASS\$HASHPASS | md5sum | sed s'/\ -//'\`
i=\`expr \$i + 1\`
done
echo -n \$1:\$HASHPASS
}
verify() {
USERS=\`cat ./versa/userpermissions\`
[[ \$# -eq 2 ]] || exit 1
[[ \$(sed -n "/^\$1\:/p" ./versa/userpermissions | cut -d':' -f 5) -eq 0 ]] && exit 1
for i in \$USERS; do
i=\$(echo "\$i" | sed -r 's/^([^\:]*:[0-9a-f]*):.*\$/\1/')
[[ "\$i" == \`genhash "\$1" "\$2"\` ]] && exit 0
done
}
[[ \$1 == "--genhash" ]] && echo \`genhash "\$2" "\$3"\`
[[ -e "\$*" ]] || exit 1
verify \`cat "\$*"\`
exit 1
EOF
}
EDIT_IT() {
cat <<EOF
#!/bin/bash
# Author: Rene Fa 2017
usage() { echo "Usage: \$0 instruction (instruction parameters) [options]
Global options:
( You have no options )
Instructions:
switch filename bname bnumber [commentsign]
Activates only one block and comments the other blocks.
bname is the name of the block.
bnumber is the name of.
filename is the name of the file.
commentsign is the character, which initiates a comment.
delete filename bname bnumber [commentsign]
Deletes a block.
create filename bname bnumber [line-number] [commentsign]
Create a new block after the given line-number.
clear filename bname bnumber [commentsign]
Empty the content of a block.
enable filename bname bnumber [commentsign]
Uncomments a single block.
disable filename bname bnumber [commentsign]
Comments a single block.
get filename bname bnumber [commentsign]
Prints the content of the block uncommented.
state filename bname [commentsign]
Prints the state of a block.
insert filename bname bnumber new-line [line-number] [commentsign]
Appends a new line to a block.
A line-number of 0 means appending at the end of
the block. (default value)
dell filename bname bnumber line-number [commentsign]
Deletes a specific line from a block.
"
}
if test \$# -lt 3
then
echo "editIt: Too few arguments!"
usage
exit
fi
instr=\$1
case "\$instr" in
"state")
filename=\$2
block=\$3
csign=\$4
;;
"insert")
if test \$# -lt 5
then
echo "editIt: Too few arguments!"
exit
fi
filename=\$2
block=\$3
state=\$4
data=\$5
if test \$# -gt 5; then
data2=\$6
csign=\$7
else
data2=0
csign=""
fi
;;
"dell")
if test \$# -lt 5
then
echo "editIt: Too few arguments!"
exit
fi
filename=\$2
block=\$3
state=\$4
data=\$5
csign=\$6
;;
"create")
if test \$# -lt 4
then
echo "editIt: Too few arguments!"
exit
fi
filename=\$2
block=\$3
state=\$4
if test \$# -gt 4; then
data=\$5
csign=\$6
else
data=0
csign=""
fi
;;
*)
if test \$# -lt 4
then
echo "editIt: Too few arguments!"
exit
fi
filename=\$2
block=\$3
state=\$4
csign=\$5
;;
esac
if test "\$csign" = ""
then
csign="#"
fi
if test "\$instr" = "state"
then
awk "/\$csign IFCBLOCK \$block /{print}" \$filename | awk '{print \$4" "\$5}'
elif test "\$instr" = "create"; then
test "\$data" == "0" && data=\$(sed -n '\$=' \$filename)
sed -i "\${data}a \${csign} IFCBLOCK \$block \$state 1" \$filename
sed -i "\$(( \${data} +1 ))a \${csign} ENDIFCBLOCK \$block" \$filename
elif [[ \$instr =~ (switch|delete|clear|enable|disable|insert|get|dell) ]]
then
snum=(\`sed -n "/^\$csign IFCBLOCK \$block /=" \$filename | tr '\n' ' '\`)
enum=(\`sed -n "/^\$csign ENDIFCBLOCK \$block\$/=" \$filename | tr '\n' ' '\`)
if test \${#snum[*]} -ne \${#enum[*]}
then
echo "editIt: Syntax Error!"
exit
fi
count=\${#snum[*]}
for (( i=0; i<\$count; i++ ))
do
sb=\$(( \${snum[\$i]} + 1 ))
eb=\$(( \${enum[\$i]} - 1 ))
empty=0
test \$sb -gt \$eb && empty=1
lstate=\$(sed -ne "\${snum[\$i]}p" \$filename | awk "/\$csign IFCBLOCK \$block /{print}" | awk '{print \$4}')
case "\$instr" in
"switch")
if test \$state -eq \$lstate
then
bst=\$(sed -n "\${snum[\$i]}p" \$filename | cut -d' ' -f 5)
test \$bst -eq 1 && continue
test \$empty -eq 1 && continue
sed -i -r -e "\${sb},\${eb}s/^\${csign}(.*)/\1/" \$filename
sed -i -e "\${snum[\$i]}s/^.*\$/\${csign} IFCBLOCK \$block \$lstate 1/" \$filename
else
bst=\$(sed -n "\${snum[\$i]}p" \$filename | cut -d' ' -f 5)
test \$bst -eq 0 && continue
sed -i -e "\${sb},\${eb}s/^/\${csign}/" \$filename
sed -i -e "\${snum[\$i]}s/^.*\$/\${csign} IFCBLOCK \$block \$lstate 0/" \$filename
fi
;;
"delete")
if test \$state -eq \$lstate
then
sed -i -e "\${snum[\$i]},\${enum[\$i]}d" \$filename
dsub=\$(( \${enum[\$i]} +1 -\${snum[\$i]} ))
for (( j=\$i; j<\$count; j++ )) ; do
snum[\$j]=\$(( \${snum[\$j]} -\$dsub ))
enum[\$j]=\$(( \${enum[\$j]} -\$dsub ))
done
fi
;;
"clear")
if test \$state -eq \$lstate
then
test \$empty -eq 1 && continue
sed -i -e "\${sb},\${eb}d" \$filename
dsub=\$(( \${eb} +1 -\${sb} ))
for (( j=\$i; j<\$count; j++ )) ; do
snum[\$j]=\$(( \${snum[\$j]} -\$dsub ))
enum[\$j]=\$(( \${enum[\$j]} -\$dsub ))
done
fi
;;
"enable")
if test \$state -eq \$lstate
then
bst=\$(sed -n "\${snum[\$i]}p" \$filename | cut -d' ' -f 5)
test \$bst -eq 1 && continue
test \$empty -eq 1 && continue
sed -i -r -e "\${sb},\${eb}s/^\${csign}(.*)/\1/" \$filename
sed -i -e "\${snum[\$i]}s/^.*\$/\${csign} IFCBLOCK \$block \$lstate 1/" \$filename
fi
;;
"disable")
if test \$state -eq \$lstate
then
bst=\$(sed -n "\${snum[\$i]}p" \$filename | cut -d' ' -f 5)
test \$bst -eq 0 && continue
test \$empty -eq 1 && continue
sed -i -e "\${sb},\${eb}s/^/\${csign}/" \$filename
sed -i -e "\${snum[\$i]}s/^.*\$/\${csign} IFCBLOCK \$block \$lstate 0/" \$filename
fi
;;
"get")
if test \$state -eq \$lstate
then
test \$empty -eq 1 && continue
sed -n -e "\${sb},\${eb}s/^\${csign}*//p" \$filename
exit 0
fi
;;
"insert")
if test \$state -eq \$lstate
then
test \$data2 -eq 0 && tb=\$eb || tb=\$(( \${snum[\$i]} -1 +\$data2))
bst=\$(sed -n "\${snum[\$i]}p" \$filename | cut -d' ' -f 5)
sed -i "\${tb}a \$data" \$filename
dsub=\$(echo "\$data" | sed 's/\\n/\n/g' | wc -l)
if test \$bst -eq 0; then
for (( j=0; j<\$dsub; j++ )) ; do
sed -i "\$(( \${tb} +1 +\$j ))s/^/\${csign}/" \$filename
done
fi
for (( j=\$i; j<\$count; j++ )) ; do
snum[\$j]=\$(( \${snum[\$j]} +\$dsub ))
enum[\$j]=\$(( \${enum[\$j]} +\$dsub ))
done
fi
;;
"dell")
if test \$state -eq \$lstate
then
ln=\$(( \${snum[\$i]} +\$data ))
if test \$ln -lt \$sb -o \$ln -gt \$eb; then
echo "editIt: Index out of bounds"
exit 1
fi
sed -i "\${ln}d" \$filename
dsub=1
for (( j=\$i; j<\$count; j++ )) ; do
snum[\$j]=\$(( \${snum[\$j]} -\$dsub ))
enum[\$j]=\$(( \${enum[\$j]} -\$dsub ))
done
fi
;;
esac
done
fi
EOF
}
VERSA_AUTOCOMPLETE() {
cat <<EOF
_versa_getuser() {
COMPREPLY=(\$( compgen -W "\$(cat $OPENVPN_PATH/versa/userpermissions | cut -d':' -f 1)" -- \$cur))
}
_versa_getgroup() {
COMPREPLY=(\$( compgen -W "\$(cat $OPENVPN_PATH/versa/groups)" -- \$cur))
}
_versa_gettargetgroup() {
COMPREPLY=(\$( compgen -W "\$(cat $OPENVPN_PATH/versa/targetgroups)" -- \$cur))
}
_versa ()
{
local cur
COMPREPLY=( )
cur=\${COMP_WORDS[COMP_CWORD]}
case "\$COMP_CWORD" in
1)
COMPREPLY=(\$( compgen -W 'install useradd userdel usermod certrefresh certexport userlist userinfo rebuild clean useraccessadd useraccessdel usertargetadd usertargetdel userenable userdisable vlanlist vlaninfo' -- \$cur ) )
;;
2)
case "\${COMP_WORDS[1]}" in
userdel) _versa_getuser ;;
usermod) _versa_getuser ;;
certrefresh) _versa_getuser ;;
certexport) _versa_getuser ;;
userinfo) _versa_getuser ;;
useraccessadd) _versa_getuser ;;
useraccessdel) _versa_getuser ;;
usertargetadd) _versa_getuser ;;
userenable) _versa_getuser ;;
userdisable) _versa_getuser ;;
vlaninfo) _versa_getgroup ;;
esac
;;
3)
case "\${COMP_WORDS[1]}" in
useraccessadd) _versa_getgroup ;;
useraccessdel) _versa_getgroup ;;
usertargetadd) _versa_gettargetgroup ;;
usertargetdel) _versa_gettargetgroup ;;
esac
;;
esac
return 0
}
complete -F _versa -o filenames versa
EOF
}
##################################################################################
##################################################################################
##################################################################################
usage() { echo "Usage: $0 instruction (instruction parameters) [options]
Global options:
-q Quick and Quiet Mode. Just fill in the standard parameters and
dont ask any questions. Just ask for password if needed.
-d Debug mode. Forwards the stdout of used programs.
Prints much more information.
-c filename Change the output directory for the zip Package with the
certificates and config.
The default output is defined in the first lines of this script.
The password of the file is the new password of the user.
-m Multiple files flag. With this mode enabled, the certicatates
are seperated in own files. Default is defined in config.
-s Single file flag. Store all certificated in a single file.
The default setting is defined in the config.
Instructions:
install
Backup the old configuration and install versa.
useradd username [options]
Adds the specified user to the userlist, generate the
certificats and stores them in a given directory.
-p string Set a new password, to avoid the interactive prompt.
AVOID USING THIS PARAMETER FOR SECURITY REASONS!
-v stringrule Define the VLAN access rules.
Example: \"t=gaming,private;a=gaming,private,gateway\"
You are in the group gaming and private that means all
clients with the gaming and private permission can reach
your machine. The access rule are the networks that you can
reach.
-a ip-address Preselect a custom IP-address. With the -q option,
the prompt will be omited.
userdel username [options]
Removes a user from the userlist and adds the certificates
to the blacklist.
usermod username [options]
Change properties of a user.
-p string Set a new password, to avoid the interactive prompt.
AVOID USING THIS PARAMETER FOR SECURITY REASONS!
-a ip-address Preselect a custom IP-address. With the -q option,
the prompt will be omited.
certrefresh username [options]
Creates new certificates and revoke the old ones.
certexport username [options]
Export the certificates to a specified directory.
userlist [options]
List all users.
userinfo username [options]
Gives detailed information about a user.
userenable username [options]
Activates a VPN user.
userdisable username [options]
Deactivates a VPN user.
rebuild [options]
Rebuild all Firewall rules and DNS rules. Must be called
after every change in the VLAN/DNS config section.
clean [options]
Revert all changes which are made by versa.
Including /etc/hosts, iptables rules
useraccessadd username vlanname [options]
Grant the access to a group
useraccessdel username vlanname [options]
Revokes the access to a group.
usertargetadd username vlanname [options]
Add a user to a group target list. Every user with the
access permision can reach this client.
usertargetdel username vlanname [options]
Removes a user from a group target list.
vlanlist [options]
List all current VPN VLANs.
-u Just list the VPN VLANs without more information.
vlaninfo vlanname [options]
Gives detailed information about a VPN VLAN.
"
exit 1
}
vkilled() {
stty echo
echo -en "\033[0m"
[[ $(jobs -p | wc -l) -gt 0 ]] && kill $(jobs -p)
}
tmpc=0
for (( i=0; i<4; i++ ))
do
oct=$(echo $SUBNET_MASK | cut -d'.' -f $(( $i +1 )))
[[ "$oct" = "255" ]] && tmpc=$(( $tmpc +8 ))
[[ "$oct" = "0" ]] && break
[[ "$oct" = "128" ]] && tmpc=$(($tmpc +1)) && break
[[ "$oct" = "192" ]] && tmpc=$(($tmpc +2)) && break
[[ "$oct" = "224" ]] && tmpc=$(($tmpc +3)) && break
[[ "$oct" = "240" ]] && tmpc=$(($tmpc +4)) && break
[[ "$oct" = "248" ]] && tmpc=$(($tmpc +5)) && break
[[ "$oct" = "252" ]] && tmpc=$(($tmpc +6)) && break
[[ "$oct" = "254" ]] && tmpc=$(($tmpc +7)) && break
done
SUBNET_CIDR="$SUBNET_IP/$tmpc"
iptoraw() {
let "rawip= $(echo $1 | cut -d'.' -f 4) +$(echo $1 | cut -d'.' -f 3)*256 +$(echo $1 | cut -d'.' -f 2)*256*256 +$(echo $1 | cut -d'.' -f 1)*256*256*256"
echo -n "$rawip"
}
iptoform() {
r=$1
let "o1 = $r % 256"
let "r = $r / 256"
let "o2 = $r % 256"
let "r = $r / 256"
let "o3 = $r % 256"
let "r = $r / 256"
let "o4 = $r % 256"
echo -n "$o4.$o3.$o2.$o1"
}
IP_OFFSET=$(( $(iptoraw $(echo "$USER_IP_RANGE" | cut -d'-' -f 1)) -$(iptoraw $(echo "$SUBNET_IP" | cut -d'-' -f 1)) -1 ))
MIN_IP="$(iptoraw $(echo -n "$USER_IP_RANGE" | cut -d'-' -f 1))"
MAX_IP="$(iptoraw $(echo -n "$USER_IP_RANGE" | cut -d'-' -f 2))"
AMIN_IP="$(( $(iptoraw $SUBNET_IP) +2 ))"
AMAX_IP="$(( ( $(iptoraw $SUBNET_IP) | ( 4294967295 & ~$(iptoraw $SUBNET_MASK) ) ) -1 ))"
SERVER_IP="$(iptoform $(( $(iptoraw $SUBNET_IP) +1 )))"
checkip() {
FIP=$(iptoraw $1)
[[ $(( $FIP -$AMIN_IP )) -lt 0 ]] && return 1
[[ $(( $AMAX_IP -$FIP )) -lt 0 ]] && return 1
for i in $(ls /etc/openvpn/ccd/)
do
[[ "$1" = "$(cat /etc/openvpn/ccd/$i | head -n 1 | cut -d' ' -f 2)" ]] && return 1
done
return 0
}
searchip() {
for (( i=0;i<$(( $MAX_IP -$MIN_IP ));i++ ))
do
found=1
tip=$(iptoform $(( $MIN_IP+$i )))
for j in $(ls /etc/openvpn/ccd/)
do
[[ "$tip" = "$(cat /etc/openvpn/ccd/$j | head -n 1 | cut -d' ' -f 2)" ]] && found=0 && break
done
[[ $found -eq 1 ]] && echo "$tip" && return 0
done
return 1
}
verboselog() {
trap '[[ $(ps $rcid | wc -l) -gt 1 ]] && kill $rcid' EXIT
while : ;
do
if test $d -eq 1
then
cat $FP &
rcid=$!
wait $rcid
else
cat $FP > /dev/null &
rcid=$!
wait $rcid
fi
done
}
VLAN_EXIST() {
VEOPWD="$(pwd)"
cd $OPENVPN_PATH/versa
for (( j=0; j<${#GROUP_NAME[*]}; j++ ))
do
[[ "${GROUP_NAME[$j]}" = "$1" ]] && cd $VEOPWD && return 0
done
cd $VEOPWD
return 1
}
VLAN_T_EXIST() {
VEOPWD="$(pwd)"
cd $OPENVPN_PATH/versa
for (( j=0; j<${#GROUP_NAME[*]}; j++ ))
do
[[ "${GROUP_NAME[$j]}" = "$1" ]] && [[ -n "$(echo "${GROUP_VLAN_RULE[$j]}" | sed -n '/TARGET/p')" ]] && cd $VEOPWD && return 0
done
cd $VEOPWD
return 1
}
USER_EXIST() {
OPWD="$(pwd)"
cd $OPENVPN_PATH/versa
for i in $(cat userpermissions | cut -d':' -f 1 | tr '\n' ' ')
do
if test "$1" = "$i"
then
return 0
fi
done
return 1
cd $OPWD
}
vlan_user_add() {
OPWD="$(pwd)"
cd $OPENVPN_PATH/versa
userstr=$(cat userpermissions | sed -n "/^$1:/p")
tname=$(echo "$userstr" | cut -d':' -f 1)
U_IP="$(cat ../ccd/$tname | head -n 1 | cut -d' ' -f 2)"
lista=(`echo "$userstr" | cut -d':' -f 3 | tr ',' ' '`)
for (( i=0; i<${#lista[*]}; i++ ))
do
cag="${lista[$i]}"
if !(VLAN_EXIST $cag); then
echo -e "\033[31mGroup $cag not found!\033[0m"
return 1
fi
count=${#GROUP_NAME[*]}
for (( j=0; j<$count; j++ ))
do
[[ "${GROUP_NAME[$j]}" = "$cag" ]] && gn=$j
done
if test -n "${GROUP_SERVER_RULE[$gn]}"; then
IFS=";"
for rule in ${GROUP_SERVER_RULE[$gn]}; do
eval $(echo "iptables -A v-${GROUP_NAME[$gn]}-s -s $U_IP $rule")
done
unset IFS
iptables -A v-${GROUP_NAME[$gn]}-s -j RETURN
iptables -D v-${GROUP_NAME[$gn]}-s -j RETURN
fi
if test -n "${GROUP_VLAN_RULE[$gn]}"; then
IFS=";"
for rule in ${GROUP_VLAN_RULE[$gn]}; do
eval $(echo "iptables -A v-${GROUP_NAME[$gn]}-a -s $U_IP $rule" | sed "s/TARGET/v\-${GROUP_NAME[$gn]}\-t/")
done
unset IFS
iptables -A v-${GROUP_NAME[$gn]}-a -j RETURN
iptables -D v-${GROUP_NAME[$gn]}-a -j RETURN
fi
# Conf rules
if test -n "${GROUP_CONF_RULE[$gn]}"; then
IFS=";"
for rule in ${GROUP_CONF_RULE[$gn]}; do
./editIt.sh insert ../ccd/$1 VERSA_CONF 0 "$rule"
done
unset IFS
fi
# Custom rules
if test -n "${GROUP_CUSTOM_ADD_USER[$gn]}"; then
eval $(echo "${GROUP_CUSTOM_ADD_USER[$gn]}" | sed "s/U_IP/$U_IP/" | sed "s/U_NAME/$1/")
fi
done
listt=(`echo "$userstr" | cut -d':' -f 4 | tr ',' ' '`)
for (( i=0; i<${#listt[*]}; i++ ))
do
ctg="${listt[$i]}"
$(echo "iptables -A v-$ctg-t" -d $U_IP -j ACCEPT)
iptables -A v-$ctg-t -j RETURN
iptables -D v-$ctg-t -j RETURN
done
./editIt.sh insert /etc/hosts VERSA_DNS 0 "$U_IP ${1}${DNS_LONG_PREFIX} ${1}${DNS_SHORT_PREFIX}"
cd $OPWD
}
versa_clean() {
OPWD="$(pwd)"
cd $OPENVPN_PATH/versa
./editIt.sh clear /etc/hosts VERSA_DNS 0
systemctl restart dnsmasq
# Base Rule
iptables -t nat -D POSTROUTING -s $SUBNET_CIDR -o $GATEWAY_INTERFACE -j MASQUERADE 2>/dev/null
# Delete Chains
iptables -D FORWARD -s $SUBNET_CIDR -j versa-vlan 2>/dev/null
iptables -D FORWARD -d $SUBNET_CIDR -m state --state ESTABLISHED,RELATED -j ACCEPT 2>/dev/null
#iptables -D FORWARD -s $SUBNET_CIDR -j DROP 2>/dev/null
iptables -D INPUT -s $SUBNET_CIDR -j versa-server 2>/dev/null
#iptables -D INPUT -s $SUBNET_CIDR -j DROP 2>/dev/null
iptables -F versa-vlan 2>/dev/null
iptables -X versa-vlan 2>/dev/null
iptables -F versa-server 2>/dev/null
iptables -X versa-server 2>/dev/null
ochains=(`iptables -nL | sed -nr 's/^Chain (v\-[A-Za-z0-9\_]*\-a) .*$/\1/p' | tr '\n' ' '`)
count=${#ochains[*]}
for (( i=0; i<$count; i++ ))
do
ch="${ochains[$i]}"
iptables -F $ch
iptables -X $ch
done
ochains=(`iptables -nL | sed -nr 's/^Chain (v\-[A-Za-z0-9\_]*\-t) .*$/\1/p' | tr '\n' ' '`)
count=${#ochains[*]}
for (( i=0; i<$count; i++ ))
do
ch="${ochains[$i]}"
iptables -F $ch
iptables -X $ch
done
ochains=(`iptables -nL | sed -nr 's/^Chain (v\-[A-Za-z0-9\_]+\-s) .*$/\1/p' | tr '\n' ' '`)
count=${#ochains[*]}
for (( i=0; i<$count; i++ ))
do
ch="${ochains[$i]}"
iptables -F $ch
iptables -X $ch
done
# Delete conf rules
if [ -n "$(ls -A ../ccd)" ]; then
for filename in ../ccd/*; do
./editIt.sh clear $filename VERSA_CONF 0
done
fi
# Delete custom rules
count=${#GROUP_NAME[*]}
for (( i=0; i<$count; i++ ))
do
[[ -n ${GROUP_CUSTOM_CLEAN[$i]} ]] && eval ${GROUP_CUSTOM_CLEAN[$i]}
[[ -n ${GROUP_CUSTOM_FINALIZE[$i]} ]] && eval ${GROUP_CUSTOM_FINALIZE[$i]}
done
cd $OPWD
}
versa_build() {
[[ $GROUPS_ENABLED -eq 0 ]] && echo -e "\033[31mGroupsystem deactivated.\033[0m" && return 1
OPWD="$(pwd)"
cd $OPENVPN_PATH/versa
iptables -t nat -A POSTROUTING -s $SUBNET_CIDR -o $GATEWAY_INTERFACE -j MASQUERADE
iptables -N versa-vlan
iptables -N versa-server