-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
249 lines (216 loc) · 7.37 KB
/
Copy pathmain.go
File metadata and controls
249 lines (216 loc) · 7.37 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
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"golang.org/x/crypto/bcrypt"
"gomail/config"
"gomail/delivery"
"gomail/dns"
"gomail/reporting"
"gomail/smtp"
"gomail/store"
tlsconfig "gomail/tls"
"gomail/web"
)
func main() {
configPath := flag.String("config", "config.json", "Path to configuration file")
hashPassword := flag.String("hash-password", "", "Generate bcrypt hash for a password")
flag.Parse()
// Password hashing utility
if *hashPassword != "" {
hash, err := hashPasswordBcrypt(*hashPassword)
if err != nil {
log.Fatalf("Error hashing password: %v", err)
}
fmt.Printf("Password hash: %s\n", hash)
fmt.Println("Add this to config.json under web.bootstrap_admin.password_hash")
return
}
// Load configuration
cfg, err := config.Load(*configPath)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
log.Printf("[main] GoMail %s starting (hostname=%s)", config.Version(), cfg.Server.Hostname)
// Initialize DNS cache
dns.InitCache(cfg.DNS.CacheTTL)
// Open database
db, err := store.Open(cfg.Store.DBPath, cfg.Store.AttachmentsPath)
if err != nil {
log.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
log.Println("[main] database opened")
// Bootstrap admin: create initial domain + account if no accounts exist
bootstrapAdmin(cfg, db)
// Initialize TLS (skip for mode "none")
certMgr, err := tlsconfig.NewCertManager(cfg)
if err != nil {
log.Fatalf("Failed to initialize TLS: %v", err)
}
log.Println("[main] TLS initialized")
// Get TLS config for SMTP (nil when mode=none)
var smtpTLS *tls.Config
if cfg.TLS.Mode != "none" {
smtpTLS, err = certMgr.GetCertificateForSMTP(cfg.Server.Hostname)
if err != nil {
log.Printf("[main] warning: SMTP TLS config: %v (STARTTLS will be unavailable)", err)
}
} else {
log.Println("[main] SMTP STARTTLS disabled (tls.mode=none)")
}
// Start SMTP inbound server
smtpServer := smtp.NewInboundServer(cfg, db, smtpTLS)
if err := smtpServer.Start(); err != nil {
log.Fatalf("Failed to start SMTP server: %v", err)
}
// Start delivery queue workers
deliveryPool := delivery.NewPool(cfg, db, certMgr.TLSConfig)
deliveryPool.Start()
// Start greylisting cleanup worker (runs daily)
go func() {
ticker := time.NewTicker(24 * time.Hour)
defer ticker.Stop()
for range ticker.C {
if err := db.CleanupGreylisting(); err != nil {
log.Printf("[cleanup] greylisting cleanup error: %v", err)
} else {
log.Printf("[cleanup] greylisting cleanup completed")
}
}
}()
// Create delivery queue for the web interface (per-domain DKIM from DB)
queue := delivery.NewQueue(db, cfg)
// Start DMARC report scheduler (weekly reports)
// Create a wrapper function to queue DMARC reports for delivery
enqueueFunc := func(from, to, message string) error {
// Queue the DMARC report for delivery through the SMTP system
// Use account ID 0 for system-generated reports
return queue.Enqueue(from, []string{to}, []byte(message), 0)
}
reporting.ScheduleWeeklyReports(cfg, db, enqueueFunc)
// Start web server
webServer := web.NewServer(cfg, db, queue, enqueueFunc)
if cfg.Web.IsTLSEnabled() {
// TLS mode: HTTPS on listen_addr, HTTP redirect + ACME on http_addr
go func() {
httpHandler := web.HTTPSRedirect(cfg.Web.ListenAddr)
if certMgr.AutocertMgr != nil {
httpHandler = certMgr.AutocertMgr.HTTPHandler(httpHandler)
}
httpAddr := cfg.Web.HTTPAddr
if httpAddr == "" {
httpAddr = ":80"
}
log.Printf("[main] HTTP redirect server on %s", httpAddr)
if err := http.ListenAndServe(httpAddr, httpHandler); err != nil {
log.Printf("[main] HTTP server error: %v", err)
}
}()
go func() {
httpServer := webServer.GetHTTPServer()
httpServer.TLSConfig = certMgr.TLSConfig
if err := webServer.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed {
log.Fatalf("HTTPS server error: %v", err)
}
}()
} else {
// No TLS: plain HTTP web interface (e.g. behind nginx / reverse proxy)
// If autocert is enabled, integrate it into the web server for ACME challenges
log.Printf("[main] web.enable_tls=false, checking autocert: mode=%s, hasMgr=%v", cfg.TLS.Mode, certMgr.AutocertMgr != nil)
if cfg.TLS.Mode == "autocert" && certMgr.AutocertMgr != nil {
webServer.IntegrateAutocert(certMgr.AutocertMgr)
log.Printf("[main] autocert integrated for /.well-known/acme-challenge/ (proxy port 80 through nginx)")
} else if cfg.TLS.Mode == "autocert" {
log.Printf("[main] WARNING: autocert mode but no AutocertMgr (check acme_dir and acme_email config)")
}
go func() {
if err := webServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("HTTP server error: %v", err)
}
}()
}
// Log active domains
domainNames, _ := db.ListAllDomainNames()
if len(domainNames) > 0 {
log.Printf("[main] accepting mail for domains: %s", strings.Join(domainNames, ", "))
} else {
log.Println("[main] warning: no domains configured. Add domains via the admin panel.")
}
log.Println("[main] GoMail is running. Press Ctrl+C to stop.")
// Wait for shutdown signal
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
log.Println("[main] shutting down...")
smtpServer.Stop()
deliveryPool.Stop()
webServer.Shutdown()
log.Println("[main] GoMail stopped.")
}
// bootstrapAdmin creates the initial domain and admin account on first run
// using the bootstrap_admin settings from config.json.
func bootstrapAdmin(cfg *config.Config, db *store.DB) {
count, _ := db.CountAccounts()
if count > 0 {
return // Accounts already exist, skip bootstrap
}
if cfg.Web.BootstrapAdmin.Email == "" || cfg.Web.BootstrapAdmin.PasswordHash == "" {
log.Println("[main] no accounts exist and no bootstrap_admin configured.")
log.Println("[main] set web.bootstrap_admin.email and web.bootstrap_admin.password_hash in config.json")
log.Println("[main] generate a password hash with: gomail -hash-password 'your-password'")
return
}
email := cfg.Web.BootstrapAdmin.Email
parts := strings.SplitN(email, "@", 2)
if len(parts) != 2 {
log.Printf("[main] bootstrap_admin.email '%s' is not a valid email address", email)
return
}
domainName := parts[1]
log.Printf("[main] bootstrapping: creating domain '%s' and admin account '%s'", domainName, email)
// Create the domain
domain := &store.Domain{
Domain: domainName,
IsActive: true,
DKIMSelector: cfg.DKIM.DefaultSelector,
DKIMAlgorithm: cfg.DKIM.DefaultAlgorithm,
}
domainID, err := db.CreateDomain(domain)
if err != nil {
log.Printf("[main] bootstrap: failed to create domain: %v", err)
return
}
// Create the admin account
account := &store.Account{
DomainID: domainID,
Email: email,
DisplayName: "Administrator",
PasswordHash: cfg.Web.BootstrapAdmin.PasswordHash,
IsAdmin: true,
IsActive: true,
QuotaBytes: 0, // unlimited
}
_, err = db.CreateAccount(account)
if err != nil {
log.Printf("[main] bootstrap: failed to create admin account: %v", err)
return
}
log.Printf("[main] bootstrap complete. Login with: %s", email)
log.Println("[main] generate DKIM keys via the admin panel: /admin/domains")
}
func hashPasswordBcrypt(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(hash), nil
}