forked from forwardemail/forwardemail.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imap-server.js
389 lines (339 loc) · 12.9 KB
/
imap-server.js
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* WildDuck Mail Agent is licensed under the European Union Public License 1.2 or later.
* https://github.com/nodemailer/wildduck
*/
const fs = require('node:fs');
const os = require('node:os');
const MessageHandler = require('wildduck/lib/message-handler');
const RateLimiter = require('async-ratelimiter');
const bytes = require('bytes');
const mongoose = require('mongoose');
const pRetry = require('p-retry');
const pWaitFor = require('p-wait-for');
const pify = require('pify');
const ms = require('ms');
const safeStringify = require('fast-safe-stringify');
const { IMAPServer } = require('wildduck/imap-core');
const Aliases = require('#models/aliases');
const AttachmentStorage = require('#helpers/attachment-storage');
const IMAPNotifier = require('#helpers/imap-notifier');
const Indexer = require('#helpers/indexer');
const config = require('#config');
const createTangerine = require('#helpers/create-tangerine');
const env = require('#config/env');
const imap = require('#helpers/imap');
const isRetryableError = require('#helpers/is-retryable-error');
const logger = require('#helpers/logger');
const onAuth = require('#helpers/on-auth');
const refreshSession = require('#helpers/refresh-session');
//
// TODO: handle translation of the folder names (similar to wildduck)
// TODO: send welcome email to user in their sqlite dbs
// TODO: when user deletes account then also purge sqlite databases and backups
// TODO: search filters like has:attachment
// TODO: run validate() on all docs before 'update' and 'insert'
// TODO: mx server forwarding (e.g. can forward to another mailserver such as gmail)
// TODO: auto-reply/vacation responder
// TODO: enforce maxDownload and maxUpload
// TODO: each R2 bucket seems like it's 18 TB max?
// TODO: use error.cause instead of `original_error` or some other stuff
// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause>
// TODO: future items
// - [ ] contacts
// - [ ] calendar
// - [ ] remove setImmediate from SMTP server
// - [ ] remove or enforce maxTimeMS for all imap handlers
// - [ ] projection on all handlers (instead of returning entire document)
// - [ ] allow aliases to configure retention in the future (default is 0)
// TODO: urgent items
// - [ ] when users delete their account then delete all Emails, Messages, Attachments, Threads, and Mailboxes in the system (make this both a job and hook on delete)
// - [ ] auto expunge in bree job when message gets a Deleted flag
// - [ ] message.exp needs to be deleted after time (message.rtime) via background job
// - [ ] mailboxes with retention not 0 need all messages purged after that time
// - [ ] messages expunged need removed after retention time
// TODO: other items
// - [ ] axe should parse out streams
class IMAP {
constructor(
options = {},
secure = env.IMAP_PORT === 993 || env.IMAP_PORT === 2993
) {
this.client = options.client;
this.subscriber = options.subscriber;
this.wsp = options.wsp;
this.resolver = createTangerine(this.client, logger);
//
// NOTE: hard-coded values for now (switch to env later)
// (current limit is 10 failed login attempts per hour)
//
this.rateLimiter = new RateLimiter({
db: this.client,
max: config.smtpLimitMessages,
duration: config.smtpLimitDuration,
namespace: config.smtpLimitNamespace
});
this.logger = logger;
const server = new IMAPServer({
aps: {
enabled: env.APPLE_KEY_PATH && env.APPLE_KEY_ID && env.APPLE_TEAM_ID
},
secure,
secured: false,
disableSTARTTLS: secure,
ignoreSTARTTLS: !secure,
useProxy: false,
socketTimeout: config.socketTimeout,
ignoredHosts: [],
id: {
name: os.hostname(),
version: config.pkg.version,
vendor: config.pkg.author
},
logger: this.logger,
maxMessage: bytes('50MB'),
// NOTE: we don't need this since we have custom logic
// settingsHandler: imap.settingsHandler.bind(this)
enableCompression: true,
skipFetchLog: false,
// <https://github.com/nodemailer/wildduck/issues/635>
// <https://github.com/nodemailer/wildduck/blob/b9349f6e8315873668d605e6567ced2d7b1c0c80/imap-core/lib/imap-server.js#L413-L419>
SNICallback(servername, cb) {
cb(null, server.secureContext.get(servername));
},
logoutMessages: ['Logging out'],
...(config.env === 'production'
? {
key: fs.readFileSync(env.WEB_SSL_KEY_PATH),
cert: fs.readFileSync(env.WEB_SSL_CERT_PATH),
ca: fs.readFileSync(env.WEB_SSL_CA_PATH),
ecdhCurve: 'auto'
}
: {})
});
// override logger
server.logger = this.logger;
server.loggelf = () => {}; // (...args) => this.logger.debug(...args);
server.onAuth = onAuth.bind(this);
server.onAppend = imap.onAppend.bind(this);
server.onCopy = imap.onCopy.bind(this);
server.onCreate = imap.onCreate.bind(this);
server.onDelete = imap.onDelete.bind(this);
server.onExpunge = imap.onExpunge.bind(this);
server.onFetch = imap.onFetch.bind(this);
server.onGetQuota = imap.onGetQuota.bind(this);
server.onGetQuotaRoot = imap.onGetQuotaRoot.bind(this);
server.onList = imap.onList.bind(this);
server.onLsub = imap.onLsub.bind(this);
server.onMove = imap.onMove.bind(this);
server.onOpen = imap.onOpen.bind(this);
server.onRename = imap.onRename.bind(this);
server.onSearch = imap.onSearch.bind(this);
server.onStatus = imap.onStatus.bind(this);
server.onStore = imap.onStore.bind(this);
server.onSubscribe = imap.onSubscribe.bind(this);
server.onUnsubscribe = imap.onUnsubscribe.bind(this);
server.onXAPPLEPUSHSERVICE = imap.onXAPPLEPUSHSERVICE.bind(this);
// kind of hacky but I filed a GH issue
// <https://github.com/nodemailer/smtp-server/issues/135>
server.address = server.server.address.bind(server.server);
server.on('error', (err) => {
logger.error(err);
});
this.attachmentStorage = new AttachmentStorage();
this.indexer = new Indexer({ attachmentStorage: this.attachmentStorage });
// promisified version of prepare message from wildduck message handler
this.prepareMessage = pify(
MessageHandler.prototype.prepareMessage.bind({
indexer: this.indexer,
normalizeSubject: MessageHandler.prototype.normalizeSubject,
generateIndexedHeaders: MessageHandler.prototype.generateIndexedHeaders
})
);
// every hour attempt to run a backup on the connected users
// (initial auth may attempt to backup, but could fail)
this.backupConnections = this.backupConnections.bind(this);
setTimeout(() => {
this.backupConnections();
}, ms('1h'));
// listen for websocket write stream
this.wsp.onUnpackedMessage.addListener(async (data) => {
try {
if (
typeof data === 'object' &&
typeof data.uuid === 'string' &&
typeof data.session_id === 'string' &&
typeof data.alias_id === 'string' &&
data.payload !== undefined
) {
for (const connection of this.server.connections) {
// extra safeguard to use two sources of truth (session ID + alias ID)
if (
connection?.id !== data.session_id ||
connection?.session?.user?.alias_id !== data.alias_id
)
continue;
if (Array.isArray(data.payload)) {
for (const payload of data.payload) {
connection.session.writeStream.write(payload);
}
} else {
connection.session.writeStream.write(data.payload);
}
// attempt to send the request 3x
// eslint-disable-next-line no-await-in-loop
await pRetry(
async () => {
await pWaitFor(
async () => {
try {
await this.wsp.open();
return true;
} catch (err) {
logger.debug(err);
return false;
}
},
{ timeout: ms('15s') }
);
this.wsp.send(data.uuid);
},
{
retries: 2,
onFailedAttempt(err) {
logger.error(err);
if (isRetryableError(err)) {
return;
}
throw err;
}
}
);
break;
}
}
} catch (err) {
logger.fatal(err);
}
});
//
// the notifier is utilized in the IMAP connection (see `wildduck/imap-core/lib/imap-connection.js`)
// in order to `getUpdates` and send them over the socket (e.g. `EXIST`, `EXPUNGE`, `FETCH`)
// <https://github.com/nodemailer/wildduck/issues/509>
//
server.notifier = new IMAPNotifier({
publisher: this.client,
subscriber: this.subscriber
});
this.subscriber.on('message', async (channel, id) => {
if (
channel !== 'sqlite_auth_request' &&
channel !== 'sqlite_auth_reset' &&
channel !== 'pgp_reload'
)
return;
try {
if (typeof id !== 'string' || !mongoose.isObjectIdOrHexString(id))
throw new TypeError('Alias ID missing');
if (channel === 'sqlite_auth_reset') {
for (const connection of this.server.connections) {
if (connection?.session?.user?.alias_id !== id) continue;
connection.clearNotificationListener();
connection.send('* BYE Password was changed');
setImmediate(() => connection.close());
}
return;
}
if (channel === 'sqlite_auth_request') {
for (const connection of this.server.connections) {
if (connection?.session?.user?.alias_id === id) {
// find the most recent connection if any and broadcast that
this.client.publish(
'sqlite_auth_response',
safeStringify(connection.session.user)
);
break;
}
}
return;
}
if (channel === 'pgp_reload') {
const alias = await Aliases.findOne({ id })
.select('has_pgp public_key')
.lean()
.exec();
if (alias) {
for (const connection of this.server.connections) {
if (connection?.session?.user?.alias_id !== id) continue;
connection.session.user.alias_has_pgp = alias.has_pgp;
connection.session.user.alias_public_key = alias.public_key;
}
}
return;
}
throw new TypeError(`Unknown channel ${channel}`);
} catch (err) {
this.logger.error(err);
}
});
this.server = server;
this.refreshSession = refreshSession.bind(this);
this.listen = this.listen.bind(this);
this.close = this.close.bind(this);
}
async backupConnections() {
try {
if (!this?.server?.connections || this.server.connections.size === 0) {
setTimeout(() => {
this.backupConnections();
}, ms('1h'));
return;
}
const ids = new Set();
for (const connection of this.server.connections) {
if (!connection?.session?.user?.alias_id) continue;
if (ids.has(connection.session.user.alias_id)) continue;
ids.add(connection.session.user.alias_id);
try {
// iterate in series with delay
// to prevent piscina worker flooding
// eslint-disable-next-line no-await-in-loop
await this.wsp.request({
action: 'backup',
backup_at: new Date().toISOString(),
session: { user: connection.session.user }
});
// eslint-disable-next-line no-await-in-loop
await ms('1s');
} catch (err) {
this.logger.debug(err, { session: connection.session.user });
}
}
setTimeout(() => {
this.backupConnections();
}, ms('1h'));
} catch (err) {
this.logger.fatal(err);
}
}
async listen(port = env.IMAP_PORT, host = '::', ...args) {
this.subscriber.subscribe('sqlite_auth_request');
this.subscriber.subscribe('sqlite_auth_reset');
await pify(this.server.listen).bind(this.server)(port, host, ...args);
}
async close() {
this.subscriber.unsubscribe('sqlite_auth_request');
this.subscriber.unsubscribe('sqlite_auth_reset');
await pify(this.server.close).bind(this.server)();
}
}
module.exports = IMAP;