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 pam2021import (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/external"31)3233type Auth struct {34 instName string35 useHelper bool36 helperPath string3738 Log log.Logger39}4041func New(modName, instName string, _, inlineArgs []string) (module.Module, error) {42 if len(inlineArgs) != 0 {43 return nil, errors.New("pam: inline arguments are not used")44 }45 return &Auth{46 instName: instName,47 Log: log.Logger{Name: modName},48 }, nil49}5051func (a *Auth) Name() string {52 return "pam"53}5455func (a *Auth) InstanceName() string {56 return a.instName57}5859func (a *Auth) Init(cfg *config.Map) error {60 cfg.Bool("debug", true, false, &a.Log.Debug)61 cfg.Bool("use_helper", false, false, &a.useHelper)62 if _, err := cfg.Process(); err != nil {63 return err64 }65 if !canCallDirectly && !a.useHelper {66 return errors.New("pam: this build lacks support for direct libpam invocation, use helper binary")67 }6869 if a.useHelper {70 a.helperPath = filepath.Join(config.LibexecDirectory, "maddy-pam-helper")71 if _, err := os.Stat(a.helperPath); err != nil {72 return fmt.Errorf("pam: no helper binary (maddy-pam-helper) found in %s", config.LibexecDirectory)73 }74 }7576 return nil77}7879func (a *Auth) AuthPlain(username, password string) error {80 if a.useHelper {81 if err := external.AuthUsingHelper(a.helperPath, username, password); err != nil {82 return err83 }84 }85 err := runPAMAuth(username, password)86 if err != nil {87 return err88 }89 return nil90}9192func init() {93 module.Register("auth.pam", New)94}