-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandshake.go
198 lines (175 loc) · 4.02 KB
/
handshake.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package dovecotsasl
import (
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"os"
"strconv"
)
type Mechanism struct {
Anonymous bool
Plaintext bool
Dictonary bool
Active bool
ForwardSecrecy bool
MutualAuth bool
Private bool
}
func parseMech(params []string) (string, Mechanism, error) {
mech := Mechanism{}
if len(params) == 0 {
return "", mech, fmt.Errorf("dovecotsasl: missing mechanism name")
}
for _, p := range params[1:] {
switch p {
case "anonymous":
mech.Anonymous = true
case "plaintext":
mech.Plaintext = true
case "dictonary":
mech.Dictonary = true
case "active":
mech.Active = true
case "forward-secrecy":
mech.ForwardSecrecy = true
case "mutual-auth":
mech.MutualAuth = true
case "private":
mech.Private = true
}
// Ignore unknown params as required by spec.
}
return params[0], mech, nil
}
func (mech Mechanism) format(name string) []string {
params := make([]string, 0, 1+7)
params = append(params, name)
if mech.Anonymous {
params = append(params, "anonymous")
}
if mech.Plaintext {
params = append(params, "plaintext")
}
if mech.Dictonary {
params = append(params, "dictonary")
}
if mech.Active {
params = append(params, "active")
}
if mech.ForwardSecrecy {
params = append(params, "forward-secrecy")
}
if mech.MutualAuth {
params = append(params, "mutual-auth")
}
if mech.Private {
params = append(params, "mutual-auth")
}
return params
}
type ConnInfo struct {
CPID, SPID string
CUID string
Cookie string
Mechs map[string]Mechanism
}
func (c *conn) handshakeServer(cuid string, mechs map[string]Mechanism) (ConnInfo, error) {
info := ConnInfo{
SPID: strconv.Itoa(os.Getpid()),
CUID: cuid,
Mechs: mechs,
}
if err := c.Writeln("VERSION", "1", "1"); err != nil {
return info, err
}
if err := c.Writeln("SPID", info.SPID); err != nil {
return info, err
}
if err := c.Writeln("CUID", info.CUID); err != nil {
return info, err
}
cookie := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, cookie); err != nil {
return info, fmt.Errorf("dovecotsasl: failed to generate cookie: %w", err)
}
info.Cookie = hex.EncodeToString(cookie)
if err := c.Writeln("COOKIE", info.Cookie); err != nil {
return info, err
}
for name, mech := range info.Mechs {
if err := c.Writeln("MECH", mech.format(name)...); err != nil {
return info, err
}
}
if err := c.Writeln("DONE"); err != nil {
return info, err
}
version, err := c.ReadlnExpect("VERSION", 2)
if err != nil {
return info, err
}
if version[0] != "1" {
return info, fmt.Errorf("dovecotsasl: incompatible client version: %s.%s", version[0], version[1])
}
cpid, err := c.ReadlnExpect("CPID", 1)
if err != nil {
return info, err
}
info.CPID = cpid[0]
return info, nil
}
func (c *conn) handshakeClient() (ConnInfo, error) {
info := ConnInfo{
CPID: strconv.Itoa(os.Getpid()),
Mechs: make(map[string]Mechanism),
}
for {
cmd, params, err := c.Readln()
if err != nil {
return info, err
}
if cmd == "DONE" {
break
}
switch cmd {
case "DONE":
break
case "VERSION":
if len(params) == 0 {
return info, fmt.Errorf("dovecotsasl: missing parameter in VERSION")
}
if params[0] != "1" {
return info, fmt.Errorf("dovecotsasl: incompatible server version: %s.%s", params[0], params[1])
}
case "MECH":
name, mech, err := parseMech(params)
if err != nil {
return info, err
}
info.Mechs[name] = mech
case "SPID":
if len(params) == 0 {
return info, fmt.Errorf("dovecotsasl: missing parameter in SPID")
}
info.SPID = params[0]
case "CUID":
if len(params) == 0 {
return info, fmt.Errorf("dovecotsasl: missing parameter in CUID")
}
info.CUID = params[0]
case "COOKIE":
if len(params) == 0 {
return info, fmt.Errorf("dovecotsasl: missing parameter in COOKIE")
}
info.Cookie = params[0]
}
}
if err := c.Writeln("VERSION", "1", "1"); err != nil {
return info, err
}
if err := c.Writeln("CPID", info.CPID); err != nil {
return info, err
}
return info, nil
}