-
Notifications
You must be signed in to change notification settings - Fork 22
/
session.go
279 lines (242 loc) · 6.36 KB
/
session.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package milter
import (
"bufio"
"bytes"
"encoding/binary"
"io"
"log"
"net"
"net/textproto"
"strings"
)
// OptAction sets which actions the milter wants to perform.
// Multiple options can be set using a bitmask.
type OptAction uint32
// OptProtocol masks out unwanted parts of the SMTP transaction.
// Multiple options can be set using a bitmask.
type OptProtocol uint32
const (
// set which actions the milter wants to perform
OptAddHeader OptAction = 0x01
OptChangeBody OptAction = 0x02
OptAddRcpt OptAction = 0x04
OptRemoveRcpt OptAction = 0x08
OptChangeHeader OptAction = 0x10
OptQuarantine OptAction = 0x20
OptChangeFrom OptAction = 0x40
// mask out unwanted parts of the SMTP transaction
OptNoConnect OptProtocol = 0x01
OptNoHelo OptProtocol = 0x02
OptNoMailFrom OptProtocol = 0x04
OptNoRcptTo OptProtocol = 0x08
OptNoBody OptProtocol = 0x10
OptNoHeaders OptProtocol = 0x20
OptNoEOH OptProtocol = 0x40
)
// milterSession keeps session state during MTA communication
type milterSession struct {
actions OptAction
protocol OptProtocol
sock io.ReadWriteCloser
headers textproto.MIMEHeader
macros map[string]string
milter Milter
}
// ReadPacket reads incoming milter packet
func (c *milterSession) ReadPacket() (*Message, error) {
// read packet length
var length uint32
if err := binary.Read(c.sock, binary.BigEndian, &length); err != nil {
return nil, err
}
// read packet data
data := make([]byte, length)
if _, err := io.ReadFull(c.sock, data); err != nil {
return nil, err
}
// prepare response data
message := Message{
Code: data[0],
Data: data[1:],
}
return &message, nil
}
// WritePacket sends a milter response packet to socket stream
func (m *milterSession) WritePacket(msg *Message) error {
buffer := bufio.NewWriter(m.sock)
// calculate and write response length
length := uint32(len(msg.Data) + 1)
if err := binary.Write(buffer, binary.BigEndian, length); err != nil {
return err
}
// write response code
if err := buffer.WriteByte(msg.Code); err != nil {
return err
}
// write response data
if _, err := buffer.Write(msg.Data); err != nil {
return err
}
// flush data to network socket stream
if err := buffer.Flush(); err != nil {
return err
}
return nil
}
// Process processes incoming milter commands
func (m *milterSession) Process(msg *Message) (Response, error) {
switch msg.Code {
case 'A':
// abort current message and start over
m.headers = nil
m.macros = nil
// do not send response
return nil, nil
case 'B':
// body chunk
return m.milter.BodyChunk(msg.Data, newModifier(m))
case 'C':
// new connection, get hostname
Hostname := readCString(msg.Data)
msg.Data = msg.Data[len(Hostname)+1:]
// get protocol family
protocolFamily := msg.Data[0]
msg.Data = msg.Data[1:]
// get port
var Port uint16
if protocolFamily == '4' || protocolFamily == '6' {
if len(msg.Data) < 2 {
return RespTempFail, nil
}
Port = binary.BigEndian.Uint16(msg.Data)
msg.Data = msg.Data[2:]
if protocolFamily == '6' {
// trim IPv6 prefix when necessary
Prefix := []byte("IPv6:")
if bytes.HasPrefix(msg.Data, Prefix) {
msg.Data = bytes.TrimPrefix(msg.Data, Prefix)
}
}
}
// get address
Address := readCString(msg.Data)
// convert address and port to human readable string
family := map[byte]string{
'U': "unknown",
'L': "unix",
'4': "tcp4",
'6': "tcp6",
}
// run handler and return
return m.milter.Connect(
Hostname,
family[protocolFamily],
Port,
net.ParseIP(Address),
newModifier(m))
case 'D':
// define macros
m.macros = make(map[string]string)
// convert data to Go strings
data := decodeCStrings(msg.Data[1:])
if len(data) != 0 {
// store data in a map
for i := 0; i < len(data); i += 2 {
m.macros[data[i]] = data[i+1]
}
}
// do not send response
return nil, nil
case 'E':
// call and return milter handler
return m.milter.Body(newModifier(m))
case 'H':
// helo command
name := strings.TrimSuffix(string(msg.Data), null)
return m.milter.Helo(name, newModifier(m))
case 'L':
// make sure headers is initialized
if m.headers == nil {
m.headers = make(textproto.MIMEHeader)
}
// add new header to headers map
HeaderData := decodeCStrings(msg.Data)
if len(HeaderData) != 0 {
headerDataStr := ""
if len(HeaderData) == 2 {
headerDataStr = HeaderData[1]
}
m.headers.Add(HeaderData[0], headerDataStr)
// call and return milter handler
return m.milter.Header(HeaderData[0], headerDataStr, newModifier(m))
}
case 'M':
// envelope from address
envfrom := readCString(msg.Data)
return m.milter.MailFrom(strings.Trim(envfrom, "<>"), newModifier(m))
case 'N':
// end of headers
return m.milter.Headers(m.headers, newModifier(m))
case 'O':
// ignore request and prepare response buffer
buffer := new(bytes.Buffer)
// prepare response data
for _, value := range []uint32{2, uint32(m.actions), uint32(m.protocol)} {
if err := binary.Write(buffer, binary.BigEndian, value); err != nil {
return nil, err
}
}
// build and send packet
return NewResponse('O', buffer.Bytes()), nil
case 'Q':
// client requested session close
return nil, errCloseSession
case 'R':
// envelope to address
envto := readCString(msg.Data)
return m.milter.RcptTo(strings.Trim(envto, "<>"), newModifier(m))
case 'T':
// data, ignore
default:
// print error and close session
log.Printf("Unrecognized command code: %c", msg.Code)
return nil, errCloseSession
}
// by default continue with next milter message
return RespContinue, nil
}
// HandleMilterComands processes all milter commands in the same connection
func (m *milterSession) HandleMilterCommands() {
// close session socket on exit
defer m.sock.Close()
for {
// ReadPacket
msg, err := m.ReadPacket()
if err != nil {
if err != io.EOF {
log.Printf("Error reading milter command: %v", err)
}
return
}
// process command
resp, err := m.Process(msg)
if err != nil {
if err != errCloseSession {
// log error condition
log.Printf("Error performing milter command: %v", err)
}
return
}
// ignore empty responses
if resp != nil {
// send back response message
if err = m.WritePacket(resp.Response()); err != nil {
log.Printf("Error writing packet: %v", err)
return
}
if !resp.Continue() {
return
}
}
}
}