/
localhandler.go
139 lines (116 loc) · 3.24 KB
/
localhandler.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
// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package smtp
import "strings"
// LocalHandler is an handler using local environment.
type LocalHandler struct {
env *Environment
}
// NewLocalHandler create an handler using local environment.
func NewLocalHandler(env *Environment) (local *LocalHandler) {
local = &LocalHandler{
env: env,
}
return local
}
// ServeAuth handle SMTP AUTH parameter username and password.
func (lh *LocalHandler) ServeAuth(username, password string) (
res *Response, err error,
) {
res = &Response{
Code: StatusAuthenticated,
Message: "2.7.0 Authentication successful",
}
username = strings.ToLower(username)
localDomain := strings.Split(username, "@")
if len(localDomain) != 2 {
return nil, ErrInvalidCredential
}
if localDomain[1] == lh.env.PrimaryDomain.Name {
acc, ok := lh.env.PrimaryDomain.Accounts[localDomain[0]]
if !ok {
return nil, ErrInvalidCredential
}
// System accounts have empty length password.
if len(acc.HashPass) == 0 {
return nil, ErrInvalidCredential
}
err = acc.Authenticate(password)
if err != nil {
return nil, ErrInvalidCredential
}
return res, nil
}
for _, vdom := range lh.env.VirtualDomains {
if localDomain[1] != vdom.Name {
continue
}
acc, ok := vdom.Accounts[localDomain[0]]
if !ok {
return nil, ErrInvalidCredential
}
// System accounts have empty length password.
if len(acc.HashPass) == 0 {
return nil, ErrInvalidCredential
}
err = acc.Authenticate(password)
if err != nil {
return nil, ErrInvalidCredential
}
return res, nil
}
return nil, ErrInvalidCredential
}
// ServeBounce handle email transaction with unknown or invalid recipent.
func (lh *LocalHandler) ServeBounce(_ *MailTx) (res *Response, err error) {
// TODO: send delivery status notification to sender address.
return nil, nil
}
// ServeExpand handle SMTP EXPN command.
//
// TODO: The group feature currently is not supported.
func (lh *LocalHandler) ServeExpand(_ string) (res *Response, err error) {
res = &Response{
Code: StatusCmdNotImplemented,
Message: "Command not implemented",
}
return res, nil
}
// ServeMailTx handle processing the final delivery of incoming mail.
// TODO: implement it.
func (lh *LocalHandler) ServeMailTx(_ *MailTx) (res *Response, err error) {
return nil, nil
}
// ServeVerify handle SMTP VRFY command. The username must be in the format
// of mailbox, "local@domain".
func (lh *LocalHandler) ServeVerify(username string) (res *Response, err error) {
username = strings.ToLower(username)
localDomain := strings.Split(username, "@")
if len(localDomain) != 2 {
return nil, errCmdSyntaxError
}
res = &Response{
Code: StatusOK,
}
if localDomain[1] == lh.env.PrimaryDomain.Name {
acc, ok := lh.env.PrimaryDomain.Accounts[localDomain[0]]
if ok {
res.Message = acc.String()
return res, nil
}
}
for _, vdom := range lh.env.VirtualDomains {
if localDomain[1] != vdom.Name {
continue
}
acc, ok := vdom.Accounts[localDomain[0]]
if ok {
res.Message = acc.String()
return res, nil
}
}
res.Code = StatusMailboxNotFound
res.Message = "mailbox not found"
return res, nil
}