-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserve.go
187 lines (164 loc) · 3.94 KB
/
serve.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
package milter
import (
"errors"
"fmt"
"io"
"log"
"net"
"net/textproto"
"strconv"
"strings"
)
// Serve accepts connections received on l, and processes them with milters
// returned by newMilter.
func Serve(l net.Listener, newMilter func() Milter) error {
for {
c, err := l.Accept()
if err != nil {
return err
}
go func() {
mc := newConn(c)
err := mc.run(newMilter())
if err != nil {
log.Println(err)
}
}()
}
}
func (c *conn) run(milter Milter) error {
defer c.conn.Close()
for {
data, err := c.readPacket()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
if len(data) == 0 {
return errors.New("zero-length command packet")
}
command := data[0]
data = data[1:]
var resp Response = Continue
if command != c.macrosFor {
c.macros = nil
}
switch command {
case 'O':
// Negotiate connection options.
var optNeg struct {
Version uint32
Actions uint32
Protocol uint32
}
if err := decode(data, &optNeg); err != nil {
return fmt.Errorf("error decoding options from server: %v", err)
}
optNeg.Version = 2
optNeg.Actions = 0x1f
optNeg.Protocol = 0
if err := c.writeResponse('O', encode(optNeg)); err != nil {
return err
}
continue // Writing the 'O' response was all the response that was needed.
case 'D':
// Define macros.
if len(data) == 0 {
return errors.New("macro-definition packet with no data")
}
c.macrosFor = data[0]
c.macros = map[string]string{}
kv := splitCStrings(data[1:])
for i := 0; i < len(kv)-1; i += 2 {
c.macros[stripBrackets(kv[i], "{}")] = kv[i+1]
}
continue // A macro packet doesn't need a response.
case 'A':
// Abort (cancel current message and get ready to process a new one).
c.headers = nil
c.body = nil
continue // An abort packet doesn't need a response.
case 'Q':
// Quit.
return nil
case 'C':
// Connect.
var connInfo struct {
Hostname string
ProtocolFamily byte
Port uint16
Address string
}
if err := decode(data, &connInfo); err != nil {
return fmt.Errorf("error decoding connection info: %v", err)
}
var network, address string
switch connInfo.ProtocolFamily {
case 'L':
network = "unix"
address = connInfo.Address
case '4':
network = "tcp4"
address = net.JoinHostPort(connInfo.Address, strconv.Itoa(int(connInfo.Port)))
case '6':
network = "tcp6"
address = net.JoinHostPort(connInfo.Address, strconv.Itoa(int(connInfo.Port)))
}
resp = milter.Connect(connInfo.Hostname, network, address, c.macros)
case 'H':
// HELO.
name := strings.TrimSuffix(string(data), "\x00")
resp = milter.Helo(name, c.macros)
case 'M':
// MAIL FROM.
args := splitCStrings(data)
if len(args) == 0 {
return errors.New("MAIL FROM with no address")
}
from := stripBrackets(args[0], "<>")
resp = milter.From(from, c.macros)
case 'R':
// RCPT TO.
args := splitCStrings(data)
if len(args) == 0 {
return errors.New("RCPT TO with no address")
}
to := stripBrackets(args[0], "<>")
resp = milter.To(to, c.macros)
case 'T':
// DATA.
// Just ignore it, to avoid complicating the milter interface further.
case 'L':
// a header
if c.headers == nil {
c.headers = make(textproto.MIMEHeader)
}
keyVal := splitCStrings(data)
if len(keyVal) != 2 {
return fmt.Errorf("header key/value pair with %d items (should be 2)", len(keyVal))
}
c.headers.Add(keyVal[0], keyVal[1])
case 'N':
// end of headers
resp = milter.Headers(c.headers)
c.headers = nil
case 'B':
// a chunk of the body
c.body = append(c.body, data...)
case 'E':
// the end of the body
resp = milter.Body(c.body, c)
if c.err != nil {
return c.err
}
c.body = nil
default:
log.Printf("Unrecognized command code: %c", command)
}
if err := c.writeResponse(resp.Response()); err != nil {
return err
}
}
}