-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathssl_config.bat
1547 lines (1206 loc) · 49.2 KB
/
ssl_config.bat
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
@echo off
cls
setlocal EnableExtensions EnableDelayedExpansion
rem -----------------------
rem INITIALISATION
rem -----------------------
rem Set default variables
rem -----------------------
set $scriptVersion=1.2.3
set $scriptLogFileName=ssl_config.log
rem WampServer sub-paths.
set $subPathToApacheFolders=bin\apache
rem WampServer Apache sub-paths.
set $subPathToApacheHttpdExe=bin\httpd.exe
set $subPathToApacheOpenSslExe=bin\openssl.exe
rem Operating system paths.
set $pathToOSHostsFile=%systemroot%\System32\drivers\etc\hosts
set $pathToUsersTempFile=%temp%\ssl_config_temp_file.txt
rem -------------------
rem Get computer name
rem -------------------
set $computerName=%ComputerName%
rem ----------------
rem Get IP address
rem ----------------
for /f "tokens=2 delims=[]" %%a in ('ping %ComputerName% -4 -n 1') do set $ipAddress=%%a
rem ---------------------
rem Get IP network part
rem ---------------------
for /f "tokens=1,2 delims=." %%a in ("%$ipAddress%") do set $ipNetworkPart=%%a.%%b
rem ----------------------
rem Set echo offset hack
rem ----------------------
rem Hack to define a backspace so the 'set /p' command can be offset from the windows edge.
for /f %%a in ('"prompt $H &echo on &for %%b in (1) do rem"') do set backspace=%%a
rem ------------------
rem Set window title
rem ------------------
title WampServer SSL Auto Config (v%$scriptVersion%)
rem -------------
rem Show header
rem -------------
echo:
echo WampServer SSL Auto Config (v%$scriptVersion%)
echo -----------------------------------
echo:
rem -------------------
rem CLI
rem -------------------
rem Get CLI arg count
rem -------------------
rem Count the number of arguments.
set $argumentCount=0
for %%x in (%*) do Set /A $argumentCount+=1
rem Check if no arguments were given.
if !$argumentCount! equ 0 (
call :failure "CLI Argument Error" "ssl_conf.bat" "No ini file was given." "Please pass in the path to your config.ini file."
)
rem Check if more than two arguments were given.
if !$argumentCount! gtr 2 (
call :failure "CLI Argument Error" "ssl_conf.bat" "More than two arguments were given." "Please only pass in a maximum of 2 arguments, your ini file and the optional restore command."
)
rem ----------------
rem Get CLI arg(s)
rem ----------------
rem Set the variables.
set $configPath=
set $restoreFlag=false
rem Check if two arguments were given.
if !$argumentCount! equ 2 (
rem Check for first combination.
if /i "%1" equ "restore" (
set $restoreFlag=true
set $configPath=%~f2
) else (
rem Check for second combination.
if /i "%2" equ "restore" (
set $restoreFlag=true
set $configPath=%~f1
) else (
rem CLI command not recognised.
call :failure "CLI Argument Error" "ssl_conf.bat" "Argument not recognised."
)
)
)
rem Check if one argument was given.
if !$argumentCount! equ 1 (
set $configPath=%~f1
)
rem -------------------------------------------
rem Check CLI config arg has '.ini' extension
rem -------------------------------------------
rem Check if the file is an .ini file.
call :isIniFile "!$configPath!"
rem Check the result.
if /i "!$result!" equ "false" (
rem Config file name does not contain a valid extension (.ini)
call :failure "CLI Argument Error" "ssl_conf.bat" "Your config file must have a .ini extension."
)
rem --------------------------
rem CONFIG FILE
rem --------------------------
rem Check config file exists
rem --------------------------
rem Check that the configuration file path exists.
if not exist "%$configPath%" (
call :failure "CLI Argument Error" "ssl_conf.bat" "Path to ^"!$configPath!^" does not exist."
)
rem -------------------
rem Parse config file
rem -------------------
rem Set the default variables.
set $inSection=false
set $totalConfigDomains=0
rem Parse the configuration file line by line, skipping (by default) all blank lines and lines starting with a semicolon.
for /F "usebackq delims=" %%a in ("!$configPath!") do (
rem Set the variables.
set $line=%%a
rem Check for a section.
if "!$line:~0,1!" == "[" (
if "!$line:~-1!" == "]" (
set $inSection=true
set /A $totalConfigDomains=$totalConfigDomains+1
set $key=name
set $value=!$line:~1,-1!
) else (
call :failure "Config File Error" "!$configPath!" "!$line!" "A [Section] name must not contain any trailing characters."
)
) else (
rem Split the line around the '=' sign (assuming one exists).
for /F "tokens=1,2 delims==" %%b in ("!$line!") do (
rem Check for a valid key / value pair.
if not "%%b%%c" == "%%c%%b" (
set $key=%%b
set $value=%%c
) else (
call :failure "Config File Error" "!$configPath!" "^"!$line!^" key or value missing."
)
)
)
rem Build the config array.
if "!$inSection!" == "false" (
set $config[!$key!]=!$value!
) else (
set $config[!$totalConfigDomains!][!$key!]=!$value!
)
)
call :logToScreen "Parsed configuration file."
rem ---------------------
rem Initialise log path
rem ---------------------
rem Create the log path if it does not exist.
if not exist "%$config[wampServerExtensionsPath]%\logs" (
md "%$config[wampServerExtensionsPath]%\logs"
call :logToScreen "Created common 'logs' directory."
) else (
call :logToScreen "Common 'logs' directory already exists."
)
rem Set the log file path.
set $logFilePath=!$config[wampServerExtensionsPath]!\logs\!$scriptLogFileName!
rem Write header to log file.
(
echo:
echo ===========================================================
echo:
echo !date! : WampServer SSL Auto Config Script ^(v%$scriptVersion%^)
echo:
) >> "!$logFilePath!"
call :logToBoth "Script initialised."
call :logToBoth "---------------------------------------------"
rem --------------------------------------
rem WAMPSERVER
rem --------------------------------------
rem Check WampServer install path exists
rem --------------------------------------
rem Check that the WampServer installation path exists.
if not exist "%$config[wampServerInstallPath]%" (
call :failure "Config File Error" "!$configPath!" "The WampServer installation path ^"!$config[wampServerInstallPath]!^" does not exist."
) else (
call :logToFile "Found WampServer installation path at '!$config[wampServerInstallPath]!'"
call :logToScreen "Found WampServer installation path."
)
rem Remove any trailing slash.
call :removeTrailingSlash "%$config[wampServerInstallPath]%"
set $config[wampServerInstallPath]=!$result!
rem --------------------------
rem APACHE
rem --------------------------
rem Check Apache path exists
rem --------------------------
rem Check that the WampServer Apache folder path exists.
if not exist "%$config[wampServerInstallPath]%\%$subPathToApacheFolders%" (
call :failure "SSL Script" "ssl_conf.bat" "Path to WampServer Apache folder ^"!$config[wampServerInstallPath]!\!$subPathToApacheFolders!^" does not exist." "Please file an issue on github."
) else (
call :logToFile "Found WampServer Apache path at '%$config[wampServerInstallPath]%\%$subPathToApacheFolders%'"
call :logToScreen "Found WampServer Apache path."
)
rem -------------------------------
rem Get installed Apache versions
rem -------------------------------
rem Initialise the counter.
set $totalApacheVersionsInstalled=0
call :logToBoth "Found WampServer Apache installation(s):"
rem Iterate through the WampServer Apache folder paths adding each version folder to the array.
for /f "delims=" %%a in ('dir %$config[wampServerInstallPath]%\%$subPathToApacheFolders% /AD /B') do (
rem ------------------
rem Get version path
rem ------------------
if "%%a" neq "modules_sup" (
set /A $totalApacheVersionsInstalled=$totalApacheVersionsInstalled+1
rem Set the Apache version folder path to the array.
set $installedApacheVersionsArray[!$totalApacheVersionsInstalled!]=%%a
set $installedApacheVersionPathsArray[!$totalApacheVersionsInstalled!]=%$config[wampServerInstallPath]%\%$subPathToApacheFolders%\%%a
call :logToBoth " '%%a'"
)
)
rem ------------------------------
rem OS
rem ------------------------------
rem Check OS 'hosts' file exists
rem ------------------------------
rem Check if the OS 'hosts' file exists.
if not exist "%$pathToOSHostsFile%" (
call :failure "SSL Script Error" "ssl_conf.bat" "Path to OS 'hosts' file ^"!$pathToOSHostsFile!^" does not exist" "Please file an issue on github."
)
call :logToFile "Found OS 'hosts' file at '%$pathToOSHostsFile%'"
call :logToScreen "Found OS 'hosts' file."
rem -------------------------------
rem Get OS service name of Apache
rem -------------------------------
rem Get the Apache (OS) service name from the WampServer 'wampmanager.conf' file.
call :getIniValue "%$config[wampServerInstallPath]%\wampmanager.conf" "service" "ServiceApache"
rem Check if a result was return.
if [!$result!] == [] (
call :failure "SSL Script Error" "ssl_conf.bat" "Unable to find 'ServiceApache' key in ^"!$config[wampServerInstallPath]!\wampmanager.conf^"" "Please file an issue on github."
)
rem Strip the surrounding quotes from the value.
set $apacheServiceName=!$result:~1,-1!
call :logToFile "Found OS Apache service name '!$apacheServiceName!'"
call :logToScreen "Found OS Apache service name."
rem --------------------
rem Restore (CLI Flag)
rem --------------------
rem Check if the restore flag is set to true.
if /i "!$restoreFlag!" equ "true" (
rem ------------------------------
rem Loop through Apache versions
rem ------------------------------
call :logToBoth "Restoring Apache config file(s):"
rem Iterate though the installed Apache version folders.
for /l %%a in (1,1,%$totalApacheVersionsInstalled%) do (
rem ----------------------------
rem Restore Apache config file
rem ----------------------------
rem Check if the 'httpd-backup.conf' file exists.
if exist "!$installedApacheVersionPathsArray[%%a]!\conf\httpd-backup.conf" (
rem Restore the 'httpd.conf' file.
type "!$installedApacheVersionPathsArray[%%a]!\conf\httpd-backup.conf" > "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
rem Delete the 'httpd-backup.conf' file.
call :deleteFileIfExists "!$installedApacheVersionPathsArray[%%a]!\conf\httpd-backup.conf"
call :logToBoth " '!$installedApacheVersionsArray[%%a]!' restored."
) else (
call :logToBoth " '!$installedApacheVersionsArray[%%a]!' backup not found."
)
rem --------------------------------
rem Validate Apache config file(s)
rem --------------------------------
rem Get the validity of the Apache config file(s).
call :validateApacheConfigFile "!$installedApacheVersionPathsArray[%%a]!"
rem Check the result.
if "!$result!" neq "Syntax OK" (
call :failure "WampServer Apache Validation Error" "See below" "!$result!" "Please correct the error in the stated configuration file and restart WampServer."
)
call :logToBoth " '!$installedApacheVersionsArray[%%a]!' validated."
)
rem ----------------------
rem Loop through domains
rem ----------------------
call :logToBoth "Deleting certificates from store:"
rem Iterate through all config listed domains.
for /l %%a in (1,1,%$totalConfigDomains%) do (
rem -----------------------------------
rem Delete cert(s) from Windows store
rem -----------------------------------
rem Delete certificate from 'trusted root certificate store'.
rem View store by entering 'certmgr.msc' at the command line.
certutil -delstore "root" "!$config[%%a][hostname]!" > nul
call :logToBoth " '!$config[%%a][hostname]!'"
)
rem -------------------------
rem Restore OS 'hosts' file
rem -------------------------
rem Set 'hosts' file updated flag.
set $osHostsFileUpdated=false
call :logToBoth "Attempting to restore OS 'hosts' file."
rem Check if the OS 'hosts-backup' file exists.
if exist "%$pathToOSHostsFile%-backup" (
rem Restore the OS 'hosts' file.
rem Unable to redirect error output without breaking updating of file...
type "!$pathToOSHostsFile!-backup" > "!$pathToOSHostsFile!" 2>nul
rem Check if the OS 'hosts' file matches the 'hosts-backup'file.
fc "!$pathToOSHostsFile!-backup" "!$pathToOSHostsFile!" >nul && (
set $osHostsFileUpdated=true
call :deleteFileIfExists "!$pathToOSHostsFile!-backup" 2>nul
call :logToBoth "Restored OS 'hosts' file."
) || (
call :logToBoth "Unable to restore OS 'hosts' file."
)
) else (
set $osHostsFileUpdated=true
call :logToBoth "OS 'hosts' backup file not found."
)
rem ----------------
rem Restart Apache
rem ----------------
rem Restart Apache.
call :logToBoth "Re-starting Apache."
call :restartApache
call :logToBoth "Re-started Apache."
rem ----------------------------------
rem Exit showing appropriate message
rem ----------------------------------
if /i "!$osHostsFileUpdated!" equ "false" (
call :warning
) else (
call :success
)
)
rem ------------------------------
rem APACHE
rem ------------------------------
rem Loop through Apache versions
rem ------------------------------
call :logToBoth "Validating Apache config file(s):"
rem Iterate though the installed Apache version folders.
for /l %%a in (1,1,%$totalApacheVersionsInstalled%) do (
rem ------------------------------
rem Validate Apache config files
rem ------------------------------
rem Get the validity of the Apache config file(s).
call :validateApacheConfigFile "!$installedApacheVersionPathsArray[%%a]!"
rem Check the result.
if "!$result!" neq "Syntax OK" (
call :failure "WampServer Apache Validation Error" "See below" "!$result!" "Please correct the error in the stated configuration file and restart WampServer."
)
call :logToBoth " '!$installedApacheVersionsArray[%%a]!'"
)
rem ------------------------------------
rem Get latest OpenSSL executable path
rem ------------------------------------
rem Use the latest Apache version folder.
set $pathToLatestOpenSslExe=!$installedApacheVersionPathsArray[%$totalApacheVersionsInstalled%]!\!$subPathToApacheOpenSslExe!
call :logToBoth "Using 'openssl.exe' from '!$installedApacheVersionsArray[%$totalApacheVersionsInstalled%]!'"
rem ---------------------------------
rem Start Apache service if stopped
rem ---------------------------------
rem Get the status of the Apache service.
call :getServiceStatus "!$apacheServiceName!"
rem Start Apache service if it is not already running.
if /i "!$result!" neq "Running" (
call :logToBoth "Starting Apache service."
rem Start Apache.
net start !$apacheServiceName! > nul
rem Get the status of the Apache service.
call :getServiceStatus "!$apacheServiceName!"
rem Check if the Apache service is running.
if /i "!$result!" neq "Running" (
call :failure "WampServer Apache Service Startup Error" "See below" "!$result!" "Please correct the error and restart WampServer."
) else (
call :logToBoth "Apache service started."
)
) else (
call :logToBoth "Apache service already started."
)
rem ------------------------------------
rem BACKUP
rem ------------------------------------
rem Backup OS 'hosts' file (once only)
rem ------------------------------------
if not exist "%$pathToOSHostsFile%-backup" (
type "!$pathToOSHostsFile!" > "!$pathToOSHostsFile!-backup"
call :logToBoth "OS 'hosts' file backed up."
) else (
call :logToBoth "OS 'hosts' file already backed up."
)
rem ------------------------------
rem Loop through Apache versions
rem ------------------------------
call :logToBoth "Backing up Apache 'httpd.conf' file(s):"
rem Iterate though the installed Apache version folders.
for /l %%a in (1,1,%$totalApacheVersionsInstalled%) do (
rem ---------------------------------------------
rem Backup Apache 'httpd.conf' file (once only)
rem ---------------------------------------------
if not exist "!$installedApacheVersionPathsArray[%%a]!\conf\httpd-backup.conf" (
type "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf" > "!$installedApacheVersionPathsArray[%%a]!\conf\httpd-backup.conf"
call :logToBoth " '!$installedApacheVersionsArray[%%a]!'"
) else (
call :logToBoth " '!$installedApacheVersionsArray[%%a]!' already backed up."
)
)
rem --------------------------------------------------------
rem Create common 'certs', 'logs' And 'vhosts' directories
rem --------------------------------------------------------
if not exist "%$config[wampServerExtensionsPath]%\certs" (
md "%$config[wampServerExtensionsPath]%\certs"
call :logToFile "Created common 'certs' directory at '!$config[wampServerExtensionsPath]!\certs'"
call :logToScreen "Created common 'certs' directory."
) else (
call :logToFile "Common 'certs' directory already exists at '!$config[wampServerExtensionsPath]!\certs'"
call :logToScreen "Common 'certs' directory already exists."
)
if not exist "%$config[wampServerExtensionsPath]%\vhosts\http" (
md "%$config[wampServerExtensionsPath]%\vhosts\http"
call :logToFile "Created common 'vhosts\http' directory at '!$config[wampServerExtensionsPath]!\vhosts\http'"
call :logToScreen "Created common 'vhosts\http' directory."
) else (
call :logToFile "Common 'vhosts\http' directory already exists at '!$config[wampServerExtensionsPath]!\vhosts\http'"
call :logToScreen "Common 'vhosts\http' directory already exists."
)
if not exist "%$config[wampServerExtensionsPath]%\vhosts\https\conf" (
md "%$config[wampServerExtensionsPath]%\vhosts\https\conf"
call :logToFile "Created common 'vhosts\https' directory at '!$config[wampServerExtensionsPath]!\vhosts\https'"
call :logToScreen "Created common 'vhosts\https' directory."
) else (
call :logToFile "Common 'vhosts\https' directory already exists at '!$config[wampServerExtensionsPath]!\vhosts\https'"
call :logToScreen "Common 'vhosts\https' directory already exists."
)
rem ------------------------------------------------
rem (Re)Create common vhosts 'httpd-ssl.conf' file
rem ------------------------------------------------
if not exist "%$config[wampServerExtensionsPath]%\vhosts\https\conf\httpd-ssl.conf" (
(call :httpdSslCommonConfigFile) >> "%$config[wampServerExtensionsPath]%\vhosts\https\conf\httpd-ssl.conf"
call :logToBoth "Created common 'httpd-ssl.conf' file."
) else (
call :deleteFileIfExists "%$config[wampServerExtensionsPath]%\vhosts\https\conf\httpd-ssl.conf"
(call :httpdSslCommonConfigFile) >> "%$config[wampServerExtensionsPath]%\vhosts\https\conf\httpd-ssl.conf"
call :logToBoth "Re-created common 'httpd-ssl.conf' file."
)
rem ----------------------
rem Loop through domains
rem ----------------------
rem Iterate through all config listed domains.
for /l %%a in (1,1,%$totalConfigDomains%) do (
rem Set the variables for easier replacement in the config files.
set $config[name]=!$config[%%a][name]!
set $config[hostname]=!$config[%%a][hostname]!
set $config[documentRoot]=!$config[%%a][documentRoot]!
set $config[http2]=!$config[%%a][http2]!
rem Show domain name.
call :logToBoth "---------------------------------------------"
call :logToBoth "!$config[name]! ^(!$config[hostname]!^)"
rem ---------------------------
rem Create domain directories
rem ---------------------------
rem Create the 'certs' directory.
if not exist "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!" (
md "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!"
call :logToBoth " 'certs' directory created."
) else (
call :logToBoth " 'certs' directory already exists."
)
rem Create the 'logs' directory.
if not exist "!$config[wampServerExtensionsPath]!\logs\!$config[hostname]!" (
md "!$config[wampServerExtensionsPath]!\logs\!$config[hostname]!"
call :logToBoth " 'logs' directory created."
) else (
call :logToBoth " 'logs' directory already exists."
)
rem -------------------------------
rem (Re)Create 'openssl.cnf' file
rem -------------------------------
if not exist "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!\openssl.cnf" (
(call :openSslCnfFile) >> "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!\openssl.cnf"
call :logToBoth " Created 'openssl.conf' file."
) else (
call :deleteFileIfExists "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!\openssl.cnf"
(call :openSslCnfFile) >> "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!\openssl.cnf"
call :logToBoth " Re-created 'openssl.cnf' file."
)
rem ----------------------------
rem (Re)Create HTTP vhost file
rem ----------------------------
if not exist "!$config[wampServerExtensionsPath]!\vhosts\http\!$config[hostname]!.conf" (
(call :apache24HttpVhostConfigFile) >> "%$config[wampServerExtensionsPath]%\vhosts\http\!$config[hostname]!.conf"
call :logToBoth " Created Virtual Host http file."
) else (
call :deleteFileIfExists "!$config[wampServerExtensionsPath]!\vhosts\http\!$config[hostname]!.conf"
(call :apache24HttpVhostConfigFile) >> "%$config[wampServerExtensionsPath]%\vhosts\http\!$config[hostname]!.conf"
call :logToBoth " Re-created Virtual Host http file."
)
rem -----------------------------
rem (Re)Create HTTPS vhost file
rem -----------------------------
if not exist "!$config[wampServerExtensionsPath]!\vhosts\https\!$config[hostname]!.conf" (
if /i "!$config[http2]!" equ "false" (
(call :apache24Https11VhostConfigFile) >> "!$config[wampServerExtensionsPath]!\vhosts\https\!$config[hostname]!.conf"
) else (
(call :apache24Https2VhostConfigFile) >> "!$config[wampServerExtensionsPath]!\vhosts\https\!$config[hostname]!.conf"
)
call :logToBoth " Created Virtual Host https file."
) else (
call :deleteFileIfExists "!$config[wampServerExtensionsPath]!\vhosts\https\!$config[hostname]!.conf"
if /i "!$config[http2]!" equ "false" (
(call :apache24Https11VhostConfigFile) >> "!$config[wampServerExtensionsPath]!\vhosts\https\!$config[hostname]!.conf"
) else (
(call :apache24Https2VhostConfigFile) >> "!$config[wampServerExtensionsPath]!\vhosts\https\!$config[hostname]!.conf"
)
call :logToBoth " Re-created Virtual Host https file."
)
rem ------------------------------------
rem Delete old cert from Windows store
rem ------------------------------------
rem Delete certificate from 'trusted root certificate store'.
rem View store by entering 'certmgr.msc' at the command line.
certutil -delstore "root" "!$config[hostname]!" > nul
call :logToBoth " Deleted old certificate from store."
rem -----------------------
rem Create SSL key & cert
rem -----------------------
rem Create private (and public) RSA key.
cmd /C !$pathToLatestOpenSslExe! "genrsa" "-out" "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!\private.pem" 2> nul
call :logToBoth " Generated certificate keys."
rem Remove private key passphrase.
cmd /C !$pathToLatestOpenSslExe! "rsa" "-in" "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!\private.pem" "-out" "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!\private.key" 2> nul
call :logToBoth " Removed certificate passphrase."
rem Generate self signed certificate.
cmd /C !$pathToLatestOpenSslExe! "req" "-x509" "-days" "!$config[sslDays]!" "-key" "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!\private.key" "-out" "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!\server.crt" "-config" "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!\openssl.cnf"
call :logToBoth " Generated Certificate."
rem Delete the redundant RSA key file.
call :deleteFileIfExists "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!\private.pem"
call :logToBoth " Deleted 'private.pem' file."
rem -------------------------------
rem Add new cert to Windows store
rem -------------------------------
rem Add certificate to 'trusted root certificate store'.
rem View store by entering 'certmgr.msc' at the command line.
certutil -f -addstore "root" "!$config[wampServerExtensionsPath]!\certs\!$config[hostname]!\server.crt" > nul
call :logToBoth " Added new certificate to store."
rem -------------------------------
rem Add domain to OS 'hosts' file
rem -------------------------------
rem Set 'hosts' file updated flag.
set $osHostsFileUpdated=false
rem Check if the hostname has already been added to the 'hosts' file.
call :findInFile "]# Hostname: !$config[hostname]!" "!$pathToOSHostsFile!"
rem Check the result.
if /i "!$result!" equ "false" (
rem Try adding the hostname.
call :logToBoth " Attempting to add hostname to OS 'hosts' file."
rem Unable to redirect error output without breaking updating of file...
(call :includeOsHostsFile) >> "!$pathToOSHostsFile!" 2>nul
rem Check if the hostname has been added.
call :findInFile "]# Hostname: !$config[hostname]!" "!$pathToOSHostsFile!"
rem Check the result.
if /i "!$result!" equ "true" (
set $osHostsFileUpdated=true
call :logToBoth " Hostname added to OS 'hosts' file."
) else (
call :logToBoth " Unable to added hostname to OS 'hosts' file."
)
) else (
rem Hostname already added to the 'hosts' file.
set $osHostsFileUpdated=true
call :logToBoth " Hostname already added to OS 'hosts' file."
)
)
call :logToBoth "---------------------------------------------"
rem ------------------------------
rem Loop through Apache versions
rem ------------------------------
call :logToBoth "Updating Apache primary config file(s):"
rem Iterate though the installed Apache version folders.
for /l %%a in (1,1,%$totalApacheVersionsInstalled%) do (
rem Show the header info.
call :logToBoth " '!$installedApacheVersionsArray[%%a]!\conf\httpd.conf'"
rem --------------------------------------------
rem Uncomment 'socache_shmcb_module' module
rem
rem Low level shared memory based object cache
rem for caching information such as SSL
rem sessions and authentication credentials.
rem --------------------------------------------
rem Check if the module is commented out / disabled.
call :findInFile "]#LoadModule socache_shmcb_module modules/mod_socache_shmcb.so" "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
if /i "!$result!" equ "true" (
rem Uncomment / enable the module.
call :findAndReplaceInFile "]#LoadModule socache_shmcb_module modules/mod_socache_shmcb.so" "]LoadModule socache_shmcb_module modules/mod_socache_shmcb.so" "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
call :logToBoth " 'socache_shmcb_module' uncommented."
) else (
rem Module already uncommented / enabled.
call :logToBoth " 'socache_shmcb_module' already uncommented."
)
rem -------------------------------
rem Uncomment 'ssl_module' module
rem
rem This module used the socache
rem interface to provide a
rem session cache and stapling
rem cache.
rem -------------------------------
rem Check if the module is commented out / disabled.
call :findInFile "]#LoadModule ssl_module modules/mod_ssl.so" "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
if /i "!$result!" equ "true" (
rem Uncomment / enable the module.
call :findAndReplaceInFile "]#LoadModule ssl_module modules/mod_ssl.so" "]LoadModule ssl_module modules/mod_ssl.so" "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
call :logToBoth " 'ssl_module' uncommented."
) else (
rem Module already uncommented / enabled.
call :logToBoth " 'ssl_module' already uncommented."
)
rem -----------------------------------------
rem Uncomment 'http2_module' module
rem
rem This module enables HTTP/2 support.
rem
rem HTTP/2 functionality set per development
rem domain.
rem
rem OpenSSL version must be greater than or
rem equal to 1.0.2 for HTTP/2 compatibility.
rem
rem OpenSSL cipher suite must be greater
rem than or equal to TLS 1.3 for HTTP/2
rem compatibility.
rem -----------------------------------------
rem Check if the module is commented out / disabled.
call :findInFile "]#LoadModule http2_module modules/mod_http2.so" "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
if /i "!$result!" equ "true" (
rem Uncomment / enable the module.
call :findAndReplaceInFile "]#LoadModule http2_module modules/mod_http2.so" "]LoadModule http2_module modules/mod_http2.so" "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
call :logToBoth " 'http2_module' uncommented."
) else (
rem Module already uncommented / enabled.
call :logToBoth " 'http2_module' already uncommented."
)
rem ----------------------------------------
rem Add vhosts HTTPS 'httpd-ssl.conf' link
rem ----------------------------------------
rem Check if the link has been added.
call :findInFile "]# SSL Config - Additional" "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
if /i "!$result!" equ "false" (
rem Add the link.
(call :includeSslInConfigFile) >> "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
call :logToBoth " Added SSL Config link."
) else (
rem Link already added.
call :logToBoth " SSL Config link already added."
)
rem -------------------------------
rem Add vhosts HTTP '*.conf' link
rem -------------------------------
rem Check if the link has been added.
call :findInFile "]# HTTP Vhost(s) - Additional" "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
if /i "!$result!" equ "false" (
rem Add the link.
(call :includeHttpVhostInConfigFile) >> "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
call :logToBoth " Added HTTP vhosts link."
) else (
rem Link already added.
call :logToBoth " HTTP vhosts link already added."
)
rem --------------------------------
rem Add vhosts HTTPS '*.conf' link
rem --------------------------------
rem Check if the link has been added.
call :findInFile "]# HTTPS Vhost(s) - Additional" "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
if /i "!$result!" equ "false" (
rem Add the link.
(call :includeHttpsVhostInConfigFile) >> "!$installedApacheVersionPathsArray[%%a]!\conf\httpd.conf"
call :logToBoth " Added HTTPS vhosts link."
) else (
rem Link already added.
call :logToBoth " HTTPS vhosts link already added."
)
rem -----------------------
rem Validate config files
rem -----------------------
rem Get the validity of the Apache config files.
call :validateApacheConfigFile "!$installedApacheVersionPathsArray[%%a]!"
rem Check the result.
if "!$result!" neq "Syntax OK" (
call :failure "WampServer Apache Validation Error" "See below" "!$result!" "Please correct the error in the stated configuration file and restart WampServer."
)
call :logToBoth " Validated config file(s)."
)
call :logToBoth "---------------------------------------------"
rem -----------
rem Flush DNS
rem -----------
call :logToBoth "Flushing DNS."
ipconfig /flushdns > nul
call :logToBoth "Flushed DNS."
rem ---------------------
rem Restart WampServer
rem ---------------------
rem Restart Apache.
call :logToBoth "Re-starting Apache."
call :restartApache
call :logToBoth "Re-started Apache."
rem ----------------------------------
rem Exit showing appropriate message
rem ----------------------------------
if /i "!$osHostsFileUpdated!" equ "false" (
call :warning
) else (
call :success
)
rem ====================================================================================================================
rem Functions
rem ====================================================================================================================
rem ---------------
rem Log to screen
rem ---------------
:logToScreen $message
rem Set the variable.
set $message=%~1
rem Display the message.
echo %time% : !$message!
exit /B
rem -------------
rem Log to file
rem -------------
:logToFile $message
rem Set the variable.
set $message=%~1
rem Log the message.
echo %time% : !$message! >> "!$logFilePath!"
exit /B
rem ----------------------
rem Log to screen & file
rem ----------------------
:logToBoth $message
call :logToScreen %1
call :logToFile %1
exit /B
rem ----------------
rem Restart Apache
rem ----------------
:restartApache
rem Stop Apache.
net stop %$apacheServiceName% > nul
rem Start Apache.
net start %$apacheServiceName% > nul
exit /B
rem -----------------------------
rem Validate Apache config file
rem -----------------------------
:validateApacheConfigFile $file
rem Set the variable scope.
setlocal
rem Set the variables.
set $file=%~1
set $output=
rem As "all" Apache output goes to STDERR, we have redirect it back to STDOUT so it can be processed.
for /f "tokens=* delims=" %%a in ('%$file%\%$subPathToApacheHttpdExe% -t 2^>^&1') do (
set $output=%%a
goto :exitValidateApacheConfigFilesFunction
)
:exitValidateApacheConfigFilesFunction
endlocal & set $result=%$output%
exit /B
rem --------------------
rem Get service status
rem --------------------
:getServiceStatus $name