Change default file locations (#35)

* Change default files location as described in #27

* config: Change default config file name
This commit is contained in:
fox.cpp 2019-04-08 19:57:21 +03:00 committed by Simon Ser
parent 3c40e21817
commit 6e17ecab31
6 changed files with 82 additions and 25 deletions

View file

@ -23,6 +23,8 @@ Build tags:
## Configuration
Read from `/etc/maddy/maddy.conf` by default.
### Syntax
Maddy uses configuration format similar (but not the same!) to Caddy's
@ -70,6 +72,7 @@ are options that can be used like that:
Default TLS certificate to use. See
[CONFIG_REFERENCE.md](CONFIG_REFERENCE.md) for details.
<<<<<<< HEAD
* `debug`
Write verbose logs describing what exactly is happening and how its going.
Default mode is relatively quiet and still produces useful logs so
@ -97,6 +100,22 @@ These can be specified only outside of any configuration block.
log off /log
```
* `statedir`
Change directory used for all state-related files.
Default is $MADDYSTATE environment variable or `/var/lib/maddy` if $MADDYSTATE is not set.
Default value can be changed using -X linker flag:
```
go build --ldflags '-X github.com/emersion/maddy.defaultStateDirectory=/opt/maddy/state'
```
* `libexecdir`
Change directory where all auxilary binaries are stored.
Default is $MADDYLIBEXEC environment variable or `/usr/libexec/maddy` if $MADDYLIBEXEC is not set.
Default value can be changed using -X linker flag:
```
go build --ldflags '-X github.com/emersion/maddy.defaultLibexecDirectory=/opt/maddy/bin'
```
### Defaults
Maddy provides reasonable defaults so you can start using it without spending
@ -115,11 +134,12 @@ submission smtp://0.0.0.0:587 smtps://0.0.0.0:465
Don't forget to use actual values instead of placeholders.
With this configuration, maddy will create an SQLite3 database for messages in
current directory and use it to store all messages.
/var/lib/maddy and use it to store all messages. You need to ensure that this
directory exists and maddy can write to it.
### go-imap-sql: Database location
If you don't like SQLite3 or don't want to have it in the current directory,
If you don't like SQLite3 or don't want to have it in /var/lib/maddy,
you can override the configuration of the default module.
See [go-imap-sql repository](https://github.com/foxcpp/go-imap-sql) for

View file

@ -12,7 +12,7 @@ import (
func main() {
var configpath string
flag.StringVar(&configpath, "config", "Maddyfile", "path to Maddyfile")
flag.StringVar(&configpath, "config", filepath.Join(maddy.ConfigDirectory(), "maddy.conf"), "path to configuration file")
flag.Parse()
absCfg, err := filepath.Abs(configpath)

View file

@ -3,21 +3,16 @@ package maddy
import (
"database/sql"
"fmt"
"path/filepath"
"github.com/emersion/maddy/config"
"github.com/emersion/maddy/module"
)
var defaultDriver, defaultDsn string
func createDefaultStorage(_ string) (module.Module, error) {
if defaultDriver == "" {
defaultDriver = "sqlite3"
}
if defaultDsn == "" {
defaultDsn = "maddy.db"
}
var defaultDriver = "sqlite3"
var defaultDsn string
func createDefaultStorage(globals *config.Map, _ string) (module.Module, error) {
driverSupported := false
for _, driver := range sql.Drivers() {
if driver == defaultDriver {
@ -32,7 +27,7 @@ func createDefaultStorage(_ string) (module.Module, error) {
return NewSQLStorage("sql", "default")
}
func defaultStorageConfig(name string) config.Node {
func defaultStorageConfig(globals *config.Map, name string) config.Node {
return config.Node{
Name: "sql",
Args: []string{name},
@ -43,12 +38,12 @@ func defaultStorageConfig(name string) config.Node {
},
{
Name: "dsn",
Args: []string{defaultDsn},
Args: []string{filepath.Join(StateDirectory(globals.Values), "maddy.db")},
},
},
}
}
func createDefaultRemoteDelivery(name string) (module.Module, error) {
func createDefaultRemoteDelivery(_ *config.Map, name string) (module.Module, error) {
return Dummy{instName: name}, nil
}

39
directories.go Normal file
View file

@ -0,0 +1,39 @@
package maddy
import (
"os"
)
var (
defaultConfigDirectory = "/etc/maddy"
defaultStateDirectory = "/var/lib/maddy"
defaultLibexecDirectory = "/usr/libexec/maddy"
)
func ConfigDirectory() string {
return defaultConfigDirectory
}
func StateDirectory(globals map[string]interface{}) string {
if dir := os.Getenv("MADDYSTATE"); dir != "" {
return dir
}
if val, ok := globals["statedir"]; ok {
return val.(string)
}
return defaultStateDirectory
}
func LibexecDirectory(globals map[string]interface{}) string {
if dir := os.Getenv("MADDYLIBEXEC"); dir != "" {
return dir
}
if val, ok := globals["libexecdir"]; ok {
return val.(string)
}
return defaultLibexecDirectory
}

View file

@ -3,7 +3,9 @@ package maddy
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/emersion/maddy/config"
@ -57,10 +59,9 @@ func (ea *ExternalAuth) Init(cfg *config.Map) error {
helperName = "maddy-shadow-helper"
}
var err error
ea.helperPath, err = exec.LookPath(helperName)
if err != nil {
return fmt.Errorf("no %s authentication support, %s is not found in $PATH and no custom path is set", ea.modName, helperName)
ea.helperPath = filepath.Join(LibexecDirectory(cfg.Globals), helperName)
if _, err := os.Stat(ea.helperPath); err != nil {
return fmt.Errorf("no %s authentication support, %s is not found in %s and no custom path is set", ea.modName, LibexecDirectory(cfg.Globals), helperName)
}
ea.Log.Debugln("using helper:", ea.helperPath)

View file

@ -21,6 +21,8 @@ func Start(cfg []config.Node) error {
instances := make(map[string]modInfo)
globals := config.NewMap(nil, &config.Node{Children: cfg})
globals.String("hostname", false, false, "", nil)
globals.String("statedir", false, false, "", nil)
globals.String("libexecdir", false, false, "", nil)
globals.Custom("tls", false, true, nil, tlsDirective, nil)
globals.Custom("log", false, false, defaultLogOutput, logOutput, &log.DefaultLogger.Out)
globals.Bool("debug", false, &log.DefaultLogger.Debug)
@ -60,8 +62,8 @@ func Start(cfg []config.Node) error {
instances[instName] = modInfo{instance: inst, cfg: block}
}
addDefaultModule(instances, "default", createDefaultStorage, globals.Values, defaultStorageConfig)
addDefaultModule(instances, "default_remote_delivery", createDefaultRemoteDelivery, globals.Values, nil)
addDefaultModule(instances, globals, "default", createDefaultStorage, defaultStorageConfig)
addDefaultModule(instances, globals, "default_remote_delivery", createDefaultRemoteDelivery, nil)
for _, inst := range instances {
if module.Initialized[inst.instance.InstanceName()] {
@ -97,17 +99,17 @@ func Start(cfg []config.Node) error {
return nil
}
func addDefaultModule(insts map[string]modInfo, name string, factory func(string) (module.Module, error), globalCfg map[string]interface{}, cfgFactory func(string) config.Node) {
func addDefaultModule(insts map[string]modInfo, globals *config.Map, name string, factory func(*config.Map, string) (module.Module, error), cfgFactory func(*config.Map, string) config.Node) {
if _, ok := insts[name]; !ok {
if mod, err := factory(name); err != nil {
if mod, err := factory(globals, name); err != nil {
log.Printf("failed to register %s: %v", name, err)
} else {
log.Debugf("module create %s %s (built-in)", mod.Name(), name)
info := modInfo{instance: mod}
if cfgFactory != nil {
info.cfg = cfgFactory(name)
info.cfg = cfgFactory(globals, name)
}
module.RegisterInstance(mod, config.NewMap(globalCfg, &info.cfg))
module.RegisterInstance(mod, config.NewMap(globals.Values, &info.cfg))
insts[name] = info
}
} else {