forked from katzenpost/mailproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
232 lines (202 loc) · 6.96 KB
/
api.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
// api.go - Katzenpost client mailproxy external API
// Copyright (C) 2018 Yawning Angel, David Anthony Stainton.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package mailproxy
import (
"errors"
"github.com/emersion/go-message"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/mailproxy/event"
"github.com/katzenpost/mailproxy/internal/account"
"github.com/katzenpost/mailproxy/internal/imf"
)
var (
// ErrUnknownRecipient is the error that is returned when a recipient for
// which there is no public key is specified.
ErrUnknownRecipient = errors.New("mailproxy/api: unknown recipient, missing public key")
// ErrNoMessages is the error that is returned when an account's receive
// queue is empty.
ErrNoMessages = errors.New("mailproxy/api: account receive queue empty")
)
// SendMessage enqueues payload for transmission from the sender to the
// recipient (account IDs). The payload MUST be a well formed IMF message.
//
// Any delivery failures after the message has been successfully enqueued will
// result in a delivery status notification message being sent from the
// postmaster to the senderID account.
func (p *Proxy) SendMessage(senderID, recipientID string, payload []byte) error {
acc, _, err := p.getAccount(senderID)
if err != nil {
return err
}
defer acc.Deref()
rcpt, err := p.toAccountRecipient(recipientID)
if err != nil {
return err
}
if rcpt.PublicKey == nil {
return ErrUnknownRecipient
}
// Validate and pre-process the outgoing message body.
payloadIMF, _, isUnreliable, err := p.preprocessOutgoing(payload, true)
if err != nil {
return err
}
// Enqueue the outgoing message.
if err = acc.EnqueueMessage(rcpt, payloadIMF, isUnreliable); err != nil {
return err
}
return nil
}
func (p *Proxy) toAccountRecipient(recipientID string) (*account.Recipient, error) {
rcptID, local, domain, err := p.recipients.Normalize(recipientID)
if err != nil {
return nil, err
}
return &account.Recipient{
ID: rcptID,
User: local,
Provider: domain,
PublicKey: p.recipients.Get(rcptID),
}, nil
}
func (p *Proxy) preprocessOutgoing(b []byte, viaESMTP bool) ([]byte, *message.Entity, bool, error) {
// Parse the message payload so that headers can be manipulated,
// and ensure that there is a Message-ID header, and prepend the
// "Received" header.
entity, err := imf.BytesToEntity(b)
if err != nil {
return nil, nil, false, err
}
imf.AddMessageID(entity)
imf.AddReceived(entity, true, viaESMTP)
isUnreliable, err := imf.IsUnreliable(entity)
if err != nil {
return nil, nil, false, err
}
// Re-serialize the IMF message now to apply the new headers,
// and canonicalize the line endings.
payload, err := imf.EntityToBytes(entity)
return payload, entity, isUnreliable, err
}
// ReceivePeek returns the eldest message in the given account's receive queue,
// the sender's public key if any, and a unique identifier tag. The account's
// receive queue is left intact.
func (p *Proxy) ReceivePeek(accountID string) ([]byte, *ecdh.PublicKey, []byte, error) {
return p.doReceivePeekPop(accountID, false)
}
// ReceivePop removes and returns the eldest message in the given account's
// receive queue, the sender's public key if any, and a unique identifier tag.
func (p *Proxy) ReceivePop(accountID string) ([]byte, *ecdh.PublicKey, []byte, error) {
return p.doReceivePeekPop(accountID, true)
}
func (p *Proxy) doReceivePeekPop(accountID string, isPop bool) ([]byte, *ecdh.PublicKey, []byte, error) {
acc, _, err := p.getAccount(accountID)
if err != nil {
return nil, nil, nil, err
}
defer acc.Deref()
msg, sender, msgID, err := acc.ReceivePeekPop(isPop)
if msg == nil && sender == nil && msgID == nil && err == nil {
// Allow the caller to easily distinguish an empty queue.
err = ErrNoMessages
}
return msg, sender, msgID, err
}
func (p *Proxy) getAccount(accountID string) (*account.Account, string, error) {
accID, _, _, err := p.recipients.Normalize(accountID)
if err != nil {
return nil, "", err
}
acc, err := p.accounts.Get(accID)
if err != nil {
return nil, "", err
}
return acc, accID, nil
}
// GetRecipient returns the public key for the provided recipient.
func (p *Proxy) GetRecipient(recipientID string) (*ecdh.PublicKey, error) {
// Somewhat redundant because Store.Get will also normalize, but
// Get treats parse errors as unknown recipients rather than
// returning an error.
_, _, _, err := p.recipients.Normalize(recipientID)
if err != nil {
return nil, err
}
pk := p.recipients.Get(recipientID)
if pk == nil {
err = ErrUnknownRecipient
}
return pk, err
}
// SetRecipient sets the public key for the provided recipient.
func (p *Proxy) SetRecipient(recipientID string, publicKey *ecdh.PublicKey) error {
return p.recipients.Set(recipientID, publicKey)
}
// RemoveRecipient removes the provided recipient. This has no impact on
// messages that have already been enqueued for transmission via SendMessage.
func (p *Proxy) RemoveRecipient(recipientID string) error {
return p.recipients.Clear(recipientID)
}
// ListRecipients returns a map of recipientIDs to public keys consisting of
// all currently known entries. Modifications to the returned map have no
// effect.
func (p *Proxy) ListRecipients() map[string]*ecdh.PublicKey {
return p.recipients.CloneRecipients()
}
// IsConnected returns true iff a connection to the provider is established.
func (p *Proxy) IsConnected(accountID string) bool {
acc, _, err := p.getAccount(accountID)
if err != nil {
return false
}
defer acc.Deref()
return acc.IsConnected()
}
func (p *Proxy) apiEventWorker() {
defer p.eventCh.Close()
// The main reason why this helper exists is so that the internal event
// generation code can assume there is an event sync regardless of if
// the caller has specified an event listener or not.
var flushCh chan event.Event
if p.cfg.Proxy.EventSink != nil {
flushCh = make(chan event.Event)
defer close(flushCh)
p.Go(func() {
for {
select {
case <-p.HaltCh():
return
case ev := <-flushCh:
p.cfg.Proxy.EventSink <- ev
}
}
})
}
for {
select {
case <-p.HaltCh():
return
case ev := <-p.eventCh.Out():
if flushCh != nil {
// "Bad" things will happen if the sink is slow, but
// p.eventCh is a drop-head buffered channel with a
// fairly large capacity, so the "bad" is limited to
// missed events.
flushCh <- ev.(event.Event)
}
}
}
}