-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMAILSvr.cpp
1562 lines (1265 loc) · 47.8 KB
/
SMAILSvr.cpp
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
/*
* XMail by Davide Libenzi (Intranet and Internet mail server)
* Copyright (C) 1999,..,2010 Davide Libenzi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Davide Libenzi <[email protected]>
*
*/
#include "SysInclude.h"
#include "SysDep.h"
#include "SvrDefines.h"
#include "SList.h"
#include "ShBlocks.h"
#include "StrUtils.h"
#include "BuffSock.h"
#include "MiscUtils.h"
#include "ResLocks.h"
#include "BuffSock.h"
#include "SvrUtils.h"
#include "UsrUtils.h"
#include "MessQueue.h"
#include "SMAILUtils.h"
#include "QueueUtils.h"
#include "MailDomains.h"
#include "AliasDomain.h"
#include "SMTPUtils.h"
#include "ExtAliases.h"
#include "UsrMailList.h"
#include "Filter.h"
#include "DNS.h"
#include "MailConfig.h"
#include "SMAILSvr.h"
#include "AppDefines.h"
#include "MailSvr.h"
#define SMAIL_WAITMSG_TIMEOUT 2000
#define CUSTOM_PROC_LINE_MAX 1024
#define SMAIL_EXTERNAL_EXIT_BREAK 16
#define SMAIL_STOP_PROCESSING 3111965L
struct MacroSubstCtx {
SPLF_HANDLE hFSpool;
FileSection FSect;
};
static int SMAILThreadCountAdd(long lCount, SHB_HANDLE hShbSMAIL, SMAILConfig *pSMAILCfg = NULL);
static int SMAILLogEnabled(SHB_HANDLE hShbSMAIL, SMAILConfig *pSMAILCfg = NULL);
static SMAILConfig *SMAILGetConfigCopy(SHB_HANDLE hShbSMAIL)
{
SMAILConfig *pLMAILCfg = (SMAILConfig *) ShbLock(hShbSMAIL);
if (pLMAILCfg == NULL)
return NULL;
SMAILConfig *pNewLMAILCfg = (SMAILConfig *) SysAlloc(sizeof(SMAILConfig));
if (pNewLMAILCfg != NULL)
memcpy(pNewLMAILCfg, pLMAILCfg, sizeof(SMAILConfig));
ShbUnlock(hShbSMAIL);
return pNewLMAILCfg;
}
static int SMAILThreadCountAdd(long lCount, SHB_HANDLE hShbSMAIL, SMAILConfig *pSMAILCfg)
{
int iDoUnlock = 0;
if (pSMAILCfg == NULL) {
if ((pSMAILCfg = (SMAILConfig *) ShbLock(hShbSMAIL)) == NULL)
return ErrGetErrorCode();
++iDoUnlock;
}
pSMAILCfg->lThreadCount += lCount;
if (iDoUnlock)
ShbUnlock(hShbSMAIL);
return 0;
}
static int SMAILLogEnabled(SHB_HANDLE hShbSMAIL, SMAILConfig *pSMAILCfg)
{
int iDoUnlock = 0;
if (pSMAILCfg == NULL) {
if ((pSMAILCfg = (SMAILConfig *) ShbLock(hShbSMAIL)) == NULL)
return ErrGetErrorCode();
++iDoUnlock;
}
unsigned long ulFlags = pSMAILCfg->ulFlags;
if (iDoUnlock)
ShbUnlock(hShbSMAIL);
return (ulFlags & SMAILF_LOG_ENABLED) ? 1: 0;
}
static int SMAILHandleResendNotify(SVRCFG_HANDLE hSvrConfig, QUEUE_HANDLE hQueue,
QMSG_HANDLE hMessage, SPLF_HANDLE hFSpool)
{
/* Check if it's time to notify about a failed delivery attempt */
int iRetryCount = QueGetTryCount(hMessage);
char szNotifyPattern[128];
SvrConfigVar("NotifyTryPattern", szNotifyPattern, sizeof(szNotifyPattern) - 1,
hSvrConfig, "");
for (char *pszTry = szNotifyPattern; pszTry != NULL; ++pszTry) {
if (isdigit(*pszTry) && atoi(pszTry) == iRetryCount)
break;
if ((pszTry = strchr(pszTry, ',')) == NULL)
return 0;
}
/* Build the notification text and send the message */
time_t tLastTry = QueGetLastTryTime(hMessage);
time_t tNextTry = QueGetMessageNextOp(hQueue, hMessage);
char szTimeLast[128];
char szTimeNext[128];
MscGetTimeStr(szTimeLast, sizeof(szTimeLast) - 1, tLastTry);
MscGetTimeStr(szTimeNext, sizeof(szTimeNext) - 1, tNextTry);
char *pszText =
StrSprint("** This is a temporary error and you do not have to resend the message\r\n"
"** The system tried to send the message at : %s\r\n"
"** The current number of delivery attempts is : %d\r\n"
"** The system will try to resend the message at : %s\r\n",
szTimeLast, iRetryCount, szTimeNext);
if (pszText == NULL)
return ErrGetErrorCode();
int iNotifyResult = QueUtNotifyTempErrDelivery(hQueue, hMessage, hFSpool,
NULL, pszText, NULL);
SysFree(pszText);
return iNotifyResult;
}
static int SMAILMailingListExplode(UserInfo *pUI, SPLF_HANDLE hFSpool)
{
char const *const *ppszFrom = USmlGetMailFrom(hFSpool);
char const *const *ppszRcpt = USmlGetRcptTo(hFSpool);
char szDestUser[MAX_ADDR_NAME];
char szDestDomain[MAX_ADDR_NAME];
if (USmtpSplitEmailAddr(ppszRcpt[0], szDestUser, szDestDomain) < 0)
return ErrGetErrorCode();
/* Get Mailing List Sender address. If this account variable does not exist */
/* the sender will be the "real" message sender */
char *pszMLSender = UsrGetUserInfoVar(pUI, "ListSender");
/* Check if the use of the Reply-To: is requested */
int iUseReplyTo = UsrGetUserInfoVarInt(pUI, "UseReplyTo", 1);
USRML_HANDLE hUsersDB = UsrMLOpenDB(pUI);
if (hUsersDB == INVALID_USRML_HANDLE) {
ErrorPush();
SysFree(pszMLSender);
return ErrorPop();
}
/* Mailing list scan */
MLUserInfo *pMLUI = UsrMLGetFirstUser(hUsersDB);
for (; pMLUI != NULL; pMLUI = UsrMLGetNextUser(hUsersDB)) {
if (strchr(pMLUI->pszPerms, 'R') != NULL) {
/* Get message handle */
QMSG_HANDLE hMessage = QueCreateMessage(hSpoolQueue);
if (hMessage == INVALID_QMSG_HANDLE) {
ErrorPush();
SysFree(pszMLSender);
UsrMLFreeUser(pMLUI);
UsrMLCloseDB(hUsersDB);
return ErrorPop();
}
char szQueueFilePath[SYS_MAX_PATH];
QueGetFilePath(hSpoolQueue, hMessage, szQueueFilePath);
/* Create spool file. If "pszMLSender" is NULL the original sender is kept */
if (USmlCreateSpoolFile(hFSpool, pszMLSender, pMLUI->pszAddress,
szQueueFilePath, (iUseReplyTo != 0) ? "Reply-To" : "",
ppszRcpt[0], NULL) < 0) {
ErrorPush();
QueCleanupMessage(hSpoolQueue, hMessage);
QueCloseMessage(hSpoolQueue, hMessage);
SysFree(pszMLSender);
UsrMLFreeUser(pMLUI);
UsrMLCloseDB(hUsersDB);
return ErrorPop();
}
/* Transfer file to the spool */
if (QueCommitMessage(hSpoolQueue, hMessage) < 0) {
ErrorPush();
QueCleanupMessage(hSpoolQueue, hMessage);
QueCloseMessage(hSpoolQueue, hMessage);
SysFree(pszMLSender);
UsrMLFreeUser(pMLUI);
UsrMLCloseDB(hUsersDB);
return ErrorPop();
}
}
UsrMLFreeUser(pMLUI);
}
UsrMLCloseDB(hUsersDB);
SysFree(pszMLSender);
return 0;
}
static int SMAILRemoteMsgSMTPSend(SVRCFG_HANDLE hSvrConfig, SHB_HANDLE hShbSMAIL,
SPLF_HANDLE hFSpool, QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage,
char const *pszDestDomain, SMTPError *pSMTPE)
{
/* Apply filters ... */
if (FilFilterMessage(hFSpool, hQueue, hMessage, FILTER_MODE_OUTBOUND) < 0)
return ErrGetErrorCode();
int iError;
char const *pszSMTPDomain = USmlGetSMTPDomain(hFSpool);
char const *pszSmtpMessageID = USmlGetSmtpMessageID(hFSpool);
char const *pszSpoolFile = USmlGetSpoolFile(hFSpool);
char const *pszSpoolFilePath = USmlGetSpoolFilePath(hFSpool);
char const *pszMailFrom = USmlMailFrom(hFSpool);
char const *pszSendMailFrom = USmlSendMailFrom(hFSpool);
char const *pszRcptTo = USmlRcptTo(hFSpool);
char const *pszSendRcptTo = USmlSendRcptTo(hFSpool);
char const *pszRelayDomain = USmlGetRelayDomain(hFSpool);
FileSection FSect;
/* This function retrieve the spool file message section and sync the content. */
/* This is necessary before sending the file */
if (USmlGetMsgFileSection(hFSpool, FSect) < 0)
return ErrGetErrorCode();
/* Get HELO domain */
char szHeloDomain[MAX_HOST_NAME];
SvrConfigVar("HeloDomain", szHeloDomain, sizeof(szHeloDomain) - 1, hSvrConfig, "");
char const *pszHeloDomain = IsEmptyString(szHeloDomain) ? NULL: szHeloDomain;
/* If it's a relayed message use the associated relay */
if (pszRelayDomain != NULL) {
SysLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send CMX = \"%s\" SMTP = \"%s\" From = \"%s\" To = \"%s\"\n",
pszRelayDomain, pszSMTPDomain, pszMailFrom, pszRcptTo);
USmtpCleanupError(pSMTPE);
if (USmtpMailRmtDeliver(hSvrConfig, pszRelayDomain, pszHeloDomain, pszSendMailFrom,
pszSendRcptTo, &FSect, pSMTPE) < 0) {
ErrorPush();
char szSmtpError[512];
USmtpGetSMTPError(pSMTPE, szSmtpError, sizeof(szSmtpError));
ErrLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send CMX = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", pszRelayDomain, pszSMTPDomain, pszMailFrom,
pszRcptTo, SMTP_ERROR_VARNAME, szSmtpError,
SMTP_SERVER_VARNAME, USmtpGetErrorServer(pSMTPE));
QueUtErrLogMessage(hQueue, hMessage,
"SMAIL SMTP-Send CMX = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", pszRelayDomain, pszSMTPDomain,
pszMailFrom, pszRcptTo, SMTP_ERROR_VARNAME,
szSmtpError, SMTP_SERVER_VARNAME,
USmtpGetErrorServer(pSMTPE));
return ErrorPop();
}
if (SMAILLogEnabled(hShbSMAIL)) {
char szRmtMsgID[256];
USmtpGetSMTPRmtMsgID(USmtpGetErrorMessage(pSMTPE), szRmtMsgID,
sizeof(szRmtMsgID));
USmlLogMessage(hFSpool, "SMTP", szRmtMsgID, pszRelayDomain);
}
return 0;
}
/* Check the existance of direct SMTP forwarders */
SMTPGateway **ppszFwdGws = USmtpGetFwdGateways(hSvrConfig, pszDestDomain);
if (ppszFwdGws != NULL) {
/*
* By initializing this to zero makes XMail to discharge all mail
* for domains that have an empty forwarders list
*/
iError = 0;
for (int i = 0; ppszFwdGws[i] != NULL; i++) {
SysLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send FWD = \"%s\" SMTP = \"%s\" From = \"%s\" To = \"%s\"\n",
ppszFwdGws[i]->pszHost, pszSMTPDomain, pszMailFrom, pszRcptTo);
USmtpCleanupError(pSMTPE);
if ((iError = USmtpSendMail(ppszFwdGws[i], pszHeloDomain, pszSendMailFrom,
pszSendRcptTo, &FSect, pSMTPE)) == 0) {
/* Log Mailer operation */
if (SMAILLogEnabled(hShbSMAIL)) {
char szRmtMsgID[256];
USmtpGetSMTPRmtMsgID(USmtpGetErrorMessage(pSMTPE), szRmtMsgID,
sizeof(szRmtMsgID));
USmlLogMessage(hFSpool, "FWD", szRmtMsgID, ppszFwdGws[i]->pszHost);
}
break;
}
char szSmtpError[512];
USmtpGetSMTPError(pSMTPE, szSmtpError, sizeof(szSmtpError));
ErrLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send FWD = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", ppszFwdGws[i]->pszHost, pszSMTPDomain, pszMailFrom,
pszRcptTo, SMTP_ERROR_VARNAME, szSmtpError,
SMTP_SERVER_VARNAME, USmtpGetErrorServer(pSMTPE));
QueUtErrLogMessage(hQueue, hMessage,
"SMAIL SMTP-Send FWD = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", ppszFwdGws[i]->pszHost, pszSMTPDomain,
pszMailFrom, pszRcptTo, SMTP_ERROR_VARNAME,
szSmtpError, SMTP_SERVER_VARNAME,
USmtpGetErrorServer(pSMTPE));
if (USmtpIsFatalError(pSMTPE))
break;
}
USmtpFreeGateways(ppszFwdGws);
return iError;
}
/* Try to get custom mail exchangers or DNS mail exchangers and if both tests */
/* fails try direct */
SMTPGateway **ppMXGWs = USmtpGetMailExchangers(hSvrConfig, pszDestDomain);
if (ppMXGWs == NULL) {
/* If the destination domain is an IP-domain do direct deliver */
char szIP[64];
if (MscIsIPDomain(pszDestDomain, szIP, sizeof(szIP))) {
SysLogMessage(LOG_LEV_MESSAGE, "Direct IP delivery for \"%s\".\n", szIP);
SysLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send IP = \"%s\" SMTP = \"%s\" From = \"%s\" To = \"%s\"\n",
szIP, pszSMTPDomain, pszMailFrom, pszRcptTo);
USmtpCleanupError(pSMTPE);
if (USmtpMailRmtDeliver(hSvrConfig, szIP, pszHeloDomain, pszSendMailFrom, pszSendRcptTo,
&FSect, pSMTPE) < 0) {
ErrorPush();
char szSmtpError[512];
USmtpGetSMTPError(pSMTPE, szSmtpError, sizeof(szSmtpError));
ErrLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send IP = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", szIP, pszSMTPDomain, pszMailFrom,
pszRcptTo, SMTP_ERROR_VARNAME, szSmtpError,
SMTP_SERVER_VARNAME, USmtpGetErrorServer(pSMTPE));
QueUtErrLogMessage(hQueue, hMessage,
"SMAIL SMTP-Send IP = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", szIP, pszSMTPDomain,
pszMailFrom, pszRcptTo, SMTP_ERROR_VARNAME,
szSmtpError, SMTP_SERVER_VARNAME,
USmtpGetErrorServer(pSMTPE));
return ErrorPop();
}
/* Log Mailer operation */
if (SMAILLogEnabled(hShbSMAIL)) {
char szRmtMsgID[256];
USmtpGetSMTPRmtMsgID(USmtpGetErrorMessage(pSMTPE), szRmtMsgID,
sizeof(szRmtMsgID));
USmlLogMessage(hFSpool, "SMTP", szRmtMsgID, szIP);
}
return 0;
}
/* Do DNS MX lookup and send to mail exchangers */
char szDomainMXHost[MAX_HOST_NAME];
MXS_HANDLE hMXSHandle = USmtpGetMXFirst(hSvrConfig, pszDestDomain,
szDomainMXHost);
if (hMXSHandle != INVALID_MXS_HANDLE) {
iError = 0;
do {
SysLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send MX = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\"\n",
szDomainMXHost, pszSMTPDomain, pszMailFrom,
pszRcptTo);
USmtpCleanupError(pSMTPE);
if ((iError = USmtpMailRmtDeliver(hSvrConfig, szDomainMXHost,
pszHeloDomain,
pszSendMailFrom,
pszSendRcptTo, &FSect,
pSMTPE)) == 0) {
/* Log Mailer operation */
if (SMAILLogEnabled(hShbSMAIL)) {
char szRmtMsgID[256];
USmtpGetSMTPRmtMsgID(USmtpGetErrorMessage(pSMTPE),
szRmtMsgID,
sizeof(szRmtMsgID));
USmlLogMessage(hFSpool, "SMTP", szRmtMsgID,
szDomainMXHost);
}
break;
}
char szSmtpError[512];
USmtpGetSMTPError(pSMTPE, szSmtpError, sizeof(szSmtpError));
ErrLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send MX = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", szDomainMXHost, pszSMTPDomain,
pszMailFrom, pszRcptTo, SMTP_ERROR_VARNAME,
szSmtpError, SMTP_SERVER_VARNAME,
USmtpGetErrorServer(pSMTPE));
QueUtErrLogMessage(hQueue, hMessage,
"SMAIL SMTP-Send MX = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", szDomainMXHost, pszSMTPDomain,
pszMailFrom, pszRcptTo, SMTP_ERROR_VARNAME,
szSmtpError, SMTP_SERVER_VARNAME,
USmtpGetErrorServer(pSMTPE));
if (USmtpIsFatalError(pSMTPE))
break;
} while (USmtpGetMXNext(hMXSHandle, szDomainMXHost) == 0);
USmtpMXSClose(hMXSHandle);
if (iError < 0)
return iError;
} else {
iError = ErrGetErrorCode();
/*
* If the target domain does not exist at all (or it has a
* misconfigured DNS), bounce soon without trying the A record.
* It's pointless engaging in retry policies when the recipient
* domain does not exist.
*/
if (DNS_FatalError(iError)) {
ErrorPush();
/* No account inside the handled domain */
char szBounceMsg[512];
SysSNPrintf(szBounceMsg, sizeof(szBounceMsg) - 1,
"Recipient domain \"%s\" does not exist "
"(or it has a misconfigured DNS)", pszDestDomain);
SysLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send NXD/EDNS = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed!\n",
pszDestDomain, pszSMTPDomain, pszMailFrom, pszRcptTo);
QueUtErrLogMessage(hQueue, hMessage, "%s\n", szBounceMsg);
QueUtNotifyPermErrDelivery(hQueue, hMessage, hFSpool, szBounceMsg, NULL,
true);
return ErrorPop();
}
/*
* Fall back to A record delivery only if the domain has
* no MX records.
*/
if (iError != ERR_DNS_NOTFOUND) {
ErrLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send EDNS = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", pszDestDomain, pszSMTPDomain,
pszMailFrom, pszRcptTo, SMTP_ERROR_VARNAME,
ErrGetErrorString(iError), SMTP_SERVER_VARNAME,
pszDestDomain);
QueUtErrLogMessage(hQueue, hMessage,
"SMAIL SMTP-Send EDNS = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", pszDestDomain, pszSMTPDomain,
pszMailFrom, pszRcptTo, SMTP_ERROR_VARNAME,
ErrGetErrorString(iError), SMTP_SERVER_VARNAME,
pszDestDomain);
return ErrorSet(iError);
}
/* MX records for destination domain not found, try direct ! */
SysLogMessage(LOG_LEV_MESSAGE,
"MX records for domain \"%s\" not found, trying direct.\n",
pszDestDomain);
SysLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send FF = \"%s\" SMTP = \"%s\" From = \"%s\" To = \"%s\"\n",
pszDestDomain, pszSMTPDomain, pszMailFrom, pszRcptTo);
USmtpCleanupError(pSMTPE);
if ((iError = USmtpMailRmtDeliver(hSvrConfig, pszDestDomain,
pszHeloDomain, pszSendMailFrom,
pszSendRcptTo, &FSect, pSMTPE)) < 0) {
ErrorPush();
/*
* If the A record of the recipient host does not exist, set the
* fatal SMTP error so that the caller can properly bounce the
* message.
*/
if (iError == ERR_DNS_NOTFOUND || iError == ERR_BAD_SERVER_ADDR)
USmtpSetError(pSMTPE, SMTP_FATAL_ERROR,
ErrGetErrorString(iError), pszDestDomain);
char szSmtpError[512];
USmtpGetSMTPError(pSMTPE, szSmtpError, sizeof(szSmtpError));
ErrLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send FF = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", pszDestDomain, pszSMTPDomain,
pszMailFrom, pszRcptTo, SMTP_ERROR_VARNAME,
szSmtpError, SMTP_SERVER_VARNAME,
USmtpGetErrorServer(pSMTPE));
QueUtErrLogMessage(hQueue, hMessage,
"SMAIL SMTP-Send FF = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", pszDestDomain, pszSMTPDomain,
pszMailFrom, pszRcptTo, SMTP_ERROR_VARNAME,
szSmtpError, SMTP_SERVER_VARNAME,
USmtpGetErrorServer(pSMTPE));
return ErrorPop();
}
/* Log Mailer operation */
if (SMAILLogEnabled(hShbSMAIL)) {
char szRmtMsgID[256];
USmtpGetSMTPRmtMsgID(USmtpGetErrorMessage(pSMTPE),
szRmtMsgID, sizeof(szRmtMsgID));
USmlLogMessage(hFSpool, "SMTP", szRmtMsgID, pszDestDomain);
}
}
} else {
iError = 0;
for (int i = 0; ppMXGWs[i] != NULL; i++) {
SysLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send MX = \"%s\" SMTP = \"%s\" From = \"%s\" To = \"%s\"\n",
ppMXGWs[i]->pszHost, pszSMTPDomain, pszMailFrom, pszRcptTo);
USmtpCleanupError(pSMTPE);
if ((iError = USmtpSendMail(ppMXGWs[i], pszHeloDomain, pszSendMailFrom,
pszSendRcptTo, &FSect, pSMTPE)) == 0) {
/* Log Mailer operation */
if (SMAILLogEnabled(hShbSMAIL)) {
char szRmtMsgID[256];
USmtpGetSMTPRmtMsgID(USmtpGetErrorMessage(pSMTPE),
szRmtMsgID, sizeof(szRmtMsgID));
USmlLogMessage(hFSpool, "SMTP", szRmtMsgID,
ppMXGWs[i]->pszHost);
}
break;
}
char szSmtpError[512];
USmtpGetSMTPError(pSMTPE, szSmtpError, sizeof(szSmtpError));
ErrLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send MX = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", ppMXGWs[i]->pszHost, pszSMTPDomain, pszMailFrom,
pszRcptTo, SMTP_ERROR_VARNAME, szSmtpError,
SMTP_SERVER_VARNAME, USmtpGetErrorServer(pSMTPE));
QueUtErrLogMessage(hQueue, hMessage,
"SMAIL SMTP-Send MX = \"%s\" SMTP = \"%s\" "
"From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", ppMXGWs[i]->pszHost, pszSMTPDomain,
pszMailFrom, pszRcptTo, SMTP_ERROR_VARNAME,
szSmtpError, SMTP_SERVER_VARNAME,
USmtpGetErrorServer(pSMTPE));
if (USmtpIsFatalError(pSMTPE))
break;
}
USmtpFreeGateways(ppMXGWs);
if (iError < 0)
return iError;
}
return 0;
}
static char *SMAILMacroLkupProc(void *pPrivate, char const *pszName, size_t sSize)
{
MacroSubstCtx *pMSC = (MacroSubstCtx *) pPrivate;
if (MemMatch(pszName, sSize, "FROM", 4)) {
char const *const *ppszFrom = USmlGetMailFrom(pMSC->hFSpool);
int iFromDomains = StrStringsCount(ppszFrom);
return SysStrDup((iFromDomains > 0) ? ppszFrom[iFromDomains - 1] : "");
} else if (MemMatch(pszName, sSize, "RCPT", 4)) {
char const *const *ppszRcpt = USmlGetRcptTo(pMSC->hFSpool);
int iRcptDomains = StrStringsCount(ppszRcpt);
return SysStrDup((iRcptDomains > 0) ? ppszRcpt[iRcptDomains - 1] : "");
} else if (MemMatch(pszName, sSize, "FILE", 4)) {
return SysStrDup(pMSC->FSect.szFilePath);
} else if (MemMatch(pszName, sSize, "MSGID", 5)) {
return SysStrDup(USmlGetSpoolFile(pMSC->hFSpool));
} else if (MemMatch(pszName, sSize, "MSGREF", 6)) {
return SysStrDup(USmlGetSmtpMessageID(pMSC->hFSpool));
} else if (MemMatch(pszName, sSize, "LOCALADDR", 9)) {
char const *const *ppszInfo = USmlGetInfo(pMSC->hFSpool);
return SysStrDup(ppszInfo[smiServerAddr]);
} else if (MemMatch(pszName, sSize, "REMOTEADDR", 10)) {
char const *const *ppszInfo = USmlGetInfo(pMSC->hFSpool);
return SysStrDup(ppszInfo[smiClientAddr]);
} else if (MemMatch(pszName, sSize, "TMPFILE", 7)) {
char szTmpFile[SYS_MAX_PATH];
MscSafeGetTmpFile(szTmpFile, sizeof(szTmpFile));
if (MscCopyFile(szTmpFile, pMSC->FSect.szFilePath) < 0) {
CheckRemoveFile(szTmpFile);
return NULL;
}
return SysStrDup(szTmpFile);
} else if (MemMatch(pszName, sSize, "USERAUTH", 8)) {
char szAuthName[MAX_ADDR_NAME] = "-";
USmlMessageAuth(pMSC->hFSpool, szAuthName, sizeof(szAuthName) - 1);
return SysStrDup(szAuthName);
}
return SysStrDup("");
}
static int SMAILCmdMacroSubstitutes(char **ppszCmdTokens, SPLF_HANDLE hFSpool)
{
MacroSubstCtx MSC;
MSC.hFSpool = hFSpool;
/*
* This function retrieve the spool file message section and sync the content.
* This is necessary before passing the file name to external programs.
*/
if (USmlGetMsgFileSection(hFSpool, MSC.FSect) < 0)
return ErrGetErrorCode();
return MscReplaceTokens(ppszCmdTokens, SMAILMacroLkupProc, &MSC);
}
static int SMAILCmd_external(SVRCFG_HANDLE hSvrConfig, SHB_HANDLE hShbSMAIL,
char const *pszDestDomain, char **ppszCmdTokens, int iNumTokens,
SPLF_HANDLE hFSpool, QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage)
{
/* Apply filters ... */
if (FilFilterMessage(hFSpool, hQueue, hMessage, FILTER_MODE_INBOUND) < 0)
return ErrGetErrorCode();
if (iNumTokens < 5) {
ErrSetErrorCode(ERR_BAD_DOMAIN_PROC_CMD_SYNTAX);
return ERR_BAD_DOMAIN_PROC_CMD_SYNTAX;
}
int iPriority = atoi(ppszCmdTokens[1]);
int iWaitTimeout = atoi(ppszCmdTokens[2]) * 1000;
int iExitStatus = 0;
if (SysExec(ppszCmdTokens[3], &ppszCmdTokens[3], iWaitTimeout, iPriority,
&iExitStatus) < 0) {
ErrorPush();
char const *pszMailFrom = USmlMailFrom(hFSpool);
char const *pszRcptTo = USmlRcptTo(hFSpool);
ErrLogMessage(LOG_LEV_MESSAGE,
"SMAIL EXTRN-Send Prg = \"%s\" Domain = \"%s\" From = \"%s\" To = \"%s\" Failed !\n",
ppszCmdTokens[3], pszDestDomain, pszMailFrom, pszRcptTo);
QueUtErrLogMessage(hQueue, hMessage,
"SMAIL EXTRN-Send Prg = \"%s\" Domain = \"%s\" From = \"%s\" To = \"%s\" Failed !\n",
ppszCmdTokens[3], pszDestDomain, pszMailFrom, pszRcptTo);
return ErrorPop();
}
/* Log Mailer operation */
if (SMAILLogEnabled(hShbSMAIL))
USmlLogMessage(hFSpool, "EXTRN", NULL, ppszCmdTokens[3]);
return (iExitStatus == SMAIL_EXTERNAL_EXIT_BREAK) ? SMAIL_STOP_PROCESSING: 0;
}
static int SMAILCmd_filter(SVRCFG_HANDLE hSvrConfig, SHB_HANDLE hShbSMAIL,
char const *pszDestDomain, char **ppszCmdTokens, int iNumTokens,
SPLF_HANDLE hFSpool, QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage)
{
if (iNumTokens < 5) {
ErrSetErrorCode(ERR_BAD_MAILPROC_CMD_SYNTAX);
return ERR_BAD_MAILPROC_CMD_SYNTAX;
}
int iPriority = atoi(ppszCmdTokens[1]);
int iWaitTimeout = atoi(ppszCmdTokens[2]) * 1000;
int iExitStatus = 0;
if (SysExec(ppszCmdTokens[3], &ppszCmdTokens[3], iWaitTimeout, iPriority,
&iExitStatus) < 0) {
ErrorPush();
char const *pszMailFrom = USmlMailFrom(hFSpool);
char const *pszRcptTo = USmlRcptTo(hFSpool);
ErrLogMessage(LOG_LEV_MESSAGE,
"USMAIL FILTER Prg = \"%s\" From = \"%s\" To = \"%s\" Failed !\n",
ppszCmdTokens[3], pszMailFrom, pszRcptTo);
QueUtErrLogMessage(hQueue, hMessage,
"USMAIL FILTER Prg = \"%s\" From = \"%s\" To = \"%s\" Failed !\n",
ppszCmdTokens[3], pszMailFrom, pszRcptTo);
return ErrorPop();
}
/* Log Mailer operation */
if (SMAILLogEnabled(hShbSMAIL))
USmlLogMessage(hFSpool, "FILTER", NULL, ppszCmdTokens[3]);
/* Separate code from flags */
int iExitFlags = iExitStatus & FILTER_FLAGS_MASK;
iExitStatus &= ~FILTER_FLAGS_MASK;
if (iExitStatus == FILTER_OUT_EXITCODE ||
iExitStatus == FILTER_OUT_NN_EXITCODE ||
iExitStatus == FILTER_OUT_NNF_EXITCODE) {
/* Filter out message */
char *pszRejMsg = FilGetFilterRejMessage(USmlGetSpoolFilePath(hFSpool));
if (iExitStatus == FILTER_OUT_EXITCODE)
QueUtNotifyPermErrDelivery(hQueue, hMessage, NULL,
(pszRejMsg != NULL) ? pszRejMsg :
ErrGetErrorString(ERR_FILTERED_MESSAGE),
NULL, true);
else if (iExitStatus == FILTER_OUT_NN_EXITCODE)
QueCleanupMessage(hQueue, hMessage,
!QueUtRemoveSpoolErrors());
else
QueCleanupMessage(hQueue, hMessage, false);
SysFree(pszRejMsg);
ErrSetErrorCode(ERR_FILTERED_MESSAGE);
return ERR_FILTERED_MESSAGE;
} else if (iExitStatus == FILTER_MODIFY_EXITCODE) {
/* Filter modified the message, we need to reload the spool handle */
if (USmlReloadHandle(hFSpool) < 0) {
ErrorPush();
char const *pszMailFrom = USmlMailFrom(hFSpool);
char const *pszRcptTo = USmlRcptTo(hFSpool);
SysLogMessage(LOG_LEV_MESSAGE,
"Filter error [ Modified message corrupted ]: Sender = \"%s\" Recipient = \"%s\" (%s)\n",
pszMailFrom, pszRcptTo,
ppszCmdTokens[3]);
QueUtErrLogMessage(hQueue, hMessage,
"Filter error [ Modified message corrupted ]: Sender = \"%s\" Recipient = \"%s\" (%s)\n",
pszMailFrom, pszRcptTo,
ppszCmdTokens[3]);
QueCleanupMessage(hQueue, hMessage, true);
return ErrorPop();
}
}
return (iExitFlags & FILTER_FLAGS_BREAK) ? SMAIL_STOP_PROCESSING: 0;
}
static int SMAILCmd_smtp(SVRCFG_HANDLE hSvrConfig, SHB_HANDLE hShbSMAIL,
char const *pszDestDomain, char **ppszCmdTokens, int iNumTokens,
SPLF_HANDLE hFSpool, QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage)
{
if (iNumTokens != 1) {
ErrSetErrorCode(ERR_BAD_DOMAIN_PROC_CMD_SYNTAX);
return ERR_BAD_DOMAIN_PROC_CMD_SYNTAX;
}
SMTPError SMTPE;
USmtpInitError(&SMTPE);
if (SMAILRemoteMsgSMTPSend(hSvrConfig, hShbSMAIL, hFSpool, hQueue, hMessage,
pszDestDomain, &SMTPE) < 0) {
/* If we get an SMTP fatal error We must return <0 , otherwise >0 to give */
/* XMail to ability to resume the command */
int iReturnCode = USmtpIsFatalError(&SMTPE) ?
ErrGetErrorCode(): -ErrGetErrorCode();
/* If a permanent SMTP error has been detected, then notify the message sender */
if (USmtpIsFatalError(&SMTPE))
QueUtNotifyPermErrDelivery(hQueue, hMessage, hFSpool,
USmtpGetErrorMessage(&SMTPE),
USmtpGetErrorServer(&SMTPE), false);
USmtpCleanupError(&SMTPE);
return iReturnCode;
}
USmtpCleanupError(&SMTPE);
return 0;
}
static int SMAILCmd_smtprelay(SVRCFG_HANDLE hSvrConfig, SHB_HANDLE hShbSMAIL,
char const *pszDestDomain, char **ppszCmdTokens, int iNumTokens,
SPLF_HANDLE hFSpool, QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage)
{
/* Apply filters ... */
if (FilFilterMessage(hFSpool, hQueue, hMessage, FILTER_MODE_OUTBOUND) < 0)
return ErrGetErrorCode();
if (iNumTokens < 2) {
ErrSetErrorCode(ERR_BAD_DOMAIN_PROC_CMD_SYNTAX);
return ERR_BAD_DOMAIN_PROC_CMD_SYNTAX;
}
char **ppszRelays = NULL;
if (ppszCmdTokens[1][0] == '#') {
if ((ppszRelays = StrTokenize(ppszCmdTokens[1] + 1, ";")) != NULL)
MscRandomizeStringsOrder(ppszRelays);
} else
ppszRelays = StrTokenize(ppszCmdTokens[1], ";");
if (ppszRelays == NULL)
return ErrGetErrorCode();
SMTPGateway **ppGws = USmtpGetCfgGateways(hSvrConfig, ppszRelays,
iNumTokens > 2 ? ppszCmdTokens[2]: NULL);
StrFreeStrings(ppszRelays);
if (ppGws == NULL)
return ErrGetErrorCode();
/* This function retrieve the spool file message section and sync the content. */
/* This is necessary before sending the file */
FileSection FSect;
if (USmlGetMsgFileSection(hFSpool, FSect) < 0) {
ErrorPush();
USmtpFreeGateways(ppGws);
return ErrorPop();
}
/* Get spool file infos */
char const *pszSMTPDomain = USmlGetSMTPDomain(hFSpool);
char const *pszMailFrom = USmlMailFrom(hFSpool);
char const *pszRcptTo = USmlRcptTo(hFSpool);
char const *pszSendMailFrom = USmlSendMailFrom(hFSpool);
char const *pszSendRcptTo = USmlSendRcptTo(hFSpool);
char const *pszSpoolFilePath = USmlGetSpoolFilePath(hFSpool);
/* Get HELO domain */
char szHeloDomain[MAX_HOST_NAME];
SvrConfigVar("HeloDomain", szHeloDomain, sizeof(szHeloDomain) - 1, hSvrConfig, "");
char const *pszHeloDomain = IsEmptyString(szHeloDomain) ? NULL: szHeloDomain;
SMTPError SMTPE;
USmtpInitError(&SMTPE);
/* By initializing this to zero makes XMail to discharge all mail for domains */
/* that have an empty relay list */
int iReturnCode = 0;
for (int i = 0; ppGws[i] != NULL && iReturnCode >= 0; i++) {
SysLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send RLYS = \"%s\" SMTP = \"%s\" From = \"%s\" To = \"%s\"\n",
ppGws[i]->pszHost, pszSMTPDomain, pszMailFrom, pszRcptTo);
USmtpCleanupError(&SMTPE);
if (USmtpSendMail(ppGws[i], pszHeloDomain, pszSendMailFrom,
pszSendRcptTo, &FSect, &SMTPE) == 0) {
/* Log Mailer operation */
if (SMAILLogEnabled(hShbSMAIL)) {
char szRmtMsgID[256];
USmtpGetSMTPRmtMsgID(USmtpGetErrorMessage(&SMTPE),
szRmtMsgID,
sizeof(szRmtMsgID));
USmlLogMessage(hFSpool, "RLYS", szRmtMsgID,
ppGws[i]->pszHost);
}
USmtpCleanupError(&SMTPE);
USmtpFreeGateways(ppGws);
return 0;
}
int iErrorCode = ErrGetErrorCode();
char szSmtpError[512];
USmtpGetSMTPError(&SMTPE, szSmtpError, sizeof(szSmtpError));
ErrLogMessage(LOG_LEV_MESSAGE,
"SMAIL SMTP-Send RLYS = \"%s\" SMTP = \"%s\" From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", ppGws[i]->pszHost, pszSMTPDomain, pszMailFrom,
pszRcptTo, SMTP_ERROR_VARNAME, szSmtpError, SMTP_SERVER_VARNAME,
USmtpGetErrorServer(&SMTPE));
QueUtErrLogMessage(hQueue, hMessage,
"SMAIL SMTP-Send RLYS = \"%s\" SMTP = \"%s\" From = \"%s\" To = \"%s\" Failed !\n"
"%s = \"%s\"\n"
"%s = \"%s\"\n", ppGws[i]->pszHost, pszSMTPDomain, pszMailFrom,
pszRcptTo, SMTP_ERROR_VARNAME, szSmtpError,
SMTP_SERVER_VARNAME, USmtpGetErrorServer(&SMTPE));
/* If a permanent SMTP error has been detected, then notify the message sender */
if (USmtpIsFatalError(&SMTPE))
QueUtNotifyPermErrDelivery(hQueue, hMessage, hFSpool,
USmtpGetErrorMessage(&SMTPE),
USmtpGetErrorServer(&SMTPE), false);
iReturnCode = USmtpIsFatalError(&SMTPE) ? iErrorCode: -iErrorCode;
}