-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
easytls-client-connect.sh
executable file
·1396 lines (1232 loc) · 39.1 KB
/
easytls-client-connect.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/sh
EASYTLS_VERSION="2.8.0"
# Copyright - negotiable
#
# VERBATUM_COPYRIGHT_HEADER_INCLUDE_NEGOTIABLE
# easytls-client-connect.sh -- Do simple magic
#
# Copyright (C) 2020 Richard Bonhomme (Friday 13th of March 2020)
# https://github.com/TinCanTech/easy-tls
# All Rights reserved.
#
# This code is released under version 2 of the GNU GPL
# See LICENSE of this project for full licensing details.
#
# VERBATUM_COPYRIGHT_HEADER_INCLUDE_NEGOTIABLE
#
# Help
help_text ()
{
help_msg="
easytls-client-connect.sh
This script is intended to be used by tls-crypt-v2 client keys
generated by EasyTLS. See: https://github.com/TinCanTech/easy-tls
Options:
help|-h|--help This help text.
-V|--version
-v|--verbose Be a lot more verbose at run time (Not Windows).
-s|--source-vars=<FILENAME>
Force Easy-TLS to source a vars file.
The default vars file is sourced if no FILENAME is given.
-a|--allow-no-check If the key has a hardware-address configured
and the client did NOT use --push-peer-info
then allow the connection. Otherwise, keys with a
hardware-address MUST use --push-peer-info.
-M|--ignore-x509-mismatch
Ignore tlskey-x509 vs openvpn-x509 mismatch.
-m|--ignore-hw-mismatch
Ignore tlskey-hwaddr vs openvpn-hwaddr mismatch.
-p|--push-required Require all clients to use --push-peer-info.
-c|--crypt-v2-required Require all clients to use a TLS-Crypt-V2 key.
-k|--key-hw-required Require all client keys to have a hardware-address.
-i|--client-ip-match Match client source IP to Key metadata.
-d|--dyn-opts=<FILE> Path and name of Openvpn client dynamic options file.
-w|--work-dir=<DIR> Path to Easy-TLS scripts and vars for this server.
-t|--tmp-dir=<DIR> Temp directory where server-scripts write data.
Default: *nix /tmp/easytls
Windows C:/Windows/Temp/easytls
-b|--base-dir=<DIR> Path to OpenVPN base directory. (Windows Only)
Default: C:/Progra~1/OpenVPN
-o|--ovpnbin-dir=<DIR> Path to OpenVPN bin directory. (Windows Only)
Default: C:/Progra~1/OpenVPN/bin
-e|--ersabin-dir=<DIR> Path to Easy-RSA3 bin directory. (Windows Only)
Default: C:/Progra~1/Openvpn/easy-rsa/bin
Exit codes:
0 - Allow connection, Client hwaddr is correct or not required.
2 - Disallow connection, pushed hwaddr does not match.
3 - Disallow connection, hwaddr required and not pushed.
4 - Disallow connection, hwaddr required and not keyed.
5 - Disallow connection, Kill client.
6 - Disallow connection, TLS Auth/Crypt-v1 banned
7 - Disallow connection, X509 certificate incorrect for this TLS-key.
8 - Disallow connection, missing X509 client cert serial. (BUG)
9 - Disallow connection, unexpected failure. (BUG)
12 - Disallow connection, source IPaddr does not match.
18 - BUG Disallow connection, failed to read client_md_file_stack
19 - BUG Disallow connection, failed to parse metadata string
21 - USER ERROR Disallow connection, options error.
60 - USER ERROR Disallow connection, missing Temp dir
61 - USER ERROR Disallow connection, missing Base dir
62 - USER ERROR Disallow connection, missing Easy-RSA bin dir
63 - USER ERROR Disallow connection, missing Openvpn bin dir
64 - USER ERROR Disallow connection, missing openssl.exe
65 - USER ERROR Disallow connection, missing cat.exe
66 - USER ERROR Disallow connection, missing date.exe
67 - USER ERROR Disallow connection, missing grep.exe
68 - USER ERROR Disallow connection, missing sed.exe
69 - USER ERROR Disallow connection, missing printf.exe
70 - USER ERROR Disallow connection, missing rm.exe
71 - USER ERROR Disallow connection, missing metadata.lib
77 - BUG Disallow connection, failed to sources vars file
160 - BUG Disallow connection, stack error
253 - Disallow connection, exit code when --help is called.
254 - BUG Disallow connection, fail_and_exit() exited with default error code.
255 - BUG Disallow connection, die() exited with default error code.
"
print "${help_msg}"
# For secrity, --help must exit with an error
exit 253
}
# Wrapper around 'printf' - clobber 'print' since it's not POSIX anyway
# shellcheck disable=SC1117
print () { "${EASYTLS_PRINTF}" '%s\n' "${1}"; }
verbose_print ()
{
[ -n "${EASYTLS_VERBOSE}" ] || return 0
print "${1}"
print ''
}
banner ()
{
[ -n "${EASYTLS_VERBOSE}" ] || return 0
"${EASYTLS_PRINTF}" '\n%s\n\n' "${1}"
}
# Set the Easy-TLS version
easytls_version ()
{
verbose_print
print "Easy-TLS version: ${EASYTLS_VERSION}"
verbose_print
} # => easytls_version ()
# Exit on error
die ()
{
# TLSKEY connect log
tlskey_status "FATAL" || update_status "tlskey_status FATAL"
easytls_version
verbose_print "<ERROR> ${status_msg}"
[ -z "${help_note}" ] || print "${help_note}"
[ -z "${failure_msg}" ] || print "${failure_msg}"
[ -z "${err_msg}" ] || print "${err_msg}"
print "ERROR: ${1}"
[ -z "${EASYTLS_FOR_WINDOWS}" ] || "${EASYTLS_PRINTF}" "%s\n%s\n" \
"<ERROR> ${status_msg}" "ERROR: ${1}" > "${EASYTLS_WLOG}"
#exit "${2:-255}"
if [ -n "${ENABLE_KILL_SERVER}" ]; then
echo 1 > "${temp_stub}-die"
echo 'XXXXX CC XXXXX KILL SERVER'
if [ -n "${EASYTLS_FOR_WINDOWS}" ]; then
"${EASYTLS_PRINTF}" "%s\n%s\n" \
"<ERROR> ${status_msg}" "ERROR: ${1}" > "${EASYTLS_WLOG}"
taskkill /F /PID "${EASYTLS_srv_pid}"
else
kill -15 "${EASYTLS_srv_pid}"
fi
fi
exit "${2:-255}"
} # => die ()
# failure not an error
fail_and_exit ()
{
delete_metadata_files
# stack_down does not errot to fail_and_exit, no need to remove lock
#stack_down || die "stack_down - fail_and_exit"
# Unlock
#release_lock "${easytls_lock_stub}-stack.d" || \
# die "release_lock:stack FAIL" 99
#update_status "stack-lock-released"
print "<FAIL> ${status_msg}"
print "${failure_msg}"
print "${1} ${2}"
# TLSKEY connect log
tlskey_status "!*! FAIL" || update_status "tlskey_status FAIL"
[ -z "${EASYTLS_FOR_WINDOWS}" ] || "${EASYTLS_PRINTF}" "%s\n%s\n" \
"<FAIL> ${status_msg}" "${failure_msg}" "${1} ${2}" > "${EASYTLS_WLOG}"
exit "${2:-254}"
} # => fail_and_exit ()
# Delete all metadata files
delete_metadata_files ()
{
# shellcheck disable=SC2154 # auth_control_file
"${EASYTLS_RM}" -f "${EASYTLS_KILL_FILE}"
# stack_down takes care of "${client_md_file_stack}"
update_status "temp-files deleted"
} # => delete_metadata_files ()
# Log fatal warnings
warn_die ()
{
if [ -n "${1}" ]; then
fatal_msg="${fatal_msg}
${1}"
else
[ -z "${fatal_msg}" ] || die "${fatal_msg}" 21
fi
} # => warn_die ()
# Update status message
update_status ()
{
status_msg="${status_msg} => ${*}"
} # => update_status ()
# Remove colons ':' and up-case
format_number ()
{
"${EASYTLS_PRINTF}" '%s' "${1}" | \
"${EASYTLS_SED}" -e 's/://g' -e 'y/abcdef/ABCDEF/'
} # => format_number ()
#=# 9273398a-5284-4c1f-aec5-d597ceb1d085
# Verbose message
verbose_easytls_tctip_lib ()
{
[ -z "${EASYTLS_SILENT}" ] || return 0
[ -n "${EASYTLS_TCTIP_LIB_VERBOSE}" ] || return 0
"${EASYTLS_PRINTF}" '%s\n' "${1}"
} # => verbose_easytls_tctip_lib ()
# Front end validate IP address
validate_ip_address ()
{
[ "${1}" = "${1%%.*}" ] || ipv4=1
[ "${1}" = "${1%%:*}" ] || ipv6=1
[ -n "${ipv4}${ipv6}" ] || return 1
if [ -n "${ipv4}" ] && [ -n "${ipv6}" ]; then
easytls_verbose "Unsupported <:Port>"
return 1
fi
[ -n "${ipv4}" ] && validate_ip4_data "$@" && valid4=1
[ -n "${ipv6}" ] && validate_ip6_data "$@" && valid6=1
[ -n "${valid4}" ] && [ -n "${valid6}" ] && return 1
[ -z "${valid4}" ] && [ -z "${valid6}" ] && return 1
} # => validate_ip_address ()
# Exit with error
invalid_address ()
{
case "${1}" in
1) print "easytls invalid" ;;
10) print "excess input" ;;
11) print "illegal format" ;;
12) print "illegal mask" ;;
13) print "mask range" ;;
14) print "leading zero" ;;
15) print "class range" ;;
16) print "class count" ;;
17) print "cidr mask" ;;
18) print "input error" ;;
19) print "ip2dec error" ;;
20)
print "ip6/mask-length mismatch"
print "ip6/mask correct example: 1:22:333:4444:5::/80"
;;
*) print "undocumented ${1} ?" ;;
esac
} # => invalid_address ()
# IPv4 address to decimal
ip2dec ()
{
case "${1}" in
*[!1234567890.]* | .* | *. | *..* ) return 1 ;;
*.*.*.* ) : ;; #OK
* ) return 1 ;;
esac
temp_ip_addr="${1}"
a="${temp_ip_addr%%.*}"; temp_ip_addr="${temp_ip_addr#*.}"
b="${temp_ip_addr%%.*}"; temp_ip_addr="${temp_ip_addr#*.}"
c="${temp_ip_addr%%.*}"; temp_ip_addr="${temp_ip_addr#*.}"
d="${temp_ip_addr%%.*}"
for i in "${a}" "${b}" "${c}" "${d}"; do
[ "${#i}" -gt 1 ] || continue
[ -n "${i%%0*}" ] || return 1
if [ 0 -gt "${i}" ] || [ "${i}" -gt 255 ]; then return 1; fi
done
ip4_dec="$(( (a << 24) + (b << 16) + (c << 8) + d ))" || return 1
unset -v temp_ip_addr a b c d
} # => ip2dec ()
# IPv4 CIDR mask length to decimal
cidrmask2dec ()
{
mask_dec=0
imsk_dec=0
count=32 # or 128 - If possible..
power=1
while [ "${count}" -gt 0 ]; do
count="$(( count - 1 ))"
if [ "${1}" -gt "${count}" ]; then
# mask
mask_dec="$(( mask_dec + power ))"
else
# inverse
imsk_dec="$(( imsk_dec + power ))"
fi
power="$(( power * 2 ))"
done
unset -v count power
} # => cidrmask2dec ()
# EXPAND IPv6
expand_ip6_address ()
{
[ -z "${2}" ] || return 10
in_ip_addr="${1}"
shift
in_valid_hextets="${in_ip_addr%/*}"
in_valid_mask_len="${in_ip_addr##*/}"
unset -v in_ip_addr
# mask length
case "${in_valid_mask_len}" in
"${in_valid_hextets}" | '' ) in_valid_mask_len=128 ;;
[!1234567890] | 0* ) return 11 ;;
* ) : # OK
esac
if [ 0 -gt "${in_valid_mask_len}" ] || [ "${in_valid_mask_len}" -gt 128 ]
then
return 11
fi
# ADDRESS 6
temp_valid_hextets="${in_valid_hextets}"
# expand leading colon
[ "${temp_valid_hextets}" = "${temp_valid_hextets#:}" ] || \
lead_colon=1
[ -z "${lead_colon}" ] || temp_valid_hextets="0${temp_valid_hextets}"
# Count valid compressed hextets
count_valid_hextets=0
while [ -n "${temp_valid_hextets}" ]; do
count_valid_hextets="$(( count_valid_hextets + 1 ))"
if [ "${temp_valid_hextets}" = "${temp_valid_hextets#*:}" ]; then
temp_valid_hextets="${temp_valid_hextets}:"
fi
temp_valid_hextets="${temp_valid_hextets#*:}"
temp_valid_hextets="${temp_valid_hextets#:}"
done
verbose_easytls_tctip_lib "count_valid_hextets: ${count_valid_hextets}"
# expand double colon
temp_valid_hextets="${in_valid_hextets}"
expa_valid_hextets="${in_valid_hextets}"
if [ "${count_valid_hextets}" -lt 8 ]; then
hi_part="${temp_valid_hextets%::*}"
lo_part="${temp_valid_hextets#*::}"
missing_zeros="$(( 8 - count_valid_hextets ))"
while [ "${missing_zeros}" -gt 0 ]; do
hi_part="${hi_part}:0"
missing_zeros="$(( missing_zeros - 1 ))"
done
unset -v missing_zeros
expa_valid_hextets="${hi_part}:${lo_part}"
# Re-expand leading colon
[ -z "${lead_colon}" ] || expa_valid_hextets="0${expa_valid_hextets}"
fi
# Save the orangutan
unset -v lead_colon lo_part hi_part count_valid_hextets
verbose_easytls_tctip_lib "expa_valid_hextets: ${expa_valid_hextets}"
temp_valid_hextets="${expa_valid_hextets}"
hex_count=8
unset -v full_valid_hextets delim
# Expand compressed zeros
while [ "${hex_count}" -gt 0 ]; do
hextet="${temp_valid_hextets%%:*}"
while [ "${#hextet}" -lt 4 ]; do
hextet="0${hextet}"
done
full_valid_hextets="${full_valid_hextets}${delim}${hextet}"
delim=':'
temp_valid_hextets="${temp_valid_hextets#*:}"
hex_count="$(( hex_count - 1 ))"
done
# Save "The violence inherent in the system"
unset -v hex_count delim
verbose_easytls_tctip_lib "full_valid_hextets: ${full_valid_hextets}"
# Split IP at mask_len
[ "$(( in_valid_mask_len % 4 ))" -eq 0 ] || \
die "in_valid_mask_len % 4: ${in_valid_mask_len}"
hex_mask="$(( in_valid_mask_len / 4 ))"
temp_valid_hextets="${full_valid_hextets}"
while [ "${hex_mask}" -gt 0 ]; do
delete_mask="${temp_valid_hextets#?}"
verbose_easytls_tctip_lib "delete_mask: ${delete_mask}"
hex_char="${temp_valid_hextets%"${delete_mask}"}"
verbose_easytls_tctip_lib "hex_char: ${hex_char}"
temp_valid_hextets="${temp_valid_hextets#?}"
verbose_easytls_tctip_lib "temp_valid_hextets: ${temp_valid_hextets}"
full_subnet_addr6="${full_subnet_addr6}${hex_char}"
verbose_easytls_tctip_lib "full_subnet_addr6: ${full_subnet_addr6}"
[ "${hex_char}" = ':' ] || hex_mask="$(( hex_mask - 1 ))"
verbose_easytls_tctip_lib "*** hex_mask: ${hex_mask}"
done
# Save the polar ice-caps
unset -v hex_char hex_mask delete_mask
# The remainder should equal zero
while [ -n "${temp_valid_hextets}" ]; do
hextet="${temp_valid_hextets%%:*}"
if [ -z "${hextet}" ]; then
temp_valid_hextets="${temp_valid_hextets#*:}"
hextet="${temp_valid_hextets%%:*}"
fi
if [ "${temp_valid_hextets}" = "${temp_valid_hextets#*:}" ]; then
temp_valid_hextets="${temp_valid_hextets}:"
fi
temp_valid_hextets="${temp_valid_hextets#*:}"
# shellcheck disable=SC2249 # (info): default *) case
case "${hextet}" in
*[!0:]* ) return 20 ;;
esac
done
verbose_easytls_tctip_lib "full_valid_hextets: ${full_valid_hextets}"
verbose_easytls_tctip_lib "full_subnet_addr6: ${full_subnet_addr6}"
verbose_easytls_tctip_lib "temp_valid_hextets: ${temp_valid_hextets}"
# Save the trees
unset -v hextet temp_valid_hextets
# Return full_valid_hextets full_subnet_addr6
} # => expand_ip6_address ()
#=# b66633f8-3746-436a-901f-29638199b187
# Allow connection
connection_allowed ()
{
absolute_fail=0
update_status "connection allowed"
} # => connection_allowed ()
# Update conntrac
update_conntrac ()
{
# Source conntrac lib
lib_file="${EASYTLS_WORK_DIR}/easytls-conntrac.lib"
[ -f "${lib_file}" ] || \
lib_file="${EASYTLS_WORK_DIR}/dev/easytls-conntrac.lib"
# shellcheck source=./easytls-conntrac.lib
if [ -f "${lib_file}" ]; then
. "${lib_file}" || die "Source failed: ${lib_file}" 77
unset -v lib_file
else
die "Missing file: ${lib_file}" 77
fi
# Absolute start time
easytls_start_d_file="${EASYTLS_CONN_TRAC}-start-d"
if [ ! -f "${easytls_start_d_file}" ]; then
# shellcheck disable=SC2154 # daemon_start_time
"${EASYTLS_PRINTF}" '%s' \
"${daemon_start_time}" > "${easytls_start_d_file}"
fi
# shellcheck disable=SC2034
easytls_start_d="$("$EASYTLS_CAT" "${easytls_start_d_file}")"
# Begin conntrac_record
conntrac_record="${UV_TLSKEY_SERIAL:-TLSAC}=${client_serial}"
conntrac_record="${conntrac_record}==${common_name}"
# Detect IP Pool exhausted
# shellcheck disable=SC2154
if [ -z "${ifconfig_pool_remote_ip}" ]; then
# Kill the server
[ -z "${POOL_EXHAUST_FATAL}" ] || {
ENABLE_KILL_SERVER=1
die "IP_POOL_EXHASTED" 101
}
# Otherwise, the client will connect but get no IP
ip_pool_exhausted=1
conntrac_record="${conntrac_record}==0.0.0.0"
banner "********* WARNING: IP POOL EXHAUSTED *********"
# This will kill the client
[ -z "${POOL_EXHAUST_KILL_CLIENT}" ] || {
"${EASYTLS_CAT}" "${EASYTLS_DYN_OPTS_FILE}"
"${EASYTLS_PRINTF}" '%s\n' "disable"
} > "${ovpn_dyn_opts_file}"
else
conntrac_record="${conntrac_record}==${ifconfig_pool_remote_ip}"
fi
# shellcheck disable=SC2154
[ -z "${peer_id}" ] || conntrac_record="${conntrac_record}==${peer_id}"
# shellcheck disable=SC2154
timestamp="${local_date_ascii}=${local_time_ascii}"
conntrac_record="${conntrac_record}==${timestamp}"
# shellcheck disable=SC2154
conntrac_record="${conntrac_record}++${untrusted_ip}:${untrusted_port}"
conn_trac_connect "${conntrac_record}" "${EASYTLS_CONN_TRAC}" || {
case "$?" in
6) # Duplicate TLSKEY
update_status "conn_trac_connect DUPLICATE_TLSKEY"
conntrac_dupl=1
;;
2) # Duplicate record, includes VPN-IP 0.0.0.0 (Pool exhausted)
update_status "conn_trac_connect FAIL"
conntrac_fail=1
;;
1) # Fatal because these are usage errors
update_status "conn_trac_connect ERROR"
conntrac_error=1
;;
9) # Absolutely fatal
ENABLE_KILL_SERVER=1
die "CONNTRAC_CONNECT_CT_LOCK_9" 96
;;
*) # Absolutely fatal
conntrac_unknown=1
;;
esac
}
# Log failure
if [ -n "${conntrac_dupl}" ] || [ -n "${conntrac_fail}" ] || \
[ -n "${conntrac_error}" ] ||[ -n "${conntrac_unknown}" ]
then
if [ -n "${ENABLE_CONNTRAC_FAIL_LOG}" ]; then
{
[ ! -f "${EASYTLS_CONN_TRAC}.fail" ] || \
"${EASYTLS_CAT}" "${EASYTLS_CONN_TRAC}.fail"
"${EASYTLS_PRINTF}" '%s ' "${timestamp}"
[ -z "${conntrac_fail}" ] || \
"${EASYTLS_PRINTF}" '%s ' "Pre-Reg"
[ -z "${conntrac_error}" ] || \
"${EASYTLS_PRINTF}" '%s ' "ERROR"
[ -z "${ip_pool_exhausted}" ] || \
"${EASYTLS_PRINTF}" '%s ' "IP-POOL"
[ -z "${conntrac_dupl}" ] || \
"${EASYTLS_PRINTF}" '%s ' "DUPL-TLSK"
[ -z "${conntrac_unknown}" ] || \
"${EASYTLS_PRINTF}" '%s ' "UNKNOWN!"
"${EASYTLS_PRINTF}" '%s\n' "CON: ${conntrac_record}"
} > "${EASYTLS_CONN_TRAC}.fail.tmp" || die "conn: conntrac file" 156
"${EASYTLS_MV}" "${EASYTLS_CONN_TRAC}.fail.tmp" \
"${EASYTLS_CONN_TRAC}.fail" || die "conn: conntrac file" 157
fi # ENABLE_CONNTRAC_FAIL_LOG
# Absolutely fatal
[ -z "${conntrac_unknown}" ] || {
ENABLE_KILL_SERVER=1
die "CONNTRAC_CONNECT_UNKNOWN" 98
}
# # Fatal because these are usage errors
if [ -n "${conntrac_error}" ] && [ -n "${FATAL_CONN_TRAC}" ]; then
ENABLE_KILL_SERVER=1
die "CONNTRAC_CONNECT_ERROR" 99
fi
# Duplicate record, includes VPN-IP 0.0.0.0 (Pool exhausted)
if [ -n "${conntrac_fail}" ] && [ -n "${FATAL_CONN_TRAC_2}" ]; then
ENABLE_KILL_SERVER=1
die "CONNTRAC_CONNECT_FAIL_2" 91
fi
# Duplicate TLS keys
if [ -n "${conntrac_dupl}" ]; then
[ -z "${ENFORCE_UNIQUE_TLSKEY}" ] || \
fail_and_exit "Duplicate TLS Key"
update_status "IGNORE Duplicate TLS Key"
fi
fi
unset -v env_file conntrac_record conntrac_fail conntrac_error
} # => update_contrac ()
# Stack down
stack_down ()
{
[ -z "${stack_completed}" ] || die "STACK_DOWN CAN ONLY RUN ONCE" 161
stack_completed=1
# file exists or the client pushed an incorrect UV_TLSKEY_SERIAL
[ -f "${client_md_file_stack}" ] || return 0
# Lock
acquire_lock "${easytls_lock_stub}-stack.d" || \
die "acquire_lock:stack FAIL" 99
update_status "stack-lock-acquired"
unset -v stack_err
i=0
s=''
# Full Stack DOWN
while :
do
i="$(( i + 1 ))"
if [ -f "${client_md_file_stack}_${i}" ]; then
[ "${i}" -eq 1 ] || s="${s}."
else
if [ "${i}" -eq 1 ]; then
# There are no stacked files so delete the original
[ -f "${client_md_file_stack}" ] || die "***" 163
"${EASYTLS_RM}" "${client_md_file_stack}" || stack_err=1
update_status "stack-down: clear"
tlskey_status " | = stack: clear -"
else
# Delete the last file found
p="$(( i - 1 ))"
[ -f "${client_md_file_stack}_${p}" ] || die "_i***" 164
"${EASYTLS_RM}" "${client_md_file_stack}_${p}" || stack_err=1
update_status "stack-down: ${p}"
tlskey_status " | <= stack:- ${s}${p} -"
fi
break
fi
done
# Unlock
release_lock "${easytls_lock_stub}-stack.d" || \
die "release_lock:stack FAIL" 99
update_status "stack-lock-released"
[ -z "${stack_err}" ] || die "STACK_DOWN_FULL_ERROR" 160
} # => stack_down ()
# TLSKEY tracking .. because ..
tlskey_status ()
{
# >> may fail on easytls/github/actions/wtest - No TERM
[ -n "${EASYTLS_TLSKEY_STATUS}" ] || return 0
{
# shellcheck disable=SC2154
"${EASYTLS_PRINTF}" '%s %s %s %s\n' "${local_date_ascii}" \
"${UV_TLSKEY_SERIAL:-TLSAC}" "CONN:${1}" \
"${common_name} ${UV_REAL_NAME}"
} >> "${EASYTLS_TK_XLOG}"
} # => tlskey_status ()
# Retry pause
retry_pause ()
{
if [ -n "${EASYTLS_FOR_WINDOWS}" ]; then
ping -n 1 127.0.0.1
else
sleep 1
fi
} # => retry_pause ()
# Simple lock dir
acquire_lock ()
{
[ -n "${1}" ] || return 1
unset -v lock_acquired
lock_attempt="${LOCK_TIMEOUT}"
set -o noclobber
while [ "${lock_attempt}" -gt 0 ]; do
[ "${lock_attempt}" -eq "${LOCK_TIMEOUT}" ] || retry_pause
lock_attempt="$(( lock_attempt - 1 ))"
"${EASYTLS_MKDIR}" "${1}" || continue
lock_acquired=1
break
done
set +o noclobber
[ -n "${lock_acquired}" ] || return 1
} # => acquire_lock ()
# Release lock
release_lock ()
{
[ -d "${1}" ] || return 0
"${EASYTLS_RM}" -r "${1}"
} # => release_lock ()
# easytls-metadata.lib
#=# 35579017-b084-4d6b-94d5-76397c2d4a1f
# Break metadata_string into variables
# shellcheck disable=SC2034 # foo appears unused. Verify it or export it.
metadata_string_to_vars ()
{
MD_TLSKEY_SERIAL="${1%%-*}" || return 1
#seed="${*}" || return 1
#MD_SEED="${seed#*-}" || return 1
#unset -v seed
#md_padding="${md_seed%%--*}" || return 1
md_easytls_ver="${1#*--}" || return 1
MD_EASYTLS="${md_easytls_ver%-*}" || return 1
unset -v md_easytls_ver
MD_IDENTITY="${2%%-*}" || return 1
MD_SRV_NAME="${2##*-}" || return 1
MD_x509_SERIAL="${3}" || return 1
MD_DATE="${4}" || return 1
MD_CUSTOM_G="${5}" || return 1
MD_NAME="${6}" || return 1
MD_SUBKEY="${7}" || return 1
MD_OPT="${8}" || return 1
MD_FILTERS="${9}" || return 1
} # => metadata_string_to_vars ()
# Break metadata string at delimeter: New Newline, old space
# shellcheck disable=SC2034 # foo appears unused. Verify it or export it.
metadata_stov_safe ()
{
[ -n "$1" ] || return 1
input="$1"
# Not using IFS
err_msg="Unspecified delimiter"
delim_save="${delimiter}"
delimiter="${delimiter:-${newline}}"
[ -n "${delimiter}" ] || return 1
case "${input}" in
*"${delimiter}"*) : ;;
*) delimiter=' '
esac
MD_SEED="${input#*-}"
# Expansions inside ${..} need to be quoted separately,
# otherwise they will match as a pattern.
# Which is the required behaviour.
# shellcheck disable=SC2295
{ # Required group for shellcheck
m1="${input%%${delimiter}*}"
input="${input#*${delimiter}}"
m2="${input%%${delimiter}*}"
input="${input#*${delimiter}}"
m3="${input%%${delimiter}*}"
input="${input#*${delimiter}}"
m4="${input%%${delimiter}*}"
input="${input#*${delimiter}}"
m5="${input%%${delimiter}*}"
input="${input#*${delimiter}}"
m6="${input%%${delimiter}*}"
input="${input#*${delimiter}}"
m7="${input%%${delimiter}*}"
input="${input#*${delimiter}}"
m8="${input%%${delimiter}*}"
input="${input#*${delimiter}}"
m9="${input%%${delimiter}*}"
input="${input#*${delimiter}}"
}
# An extra space has been used, probably in the name
err_msg="metadata-lib: ${m9} vs ${input}"
[ "${m9}" = "${input}" ] || return 1
delimiter="${delim_save}"
err_msg="metadata-lib: metadata_string_to_vars"
metadata_string_to_vars "$m1" "$m2" "$m3" "$m4" \
"$m5" "$m6" "$m7" "$m8" "$m9" || return 1
unset -v m1 m2 m3 m4 m5 m6 m7 m8 m9 input err_msg
} # => metadata_stov_safe ()
#=# 70b4ec32-f1fc-47fb-a261-f02e7f572b62
# Initialise
init ()
{
# Fail by design
absolute_fail=1
delimiter='
'
# Defaults
if [ -z "${EASYTLS_UNIT_TEST}" ]; then
EASYTLS_srv_pid="${PPID}"
else
EASYTLS_srv_pid=999
fi
# Log message
status_msg="* EasyTLS-client-connect"
# Identify Windows
# shellcheck disable=SC2016
EASYRSA_KSH='@(#)MIRBSD KSH R39-w32-beta14 $Date: 2013/06/28 21:28:57 $'
# shellcheck disable=SC2154
if [ "${KSH_VERSION}" = "${EASYRSA_KSH}" ]; then
EASYTLS_FOR_WINDOWS=1
fi
# Required binaries
EASYTLS_OPENSSL='openssl'
EASYTLS_CAT='cat'
EASYTLS_DATE='date'
EASYTLS_GREP='grep'
EASYTLS_MKDIR='mkdir'
EASYTLS_MV='mv'
EASYTLS_SED='sed'
EASYTLS_PRINTF='printf'
EASYTLS_RM='rm'
# Directories and files
if [ -n "${EASYTLS_FOR_WINDOWS}" ]; then
# Windows
host_drv="${PATH%%\:*}"
base_dir="${EASYTLS_base_dir:-${host_drv}:/Progra~1/Openvpn}"
EASYTLS_ersabin_dir="${EASYTLS_ersabin_dir:-${base_dir}/easy-rsa/bin}"
EASYTLS_ovpnbin_dir="${EASYTLS_ovpnbin_dir:-${base_dir}/bin}"
[ -d "${base_dir}" ] || exit 61
[ -d "${EASYTLS_ersabin_dir}" ] || exit 62
[ -d "${EASYTLS_ovpnbin_dir}" ] || exit 63
[ -f "${EASYTLS_ovpnbin_dir}/${EASYTLS_OPENSSL}.exe" ] || exit 64
[ -f "${EASYTLS_ersabin_dir}/${EASYTLS_CAT}.exe" ] || exit 65
[ -f "${EASYTLS_ersabin_dir}/${EASYTLS_DATE}.exe" ] || exit 66
[ -f "${EASYTLS_ersabin_dir}/${EASYTLS_GREP}.exe" ] || exit 67
[ -f "${EASYTLS_ersabin_dir}/${EASYTLS_MKDIR}.exe" ] || exit 72
[ -f "${EASYTLS_ersabin_dir}/${EASYTLS_MV}.exe" ] || exit 71
[ -f "${EASYTLS_ersabin_dir}/${EASYTLS_SED}.exe" ] || exit 68
[ -f "${EASYTLS_ersabin_dir}/${EASYTLS_PRINTF}.exe" ] || exit 69
[ -f "${EASYTLS_ersabin_dir}/${EASYTLS_RM}.exe" ] || exit 70
export PATH="${EASYTLS_ersabin_dir};${EASYTLS_ovpnbin_dir};${PATH}"
fi
} # => init ()
# Dependancies
deps ()
{
# Source vars file
prog_dir="${0%/*}"
EASYTLS_WORK_DIR="${EASYTLS_WORK_DIR:-${prog_dir}}"
default_vars="${EASYTLS_WORK_DIR}/easytls-client-connect.vars"
EASYTLS_VARS_FILE="${EASYTLS_VARS_FILE:-${default_vars}}"
if [ -f "${EASYTLS_VARS_FILE}" ]; then
# .vars-example is correct for shellcheck
# shellcheck source=examples/easytls-client-connect.vars-example
. "${EASYTLS_VARS_FILE}" || die "Source failed: ${EASYTLS_VARS_FILE}" 77
update_status "vars loaded"
else
[ -z "${EASYTLS_REQUIRE_VARS}" ] || \
die "Missing file: ${EASYTLS_VARS_FILE}" 77
fi
# Source metadata lib
lib_file="${EASYTLS_WORK_DIR}/easytls-metadata.lib"
[ -f "${lib_file}" ] || \
lib_file="${EASYTLS_WORK_DIR}/dev/easytls-metadata.lib"
if [ -f "${lib_file}" ]; then
# shellcheck source=dev/easytls-metadata.lib
. "${lib_file}" || die "Failed to source: ${lib_file}"
easytls_metadata_lib_ver
fi
# Source tctip lib
lib_file="${EASYTLS_WORK_DIR}/easytls-tctip.lib"
[ -f "${lib_file}" ] || \
lib_file="${EASYTLS_WORK_DIR}/dev/easytls-tctip.lib"
if [ -f "${lib_file}" ]; then
# shellcheck source=dev/easytls-tctip.lib
. "${lib_file}" || die "Failed to source: ${lib_file}"
easytls_tctip_lib_ver
fi
unset -v default_vars EASYTLS_VARS_FILE EASYTLS_REQUIRE_VARS prog_dir lib_file
if [ -n "${EASYTLS_FOR_WINDOWS}" ]; then
WIN_TEMP="${host_drv}:/Windows/Temp"
export EASYTLS_tmp_dir="${EASYTLS_tmp_dir:-${WIN_TEMP}}"
else
export EASYTLS_tmp_dir="${EASYTLS_tmp_dir:-/tmp}"
fi
# Test temp dir
[ -d "${EASYTLS_tmp_dir}" ] || exit 60
# Temp files name stub
temp_stub="${EASYTLS_tmp_dir}/easytls-${EASYTLS_srv_pid}"
# Lock dir
easytls_lock_stub="${temp_stub}-lock"
LOCK_TIMEOUT="${LOCK_TIMEOUT:-30}"
# Need the date/time ..
#full_date="$("${EASYTLS_DATE}" '+%s %Y/%m/%d-%H:%M:%S')"
# shellcheck disable=SC2154
full_date="${time_ascii}"
local_date_ascii="${full_date% *}"
local_time_ascii="${full_date#* }"
# Windows log
EASYTLS_WLOG="${temp_stub}-client-connect.log"
EASYTLS_TK_XLOG="${temp_stub}-tcv2-ct.x-log"
# Conn track
EASYTLS_CONN_TRAC="${temp_stub}-conn-trac"
# Kill server file
if [ -f "${temp_stub}-die" ]; then
print "Kill Server Signal -> exit CC"
exit 9
fi
# Kill client file
EASYTLS_KILL_FILE="${temp_stub}-kill-client"
# Dynamic opts file
if [ -f "${EASYTLS_DYN_OPTS_FILE}" ] && [ -n "${ovpn_dyn_opts_file}" ]
then
"${EASYTLS_CAT}" "${EASYTLS_DYN_OPTS_FILE}" > "${ovpn_dyn_opts_file}"
update_status "dyn opts loaded"
fi
}
#######################################
# Initialise
init
# Options
while [ -n "${1}" ]; do
# Separate option from value:
opt="${1%%=*}"
val="${1#*=}"
empty_ok="" # Empty values are not allowed unless expected
case "${opt}" in
help|-h|--help)
empty_ok=1
help_text
;;
-V|--version)
easytls_version
exit 9
;;
-v|--verbose)
empty_ok=1
EASYTLS_VERBOSE=1
;;
-s|--source-vars)
empty_ok=1
EASYTLS_REQUIRE_VARS=1
case "${val}" in
-s|--source-vars)
unset -v EASYTLS_VARS_FILE ;;
*)
EASYTLS_VARS_FILE="${val}" ;;
esac
;;
-a|--allow-no-check)
empty_ok=1
ENABLE_NO_CHECK=1
;;
-m|--ignore-hw-mismatch) # tlskey-hwaddr does not match openvpn-hwaddr
empty_ok=1
IGNORE_HWADDR_MISMATCH=1
;;
-M|--ignore-x509-mismatch) # tlskey-x509 does not match openvpn-x509
empty_ok=1
IGNORE_X509_MISMATCH=1
;;
-p|--push-hwaddr-required)
empty_ok=1
ENFORCE_PUSH_HWADDR=1
;;
-c|--crypt-v2-required)
empty_ok=1
ENFORCE_CRYPT_V2=1
;;
-k|--key-hwaddr-required)
empty_ok=1
ENFORCE_KEY_HWADDR=1
;;
-i|--client-ip-match)
empty_ok=1
PEER_IP_MATCH=1
;;
-d|--dyn-opts)
EASYTLS_DYN_OPTS_FILE="${val}"
[ -f "${EASYTLS_DYN_OPTS_FILE}" ] || \
warn_die "Easy-TLS dynamic opts file missing"
;;
-w|--work-dir)
EASYTLS_WORK_DIR="${val}"
;;
-t|--tmp-dir)
EASYTLS_tmp_dir="${val}"
;;
-b|--base-dir)
EASYTLS_base_dir="${val}"
;;
-o|--openvpn-bin-dir)
EASYTLS_ovpnbin_dir="${val}"
;;