mirror of
https://github.com/foxcpp/maddy.git
synced 2025-04-06 06:27:38 +03:00
Initial implementation of Dovecot SASL endpoint
This commit is contained in:
parent
d8a6f9dd22
commit
3f1a4e5868
5 changed files with 109 additions and 1 deletions
2
go.mod
2
go.mod
|
@ -18,7 +18,7 @@ require (
|
|||
github.com/emersion/go-msgauth v0.4.1-0.20200429175443-e4c87369d72f
|
||||
github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b
|
||||
github.com/emersion/go-smtp v0.12.2-0.20200219094142-f9be832b5554
|
||||
github.com/foxcpp/go-dovecot-sasl v0.0.0-20200504181415-c4db6731332d // indirect
|
||||
github.com/foxcpp/go-dovecot-sasl v0.0.0-20200504194015-e35592c01a2c
|
||||
github.com/foxcpp/go-imap-i18nlevel v0.0.0-20200208001533-d6ec88553005
|
||||
github.com/foxcpp/go-imap-sql v0.4.1-0.20200426175844-c3172a53940a
|
||||
github.com/foxcpp/go-mockdns v0.0.0-20200503193630-ff72b88723f2
|
||||
|
|
4
go.sum
4
go.sum
|
@ -62,6 +62,10 @@ github.com/foxcpp/go-dovecot-sasl v0.0.0-20200504173201-4582a99ffc3f h1:eT/8SMQq
|
|||
github.com/foxcpp/go-dovecot-sasl v0.0.0-20200504173201-4582a99ffc3f/go.mod h1:5yZUmwr851vgjyAfN7OEfnrmKOh/qLA5dbGelXYsu1E=
|
||||
github.com/foxcpp/go-dovecot-sasl v0.0.0-20200504181415-c4db6731332d h1:R2o7Kw2CJEy2N8ycjDxFTMxU0xp8gsGv7ZjWyR65LQs=
|
||||
github.com/foxcpp/go-dovecot-sasl v0.0.0-20200504181415-c4db6731332d/go.mod h1:5yZUmwr851vgjyAfN7OEfnrmKOh/qLA5dbGelXYsu1E=
|
||||
github.com/foxcpp/go-dovecot-sasl v0.0.0-20200504192305-6a7652986e8b h1:gx69er1ld3fIIECH3bDqdS5SOHKQ9KwlXrBkE3FGBEY=
|
||||
github.com/foxcpp/go-dovecot-sasl v0.0.0-20200504192305-6a7652986e8b/go.mod h1:5yZUmwr851vgjyAfN7OEfnrmKOh/qLA5dbGelXYsu1E=
|
||||
github.com/foxcpp/go-dovecot-sasl v0.0.0-20200504194015-e35592c01a2c h1:P47zvAdjLcQU1UouNlJSVWN1Pg0S7QX8pWKFBWKqCCg=
|
||||
github.com/foxcpp/go-dovecot-sasl v0.0.0-20200504194015-e35592c01a2c/go.mod h1:5yZUmwr851vgjyAfN7OEfnrmKOh/qLA5dbGelXYsu1E=
|
||||
github.com/foxcpp/go-imap-backend-tests v0.0.0-20200426175250-4110e9b66176 h1:qyze36XjZnwK9geEzr5qlChS8zJgz7L+YZ5O8JNhh90=
|
||||
github.com/foxcpp/go-imap-backend-tests v0.0.0-20200426175250-4110e9b66176/go.mod h1:yUISYv/uXLQ6tQZcds/p/hdcZ5JzrEUifyED2VffWpc=
|
||||
github.com/foxcpp/go-imap-i18nlevel v0.0.0-20200208001533-d6ec88553005 h1:pfoFtkTTQ473qStSN79jhCFBWqMQt/3DQ3NGuXvT+50=
|
||||
|
|
98
internal/endpoint/dovecot_sasld/dovecot_sasl.go
Normal file
98
internal/endpoint/dovecot_sasld/dovecot_sasl.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
package dovecotsasld
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"github.com/emersion/go-sasl"
|
||||
dovecotsasl "github.com/foxcpp/go-dovecot-sasl"
|
||||
"github.com/foxcpp/maddy/internal/auth"
|
||||
"github.com/foxcpp/maddy/internal/config"
|
||||
"github.com/foxcpp/maddy/internal/log"
|
||||
"github.com/foxcpp/maddy/internal/module"
|
||||
)
|
||||
|
||||
const modName = "dovecot_sasld"
|
||||
|
||||
type Endpoint struct {
|
||||
addrs []string
|
||||
log log.Logger
|
||||
saslAuth auth.SASLAuth
|
||||
|
||||
listenersWg sync.WaitGroup
|
||||
|
||||
srv *dovecotsasl.Server
|
||||
}
|
||||
|
||||
func New(_ string, addrs []string) (module.Module, error) {
|
||||
return &Endpoint{
|
||||
addrs: addrs,
|
||||
saslAuth: auth.SASLAuth{
|
||||
Log: log.Logger{Name: modName + "/saslauth"},
|
||||
},
|
||||
log: log.Logger{Name: modName, Debug: log.DefaultLogger.Debug},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (endp *Endpoint) Name() string {
|
||||
return modName
|
||||
}
|
||||
|
||||
func (endp *Endpoint) InstanceName() string {
|
||||
return modName
|
||||
}
|
||||
|
||||
func (endp *Endpoint) Init(cfg *config.Map) error {
|
||||
cfg.Callback("auth", func(m *config.Map, node config.Node) error {
|
||||
return endp.saslAuth.AddProvider(m, node)
|
||||
})
|
||||
if _, err := cfg.Process(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
endp.srv = dovecotsasl.NewServer()
|
||||
|
||||
for _, mech := range endp.saslAuth.SASLMechanisms() {
|
||||
mech := mech
|
||||
endp.srv.AddMechanism(mech, mechInfo[mech], func(req *dovecotsasl.AuthReq) sasl.Server {
|
||||
var remoteAddr net.Addr
|
||||
if req.RemoteIP != nil && req.RemotePort != 0 {
|
||||
remoteAddr = &net.TCPAddr{IP: req.RemoteIP, Port: int(req.RemotePort)}
|
||||
}
|
||||
|
||||
return endp.saslAuth.CreateSASL(mech, remoteAddr, func(_ string) error { return nil })
|
||||
})
|
||||
}
|
||||
|
||||
for _, addr := range endp.addrs {
|
||||
parsed, err := config.ParseEndpoint(addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %v", modName, err)
|
||||
}
|
||||
|
||||
l, err := net.Listen(parsed.Network(), parsed.Address())
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %v", modName, err)
|
||||
}
|
||||
endp.log.Printf("listening on %v", modName, l.Addr())
|
||||
|
||||
endp.listenersWg.Add(1)
|
||||
go func() {
|
||||
defer endp.listenersWg.Done()
|
||||
if err := endp.srv.Serve(l); err != nil {
|
||||
endp.log.Printf("failed to serve %v: %v", modName, l.Addr(), err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (endp *Endpoint) Close() error {
|
||||
return endp.srv.Close()
|
||||
}
|
||||
|
||||
func init() {
|
||||
module.RegisterEndpoint(modName, New)
|
||||
}
|
5
internal/endpoint/dovecot_sasld/mech_info.go
Normal file
5
internal/endpoint/dovecot_sasld/mech_info.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package dovecotsasld
|
||||
|
||||
import dovecotsasl "github.com/foxcpp/go-dovecot-sasl"
|
||||
|
||||
var mechInfo = map[string]dovecotsasl.Mechanism{}
|
1
maddy.go
1
maddy.go
|
@ -32,6 +32,7 @@ import (
|
|||
_ "github.com/foxcpp/maddy/internal/check/milter"
|
||||
_ "github.com/foxcpp/maddy/internal/check/requiretls"
|
||||
_ "github.com/foxcpp/maddy/internal/check/spf"
|
||||
_ "github.com/foxcpp/maddy/internal/endpoint/dovecot_sasld"
|
||||
_ "github.com/foxcpp/maddy/internal/endpoint/imap"
|
||||
_ "github.com/foxcpp/maddy/internal/endpoint/smtp"
|
||||
_ "github.com/foxcpp/maddy/internal/modify"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue