-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathmaddy.go
More file actions
587 lines (499 loc) · 16.7 KB
/
maddy.go
File metadata and controls
587 lines (499 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
Maddy Mail Server - Composable all-in-one email server.
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package maddy
import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"sync"
"github.com/caddyserver/certmagic"
parser "github.com/foxcpp/maddy/framework/cfgparser"
"github.com/foxcpp/maddy/framework/config"
modconfig "github.com/foxcpp/maddy/framework/config/module"
"github.com/foxcpp/maddy/framework/config/tls"
"github.com/foxcpp/maddy/framework/container"
"github.com/foxcpp/maddy/framework/hooks"
"github.com/foxcpp/maddy/framework/log"
"github.com/foxcpp/maddy/framework/module"
"github.com/foxcpp/maddy/framework/resource/netresource"
"github.com/foxcpp/maddy/internal/authz"
maddycli "github.com/foxcpp/maddy/internal/cli"
"github.com/urfave/cli/v2"
// Import packages for side-effect of module registration.
_ "github.com/foxcpp/maddy/internal/auth/dovecot_sasl"
_ "github.com/foxcpp/maddy/internal/auth/external"
_ "github.com/foxcpp/maddy/internal/auth/ldap"
_ "github.com/foxcpp/maddy/internal/auth/netauth"
_ "github.com/foxcpp/maddy/internal/auth/pam"
_ "github.com/foxcpp/maddy/internal/auth/pass_table"
_ "github.com/foxcpp/maddy/internal/auth/plain_separate"
_ "github.com/foxcpp/maddy/internal/auth/shadow"
_ "github.com/foxcpp/maddy/internal/check/authorize_sender"
_ "github.com/foxcpp/maddy/internal/check/command"
_ "github.com/foxcpp/maddy/internal/check/dkim"
_ "github.com/foxcpp/maddy/internal/check/dns"
_ "github.com/foxcpp/maddy/internal/check/dnsbl"
_ "github.com/foxcpp/maddy/internal/check/milter"
_ "github.com/foxcpp/maddy/internal/check/requiretls"
_ "github.com/foxcpp/maddy/internal/check/rspamd"
_ "github.com/foxcpp/maddy/internal/check/spf"
_ "github.com/foxcpp/maddy/internal/endpoint/dovecot_sasld"
_ "github.com/foxcpp/maddy/internal/endpoint/imap"
_ "github.com/foxcpp/maddy/internal/endpoint/openmetrics"
_ "github.com/foxcpp/maddy/internal/endpoint/smtp"
_ "github.com/foxcpp/maddy/internal/imap_filter"
_ "github.com/foxcpp/maddy/internal/imap_filter/command"
_ "github.com/foxcpp/maddy/internal/libdns"
_ "github.com/foxcpp/maddy/internal/modify"
_ "github.com/foxcpp/maddy/internal/modify/dkim"
_ "github.com/foxcpp/maddy/internal/storage/blob/fs"
_ "github.com/foxcpp/maddy/internal/storage/blob/s3"
_ "github.com/foxcpp/maddy/internal/storage/imapsql"
_ "github.com/foxcpp/maddy/internal/table"
_ "github.com/foxcpp/maddy/internal/target/queue"
_ "github.com/foxcpp/maddy/internal/target/remote"
_ "github.com/foxcpp/maddy/internal/target/smtp"
_ "github.com/foxcpp/maddy/internal/tls"
_ "github.com/foxcpp/maddy/internal/tls/acme"
)
var (
Version = "go-build"
enableDebugFlags = false
)
func BuildInfo() string {
version := Version
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" {
version = info.Main.Version
}
return fmt.Sprintf(`%s %s/%s %s
default config: %s
default state_dir: %s
default runtime_dir: %s`,
version, runtime.GOOS, runtime.GOARCH, runtime.Version(),
filepath.Join(ConfigDirectory, "maddy.conf"),
DefaultStateDirectory,
DefaultRuntimeDirectory)
}
func init() {
maddycli.AddGlobalFlag(
&cli.PathFlag{
Name: "config",
Usage: "Configuration file to use",
EnvVars: []string{"MADDY_CONFIG"},
Value: filepath.Join(ConfigDirectory, "maddy.conf"),
},
)
maddycli.AddGlobalFlag(&cli.BoolFlag{
Name: "debug",
Usage: "enable debug logging early",
Destination: &log.DefaultLogger.Debug,
})
maddycli.AddSubcommand(&cli.Command{
Name: "verify-config",
Usage: "Check configuration file for errors",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Usage: "enable debug logging early",
Destination: &log.DefaultLogger.Debug,
},
},
Action: VerifyConfig,
})
maddycli.AddSubcommand(&cli.Command{
Name: "run",
Usage: "Start the server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "libexec",
Value: DefaultLibexecDirectory,
Usage: "path to the libexec directory",
Destination: &config.LibexecDirectory,
},
&cli.StringSliceFlag{
Name: "log",
Usage: "default logging target(s)",
Value: cli.NewStringSlice("stderr"),
},
&cli.BoolFlag{
Name: "v",
Usage: "print version and build metadata, then exit",
Hidden: true,
},
},
Action: Run,
})
maddycli.AddSubcommand(&cli.Command{
Name: "version",
Usage: "Print version and build metadata, then exit",
Action: func(c *cli.Context) error {
fmt.Println(BuildInfo())
return nil
},
})
if enableDebugFlags {
maddycli.AddGlobalFlag(&cli.StringFlag{
Name: "debug.pprof",
Usage: "enable live profiler HTTP endpoint and listen on the specified address",
})
maddycli.AddGlobalFlag(&cli.IntFlag{
Name: "debug.blockprofrate",
Usage: "set blocking profile rate",
})
maddycli.AddGlobalFlag(&cli.IntFlag{
Name: "debug.mutexproffract",
Usage: "set mutex profile fraction",
})
}
}
// Run is the entry point for all server-running code. It takes care of command line arguments processing,
// logging initialization, directives setup, configuration reading. After all that, it
// calls moduleMain to initialize and run modules.
func Run(c *cli.Context) error {
certmagic.UserAgent = "maddy/" + Version
if c.NArg() != 0 {
return cli.Exit(fmt.Sprintln("usage:", os.Args[0], "[options]"), 2)
}
if c.Bool("v") {
fmt.Println("maddy", BuildInfo())
return nil
}
var err error
log.DefaultLogger.Out, err = LogOutputOption(c.StringSlice("log"))
if err != nil {
systemdStatusErr(err)
return cli.Exit(err.Error(), 2)
}
initDebug(c)
err = os.Setenv("PATH", config.LibexecDirectory+string(filepath.ListSeparator)+os.Getenv("PATH"))
if err != nil {
systemdStatusErr(err)
return cli.Exit(err.Error(), 1)
}
hooks.AddHook(hooks.EventLogRotate, reinitLogging)
defer func() {
if err := log.DefaultLogger.Out.Close(); err != nil {
log.Println("failed to close default logger output:", err)
}
}()
defer hooks.RunHooks(hooks.EventShutdown)
defer func() {
if err := netresource.CloseAllListeners(); err != nil {
log.DefaultLogger.Error("CloseAllListeners failed", err)
}
}()
if err := moduleMain(c.Path("config")); err != nil {
systemdStatusErr(err)
return cli.Exit(err.Error(), 1)
}
return nil
}
func VerifyConfig(c *cli.Context) error {
err := os.Setenv("PATH", config.LibexecDirectory+string(filepath.ListSeparator)+os.Getenv("PATH"))
if err != nil {
return cli.Exit(err.Error(), 1)
}
if _, err := moduleConfigure(c.Path("config")); err != nil {
return cli.Exit(err.Error(), 2)
}
log.DefaultLogger.Msg("No errors detected")
return nil
}
func initDebug(c *cli.Context) {
if !enableDebugFlags {
return
}
if c.IsSet("debug.pprof") {
profileEndpoint := c.String("debug.pprof")
go func() {
log.Println("listening on", "http://"+profileEndpoint, "for profiler requests")
log.Println("failed to listen on profiler endpoint:", http.ListenAndServe(profileEndpoint, nil))
}()
}
// These values can also be affected by environment so set them
// only if argument is specified.
if c.IsSet("debug.mutexproffract") {
runtime.SetMutexProfileFraction(c.Int("debug.mutexproffract"))
}
if c.IsSet("debug.blockprofrate") {
runtime.SetBlockProfileRate(c.Int("debug.blockprofrate"))
}
}
func InitDirs(c *container.C) error {
if c.Config.StateDirectory == "" {
c.Config.StateDirectory = DefaultStateDirectory
}
if c.Config.RuntimeDirectory == "" {
c.Config.RuntimeDirectory = DefaultRuntimeDirectory
}
if c.Config.LibexecDirectory == "" {
c.Config.LibexecDirectory = DefaultLibexecDirectory
}
if err := ensureDirectoryWritable(c.Config.StateDirectory); err != nil {
return err
}
if err := ensureDirectoryWritable(c.Config.RuntimeDirectory); err != nil {
return err
}
// Make sure all paths we are going to use are absolute
// before we change the working directory.
if !filepath.IsAbs(c.Config.StateDirectory) {
return errors.New("statedir should be absolute")
}
if !filepath.IsAbs(c.Config.RuntimeDirectory) {
return errors.New("runtimedir should be absolute")
}
if !filepath.IsAbs(c.Config.LibexecDirectory) {
return errors.New("-libexec should be absolute")
}
// Change the working directory to make all relative paths
// in configuration relative to state directory.
if err := os.Chdir(c.Config.StateDirectory); err != nil {
log.Println(err)
}
config.StateDirectory = c.Config.StateDirectory
config.RuntimeDirectory = c.Config.RuntimeDirectory
config.LibexecDirectory = c.Config.LibexecDirectory
return nil
}
func ensureDirectoryWritable(path string) error {
if err := os.MkdirAll(path, 0o700); err != nil {
return err
}
testFile, err := os.Create(filepath.Join(path, "writeable-test"))
if err != nil {
return err
}
if err := testFile.Close(); err != nil {
log.Println("failed to close writeable-test file:", err)
}
return os.RemoveAll(testFile.Name())
}
func ReadGlobals(c *container.C, cfg []config.Node) (map[string]interface{}, []config.Node, error) {
globals := config.NewMap(nil, config.Node{Children: cfg})
globals.String("state_dir", false, false, DefaultStateDirectory, &c.Config.StateDirectory)
globals.String("runtime_dir", false, false, DefaultRuntimeDirectory, &c.Config.RuntimeDirectory)
globals.String("hostname", false, false, "", nil)
globals.String("autogenerated_msg_domain", false, false, "", nil)
globals.Custom("tls", false, false, nil, tls.TLSDirective, nil)
globals.Custom("tls_client", false, false, nil, tls.TLSClientBlock, nil)
globals.Bool("storage_perdomain", false, false, nil)
globals.Bool("auth_perdomain", false, false, nil)
globals.StringList("auth_domains", false, false, nil, nil)
globals.Custom("log", false, false, defaultLogOutput, logOutput, &c.DefaultLogger.Out)
globals.Bool("debug", false, log.DefaultLogger.Debug, &c.DefaultLogger.Debug)
config.EnumMapped(globals, "auth_map_normalize", true, false, authz.NormalizeFuncs, authz.NormalizeAuto, nil)
modconfig.Table(globals, "auth_map", true, false, nil, nil)
globals.AllowUnknown()
unknown, err := globals.Process()
return globals.Values, unknown, err
}
func ReadConfig(path string) ([]config.Node, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer func() {
if err := f.Close(); err != nil {
log.Println("failed to close config file:", err)
}
}()
return parser.Read(f, path)
}
func moduleConfigure(configPath string) (*container.C, error) {
c := container.New()
container.Global = c
cfg, err := ReadConfig(configPath)
if err != nil {
return nil, fmt.Errorf("failed to read config %s: %w", configPath, err)
}
globals, modBlocks, err := ReadGlobals(c, cfg)
if err != nil {
return nil, err
}
if err := InitDirs(c); err != nil {
return nil, err
}
err = RegisterModules(c, globals, modBlocks)
if err != nil {
return nil, err
}
for _, inst := range c.Modules.NotInitialized() {
return nil, fmt.Errorf("unused configuration block %s (%s)",
inst.InstanceName(), inst.Name())
}
return c, nil
}
func moduleStart(c *container.C) error {
return c.Lifetime.StartAll()
}
func moduleStop(c *container.C, earlyStop bool) error {
if earlyStop {
if err := c.Lifetime.EarlyStopAll(); err != nil {
log.DefaultLogger.Error("early stop failed", err)
}
}
return c.Lifetime.StopAll()
}
func moduleMain(configPath string) error {
log.DefaultLogger.Msg("loading configuration...")
// Make path absolute to make sure we can still read it if current directory changes (in moduleConfigure).
configPath, err := filepath.Abs(configPath)
if err != nil {
return err
}
c, err := moduleConfigure(configPath)
if err != nil {
return err
}
c.DefaultLogger.Msg("configuration loaded")
if err := moduleStart(c); err != nil {
return err
}
c.DefaultLogger.Msg("server started", "version", Version)
systemdStatus(SDReady, "Configuration running.")
asyncStopWg := sync.WaitGroup{} // Some containers might still be waiting on moduleStop
for handleSignals() {
hooks.RunHooks(hooks.EventReload)
c = moduleReload(c, configPath, &asyncStopWg)
}
c.DefaultLogger.Msg("server stopping...")
systemdStatus(SDStopping, "Waiting for old configuration to stop...")
asyncStopWg.Wait()
systemdStatus(SDStopping, "Waiting for current configuration to stop...")
if err := moduleStop(c, true); err != nil {
c.DefaultLogger.Msg("moduleStop failed", err)
}
c.DefaultLogger.Msg("server stopped")
return nil
}
func moduleReload(oldContainer *container.C, configPath string, asyncStopWg *sync.WaitGroup) *container.C {
oldContainer.DefaultLogger.Msg("reloading server...")
systemdStatus(SDReloading, "Reloading server...")
oldContainer.DefaultLogger.Msg("loading new configuration...")
newContainer, err := moduleConfigure(configPath)
if err != nil {
oldContainer.DefaultLogger.Error("failed to load new configuration", err)
return oldContainer
}
oldContainer.DefaultLogger.Msg("configuration loaded")
if err := oldContainer.Lifetime.EarlyStopAll(); err != nil {
oldContainer.DefaultLogger.Error("failed to early-stop old server", err)
container.Global = oldContainer
return oldContainer
}
netresource.ResetListenersUsage()
oldContainer.DefaultLogger.Msg("starting new server")
if err := moduleStart(newContainer); err != nil {
oldContainer.DefaultLogger.Error("failed to start new server", err)
container.Global = oldContainer
return oldContainer
}
newContainer.DefaultLogger.Msg("new server started", "version", Version)
systemdStatus(SDReloading, "New configuration running. Waiting for old connections and transactions to finish...")
asyncStopWg.Add(1)
go func() {
defer asyncStopWg.Done()
defer func() {
if err := netresource.CloseUnusedListeners(); err != nil {
oldContainer.DefaultLogger.Error("CloseUnusedListeners failed", err)
}
}()
oldContainer.DefaultLogger.Msg("stopping old server")
if err := moduleStop(oldContainer, false); err != nil {
oldContainer.DefaultLogger.Error("moduleStop failed", err)
}
oldContainer.DefaultLogger.Msg("old server stopped")
systemdStatus(SDReloading, "Configuration running.")
}()
return newContainer
}
func RegisterModules(c *container.C, globals map[string]interface{}, nodes []config.Node) (err error) {
var endpoints []struct {
Endpoint module.LifetimeModule
Cfg *config.Map
}
for _, block := range nodes {
var instName string
var modAliases []string
if len(block.Args) == 0 {
instName = block.Name
} else {
instName = block.Args[0]
modAliases = block.Args[1:]
}
modName := block.Name
endpFactory := module.GetEndpoint(modName)
if endpFactory != nil {
inst, err := endpFactory(modName, block.Args)
if err != nil {
return err
}
endpoints = append(endpoints, struct {
Endpoint module.LifetimeModule
Cfg *config.Map
}{Endpoint: inst, Cfg: config.NewMap(globals, block)})
continue
}
factory := module.Get(modName)
if factory == nil {
return config.NodeErr(block, "unknown module or global directive: %s", modName)
}
inst, err := factory(modName, instName)
if err != nil {
return err
}
err = c.Modules.Register(inst, func() error {
err := inst.Configure(nil, config.NewMap(globals, block))
if err != nil {
return err
}
if lt, ok := inst.(module.LifetimeModule); ok {
c.Lifetime.Add(lt)
}
return nil
})
if err != nil {
if errors.Is(err, module.ErrInstanceNameDuplicate) {
return config.NodeErr(block, "config block named %s already exists", inst.InstanceName())
}
return err
}
for _, alias := range modAliases {
if err := c.Modules.AddAlias(instName, alias); err != nil {
if errors.Is(err, module.ErrInstanceNameDuplicate) {
return config.NodeErr(block, "config block named %s already exists", alias)
}
return err
}
}
log.Debugf("%v:%v: register config block %v %v", block.File, block.Line, instName, modAliases)
}
if len(endpoints) == 0 {
return fmt.Errorf("at least one endpoint should be configured")
}
// Endpoints are configured directly after registration.
for _, endp := range endpoints {
if err := endp.Endpoint.Configure(nil, endp.Cfg); err != nil {
return err
}
c.Lifetime.Add(endp.Endpoint)
}
return nil
}