forked from gearbox-solutions/FileMaker-LetsEncrypt-Win
-
Notifications
You must be signed in to change notification settings - Fork 9
/
GetSSL.ps1
1234 lines (1078 loc) · 45.9 KB
/
GetSSL.ps1
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
<#
.SYNOPSIS
Get an SSL certificate from Let's Encrypt and install it on FileMaker Server.
.NOTES
Author: Daniel Smith [email protected]
Contributor: Nick Roemer [email protected]
Revised: 2024-SEP-19
Version: 2.2.1
.LINK
https://github.com/dansmith65/FileMaker-LetsEncrypt-Win
.LINK
http://bluefeathergroup.com/blog/how-to-use-lets-encrypt-ssl-certificates-with-filemaker-server/
.LINK
https://github.com/rmbolger/Posh-ACME/wiki/%28Advanced%29-Manual-HTTP-Challenge-Validation
.EXAMPLE
.\GetSSL.ps1 -Setup test.com [email protected] -WhatIf
Display the inputs, then exit; use to verify you passed parameters in the correct format
.EXAMPLE
.\GetSSL.ps1 -Renew -Confirm:0
Don't ask for confirmation; use this parameter when called from a scheduled task.
To have this script run silently, it must also be able to perform fmsadmin.exe without asking for username and password. There are two ways to do that:
1. Add a group name that is allowed to access the Admin Console and run the script as a user
that belongs to the group.
2. Run this script with the Setup parameter the first time you use it and enter your admin
console credentials when it asks you.
#>
[cmdletbinding(SupportsShouldProcess=$true,ConfirmImpact='High',DefaultParameterSetName='InstallDependencies')]
Param(
<#
Specify an alternate web root directory for HTTP-01 challenge. For use if FileMaker's web server
is not exposed to the internet, but another web server is. As long as that other server's
filesystem is accessible to the FileMaker server, anyway.
For example, -WebRoot '\\web-server\C$\inetpub\wwwroot'
#>
[Parameter(ParameterSetName='Setup')]
[Parameter(ParameterSetName='ScheduleTask')]
[Parameter(ParameterSetName='Renew')]
[string] $WebRoot,
<#
Install all dependent libraries; restart may be required if .NET is updated. Run this once
before using this script for the first time. This action is performed if no parameters sent to
the script.
Include the -Force parameter to update dependencies.
#>
[Parameter(ParameterSetName='InstallDependencies')]
[switch] $InstallDependencies,
<#
Store new domains and emails, or modify them, then get and install a certificate. This is
intended to be run with a user at the console so they can enter fmsadmin credentials, if needed.
#>
[Parameter(ParameterSetName='Setup')]
[switch] $Setup,
<#
Array of domain(s) for which you would like an SSL Certificate. Let's Encrypt will peform
separate validation for each of the domains, so be sure that your server is reachable at all of
them before attempting to get a certificate. 100 domains is the max.
#>
[Parameter(ParameterSetName='Setup',Mandatory=$True,Position=1)]
[Alias('d')]
[string[]] $Domains,
<#
Contact email address(es) to your real email address so Let's Encrypt can contact you if there
are any problems (or if the certificate is about to expire).
#>
[Parameter(ParameterSetName='Setup',Mandatory=$True,Position=2)]
[Alias('e')]
[string[]] $Emails,
<#
Installs the most recently retrieved certificate. Useful if a certificate was successfully
retrieved, but something failed before it was installed.
#>
[Parameter(ParameterSetName='InstallCertificate')]
[switch] $InstallCertificate,
<# Schedule a task via Windows Task Scheduler to renew the certificate automatically. #>
[Parameter(ParameterSetName='ScheduleTask')]
[Alias('s')]
[switch] $ScheduleTask,
<#
When scheduling a task, specify an interval to repeat on. Default is 63 days because:
- Let's Encrypt's recommendation is to renew when a certificate has a third of it's total
lifetime left.
- if interval is divisible by 7, then it will always occur on the same day of the week
#>
[Parameter(ParameterSetName='ScheduleTask')]
[Alias('i')]
[string] $IntervalDays=63,
<#
When scheduling a task, specify a time of day to run it. Can optionally specify the exact
date/time of the first schedule. Value should be a PowerShell DateTime object, or a string
that can be converted to DateTime.
#>
[Parameter(ParameterSetName='ScheduleTask')]
[Alias('t')]
[DateTime] $Time="4:00am",
<# Renew the most recently used certificate. #>
[Parameter(ParameterSetName='Renew')]
[switch] $Renew,
<# Store credentials and SMTP info so this script can send logs when it runs. #>
[Parameter(ParameterSetName='ConfigureEmail')]
[switch] $ConfigureEmail,
<#
Use Let's Encrypt Staging server and don't restart FileMaker Server service. Use this option for
testing/setup, but beware that the certificate will be imported, so you would either need to
restore the old certificate or call this script again without this parameter to install a
production certificate.
Include the Force parameter to also restart FileMaker Server, which is a more complete test.
#>
[Parameter(ParameterSetName='InstallDependencies')]
[Parameter(ParameterSetName='Setup')]
[Parameter(ParameterSetName='InstallCertificate')]
[Parameter(ParameterSetName='ScheduleTask')]
[Parameter(ParameterSetName='Renew')]
[switch] $Staging,
<#
When installing dependencies, re-install (or update) even if they are already installed.
When renewing, force renewal even if certificate is not recommended for renewal yet.
When using Staging parameter, force FMS to restart.
#>
[Parameter(ParameterSetName='InstallDependencies')]
[Parameter(ParameterSetName='Setup')]
[Parameter(ParameterSetName='InstallCertificate')]
[Parameter(ParameterSetName='ScheduleTask')]
[Parameter(ParameterSetName='Renew')]
[switch] $Force,
<#
Add a Firewall rule named "GetSSL Port 80" to allow inbound connections on port 80 while
waiting for validation, then modify it to block port 80 at all other times.
Once created, this script will not change the firewall rule other than the allow/block
state, so any changes you make to the rule yourself will persist.
#>
[Parameter(ParameterSetName='InstallDependencies')]
[Parameter(ParameterSetName='Setup')]
[Parameter(ParameterSetName='InstallCertificate')]
[Parameter(ParameterSetName='ScheduleTask')]
[Parameter(ParameterSetName='Renew')]
[switch] $ModifyFirewall
)
<# Exit immediately on error #>
$ErrorActionPreference = "Stop"
#########################################################################################################################
# Functions
function Backup-File {
Param(
[Parameter(Position=1)]
[string]$path,
[string]$BackupDirectory = $(Join-Path $FMSPath ('CStore\Backup\' + $Start.ToString("yyyy-MM-dd_HHmmss") +"\"))
)
If ( -not (Test-Path $BackupDirectory) ) { New-Item -ItemType directory -Path $BackupDirectory | Out-Null }
Write-Output "backing up $(Split-Path $path -Leaf) to $BackupDirectory"
Copy-Item $path $BackupDirectory
}
function Get-ScriptLineNumber { return $MyInvocation.ScriptLineNumber }
function Send-Email ($subject, $body) {
$result = ""
try {
$credentials = Get-StoredCredential -Target "GetSSL Send Email"
if ($credentials) {
$smtpInfo = (Get-StoredCredential -AsCredentialObject -Target "GetSSL Send Email").Comment | ConvertFrom-Json
if (! $Emails) {
$Emails = ((Get-PAAccount).contact).Replace('mailto:','')
}
if ($smtpInfo) {
Send-MailMessage -Subject $subject -Body $body -Encoding UTF8 -Credential $credentials -To $Emails `
-From $smtpInfo.from `
-SmtpServer $smtpInfo.server `
-Port $smtpInfo.port `
-UseSsl:$smtpInfo.useSSL
$result = "email sent"
} else {
$result = "email could not be sent; no credentials loaded"
}
}
$credentials = $null
}
catch {
# don't let this stop the script from continuing
$result = ($_ | Out-String)
$result += "Stack Trace:`r`n"
$result += $_.ScriptStackTrace
}
return $result
}
function Install-Dependencies {
$PackageProvider = Get-PackageProvider -ListAvailable -Name NuGet -ErrorAction:Ignore
if (-not($PackageProvider) -or ($PackageProvider.Version -lt [System.Version]"2.8.5.201")) {
Write-Output "installing NuGet package provider"
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force | Out-Null
}
if ((Get-ItemProperty "HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full").Release -lt 461308) {
if ($PSCmdlet.ShouldProcess(
"[OPTIONAL] Update Dot Net Framework to latest version, so the main branch of Posh-ACME can be used? Restart will likely be required which you can choose to do later, but won't be able to proceed with certificate installation until you do.",
"[OPTIONAL] Update Dot Net Framework to latest version, so the main branch of Posh-ACME can be used? Restart will likely be required which you can choose to do later, but won't be able to proceed with certificate installation until you do.",
"Update Dot Net Framework?"
)) {
# https://docs.microsoft.com/en-us/dotnet/framework/deployment/deployment-guide-for-developers
$dlurl = 'https://go.microsoft.com/fwlink/?LinkId=2085155'
$installerPath = Join-Path $env:TEMP ndp48-web.exe
Invoke-WebRequest $dlurl -OutFile $installerPath
Start-Process -FilePath $installerPath -Wait -Args "/passive /promptrestart"
# Am assuming the computer will have to restart and this script run again at this point
}
}
if ((Get-ItemProperty "HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full").Release -lt 461308) {
if ($Force -or -not(Get-Module -Listavailable -Name Posh-ACME.net46)) {
Write-Output "Install Posh-ACME.net46"
Install-Module -Name Posh-ACME.net46 -AllowClobber -Confirm:$false -Force
}
} else {
if ($Force -or -not(Get-Module -Listavailable -Name Posh-ACME)) {
Write-Output "Install Posh-ACME"
Install-Module -Name Posh-ACME -AllowClobber -Confirm:$false -Force
}
}
if ($Force -or -not(Get-Module -Listavailable -Name CredentialManager)) {
Write-Output "Install CredentialManager"
Install-Module -Name CredentialManager -AllowClobber -Confirm:$false -Force
}
}
function Install-Cert {
<# validate FMSPath #>
if (-not(Test-Path $fmsadmin)) {
throw "fmsadmin could not be found at: '$fmsadmin', please check the FMSPath parameter: '$FMSPath'"
}
Write-Output "Confirming access to fmsadmin.exe: ______________________________________________"
<# FileMaker Server must be started to import a certificate (and to confirm access) #>
if ((Get-Service "FileMaker Server").Status -ne "Running") {
Write-Output "FileMaker Server service was not running, it will be started now"
Start-Service "FileMaker Server"
Start-Sleep 2 # give the service a few seconds to start before testing if fmserver is running
}
if (-not(Get-Process fmserver -ErrorAction:Ignore)) {
Write-Output "FileMaker Server process was not running, it will be started now"
Invoke-FMSAdmin start, server -Timeout 90
Start-Sleep -Seconds 1
if (-not(Get-Process fmserver -ErrorAction:Ignore)) {
throw ("server process still not running after starting it; check FileMaker logs to see what's wrong")
}
if ($externalAuth) {
# Sometimes external authentication fails right after starting server; trigger that failure here
Test-FMSAccess
}
}
# stored credentials take precedence over external authentication
$fmsCredential = Get-StoredCredential -Target "GetSSL FileMaker Server Admin Console" -AsCredentialObject
if ($fmsCredential) {
$username = $fmsCredential.UserName
$password = $fmsCredential.Password
if ($username -and $password) {
$userAndPassParamString = "-u ""$username"" -p ""$password"""
Write-Output "retrieved stored credentials"
$FMAccessConfirmed = Test-FMSAccess
}
$fmsCredential = $username = $password = $null
} else {
Write-Output "no credentials were stored"
}
if (! $FMAccessConfirmed) {
$externalAuth = Test-FMSAccess
if (! $externalAuth) {
# I don't know, but I suspect fmsadmin occasionally needs some time here, or throttles logins
Start-Sleep -Seconds 1
<# Sometimes fmsadmin asks for a password even if it's configured properly to use external
authentication, check again to be sure it's for real.
https://community.filemaker.com/message/803496 #>
$externalAuth = Test-FMSAccess
}
if ($externalAuth) {
$FMAccessConfirmed = $true
Write-Output "external authentication credentials used"
}
}
if ($FMAccessConfirmed) {
Write-Output "confirmed"
} elseif (Test-IsInteractiveShell) {
Write-Output "no access via stored credentials or external authentication"
Write-Output "A window will open where you can securely enter your fmsadmin login. Sometimes it takes a moment to open."
while ($True) {
$fmsCredential = Get-Credential -Message "FileMaker Server Admin Console Sign In" | New-StoredCredential -Target "GetSSL FileMaker Server Admin Console" -Persist LocalMachine
if ($fmsCredential) {
$username = $fmsCredential.UserName
$password = $fmsCredential.Password
$userAndPassParamString = "-u ""$username"" -p ""$password"""
$fmsCredential = $username = $password = $null
$FMAccessConfirmed = Test-FMSAccess
if ($FMAccessConfirmed) {
break
} else {
Write-Output "That account didn't work, please try again."
}
} else {
throw ("Permissions not setup to allow performing fmsadmin.exe without entering username and password.")
}
}
} else {
throw ( "no access! Must be able to perform fmsadmin without entering user/pass when this script is not run interactively" )
}
Write-Output ""
Write-Output "Get certificate: ________________________________________________________________"
$certObj = Get-PACertificate
if (! $certObj) { throw "no certificate found" }
Write-Output "Export the private key"
$keyPath = Join-Path $FMSPath 'CStore\serverKey.pem'
if (Test-Path $keyPath) {
Backup-File $keyPath
Remove-Item $keyPath
}
Copy-Item ($certObj.KeyFile) $keyPath
Write-Output ""
$serverCustomPath = Join-Path $FMSPath 'CStore\serverCustom.pem'
if (Test-Path $serverCustomPath) {
Backup-File $serverCustomPath
Write-Output ""
}
Write-Output "Import certificate via fmsadmin: ________________________________________________"
Invoke-FMSAdmin certificate, import, """$($certObj.CertFile)""", --intermediateCA, """$($certObj.FullChainFile)""", -y
Write-Output ""
Write-Output "Stop FileMaker Server: __________________________________________________________"
if ($Staging -and !$Force) {
Write-Output "skipped because -Staging parameter was provided"
} else {
$WPEWasRunning = Get-Process fmscwpc -ErrorAction:Ignore
Write-Output "check if files are open first"
try { $FilesWereOpen = Invoke-FMSAdmin list, files -Timeout 5 }
catch [System.TimeoutException] {
Write-Output "failed to list files within 5 seconds"
Write-Output "assume files are open since it's safer than the alternative"
$FilesWereOpen = $true
}
if ($FilesWereOpen) {
Write-Output "files are open"
} else {
Write-Output "no files open"
}
Write-Output "now stop server"
<# Try to stop server multiple times, if necessary. #>
$retries = 3
$timeout = 90
while ($true) {
$retries--
try {
Write-Output ("with timeout of $timeout seconds, starting at {0}..." -f (Get-Date).ToLongTimeString())
# first timeout is sent to fmsadmin, to give users this amount of time to close files before they are forcibly closed
# second timeout is to forcibly stop the fmsadmin command in case it hangs
Invoke-FMSAdmin stop, server, -y, -t, $timeout -Timeout ($timeout + 20)
break
}
catch [System.TimeoutException] {
if ($retries -gt 0) {
Write-Output " timed out, $retries attempt(s) left before aborting"
$timeout *= 2
} else { throw }
}
catch [System.ApplicationException] {
if ($_.Exception.Message.StartsWith("fmsadmin")) {
if (($_.Exception.Data.ExitCode) -eq 10002) {
if ($retries -gt 0) {
Write-Output " fmsadmin timed out, $retries attempt(s) left before aborting"
$timeout *= 2
} else { throw }
}
elseif (($_.Exception.Data.ExitCode) -eq 10502) {
Write-Output "10502 (Host unreachable) occurs when the service or server is stopped"
break
} else { throw }
} else { throw }
}
}
$mustStartServer = $True
}
Write-Output ""
Write-Output "Restart the FMS service: ________________________________________________________"
if ($Staging -and !$Force) {
Write-Output "skipped because -Staging parameter was provided"
} else {
Restart-Service "FileMaker Server"
}
Write-Output ""
<# Just in case server isn't configured to start automatically #>
Write-Output "Start FileMaker Server: _________________________________________________________"
if ($Staging -and !$Force) {
Write-Output "skipped because -Staging parameter was provided"
} else {
<# Try to start server multiple times, if necessary. I'm not sure, but I suspect fmsadmin
sometimes asks for credentials here, which causes the script to fail #>
$retries = 3
$timeout = 90
while ($true) {
$retries--
try {
Write-Output ("with timeout of $timeout seconds, starting at {0}..." -f (Get-Date).ToLongTimeString())
Invoke-FMSAdmin start, server -Timeout $timeout
<# give FMS a little time to start other processes, like WPE #>
Start-Sleep -Seconds 3
break
}
catch [System.TimeoutException] {
if ($retries -gt 0) {
Write-Output " timed out, $retries attempt(s) left before aborting"
$timeout *= 2
} else { throw }
}
catch [System.ApplicationException] {
# NOTE: Error: 10007 (Requested object does not exist) occurs when service is stopped
if ($_.Exception.Message.StartsWith("fmsadmin") -and ($_.Exception.Data.ExitCode) -eq 10006) {
Write-Output "(If server is set to start automatically, error 10006 is expected)"
break
} else { throw }
}
}
$mustStartServer = $False
if ($WPEWasRunning -and -not(Get-Process fmscwpc -ErrorAction:Ignore)) {
<# NOTE: this will only work as expected from 64 bit PowerShell since Get-Process only lists processes running the same bit depth as PowerShell #>
Write-Output "start WPE because it was running before FMS was stopped, but isn't now:"
try { Invoke-FMSAdmin start, wpe }
catch {
if ($_.Exception.Message.StartsWith("fmsadmin") -and ($_.Exception.Data.ExitCode) -eq 10006) {
Write-Output "(If there is a delay in WPE starting, error 10006 is expected"
break
} else { throw }
}
Write-Output "done starting WPE"
}
if ($FilesWereOpen) {
Write-Output "files were open, confirm access to fmsadmin"
<# Confirm FMAccess again, since it can ask for a password again after starting
server. Do it twice; first time will likely fail, second time should succeed.
https://community.filemaker.com/thread/191306 #>
Start-Sleep -Seconds 3 # if the next check is too soon after starting server, it will fail
$FMAccessConfirmedAfterRestart = Test-FMSAccess
if (-not ($FMAccessConfirmedAfterRestart)) {
# NOTE: I don't know, but I suspect fmsadmin occasionally needs some time here, or throttles logins
Start-Sleep -Seconds 1
<# Sometimes fmsadmin asks for a password even if it's configured properly to use external
authentication, check again to be sure it's for real.
https://community.filemaker.com/message/803496 #>
$FMAccessConfirmedAfterRestart = Test-FMSAccess
}
if ($FMAccessConfirmedAfterRestart) {
Write-Output "check if files are open now"
try { $FilesAreOpen = Invoke-FMSAdmin list, files -Timeout 5 }
catch [System.TimeoutException] {
Write-Output "failed to list files within 5 seconds"
Write-Output "assume files are not open since it's safer than the alternative"
$FilesAreOpen = $false
}
if(-not($FilesAreOpen)) {
Write-Output "open files because they were open before FMS was stopped, but aren't now:"
Invoke-FMSAdmin open
} else {
Write-Output "they are"
}
} else {
throw "could not connect to fmsadmin"
}
}
}
Write-Output "done"
Write-Output ""
}
function Invoke-FMSAdmin {
<#
.SYNOPSIS
Calls fmsadmin.exe with specified parameters and will prevent it from hanging a script
if it waits for user input.
.OUTPUTS
Returns standard output from fmsadmin.
fmsadmin errors will throw an exception which contains the exit code.
.PARAMETER Parameters
List of parameters to send to fmsadmin. Parameters with a space must be a quoted string.
.PARAMETER Timeout
Seconds to wait for the process before failing with an error.
Will throw [System.TimeoutException] if fmsadmin does not complete within this time.
.EXAMPLE
Invoke-FMSAdmin list, files
.EXAMPLE
Invoke-FMSAdmin list, files -Timeout 5
.EXAMPLE
Invoke-FMSAdmin open, """File With Spaces"""
Invoke-FMSAdmin open, """$fileNameWithSpaces"""
.EXAMPLE
Invoke-FMSAdmin stop, server, -t, 30
Invoke-FMSAdmin stop, server, "-t 30"
Either of these formats will work
.EXAMPLE
try { Invoke-FMSAdmin bad, command }
catch [System.ApplicationException] {
if ($_.Exception.Message.StartsWith("fmsadmin")) {
$_.Exception.Data.ExitCode
} else {
# was not an fmsadmin-specific exception, so throw it back
throw
}
}
#>
Param(
[Parameter(Mandatory=$false,ValueFromPipeline=$true,Position=1)]
[string[]] $Parameters,
[int]$Timeout = 30
)
# https://stackoverflow.com/a/36539226
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $fmsadmin
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false # must be false to redirect IO streams
$pinfo.Arguments = "$Parameters $userAndPassParamString"
$pinfo.CreateNoWindow = $true
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
[Void]$p.Start()
$stdoutTask = $p.StandardOutput.ReadToEndAsync();
$stderrTask = $p.StandardError.ReadToEndAsync();
[Void]$p.WaitForExit($Timeout * 1000)
if (! $p.HasExited) {
$p.Kill()
$p.Close()
throw [System.TimeoutException] "fmsadmin did not complete within timeout of $Timeout seconds`n$fmsadmin $Parameters"
}
$stdout = $stdoutTask.Result;
$stderr = $stderrTask.Result;
if ($p.ExitCode) {
Write-Verbose "$fmsadmin $Parameters" -Verbose
if ($stderr) {
# NOTE: I don't think fmsadmin uses stderr, but I'd rather include it to be safe
Write-Host "stderr: $stderr"
}
if ($stdout) {Write-Verbose $stdout -Verbose}
$e = [System.ApplicationException]::New("fmsadmin exit code: " + $p.ExitCode)
$e.Data.Add('ExitCode', $p.ExitCode)
throw $e
}
if ($stderr) {
# NOTE: I don't think fmsadmin uses stderr, but I'd rather include it to be safe
Write-Verbose "$fmsadmin $Parameters" -Verbose
Write-Error "stderr: $stderr"
}
return $stdout
}
function New-Cert {
if (! $account) {
Write-Output "Account Setup ___________________________________________________________________"
$accounts = Get-PAAccount -List -Contact $Emails -Status valid
if ($accounts) {
# valid account(s) already exist for this email
if ($accounts -is [array]) {
Write-Output "multiple accounts found; selected the last one"
$account = $accounts[-1]
} else {
Write-Output "selected an existing account"
$account = $accounts
}
Set-PAAccount -ID $account.id
} else {
Write-Output "create new account"
$account = New-PAAccount -Contact $Emails -AcceptTos
}
($account | Select-Object id, status, contact, location | Format-List | Out-String).Trim()
Write-Output ""
}
Write-Output "Create an Order _________________________________________________________________"
New-PAOrder $Domains -Force:$Force
$order = Get-PAOrder
if (-not $order) {
throw "No order found. This should never be able to happen."
} elseif ($order.status -eq "valid") {
Write-Output "Order has already been completed; previous certificate will be used."
Write-Output "Use the -Force parameter to always issue a new certificate here."
Write-Output ""
return
}
Write-Output ""
Write-Output "Authorizations and Challenges ___________________________________________________"
if ($WebRoot) {
$acmeChallengePath = Join-Path $WebRoot '.well-known\acme-challenge'
} else {
$acmeChallengePath = Join-Path $FMSPath 'HTTPServer\conf\.well-known\acme-challenge'
}
if (! (Test-Path $acmeChallengePath)) {
Write-Output "Create acme-challenge directory"
(New-Item -Path $acmeChallengePath -ItemType Directory).ToString().Trim()
}
$webConfigPath = Join-Path $acmeChallengePath "web.config"
if (! (Test-Path $webConfigPath)) {
Write-Output "Create web.config file"
'<configuration><system.webServer><staticContent><remove fileExtension="." /><mimeMap fileExtension="." mimeType="text/plain" /></staticContent></system.webServer></configuration>' | Out-File $webConfigPath
}
Write-Output "Create challenge file(s)"
$auths = $order | Get-PAAuthorizations
foreach ($auth in $auths) {
$path = Join-Path $acmeChallengePath $auth.HTTP01Token
$body = Get-KeyAuthorization $auth.HTTP01Token $account
$body | Out-File -Encoding ascii -FilePath $path
}
if ($ModifyFirewall) {
$firewallRule = Get-NetFirewallRule -DisplayName $firewallRuleName -ErrorAction Ignore
if (-not $firewallRule) {
Write-Output "create firewall rule to allow port 80"
$firewallRule = New-NetFirewallRule -DisplayName $firewallRuleName -Direction Inbound -LocalPort 80 -RemotePort 80 -Protocol TCP -Action Allow
} elseif ($firewallRule.Action -ne "Allow") {
Write-Output "modify firewall rule to allow port 80"
$firewallRule.Action = "Allow"
Set-NetFirewallRule -InputObject $firewallRule
} else {
Write-Output "firewall rule already allowed port 80, so it wasn't modified"
}
}
# Send all challenges at once
$auths.HTTP01Url | Send-ChallengeAck
Write-Output "Wait for LE to validate"
# https://tools.ietf.org/html/rfc8555#section-7.1.6
# Once the authorization is in the "valid" state, it can expire ("expired"), be deactivated by the client ("deactivated", see Section 7.5.2), or revoked by the server ("revoked")
Write-Output (Get-Date).ToLongTimeString()
$timeout = New-TimeSpan -Minutes 1
$sw = [diagnostics.stopwatch]::StartNew()
do {
if ($sw.elapsed -gt $timeout) {
$auths
throw ("authorization was not processed before timing out")
}
<# I tested with 1ms sleep and it was valid on the first iteration,
but to be curtious to LE's server's, I set it to sleep for 1 second #>
Start-Sleep -Seconds 1
$auths = $order | Get-PAAuthorizations
# they start as pending then move to processing, then valid or invalid
$pending = $auths | Where-Object {($_.status -eq "pending") -or ($_.status -eq "processing")}
Write-Progress "Wait for LE to validate" -SecondsRemaining $timeout.Subtract($sw.Elapsed).TotalSeconds
}
until (! $pending)
Write-Progress "Wait for LE to validate" -Complete
Write-Output "completed in $([math]::Round($sw.Elapsed.TotalSeconds)) seconds"
$notValid = $auths | Where-Object status -ne "valid"
$order = Get-PAOrder -Refresh
if ($notValid) {
Write-Output ""
($notValid | Format-List | Out-String).Trim()
($notValid.challenges.error | Format-List | Out-String).Trim()
($order | Format-List | Out-String).Trim()
throw ("unexpected status value")
}
if (($order.status -ne "ready") -and ($order.status -ne "valid")) {
($order | Format-List | Out-String).Trim()
throw ("order wasn't ready or valid, but should have been at this point in the script")
}
if ($ModifyFirewall) {
Write-Output "modify firewall rule to block port 80"
$firewallRule.Action = "Block"
try {
Set-NetFirewallRule -InputObject $firewallRule
}
catch {
Write-Output "failed to modify firewall; this will be tried again at the end of the script"
}
}
Write-Output "done"
Write-Output ""
Write-Output "Request Certificate _____________________________________________________________"
New-PACertificate $Domains -Force:$Force
$order = Get-PAOrder -Refresh
if ($order.status -ne "valid") {
$order | Format-List
throw ("order wasn't valid, but should have been at this point in the script")
}
if (! (Get-PACertificate)) {
throw ("certificate didn't exist, which shouldn't be able to happen given the above validations that already ran")
}
Write-Output ""
}
function Set-Server {
$Server = Get-PAServer
if (! $Server) {
if ($Staging) {
Write-Output "Select staging server"
Set-PAServer LE_STAGE
} else {
Write-Output "Select production server"
Set-PAServer LE_PROD
}
} else {
<# Make sure Staging matches Staging parameter #>
if ($Server.location.Contains('staging')) {
if ($Staging) {
Write-Output "correct server already selected"
} else {
Write-Output "Switch from Staging to Production"
Set-PAServer LE_PROD
}
} elseif ($Staging) {
Write-Output "Switch from Production to Staging"
Set-PAServer LE_STAGE
} else {
Write-Output "correct server already selected"
}
}
}
function Schedule-Task {
if ($Time.Date -eq $Start.Date) {
# Date contained in Time parameter was today, so add IntervalDays
$Time = $Time.AddDays($IntervalDays)
}
if ($PSCmdlet.ShouldProcess(
"Schedule a task to renew the certificate every $IntervalDays days starting ${Time}", #NOTE: shown with -WhatIf parameter
"NOTE: If the fmsadmin.exe command cannot run without having to type the username/password when this script is run, the task will fail.",
"Schedule a task to renew the certificate every $IntervalDays days starting ${Time}?"
)) {
$StagingParameterAsText = if ($Staging) {"-Staging"}
$ForceParameterAsText = if ($Force) {"-Force"}
$ModifyFirewallAsText = if ($ModifyFirewall) {"-ModifyFirewall"}
$WebRootParameterAsText = if ($WebRoot) {"-WebRoot '" + $WebRoot + "'"}
$Action = New-ScheduledTaskAction `
-Execute powershell.exe `
-Argument "-NoProfile -NonInteractive -ExecutionPolicy Bypass -Command ""& '$PSCommandPath' -Renew $StagingParameterAsText $ForceParameterAsText $ModifyFirewallAsText $WebRootParameterAsText -Confirm:0"""
$Trigger = New-ScheduledTaskTrigger `
-Daily `
-DaysInterval $IntervalDays `
-At $Time
$Settings = New-ScheduledTaskSettingsSet `
-DontStopIfGoingOnBatteries `
-ExecutionTimeLimit 00:30
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings `
-Description "Get an SSL certificate from Let's Encrypt and install it on FileMaker Server."
$TaskName = "GetSSL"
try {
Write-Output ("You will now be asked for your Windows password, you should trust this script before entering it. You can audit this section of code by reviewing line #{0} of {1}." -f (Get-ScriptLineNumber), (Split-Path $PSCommandPath -Leaf))
$credentials = Get-Credential `
-UserName $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name) `
-Message "Windows user to run the task"
Register-ScheduledTask -TaskName $TaskName -InputObject $Task -Force `
-User $credentials.UserName `
-Password $credentials.GetNetworkCredential().Password
}
finally { $credentials = $null }
}
}
function Test-Administrator {
# NOTE: must be admin to create challenge file in hosted directory
$user = [Security.Principal.WindowsIdentity]::GetCurrent()
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
function Test-IsInteractiveShell {
<#
.SYNOPSIS
Returns boolean determining if prompt was run noninteractive.
.DESCRIPTION
First, we check `[Environment]::UserInteractive` to determine if we're if the shell if running
interactively. An example of not running interactively would be if the shell is running as a service.
If we are running interactively, we check the Command Line Arguments to see if the `-NonInteractive`
switch was used; or an abbreviation of the switch.
.LINK
https://github.com/UNT-CAS/Test-IsNonInteractiveShell
(function was modified from this version before adding to GetSSL.ps1)
#>
return ([Environment]::UserInteractive -and (-not ([Environment]::GetCommandLineArgs() | ?{ $_ -like '-NonI*' })))
}
function Test-FMSAccess {
<#
.SYNOPSIS
Determine if user has to enter username and password to perform fmsadmin.exe
Alternatively, if $userAndPassParamString is set, will test if those credentials are valid.
(This is handled by the Invoke-FMSAdmin function)
.OUTPUTS
Boolean $true if user can access fmsadmin.exe
#>
try {
Invoke-FMSAdmin list, files -Timeout 5 | Out-Null
return $true
}
catch {
# If process exceeded timeout, assume it asked user for credentials, but since the window was hidden, user
# could not see/enter them, therefor they don't have access to fmsadmin.exe without providing credentials.
return $false
}
}
#########################################################################################################################
Try {
$Start = Get-Date # Save start date/time so it can be accessed repeatedly throughout the script
<# Options that are unlikely to need to be changed #>
[string] $FMSPath = 'C:\Program Files\FileMaker\FileMaker Server\'
[int] $LogsToKeep = 50
[switch] $Logging = -not $host.name.contains('ISE')
[string] $LogDirectory = Join-Path $FMSPath ('Data\Documents\' + (Split-Path $PSCommandPath -Leaf))
[string] $fmsadmin = Join-Path $FMSPath 'Database Server\fmsadmin.exe' | Convert-Path
[string] $firewallRuleName = "GetSSL Port 80"
$externalAuth = '' # declare as script-level variable
$userAndPassParamString = '' # declare as script-level variable (set user/pass here if you want to hard-code it in your script)
if (-not (Test-Administrator)) {
throw 'This script must be run as Administrator'
}
if ($Logging) {
If ( -not (Test-Path $LogDirectory) ) { New-Item -ItemType directory -Path $LogDirectory | Out-Null}
$LogFilePath = Join-Path $LogDirectory "\powershell $($Start.ToString("yyyy-MM-dd_HHmmss")).log"
Start-Transcript -Append -Path $LogFilePath
}
Write-Output $Start.ToString("F") # add nicely formatted date to logs
Write-Output ""
# fix Issue #12: MSG:UnableToDownload error when installing dependencies (or listing installed dependencies)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
switch ($PSCmdlet.ParameterSetName) {
'Setup' {
if (-not(Get-Module -Listavailable -Name Posh-ACME*)) {
Write-Output "Posh-ACME module not found, Installing Dependencies..."
Install-Dependencies
Write-Output "done"
}
Write-Output "Setup..."
Set-Server
# NOTE: $Setup may or may not be set since passing -Domains and -Emails parameter will activate Setup mode even without the -Setup flag
if (! $Setup) { [switch] $Setup = $True }
}
'Renew' {
Write-Output "Renew..."
Set-Server
# get Emails from Get-PAAccount
$account = Get-PAAccount
if (! $account) {
throw "No ACME account configured. Run Setup first."
}
$Emails = ($account.contact).Replace('mailto:','')
# get Domains from Get-PAOrder
$order = Get-PAOrder -Refresh
if (! $order) {
throw "No previously configured order found. Run Setup first."
}
$Domains = @($order.MainDomain) + $order.SANs
}
{$_ -in 'Setup','Renew'} {
Write-Output " domains: $($Domains -join ', ')"
Write-Output " emails: $($Emails -join ', ')"
Write-Output " Staging: $Staging"
Write-Output " Force: $Force"
Write-Output " ModifyFirewall: $ModifyFirewall"
Write-Output ""
if ($Staging -and $Force) {
<# either the first message is shown, or both the second AND third #>
$messages = @(
<# verboseDescription: Textual description of the action to be performed. This is what will be displayed to the user for ActionPreference.Continue. (-WhatIf parameter will show this) #>
"Replace FileMaker Server Certificate with one from Let's Encrypt Staging server, then restart FileMaker Server service.",
<# verboseWarning: Textual query of whether the action should be performed, usually in the form of a question. This is what will be displayed to the user for ActionPreference.Inquire. #>
"If you proceed, and this script is successful, FileMaker Server service will be restarted and ALL USERS DISCONNECTED.",
<# caption: Caption of the window which may be displayed if the user is prompted whether or not to perform the action. caption may be displayed by some hosts, but not all.#>
"Replace with Staging Certificate?"
)
} elseif ($Staging) {
<# either the first message is shown, or both the second AND third #>
$messages = @(
"Replace FileMaker Server Certificate with one from Let's Encrypt Staging server, will NOT restart FileMaker Server service, because this is just for testing/setup.",
"Will NOT restart FileMaker Server service, because this is just for testing/setup, right?",
"Replace with Staging Certificate?"
)
} else {
$messages = @(
"Replace FileMaker Server Certificate with one from Let's Encrypt, then restart FileMaker Server service.",
"If you proceed, and this script is successful, FileMaker Server service will be restarted and ALL USERS DISCONNECTED.",
"Replace Certificate?"
)
}
if ($PSCmdlet.ShouldProcess($messages[0], $messages[1], $messages[2])) {
Write-Output ""
New-Cert
Install-Cert
if (Test-IsInteractiveShell) {
# just call the script again to schedule a task; it will ask user if they want to proceed
& $PSCommandPath -ScheduleTask -Staging:$Staging -Force:$Force -ModifyFirewall:$ModifyFirewall
}
}
break
}
'InstallCertificate' {
Write-Output "InstallCertificate..."
Write-Output " Staging: $Staging"
Write-Output ""