This repository has been archived by the owner on Feb 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.go
154 lines (146 loc) · 3.35 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
package gosmtp
import (
"encoding/base64"
"strings"
"unicode"
)
/*
SupportedAuthMechanisms is array of string describing currently supported/implemented
authentication mechanisms
*/
var SupportedAuthMechanisms = []string{"LOGIN", "PLAIN"}
func (s *session) handleLoginAuth(cmd *command) {
args := cmd.arguments
var username string
var password []byte
if len(args) == 1 {
line, err := s.ReadLine()
if err != nil {
s.state = sessionStateAborted
return
}
cmd := strings.TrimRightFunc(line, unicode.IsSpace)
s.log.Printf("INFO: received AUTH cmd: '%s'", cmd)
data, err := base64.StdEncoding.DecodeString(cmd)
if err != nil {
s.Out("501 malformed auth input")
s.badCommandsCount++
return
}
username = string(data)
s.Out("334 UGFzc3dvcmQ6")
line, err = s.ReadLine()
if err != nil {
s.state = sessionStateAborted
return
}
cmd = strings.TrimRightFunc(line, unicode.IsSpace)
s.log.Printf("INFO: received second AUTH cmd: '%s'", cmd)
password, err = base64.StdEncoding.DecodeString(cmd)
if err != nil {
s.Out("501 malformed auth input")
return
}
} else if len(args) == 2 {
data, err := base64.StdEncoding.DecodeString(args[1])
if err != nil {
s.Out("501 malformed auth input")
return
}
username = string(data)
s.Out("334 UGFzc3dvcmQ6")
line, err := s.ReadLine()
if err != nil {
s.Out(Codes.FailLineTooLong)
s.badCommandsCount++
return
}
cmd := strings.TrimRightFunc(line, unicode.IsSpace)
s.log.Printf("INFO: received second AUTH cmd: '%s'", cmd)
password, err = base64.StdEncoding.DecodeString(cmd)
if err != nil {
s.Out("501 malformed auth input")
s.state = sessionStateAborted
return
}
}
// check login
s.peer.Username = username
ok, err := s.srv.Authenticator(s.peer, password)
if err != nil {
s.Out(Codes.ErrorAuth)
s.state = sessionStateAborted
return
}
if !ok {
s.Out(Codes.FailAuthentication)
return
}
// login succeeded
s.peer.Authenticated = true
s.Out(Codes.SuccessAuthentication)
return
}
func (s *session) handlePlainAuth(cmd *command) {
args := cmd.arguments
var authData []byte
var err error
// check that PLAIN auth input is of valid form
if len(args) == 1 {
s.Out("334")
line, err := s.ReadLine()
if err != nil {
s.state = sessionStateAborted
return
}
cmd, err := parseCommand(strings.TrimRightFunc(line, unicode.IsSpace))
s.log.Printf("INFO: received AUTH cmd: '%s'", cmd)
if err != nil {
s.Out(Codes.FailUnrecognizedCmd)
s.badCommandsCount++
return
}
authData, err = base64.StdEncoding.DecodeString(cmd.arguments[0])
if err != nil {
s.Out("501 malformed auth input")
s.badCommandsCount++
return
}
} else if len(args) == 2 {
authData, err = base64.StdEncoding.DecodeString(args[1])
if err != nil {
s.Out("501 malformed auth input")
s.badCommandsCount++
return
}
}
// split
t := make([][]byte, 3)
i := 0
for _, b := range authData {
if b == 0 {
i++
continue
}
t[i] = append(t[i], b)
}
//authId := string(t[0])
authLogin := string(t[1])
authPasswd := t[2]
// check login
s.peer.Username = authLogin
ok, err := s.srv.Authenticator(s.peer, authPasswd)
if err != nil {
s.Out(Codes.ErrorAuth)
s.state = sessionStateAborted
return
}
if !ok {
s.Out(Codes.FailAuthentication)
return
}
// login succeeded
s.peer.Authenticated = true
s.Out(Codes.SuccessAuthentication)
return
}