/
dovecot_sasl.go
163 lines (132 loc) · 4.07 KB
/
dovecot_sasl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Maddy Mail Server - Composable all-in-one email server.
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package dovecotsasl
import (
"fmt"
"net"
"github.com/emersion/go-sasl"
dovecotsasl "github.com/foxcpp/go-dovecot-sasl"
"github.com/foxcpp/maddy/framework/config"
"github.com/foxcpp/maddy/framework/exterrors"
"github.com/foxcpp/maddy/framework/log"
"github.com/foxcpp/maddy/framework/module"
"github.com/foxcpp/maddy/internal/auth"
)
type Auth struct {
instName string
serverEndpoint string
log log.Logger
network string
addr string
mechanisms map[string]dovecotsasl.Mechanism
}
const modName = "dovecot_sasl"
func New(_, instName string, _, inlineArgs []string) (module.Module, error) {
a := &Auth{
instName: instName,
log: log.Logger{Name: modName, Debug: log.DefaultLogger.Debug},
}
switch len(inlineArgs) {
case 0:
case 1:
a.serverEndpoint = inlineArgs[0]
default:
return nil, fmt.Errorf("%s: one or none arguments needed", modName)
}
return a, nil
}
func (a *Auth) Name() string {
return modName
}
func (a *Auth) InstanceName() string {
return a.instName
}
func (a *Auth) getConn() (*dovecotsasl.Client, error) {
// TODO: Connection pooling
conn, err := net.Dial(a.network, a.addr)
if err != nil {
return nil, fmt.Errorf("%s: unable to contact server: %v", modName, err)
}
cl, err := dovecotsasl.NewClient(conn)
if err != nil {
return nil, fmt.Errorf("%s: unable to contact server: %v", modName, err)
}
return cl, nil
}
func (a *Auth) returnConn(cl *dovecotsasl.Client) {
cl.Close()
}
func (a *Auth) Init(cfg *config.Map) error {
cfg.String("endpoint", false, false, a.serverEndpoint, &a.serverEndpoint)
if _, err := cfg.Process(); err != nil {
return err
}
if a.serverEndpoint == "" {
return fmt.Errorf("%s: missing server endpoint", modName)
}
endp, err := config.ParseEndpoint(a.serverEndpoint)
if err != nil {
return fmt.Errorf("%s: invalid server endpoint: %v", modName, err)
}
if endp.Path == "" {
return fmt.Errorf("%s: unexpected path in endpoint ", modName)
}
// Dial once to check usability and also to get list of mechanisms.
conn, err := net.Dial(endp.Scheme, endp.Address())
if err != nil {
return fmt.Errorf("%s: unable to contact server: %v", modName, err)
}
cl, err := dovecotsasl.NewClient(conn)
if err != nil {
return fmt.Errorf("%s: unable to contact server: %v", modName, err)
}
defer cl.Close()
a.mechanisms = make(map[string]dovecotsasl.Mechanism, len(cl.ConnInfo().Mechs))
for name, mech := range cl.ConnInfo().Mechs {
if mech.Private {
continue
}
a.mechanisms[name] = mech
}
a.network = endp.Scheme
a.addr = endp.Address()
return nil
}
func (a *Auth) AuthPlain(username, password string) error {
if _, ok := a.mechanisms[sasl.Plain]; ok {
cl, err := a.getConn()
if err != nil {
return exterrors.WithTemporary(err, true)
}
defer a.returnConn(cl)
// Pretend it is SMTPS even though we really don't know.
// We also have no connection information to pass to the server...
return cl.Do("SMTP", sasl.NewPlainClient("", username, password),
dovecotsasl.Secured, dovecotsasl.NoPenalty)
}
if _, ok := a.mechanisms[sasl.Login]; ok {
cl, err := a.getConn()
if err != nil {
return err
}
defer a.returnConn(cl)
return cl.Do("SMTP", sasl.NewLoginClient(username, password),
dovecotsasl.Secured, dovecotsasl.NoPenalty)
}
return auth.ErrUnsupportedMech
}
func init() {
module.Register(modName, New)
}