-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinkit.js
1339 lines (1234 loc) · 37.3 KB
/
binkit.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
// $Id: binkit.js,v 2.37 2020/03/25 01:27:35 rswindell Exp $
// vi: tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab
/*
* Intentionally simple "Advanced BinkleyTerm Style Outbound"
* mailer.
*
* Known limitations:
* 1) Does NOT support upper-case filenames in derived files/paths.
* This means that all .flo files must be in lower-case, all hex
* filename/path components must be in lower-case, and the ".pnt"
* directory extension must be in lower case.
* 2) Will not check in a zone-specified directory for the default
* zone. That is, if the default zone is zone 1, and the outbound
* is "/path/to/outbound", it will not correctly handle the case
* where there is a "/path/to/outbound.001" directory.
*
* See FTS-5005 for details.
*/
load('fido.js');
load('fidocfg.js');
load('binkp.js');
load('freqit_common.js');
var REVISION = "$Revision: 2.37 $".split(' ')[1];
var version_notice = "BinkIT/" + REVISION;
var semaphores = [];
// data/binkstats.ini
var stats = { inbound: { true: {}, false: {} }, callout: { true: {}, false: {} }, totals: {} };
function update_stats(stats, addr, bp, host)
{
stats[addr] = {};
if(bp.remote_operator)
stats[addr].oper = bp.remote_operator;
if(bp.remote_addrs)
stats[addr].AKAs = bp.remote_addrs;
if(bp.remote_capabilities)
stats[addr].caps = bp.remote_capabilities;
if(bp.remote_ver)
stats[addr].vers = bp.remote_ver;
if(bp.connect_host)
stats[addr].host = bp.connect_host;
else if(host)
stats[addr].host = host;
if(bp.connect_port)
stats[addr].port = bp.connect_port;
if(bp.connect_error !== undefined)
stats[addr].connect_error = bp.connect_error;
for(var i in bp.remote_info)
stats[addr]['info.' + i.toLowerCase()] = bp.remote_info[i];
stats[addr].localtime = new Date();
if(bp.sent_files.length)
stats[addr].sent_files = bp.sent_files;
if(bp.failed_sent_files.length)
stats[addr].failed_sent_files = bp.failed_sent_files;
if(bp.received_files.length)
stats[addr].received_files = bp.received_files;
if(bp.failed_received_files.length)
stats[addr].failed_received_files = bp.failed_received_files;
}
function update_totals(stats, addr, bp, callout, success)
{
if(!stats[addr])
stats[addr] = {};
var counter = format("%s_%s", success ? "successful" : "failed", callout ? "callouts":"inbounds");
if(!stats[addr][counter])
stats[addr][counter] = 0;
stats[addr][counter]++;
if(!stats[addr].sent_files)
stats[addr].sent_files = 0;
stats[addr].sent_files += bp.sent_files.length;
if(!stats[addr].received_files)
stats[addr].received_files = 0;
stats[addr].received_files += bp.received_files.length;
if(!stats[addr].failed_sent_files)
stats[addr].failed_sent_files = 0;
stats[addr].failed_sent_files += bp.failed_sent_files.length;
if(!stats[addr].failed_received_files)
stats[addr].failed_received_files = 0;
stats[addr].failed_received_files += bp.failed_received_files.length;
}
FREQIT.add_file = function(filename, bp, cfg)
{
if (filename === undefined)
return;
if (FREQIT.files >= cfg.maxfiles)
return;
if (FREQIT.added[filename] !== undefined)
return;
bp.addFile(filename);
FREQIT.files++;
FREQIT.added[filename]='';
};
function lock_flow(file)
{
var ret = {
bsy:new File(file.replace(/\.[^\.]*?$/, '.bsy')),
};
// Takes ownership of a lockfile if it's more than six hours old.
/*
* Race-safe version...
* 1) If date is "too old", pick a random time in the future.
* 2) Set the file date to that random time.
* 3) Wait one second (there's still an unlikely race here)
* 4) If the file date is the random time you chose, set the date
* to now and take "ownership" of the file.
*/
function take_lockfile(f)
{
var orig_date;
var future;
var remain;
var now = time();
if (!f.exists)
return false;
// TODO: This is hacked for a signed 32-bit time_t... watch out in 2038!
orig_date = f.date;
if (orig_date > (now - 60*60*6)) {
log(LOG_INFO, format("Lock is too recent to override now (%ld) > six hours ago (%ld)", orig_date, now-60*60*6));
return false;
}
remain = 0x80000000 - now;
future = now + random(remain);
f.date = future;
mswait(1000);
if (f.date != future) {
log(LOG_WARNING, format("Failed to set date in the future fdate (%ld) != future (%ld)", f.date, future));
return false;
}
if (!f.open("wb")) {
f.date = orig_date;
log(LOG_WARNING, "Error " + f.error + " opening " + f.name);
return false;
}
f.date = now;
return true;
}
log(LOG_DEBUG, "Locking "+ret.bsy.name);
if(!mkpath(ret.bsy.name.slice(0, -file_getname(ret.bsy.name).length)))
log(LOG_WARNING, "MKPATH ERROR " + errno + " (" + errno_str + "): " + ret.bsy.name);
if (!ret.bsy.open("wb")) { // Used to include 'e' mode flag (which never worked)
log(LOG_WARNING, "Error " + ret.bsy.error + " creating " + ret.bsy.name);
if (!take_lockfile(ret.bsy)) {
log(LOG_NOTICE, "Lock on "+ret.bsy.name+" failed.");
return undefined;
}
}
ret.bsy.writeln(version_notice + " args: " + argv.join(' '));
ret.bsy.flush();
log(LOG_DEBUG, "Lock successful.");
return ret;
}
function unlock_flow(locks)
{
log(LOG_DEBUG, "Unlocking "+locks.bsy.name+".");
if (locks.bsy !== undefined) {
locks.bsy.close();
locks.bsy.remove();
}
}
function outbound_root(addr, scfg)
{
if (FIDO.FTNDomains.outboundMap[addr.domain] === undefined)
return fullpath(scfg.outbound.replace(/[\\\/]$/, ''));
return fullpath(FIDO.FTNDomains.outboundMap[addr.domain]);
}
/*
* Given a list of addresses to rescan, calls
* bp.addFile() for any pending file transfers.
*
* TODO: Call this after sending a M_EOB to rescan per FSP-1024? We
* hold the lock files, so nothing should be changing the flow
* files (per FTS-5005) though. This is mostly for REQ handling,
* so if we do integrate freqit.js, we should be fine to ignore
* the spec on this point.
*/
function add_outbound_files(addrs, bp)
{
function has_lock(addr) {
var bsy = outbound_root(addr, bp.cb_data.binkit_scfg)+addr.flo_outbound(bp.default_zone, bp.default_domain)+'bsy';
var i;
for (i=0; i<bp.cb_data.binkit_locks.length; i++) {
if (bp.cb_data.binkit_locks[i].bsy.name === bsy)
return true;
}
return false;
}
addrs.forEach(function(addr) {
var lock_files;
log(LOG_DEBUG, "Adding outbound files for "+addr);
// Find all possible flow files for the remote.
var allfiles = directory(outbound_root(addr, bp.cb_data.binkit_scfg)+addr.flo_outbound(bp.default_zone, bp.default_domain)+'*');
// Parse flow files and call addFile() tracking what to do on success.
if (allfiles.length > 0) {
if (!has_lock(addr)) {
lock_files = lock_flow(outbound_root(addr, bp.cb_data.binkit_scfg)+addr.flo_outbound(bp.default_zone, bp.default_domain));
if (lock_files === undefined)
return;
bp.cb_data.binkit_locks.push(lock_files);
}
allfiles.forEach(function(file) {
var flo;
var line;
var action;
var i;
var fnchars = '0123456789abcdefghijklmnopqrstuvwxyz';
var fname;
if(file_isdir(file))
return;
var ext = file_getext(file);
if (ext !== undefined)
ext = ext.toLowerCase();
switch(ext) {
case '.clo':
case '.dlo':
case '.flo':
case '.hlo':
case '.ilo':
flo = new File(file);
if (!flo.open("r")) {
log(LOG_ERROR, "Unable to open FLO file '"+flo.name+"'.");
break;
}
if (bp.cb_data.binkit_flow_contents[flo.name] === undefined)
bp.cb_data.binkit_flow_contents[flo.name] = [];
while((line = flo.readln(2048))) {
switch(line.charAt(0)) {
case '#':
if (bp.addFile(line.substr(1)))
bp.cb_data.binkit_file_actions[line.substr(1)] = 'TRUNCATE';
bp.cb_data.binkit_flow_contents[flo.name].push(line.substr(1));
break;
case '^':
case '-':
if (bp.addFile(line.substr(1)))
bp.cb_data.binkit_file_actions[line.substr(1)] = 'DELETE';
bp.cb_data.binkit_flow_contents[flo.name].push(line.substr(1));
break;
case '~':
case '!':
break;
case '@':
bp.addFile(line.substr(1));
bp.cb_data.binkit_flow_contents[flo.name].push(line.substr(1));
break;
default:
bp.addFile(line);
bp.cb_data.binkit_flow_contents[flo.name].push(line);
break;
}
}
flo.close();
break;
case '.cut':
case '.dut':
case '.hut':
case '.iut':
case '.out':
fname = '';
for (i=0; i<8; i++)
fname += fnchars[random(fnchars.length)];
fname += '.pkt';
if (bp.addFile(file, fname))
bp.cb_data.binkit_file_actions[file] = 'DELETE';
break;
case '.req':
fname = '';
for (i=0; i<8; i++)
fname += fnchars[random(fnchars.length)];
fname += '.req';
if (bp.addFile(file, fname))
bp.cb_data.binkit_file_actions[file] = 'DELETE';
break;
case '.bsy':
break;
default:
log(LOG_WARNING, "Unsupported flow file type '"+file+"'.");
break;
}
});
}
if(bp.cb_data.binkitcfg.node[addr] === undefined
|| !bp.cb_data.binkitcfg.node[addr].outbox)
return;
var boxfiles = directory(backslash(bp.cb_data.binkitcfg.node[addr].outbox) + '*');
for(var f in boxfiles) {
var fname = boxfiles[f];
log("outbox file: " + fname);
if (bp.addFile(fname))
bp.cb_data.binkit_file_actions[fname] = 'DELETE';
}
});
}
function callout_auth_cb(mode, bp)
{
/*
* Loop through remote addresses, building a list of the ones with
* the same password (if we used an empty password, no other nodes
* are allowed)
*/
var addrs = [];
if (!bp.cb_data.binkitpw || bp.cb_data.binkitpw === '-')
addrs.push(bp.cb_data.binkit_to_addr);
else {
bp.remote_addrs.forEach(function(addr) {
if (bp.cb_data.binkitcfg.node[addr] !== undefined) {
if (bp.cb_data.binkitcfg.node[addr].pass === bp.cb_data.binkitpw)
addrs.push(addr);
}
else
log(LOG_DEBUG, "Unconfigured address "+addr);
});
}
add_outbound_files(addrs, bp);
}
function remove_file(fname)
{
if (file_remove(fname))
log(LOG_INFO, "Deleted file: " + fname);
else
log(LOG_ERROR, "Unable to delete file: " + fname);
}
/*
* Delete completed flo files.
*/
function tx_callback(fname, bp)
{
var j;
// Remove flow files that have been completely processed.
Object.keys(bp.cb_data.binkit_flow_contents).forEach(function(flo) {
if (file_exists(flo)) {
while ((j = bp.cb_data.binkit_flow_contents[flo].indexOf(fname)) !== -1)
bp.cb_data.binkit_flow_contents[flo].splice(j, 1);
if (bp.cb_data.binkit_flow_contents[flo].length == 0)
remove_file(flo);
}
});
}
function handle_freq(reqfname, bp)
{
var req=new File(reqfname);
var m;
var fname;
var pw;
var cfg = new FREQITCfg();
if (!req.open("r"))
return;
FREQIT.reset();
next_file:
while((fname=req.readln())) {
if ((m=fname.match(/^(.*) !(.*?)$/))!==null) {
pw=m[2];
fname=m[1];
}
// First, check for magic!
for (m in cfg.magic) {
if (m == fname.toLowerCase()) {
FREQIT.handle_magic(cfg.magic[m], bp, bp.authenticated === 'secure', pw, cfg);
continue next_file;
}
}
// Now, check for the file...
FREQIT.handle_regular(fname, bp, bp.authenticated === 'secure', pw, cfg);
}
}
function rename_or_move(src, dst_dir, dst_fname)
{
var sf;
var df;
var buf;
var remain;
if (!mkpath(dst_dir)) {
log(LOG_ERR, "Error " + errno + " making directory: " + dst_dir);
return false;
}
var dst = dst_dir + dst_fname;
if (file_rename(src, dst))
return true;
sf = new File(src);
if (!sf.open("rb")) {
log(LOG_ERR, "Error " + sf.error + " opening " + sf.name);
return false;
}
df = new File(dst);
if (!df.open("wb")) { // Used to include 'e' mode flag (which never worked)
log(LOG_ERR, "Error " + df.error + " opening " + df.name);
sf.close();
return false;
}
while (!sf.eof) {
// Read 2MB at a time...
remain = sf.length - sf.position;
if (remain === 0)
break;
if (remain > 0x200000)
remain = 0x200000;
buf = sf.read(remain);
if (!df.write(buf)) {
log(LOG_ERR, "Error " + df.error + " writing to " + df.name);
df.close();
df.remove();
sf.close();
return false;
}
}
df.close();
sf.close();
df.date = sf.date;
sf.remove();
return true;
}
function rx_callback(fname, bp)
{
var semname;
var secure_inbound = bp.cb_data.binkit_scfg.secure_inbound;
var inbound = bp.cb_data.binkit_scfg.inbound;
if (secure_inbound == undefined)
secure_inbound = inbound;
log(LOG_INFO, "Received file: " + fname + format(" (%1.1fKB)", file_size(fname) / 1024.0));
if (fname.search(/\.(?:pkt|su.|mo.|tu.|we.|th.|fr.|sa.)$/i) !== -1) {
semname = system.data_dir + 'fidoin.now';
if (semaphores.indexOf(semname) == -1)
semaphores.push(semname);
}
else if (fname.search(/\.tic$/i) !== -1) {
semname = system.data_dir + 'tickit.now';
if (semaphores.indexOf(semname) == -1)
semaphores.push(semname);
}
if (fname.search(/\.req$/i) !== -1) {
handle_freq(fname, bp);
remove_file(fname);
}
else {
if (bp.authenticated === 'secure') {
if (secure_inbound === undefined)
log(LOG_ERROR, "No secure inbound configured in sbbsecho! Leaving secure file as '"+fname+"'.");
else {
log(LOG_INFO, "Moving '"+fname+"' to '"+secure_inbound+file_getname(fname)+"'.");
if (!rename_or_move(fname, secure_inbound, file_getname(fname)))
return false;
}
}
else {
log(LOG_WARNING, "Non-secure session type: " + format("'%s'", bp.authenticated));
if (inbound === undefined)
log(LOG_ERROR, "No inbound configured in sbbsecho! Leaving insecure file as '"+fname+"'.");
else {
log(LOG_INFO, "Moving '"+fname+"' to '"+inbound+file_getname(fname)+"'.");
if (!rename_or_move(fname, inbound, file_getname(fname)))
return false;
}
}
}
return true;
}
function callout_want_callback(fobj, fsize, fdate, offset, bp)
{
/*
* TODO: Currently a copy/paste from binkp.js...
* Likely we'll want magical handling for control files (*.TIC,
* *.REQ, and *.PKT, Bundle Files, and the default handling for the
* rest.
*
* We should lower-case incoming filenames here too...
*
* Also, for partial files, we can cancel the remote and do an M_GET resume.
*
* Ok, so put active transfers into a temp directory somewhere with some way
* of tying it back to a set of address (we don't actually know which address it
* comes from, just a list of addresses it may come from) and store enough
* info to resume it. This means the partially transferred file and some sort
* of bookkeeping info file need to be tied together somewhere... maybe resume
* support is simply "too hard" to bother with?
*
* Once the file is completely received, move it to the final resting place
* (in rx_callback) and do any required processing then.
*/
// Reject duplicate filenames... a more robust callback would rename them.
// Or process the old ones first.
if (this.received_files.indexOf(fobj.name) != -1)
return this.file.REJECT;
// Reject or skip existing files.
if (file_exists(fobj.name)) {
log(LOG_WARNING, "Inbound file already exists: " + fobj.name);
// If the size and date are the same, reject it.
if (fsize == file_size(fobj.name) && fdate == file_date(fobj.name))
return this.file.REJECT;
// Otherwise, skip it.
return this.file.SKIP;
}
// Accept everything else
return this.file.ACCEPT;
}
function callout_done(bp)
{
var f;
var lines;
var semname;
bp.sent_files.forEach(function(file) {
if (bp.cb_data.binkit_file_actions[file] !== undefined) {
switch(bp.cb_data.binkit_file_actions[file]) {
case 'TRUNCATE':
f = new File(file);
if (f.truncate())
log(LOG_INFO, "Truncated '"+f.name+"'.");
else
log(LOG_ERROR, "Unable to truncate '"+f.name+"'.");
break;
case 'DELETE':
remove_file(file);
break;
}
}
});
Object.keys(bp.cb_data.binkit_flow_contents).forEach(function(key) {
if (bp.cb_data.binkit_flow_contents[key].length > 0) {
// We have some unsent files in here... re-write the flo file...
f = new File(key);
if (!f.open("r+")) {
log(LOG_ERROR, "Unable to update flow file '"+key+"'.");
return;
}
lines = f.readAll(2048);
f.truncate(0);
lines.forEach(function(line) {
switch(line[0]) {
case '#':
case '^':
case '-':
case '@':
if (bp.cb_data.binkit_flow_contents[key].indexOf(line.substr(1)) == -1)
f.writeln('~'+line.substr(1));
else
f.writeln(line);
break;
// Already skipped...
case '~':
case '!':
f.writeln(line);
break;
default:
if (bp.cb_data.binkit_flow_contents[key].indexOf(line) == -1)
f.writeln('~'+line);
else
f.writeln(line);
break;
}
});
}
});
// Remove flow files that have been completely processed.
Object.keys(bp.cb_data.binkit_flow_contents).forEach(function(flo) {
if (file_exists(flo)) {
if (bp.cb_data.binkit_flow_contents[flo].length == 0)
remove_file(flo);
}
});
}
function callout(addr, scfg, locks, bicfg)
{
var myaddr = FIDO.parse_addr(system.fido_addr_list[0], 1, 'fidonet');
var bp = new BinkP(version_notice, undefined, rx_callback, tx_callback);
var port;
var host;
var tls = false;
var f;
var success = false;
var src_addr;
log(LOG_INFO, format("%s callout to %s started", bp.revision, addr));
if (bicfg === undefined)
bicfg = new BinkITCfg();
bp.system_operator = bicfg.sysop;
bp.plain_auth_only = bicfg.plain_auth_only;
bp.crypt_support = bicfg.crypt_support;
bp.cb_data = {
binkitcfg:bicfg,
binkit_to_addr:addr,
binkit_scfg:scfg,
binkit_file_actions:{},
binkit_flow_contents:{},
binkit_locks:locks
};
if (bp.cb_data.binkitcfg.node[addr] !== undefined) {
bp.cb_data.binkitpw = bp.cb_data.binkitcfg.node[addr].pass;
port = bp.cb_data.binkitcfg.node[addr].port;
host = bp.cb_data.binkitcfg.node[addr].host;
tls = bp.cb_data.binkitcfg.node[addr].tls;
if (bp.plain_auth_only) {
bp.require_md5 = false;
bp.require_crypt = false;
} else {
bp.require_md5 = !(bp.cb_data.binkitcfg.node[addr].nomd5);
bp.require_crypt = bp.crypt_support && !(bp.cb_data.binkitcfg.node[addr].nocrypt);
bp.plain_auth_only = bp.cb_data.binkitcfg.node[addr].plain_auth_only;
}
}
// TODO: Force debug mode for now...
bp.debug = true;
bp.default_zone = myaddr.zone;
bp.default_domain = myaddr.domain;
bp.want_callback = callout_want_callback;
if (bp.cb_data.binkitcfg.caps !== undefined)
bp.capabilities = bp.cb_data.binkitcfg.caps;
/*
* We can't use the defaults since the defaults are only 4D addresses,
* and the address we're calling from needs to be first...
*/
bp.addr_list = [];
system.fido_addr_list.forEach(function(faddr){
bp.addr_list.push(FIDO.parse_addr(faddr, this.default_zone));
}, this);
/*
* Set src_addr to the node we want first in our list. If
* SourceAddress is set for this node, use that. Otherwise, use the
* address we are contacting.
*/
if (bp.cb_data.binkitcfg.node[addr] !== undefined && bp.cb_data.binkitcfg.node[addr].src !== undefined)
src_addr = FIDO.parse_addr(bp.cb_data.binkitcfg.node[addr].src, 1);
else
src_addr = addr;
// Sort by "distance" from src_addr address.
FIDO.distance_sort(bp.addr_list, src_addr);
log(LOG_DEBUG, format("connecting to %s at %s", addr, host));
// We won't add files until the auth finishes...
success = bp.connect(addr, bp.cb_data.binkitpw, callout_auth_cb, port, host, tls);
// Statistics
update_stats(stats.callout[success], addr, bp, host);
update_totals(stats.totals, addr, bp, true, success);
callout_done(bp);
}
function check_held(addr, scfg, myaddr)
{
var until;
var f = new File(outbound_root(addr, scfg)+addr.flo_outbound(myaddr.zone)+'.hld');
if (!f.exists)
return false;
if (!f.open("r")) {
log(LOG_ERROR, "Unable to open hold file '"+f.name+"'");
return true;
}
until = f.readln();
if (until.search(/^[0-9]+$/) !== 0) {
log(LOG_WARNING, "First line of '"+f.name+"' invalid ("+until+"). Should be a positive integer.");
return false;
}
f.close();
until = parseInt(until, 10);
if (until < time()) {
f.remove();
log(LOG_INFO, "Removed stale ("+system.timestr(until)+") hold file '"+f.name+"'.");
return false;
}
log(LOG_INFO, addr+" held until "+system.timestr(until)+".");
return true;
}
function run_one_outbound_dir(dir, scfg, ran)
{
var myaddr = FIDO.parse_addr(system.fido_addr_list[0], 1, 'fidonet');
var locks = [];
var addr;
var lock_files;
var ext;
var i;
var flow_files;
log(LOG_DEBUG, "Running outbound dir "+dir);
flow_files = directory(dir+'*.?ut').concat(directory(dir+'*.?lo'));
function flow_order(a, b) {
var flavour_order=['i','c','d','o','f','h'];
var type_order=['ut', 'lo'];
var aext = flavour_order.indexOf(a.substr(-3, 1).toLowerCase());
var bext = flavour_order.indexOf(b.substr(-3, 1).toLowerCase());
if (aext !== bext)
return aext - bext;
aext = type_order.indexOf(a.substr(-2, 2).toLowerCase());
bext = type_order.indexOf(b.substr(-2, 2).toLowerCase());
if (aext !== bext)
return aext - bext;
if (a < b)
return -1;
if (b < a)
return 1;
return 0;
}
while (!js.terminated) {
if (flow_files.length == 0)
break;
flow_files.sort(flow_order);
flow_file_loop:
for (i=0; i<flow_files.length; i++) {
try {
addr = FIDO.parse_flo_file_path(flow_files[i], myaddr.zone);
}
catch(addr_e) {
log(LOG_WARNING, addr_e+" when checking '"+flow_files[i]+"' (default zone: "+myaddr.zone+")");
continue;
}
if (ran[addr] !== undefined)
continue;
ext = file_getext(flow_files[i]);
if (ext === undefined) {
log(LOG_WARNING, "Unknown flow file flavour '"+flow_files[i]+"'.");
continue;
}
// Ensure this is the "right" outbound (file case, etc)
if (flow_files[i] !== outbound_root(addr, scfg)+addr.flo_outbound(myaddr.zone)+ext.substr(1)) {
log(LOG_WARNING, "Unexpected file path '"+flow_files[i]+"' expected '"+outbound_root(addr, scfg)+addr.flo_outbound(myaddr.zone)+ext.substr(1)+"' (skipped)");
continue;
}
switch(ext.substr(0, 2)) {
case '.h':
log(LOG_DEBUG, "Skipping hold flavoured flow file '"+flow_files[i]+"'.");
continue;
case '.c':
case '.d':
case '.i':
break;
case '.f':
if (ext === '.flo')
break;
log(LOG_WARNING, "Unknown flow file flavour '"+flow_files[i]+"'.");
continue;
case '.o':
if (ext === '.out')
break;
log(LOG_WARNING, "Unknown flow file flavour '"+flow_files[i]+"'.");
continue;
default:
log(LOG_WARNING, "Unknown flow file flavour '"+flow_files[i]+"'.");
continue;
}
if ((lock_files = lock_flow(flow_files[i]))!==undefined) {
if (check_held(addr, scfg, myaddr)) {
unlock_flow(lock_files);
continue;
}
break;
}
}
if (i<flow_files.length) {
log(LOG_INFO, "Attempting callout for " + addr + ", file: " + flow_files[i]);
locks.push(lock_files);
// Use a try/catch to ensure we clean up the lock files.
callout(addr, scfg, locks);
ran[addr] = true;
locks.forEach(unlock_flow);
}
else {
log(LOG_DEBUG, "No flow files to be processed.");
break;
}
}
log(LOG_DEBUG, "Done checking in "+dir+".");
}
function run_one_outbox_dir(addr, scfg, bicfg, ran)
{
var dir = bicfg.node[addr].outbox;
if(!dir)
return;
dir = backslash(dir);
log(LOG_DEBUG, "Running outbox dir for (" + addr + "): " + dir);
var files = directory(dir + '*');
if(files.length) {
log(LOG_INFO, "Attempting callout for " + addr + ", outbox files: " + files);
var locks = [];
callout(addr, scfg, locks, bicfg);
ran[addr] = true;
locks.forEach(unlock_flow);
}
}
function touch_semaphores()
{
semaphores.forEach(function(semname) {
log(LOG_DEBUG, "Touching semaphore file: " + semname);
file_touch(semname);
});
}
function run_outbound(ran)
{
var bicfg;
var scfg;
var outbound_dirs=[];
var outbound_roots=[];
var scfg_ob;
log(LOG_DEBUG, "Running outbound");
scfg = new SBBSEchoCfg();
bicfg = new BinkITCfg();
if (!scfg.is_flo) {
log(LOG_ERROR, "sbbsecho not configured for FLO-style mailers.");
return false;
}
Object.keys(FIDO.FTNDomains.outboundMap).forEach(function(key) {
outbound_roots.push(FIDO.FTNDomains.outboundMap[key]);
});
scfg_ob = scfg.outbound.replace(/[\\\/]$/, '');
if (outbound_roots.indexOf(scfg_ob) == -1)
outbound_roots.push(scfg_ob);
log(LOG_DEBUG, "Outbound roots: " + JSON.stringify(outbound_roots, null, 0));
outbound_roots.forEach(function(oroot) {
var dirs;
function addDir(dir) {
var bdir = backslash(dir);
bdir = fullpath(bdir);
if (outbound_dirs.indexOf(bdir) == -1) outbound_dirs.push(bdir);
}
function addPoints(dir) {
var pnts = directory(backslash(dir) + '*.pnt', false);
pnts.forEach(function (pdir) {
if (pdir.search(/[\\\/][0-9a-z]{8}.pnt$/) >= 0 && file_isdir(pdir)) {
addDir(pdir);
} else {
log(LOG_WARNING, "Unhandled/Unexpected point path '"+pdir+"'.");
}
});
}
if (file_isdir(oroot)) {
addDir(oroot);
addPoints(oroot);
} else {
log(LOG_NOTICE, "Skipping non-existent outbound directory: " + oroot);
return;
}
dirs = directory(oroot+'.*', 0);
dirs.forEach(function(dir) {
var ext = file_getext(dir);
if (ext === undefined)
return;
if (ext.search(/^\.[0-9a-f]+$/) == 0) {
if (file_isdir(dir)) {
addDir(dir);
addPoints(dir);
} else {
log(LOG_WARNING, "Unexpected file in outbound '"+dir+"'.");
}
} else {
log(LOG_WARNING, "Unhandled outbound '"+dir+"'.");
}
});
});
log(LOG_DEBUG, "Outbound dirs: " + JSON.stringify(outbound_dirs, null, 0));
outbound_dirs.forEach(function(dir) {
run_one_outbound_dir(dir, scfg, ran);
});
for(var addr in bicfg.node) {
run_one_outbox_dir(addr, scfg, bicfg, ran);
}
}
/*
* MysticBBS v1.12A39 at least has an issue when the CRYPT
* option is included after the CRAM-MD5 challenge. It appends
* three NULs to the end of the challenge data. If the remote told
* us it was Mystic, see if that matches.
*/
function mystic_broken_cram(bp)
{
var dot;
var min;
var ver;
if (bp.remote_ver === undefined)
return false;
if (bp.remote_ver.substr(0, 7) !== 'Mystic/')
return false;
if (bp.wont_crypt)
return false;
/*
* TODO: This is in case Mystic/1.12A39 has both a working and
* non-working build. Hopefully, this is not the case, and this
* block can be removed.
*/
if (bp.remote_ver === 'Mystic/1.12A39')
return false;
ver = bp.remote_ver.substr(7);
for (dot = 0; dot < ver.length; dot++) {
if (ver[dot] == '.')
break;
}
if (parseInt(ver.substr(0, dot), 10) < 1)
return true;
if (parseInt(ver.substr(0, dot), 10) > 1)
return false;
for (min = dot + 1; min < ver.length; min++) {
if (ver[min] < '0' || ver[min] > '9')
break;
}
if (parseInt(ver.substr(dot+1, min-1), 10) < 12)
return true;
if (parseInt(ver.substr(dot+1, min-1), 10) > 12)
return false;
if (min > ver.length)
return false;
if (ver[min] != 'A')
return false;
if (parseInt(ver.substr(min+1), 10) <= 39)
return true;
return false;
}
function inbound_auth_cb(pwd, bp)
{
/*
* Loop through remote addresses, building a list of the ones with
* the same password that we can send mail for.
*/
var addrs = [];
var ret = '-';
var nocrypt;
var invalid=false;
function check_nocrypt(node) {
if (node.nocrypt) {
if (nocrypt === undefined)
nocrypt = true;
}
else {
nocrypt = false;
}
}
log(LOG_INFO, "Remote addresses: " + bp.remote_addrs.join(' '));
bp.remote_addrs.forEach(function(addr) {
var cpw;
if (bp.cb_data.binkitcfg.node[addr] !== undefined) {
log(LOG_INFO, "Inbound session for: " + addr);
cpw = bp.cb_data.binkitcfg.node[addr].pass;
if (!cpw)
cpw = '-';
if (pwd[0].substr(0, 9) === 'CRAM-MD5-') {
if (mystic_broken_cram(bp))
bp.cram.challenge += '\x00\x00\x00';
var expected = bp.getCRAM('MD5', cpw);
if (expected === pwd[0]) {