-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
133 lines (115 loc) · 2.81 KB
/
client.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
package dovecotsasl
import (
"bufio"
"encoding/base64"
"fmt"
"net"
"strconv"
"github.com/emersion/go-sasl"
)
type Client struct {
c conn
info ConnInfo
rid int
}
func NewClient(netConn net.Conn) (*Client, error) {
c := Client{
c: conn{
C: netConn,
W: bufio.NewWriter(netConn),
R: bufio.NewScanner(netConn),
},
}
info, err := c.c.handshakeClient()
if err != nil {
return nil, err
}
c.info = info
return &c, nil
}
// LocalIP formats local server IP for use in Client.Do params.
func LocalIP(ip net.IP) string {
return "lip=" + ip.String()
}
// LocalIP formats local port for use in Client.Do params.
func LocalPort(i uint16) string {
return "lport=" + strconv.Itoa(int(i))
}
// LocalIP formats remote IP for use in Client.Do params.
func RemoteIP(ip net.IP) string {
return "rip=" + ip.String()
}
// LocalIP formats remote port for use in Client.Do params.
func RemotePort(i uint16) string {
return "rport=" + strconv.Itoa(int(i))
}
// Constants for Client.Do params.
// See https://wiki.dovecot.org/Design/AuthProtocol for description.
const (
Secured = "secured"
CertUsername = "cert_username"
ValidClientCert = "valid-client-cert"
NoPenalty = "no-penalty"
)
// Do performs SASL authentication using Dovecot SASL server and provided
// sasl.Client implementation.
func (c *Client) Do(service string, cl sasl.Client, extraParams ...string) error {
mech, ir, err := cl.Start()
if err != nil {
return err
}
if _, ok := c.info.Mechs[mech]; !ok {
return fmt.Errorf("dovecotsasl: unsupported mechanism: %v", mech)
}
c.rid++
rid := strconv.Itoa(c.rid)
params := make([]string, 0, 8)
params = append(params, rid, mech, "service="+service)
params = append(params, extraParams...)
if ir != nil {
params = append(params, "resp="+base64.StdEncoding.EncodeToString(ir))
}
if err := c.c.Writeln("AUTH", params...); err != nil {
return err
}
for {
cmd, params, err := c.c.Readln()
if err != nil {
return err
}
if len(params) == 0 {
return fmt.Errorf("dovecotsasl: missing reply params")
}
if params[0] != rid {
return fmt.Errorf("dovecotsasl: request ID mismatch, sent %s, received %s", rid, params[0])
}
switch cmd {
case "FAIL":
return parseFail(params)
case "CONT":
if len(params) < 2 {
return fmt.Errorf("dovecotsasl: missing challenge param")
}
challenge, err := base64.StdEncoding.DecodeString(params[1])
if err != nil {
return fmt.Errorf("dovecotsasl: malformed challenge: %v", err)
}
response, err := cl.Next(challenge)
if err != nil {
return err
}
err = c.c.Writeln("CONT", rid, base64.StdEncoding.EncodeToString(response))
if err != nil {
return err
}
case "OK":
return nil
}
}
}
func (c *Client) ConnInfo() ConnInfo {
return c.info
}
func (cl *Client) Close() error {
return cl.c.Close()
}