-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlocal-https.sh
More file actions
2146 lines (1801 loc) · 61.2 KB
/
local-https.sh
File metadata and controls
2146 lines (1801 loc) · 61.2 KB
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2024-2026 Luiz Bizzio
set -euo pipefail
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
STEP_DELAY="${STEP_DELAY:-0.6}"
NONINTERACTIVE="${LOCAL_HTTPS_NONINTERACTIVE:-0}"
VERBOSE="${LOCAL_HTTPS_VERBOSE:-0}"
BOOTSTRAP="${LOCAL_HTTPS_BOOTSTRAP:-0}"
SAN_MAX_IPS="${LOCAL_HTTPS_SAN_MAX_IPS:-20}"
ADD_SUDO_USER_TO_CERT_GROUP="${LOCAL_HTTPS_ADD_SUDO_USER_TO_CERT_GROUP:-0}"
TECH_INSECURE_TLS="${LOCAL_HTTPS_TECH_INSECURE_TLS:-0}"
case "$TECH_INSECURE_TLS" in 1|true|TRUE|yes|YES) TECH_INSECURE_TLS=1 ;; *) TECH_INSECURE_TLS=0 ;; esac
case "$NONINTERACTIVE" in 1|true|TRUE|yes|YES) NONINTERACTIVE=1 ;; *) NONINTERACTIVE=0 ;; esac
case "$VERBOSE" in 1|true|TRUE|yes|YES) VERBOSE=1 ;; *) VERBOSE=0 ;; esac
case "$BOOTSTRAP" in 1|true|TRUE|yes|YES) BOOTSTRAP=1 ;; *) BOOTSTRAP=0 ;; esac
case "$ADD_SUDO_USER_TO_CERT_GROUP" in 1|true|TRUE|yes|YES) ADD_SUDO_USER_TO_CERT_GROUP=1 ;; *) ADD_SUDO_USER_TO_CERT_GROUP=0 ;; esac
AUTO_PIHOLE="${LOCAL_HTTPS_AUTO_PIHOLE:-}"
case "$AUTO_PIHOLE" in
1|true|TRUE|yes|YES) AUTO_PIHOLE=1 ;;
0|false|FALSE|no|NO) AUTO_PIHOLE=0 ;;
"")
if [ "$BOOTSTRAP" -eq 1 ]; then
AUTO_PIHOLE=1
else
AUTO_PIHOLE=0
fi
;;
*) AUTO_PIHOLE=0 ;;
esac
[ ! -t 0 ] && NONINTERACTIVE=1
pause_step() { [ "$NONINTERACTIVE" -eq 1 ] && return 0; [ ! -t 1 ] && return 0; sleep "$STEP_DELAY"; }
out() { printf '%b\n' "$1"; pause_step; }
vout() { [ "$VERBOSE" -eq 1 ] && out "$1" || true; }
die() { printf '%b\n' "\033[31m[ERROR]\033[0m $1" >&2; exit 1; }
SCRIPT_CMD_NAME="local-https"
INSTALL_PATH="/usr/local/sbin/local-https"
SCRIPT_SOURCE_URL_DEFAULT="https://raw.githubusercontent.com/luizbizzio/local-https/main/local-https.sh"
SCRIPT_SOURCE_URL="${LOCAL_HTTPS_SOURCE_URL:-$SCRIPT_SOURCE_URL_DEFAULT}"
SSL_DIR="/etc/ssl/servercerts"
CERT_GROUP="certs"
CA_KEY="$SSL_DIR/rootCA.key"
CA_CRT="$SSL_DIR/rootCA.crt"
CA_SRL="$SSL_DIR/rootCA.srl"
SERVER_KEY="$SSL_DIR/server.key"
SERVER_CSR="$SSL_DIR/server.csr"
SERVER_CRT="$SSL_DIR/server.crt"
SERVER_PEM="$SSL_DIR/server.pem"
SERVER_PFX="$SSL_DIR/server.pfx"
PFX_PASS_FILE="$SSL_DIR/.pfx-pass"
RENEW_WINDOW_SECONDS=$((7*24*3600))
CA_EC_CURVE="prime256v1"
SERVER_EC_CURVE="prime256v1"
PIHOLE_TOML="/etc/pihole/pihole.toml"
PIHOLE_FTL_CERT=""
PIHOLE_PRESENT=0
TECH_PRESENT=0
TECH_BASE_URL=""
TECH_SERVICE=""
FILTERED_IPS=""
TAILSCALE_DNS=""
TAILSCALE_SHORT=""
STATE_DIR="/var/lib/local-https"
STATE_FILE="$STATE_DIR/state.env"
INSTALL_MARKER="$STATE_DIR/installed"
SYSTEMD_SERVICE_PATH="/etc/systemd/system/local-https-renew.service"
SYSTEMD_TIMER_PATH="/etc/systemd/system/local-https-renew.timer"
CRON_LOG_PATH="/var/log/local-https-renew.log"
LOGROTATE_PATH="/etc/logrotate.d/local-https-renew"
AUTORENEW_METHOD="none"
CERT_RENEWED=0
FORCE_RENEW=0
UNINSTALL_YES=0
UNINSTALL_PURGE=0
has_systemctl() { command -v systemctl >/dev/null 2>&1; }
has_service_cmd() { command -v service >/dev/null 2>&1; }
need_cmd() { command -v "$1" >/dev/null 2>&1; }
has_logger() { command -v logger >/dev/null 2>&1; }
sh_quote() {
local s="$1"
printf "'%s'" "$(printf '%s' "$s" | sed "s/'/'\\\\''/g")"
}
state_unquote() {
local v="$1"
if [ -z "$v" ]; then
printf '%s' ""
return 0
fi
case "$v" in
\'*\')
v="${v#\'}"
v="${v%\'}"
v="${v//\'\\\'\'/\'}"
printf '%s' "$v"
return 0
;;
*)
printf '%s' "$v"
return 0
;;
esac
}
read_state_value() {
local key="$1"
[ -f "$STATE_FILE" ] || return 0
local line val
line="$(grep -E "^${key}=" "$STATE_FILE" 2>/dev/null | head -n1 || true)"
[ -n "$line" ] || return 0
val="${line#*=}"
state_unquote "$val" || true
}
svc_active() {
local name="$1"
if has_systemctl; then
systemctl is-active --quiet "$name" >/dev/null 2>&1
return $?
fi
if has_service_cmd; then
service "$name" status >/dev/null 2>&1
return $?
fi
return 1
}
svc_restart() {
local name="$1"
if has_systemctl; then
systemctl restart "$name" >/dev/null 2>&1 || return 1
return 0
fi
if has_service_cmd; then
service "$name" restart >/dev/null 2>&1 || return 1
return 0
fi
return 1
}
svc_restart_cmd_hint() {
local name="$1"
if has_systemctl; then
echo "sudo systemctl restart $name"
return 0
fi
if has_service_cmd; then
echo "sudo service $name restart"
return 0
fi
echo "restart $name manually"
}
restart_or_warn() {
local name="$1"
local label="$2"
if [ "$name" = "pihole-FTL" ] || [ "$label" = "Pi-hole FTL" ]; then
out "\033[34m[i]\033[0m Restarting Pi-hole FTL (can take up to 90s)..."
else
out "\033[34m[i]\033[0m Restarting ${label}..."
fi
if svc_restart "$name"; then
out "\033[32m[✓]\033[0m ${label} restarted."
return 0
fi
out "\033[31m[✗]\033[0m Failed to restart ${label}. Run: $(svc_restart_cmd_hint "$name")"
return 1
}
require_root() { [ "$(id -u)" -eq 0 ] || die "Run as root (sudo)."; }
require_installed() {
[ -f "$INSTALL_MARKER" ] || [ -f "$INSTALL_PATH" ] || die "Not installed. Run: $SCRIPT_CMD_NAME --install"
}
need_apt() { command -v apt-get >/dev/null 2>&1 || die "apt-get not found. Debian/Ubuntu only."; }
prompt_yn() {
local prompt="$1"
local default="${2:-N}"
if [ "$NONINTERACTIVE" -eq 1 ]; then
[ "$default" = "Y" ] && return 0 || return 1
fi
local ans=""
read -r -p "$prompt" ans
case "$ans" in
y|Y|yes|YES) return 0 ;;
n|N|no|NO) return 1 ;;
"") [ "$default" = "Y" ] && return 0 || return 1 ;;
*) return 2 ;;
esac
}
prompt_yn_loop() {
local prompt="$1"
local default="${2:-N}"
while true; do
if prompt_yn "$prompt" "$default"; then
return 0
else
local rc=$?
if [ "$rc" -eq 1 ]; then
return 1
fi
out "\033[33m[!]\033[0m Invalid input. Type y or n."
fi
done
}
is_ipv4() {
local ip="$1"
[[ "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]
}
is_ipv6() {
local ip="$1"
[[ "$ip" == *:* ]]
}
ip_ok_for_san() {
local ip="$1"
if is_ipv4 "$ip"; then
case "$ip" in
0.0.0.0) return 1 ;;
127.*) return 1 ;;
169.254.*) return 1 ;;
172.17.*|172.18.*|172.19.*|172.20.*) return 1 ;;
esac
return 0
fi
if is_ipv6 "$ip"; then
case "$ip" in
::1) return 1 ;;
fe80:*|FE80:*) return 1 ;;
esac
return 0
fi
return 1
}
collect_san_ips_from_hostname_i() {
local raw ips out_ips="" kept=0 total=0 truncated=0
raw="$(hostname -I 2>/dev/null || true)"
raw="$(printf '%s' "$raw" | tr -s ' ' | sed 's/[[:space:]]*$//' || true)"
for ips in $raw; do
total=$((total + 1))
case "$ips" in
*%*) continue ;;
esac
ip_ok_for_san "$ips" || continue
if [ "$kept" -ge "$SAN_MAX_IPS" ]; then
truncated=1
continue
fi
if [ -z "$out_ips" ]; then
out_ips="$ips"
else
out_ips="$out_ips $ips"
fi
kept=$((kept + 1))
done
FILTERED_IPS="$out_ips"
if [ "$truncated" -eq 1 ]; then
vout "\033[33m[!]\033[0m Too many IPs for SAN. Truncated to $SAN_MAX_IPS."
fi
}
print_repo_hint() {
[ "${LOCAL_HTTPS_SHOW_REPO_HINT:-1}" = "0" ] && return 0
local repo="https://github.com/luizbizzio/local-https"
printf '%b\n' "\033[90mDocumentation, source code, and issue tracker:\033[0m $repo"
printf '%b\n' "\033[90mIf this tool helped you, consider starring the repository.\033[0m"
echo ""
}
write_state() {
install -d -m 755 "$STATE_DIR" >/dev/null 2>&1 || true
local ts applied tailscale="no" ca_fp="" srv_end_raw="" srv_end_epoch="" srv_end_utc="" method="" installed_at
ts="$(date -Is 2>/dev/null || date 2>/dev/null || echo unknown)"
if command -v tailscale >/dev/null 2>&1; then
tailscale="yes"
fi
if [ "${PIHOLE_PRESENT:-0}" -eq 1 ] && [ "${TECH_PRESENT:-0}" -eq 1 ]; then
applied="pihole+technitium"
elif [ "${PIHOLE_PRESENT:-0}" -eq 1 ]; then
applied="pihole"
elif [ "${TECH_PRESENT:-0}" -eq 1 ]; then
applied="technitium"
else
applied="none"
fi
if [ -f "$CA_CRT" ]; then
ca_fp="$(openssl x509 -in "$CA_CRT" -noout -fingerprint -sha256 2>/dev/null | sed 's/^.*=//' || true)"
fi
if [ -f "$SERVER_CRT" ]; then
srv_end_raw="$(openssl x509 -in "$SERVER_CRT" -noout -enddate 2>/dev/null | cut -d= -f2 || true)"
if [ -n "$srv_end_raw" ]; then
srv_end_epoch="$(date -u -d "$srv_end_raw" +%s 2>/dev/null || true)"
if [ -n "$srv_end_epoch" ]; then
srv_end_utc="$(date -u -d "@$srv_end_epoch" +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || true)"
fi
fi
fi
method="${AUTORENEW_METHOD:-}"
[ -n "$method" ] || method="$(read_state_value autorenew_method)"
[ -n "$method" ] || method="none"
installed_at="$(read_state_value installed_at)"
[ -n "$installed_at" ] || installed_at="$ts"
{
echo "installed_at=$(sh_quote "$installed_at")"
echo "last_run_at=$(sh_quote "$ts")"
echo "hostname=$(sh_quote "${HOSTNAME:-$(hostname 2>/dev/null || true)}")"
echo "applied_targets=$(sh_quote "$applied")"
echo "autorenew_method=$(sh_quote "$method")"
echo "pihole_detected=$(sh_quote "${PIHOLE_PRESENT:-0}")"
echo "technitium_detected=$(sh_quote "${TECH_PRESENT:-0}")"
echo "tailscale_detected=$(sh_quote "$tailscale")"
echo "rootca_fingerprint_sha256=$(sh_quote "$ca_fp")"
echo "server_enddate_raw=$(sh_quote "$srv_end_raw")"
echo "server_enddate_epoch=$(sh_quote "$srv_end_epoch")"
echo "server_enddate_utc=$(sh_quote "$srv_end_utc")"
echo "cert_renewed=$(sh_quote "${CERT_RENEWED:-0}")"
} > "$STATE_FILE"
touch "$INSTALL_MARKER" >/dev/null 2>&1 || true
}
detect_technitium_service_name() {
TECH_SERVICE=""
has_systemctl || return 0
local candidates=(
technitium-dns.service
technitiumdns.service
TechnitiumDnsServer.service
dns.service
)
local svc=""
for svc in "${candidates[@]}"; do
systemctl list-unit-files --type=service --no-legend 2>/dev/null | awk '{print $1}' | grep -qx "$svc" || continue
local exec=""
exec="$(systemctl show -p ExecStart --value "$svc" 2>/dev/null || true)"
printf '%s' "$exec" | grep -Eqi 'technitium|TechnitiumDnsServer|dnsserver' || continue
TECH_SERVICE="$svc"
return 0
done
local fallback=""
fallback="$(systemctl list-unit-files --type=service --no-legend 2>/dev/null \
| awk '{print $1}' \
| grep -Ei '^technitium.*\.service$' \
| head -n1 || true)"
if [ -n "$fallback" ]; then
TECH_SERVICE="$fallback"
fi
}
build_renew_args() {
echo "--renew"
}
cron_line_build() {
if has_logger; then
echo "0 3 * * * $INSTALL_PATH --renew 2>&1 | logger -t local-https"
return 0
fi
echo "0 3 * * * $INSTALL_PATH --renew >> $CRON_LOG_PATH 2>&1"
}
install_logrotate_for_cron_log() {
[ -f "$CRON_LOG_PATH" ] || touch "$CRON_LOG_PATH" >/dev/null 2>&1 || true
chmod 644 "$CRON_LOG_PATH" >/dev/null 2>&1 || true
cat > "$LOGROTATE_PATH" <<EOF
$CRON_LOG_PATH {
weekly
rotate 8
missingok
notifempty
compress
delaycompress
copytruncate
}
EOF
}
upsert_block() {
local FILE="$1"
local BEGIN_MARK="$2"
local END_MARK="$3"
local CONTENT_FILE="$4"
[ -f "$FILE" ] || touch "$FILE"
if grep -qF "$BEGIN_MARK" "$FILE"; then
grep -qF "$END_MARK" "$FILE" || die "Found BEGIN marker but END marker is missing in: $FILE. Fix it manually before running again."
awk -v b="$BEGIN_MARK" -v e="$END_MARK" -v cf="$CONTENT_FILE" '
BEGIN{inblock=0}
$0==b{
print b
while((getline line < cf)>0) print line
close(cf)
print e
inblock=1
next
}
$0==e{inblock=0; next}
inblock==0{print}
' "$FILE" > "${FILE}.tmp" && mv "${FILE}.tmp" "$FILE"
else
grep -qF "$END_MARK" "$FILE" && die "Found END marker but BEGIN marker is missing in: $FILE. Fix it manually before running again."
printf "\n%s\n" "$BEGIN_MARK" >> "$FILE"
cat "$CONTENT_FILE" >> "$FILE"
printf "\n%s\n" "$END_MARK" >> "$FILE"
fi
}
remove_block() {
local FILE="$1"
local BEGIN_MARK="$2"
local END_MARK="$3"
[ -f "$FILE" ] || return 0
grep -qF "$BEGIN_MARK" "$FILE" || return 0
grep -qF "$END_MARK" "$FILE" || return 0
awk -v b="$BEGIN_MARK" -v e="$END_MARK" '
BEGIN{skip=0}
$0==b{skip=1; next}
$0==e{skip=0; next}
skip==0{print}
' "$FILE" > "${FILE}.tmp" && mv "${FILE}.tmp" "$FILE"
}
print_help() {
echo ""
echo "Usage:"
echo " $SCRIPT_CMD_NAME --install"
echo " $SCRIPT_CMD_NAME --renew [--force-renew]"
echo " $SCRIPT_CMD_NAME --check"
echo " $SCRIPT_CMD_NAME --status"
echo " $SCRIPT_CMD_NAME --print-ca"
echo " $SCRIPT_CMD_NAME --print-pfx-pass"
echo " $SCRIPT_CMD_NAME --rotate-pfx-pass"
echo " $SCRIPT_CMD_NAME --configure"
echo " $SCRIPT_CMD_NAME --uninstall [--yes] [--purge-certs]"
echo ""
echo "Notes:"
echo " - Running without args shows this help."
echo " - If already installed, --install will not run again."
echo " - Reinstall only via: --uninstall then --install"
echo ""
}
banner() {
local cmd="${1:-}"
local mode="install"
case "$cmd" in
--install) mode="install" ;;
--configure) mode="configure" ;;
--rotate-pfx-pass) mode="rotate-pfx-pass" ;;
--renew) mode="renew" ;;
--uninstall) mode="uninstall" ;;
esac
printf '%b\n' "\033[36m============================================================\033[0m"
printf '%b\n' "\033[1m\033[36m local-https\033[0m \033[90m(${mode})\033[0m"
printf '%b\n' "\033[36m============================================================\033[0m"
echo ""
if [ "$BOOTSTRAP" -eq 1 ]; then
return 0
fi
printf '%b\n' "\033[1mWhat this does\033[0m"
printf '%b\n' " - Create a local Root CA"
printf '%b\n' " - Issue a server cert (40 days) + build PEM and PFX"
echo ""
printf '%b\n' "\033[1mFiles\033[0m"
printf '%b\n' " - Dir: $SSL_DIR"
printf '%b\n' " - Root CA: $CA_CRT"
printf '%b\n' " - Server PEM: $SERVER_PEM"
printf '%b\n' " - Server PFX: $SERVER_PFX (password in $PFX_PASS_FILE)"
echo ""
case "$mode" in
install)
printf '%b\n' "\033[1mDuring install\033[0m"
printf '%b\n' " - Setup auto renew (systemd or cron)"
printf '%b\n' " - Optional: deploy to Pi-hole"
printf '%b\n' " - If Technitium is detected: TLS will be set to the PFX"
echo ""
;;
configure)
printf '%b\n' "\033[1mDuring configure\033[0m"
printf '%b\n' " - Optional: deploy to Pi-hole"
printf '%b\n' " - If Technitium is detected: TLS will be set to the PFX"
echo ""
;;
rotate-pfx-pass)
printf '%b\n' "\033[1mDuring rotate\033[0m"
printf '%b\n' " - Rotate PFX password and rebuild PFX"
printf '%b\n' " - If Technitium is detected: TLS will be updated to the new PFX password"
echo ""
;;
esac
printf '%b\n' "\033[90mHelp:\033[0m $SCRIPT_CMD_NAME --help"
echo ""
}
confirm_start() {
[ "$BOOTSTRAP" -eq 1 ] && return 0
[ "$NONINTERACTIVE" -eq 1 ] && return 0
[ -t 0 ] || return 0
if prompt_yn "Continue? (y/N): " "N"; then
return 0
fi
printf '%b\n' "\033[31mAborted.\033[0m"
exit 1
}
install_deps_interactive() {
out "\033[36m[>]\033[0m Checking dependencies..."
need_apt
local PKGS=()
need_cmd openssl || PKGS+=("openssl")
need_cmd curl || PKGS+=("curl")
need_cmd jq || PKGS+=("jq")
if [ "${#PKGS[@]}" -gt 0 ]; then
out "\033[34m[i]\033[0m Installing: ${PKGS[*]}"
apt-get update >/dev/null || die "apt-get update failed"
apt-get install -y "${PKGS[@]}" >/dev/null || die "apt-get install failed: ${PKGS[*]}"
out "\033[32m[✓]\033[0m Dependencies installed."
else
out "\033[32m[✓]\033[0m Dependencies already present."
fi
}
ensure_runtime_deps() {
need_cmd openssl || die "Missing dependency: openssl. Run --install first."
need_cmd hostname || die "Missing dependency: hostname."
}
install_self() {
out "\033[36m[>]\033[0m Installing command: $INSTALL_PATH"
local src=""
local tmp=""
local src_real=""
local dst_real=""
src="${BASH_SOURCE[0]:-}"
[ -n "$src" ] && [ -r "$src" ] && src="$(readlink -f "$src" 2>/dev/null || echo "$src")"
if [ -n "$src" ] && [ -r "$src" ] && [ -f "$src" ]; then
install -d -m 755 /usr/local/sbin >/dev/null 2>&1 || true
src_real="$(readlink -f "$src" 2>/dev/null || echo "$src")"
dst_real="$(readlink -f "$INSTALL_PATH" 2>/dev/null || echo "$INSTALL_PATH")"
if [ "$src_real" = "$dst_real" ]; then
chmod 755 "$INSTALL_PATH" >/dev/null 2>&1 || true
out "\033[32m[✓]\033[0m Installed: $INSTALL_PATH"
return 0
fi
install -m 755 "$src" "$INSTALL_PATH" >/dev/null 2>&1 || die "Failed to install to $INSTALL_PATH"
out "\033[32m[✓]\033[0m Installed: $INSTALL_PATH"
return 0
fi
need_cmd curl || die "curl missing. Run --install first."
tmp="$(mktemp -p /tmp local-https.self.XXXXXX)"
chmod 700 "$tmp" >/dev/null 2>&1 || true
if curl -fsSL "$SCRIPT_SOURCE_URL" -o "$tmp" >/dev/null 2>&1; then
:
else
local alt=""
alt="${SCRIPT_SOURCE_URL%.sh}"
[ "$alt" != "$SCRIPT_SOURCE_URL" ] && curl -fsSL "$alt" -o "$tmp" >/dev/null 2>&1 || true
fi
[ -s "$tmp" ] || { rm -f "$tmp" >/dev/null 2>&1 || true; die "Cannot locate script source file. Set LOCAL_HTTPS_SOURCE_URL or run from a saved file."; }
install -d -m 755 /usr/local/sbin >/dev/null 2>&1 || true
install -m 755 "$tmp" "$INSTALL_PATH" >/dev/null 2>&1 || { rm -f "$tmp" >/dev/null 2>&1 || true; die "Failed to install to $INSTALL_PATH"; }
rm -f "$tmp" >/dev/null 2>&1 || true
out "\033[32m[✓]\033[0m Installed: $INSTALL_PATH"
if [ "${LOCAL_HTTPS_BOOTSTRAP:-0}" != "1" ]; then
exec env LOCAL_HTTPS_BOOTSTRAP=1 "$INSTALL_PATH" --install
fi
}
technitium_detect_base_url() {
TECH_BASE_URL=""
TECH_PRESENT=0
local code=""
code="$(curl -k -sS -o /dev/null -w '%{http_code}' --max-time 3 "https://127.0.0.1:53443/" 2>/dev/null || true)"
if [ -n "$code" ] && [ "$code" != "000" ]; then
TECH_BASE_URL="https://127.0.0.1:53443"
TECH_PRESENT=1
return 0
fi
code="$(curl -sS -o /dev/null -w '%{http_code}' --max-time 3 "http://127.0.0.1:5380/" 2>/dev/null || true)"
if [ "$code" = "200" ]; then
TECH_BASE_URL="http://127.0.0.1:5380"
TECH_PRESENT=1
return 0
fi
if [ "$code" = "301" ] || [ "$code" = "302" ] || [ "$code" = "307" ] || [ "$code" = "308" ]; then
code="$(curl -k -sS -o /dev/null -w '%{http_code}' --max-time 3 "https://127.0.0.1:53443/" 2>/dev/null || true)"
if [ -n "$code" ] && [ "$code" != "000" ]; then
TECH_BASE_URL="https://127.0.0.1:53443"
TECH_PRESENT=1
return 0
fi
fi
return 0
}
detect_pihole_and_technitium() {
out "\033[36m[>]\033[0m Detecting Pi-hole and Technitium..."
if command -v pihole >/dev/null 2>&1; then
PIHOLE_PRESENT=1
out "\033[32m[✓]\033[0m Pi-hole detected."
else
PIHOLE_PRESENT=0
out "\033[33m[!]\033[0m Pi-hole not detected."
fi
TECH_BASE_URL=""
TECH_PRESENT=0
if command -v curl >/dev/null 2>&1; then
technitium_detect_base_url
fi
if [ "$TECH_PRESENT" -eq 1 ]; then
out "\033[32m[✓]\033[0m Technitium reachable at: $TECH_BASE_URL"
else
out "\033[33m[!]\033[0m Technitium not reachable on 127.0.0.1 (ports 5380/53443)."
fi
detect_technitium_service_name
if [ -n "$TECH_SERVICE" ]; then
vout "\033[32m[✓]\033[0m Technitium service detected: $TECH_SERVICE"
else
vout "\033[33m[>]\033[0m Technitium service name not detected."
fi
}
read_host_identity() {
out "\033[36m[>]\033[0m Reading host identity..."
HOSTNAME="$(hostname 2>/dev/null || true)"
[ -n "$HOSTNAME" ] || HOSTNAME="localhost"
collect_san_ips_from_hostname_i
local ip_count=0
local first_ip=""
local IP=""
for IP in $FILTERED_IPS; do
ip_count=$((ip_count + 1))
[ -z "$first_ip" ] && first_ip="$IP"
done
out "\033[34m[i]\033[0m Hostname: $HOSTNAME"
out "\033[34m[i]\033[0m SAN IPs: ${ip_count:-0} (example: ${first_ip:-none})"
vout "\033[34m[i]\033[0m IPs (SAN full): ${FILTERED_IPS:-none}"
TAILSCALE_DNS=""
TAILSCALE_SHORT=""
if command -v tailscale >/dev/null 2>&1 && command -v jq >/dev/null 2>&1; then
TAILSCALE_DNS="$(tailscale status -json 2>/dev/null | jq -r '.Self.DNSName' 2>/dev/null | sed 's/\.$//' || true)"
if [ -n "$TAILSCALE_DNS" ] && [ "$TAILSCALE_DNS" != "null" ]; then
TAILSCALE_SHORT="${TAILSCALE_DNS%%.*}"
out "\033[32m[✓]\033[0m Tailscale DNS: $TAILSCALE_DNS"
else
TAILSCALE_DNS=""
TAILSCALE_SHORT=""
vout "\033[33m[>]\033[0m Tailscale present but DNSName not available."
fi
else
vout "\033[33m[>]\033[0m Tailscale not installed or jq missing."
fi
}
prepare_dir() {
out "\033[36m[>]\033[0m Preparing certificate directory..."
mkdir -p "$SSL_DIR"
cd "$SSL_DIR" || die "Failed to cd into $SSL_DIR."
out "\033[32m[✓]\033[0m Using directory: $SSL_DIR"
PIHOLE_FTL_CERT="$SERVER_PEM"
}
create_or_reuse_ca() {
out "\033[36m[>]\033[0m Creating or reusing Root CA..."
cat << EOF > ca-openssl.cnf
[ req ]
distinguished_name = req_distinguished_name
prompt = no
x509_extensions = v3_ca
[ req_distinguished_name ]
CN = Local Server Root CA
[ v3_ca ]
basicConstraints = critical, CA:TRUE, pathlen:0
keyUsage = critical, keyCertSign, cRLSign
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
EOF
if [ ! -f "$CA_KEY" ] || [ ! -f "$CA_CRT" ]; then
local old_umask=""
old_umask="$(umask 2>/dev/null || echo "")"
umask 077
openssl genpkey -algorithm EC -out "$CA_KEY" -pkeyopt ec_paramgen_curve:$CA_EC_CURVE -pkeyopt ec_param_enc:named_curve >/dev/null 2>&1 || die "OpenSSL failed to generate CA key."
openssl req -x509 -new -key "$CA_KEY" -out "$CA_CRT" -days 3650 -sha256 -config ./ca-openssl.cnf >/dev/null 2>&1 || die "OpenSSL failed to create CA certificate."
[ -n "$old_umask" ] && umask "$old_umask" >/dev/null 2>&1 || true
out "\033[32m[✓]\033[0m Root CA created."
else
out "\033[32m[✓]\033[0m Root CA already exists."
fi
}
server_cert_needs_renew() {
if [ ! -f "$SERVER_CRT" ]; then
return 0
fi
if ! openssl x509 -checkend "$RENEW_WINDOW_SECONDS" -noout -in "$SERVER_CRT" >/dev/null 2>&1; then
return 0
fi
return 1
}
issue_or_renew_server_cert() {
local QUIET=0
if [ "$NONINTERACTIVE" -eq 1 ] && [ "$VERBOSE" -eq 0 ]; then
QUIET=1
fi
[ "$QUIET" -eq 1 ] || out "\033[36m[>]\033[0m Issuing or renewing server certificate..."
[ -n "${HOSTNAME:-}" ] || HOSTNAME="$(hostname 2>/dev/null || true)"
[ -n "${HOSTNAME:-}" ] || HOSTNAME="localhost"
cat << EOF > server-openssl.cnf
[ req ]
distinguished_name = req_distinguished_name
req_extensions = v3_req
prompt = no
[ req_distinguished_name ]
CN = $HOSTNAME
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = digitalSignature
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[ alt_names ]
DNS.1 = $HOSTNAME
EOF
local DNS_INDEX=2
if [ "$HOSTNAME" != "pi.hole" ]; then
echo "DNS.${DNS_INDEX} = pi.hole" >> server-openssl.cnf
DNS_INDEX=$((DNS_INDEX + 1))
fi
if [ -n "$TAILSCALE_DNS" ] && [ "$TAILSCALE_DNS" != "null" ] && [ "$TAILSCALE_DNS" != "pi.hole" ] && [ "$TAILSCALE_DNS" != "$HOSTNAME" ]; then
echo "DNS.${DNS_INDEX} = $TAILSCALE_DNS" >> server-openssl.cnf
DNS_INDEX=$((DNS_INDEX + 1))
fi
if [ -n "$TAILSCALE_SHORT" ] && [ "$TAILSCALE_SHORT" != "null" ] && [ "$TAILSCALE_SHORT" != "$HOSTNAME" ] && [ "$TAILSCALE_SHORT" != "pi" ] && [ "$TAILSCALE_SHORT" != "pihole" ]; then
echo "DNS.${DNS_INDEX} = $TAILSCALE_SHORT" >> server-openssl.cnf
DNS_INDEX=$((DNS_INDEX + 1))
fi
local IP_INDEX=1
local IP=""
for IP in $FILTERED_IPS; do
[ -n "$IP" ] && echo "IP.${IP_INDEX} = $IP" >> server-openssl.cnf && IP_INDEX=$((IP_INDEX + 1))
done
local do_renew=0
if [ "$FORCE_RENEW" -eq 1 ]; then
do_renew=1
else
if server_cert_needs_renew; then
do_renew=1
else
do_renew=0
fi
fi
if [ "$do_renew" -eq 1 ]; then
local old_umask=""
old_umask="$(umask 2>/dev/null || echo "")"
umask 077
if [ ! -f "$SERVER_KEY" ]; then
openssl genpkey -algorithm EC -out "$SERVER_KEY" -pkeyopt ec_paramgen_curve:$SERVER_EC_CURVE -pkeyopt ec_param_enc:named_curve >/dev/null 2>&1 || die "OpenSSL failed to generate server key."
fi
openssl req -new -key "$SERVER_KEY" -out "$SERVER_CSR" -config ./server-openssl.cnf >/dev/null 2>&1 || die "OpenSSL failed to generate CSR. Check: $SSL_DIR/server-openssl.cnf"
[ -f "$CA_KEY" ] && [ -f "$CA_CRT" ] || die "Root CA missing. Run: $SCRIPT_CMD_NAME --install"
openssl x509 -req -in "$SERVER_CSR" -CA "$CA_CRT" -CAkey "$CA_KEY" -CAserial "$CA_SRL" -CAcreateserial -out "$SERVER_CRT" -days 40 -sha256 -extfile ./server-openssl.cnf -extensions v3_req >/dev/null 2>&1 || die "OpenSSL failed to sign certificate."
cat "$SERVER_CRT" "$SERVER_KEY" > "$SERVER_PEM"
CERT_RENEWED=1
[ -n "$old_umask" ] && umask "$old_umask" >/dev/null 2>&1 || true
if [ "$QUIET" -eq 1 ]; then
out "\033[32m[✓]\033[0m Server certificate renewed (40 days)."
else
if [ "$FORCE_RENEW" -eq 1 ]; then
out "\033[32m[✓]\033[0m Server certificate forced renew (40 days)."
else
out "\033[32m[✓]\033[0m Server certificate issued or renewed (40 days)."
fi
fi
else
[ "$QUIET" -eq 1 ] || out "\033[32m[✓]\033[0m Server certificate still valid. No renewal needed."
fi
if [ "$QUIET" -eq 1 ]; then
return 0
fi
if [ "$VERBOSE" -eq 1 ]; then
out "\033[34m[i]\033[0m Certificate SANs:"
openssl x509 -in "$SERVER_CRT" -noout -ext subjectAltName 2>/dev/null || true
pause_step
else
local ip_count=0
local _ip=""
for _ip in $FILTERED_IPS; do ip_count=$((ip_count + 1)); done
local dns_list="$HOSTNAME"
if [ "$PIHOLE_PRESENT" -eq 1 ]; then
dns_list="$dns_list, pi.hole"
fi
if [ -n "$TAILSCALE_DNS" ] && [ "$TAILSCALE_DNS" != "null" ]; then
dns_list="$dns_list, $TAILSCALE_DNS"
fi
if [ -n "$TAILSCALE_SHORT" ] && [ "$TAILSCALE_SHORT" != "$HOSTNAME" ] && [ "$TAILSCALE_SHORT" != "pi" ]; then
dns_list="$dns_list, $TAILSCALE_SHORT"
fi
out "\033[34m[i]\033[0m SAN: DNS: $dns_list | IPs: $ip_count"
fi
}
generate_random_password() {
local p=""
p="$(openssl rand -base64 32 2>/dev/null | tr -d '\n' || true)"
[ -n "$p" ] || p="$(openssl rand -hex 32 2>/dev/null | tr -d '\n' || true)"
[ -n "$p" ] || die "Failed to generate random password."
printf '%s' "$p"
}
ensure_pfx_password_file() {
if [ -f "$PFX_PASS_FILE" ] && [ -s "$PFX_PASS_FILE" ]; then
chmod 600 "$PFX_PASS_FILE" >/dev/null 2>&1 || true
chown root:root "$PFX_PASS_FILE" >/dev/null 2>&1 || true
return 0
fi
if [ -n "${LOCAL_HTTPS_PFX_PASSWORD:-}" ]; then
install -m 600 -o root -g root /dev/null "$PFX_PASS_FILE" >/dev/null 2>&1 || true
printf '%s' "$LOCAL_HTTPS_PFX_PASSWORD" > "$PFX_PASS_FILE"
chmod 600 "$PFX_PASS_FILE" >/dev/null 2>&1 || true
chown root:root "$PFX_PASS_FILE" >/dev/null 2>&1 || true
return 0
fi
if [ -n "${LOCAL_HTTPS_PFX_PASSWORD_FILE:-}" ] && [ -f "$LOCAL_HTTPS_PFX_PASSWORD_FILE" ]; then
install -m 600 -o root -g root /dev/null "$PFX_PASS_FILE" >/dev/null 2>&1 || true
cat "$LOCAL_HTTPS_PFX_PASSWORD_FILE" > "$PFX_PASS_FILE"
chmod 600 "$PFX_PASS_FILE" >/dev/null 2>&1 || true
chown root:root "$PFX_PASS_FILE" >/dev/null 2>&1 || true
return 0
fi
if [ "$NONINTERACTIVE" -eq 1 ]; then
install -m 600 -o root -g root /dev/null "$PFX_PASS_FILE" >/dev/null 2>&1 || true
generate_random_password > "$PFX_PASS_FILE"
chmod 600 "$PFX_PASS_FILE" >/dev/null 2>&1 || true
chown root:root "$PFX_PASS_FILE" >/dev/null 2>&1 || true
out "\033[32m[✓]\033[0m PFX password generated and stored: $PFX_PASS_FILE"
return 0
fi
if prompt_yn_loop "Generate random PFX password and store it? (Y/n): " "Y"; then
install -m 600 -o root -g root /dev/null "$PFX_PASS_FILE" >/dev/null 2>&1 || true
generate_random_password > "$PFX_PASS_FILE"
chmod 600 "$PFX_PASS_FILE" >/dev/null 2>&1 || true
chown root:root "$PFX_PASS_FILE" >/dev/null 2>&1 || true
out "\033[32m[✓]\033[0m PFX password generated and stored: $PFX_PASS_FILE"
out "\033[34m[i]\033[0m To print it: $SCRIPT_CMD_NAME --print-pfx-pass"
return 0
fi
while true; do
local p1="" p2=""
read -r -s -p "Create PFX password (required): " p1
echo ""
read -r -s -p "Confirm PFX password: " p2
echo ""
if [ -n "$p1" ] && [ "$p1" = "$p2" ]; then
install -m 600 -o root -g root /dev/null "$PFX_PASS_FILE" >/dev/null 2>&1 || true
printf '%s' "$p1" > "$PFX_PASS_FILE"
chmod 600 "$PFX_PASS_FILE" >/dev/null 2>&1 || true
chown root:root "$PFX_PASS_FILE" >/dev/null 2>&1 || true
out "\033[32m[✓]\033[0m PFX password stored: $PFX_PASS_FILE"
return 0
fi
out "\033[33m[!]\033[0m Password empty or mismatch. Try again."
done
}
rotate_pfx_password_file() {
if [ -n "${LOCAL_HTTPS_PFX_PASSWORD:-}" ]; then
install -m 600 -o root -g root /dev/null "$PFX_PASS_FILE" >/dev/null 2>&1 || true
printf '%s' "$LOCAL_HTTPS_PFX_PASSWORD" > "$PFX_PASS_FILE"
chmod 600 "$PFX_PASS_FILE" >/dev/null 2>&1 || true
chown root:root "$PFX_PASS_FILE" >/dev/null 2>&1 || true
return 0
fi