forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outbound.js
1300 lines (1148 loc) · 43.4 KB
/
outbound.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
var fs = require('fs');
var path = require('path');
var dns = require('dns');
var net = require('net');
var util = require("util");
var events = require("events");
var utils = require('./utils');
var sock = require('./line_socket');
var server = require('./server');
var logger = require('./logger');
var config = require('./config');
var constants = require('./constants');
var trans = require('./transaction');
var plugins = require('./plugins');
var async = require('async');
var Address = require('./address').Address;
var TimerQueue = require('./timer_queue');
var date_to_str = utils.date_to_str;
var existsSync = utils.existsSync;
var FsyncWriteStream = require('./fsync_writestream');
var core_consts = require('constants');
var WRITE_EXCL = core_consts.O_CREAT | core_consts.O_TRUNC | core_consts.O_WRONLY | core_consts.O_EXCL;
var DENY = constants.deny;
var OK = constants.ok;
var MAX_UNIQ = 10000;
var host = require('os').hostname().replace(/\\/, '\\057').replace(/:/, '\\072');
var fn_re = /^(\d+)_(\d+)_/; // I like how this looks like a person
var queue_dir = path.resolve(config.get('queue_dir') || (process.env.HARAKA + '/queue'));
var uniq = Math.round(Math.random() * MAX_UNIQ);
var MAX_CONCURRENCY = config.get('outbound.concurrency_max') || 100;
var load_queue = async.queue(function (file, cb) {
var hmail = new HMailItem(file, path.join(queue_dir, file));
exports._add_file(hmail);
hmail.once('ready', cb);
}, MAX_CONCURRENCY);
var in_progress = 0;
var delivery_queue = async.queue(function (hmail, cb) {
in_progress++;
hmail.next_cb = function () {
in_progress--;
cb();
}
hmail.send()
}, MAX_CONCURRENCY);
var temp_fail_queue = new TimerQueue();
var queue_count = 0;
exports.get_stats = function () {
return in_progress + '/' + delivery_queue.length() + '/' + temp_fail_queue.length();
}
exports.list_queue = function (cb) {
this._load_cur_queue(null, "_list_file", cb);
}
exports.stat_queue = function (cb) {
var self = this;
this._load_cur_queue(null, "_stat_file", function () {
return cb(self.stats());
});
}
exports.scan_queue_pids = function (cb) {
var self = this;
fs.readdir(queue_dir, function (err, files) {
if (err) {
self.logerror("Failed to load queue directory (" + queue_dir + "): " + err);
return cb(err);
}
var pids = {};
files.forEach(function (file) {
if (/^\./.test(file)) {
// dot-file...
self.logwarn("Removing left over dot-file: " + file);
return fs.unlink(file, function () {});
}
// Format: $time_$attempts_$pid_$uniq.$host
var match = /^\d+_\d+_(\d+)_\d+\./.exec(file);
if (!match) {
self.logerror("Unrecognised file in queue directory: " + queue_dir + '/' + file);
return;
}
pids[match[1]] = true;
});
return cb(null, Object.keys(pids));
});
}
process.on('message', function (msg) {
if (msg.event && msg.event === 'outbound.load_pid_queue') {
exports.load_pid_queue(msg.data);
}
else if (msg.event && msg.event === 'outbound.flush_queue') {
exports.flush_queue();
}
// otherwise ignore the message
});
exports.flush_queue = function () {
temp_fail_queue.drain();
}
exports.load_pid_queue = function (pid) {
this.loginfo("Loading queue for pid: " + pid);
this.load_queue(pid);
}
exports.load_queue = function (pid) {
// Initialise and load queue
// we create the dir here because this is used when Haraka is running
// properly.
// no reason not to do this stuff syncronously - we're just loading here
if (!existsSync(queue_dir)) {
this.logdebug("Creating queue directory " + queue_dir);
try {
fs.mkdirSync(queue_dir, 493); // 493 == 0755
}
catch (err) {
if (err.code != 'EEXIST') {
logger.logerror("Error creating queue directory: " + err);
throw err;
}
}
}
this._load_cur_queue(pid, "_add_file");
}
exports._load_cur_queue = function (pid, cb_name, cb) {
var self = this;
self.loginfo("Loading outbound queue from ", queue_dir);
fs.readdir(queue_dir, function (err, files) {
if (err) {
return self.logerror("Failed to load queue directory (" + queue_dir + "): " + err);
}
self.cur_time = new Date(); // set this once so we're not calling it a lot
self.load_queue_files(pid, cb_name, files);
if (cb) cb();
});
}
exports.load_queue_files = function (pid, cb_name, files) {
var self = this;
if (files.length === 0) return;
if (config.get('outbound.disabled') && cb_name === '_add_file') {
// try again in 1 second if delivery is disabled
setTimeout(function () {self.load_queue_files(pid, cb_name, files)}, 1000);
return;
}
if (pid) {
// Pre-scan to rename PID files to my PID:
this.loginfo("Grabbing queue files for pid: " + pid);
async.eachLimit(files, 200, function (file, cb) {
var match = /^(\d+)(_\d+_)(\d+)(_\d+\..*)$/.exec(file);
if (match && match[3] == pid) {
var next_process = match[1];
var new_filename = match[1] + match[2] + process.pid + match[4];
// self.loginfo("Renaming: " + file + " to " + new_filename);
fs.rename(queue_dir + '/' + file, queue_dir + '/' + new_filename, function (err) {
if (err) {
self.logerror("Unable to rename queue file: " + file + " to " + new_filename + " : " + err);
return cb();
}
if (next_process <= self.cur_time) {
load_queue.push(new_filename);
}
else {
temp_fail_queue.add(next_process - self.cur_time, function () { load_queue.push(new_filename) });
}
// self.loginfo("Done");
cb();
});
}
else if (/^\./.test(file)) {
// dot-file...
self.logwarn("Removing left over dot-file: " + file);
return fs.unlink(queue_dir + "/" + file, function (err) {
if (err) {
self.logerror("Error removing dot-file: " + file + ": " + err);
}
cb();
});
}
else {
// Do this because otherwise we blow the stack
async.setImmediate(cb);
}
}, function (err) {
if (err) {
// no error cases yet, but log anyway
self.logerror("Error fixing up queue files: " + err);
}
self.loginfo("Done fixing up old PID queue files");
self.loginfo(delivery_queue.length() + " files in my delivery queue");
self.loginfo(load_queue.length() + " files in my load queue");
self.loginfo(temp_fail_queue.length() + " files in my temp fail queue");
});
}
else {
self.loginfo("Loading the queue...");
files.forEach(function (file) {
if (/^\./.test(file)) {
// dot-file...
self.logwarn("Removing left over dot-file: " + file);
return fs.unlink(queue_dir + "/" + file, function () {});
}
var matches = file.match(fn_re);
if (!matches) {
self.logerror("Unrecognised file in queue folder: " + file);
return;
}
var next_process = matches[1];
if (cb_name === '_add_file') {
if (next_process <= self.cur_time) {
load_queue.push(file);
}
else {
temp_fail_queue.add(next_process - self.cur_time, function () { load_queue.push(file) });
}
}
else {
self[cb_name](file);
}
});
}
}
exports._add_file = function (hmail) {
var self = this;
// this.loginfo("Adding file: " + hmail.filename);
if (hmail.next_process < this.cur_time) {
delivery_queue.push(hmail);
}
else {
temp_fail_queue.add(hmail.next_process - this.cur_time, function () { delivery_queue.push(hmail) });
}
}
exports._list_file = function (file) {
// TODO: output more data here?
console.log("Q: " + file);
}
exports._stat_file = function () {
queue_count++;
}
exports.stats = function () {
// TODO: output more data here
var results = {
queue_dir: queue_dir,
queue_count: queue_count,
};
return results;
}
function _next_uniq () {
var result = uniq++;
if (uniq >= MAX_UNIQ) {
uniq = 1;
}
return result;
}
function _fname () {
var time = new Date().getTime();
return time + '_0_' + process.pid + "_" + _next_uniq() + '.' + host;
}
exports.send_email = function () {
if (arguments.length === 2) {
this.loginfo("Sending email as with a transaction");
return this.send_trans_email(arguments[0], arguments[1]);
}
var self = this;
var from = arguments[0],
to = arguments[1],
contents = arguments[2];
var next = arguments[3];
this.loginfo("Sending email via params");
var transaction = trans.createTransaction();
this.loginfo("Created transaction: " + transaction.uuid);
// set MAIL FROM address, and parse if it's not an Address object
if (from instanceof Address) {
transaction.mail_from = from;
}
else {
try {
from = new Address(from);
}
catch (err) {
return next(DENY, "Malformed from: " + err);
}
transaction.mail_from = from;
}
// Make sure to is an array
if (!(Array.isArray(to))) {
// turn into an array
to = [ to ];
}
if (to.length === 0) {
return next(DENY, "No recipients for email");
}
// Set RCPT TO's, and parse each if it's not an Address object.
for (var i=0,l=to.length; i < l; i++) {
if (!(to[i] instanceof Address)) {
try {
to[i] = new Address(to[i]);
}
catch (err) {
return next(DENY, "Malformed to address (" + to[i] + "): " + err);
}
}
}
transaction.rcpt_to = to;
// Set data_lines to lines in contents
var match;
var re = /^([^\n]*\n?)/;
while (match = re.exec(contents)) {
var line = match[1];
line = line.replace(/\n?$/, '\r\n'); // make sure it ends in \r\n
transaction.add_data(line);
contents = contents.substr(match[1].length);
if (contents.length === 0) {
break;
}
}
transaction.message_stream.add_line_end();
this.send_trans_email(transaction, next);
}
exports.send_trans_email = function (transaction, next) {
var self = this;
// add in potentially missing headers
if (!transaction.header.get_all('Message-Id').length) {
this.loginfo("Adding missing Message-Id header");
transaction.add_header('Message-Id', '<' + transaction.uuid + '@' + config.get('me') + '>');
}
if (!transaction.header.get_all('Date').length) {
this.loginfo("Adding missing Date header");
transaction.add_header('Date', date_to_str(new Date()));
}
transaction.add_leading_header('Received', '(Haraka outbound); ' + date_to_str(new Date()));
// First get each domain
var recips = {};
var num_domains = 0;
transaction.rcpt_to.forEach(function (item) {
var domain = item.host;
if (!recips[domain]) {
recips[domain] = [];
num_domains++;
}
recips[domain].push(item);
});
var hmails = [];
var ok_paths = [];
async.forEachSeries(Object.keys(recips), function (domain, cb) {
var todo = new TODOItem(domain, recips[domain], transaction);
self.process_domain(ok_paths, todo, hmails, cb);
},
function (err) {
if (err) {
for (var i=0,l=ok_paths.length; i<l; i++) {
fs.unlink(ok_paths[i], function () {});
}
if (next) next(DENY, err);
return;
}
for (var i = 0; i < hmails.length; i++) {
var hmail = hmails[i];
delivery_queue.push(hmail);
}
if (next) next(OK, "Message Queued (" + transaction.uuid + ")");
})
}
exports.process_domain = function (ok_paths, todo, hmails, cb) {
var self = this;
this.loginfo("Processing domain: " + todo.domain);
var fname = _fname();
var tmp_path = path.join(queue_dir, '.' + fname);
var ws = new FsyncWriteStream(tmp_path, { flags: WRITE_EXCL });
// var ws = fs.createWriteStream(tmp_path, { flags: WRITE_EXCL });
ws.on('close', function () {
var dest_path = path.join(queue_dir, fname);
fs.rename(tmp_path, dest_path, function (err) {
if (err) {
self.logerror("Unable to rename tmp file!: " + err);
fs.unlink(tmp_path, function () {});
cb("Queue error");
}
else {
hmails.push(new HMailItem (fname, dest_path, todo.notes));
ok_paths.push(dest_path);
cb();
}
});
});
ws.on('error', function (err) {
self.logerror("Unable to write queue file (" + fname + "): " + err);
ws.destroy();
fs.unlink(tmp_path, function () {});
cb("Queueing failed");
});
self.build_todo(todo, ws, function () {
todo.message_stream.pipe(ws, { line_endings: '\r\n', dot_stuffing: true, ending_dot: false });
});
}
exports.build_todo = function (todo, ws, write_more) {
// Replacer function to exclude items from the queue file header
function exclude_from_json(key, value) {
switch (key) {
case 'message_stream':
return undefined;
default:
return value;
}
}
var todo_str = new Buffer(JSON.stringify(todo, exclude_from_json));
// since JS has no pack() we have to manually write the bytes of a long
var todo_length = new Buffer(4);
var todo_l = todo_str.length;
todo_length[3] = todo_l & 0xff;
todo_length[2] = (todo_l >> 8) & 0xff;
todo_length[1] = (todo_l >> 16) & 0xff;
todo_length[0] = (todo_l >> 24) & 0xff;
var buf = Buffer.concat([todo_length, todo_str], todo_str.length + 4);
var continue_writing = ws.write(buf);
if (continue_writing) return write_more();
ws.once('drain', write_more);
}
exports.split_to_new_recipients = function (hmail, recipients, response, cb) {
var self = this;
if (recipients.length === hmail.todo.rcpt_to.length) {
// Split to new for no reason - increase refcount and return self
hmail.refcount++;
return cb(hmail);
}
var fname = _fname();
var tmp_path = path.join(queue_dir, '.' + fname);
var ws = new FsyncWriteStream(tmp_path, { flags: WRITE_EXCL });
// var ws = fs.createWriteStream(tmp_path, { flags: WRITE_EXCL });
var err_handler = function (err, location) {
self.logerror("Error while splitting to new recipients (" + location + "): " + err);
hmail.bounce("Error splitting to new recipients", err);
}
ws.on('error', function (err) { err_handler(err, "tmp file writer") });
var writing = false;
var write_more = function () {
if (writing) return;
writing = true;
var rs = hmail.data_stream();
rs.pipe(ws, {end: false});
rs.on('error', function (err) {
err_handler(err, "hmail.data_stream reader");
})
rs.on('end', function () {
ws.on('close', function () {
var dest_path = path.join(queue_dir, fname);
fs.rename(tmp_path, dest_path, function (err) {
if (err) {
err_handler(err, "tmp file rename");
}
else {
var split_mail = new HMailItem (fname, dest_path);
split_mail.once('ready', function () {
cb(split_mail);
});
}
});
});
ws.destroySoon();
return;
});
}
ws.on('error', function (err) {
self.logerror("Unable to write queue file (" + fname + "): " + err);
ws.destroy();
hmail.bounce("Error re-queueing some recipients", err);
});
var new_todo = JSON.parse(JSON.stringify(hmail.todo));
new_todo.rcpt_to = recipients;
new_todo.uuid = utils.uuid();
self.build_todo(new_todo, ws, write_more);
}
// TODOItem - queue file header data
function TODOItem (domain, recipients, transaction) {
this.queue_time = Date.now();
this.domain = domain;
this.rcpt_to = recipients;
this.mail_from = transaction.mail_from;
this.message_stream = transaction.message_stream;
this.notes = transaction.notes;
this.uuid = transaction.uuid;
return this;
}
/////////////////////////////////////////////////////////////////////////////
// HMailItem - encapsulates an individual outbound mail item
var dummy_func = function () {}
function HMailItem (filename, path, notes) {
events.EventEmitter.call(this);
var matches = filename.match(fn_re);
if (!matches) {
throw new Error("Bad filename: " + filename);
}
this.path = path;
this.filename = filename;
this.next_process = matches[1];
this.num_failures = matches[2];
this.notes = notes || {};
this.refcount = 1;
this.todo = null;
this.file_size = 0;
this.next_cb = dummy_func;
this.bounce_error = null;
this.bounce_extra = null;
this.size_file();
}
util.inherits(HMailItem, events.EventEmitter);
exports.HMailItem = HMailItem;
// populate log functions - so we can use hooks
for (var key in logger) {
if (key.match(/^log\w/)) {
exports[key] = (function (key) {
return function () {
var args = ["[outbound] "];
for (var i=0, l=arguments.length; i<l; i++) {
args.push(arguments[i]);
}
logger[key].apply(logger, args);
}
})(key);
HMailItem.prototype[key] = (function (key) {
return function () {
var args = [ this ];
for (var i=0, l=arguments.length; i<l; i++) {
args.push(arguments[i]);
}
logger[key].apply(logger, args);
}
})(key);
}
}
HMailItem.prototype.data_stream = function () {
return fs.createReadStream(this.path, {start: this.data_start, end: this.file_size});
}
HMailItem.prototype.size_file = function () {
var self = this;
fs.stat(self.path, function (err, stats) {
if (err) {
// we are fucked... guess I need somewhere for this to go
self.logerror("Error obtaining file size: " + err);
self.temp_fail("Error obtaining file size");
}
else {
self.file_size = stats.size;
self.read_todo();
}
});
}
HMailItem.prototype.read_todo = function () {
var self = this;
var tl_reader = fs.createReadStream(self.path, {start: 0, end: 3});
tl_reader.on('error', function (err) {
self.logerror("Error reading queue file: " + self.path + ": " + err);
return self.temp_fail("Error reading queue file");
});
tl_reader.on('data', function (buf) {
// I'm making the assumption here we won't ever read less than 4 bytes
// as no filesystem on the planet should be that dumb...
tl_reader.destroy();
var todo_len = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
var td_reader = fs.createReadStream(self.path, {encoding: 'utf8', start: 4, end: todo_len + 3});
self.data_start = todo_len + 4;
var todo = '';
td_reader.on('data', function (str) {
todo += str;
if (Buffer.byteLength(todo) === todo_len) {
// we read everything
self.todo = JSON.parse(todo);
self.emit('ready');
}
});
td_reader.on('end', function () {
if (Buffer.byteLength(todo) !== todo_len) {
self.logcrit("Didn't find right amount of data in todo!");
fs.rename(self.path, path.join(queue_dir, "error." + self.filename), function (err) {
if (err) {
self.logerror("Error creating error file after todo read failure (" + self.filename + "): " + err);
}
});
self.emit('error', "Didn't find right amount of data in todo!"); // Note nothing picks this up yet
}
})
});
}
HMailItem.prototype.send = function () {
if (config.get('outbound.disabled')) {
// try again in 1 second if delivery is disabled
this.logdebug("delivery disabled temporarily. Retrying in 1s.");
var hmail = this;
setTimeout(function () {hmail.send()}, 1000);
return;
}
if (!this.todo) {
var self = this;
this.once('ready', function () { self._send() });
}
else {
this._send();
}
}
HMailItem.prototype._send = function () {
plugins.run_hooks('send_email', this);
}
HMailItem.prototype.send_email_respond = function (retval, delay_seconds) {
if (retval === constants.delay) {
// Try again in 'delay' seconds.
this.logdebug("Delivery of this email delayed for " + delay_seconds + " seconds");
var hmail = this;
hmail.next_cb();
temp_fail_queue.add(delay_seconds * 1000, function () { delivery_queue.push(hmail) });
}
else {
this.logdebug("Sending mail: " + this.filename);
this.get_mx();
}
}
HMailItem.prototype.get_mx = function () {
var domain = this.todo.domain;
plugins.run_hooks('get_mx', this, domain);
}
HMailItem.prototype.get_mx_respond = function (retval, mx) {
switch(retval) {
case constants.ok:
var mx_list;
if (Array.isArray(mx)) {
mx_list = mx;
}
else if (typeof mx === "object") {
mx_list = [mx];
}
else {
// assume string
var matches = /^(.*?)(:(\d+))?$/.exec(mx);
if (!matches) {
throw("get_mx returned something that doesn't match hostname or hostname:port");
}
mx_list = [{priority: 0, exchange: matches[1], port: matches[3]}];
}
this.logdebug("Got an MX from Plugin: " + this.todo.domain + " => 0 " + mx);
return this.found_mx(null, mx_list);
case constants.deny:
this.logwarn("get_mx plugin returned DENY: " + mx);
return this.bounce("No MX for " + this.domain);
case constants.denysoft:
this.logwarn("get_mx plugin returned DENYSOFT: " + mx);
return this.temp_fail("Temporary MX lookup error for " + this.domain);
}
var hmail = this;
// if none of the above return codes, drop through to this...
exports.lookup_mx(this.todo.domain, function (err, mxs) {
hmail.found_mx(err, mxs);
});
}
exports.lookup_mx = function lookup_mx (domain, cb) {
var mxs = [];
// Possible DNS errors
// NODATA
// FORMERR
// BADRESP
// NOTFOUND
// BADNAME
// TIMEOUT
// CONNREFUSED
// NOMEM
// DESTRUCTION
// NOTIMP
// EREFUSED
// SERVFAIL
// default wrap_mx just returns our object with "priority" and "exchange" keys
var wrap_mx = function (a) { return a };
var process_dns = function (err, addresses) {
if (err) {
if (err.code === 'ENODATA') {
// Most likely this is a hostname with no MX record
// Drop through and we'll get the A record instead.
return 0;
}
cb(err);
}
else if (addresses && addresses.length) {
for (var i=0,l=addresses.length; i < l; i++) {
var mx = wrap_mx(addresses[i]);
// hmail.logdebug("Got an MX from DNS: " + hmail.todo.domain + " => " + mx.priority + " " + mx.exchange);
mxs.push(mx);
}
cb(null, mxs);
}
else {
// return zero if we need to keep trying next option
return 0;
}
return 1;
};
dns.resolveMx(domain, function(err, addresses) {
if (process_dns(err, addresses)) {
return;
}
// if MX lookup failed, we lookup an A record. To do that we change
// wrap_mx() to return same thing as resolveMx() does.
wrap_mx = function (a) { return {priority:0,exchange:a} };
dns.resolve(domain, function(err, addresses) {
if (process_dns(err, addresses)) {
return;
}
var err = new Error("Found nowhere to deliver to");
err.code = 'NOMX';
cb(err);
});
});
}
HMailItem.prototype.found_mx = function (err, mxs) {
if (err) {
this.logerror("MX Lookup for " + this.todo.domain + " failed: " + err);
if (err.code === dns.NXDOMAIN || err.code === 'ENOTFOUND') {
this.bounce("No Such Domain: " + this.todo.domain);
}
else if (err.code === 'NOMX') {
this.bounce("Nowhere to deliver mail to for domain: " + this.todo.domain);
}
else {
// every other error is transient
this.temp_fail("DNS lookup failure: " + err);
}
}
else {
// got MXs
var mxlist = sort_mx(mxs);
this.mxlist = mxlist;
this.try_deliver();
}
}
// MXs must be sorted by priority order, but matched priorities must be
// randomly shuffled in that list, so this is a bit complex.
function sort_mx (mx_list) {
var sorted = mx_list.sort(function (a,b) {
return a.priority - b.priority;
});
// This isn't a very good shuffle but it'll do for now.
for (var i=0,l=sorted.length-1; i<l; i++) {
if (sorted[i].priority === sorted[i+1].priority) {
if (Math.round(Math.random())) { // 0 or 1
var j = sorted[i];
sorted[i] = sorted[i+1];
sorted[i+1] = j;
}
}
}
return sorted;
}
HMailItem.prototype.try_deliver = function () {
var self = this;
// check if there are any MXs left
if (this.mxlist.length === 0) {
return this.temp_fail("Tried all MXs");
}
var mx = this.mxlist.shift();
var host = mx.exchange;
// IP or IP:port
if (net.isIP(host)) {
self.hostlist = [ host ];
return self.try_deliver_host(mx);
}
this.loginfo("Looking up A records for: " + host);
// now we have a host, we have to lookup the addresses for that host
// and try each one in order they appear
dns.resolve(host, function (err, addresses) {
if (err) {
self.logerror("DNS lookup of " + host + " failed: " + err);
return self.try_deliver(); // try next MX
}
if (addresses.length === 0) {
// NODATA or empty host list
self.logerror("DNS lookup of " + host + " resulted in no data");
return self.try_deliver(); // try next MX
}
self.hostlist = addresses;
self.try_deliver_host(mx);
});
}
var smtp_regexp = /^(\d{3})([ -])(?:(\d\.\d\.\d)\s)?(.*)/;
function map_recips(item) {
return Object.keys(item)[0];
}
HMailItem.prototype.try_deliver_host = function (mx) {
if (this.hostlist.length === 0) {
return this.try_deliver(); // try next MX
}
var host = this.hostlist.shift();
var port = mx.port || 25;
var socket = sock.connect({port: port, host: host, localAddress: mx.bind});
var self = this;
var processing_mail = true;
this.loginfo("Attempting to deliver to: " + host + ":" + port + " (" + delivery_queue.length() + ") (" + temp_fail_queue.length() + ")");
socket.on('error', function (err) {
if (processing_mail) {
self.logerror("Ongoing connection failed to " + host + ":" + port + " : " + err);
processing_mail = false;
// try the next MX
self.try_deliver_host(mx);
}
});
socket.on('close', function () {
if (processing_mail) {
return self.try_deliver_host(mx);
}
});
socket.setTimeout(300 * 1000); // TODO: make this configurable
var command = 'connect';
var response = [];
var recipients = this.todo.rcpt_to.map(function (a) { return new Address (a.original) });
var mail_from = new Address (this.todo.mail_from.original);
var data_marker = 0;
var last_recip = null;
var ok_recips = 0;
var fail_recips = [];
var bounce_recips = [];
var secured = false;
var smtp_properties = {
"tls": false,
"max_size": 0,
"eightbitmime": false,
"enh_status_codes": false,
};
socket.send_command = function (cmd, data) {
if (!this.writable) {
self.logerror("Socket writability went away");
return self.try_deliver_host(mx);
}
var line = cmd + (data ? (' ' + data) : '');
if (cmd === 'dot') {
line = '.';
}
self.logprotocol("C: " + line);
this.write(line + "\r\n");
command = cmd.toLowerCase();
response = [];
};
socket.process_ehlo_data = function () {
for (var i=0,l=response.length; i < l; i++) {
var r = response[i];
if (r.toUpperCase() === '8BITMIME') {
smtp_properties.eightbitmime = true;
}
else if (r.toUpperCase() === 'STARTTLS') {
smtp_properties.tls = true;
}
else if (r.toUpperCase() === 'ENHANCEDSTATUSCODES') {
smtp_properties.enh_status_codes = true;
}
else {
var matches;
matches = r.match(/^SIZE\s+(\d+)$/);
if (matches) {
smtp_properties.max_size = matches[1];
}
}
}
if (smtp_properties.tls && config.get('outbound.enable_tls') && !secured) {
this.on('secure', function () {
// Set this flag so we don't try STARTTLS again if it
// is incorrectly offered at EHLO once we are secured.
secured = true;
socket.send_command('EHLO', config.get('me'));
});
this.send_command('STARTTLS');
}
else {
this.send_command('MAIL', 'FROM:' + mail_from);
}
}
socket.on('timeout', function () {
self.logerror("Outbound connection timed out to " + host + ":" + port);
processing_mail = false;
socket.end();
self.try_deliver_host(mx);
});
socket.on('connect', function () {
});
socket.on('line', function (line) {
var matches;
if (!processing_mail) {
self.logprotocol("Received data after stopping processing: " + line);