forked from foxcpp/maddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maddy.go
423 lines (355 loc) · 12.2 KB
/
maddy.go
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
/*
Maddy Mail Server - Composable all-in-one email server.
Copyright © 2019-2020 Max Mazurov <[email protected]>, 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"
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
parser "github.com/foxcpp/maddy/framework/cfgparser"
"github.com/foxcpp/maddy/framework/config"
"github.com/foxcpp/maddy/framework/config/tls"
"github.com/foxcpp/maddy/framework/hooks"
"github.com/foxcpp/maddy/framework/log"
"github.com/foxcpp/maddy/framework/module"
// 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/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/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/modify"
_ "github.com/foxcpp/maddy/internal/modify/dkim"
_ "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"
)
var (
Version = "go-build"
// ConfigDirectory specifies platform-specific value
// that should be used as a location of default configuration
//
// It should not be changed and is defined as a variable
// only for purposes of modification using -X linker flag.
ConfigDirectory = "/etc/maddy"
// DefaultStateDirectory specifies platform-specific
// default for StateDirectory.
//
// Most code should use StateDirectory instead since
// it will contain the effective location of the state
// directory.
//
// It should not be changed and is defined as a variable
// only for purposes of modification using -X linker flag.
DefaultStateDirectory = "/var/lib/maddy"
// DefaultRuntimeDirectory specifies platform-specific
// default for RuntimeDirectory.
//
// Most code should use RuntimeDirectory instead since
// it will contain the effective location of the state
// directory.
//
// It should not be changed and is defined as a variable
// only for purposes of modification using -X linker flag.
DefaultRuntimeDirectory = "/run/maddy"
// DefaultLibexecDirectory specifies platform-specific
// default for LibexecDirectory.
//
// Most code should use LibexecDirectory since it will
// contain the effective location of the libexec
// directory.
//
// It should not be changed and is defined as a variable
// only for purposes of modification using -X linker flag.
DefaultLibexecDirectory = "/usr/lib/maddy"
enableDebugFlags = false
profileEndpoint *string
blockProfileRate *int
mutexProfileFract *int
)
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)
}
// Run is the entry point for all maddy code. It takes care of command line arguments parsing,
// logging initialization, directives setup, configuration reading. After all that, it
// calls moduleMain to initialize and run modules.
func Run() int {
flag.StringVar(&config.LibexecDirectory, "libexec", DefaultLibexecDirectory, "path to the libexec directory")
flag.BoolVar(&log.DefaultLogger.Debug, "debug", false, "enable debug logging early")
var (
configPath = flag.String("config", filepath.Join(ConfigDirectory, "maddy.conf"), "path to configuration file")
logTargets = flag.String("log", "stderr", "default logging target(s)")
printVersion = flag.Bool("v", false, "print version and build metadata, then exit")
)
if enableDebugFlags {
profileEndpoint = flag.String("debug.pprof", "", "enable live profiler HTTP endpoint and listen on the specified address")
blockProfileRate = flag.Int("debug.blockprofrate", 0, "set blocking profile rate")
mutexProfileFract = flag.Int("debug.mutexproffract", 0, "set mutex profile fraction")
}
flag.Parse()
if len(flag.Args()) != 0 {
fmt.Println("usage:", os.Args[0], "[options]")
return 2
}
if *printVersion {
fmt.Println("maddy", BuildInfo())
return 0
}
var err error
log.DefaultLogger.Out, err = LogOutputOption(strings.Split(*logTargets, ","))
if err != nil {
systemdStatusErr(err)
log.Println(err)
return 2
}
initDebug()
os.Setenv("PATH", config.LibexecDirectory+string(filepath.ListSeparator)+os.Getenv("PATH"))
f, err := os.Open(*configPath)
if err != nil {
systemdStatusErr(err)
log.Println(err)
return 2
}
defer f.Close()
cfg, err := parser.Read(f, *configPath)
if err != nil {
systemdStatusErr(err)
log.Println(err)
return 2
}
if err := moduleMain(cfg); err != nil {
systemdStatusErr(err)
log.Println(err)
return 2
}
return 0
}
func initDebug() {
if !enableDebugFlags {
return
}
if *profileEndpoint != "" {
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 *mutexProfileFract != 0 {
runtime.SetMutexProfileFraction(*mutexProfileFract)
}
if *blockProfileRate != 0 {
runtime.SetBlockProfileRate(*blockProfileRate)
}
}
func InitDirs() error {
if config.StateDirectory == "" {
config.StateDirectory = DefaultStateDirectory
}
if config.RuntimeDirectory == "" {
config.RuntimeDirectory = DefaultRuntimeDirectory
}
if config.LibexecDirectory == "" {
config.LibexecDirectory = DefaultLibexecDirectory
}
if err := ensureDirectoryWritable(config.StateDirectory); err != nil {
return err
}
if err := ensureDirectoryWritable(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(config.StateDirectory) {
return errors.New("statedir should be absolute")
}
if !filepath.IsAbs(config.RuntimeDirectory) {
return errors.New("runtimedir should be absolute")
}
if !filepath.IsAbs(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(config.StateDirectory); err != nil {
log.Println(err)
}
return nil
}
func ensureDirectoryWritable(path string) error {
if err := os.MkdirAll(path, 0700); err != nil {
return err
}
testFile, err := os.Create(filepath.Join(path, "writeable-test"))
if err != nil {
return err
}
testFile.Close()
if err := os.Remove(testFile.Name()); err != nil {
return err
}
return nil
}
func ReadGlobals(cfg []config.Node) (map[string]interface{}, []config.Node, error) {
globals := config.NewMap(nil, config.Node{Children: cfg})
globals.String("state_dir", false, false, DefaultStateDirectory, &config.StateDirectory)
globals.String("runtime_dir", false, false, DefaultRuntimeDirectory, &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.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, &log.DefaultLogger.Out)
globals.Bool("debug", false, log.DefaultLogger.Debug, &log.DefaultLogger.Debug)
globals.AllowUnknown()
unknown, err := globals.Process()
return globals.Values, unknown, err
}
func moduleMain(cfg []config.Node) error {
globals, modBlocks, err := ReadGlobals(cfg)
if err != nil {
return err
}
if err := InitDirs(); err != nil {
return err
}
defer log.DefaultLogger.Out.Close()
hooks.AddHook(hooks.EventLogRotate, reinitLogging)
endpoints, mods, err := RegisterModules(globals, modBlocks)
if err != nil {
return err
}
err = initModules(globals, endpoints, mods)
if err != nil {
return err
}
systemdStatus(SDReady, "Listening for incoming connections...")
handleSignals()
systemdStatus(SDStopping, "Waiting for running transactions to complete...")
hooks.RunHooks(hooks.EventShutdown)
return nil
}
type ModInfo struct {
Instance module.Module
Cfg config.Node
}
func RegisterModules(globals map[string]interface{}, nodes []config.Node) (endpoints, mods []ModInfo, err error) {
mods = make([]ModInfo, 0, len(nodes))
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 nil, nil, err
}
endpoints = append(endpoints, ModInfo{Instance: inst, Cfg: block})
continue
}
factory := module.Get(modName)
if factory == nil {
return nil, nil, config.NodeErr(block, "unknown module or global directive: %s", modName)
}
if module.HasInstance(instName) {
return nil, nil, config.NodeErr(block, "config block named %s already exists", instName)
}
inst, err := factory(modName, instName, modAliases, nil)
if err != nil {
return nil, nil, err
}
block := block
module.RegisterInstance(inst, config.NewMap(globals, block))
for _, alias := range modAliases {
if module.HasInstance(alias) {
return nil, nil, config.NodeErr(block, "config block named %s already exists", alias)
}
module.RegisterAlias(alias, instName)
}
mods = append(mods, ModInfo{Instance: inst, Cfg: block})
}
if len(endpoints) == 0 {
return nil, nil, fmt.Errorf("at least one endpoint should be configured")
}
return endpoints, mods, nil
}
func initModules(globals map[string]interface{}, endpoints, mods []ModInfo) error {
for _, endp := range endpoints {
if err := endp.Instance.Init(config.NewMap(globals, endp.Cfg)); err != nil {
return err
}
if closer, ok := endp.Instance.(io.Closer); ok {
endp := endp
hooks.AddHook(hooks.EventShutdown, func() {
log.Debugf("close %s (%s)", endp.Instance.Name(), endp.Instance.InstanceName())
if err := closer.Close(); err != nil {
log.Printf("module %s (%s) close failed: %v", endp.Instance.Name(), endp.Instance.InstanceName(), err)
}
})
}
}
for _, inst := range mods {
if module.Initialized[inst.Instance.InstanceName()] {
continue
}
return fmt.Errorf("Unused configuration block at %s:%d - %s (%s)",
inst.Cfg.File, inst.Cfg.Line, inst.Instance.InstanceName(), inst.Instance.Name())
}
return nil
}