forked from GerHobbelt/xmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMTPSvr.cpp
3344 lines (2652 loc) · 107 KB
/
SMTPSvr.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"
/* [i_a] */
#include "SysAssert.h"
#include "SvrDefines.h"
#include "SList.h"
#include "ShBlocks.h"
#include "BuffSock.h"
#include "SSLBind.h"
#include "ResLocks.h"
#include "StrUtils.h"
#include "UsrUtils.h"
#include "SvrUtils.h"
#include "MessQueue.h"
#include "SMAILUtils.h"
#include "QueueUtils.h"
#include "MiscUtils.h"
#include "SSLConfig.h"
#include "Base64Enc.h"
#include "MD5.h"
#include "UsrMailList.h"
#include "SMTPSvr.h"
#include "SMTPUtils.h"
#include "MailDomains.h"
#include "AliasDomain.h"
#include "POP3Utils.h"
#include "Filter.h"
#include "MailConfig.h"
#include "AppDefines.h"
#include "MailSvr.h"
#define SMTP_MAX_LINE_SIZE 2048
#define STD_SMTP_TIMEOUT 30000
#define SMTP_IPMAP_FILE "smtp.ipmap.tab"
#define SMTP_LOG_FILE "smtp"
#define SMTP_POST_RCPT_FILTER "post-rcpt"
#define SMTP_PRE_DATA_FILTER "pre-data"
#define SMTP_POST_DATA_FILTER "post-data"
#define SMTP_FILTER_REJECT_CODE 3
#define PLAIN_AUTH_PARAM_SIZE 1024
#define LOGIN_AUTH_USERNAME "Username:"
#define LOGIN_AUTH_PASSWORD "Password:"
#define SVR_SMTP_AUTH_FILE "smtpauth.tab"
#define SVR_SMTPAUTH_LINE_MAX 512
#define SVR_SMTP_EXTAUTH_FILE "smtpextauth.tab"
#define SVR_SMTP_EXTAUTH_LINE_MAX 1024
#define SVR_SMTP_EXTAUTH_TIMEOUT 60000
#define SVR_SMTP_EXTAUTH_SUCCESS 0
#define SMTPF_RELAY_ENABLED (1 << 0)
#define SMTPF_MAIL_LOCKED (1 << 1)
#define SMTPF_MAIL_UNLOCKED (1 << 2)
#define SMTPF_AUTHENTICATED (1 << 3)
#define SMTPF_VRFY_ENABLED (1 << 4)
#define SMTPF_MAPPED_IP (1 << 5)
#define SMTPF_NORDNS_IP (1 << 6)
#define SMTPF_ETRN_ENABLED (1 << 7)
#define SMTPF_NOEMIT_AUTH (1 << 8)
#define SMTPF_WHITE_LISTED (1 << 9)
#define SMTPF_BLOCKED_IP (1 << 10)
#define SMTPF_IPMAPPED_IP (1 << 11)
#define SMTPF_SNDRCHECK_BYPASS (1 << 12)
#define SMTPF_WANT_TLS (1 << 13)
#define SMTPF_EASE_TLS (1 << 14)
#define SMTPF_STATIC_MASK (SMTPF_MAPPED_IP | SMTPF_NORDNS_IP | SMTPF_WHITE_LISTED | \
SMTPF_SNDRCHECK_BYPASS | SMTPF_BLOCKED_IP | SMTPF_IPMAPPED_IP)
#define SMTPF_AUTH_MASK (SMTPF_RELAY_ENABLED | SMTPF_MAIL_UNLOCKED | SMTPF_AUTHENTICATED | \
SMTPF_VRFY_ENABLED | SMTPF_ETRN_ENABLED | SMTPF_EASE_TLS)
#define SMTPF_RESET_MASK (SMTPF_AUTH_MASK | SMTPF_STATIC_MASK)
#define SMTP_FILTER_FL_BREAK (1 << 4)
#define SMTP_FILTER_FL_MASK SMTP_FILTER_FL_BREAK
enum SMTPStates {
stateInit = 0,
stateHelo,
stateAuthenticated,
stateMail,
stateRcpt,
stateExit
};
struct SMTPSession {
int iSMTPState;
ThreadConfig const *pThCfg;
SMTPConfig *pSMTPCfg;
SVRCFG_HANDLE hSvrConfig;
SYS_INET_ADDR PeerInfo;
SYS_INET_ADDR SockInfo;
int iCmdDelay;
unsigned long ulMaxMsgSize;
char szSvrFQDN[MAX_ADDR_NAME];
char szSvrDomain[MAX_ADDR_NAME];
char szClientFQDN[MAX_ADDR_NAME];
char szClientDomain[MAX_ADDR_NAME];
char szDestDomain[MAX_ADDR_NAME];
char szLogonUser[128];
char szMsgFile[SYS_MAX_PATH];
FILE *pMsgFile;
char *pszFrom;
char *pszRcpt;
char *pszSendRcpt;
char *pszRealRcpt;
int iRcptCount;
int iErrorsCount;
int iErrorsMax;
SYS_UINT64 ullMessageID;
char szMessageID[128];
char szTimeStamp[256];
unsigned long ulSetupFlags;
unsigned long ulFlags;
char *pszCustMsg;
char szRejMapName[256];
char *pszNoTLSAuths;
};
enum SmtpAuthFields {
smtpaUsername = 0,
smtpaPassword,
smtpaPerms,
smtpaMax
};
struct ExtAuthMacroSubstCtx {
char const *pszAuthType;
char const *pszUsername;
char const *pszPassword;
char const *pszChallenge;
char const *pszDigest;
char const *pszRespFile;
};
static int SMTPLogEnabled(SHB_HANDLE hShbSMTP, SMTPConfig *pSMTPCfg = NULL);
static int SMTPThreadCountAdd(long lCount, SHB_HANDLE hShbSMTP, SMTPConfig *pSMTPCfg = NULL);
static char const *SMTPGetFilterExtname(char const *pszFiltID)
{
static struct Filter_ID_Name {
char const *pszFiltID;
char const *pszName;
} const FiltIdNames[] = {
{ SMTP_POST_RCPT_FILTER, "RCPT" },
{ SMTP_PRE_DATA_FILTER, "PREDATA" },
{ SMTP_POST_DATA_FILTER, "POSTDATA" },
};
int i;
for (i = 0; i < CountOf(FiltIdNames); i++)
if (strcmp(FiltIdNames[i].pszFiltID, pszFiltID) == 0)
return FiltIdNames[i].pszName;
return "UNKNOWN";
}
static SMTPConfig *SMTPGetConfigCopy(SHB_HANDLE hShbSMTP)
{
SMTPConfig *pSMTPCfg = (SMTPConfig *) ShbLock(hShbSMTP);
if (pSMTPCfg == NULL)
return NULL;
SMTPConfig *pSMTPCfgCopy = (SMTPConfig *) SysAlloc(sizeof(SMTPConfig));
if (pSMTPCfgCopy != NULL)
memcpy(pSMTPCfgCopy, pSMTPCfg, sizeof(SMTPConfig));
ShbUnlock(hShbSMTP);
return pSMTPCfgCopy;
}
static int SMTPLogEnabled(SHB_HANDLE hShbSMTP, SMTPConfig *pSMTPCfg)
{
int iDoUnlock = 0;
if (pSMTPCfg == NULL) {
if ((pSMTPCfg = (SMTPConfig *) ShbLock(hShbSMTP)) == NULL)
return ErrGetErrorCode();
++iDoUnlock;
}
unsigned long ulFlags = pSMTPCfg->ulFlags;
if (iDoUnlock)
ShbUnlock(hShbSMTP);
return (ulFlags & SMTPF_LOG_ENABLED) ? 1 : 0;
}
static int SMTPCheckPeerIP(const SYS_INET_ADDR &PeerAddr)
{
char szIPMapFile[SYS_MAX_PATH] = "";
CfgGetRootPath(szIPMapFile, sizeof(szIPMapFile));
StrNCat(szIPMapFile, SMTP_IPMAP_FILE, sizeof(szIPMapFile));
SysLogMessage(LOG_LEV_DEBUG, "Going to look at config file: '%s'\n", szIPMapFile);
if (SysExistFile(szIPMapFile) &&
MscCheckAllowedIP(szIPMapFile, PeerAddr, true) < 0)
return ErrGetErrorCode();
return 0;
}
static int SMTPThreadCountAdd(long lCount, SHB_HANDLE hShbSMTP, SMTPConfig *pSMTPCfg)
{
int iDoUnlock = 0;
if (pSMTPCfg == NULL) {
if ((pSMTPCfg = (SMTPConfig *) ShbLock(hShbSMTP)) == NULL)
return ErrGetErrorCode();
++iDoUnlock;
}
if ((pSMTPCfg->lThreadCount + lCount) > pSMTPCfg->lMaxThreads) {
if (iDoUnlock)
ShbUnlock(hShbSMTP);
ErrSetErrorCode(ERR_SERVER_BUSY);
return ERR_SERVER_BUSY;
}
pSMTPCfg->lThreadCount += lCount;
if (iDoUnlock)
ShbUnlock(hShbSMTP);
return 0;
}
static int SMTPCheckSysResources(SVRCFG_HANDLE hSvrConfig)
{
/* Check disk space */
int iMinValue = SvrGetConfigInt("SmtpMinDiskSpace", -1, hSvrConfig);
if ((iMinValue > 0) && (SvrCheckDiskSpace(1024 * (unsigned long) iMinValue) < 0))
return ErrGetErrorCode();
/* Check virtual memory */
if (((iMinValue = SvrGetConfigInt("SmtpMinVirtMemSpace", -1, hSvrConfig)) > 0) &&
(SvrCheckVirtMemSpace(1024 * (unsigned long) iMinValue) < 0))
return ErrGetErrorCode();
return 0;
}
static int SMTPEnumIPPropsCB(void *pPrivate, char const *pszName, char const *pszVal)
{
SMTPSession *pSMTPS = (SMTPSession *) pPrivate;
if (strcmp(pszName, "WhiteList") == 0) {
if (pszVal == NULL || atoi(pszVal))
pSMTPS->ulFlags |= SMTPF_WHITE_LISTED;
} else if (strcmp(pszName, "NoAuth") == 0) {
if (pszVal == NULL || atoi(pszVal))
pSMTPS->ulFlags |= SMTPF_MAIL_UNLOCKED;
} else if (strcmp(pszName, "SenderDomainCheck") == 0) {
if (pszVal != NULL && !atoi(pszVal))
pSMTPS->ulFlags |= SMTPF_SNDRCHECK_BYPASS;
} else if (strcmp(pszName, "EaseTLS") == 0) {
if (pszVal == NULL || atoi(pszVal))
pSMTPS->ulSetupFlags &= ~SMTPF_WANT_TLS;
} else if (strcmp(pszName, "EnableVRFY") == 0) {
if (pszVal == NULL || atoi(pszVal))
pSMTPS->ulFlags |= SMTPF_VRFY_ENABLED;
} else if (strcmp(pszName, "EnableETRN") == 0) {
if (pszVal == NULL || atoi(pszVal))
pSMTPS->ulFlags |= SMTPF_ETRN_ENABLED;
}
return 0;
}
static int SMTPApplyIPProps(SMTPSession &SMTPS)
{
return SvrEnumProtoProps("smtp", &SMTPS.PeerInfo,
IsEmptyString(SMTPS.szClientFQDN) ? NULL: SMTPS.szClientFQDN,
SMTPEnumIPPropsCB, &SMTPS);
}
static int SMTPLogSession(SMTPSession &SMTPS, char const *pszSender,
char const *pszRecipient, char const *pszStatus,
unsigned long ulMsgSize)
{
char szTime[256] = "";
MscGetTimeNbrString(szTime, sizeof(szTime) - 1);
RLCK_HANDLE hResLock = RLckLockEX(SVR_LOGS_DIR SYS_SLASH_STR SMTP_LOG_FILE);
if (hResLock == INVALID_RLCK_HANDLE)
return ErrGetErrorCode();
char szIP[128] = "???.???.???.???";
MscFileLog(SMTP_LOG_FILE, "\"%s\""
"\t\"%s\""
"\t\"%s\""
"\t\"%s\""
"\t\"%s\""
"\t\"%s\""
"\t\"%s\""
"\t\"%s\""
"\t\"%s\""
"\t\"%s\""
"\t\"%s\""
"\t\"%lu\""
"\t\"%s\""
"\n", SMTPS.szSvrFQDN, SMTPS.szSvrDomain,
SysInetNToA(SMTPS.PeerInfo, szIP, sizeof(szIP)), szTime,
SMTPS.szClientDomain, SMTPS.szDestDomain, pszSender, pszRecipient,
SMTPS.szMessageID, pszStatus, SMTPS.szLogonUser, ulMsgSize,
SMTPS.szClientFQDN);
RLckUnlockEX(hResLock);
return 0;
}
static int SMTPCheckMapsList(SYS_INET_ADDR const &PeerInfo, char const *pszMapList,
char *pszMapName, int iMaxMapName, int &iMapCode)
{
for (;;) {
char const *pszColon = strchr(pszMapList, ':');
if (pszColon == NULL)
break;
int iRetCode = atoi(pszColon + 1);
int iMapLength = Min((int) (pszColon - pszMapList), MAX_HOST_NAME - 1);
char szMapName[MAX_HOST_NAME] = "";
strncpy(szMapName, pszMapList, iMapLength);
szMapName[iMapLength] = '\0';
if (USmtpDnsMapsContained(PeerInfo, szMapName)) {
char szIP[128] = "???.???.???.???";
char szMapSpec[MAX_HOST_NAME + 128] = "";
if (pszMapName != NULL)
StrNCpy(pszMapName, szMapName, iMaxMapName);
iMapCode = iRetCode;
SysInetNToA(PeerInfo, szIP, sizeof(szIP));
SysSNPrintf(szMapSpec, sizeof(szMapSpec) - 1, "%s:%s",
szMapName, szIP);
ErrSetErrorCode(ERR_MAPS_CONTAINED, szMapSpec);
return ERR_MAPS_CONTAINED;
}
if ((pszMapList = strchr(pszColon, ',')) == NULL)
break;
++pszMapList;
}
return 0;
}
static int SMTPDoIPBasedInit(SMTPSession &SMTPS, char *&pszSMTPError)
{
int iErrorCode;
/* Check using the "smtp.ipmap.tab" file */
if ((iErrorCode = SMTPCheckPeerIP(SMTPS.PeerInfo)) < 0) {
int iMapCode = SvrGetConfigInt("SMTP-IpMapDropCode", 1, SMTPS.hSvrConfig);
if (iMapCode > 0) {
if (SMTPLogEnabled(SMTPS.pThCfg->hThShb, SMTPS.pSMTPCfg))
SMTPLogSession(SMTPS, "", "", "SNDRIP=EIPBAN", 0);
pszSMTPError = SvrGetConfigVar(SMTPS.hSvrConfig, "SmtpMsgIPBan");
ErrSetErrorCode(iErrorCode);
return iErrorCode;
} else if (iMapCode == 0) {
SMTPS.ulFlags |= SMTPF_IPMAPPED_IP;
} else
SMTPS.iCmdDelay = Max(SMTPS.iCmdDelay, Abs(iMapCode));
}
/* Check if SMTP client is in "spammers.tab" file */
char *pszChkInfo = NULL;
if ((iErrorCode = USmtpSpammerCheck(SMTPS.PeerInfo, pszChkInfo)) < 0) {
int iMapCode = 1;
if (pszChkInfo != NULL) {
char szMapCode[32] = "";
if (StrParamGet(pszChkInfo, "code", szMapCode, sizeof(szMapCode) - 1))
iMapCode = atoi(szMapCode);
SysFree(pszChkInfo);
}
if (iMapCode > 0) {
if (SMTPLogEnabled(SMTPS.pThCfg->hThShb, SMTPS.pSMTPCfg))
SMTPLogSession(SMTPS, "", "", "SNDRIP=EIPSPAM", 0);
pszSMTPError = SvrGetConfigVar(SMTPS.hSvrConfig, "SmtpMsgIPBanSpammers");
ErrSetErrorCode(iErrorCode);
return iErrorCode;
} else if (iMapCode == 0) {
SMTPS.ulFlags |= SMTPF_BLOCKED_IP;
} else
SMTPS.iCmdDelay = Max(SMTPS.iCmdDelay, Abs(iMapCode));
}
/* Custom maps checking */
char *pszMapsList = SvrGetConfigVar(SMTPS.hSvrConfig, "CustMapsList");
if (pszMapsList != NULL) {
int iMapCode = 0;
char *pszCfgError = NULL;
if (SMTPCheckMapsList(SMTPS.PeerInfo, pszMapsList, SMTPS.szRejMapName,
sizeof(SMTPS.szRejMapName) - 1, iMapCode) < 0) {
if (iMapCode == 1) {
ErrorPush();
if (SMTPLogEnabled(SMTPS.pThCfg->hThShb, SMTPS.pSMTPCfg)) {
char *pszError =
StrSprint("SNDRIP=EIPMAP (%s)", SMTPS.szRejMapName);
SMTPLogSession(SMTPS, "", "",
(pszError !=
NULL) ? pszError : "SNDRIP=EIPMAP", 0);
SysFree(pszError);
}
if ((pszCfgError =
SvrGetConfigVar(SMTPS.hSvrConfig,
"SmtpMsgIPBanMaps")) != NULL) {
pszSMTPError =
StrSprint("%s (%s)", pszCfgError, SMTPS.szRejMapName);
SysFree(pszCfgError);
} else
pszSMTPError =
StrSprint
("550 Denied due inclusion of your IP inside (%s)",
SMTPS.szRejMapName);
SysFree(pszMapsList);
return ErrorPop();
}
if (iMapCode == 0)
SMTPS.ulFlags |= SMTPF_MAPPED_IP;
else
SMTPS.iCmdDelay = Max(SMTPS.iCmdDelay, Abs(iMapCode));
}
SysFree(pszMapsList);
}
/* RDNS client check */
int iCheckValue = SvrGetConfigInt("SMTP-RDNSCheck", 0, SMTPS.hSvrConfig);
if (iCheckValue != 0 &&
SysGetHostByAddr(SMTPS.PeerInfo, SMTPS.szClientFQDN, sizeof(SMTPS.szClientFQDN)) < 0) {
if (iCheckValue > 0)
SMTPS.ulFlags |= SMTPF_NORDNS_IP;
else
SMTPS.iCmdDelay = Max(SMTPS.iCmdDelay, -iCheckValue);
}
return 0;
}
static int SMTPSvrCfgOptionsAssign(void *pPrivate, char const *pszName, char const *pszValue)
{
SMTPSession *pSMTPS = (SMTPSession *) pPrivate;
if (strcmp(pszName, "mail-auth") == 0 ||
strcmp(pszName, "MailAuth") == 0) {
if (pszValue == NULL || atoi(pszValue))
pSMTPS->ulSetupFlags |= SMTPF_MAIL_LOCKED;
} else if (strcmp(pszName, "WantTLS") == 0) {
if (pszValue == NULL || atoi(pszValue))
pSMTPS->ulSetupFlags |= SMTPF_WANT_TLS;
}
return 0;
}
static int SMTPLoadConfig(SMTPSession &SMTPS, char const *pszSvrConfig)
{
return MscParseOptions(pszSvrConfig, SMTPSvrCfgOptionsAssign, &SMTPS);
}
static int SMTPApplyPerms(SMTPSession &SMTPS, char const *pszPerms)
{
for (int i = 0; pszPerms[i] != '\0'; i++) {
switch (pszPerms[i]) {
case 'M':
SMTPS.ulFlags |= SMTPF_MAIL_UNLOCKED;
break;
case 'R':
SMTPS.ulFlags |= SMTPF_RELAY_ENABLED;
break;
case 'V':
SMTPS.ulFlags |= SMTPF_VRFY_ENABLED;
break;
case 'T':
SMTPS.ulFlags |= SMTPF_ETRN_ENABLED;
break;
case 'Z':
SMTPS.ulMaxMsgSize = 0;
break;
case 'S':
SMTPS.ulFlags |= SMTPF_EASE_TLS;
break;
}
}
/* Clear bad ip mask and command delay */
SMTPS.ulFlags &= ~(SMTPF_MAPPED_IP | SMTPF_NORDNS_IP | SMTPF_BLOCKED_IP | SMTPF_IPMAPPED_IP);
SMTPS.iCmdDelay = 0;
return 0;
}
static int SMTPInitSession(ThreadConfig const *pThCfg, BSOCK_HANDLE hBSock,
SMTPSession &SMTPS, char *&pszSMTPError)
{
ZeroData(SMTPS);
SMTPS.iSMTPState = stateInit;
SMTPS.pThCfg = pThCfg;
SMTPS.hSvrConfig = INVALID_SVRCFG_HANDLE;
SMTPS.pSMTPCfg = NULL;
SMTPS.pMsgFile = NULL;
SMTPS.pszFrom = NULL;
SMTPS.pszRcpt = NULL;
SMTPS.pszSendRcpt = NULL;
SMTPS.pszRealRcpt = NULL;
SMTPS.iRcptCount = 0;
SMTPS.iErrorsCount = 0;
SMTPS.iErrorsMax = 0;
SMTPS.iCmdDelay = 0;
SMTPS.ulMaxMsgSize = 0;
SetEmptyString(SMTPS.szMessageID);
SetEmptyString(SMTPS.szDestDomain);
SetEmptyString(SMTPS.szClientFQDN);
SetEmptyString(SMTPS.szClientDomain);
SetEmptyString(SMTPS.szLogonUser);
SetEmptyString(SMTPS.szRejMapName);
SMTPS.ulFlags = 0;
SMTPS.ulSetupFlags = 0;
SMTPS.pszCustMsg = NULL;
SMTPS.pszNoTLSAuths = NULL;
MscSafeGetTmpFile(SMTPS.szMsgFile, sizeof(SMTPS.szMsgFile));
if ((SMTPS.hSvrConfig = SvrGetConfigHandle()) == INVALID_SVRCFG_HANDLE)
return ErrGetErrorCode();
if (SMTPCheckSysResources(SMTPS.hSvrConfig) < 0 ||
SysGetPeerInfo(BSckGetAttachedSocket(hBSock), SMTPS.PeerInfo) < 0 ||
SysGetSockInfo(BSckGetAttachedSocket(hBSock), SMTPS.SockInfo) < 0 ||
SMTPApplyIPProps(SMTPS) < 0) {
ErrorPush();
SvrReleaseConfigHandle(SMTPS.hSvrConfig);
return ErrorPop();
}
/* If the remote IP is not white-listed, do all IP based checks */
if ((SMTPS.ulFlags & SMTPF_WHITE_LISTED) == 0 &&
SMTPDoIPBasedInit(SMTPS, pszSMTPError) < 0) {
ErrorPush();
SvrReleaseConfigHandle(SMTPS.hSvrConfig);
return ErrorPop();
}
/* Get maximum errors count allowed in an SMTP session */
SMTPS.iErrorsMax = SvrGetConfigInt("SMTP-MaxErrors", 0, SMTPS.hSvrConfig);
/* Setup SMTP domain */
char *pszSvrDomain = SvrGetConfigVar(SMTPS.hSvrConfig, "SmtpServerDomain");
char szIP[128] = "???.???.???.???";
if (pszSvrDomain != NULL) {
StrSNCpy(SMTPS.szSvrDomain, pszSvrDomain);
StrSNCpy(SMTPS.szSvrFQDN, pszSvrDomain);
SysFree(pszSvrDomain);
} else {
if (SysGetHostByAddr(SMTPS.SockInfo, SMTPS.szSvrFQDN, sizeof(SMTPS.szSvrFQDN)) < 0)
StrSNCpy(SMTPS.szSvrFQDN, SysInetNToA(SMTPS.SockInfo, szIP,
sizeof(szIP)));
else {
/* Try to get a valid domain from the FQDN */
if (MDomGetClientDomain(SMTPS.szSvrFQDN, SMTPS.szSvrDomain,
sizeof(SMTPS.szSvrDomain) - 1) < 0)
StrSNCpy(SMTPS.szSvrDomain, SMTPS.szSvrFQDN);
}
/* Last attempt, try fetch the "RootDomain" variable ... */
if (IsEmptyString(SMTPS.szSvrDomain)) {
if ((pszSvrDomain = SvrGetConfigVar(SMTPS.hSvrConfig,
"RootDomain")) == NULL) {
SvrReleaseConfigHandle(SMTPS.hSvrConfig);
ErrSetErrorCode(ERR_NO_DOMAIN);
return ERR_NO_DOMAIN;
}
StrSNCpy(SMTPS.szSvrDomain, pszSvrDomain);
SysFree(pszSvrDomain);
}
}
/* Create timestamp */
sprintf(SMTPS.szTimeStamp, "<%lu.%lu@%s>",
(unsigned long) time(NULL), SysGetCurrentThreadId(), SMTPS.szSvrDomain);
if ((SMTPS.pSMTPCfg = SMTPGetConfigCopy(hShbSMTP)) == NULL) {
ErrorPush();
SvrReleaseConfigHandle(SMTPS.hSvrConfig);
return ErrorPop();
}
/* Get maximum accepted message size */
SMTPS.ulMaxMsgSize = 1024 * (unsigned long) SvrGetConfigInt("MaxMessageSize",
0, SMTPS.hSvrConfig);
/* Check if the emission of "X-Auth-User:" is diabled */
if (SvrGetConfigInt("DisableEmitAuthUser", 0, SMTPS.hSvrConfig))
SMTPS.ulSetupFlags |= SMTPF_NOEMIT_AUTH;
/* Try to load specific configuration */
char szConfigName[128] = "";
SysInetNToA(SMTPS.SockInfo, szIP, sizeof(szIP));
SysSNPrintf(szConfigName, sizeof(szConfigName), "SmtpConfig-%s,%d",
szIP, SysGetAddrPort(SMTPS.SockInfo));
char *pszSvrConfig = SvrGetConfigVar(SMTPS.hSvrConfig, szConfigName);
if (pszSvrConfig == NULL) {
SysSNPrintf(szConfigName, sizeof(szConfigName), "SmtpConfig-%s", szIP);
if ((pszSvrConfig = SvrGetConfigVar(SMTPS.hSvrConfig, szConfigName)) == NULL)
pszSvrConfig = SvrGetConfigVar(SMTPS.hSvrConfig, "SmtpConfig");
}
if (pszSvrConfig != NULL) {
SMTPLoadConfig(SMTPS, pszSvrConfig);
SysFree(pszSvrConfig);
}
SMTPS.ulFlags |= SMTPS.ulSetupFlags;
/* Get custom message to append to the SMTP response */
SMTPS.pszCustMsg = SvrGetConfigVar(SMTPS.hSvrConfig, "CustomSMTPMessage");
/* Get custom message to append to the SMTP response */
SMTPS.pszNoTLSAuths = SvrGetConfigVar(SMTPS.hSvrConfig, "SmtpNoTLSAuths");
return 0;
}
static void SMTPClearSession(SMTPSession &SMTPS)
{
if (SMTPS.pMsgFile != NULL)
fclose(SMTPS.pMsgFile), SMTPS.pMsgFile = NULL;
SysRemove(SMTPS.szMsgFile);
if (SMTPS.hSvrConfig != INVALID_SVRCFG_HANDLE)
SvrReleaseConfigHandle(SMTPS.hSvrConfig), SMTPS.hSvrConfig =
INVALID_SVRCFG_HANDLE;
SysFreeNullify(SMTPS.pSMTPCfg);
SysFreeNullify(SMTPS.pszFrom);
SysFreeNullify(SMTPS.pszRcpt);
SysFreeNullify(SMTPS.pszSendRcpt);
SysFreeNullify(SMTPS.pszRealRcpt);
SysFreeNullify(SMTPS.pszCustMsg);
SysFreeNullify(SMTPS.pszNoTLSAuths);
}
static void SMTPResetSession(SMTPSession &SMTPS)
{
SMTPS.ulFlags = SMTPS.ulSetupFlags | (SMTPS.ulFlags & SMTPF_RESET_MASK);
SMTPS.ullMessageID = 0;
SMTPS.iRcptCount = 0;
SetEmptyString(SMTPS.szMessageID);
if (SMTPS.pMsgFile != NULL)
fclose(SMTPS.pMsgFile), SMTPS.pMsgFile = NULL;
SysRemove(SMTPS.szMsgFile);
SetEmptyString(SMTPS.szDestDomain);
SysFreeNullify(SMTPS.pszFrom);
SysFreeNullify(SMTPS.pszRcpt);
SysFreeNullify(SMTPS.pszSendRcpt);
SysFreeNullify(SMTPS.pszRealRcpt);
SMTPS.iSMTPState = (SMTPS.ulFlags & SMTPF_AUTHENTICATED) ? stateAuthenticated:
Min(SMTPS.iSMTPState, stateHelo);
}
static void SMTPFullResetSession(SMTPSession &SMTPS)
{
SMTPS.ulFlags = 0;
SMTPS.iSMTPState = stateInit;
SMTPResetSession(SMTPS);
}
static int SMTPGetUserSmtpPerms(UserInfo *pUI, SVRCFG_HANDLE hSvrConfig, char *pszPerms,
int iMaxPerms)
{
char *pszUserPerms = UsrGetUserInfoVar(pUI, "SmtpPerms");
if (pszUserPerms != NULL) {
StrNCpy(pszPerms, pszUserPerms, iMaxPerms);
SysFree(pszUserPerms);
} else {
/* Match found, get the default permissions */
char *pszDefultPerms = SvrGetConfigVar(hSvrConfig,
"DefaultSmtpPerms", "MRVZ");
if (pszDefultPerms != NULL) {
StrNCpy(pszPerms, pszDefultPerms, iMaxPerms);
SysFree(pszDefultPerms);
} else
SetEmptyString(pszPerms);
}
return 0;
}
static int SMTPApplyUserConfig(SMTPSession &SMTPS, UserInfo *pUI)
{
/* Retrieve and apply permissions */
char *pszValue = NULL;
char szPerms[128] = "";
if (SMTPGetUserSmtpPerms(pUI, SMTPS.hSvrConfig, szPerms, sizeof(szPerms) - 1) < 0)
return ErrGetErrorCode();
SMTPApplyPerms(SMTPS, szPerms);
/* Check "MaxMessageSize" override */
if ((pszValue = UsrGetUserInfoVar(pUI, "MaxMessageSize")) != NULL) {
SMTPS.ulMaxMsgSize = 1024 * (unsigned long) atol(pszValue);
SysFree(pszValue);
}
/* Check if the emission of "X-Auth-User:" is diabled */
if ((pszValue = UsrGetUserInfoVar(pUI, "DisableEmitAuthUser")) != NULL) {
if (atoi(pszValue))
SMTPS.ulFlags |= SMTPF_NOEMIT_AUTH;
else
SMTPS.ulFlags &= ~SMTPF_NOEMIT_AUTH;
SysFree(pszValue);
}
return 0;
}
static int SMTPSendError(BSOCK_HANDLE hBSock, SMTPSession &SMTPS, char const *pszFormat, ...)
{
char *pszBuffer = NULL;
StrVSprint(pszBuffer, pszFormat, pszFormat);
if (pszBuffer == NULL)
return ErrGetErrorCode();
if (SMTPS.pszCustMsg == NULL) {
if (BSckSendString(hBSock, pszBuffer, SMTPS.pSMTPCfg->iTimeout) < 0) {
ErrorPush();
SysFree(pszBuffer);
return ErrorPop();
}
} else {
if (BSckVSendString(hBSock, SMTPS.pSMTPCfg->iTimeout,
"%s - %s", pszBuffer, SMTPS.pszCustMsg) < 0) {
ErrorPush();
SysFree(pszBuffer);
return ErrorPop();
}
}
SysFree(pszBuffer);
/*
* Increase the number of errors we encountered in this session. If the
* maximum number of allowed errors is not zero, and the current number of
* errors exceed the maximum number, we set the state to 'exit' so that the
* SMTP connection will be dropped.
*/
SMTPS.iErrorsCount++;
if (SMTPS.iErrorsMax > 0 && SMTPS.iErrorsCount >= SMTPS.iErrorsMax) {
char szIP[128] = "";
if (SMTPLogEnabled(SMTPS.pThCfg->hThShb, SMTPS.pSMTPCfg))
SMTPLogSession(SMTPS, SMTPS.pszFrom != NULL ? SMTPS.pszFrom: "",
"", "SMTP=EERRS", 0);
SysLogMessage(LOG_LEV_MESSAGE, "SMTP forced exit (too many errors: %d) [%s]\n",
SMTPS.iErrorsCount, SysInetNToA(SMTPS.PeerInfo, szIP, sizeof(szIP)));
SMTPS.iSMTPState = stateExit;
}
return 0;
}
static int SMTPTryPopAuthIpCheck(SMTPSession &SMTPS, char const *pszUser, char const *pszDomain)
{
/* Load user info */
UserInfo *pUI = UsrGetUserByNameOrAlias(pszDomain, pszUser);
if (pUI == NULL)
return ErrGetErrorCode();
/* Perform IP file checking */
if (UPopUserIpCheck(pUI, &SMTPS.PeerInfo, SMTPS.pSMTPCfg->uPopAuthExpireTime) < 0) {
ErrorPush();
UsrFreeUserInfo(pUI);
return ErrorPop();
}
/* Apply user configuration */
if (SMTPApplyUserConfig(SMTPS, pUI) < 0) {
ErrorPush();
UsrFreeUserInfo(pUI);
return ErrorPop();
}
/* If the user did not authenticate, set the logon user token */
if (IsEmptyString(SMTPS.szLogonUser))
UsrGetAddress(pUI, SMTPS.szLogonUser);
UsrFreeUserInfo(pUI);
return 0;
}
static int SMTPCheckMailParams(char const *pszCommand, char **ppszRetDomains,
SMTPSession &SMTPS, char *&pszSMTPError)
{
char const *pszParams = strrchr(pszCommand, '>');
if (pszParams == NULL)
pszParams = pszCommand;
/* Check the SIZE parameter */
if (SMTPS.ulMaxMsgSize != 0) {
char const *pszSize = pszParams;
while ((pszSize = StrIStr(pszSize, " SIZE=")) != NULL) {
pszSize += CStringSize(" SIZE=");
if (isdigit(*pszSize) &&
SMTPS.ulMaxMsgSize < (unsigned long) atol(pszSize)) {
if (SMTPLogEnabled(SMTPS.pThCfg->hThShb, SMTPS.pSMTPCfg))
SMTPLogSession(SMTPS,
(ppszRetDomains[0] !=
NULL) ? ppszRetDomains[0] : "", "",
"SIZE=EBIG",
(unsigned long) atol(pszSize));
pszSMTPError =
SysStrDup("552 Message exceeds fixed maximum message size");
ErrSetErrorCode(ERR_MESSAGE_SIZE);
return ERR_MESSAGE_SIZE;
}
}
}
return 0;
}
static int SMTPCheckReturnPath(char const *pszCommand, char **ppszRetDomains,
SMTPSession &SMTPS, char *&pszSMTPError)
{
int iDomainCount;
char szMailerUser[MAX_ADDR_NAME] = "";
char szMailerDomain[MAX_ADDR_NAME] = "";
if ((iDomainCount = StrStringsCount(ppszRetDomains)) == 0) {
if (!SvrTestConfigFlag("AllowNullSender", true, SMTPS.hSvrConfig)) {
if (SMTPLogEnabled(SMTPS.pThCfg->hThShb, SMTPS.pSMTPCfg))
SMTPLogSession(SMTPS, "", "", "SNDR=EEMPTY", 0);
pszSMTPError = SysStrDup("501 Syntax error in return path");
ErrSetErrorCode(ERR_BAD_RETURN_PATH);
return ERR_BAD_RETURN_PATH;
}
SysFree(SMTPS.pszFrom);
SMTPS.pszFrom = SysStrDup("");
return 0;
}
if (USmtpSplitEmailAddr(ppszRetDomains[0], szMailerUser, szMailerDomain) < 0) {
ErrorPush();
if (SMTPLogEnabled(SMTPS.pThCfg->hThShb, SMTPS.pSMTPCfg))
SMTPLogSession(SMTPS, ppszRetDomains[0], "", "SNDR=ESYNTAX", 0);
pszSMTPError = SysStrDup("501 Syntax error in return path");
return ErrorPop();
}
/* Check mailer domain for DNS/MX entries */
if (!(SMTPS.ulFlags & SMTPF_SNDRCHECK_BYPASS) &&
SvrTestConfigFlag("CheckMailerDomain", false, SMTPS.hSvrConfig) &&
USmtpCheckMailDomain(SMTPS.hSvrConfig, szMailerDomain) < 0) {
ErrorPush();
if (SMTPLogEnabled(SMTPS.pThCfg->hThShb, SMTPS.pSMTPCfg))
SMTPLogSession(SMTPS, ppszRetDomains[0], "", "SNDR=ENODNS", 0);
pszSMTPError = SysStrDup("505 Your domain has no DNS/MX entries");
return ErrorPop();
}
/* Check if mail come from a spammer address ( spam-address.tab ) */
if (USmtpSpamAddressCheck(ppszRetDomains[0]) < 0) {
ErrorPush();
if (SMTPLogEnabled(SMTPS.pThCfg->hThShb, SMTPS.pSMTPCfg))
SMTPLogSession(SMTPS, ppszRetDomains[0], "", "SNDR=ESPAM", 0);
if ((pszSMTPError =
SvrGetConfigVar(SMTPS.hSvrConfig, "SmtpMsgIPBanSpamAddress")) == NULL)
pszSMTPError = SysStrDup("504 You are registered as spammer");
return ErrorPop();
}
/* Check SMTP after POP3 authentication */
if (SvrTestConfigFlag("EnableAuthSMTP-POP3", true, SMTPS.hSvrConfig))
{
if (SMTPTryPopAuthIpCheck(SMTPS, szMailerUser, szMailerDomain)) /* [i_a] */
{
pszSMTPError = SysStrDup("504 You did not authenticate properly using SMTP-after-POP3");
SysLogMessage(LOG_LEV_DEBUG, "mail user '%s@%s' did not authenticate properly using SMTP-after-POP3: %d: %s\n",
szMailerUser, szMailerDomain, ErrGetErrorCode(), ErrGetErrorString());
return ErrGetErrorCode();
}
}
/* Check extended mail from parameters */
if (SMTPCheckMailParams(pszCommand, ppszRetDomains, SMTPS, pszSMTPError) < 0)
return ErrGetErrorCode();
/* Setup From string */
SysFree(SMTPS.pszFrom);
SMTPS.pszFrom = SysStrDup(ppszRetDomains[0]);
return 0;
}
static int SMTPAddMessageInfo(SMTPSession &SMTPS)
{
return USmtpAddMessageInfo(SMTPS.pMsgFile, SMTPS.szClientDomain, SMTPS.PeerInfo,
SMTPS.szSvrDomain, SMTPS.SockInfo, SMTP_SERVER_NAME);
}
static int SMTPHandleCmd_MAIL(char const *pszCommand, BSOCK_HANDLE hBSock, SMTPSession &SMTPS)
{
if (SMTPS.iSMTPState != stateHelo && SMTPS.iSMTPState != stateAuthenticated) {
SMTPResetSession(SMTPS);
SMTPSendError(hBSock, SMTPS, "503 Bad sequence of commands");
ErrSetErrorCode(ERR_SMTP_BAD_CMD_SEQUENCE);
return ERR_SMTP_BAD_CMD_SEQUENCE;
}
/* Do we need to be in TLS mode for this session? */
if ((SMTPS.ulFlags & SMTPF_WANT_TLS) && !(SMTPS.ulFlags & SMTPF_EASE_TLS) &&
strcmp(BSckBioName(hBSock), BSSL_BIO_NAME) != 0) {
SMTPSendError(hBSock, SMTPS,