-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcert_manager.sh
More file actions
1398 lines (1215 loc) · 42.2 KB
/
cert_manager.sh
File metadata and controls
1398 lines (1215 loc) · 42.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
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'
# Add some basic function here
function LOGD() {
echo -e "${yellow}[DEG] $* ${plain}"
}
function LOGE() {
echo -e "${red}[ERR] $* ${plain}"
}
function LOGI() {
echo -e "${green}[INF] $* ${plain}"
}
# check root
[[ $EUID -ne 0 ]] && LOGE "ERROR: You must be root to run this script! \n" && exit 1
# Check OS and set release variable
if [[ -f /etc/os-release ]]; then
source /etc/os-release
release=$ID
elif [[ -f /usr/lib/os-release ]]; then
source /usr/lib/os-release
release=$ID
else
echo "Failed to check the system OS, please contact the author!" >&2
exit 1
fi
echo "The OS release is: $release"
os_version=""
os_version=$(grep -i version_id /etc/os-release | cut -d \" -f2 | cut -d . -f1)
if [[ "${release}" == "arch" ]]; then
echo "Your OS is Arch Linux"
elif [[ "${release}" == "parch" ]]; then
echo "Your OS is Parch linux"
elif [[ "${release}" == "manjaro" ]]; then
echo "Your OS is Manjaro"
elif [[ "${release}" == "armbian" ]]; then
echo "Your OS is Armbian"
elif [[ "${release}" == "opensuse-tumbleweed" ]]; then
echo "Your OS is OpenSUSE Tumbleweed"
elif [[ "${release}" == "centos" ]]; then
if [[ ${os_version} -lt 8 ]]; then
echo -e "${red} Please use CentOS 8 or higher ${plain}\n" && exit 1
fi
elif [[ "${release}" == "ubuntu" ]]; then
if [[ ${os_version} -lt 20 ]]; then
echo -e "${red} Please use Ubuntu 20 or higher version!${plain}\n" && exit 1
fi
elif [[ "${release}" == "fedora" ]]; then
if [[ ${os_version} -lt 36 ]]; then
echo -e "${red} Please use Fedora 36 or higher version!${plain}\n" && exit 1
fi
elif [[ "${release}" == "debian" ]]; then
if [[ ${os_version} -lt 11 ]]; then
echo -e "${red} Please use Debian 11 or higher ${plain}\n" && exit 1
fi
elif [[ "${release}" == "almalinux" ]]; then
if [[ ${os_version} -lt 9 ]]; then
echo -e "${red} Please use AlmaLinux 9 or higher ${plain}\n" && exit 1
fi
elif [[ "${release}" == "rocky" ]]; then
if [[ ${os_version} -lt 9 ]]; then
echo -e "${red} Please use Rocky Linux 9 or higher ${plain}\n" && exit 1
fi
elif [[ "${release}" == "oracle" ]]; then
if [[ ${os_version} -lt 8 ]]; then
echo -e "${red} Please use Oracle Linux 8 or higher ${plain}\n" && exit 1
fi
else
echo -e "${red}Your operating system is not supported by this script.${plain}\n"
echo "Please ensure you are using one of the following supported operating systems:"
echo "- Ubuntu 20.04+"
echo "- Debian 11+"
echo "- CentOS 8+"
echo "- Fedora 36+"
echo "- Arch Linux"
echo "- Parch Linux"
echo "- Manjaro"
echo "- Armbian"
echo "- AlmaLinux 9+"
echo "- Rocky Linux 9+"
echo "- Oracle Linux 8+"
echo "- OpenSUSE Tumbleweed"
exit 1
fi
confirm() {
if [[ $# -gt 1 ]]; then
echo && read -p "$1 [Default $2]: " temp
if [[ "${temp}" == "" ]]; then
temp=$2
fi
else
read -p "$1 [y/n]: " temp
fi
if [[ "${temp}" == "y" || "${temp}" == "Y" ]]; then
return 0
else
return 1
fi
}
before_show_menu() {
echo && echo -n -e "${yellow}Press enter to return to the main menu: ${plain}" && read temp
show_menu
}
# Install required dependencies
install_dependencies() {
LOGI "Installing required dependencies..."
case "${release}" in
ubuntu | debian | armbian)
apt update && apt install -y curl wget socat cron
;;
centos | almalinux | rocky | oracle)
yum update -y && yum install -y curl wget socat cronie
systemctl enable crond
systemctl start crond
;;
fedora)
dnf update -y && dnf install -y curl wget socat cronie
systemctl enable crond
systemctl start crond
;;
arch | manjaro | parch)
pacman -Sy --noconfirm curl wget socat cronie
systemctl enable cronie
systemctl start cronie
;;
*)
echo -e "${red}Unsupported operating system. Please install curl, wget, socat, and cron manually.${plain}\n"
exit 1
;;
esac
if [ $? -ne 0 ]; then
LOGE "Failed to install dependencies"
exit 1
else
LOGI "Dependencies installed successfully"
fi
}
install_acme() {
cd ~ || return 1
LOGI "Installing acme.sh..."
curl https://get.acme.sh | sh
if [ $? -ne 0 ]; then
LOGE "Install acme.sh failed"
return 1
else
LOGI "Install acme.sh succeed"
fi
return 0
}
install_certbot() {
LOGI "Installing Certbot..."
case "${release}" in
ubuntu | debian | armbian)
apt update && apt install -y certbot python3-certbot
;;
centos | almalinux | rocky | oracle)
if [[ ${os_version} -ge 8 ]]; then
dnf install -y epel-release
dnf install -y certbot python3-certbot
else
yum install -y epel-release
yum install -y certbot python3-certbot
fi
;;
fedora)
dnf install -y certbot python3-certbot
;;
arch | manjaro | parch)
pacman -Sy --noconfirm certbot
;;
*)
LOGE "Unsupported operating system for Certbot installation"
return 1
;;
esac
if [ $? -ne 0 ]; then
LOGE "Failed to install Certbot"
return 1
else
LOGI "Certbot installed successfully"
fi
return 0
}
install_certbot_dns_plugins() {
local plugin=$1
LOGI "Installing Certbot DNS plugin: ${plugin}..."
case "${release}" in
ubuntu | debian | armbian)
apt install -y python3-certbot-dns-${plugin}
;;
centos | almalinux | rocky | oracle | fedora)
dnf install -y python3-certbot-dns-${plugin} 2>/dev/null || yum install -y python3-certbot-dns-${plugin}
;;
arch | manjaro | parch)
# Arch uses pip for certbot plugins
pacman -Sy --noconfirm python-pip
pip install certbot-dns-${plugin}
;;
*)
LOGE "Unsupported operating system for Certbot DNS plugins"
return 1
;;
esac
if [ $? -ne 0 ]; then
LOGE "Failed to install Certbot DNS plugin: ${plugin}"
return 1
else
LOGI "Certbot DNS plugin ${plugin} installed successfully"
fi
return 0
}
setup_auto_renewal() {
LOGI "Setting up automatic certificate renewal..."
# Check if acme.sh is installed
if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
LOGE "acme.sh is not installed. Please install it first."
return 1
fi
# Create cron job for certificate renewal (check daily at 2:30 AM)
cron_job="30 2 * * * ~/.acme.sh/acme.sh --cron --home ~/.acme.sh > /var/log/acme_renewal.log 2>&1"
# Check if cron job already exists
if crontab -l 2>/dev/null | grep -q "acme.sh --cron"; then
LOGI "Auto renewal cron job already exists"
else
# Add cron job
(crontab -l 2>/dev/null; echo "$cron_job") | crontab -
LOGI "Auto renewal cron job added successfully"
fi
# Enable acme.sh auto-upgrade
~/.acme.sh/acme.sh --upgrade --auto-upgrade
LOGI "Automatic certificate renewal setup completed"
}
ssl_cert_issue() {
# Install dependencies first
install_dependencies
# check for acme.sh first
if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
echo "acme.sh could not be found. Installing it now..."
install_acme
if [ $? -ne 0 ]; then
LOGE "Install acme.sh failed, please check logs"
exit 1
fi
fi
# get the domain here,and we need verify it
local domain=""
read -p "Please enter your domain name: " domain
LOGD "Your domain is: ${domain}, checking it..."
# here we need to judge whether there exists cert already
local currentCert
currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
if [ "${currentCert}" == "${domain}" ]; then
local certInfo
certInfo=$(~/.acme.sh/acme.sh --list)
LOGE "System already has certificates for this domain, cannot issue again. Current certificates details:"
LOGI "$certInfo"
exit 1
else
LOGI "Your domain is ready for issuing certificate now..."
fi
# create a directory for install cert
certPath="/root/cert/${domain}"
if [ ! -d "$certPath" ]; then
mkdir -p "$certPath"
else
rm -rf "$certPath"
mkdir -p "$certPath"
fi
# get needed port here
local WebPort=80
read -p "Please choose which port to use, default will be 80 port: " WebPort
if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
LOGE "Your input ${WebPort} is invalid, will use default port 80"
WebPort=80
fi
LOGI "Will use port: ${WebPort} to issue certificates, please make sure this port is open..."
# Set default CA and issue certificate
~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
~/.acme.sh/acme.sh --issue -d ${domain} --listen-v6 --standalone --httpport ${WebPort}
if [ $? -ne 0 ]; then
LOGE "Issue certificates failed, please check logs"
rm -rf ~/.acme.sh/${domain}
exit 1
else
LOGI "Issue certificates succeed, installing certificates..."
fi
# install cert
~/.acme.sh/acme.sh --installcert -d ${domain} \
--key-file /root/cert/${domain}/privkey.pem \
--fullchain-file /root/cert/${domain}/fullchain.pem
if [ $? -ne 0 ]; then
LOGE "Install certificates failed, exit"
rm -rf ~/.acme.sh/${domain}
exit 1
else
LOGI "Install certificates succeed"
fi
# Set up automatic renewal
setup_auto_renewal
LOGI "Certificate installation completed successfully!"
LOGI "Certificate files are located at: ${certPath}"
LOGI "Private key: ${certPath}/privkey.pem"
LOGI "Full chain: ${certPath}/fullchain.pem"
}
ssl_cert_issue_CF() {
echo -e ""
LOGD "******Instructions for use******"
LOGI "This Acme script requires the following data:"
LOGI "1. Cloudflare Registered email"
LOGI "2. Cloudflare Global API Key"
LOGI "3. The domain name that has been resolved DNS to the current server by Cloudflare"
LOGI "4. The script applies for a certificate. The default installation path is /root/cert"
confirm "Confirmed?" "y"
if [ $? -eq 0 ]; then
# Install dependencies first
install_dependencies
# check for acme.sh first
if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
echo "acme.sh could not be found. Installing it now..."
install_acme
if [ $? -ne 0 ]; then
LOGE "Install acme.sh failed, please check logs"
exit 1
fi
fi
CF_Domain=""
CF_GlobalKey=""
CF_AccountEmail=""
certPath=/root/cert
if [ ! -d "$certPath" ]; then
mkdir $certPath
else
rm -rf $certPath
mkdir $certPath
fi
LOGD "Please set a domain name:"
read -p "Input your domain here: " CF_Domain
LOGD "Your domain name is set to: ${CF_Domain}"
LOGD "Please set the API key:"
read -p "Input your key here: " CF_GlobalKey
LOGD "Your API key is: ${CF_GlobalKey}"
LOGD "Please set up registered email:"
read -p "Input your email here: " CF_AccountEmail
LOGD "Your registered email address is: ${CF_AccountEmail}"
~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
if [ $? -ne 0 ]; then
LOGE "Default CA, Let's Encrypt fail, script exiting..."
exit 1
fi
export CF_Key="${CF_GlobalKey}"
export CF_Email=${CF_AccountEmail}
~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
if [ $? -ne 0 ]; then
LOGE "Certificate issuance failed, script exiting..."
exit 1
else
LOGI "Certificate issued Successfully, Installing..."
fi
~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} --ca-file /root/cert/ca.cer \
--cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
--fullchain-file /root/cert/fullchain.cer
if [ $? -ne 0 ]; then
LOGE "Certificate installation failed, script exiting..."
exit 1
else
LOGI "Certificate installed Successfully"
fi
# Set up automatic renewal
setup_auto_renewal
LOGI "The certificate is installed and auto-renewal is turned on. Certificate files location:"
ls -lah /root/cert/
chmod 755 $certPath
else
show_menu
fi
}
ssl_cert_issue_acme_route53() {
echo -e ""
LOGD "******Instructions for use******"
LOGI "This script requires AWS Route53 credentials:"
LOGI "1. AWS Access Key ID"
LOGI "2. AWS Secret Access Key"
LOGI "3. Domain name managed by Route53"
LOGI "4. Certificate will be installed to /root/cert"
confirm "Confirmed?" "y"
if [ $? -eq 0 ]; then
install_dependencies
if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
LOGI "Installing acme.sh..."
install_acme
if [ $? -ne 0 ]; then
LOGE "Install acme.sh failed"
return 1
fi
fi
local domain=""
read -p "Enter your domain name: " domain
LOGD "Domain: ${domain}"
local aws_key=""
read -p "Enter AWS Access Key ID: " aws_key
local aws_secret=""
read -p "Enter AWS Secret Access Key: " aws_secret
certPath="/root/cert/${domain}"
mkdir -p "$certPath"
export AWS_ACCESS_KEY_ID="${aws_key}"
export AWS_SECRET_ACCESS_KEY="${aws_secret}"
~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
~/.acme.sh/acme.sh --issue --dns dns_aws -d ${domain} -d *.${domain} --log
if [ $? -ne 0 ]; then
LOGE "Certificate issuance failed"
return 1
fi
~/.acme.sh/acme.sh --installcert -d ${domain} -d *.${domain} \
--key-file ${certPath}/privkey.pem \
--fullchain-file ${certPath}/fullchain.pem \
--cert-file ${certPath}/cert.pem
if [ $? -eq 0 ]; then
LOGI "Certificate installed successfully at ${certPath}"
setup_auto_renewal
else
LOGE "Certificate installation failed"
return 1
fi
fi
}
ssl_cert_issue_acme_gcloud() {
echo -e ""
LOGD "******Instructions for use******"
LOGI "This script requires Google Cloud DNS credentials:"
LOGI "1. GCP Service Account JSON file"
LOGI "2. Domain name managed by Google Cloud DNS"
LOGI "3. Certificate will be installed to /root/cert"
confirm "Confirmed?" "y"
if [ $? -eq 0 ]; then
install_dependencies
if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
LOGI "Installing acme.sh..."
install_acme
if [ $? -ne 0 ]; then
LOGE "Install acme.sh failed"
return 1
fi
fi
local domain=""
read -p "Enter your domain name: " domain
LOGD "Domain: ${domain}"
local gcp_key_file=""
read -p "Enter path to GCP service account JSON file: " gcp_key_file
if [ ! -f "$gcp_key_file" ]; then
LOGE "GCP credentials file not found: ${gcp_key_file}"
return 1
fi
certPath="/root/cert/${domain}"
mkdir -p "$certPath"
export GCE_SERVICE_ACCOUNT_FILE="${gcp_key_file}"
~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
~/.acme.sh/acme.sh --issue --dns dns_gcloud -d ${domain} -d *.${domain} --log
if [ $? -ne 0 ]; then
LOGE "Certificate issuance failed"
return 1
fi
~/.acme.sh/acme.sh --installcert -d ${domain} -d *.${domain} \
--key-file ${certPath}/privkey.pem \
--fullchain-file ${certPath}/fullchain.pem \
--cert-file ${certPath}/cert.pem
if [ $? -eq 0 ]; then
LOGI "Certificate installed successfully at ${certPath}"
setup_auto_renewal
else
LOGE "Certificate installation failed"
return 1
fi
fi
}
ssl_cert_issue_acme_digitalocean() {
echo -e ""
LOGD "******Instructions for use******"
LOGI "This script requires DigitalOcean credentials:"
LOGI "1. DigitalOcean API Token"
LOGI "2. Domain name managed by DigitalOcean DNS"
LOGI "3. Certificate will be installed to /root/cert"
confirm "Confirmed?" "y"
if [ $? -eq 0 ]; then
install_dependencies
if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
LOGI "Installing acme.sh..."
install_acme
if [ $? -ne 0 ]; then
LOGE "Install acme.sh failed"
return 1
fi
fi
local domain=""
read -p "Enter your domain name: " domain
LOGD "Domain: ${domain}"
local do_token=""
read -p "Enter DigitalOcean API Token: " do_token
certPath="/root/cert/${domain}"
mkdir -p "$certPath"
export DO_API_KEY="${do_token}"
~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
~/.acme.sh/acme.sh --issue --dns dns_dgon -d ${domain} -d *.${domain} --log
if [ $? -ne 0 ]; then
LOGE "Certificate issuance failed"
return 1
fi
~/.acme.sh/acme.sh --installcert -d ${domain} -d *.${domain} \
--key-file ${certPath}/privkey.pem \
--fullchain-file ${certPath}/fullchain.pem \
--cert-file ${certPath}/cert.pem
if [ $? -eq 0 ]; then
LOGI "Certificate installed successfully at ${certPath}"
setup_auto_renewal
else
LOGE "Certificate installation failed"
return 1
fi
fi
}
ssl_cert_issue_acme_zerossl() {
echo -e ""
LOGD "******Instructions for use******"
LOGI "This script will issue certificate using ZeroSSL CA:"
LOGI "1. ZeroSSL account email"
LOGI "2. Domain name"
LOGI "3. Certificate will be installed to /root/cert"
confirm "Confirmed?" "y"
if [ $? -eq 0 ]; then
install_dependencies
if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
LOGI "Installing acme.sh..."
install_acme
if [ $? -ne 0 ]; then
LOGE "Install acme.sh failed"
return 1
fi
fi
local domain=""
read -p "Enter your domain name: " domain
LOGD "Domain: ${domain}"
local email=""
read -p "Enter your ZeroSSL account email: " email
LOGD "Email: ${email}"
certPath="/root/cert/${domain}"
if [ ! -d "$certPath" ]; then
mkdir -p "$certPath"
else
rm -rf "$certPath"
mkdir -p "$certPath"
fi
local WebPort=80
read -p "Choose port for validation (default 80): " WebPort
if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
LOGE "Invalid port ${WebPort}, using default 80"
WebPort=80
fi
LOGI "Using port: ${WebPort}"
# Register with ZeroSSL
~/.acme.sh/acme.sh --register-account --server zerossl --eab-kid "" --eab-hmac-key ""
# Set ZeroSSL as default CA
~/.acme.sh/acme.sh --set-default-ca --server zerossl
# Issue certificate
~/.acme.sh/acme.sh --issue -d ${domain} --standalone --httpport ${WebPort}
if [ $? -ne 0 ]; then
LOGE "Certificate issuance failed"
rm -rf ~/.acme.sh/${domain}
return 1
fi
~/.acme.sh/acme.sh --installcert -d ${domain} \
--key-file ${certPath}/privkey.pem \
--fullchain-file ${certPath}/fullchain.pem
if [ $? -eq 0 ]; then
LOGI "ZeroSSL certificate installed successfully!"
LOGI "Certificate location: ${certPath}"
setup_auto_renewal
else
LOGE "Certificate installation failed"
return 1
fi
fi
}
ssl_cert_selfsigned() {
LOGI "Generating self-signed certificate..."
local domain=""
read -p "Enter domain/common name for certificate: " domain
LOGD "Domain: ${domain}"
local days=365
read -p "Enter validity period in days (default 365): " days
if [[ -z "${days}" ]]; then
days=365
fi
certPath="/root/cert/${domain}"
mkdir -p "$certPath"
# Generate private key
openssl genrsa -out ${certPath}/privkey.pem 2048
if [ $? -ne 0 ]; then
LOGE "Failed to generate private key"
return 1
fi
# Generate self-signed certificate
openssl req -new -x509 -key ${certPath}/privkey.pem \
-out ${certPath}/fullchain.pem -days ${days} \
-subj "/C=US/ST=State/L=City/O=Organization/CN=${domain}"
if [ $? -eq 0 ]; then
LOGI "Self-signed certificate generated successfully!"
LOGI "Certificate location: ${certPath}"
LOGI "Private key: ${certPath}/privkey.pem"
LOGI "Certificate: ${certPath}/fullchain.pem"
LOGI "Valid for: ${days} days"
LOGI ""
LOGE "WARNING: Self-signed certificates are NOT trusted by browsers!"
LOGE "Use only for testing/development purposes."
else
LOGE "Failed to generate certificate"
return 1
fi
}
revoke_certificate() {
local domain=""
read -p "Please enter your domain name to revoke the certificate: " domain
if [ -z "$domain" ]; then
LOGE "Domain name cannot be empty"
return 1
fi
~/.acme.sh/acme.sh --revoke -d ${domain}
if [ $? -eq 0 ]; then
LOGI "Certificate revoked successfully"
# Remove certificate files
if [ -d "/root/cert/${domain}" ]; then
rm -rf "/root/cert/${domain}"
LOGI "Certificate files removed from /root/cert/${domain}"
fi
else
LOGE "Failed to revoke certificate"
fi
}
force_renew_certificate() {
local domain=""
read -p "Please enter your domain name to forcefully renew the SSL certificate: " domain
if [ -z "$domain" ]; then
LOGE "Domain name cannot be empty"
return 1
fi
~/.acme.sh/acme.sh --renew -d ${domain} --force
if [ $? -eq 0 ]; then
LOGI "Certificate renewed successfully"
else
LOGE "Failed to renew certificate"
fi
}
list_certificates() {
if command -v ~/.acme.sh/acme.sh &>/dev/null; then
LOGI "Current certificates:"
~/.acme.sh/acme.sh --list
echo ""
LOGI "Certificate files in /root/cert/:"
if [ -d "/root/cert" ]; then
ls -la /root/cert/
else
LOGI "No certificate directory found"
fi
else
LOGE "acme.sh is not installed"
fi
}
check_auto_renewal() {
LOGI "Checking automatic renewal status..."
# Check cron job
if crontab -l 2>/dev/null | grep -q "acme.sh --cron"; then
LOGI "Auto renewal cron job is configured:"
crontab -l | grep "acme.sh --cron"
else
LOGE "Auto renewal cron job is not configured"
fi
# Check acme.sh auto-upgrade status
if [ -f ~/.acme.sh/account.conf ]; then
if grep -q "AUTO_UPGRADE" ~/.acme.sh/account.conf; then
LOGI "acme.sh auto-upgrade is enabled"
else
LOGI "acme.sh auto-upgrade status unknown"
fi
fi
# Check last renewal log
if [ -f /var/log/acme_renewal.log ]; then
LOGI "Last renewal log entries:"
tail -n 10 /var/log/acme_renewal.log
else
LOGI "No renewal log found yet"
fi
}
# Certbot SSL certificate methods
ssl_cert_issue_certbot_standalone() {
install_dependencies
# Check for certbot
if ! command -v certbot &>/dev/null; then
LOGI "Certbot not found. Installing..."
install_certbot
if [ $? -ne 0 ]; then
LOGE "Failed to install Certbot"
return 1
fi
fi
local domain=""
read -p "Please enter your domain name: " domain
LOGD "Your domain is: ${domain}"
local email=""
read -p "Please enter your email address: " email
LOGD "Your email is: ${email}"
# Create certificate directory
certPath="/root/cert/${domain}"
if [ ! -d "$certPath" ]; then
mkdir -p "$certPath"
fi
# Issue certificate using standalone mode
LOGI "Issuing certificate using Certbot standalone mode..."
certbot certonly --standalone -d ${domain} --email ${email} --agree-tos --non-interactive
if [ $? -ne 0 ]; then
LOGE "Failed to issue certificate with Certbot"
return 1
fi
# Copy certificates to our standard location
cp /etc/letsencrypt/live/${domain}/privkey.pem ${certPath}/privkey.pem
cp /etc/letsencrypt/live/${domain}/fullchain.pem ${certPath}/fullchain.pem
LOGI "Certificate issued successfully!"
LOGI "Certificate files are located at: ${certPath}"
LOGI "Private key: ${certPath}/privkey.pem"
LOGI "Full chain: ${certPath}/fullchain.pem"
# Setup auto-renewal (Certbot has built-in renewal via systemd timer)
if command -v systemctl &>/dev/null; then
systemctl enable certbot.timer 2>/dev/null || systemctl enable certbot-renew.timer 2>/dev/null
systemctl start certbot.timer 2>/dev/null || systemctl start certbot-renew.timer 2>/dev/null
LOGI "Certbot automatic renewal timer enabled"
fi
}
ssl_cert_issue_certbot_webroot() {
install_dependencies
# Check for certbot
if ! command -v certbot &>/dev/null; then
LOGI "Certbot not found. Installing..."
install_certbot
if [ $? -ne 0 ]; then
LOGE "Failed to install Certbot"
return 1
fi
fi
local domain=""
read -p "Please enter your domain name: " domain
LOGD "Your domain is: ${domain}"
local email=""
read -p "Please enter your email address: " email
LOGD "Your email is: ${email}"
local webroot=""
read -p "Please enter your webroot path (e.g., /var/www/html): " webroot
LOGD "Your webroot path is: ${webroot}"
# Validate webroot exists
if [ ! -d "$webroot" ]; then
LOGE "Webroot directory does not exist: ${webroot}"
return 1
fi
# Create certificate directory
certPath="/root/cert/${domain}"
if [ ! -d "$certPath" ]; then
mkdir -p "$certPath"
fi
# Issue certificate using webroot mode
LOGI "Issuing certificate using Certbot webroot mode..."
certbot certonly --webroot -w ${webroot} -d ${domain} --email ${email} --agree-tos --non-interactive
if [ $? -ne 0 ]; then
LOGE "Failed to issue certificate with Certbot"
return 1
fi
# Copy certificates to our standard location
cp /etc/letsencrypt/live/${domain}/privkey.pem ${certPath}/privkey.pem
cp /etc/letsencrypt/live/${domain}/fullchain.pem ${certPath}/fullchain.pem
LOGI "Certificate issued successfully!"
LOGI "Certificate files are located at: ${certPath}"
LOGI "Private key: ${certPath}/privkey.pem"
LOGI "Full chain: ${certPath}/fullchain.pem"
# Setup auto-renewal
if command -v systemctl &>/dev/null; then
systemctl enable certbot.timer 2>/dev/null || systemctl enable certbot-renew.timer 2>/dev/null
systemctl start certbot.timer 2>/dev/null || systemctl start certbot-renew.timer 2>/dev/null
LOGI "Certbot automatic renewal timer enabled"
fi
}
ssl_cert_issue_certbot_dns_cloudflare() {
install_dependencies
# Check for certbot
if ! command -v certbot &>/dev/null; then
LOGI "Certbot not found. Installing..."
install_certbot
if [ $? -ne 0 ]; then
LOGE "Failed to install Certbot"
return 1
fi
fi
# Install Cloudflare DNS plugin
install_certbot_dns_plugins "cloudflare"
local domain=""
read -p "Please enter your domain name: " domain
LOGD "Your domain is: ${domain}"
local email=""
read -p "Please enter your email address: " email
LOGD "Your email is: ${email}"
local cf_token=""
read -p "Please enter your Cloudflare API Token: " cf_token
# Create Cloudflare credentials file
local creds_file="/root/.secrets/certbot/cloudflare.ini"
mkdir -p /root/.secrets/certbot
echo "dns_cloudflare_api_token = ${cf_token}" > ${creds_file}
chmod 600 ${creds_file}
# Create certificate directory
certPath="/root/cert/${domain}"
if [ ! -d "$certPath" ]; then
mkdir -p "$certPath"
fi
# Issue certificate using Cloudflare DNS
LOGI "Issuing certificate using Certbot Cloudflare DNS..."
certbot certonly --dns-cloudflare --dns-cloudflare-credentials ${creds_file} \
-d ${domain} -d *.${domain} --email ${email} --agree-tos --non-interactive
if [ $? -ne 0 ]; then
LOGE "Failed to issue certificate with Certbot Cloudflare DNS"
return 1
fi
# Copy certificates to our standard location
cp /etc/letsencrypt/live/${domain}/privkey.pem ${certPath}/privkey.pem
cp /etc/letsencrypt/live/${domain}/fullchain.pem ${certPath}/fullchain.pem
LOGI "Certificate issued successfully!"
LOGI "Certificate files are located at: ${certPath}"
LOGI "Wildcard certificate includes *.${domain}"
# Setup auto-renewal
if command -v systemctl &>/dev/null; then
systemctl enable certbot.timer 2>/dev/null || systemctl enable certbot-renew.timer 2>/dev/null
systemctl start certbot.timer 2>/dev/null || systemctl start certbot-renew.timer 2>/dev/null
LOGI "Certbot automatic renewal timer enabled"
fi
}
ssl_cert_issue_certbot_dns_route53() {
install_dependencies
# Check for certbot
if ! command -v certbot &>/dev/null; then
LOGI "Certbot not found. Installing..."
install_certbot
if [ $? -ne 0 ]; then
LOGE "Failed to install Certbot"
return 1
fi
fi
# Install Route53 DNS plugin
install_certbot_dns_plugins "route53"
local domain=""
read -p "Please enter your domain name: " domain
LOGD "Your domain is: ${domain}"
local email=""
read -p "Please enter your email address: " email