maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

  1/*
  2Maddy Mail Server - Composable all-in-one email server.
  3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
  4
  5This program is free software: you can redistribute it and/or modify
  6it under the terms of the GNU General Public License as published by
  7the Free Software Foundation, either version 3 of the License, or
  8(at your option) any later version.
  9
 10This program is distributed in the hope that it will be useful,
 11but WITHOUT ANY WARRANTY; without even the implied warranty of
 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13GNU General Public License for more details.
 14
 15You should have received a copy of the GNU General Public License
 16along with this program.  If not, see <https://www.gnu.org/licenses/>.
 17*/
 18
 19package external
 20
 21import (
 22	"errors"
 23	"fmt"
 24	"os"
 25	"path/filepath"
 26
 27	"github.com/foxcpp/maddy/framework/config"
 28	"github.com/foxcpp/maddy/framework/log"
 29	"github.com/foxcpp/maddy/framework/module"
 30	"github.com/foxcpp/maddy/internal/auth"
 31)
 32
 33type ExternalAuth struct {
 34	modName    string
 35	instName   string
 36	helperPath string
 37
 38	perDomain bool
 39	domains   []string
 40
 41	Log log.Logger
 42}
 43
 44func NewExternalAuth(modName, instName string, _, inlineArgs []string) (module.Module, error) {
 45	ea := &ExternalAuth{
 46		modName:  modName,
 47		instName: instName,
 48		Log:      log.Logger{Name: modName},
 49	}
 50
 51	if len(inlineArgs) != 0 {
 52		return nil, errors.New("external: inline arguments are not used")
 53	}
 54
 55	return ea, nil
 56}
 57
 58func (ea *ExternalAuth) Name() string {
 59	return ea.modName
 60}
 61
 62func (ea *ExternalAuth) InstanceName() string {
 63	return ea.instName
 64}
 65
 66func (ea *ExternalAuth) Init(cfg *config.Map) error {
 67	cfg.Bool("debug", false, false, &ea.Log.Debug)
 68	cfg.Bool("perdomain", false, false, &ea.perDomain)
 69	cfg.StringList("domains", false, false, nil, &ea.domains)
 70	cfg.String("helper", false, false, "", &ea.helperPath)
 71	if _, err := cfg.Process(); err != nil {
 72		return err
 73	}
 74	if ea.perDomain && ea.domains == nil {
 75		return errors.New("auth_domains must be set if auth_perdomain is used")
 76	}
 77
 78	if ea.helperPath != "" {
 79		ea.Log.Debugln("using helper:", ea.helperPath)
 80	} else {
 81		ea.helperPath = filepath.Join(config.LibexecDirectory, "maddy-auth-helper")
 82	}
 83	if _, err := os.Stat(ea.helperPath); err != nil {
 84		return fmt.Errorf("%s doesn't exist", ea.helperPath)
 85	}
 86
 87	ea.Log.Debugln("using helper:", ea.helperPath)
 88
 89	return nil
 90}
 91
 92func (ea *ExternalAuth) AuthPlain(username, password string) error {
 93	accountName, ok := auth.CheckDomainAuth(username, ea.perDomain, ea.domains)
 94	if !ok {
 95		return module.ErrUnknownCredentials
 96	}
 97
 98	return AuthUsingHelper(ea.helperPath, accountName, password)
 99}
100
101func init() {
102	module.Register("auth.external", NewExternalAuth)
103}