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 pam
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/external"
31)
32
33type Auth struct {
34	instName   string
35	useHelper  bool
36	helperPath string
37
38	Log log.Logger
39}
40
41func 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	}, nil
49}
50
51func (a *Auth) Name() string {
52	return "pam"
53}
54
55func (a *Auth) InstanceName() string {
56	return a.instName
57}
58
59func (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 err
64	}
65	if !canCallDirectly && !a.useHelper {
66		return errors.New("pam: this build lacks support for direct libpam invocation, use helper binary")
67	}
68
69	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	}
75
76	return nil
77}
78
79func (a *Auth) AuthPlain(username, password string) error {
80	if a.useHelper {
81		if err := external.AuthUsingHelper(a.helperPath, username, password); err != nil {
82			return err
83		}
84	}
85	err := runPAMAuth(username, password)
86	if err != nil {
87		return err
88	}
89	return nil
90}
91
92func init() {
93	module.Register("auth.pam", New)
94}