-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
144 lines (123 loc) · 2.6 KB
/
server.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
package dovecotsasl
import (
"bufio"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"strconv"
"sync/atomic"
"github.com/emersion/go-sasl"
)
type Server struct {
l []net.Listener
mechInfo map[string]Mechanism
mechImpl map[string]func(*AuthReq) sasl.Server
Log *log.Logger
connCount uint32
}
func NewServer() *Server {
return &Server{
mechInfo: map[string]Mechanism{},
mechImpl: map[string]func(*AuthReq) sasl.Server{},
Log: log.New(ioutil.Discard, "", 0),
}
}
func (s *Server) AddMechanism(name string, info Mechanism, handler func(*AuthReq) sasl.Server) {
s.mechInfo[name] = info
s.mechImpl[name] = handler
}
func (s *Server) Serve(l net.Listener) error {
s.l = append(s.l, l)
for {
netConn, err := l.Accept()
if err != nil {
return err
}
go s.handleConn(netConn)
}
}
func (s *Server) handleConn(netConn net.Conn) {
conn := conn{
C: netConn,
W: bufio.NewWriter(netConn),
R: bufio.NewScanner(netConn),
}
defer conn.Close()
cuid := strconv.FormatUint(uint64(atomic.AddUint32(&s.connCount, 1)), 10)
_, err := conn.handshakeServer(cuid, s.mechInfo)
if err != nil {
s.Log.Println("I/O error:", err)
return
}
for {
err := s.handleAuth(&conn)
if err != nil {
if !errors.Is(err, io.EOF) {
s.Log.Println("Protocol error:", err)
}
return
}
}
}
func (s *Server) handleAuth(c *conn) error {
params, err := c.ReadlnExpect("AUTH", 3)
if err != nil {
return err
}
req, err := parseAuthReq(params)
if err != nil {
return err
}
handler := s.mechImpl[req.Mechanism]
if handler == nil {
err = c.Writeln("FAIL", AuthFail{
RequestID: req.RequestID,
Reason: "unsupported mechanism",
}.format()...)
if err != nil {
return err
}
}
serv := handler(req)
resp := req.IR
for {
challenge, done, err := serv.Next(resp)
if err != nil {
err = c.Writeln("FAIL", AuthFail{
RequestID: req.RequestID,
Reason: "authentication failed",
}.format()...)
if err != nil {
return err
}
}
if done {
break
}
if err = c.Writeln("CONT", req.RequestID, base64.StdEncoding.EncodeToString(challenge)); err != nil {
return err
}
params, err := c.ReadlnExpect("CONT", 2)
if err != nil {
return err
}
if params[0] != req.RequestID {
return fmt.Errorf("dovecotsasl: unexpected request ID: %v", params[0])
}
resp, err = base64.StdEncoding.DecodeString(params[1])
if err != nil {
return fmt.Errorf("dovecotsasl: malformed challenge response: %v", err)
}
}
return c.Writeln("OK", req.RequestID)
}
func (s *Server) Close() error {
for _, l := range s.l {
l.Close()
}
return nil
}