{{ message }}
Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
401 lines (341 loc) · 10.8 KB
/
Copy pathinstall.ps1
File metadata and controls
401 lines (341 loc) · 10.8 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
<#
# uMailServer Installer for Windows
# Installs uMailServer as a Windows Service
#
param(
[string]$ConfigFile = "",
[string]$DataDir = "C:\ProgramData\umailserver",
[string]$InstallDir = "C:\Program Files\umailserver",
[string]$Hostname = "",
[string]$AdminEmail = "",
[string]$Domain = "",
[switch]$NoService,
[switch]$SkipSetup,
[switch]$AssumeYes
)
$ErrorActionPreference = "Stop"
# Default paths
$DefaultDataDir = "C:\ProgramData\umailserver"
$DefaultInstallDir = "$env:ProgramFiles\umailserver"
$DefaultConfigDir = "$env:PROGRAMDATA\umailserver" # Same as data dir on Windows
# Colors
function Write-Info($message) { Write-Host "[INFO] $message" -ForegroundColor Green }
function Write-Warn($message) { Write-Host "[WARN] $message" -ForegroundColor Yellow }
function Write-Err($message) { Write-Host "[ERROR] $message" -ForegroundColor Red }
# Check if running as admin
function Test-Admin {
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
# Create directories
function New-Directories {
param([string]$DataDir, [string]$InstallDir)
$dirs = @($DataDir, "$DataDir\data", "$DataDir\log", "$DataDir\run", $InstallDir)
foreach ($dir in $dirs) {
if (-not (Test-Path $dir)) {
Write-Info "Creating directory: $dir"
New-Item -ItemType Directory -Path $dir -Force | Out-Null
} else {
Write-Info "Directory exists: $dir"
}
}
}
# Generate random JWT secret
function New-Secret {
$bytes = New-Object byte[] 32
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rng.GetBytes($bytes)
return [Convert]::ToBase64String($bytes) | Select-Object -First 32
}
# Create config file
function New-Config {
param(
[string]$ConfigPath,
[string]$Hostname,
[string]$AdminEmail,
[string]$Domain,
[string]$DataDir
)
$jwtSecret = New-Secret
$config = @"
# uMailServer Configuration
# Generated by install.ps1 on $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")
server:
hostname: $Hostname
data_dir: $DataDir\data
tls:
cert_file: ""
key_file: ""
min_version: "1.2"
acme:
enabled: true
email: $AdminEmail
provider: letsencrypt
challenge: http-01
smtp:
inbound:
enabled: true
port: 25
bind: "0.0.0.0"
max_message_size: 50MB
max_recipients: 100
max_connections: 10000
submission:
enabled: true
port: 587
bind: "0.0.0.0"
require_auth: true
require_tls: true
submission_tls:
enabled: true
port: 465
bind: "0.0.0.0"
require_auth: true
imap:
enabled: true
port: 993
bind: "0.0.0.0"
starttls_port: 143
pop3:
enabled: false
port: 995
bind: "0.0.0.0"
http:
enabled: true
port: 443
http_port: 80
bind: "0.0.0.0"
cors_origins: []
admin:
enabled: true
bind: "127.0.0.1"
port: 8443
security:
max_login_attempts: 5
lockout_duration: 15m
jwt_secret: "$jwtSecret"
rate_limit:
smtp_per_minute: 30
smtp_per_hour: 500
imap_connections: 50
http_requests_per_minute: 120
spam:
enabled: true
reject_threshold: 9.0
junk_threshold: 3.0
quarantine_threshold: 6.0
bayesian:
enabled: true
auto_train: true
greylisting:
enabled: true
delay: 5m
rbl_servers:
- zen.spamhaus.org
- b.barracudacentral.org
av:
enabled: false
addr: "127.0.0.1:3310"
mcp:
enabled: true
port: 3000
bind: "127.0.0.1"
logging:
level: info
format: json
output: stdout
metrics:
enabled: true
port: 8080
bind: "127.0.0.1"
database:
path: $DataDir\data\umailserver.db
storage:
sync: true
domains:
- name: $Domain
max_accounts: 100
max_mailbox_size: 5GB
"@
$config | Out-File -FilePath $ConfigPath -Encoding UTF8 -Force
Write-Info "Configuration created: $ConfigPath"
}
# Create Windows service wrapper
function New-WinService {
param([string]$InstallDir, [string]$ConfigPath, [string]$DataDir)
$serviceWrapper = @"
// Windows Service wrapper for uMailServer
// Build: go build -o umailserver-service.exe ./cmd/service
package main
import (
"os"
"os/exec"
"syscall"
"time"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
"golang.org/x/sys/windows/svc/eventlog"
)
func main() {
const svcName = "umailserver"
var err error
var isInteractive bool
isInteractive, err = svc.IsWindowsService()
if err != nil {
eventlog.Error(1, "Failed to determine service status: %v", err)
os.Exit(1)
}
if isInteractive {
debug.Run(svcName, &umailserverService{})
} else {
svc.Run(svcName, &umailserverService{})
}
}
type umailserverService struct{}
func (m *umailserverService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
go func() {
exe := "` + "$InstallDir" + `\umailserver.exe"
cmd := exec.Command(exe, "serve", "--config", "` + "$ConfigPath" + `")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
}
err := cmd.Start()
if err != nil {
eventlog.Error(1, "Failed to start umailserver: %v", err)
return
}
changes <- svc.Status{State: svc.Running}
err = cmd.Wait()
if err != nil {
eventlog.Error(1, "umailserver exited with error: %v", err)
}
}()
for {
c := <-r
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
changes <- svc.Status{State: svc.StopPending}
time.Sleep(5 * time.Second)
return
default:
}
}
}
"@
Write-Warn "Windows service requires NSSM or similar service manager."
Write-Info "Alternative: Run as console app with: umailserver.exe serve --config $ConfigPath"
}
# Interactive setup
function Invoke-Setup {
param([string]$DataDir, [string]$InstallDir)
Write-Info "Starting interactive setup..."
Write-Host ""
# Hostname
if ([string]::IsNullOrEmpty($Hostname)) {
Write-Host "=== uMailServer Setup ===" -ForegroundColor Cyan
Write-Host ""
do {
$hostnameInput = Read-Host "Server hostname [mail.example.com]"
if ([string]::IsNullOrEmpty($hostnameInput)) { $script:Hostname = "mail.example.com" }
else { $script:Hostname = $hostnameInput }
} while ([string]::IsNullOrEmpty($script:Hostname))
}
# Admin email
if ([string]::IsNullOrEmpty($AdminEmail)) {
$defaultEmail = "admin@" + $script:Hostname.Split('.')[1..($script:Hostname.Split('.').Length-1)] -join '.'
do {
$emailInput = Read-Host "Admin email [$defaultEmail]"
if ([string]::IsNullOrEmpty($emailInput)) { $script:AdminEmail = $defaultEmail }
else { $script:AdminEmail = $emailInput }
} while ([string]::IsNullOrEmpty($script:AdminEmail))
}
# Domain
if ([string]::IsNullOrEmpty($Domain)) {
$defaultDomain = $script:Hostname.Split('.')[1..($script:Hostname.Split('.').Length-1)] -join '.'
do {
$domainInput = Read-Host "Primary email domain [$defaultDomain]"
if ([string]::IsNullOrEmpty($domainInput)) { $script:Domain = $defaultDomain }
else { $script:Domain = $domainInput }
} while ([string]::IsNullOrEmpty($script:Domain))
}
$configPath = "$InstallDir\umailserver.yaml"
New-Config -ConfigPath $configPath -Hostname $script:Hostname -AdminEmail $script:AdminEmail -Domain $script:Domain -DataDir $DataDir
Write-Host ""
Write-Info "Setup complete!"
Write-Host ""
Write-Host "Configuration:" -ForegroundColor Cyan
Write-Host " Config: $configPath"
Write-Host " Data: $DataDir"
}
# Main installation
function Install-uMailServer {
Write-Host ""
Write-Info "uMailServer Installer for Windows"
Write-Host ""
if (-not (Test-Admin)) {
Write-Warn "Running as non-admin. Some operations may fail."
if (-not $AssumeYes) {
$confirm = Read-Host "Continue anyway? [y/N]"
if ($confirm -ne "y" -and $confirm -ne "Y") { exit 1 }
}
}
# Create directories
New-Directories -DataDir $DataDir -InstallDir $InstallDir
# Copy binary if exists in current directory
$localBinary = ".\umailserver.exe"
if (Test-Path $localBinary) {
Write-Info "Copying binary from current directory..."
Copy-Item $localBinary "$InstallDir\umailserver.exe" -Force
} else {
Write-Info "Binary not found in current directory."
Write-Info "Please copy umailserver.exe to $InstallDir or run from build directory."
}
# Set permissions
if (Test-Admin) {
$acl = Get-Acl $DataDir
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
"NT SERVICE\TrustedInstaller", "FullControl",
"ContainerInherit,ObjectInherit", "None", "Allow"
)
# Simplified - just ensure basic permissions
icacls $DataDir /inheritance:r 2>$null | Out-Null
icacls $DataDir /grant:r "SYSTEM:(OI)(CI)F" 2>$null | Out-Null
}
# Setup
if (-not $SkipSetup) {
if ([string]::IsNullOrEmpty($ConfigFile) -or -not (Test-Path $ConfigFile)) {
Invoke-Setup -DataDir $DataDir -InstallDir $InstallDir
} else {
$destConfig = "$InstallDir\umailserver.yaml"
Copy-Item $ConfigFile $destConfig -Force
Write-Info "Configuration installed: $destConfig"
}
}
# Create service helper
New-WinService -InstallDir $InstallDir -ConfigPath "$InstallDir\umailserver.yaml" -DataDir $DataDir
Write-Host ""
Write-Info "Installation complete!"
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host " 1. Edit config: $InstallDir\umailserver.yaml"
Write-Host " 2. Install service: nssm install umailserver `"$InstallDir\umailserver.exe`" `"serve --config $InstallDir\umailserver.yaml`""
Write-Host " 3. Start service: nssm start umailserver"
Write-Host " 4. Or run directly: $InstallDir\umailserver.exe serve --config $InstallDir\umailserver.yaml"
Write-Host ""
Write-Host "Ports:" -ForegroundColor Cyan
Write-Host " SMTP: 25, 587, 465"
Write-Host " IMAP: 143, 993"
Write-Host " POP3: 995"
Write-Host " HTTP: 443 (Webmail + Admin)"
Write-Host " Admin: 8443 (localhost only)"
Write-Host " Metrics: 8080"
Write-Host " MCP: 3000"
Write-Host ""
}
Install-uMailServer
You can’t perform that action at this time.
