1/*2Maddy Mail Server - Composable all-in-one email server.3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors45This program is free software: you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation, either version 3 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program. If not, see <https://www.gnu.org/licenses/>.17*/1819package external2021import (22 "errors"23 "fmt"24 "os"25 "path/filepath"2627 "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)3233type ExternalAuth struct {34 modName string35 instName string36 helperPath string3738 perDomain bool39 domains []string4041 Log log.Logger42}4344func 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 }5051 if len(inlineArgs) != 0 {52 return nil, errors.New("external: inline arguments are not used")53 }5455 return ea, nil56}5758func (ea *ExternalAuth) Name() string {59 return ea.modName60}6162func (ea *ExternalAuth) InstanceName() string {63 return ea.instName64}6566func (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 err73 }74 if ea.perDomain && ea.domains == nil {75 return errors.New("auth_domains must be set if auth_perdomain is used")76 }7778 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 }8687 ea.Log.Debugln("using helper:", ea.helperPath)8889 return nil90}9192func (ea *ExternalAuth) AuthPlain(username, password string) error {93 accountName, ok := auth.CheckDomainAuth(username, ea.perDomain, ea.domains)94 if !ok {95 return module.ErrUnknownCredentials96 }9798 return AuthUsingHelper(ea.helperPath, accountName, password)99}100101func init() {102 module.Register("auth.external", NewExternalAuth)103}