-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
172 lines (154 loc) · 3.73 KB
/
auth.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
package dovecotsasl
import (
"encoding/base64"
"fmt"
"net"
"strconv"
"strings"
)
type FailCode string
const (
TempFail = "temp_fail"
AuthzFail = "authz_fail"
UserDisabled = "user_disabled"
PassExpired = "pass_expired"
)
type AuthFail struct {
RequestID string
Code FailCode
Reason string
}
func (af AuthFail) Error() string {
if af.Reason != "" {
return fmt.Sprintf("dovecotsasl: authentication failed: %s (code=%s)", af.Reason, string(af.Code))
}
return fmt.Sprintf("dovecotsasl: authentication failed (code=%s)", string(af.Code))
}
func parseFail(params []string) AuthFail {
if len(params) == 0 {
return AuthFail{}
}
af := AuthFail{
RequestID: params[0],
}
for _, p := range params[1:] {
parts := strings.SplitN(p, "=", 2)
switch parts[0] {
case "reason":
if len(parts) < 2 {
// Skip empty reason.
continue
}
af.Reason = parts[1]
case "code":
if len(parts) < 2 {
// Skip empty reason.
continue
}
af.Code = FailCode(parts[1])
// Legacy, 2.2 codes.
case "temp":
af.Code = TempFail
case "authz":
af.Code = AuthzFail
case "user_disabled":
af.Code = UserDisabled
case "pass_expired":
af.Code = PassExpired
}
}
return af
}
func (af AuthFail) format() []string {
params := make([]string, 0, 3)
params = append(params, af.RequestID)
if af.Reason != "" && !strings.ContainsAny(af.Reason, "\t\n") {
params = append(params, "reason="+af.Reason)
}
if af.Code != "" && !strings.ContainsAny(string(af.Code), "\t\n") {
params = append(params, "code="+string(af.Code))
}
return params
}
type AuthReq struct {
RequestID string
Mechanism string
Service string
LocalIP net.IP
LocalPort uint16
RemoteIP net.IP
RemotePort uint16
Secured bool
ValidClientCert bool
NoPenalty bool
CertUsername bool
IR []byte
}
func parseAuthReq(params []string) (*AuthReq, error) {
if len(params) < 3 {
return nil, fmt.Errorf("dovecotsasl: malformed request: not enough params")
}
req := AuthReq{
RequestID: params[0],
Mechanism: params[1],
}
for _, p := range params[2:] {
parts := strings.SplitN(p, "=", 2)
switch parts[0] {
case "resp":
if len(parts) != 2 {
return nil, fmt.Errorf("dovecotsasl: missing value for resp")
}
resp, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return nil, fmt.Errorf("dovecotsasl: malformed initial response: %v", err)
}
req.IR = resp
case "service":
req.Service = parts[1]
case "secured":
req.Secured = true
case "valid-client-cert":
req.ValidClientCert = true
case "no-penalty":
req.NoPenalty = true
case "cert_username":
req.CertUsername = true
case "lip":
if len(parts) != 2 {
return nil, fmt.Errorf("dovecotsasl: missing value for lip")
}
req.LocalIP = net.ParseIP(parts[1])
if req.LocalIP == nil {
return nil, fmt.Errorf("dovecotsasl: malformed lip: %v", parts[1])
}
case "lport":
if len(parts) != 2 {
return nil, fmt.Errorf("dovecotsasl: missing value for lport")
}
val, err := strconv.ParseUint(parts[1], 10, 16)
if err != nil {
return nil, fmt.Errorf("dovecotsasl: malformed lport: %v", parts[1])
}
req.LocalPort = uint16(val)
case "rip":
if len(parts) != 2 {
return nil, fmt.Errorf("dovecotsasl: missing value for rip")
}
req.RemoteIP = net.ParseIP(parts[1])
if req.RemoteIP == nil {
return nil, fmt.Errorf("dovecotsasl: malformed rip: %v", parts[1])
}
case "rport":
if len(parts) != 2 {
return nil, fmt.Errorf("dovecotsasl: missing value for rport")
}
val, err := strconv.ParseUint(parts[1], 10, 16)
if err != nil {
return nil, fmt.Errorf("dovecotsasl: malformed rport: %v", parts[1])
}
req.RemotePort = uint16(val)
}
}
return &req, nil
}