-
Notifications
You must be signed in to change notification settings - Fork 0
/
LMAILSvr.cpp
525 lines (408 loc) · 13.6 KB
/
LMAILSvr.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
/*
* 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 "MessQueue.h"
#include "SvrUtils.h"
#include "UsrUtils.h"
#include "SMTPUtils.h"
#include "SMAILUtils.h"
#include "ExtAliases.h"
#include "UsrMailList.h"
#include "MailConfig.h"
#include "LMAILSvr.h"
#include "AppDefines.h"
#include "MailSvr.h"
#define LMAIL_SERVER_NAME "[" APP_NAME_VERSION_STR " LMAIL Server]"
#define LOCAL_SPOOL_DIR "local"
#define LMAIL_LOG_FILE "lmail"
static int LMAILThreadCountAdd(long lCount, SHB_HANDLE hShbLMAIL, LMAILConfig *pLMAILCfg = NULL);
static int LMAILLogEnabled(SHB_HANDLE hShbLMAIL, LMAILConfig *pLMAILCfg = NULL);
char *LMAILGetSpoolDir(char *pszSpoolPath, size_t sMaxPath)
{
SvrGetSpoolDir(pszSpoolPath, sMaxPath);
AppendSlash(pszSpoolPath);
StrNCat(pszSpoolPath, LOCAL_SPOOL_DIR, sMaxPath);
return pszSpoolPath;
}
static LMAILConfig *LMAILGetConfigCopy(SHB_HANDLE hShbLMAIL)
{
LMAILConfig *pLMAILCfg = (LMAILConfig *) ShbLock(hShbLMAIL);
if (pLMAILCfg == NULL)
return NULL;
LMAILConfig *pNewLMAILCfg = (LMAILConfig *) SysAlloc(sizeof(LMAILConfig));
if (pNewLMAILCfg != NULL)
memcpy(pNewLMAILCfg, pLMAILCfg, sizeof(LMAILConfig));
ShbUnlock(hShbLMAIL);
return pNewLMAILCfg;
}
static int LMAILThreadCountAdd(long lCount, SHB_HANDLE hShbLMAIL, LMAILConfig *pLMAILCfg)
{
int iDoUnlock = 0;
if (pLMAILCfg == NULL) {
if ((pLMAILCfg = (LMAILConfig *) ShbLock(hShbLMAIL)) == NULL)
return ErrGetErrorCode();
++iDoUnlock;
}
pLMAILCfg->lThreadCount += lCount;
if (iDoUnlock)
ShbUnlock(hShbLMAIL);
return 0;
}
static int LMAILLogEnabled(SHB_HANDLE hShbLMAIL, LMAILConfig *pLMAILCfg)
{
int iDoUnlock = 0;
if (pLMAILCfg == NULL) {
if ((pLMAILCfg = (LMAILConfig *) ShbLock(hShbLMAIL)) == NULL)
return ErrGetErrorCode();
++iDoUnlock;
}
unsigned long ulFlags = pLMAILCfg->ulFlags;
if (iDoUnlock)
ShbUnlock(hShbLMAIL);
return (ulFlags & LMAILF_LOG_ENABLED) ? 1 : 0;
}
static int LMAILGetFilesSnapShot(LMAILConfig *pLMAILCfg, long lThreadId, char *pszSSFileName,
int iMaxSSFileName)
{
char szSpoolDir[SYS_MAX_PATH];
LMAILGetSpoolDir(szSpoolDir, sizeof(szSpoolDir));
/* Share lock local spool directory */
char szResLock[SYS_MAX_PATH];
RLCK_HANDLE hResLock = RLckLockSH(CfgGetBasedPath(szSpoolDir, szResLock,
sizeof(szResLock)));
if (hResLock == INVALID_RLCK_HANDLE)
return ErrGetErrorCode();
UsrGetTmpFile(NULL, pszSSFileName, iMaxSSFileName);
FILE *pSSFile = fopen(pszSSFileName, "wb");
if (pSSFile == NULL) {
ErrorPush();
RLckUnlockSH(hResLock);
return ErrorPop();
}
int iFileCount = 0;
char szSpoolFileName[SYS_MAX_PATH];
FSCAN_HANDLE hFileScan = MscFirstFile(szSpoolDir, 0, szSpoolFileName,
sizeof(szSpoolFileName));
if (hFileScan != INVALID_FSCAN_HANDLE) {
do {
unsigned long ulHashValue =
MscHashString(szSpoolFileName, strlen(szSpoolFileName));
if ((ulHashValue % (unsigned long) pLMAILCfg->lNumThreads) ==
(unsigned long) lThreadId) {
fprintf(pSSFile, "%s\r\n", szSpoolFileName);
++iFileCount;
}
} while (MscNextFile(hFileScan, szSpoolFileName, sizeof(szSpoolFileName)));
MscCloseFindFile(hFileScan);
}
fclose(pSSFile);
RLckUnlockSH(hResLock);
if (iFileCount == 0) {
SysRemove(pszSSFileName);
SetEmptyString(pszSSFileName);
ErrSetErrorCode(ERR_NO_LOCAL_SPOOL_FILES);
return ERR_NO_LOCAL_SPOOL_FILES;
}
return 0;
}
static int LMAILRemoveProcessed(LMAILConfig *pLMAILCfg, char const *pszListFileName)
{
char szSpoolDir[SYS_MAX_PATH];
LMAILGetSpoolDir(szSpoolDir, sizeof(szSpoolDir));
/* Share lock local spool directory */
char szResLock[SYS_MAX_PATH];
RLCK_HANDLE hResLock = RLckLockEX(CfgGetBasedPath(szSpoolDir, szResLock,
sizeof(szResLock)));
if (hResLock == INVALID_RLCK_HANDLE)
return ErrGetErrorCode();
FILE *pSSFile = fopen(pszListFileName, "rb");
if (pSSFile == NULL) {
ErrorPush();
RLckUnlockEX(hResLock);
return ErrorPop();
}
char szSpoolFileName[SYS_MAX_PATH];
while (MscGetString(pSSFile, szSpoolFileName, sizeof(szSpoolFileName) - 1) != NULL) {
char szSpoolFilePath[SYS_MAX_PATH];
sprintf(szSpoolFilePath, "%s%s%s", szSpoolDir, SYS_SLASH_STR, szSpoolFileName);
CheckRemoveFile(szSpoolFilePath);
}
fclose(pSSFile);
RLckUnlockEX(hResLock);
return 0;
}
static int LMAILLogMessage(char const *pszMailFile, char const *pszSMTPDomain,
char const *pszMessageID)
{
char szTime[256];
MscGetTimeNbrString(szTime, sizeof(szTime) - 1);
char szLocalFile[SYS_MAX_PATH];
MscGetFileName(pszMailFile, szLocalFile);
RLCK_HANDLE hResLock = RLckLockEX(SVR_LOGS_DIR SYS_SLASH_STR LMAIL_LOG_FILE);
if (hResLock == INVALID_RLCK_HANDLE)
return ErrGetErrorCode();
MscFileLog(LMAIL_LOG_FILE,
"\"%s\""
"\t\"%s\""
"\t\"%s\"" "\t\"%s\"" "\n", pszSMTPDomain, szLocalFile, pszMessageID, szTime);
RLckUnlockEX(hResLock);
return 0;
}
static int LMAILAddReceived(FILE *pSpoolFile, char const *pszSMTPDomain,
char const *pszMailFrom, char const *pszRcptTo, char const *pszTime)
{
int iError;
char szFrom[MAX_SMTP_ADDRESS];
char szRcpt[MAX_SMTP_ADDRESS];
if ((iError = USmlParseAddress(pszMailFrom, NULL, 0, szFrom,
sizeof(szFrom) - 1)) < 0 &&
iError != ERR_EMPTY_ADDRESS)
return iError;
if (USmlParseAddress(pszRcptTo, NULL, 0, szRcpt, sizeof(szRcpt) - 1) < 0)
return ErrGetErrorCode();
/* Add "Received:" tag */
fprintf(pSpoolFile,
"Received: from /spool/local\r\n"
"\tby %s with %s\r\n"
"\tfor <%s> from <%s>;\r\n"
"\t%s\r\n", pszSMTPDomain, LMAIL_SERVER_NAME, szRcpt, szFrom, pszTime);
return 0;
}
static int LMAILSubmitLocalFile(LMAILConfig *pLMAILCfg, const char *pszMailFile,
long lThreadId, char const *pszSMTPDomain)
{
FILE *pMailFile = fopen(pszMailFile, "rb");
if (pMailFile == NULL) {
ErrSetErrorCode(ERR_FILE_OPEN);
return ERR_FILE_OPEN;
}
/* Get a message ID */
SYS_UINT64 uMessageID;
char szMessageID[128];
if (SvrGetMessageID(&uMessageID) < 0) {
ErrorPush();
fclose(pMailFile);
return ErrorPop();
}
sprintf(szMessageID, "L" SYS_LLX_FMT, uMessageID);
/* Get current time */
char szTime[256];
MscGetTimeStr(szTime, sizeof(szTime) - 1);
/* Log current opeartion */
if (LMAILLogEnabled(SHB_INVALID_HANDLE, pLMAILCfg))
LMAILLogMessage(pszMailFile, pszSMTPDomain, szMessageID);
/* Search mail data start */
char szSpoolLine[MAX_SPOOL_LINE];
while (MscGetString(pMailFile, szSpoolLine, sizeof(szSpoolLine) - 1) != NULL)
if (IsEmptyString(szSpoolLine))
break;
if (feof(pMailFile)) {
fclose(pMailFile);
ErrSetErrorCode(ERR_INVALID_SPOOL_FILE, pszMailFile);
return ERR_INVALID_SPOOL_FILE;
}
/* Get the offset at which the message data begin and rewind the file */
SYS_OFF_T llMsgOffset = Sys_ftell(pMailFile);
rewind(pMailFile);
/* Read "MAIL FROM:" (1th row of the smtp-mail file) */
char szMailFrom[MAX_SPOOL_LINE];
if (MscGetString(pMailFile, szMailFrom, sizeof(szMailFrom) - 1) == NULL ||
StrINComp(szMailFrom, MAIL_FROM_STR) != 0) {
fclose(pMailFile);
ErrSetErrorCode(ERR_INVALID_SPOOL_FILE, pszMailFile);
return ERR_INVALID_SPOOL_FILE;
}
/* Read "RCPT TO:" (2nd[,...] row(s) of the local-mail file) */
while (MscGetString(pMailFile, szSpoolLine, sizeof(szSpoolLine) - 1) != NULL &&
!IsEmptyString(szSpoolLine)) {
/* Get message handle */
QMSG_HANDLE hMessage = QueCreateMessage(hSpoolQueue);
if (hMessage == INVALID_QMSG_HANDLE) {
ErrorPush();
fclose(pMailFile);
return ErrorPop();
}
char szQueueFilePath[SYS_MAX_PATH];
QueGetFilePath(hSpoolQueue, hMessage, szQueueFilePath);
FILE *pSpoolFile = fopen(szQueueFilePath, "wb");
if (pSpoolFile == NULL) {
QueCleanupMessage(hSpoolQueue, hMessage);
QueCloseMessage(hSpoolQueue, hMessage);
fclose(pMailFile);
ErrSetErrorCode(ERR_FILE_CREATE);
return ERR_FILE_CREATE;
}
/* Write info line */
USmtpWriteInfoLine(pSpoolFile, LOCAL_ADDRESS_SQB ":0",
LOCAL_ADDRESS_SQB ":0", szTime);
/* Write SMTP domain */
fprintf(pSpoolFile, "%s\r\n", pszSMTPDomain);
/* Write message ID */
fprintf(pSpoolFile, "%s\r\n", szMessageID);
/* Write "MAIL FROM:" */
fprintf(pSpoolFile, "%s\r\n", szMailFrom);
/* Write "RCPT TO:" */
fprintf(pSpoolFile, "%s\r\n", szSpoolLine);
/* Write SPOOL_FILE_DATA_START */
fprintf(pSpoolFile, "%s\r\n", SPOOL_FILE_DATA_START);
/* Write "Received:" tag */
LMAILAddReceived(pSpoolFile, pszSMTPDomain, szMailFrom, szSpoolLine, szTime);
/* Write mail data, saving and restoring the current file pointer */
SYS_OFF_T llCurrOffset = Sys_ftell(pMailFile);
if (MscCopyFile(pSpoolFile, pMailFile, llMsgOffset,
(SYS_OFF_T) -1) < 0) {
ErrorPush();
fclose(pSpoolFile);
QueCleanupMessage(hSpoolQueue, hMessage);
QueCloseMessage(hSpoolQueue, hMessage);
fclose(pMailFile);
return ErrorPop();
}
if (SysFileSync(pSpoolFile) < 0) {
ErrorPush();
fclose(pSpoolFile);
QueCleanupMessage(hSpoolQueue, hMessage);
QueCloseMessage(hSpoolQueue, hMessage);
fclose(pMailFile);
return ErrorPop();
}
if (fclose(pSpoolFile)) {
QueCleanupMessage(hSpoolQueue, hMessage);
QueCloseMessage(hSpoolQueue, hMessage);
fclose(pMailFile);
ErrSetErrorCode(ERR_FILE_WRITE, szQueueFilePath);
return ERR_FILE_WRITE;
}
Sys_fseek(pMailFile, llCurrOffset, SEEK_SET);
/* Transfer file to the spool */
if (QueCommitMessage(hSpoolQueue, hMessage) < 0) {
ErrorPush();
QueCleanupMessage(hSpoolQueue, hMessage);
QueCloseMessage(hSpoolQueue, hMessage);
fclose(pMailFile);
return ErrorPop();
}
}
fclose(pMailFile);
return 0;
}
static int LMAILProcessList(LMAILConfig *pLMAILCfg, long lThreadId, char const *pszSSFileName)
{
char szSpoolDir[SYS_MAX_PATH];
LMAILGetSpoolDir(szSpoolDir, sizeof(szSpoolDir));
/* Retrieve SMTP domain */
SVRCFG_HANDLE hSvrConfig = SvrGetConfigHandle();
if (hSvrConfig == INVALID_SVRCFG_HANDLE)
return ErrGetErrorCode();
char szSMTPDomain[MAX_HOST_NAME] = "localdomain";
char *pszDefDomain = SvrGetConfigVar(hSvrConfig, "RootDomain");
if (pszDefDomain != NULL) {
StrSNCpy(szSMTPDomain, pszDefDomain);
SysFree(pszDefDomain);
}
SvrReleaseConfigHandle(hSvrConfig);
FILE *pSSFile;
char szSpoolFileName[SYS_MAX_PATH];
if ((pSSFile = fopen(pszSSFileName, "rb")) == NULL)
return ErrGetErrorCode();
while (MscGetString(pSSFile, szSpoolFileName, sizeof(szSpoolFileName) - 1) != NULL) {
char szSpoolFilePath[SYS_MAX_PATH];
sprintf(szSpoolFilePath, "%s%s%s", szSpoolDir, SYS_SLASH_STR, szSpoolFileName);
if (LMAILSubmitLocalFile(pLMAILCfg, szSpoolFilePath, lThreadId, szSMTPDomain) < 0) {
SysLogMessage(LOG_LEV_ERROR, "LMAIL [%02ld] error (\"%s\"): %s\n",
lThreadId, ErrGetErrorString(), szSpoolFilePath);
} else {
SysLogMessage(LOG_LEV_MESSAGE, "LMAIL [%02ld] file processed: %s\n",
lThreadId, szSpoolFilePath);
}
}
fclose(pSSFile);
return 0;
}
static int LMAILProcessLocalSpool(SHB_HANDLE hShbLMAIL, long lThreadId)
{
LMAILConfig *pLMAILCfg;
char szSSFileName[SYS_MAX_PATH];
if ((pLMAILCfg = LMAILGetConfigCopy(hShbLMAIL)) == NULL)
return ErrGetErrorCode();
if (LMAILGetFilesSnapShot(pLMAILCfg, lThreadId, szSSFileName,
sizeof(szSSFileName)) < 0) {
ErrorPush();
SysFree(pLMAILCfg);
return ErrorPop();
}
if (LMAILProcessList(pLMAILCfg, lThreadId, szSSFileName) < 0) {
ErrorPush();
SysRemove(szSSFileName);
SysFree(pLMAILCfg);
return ErrorPop();
}
LMAILRemoveProcessed(pLMAILCfg, szSSFileName);
SysRemove(szSSFileName);
SysFree(pLMAILCfg);
return 0;
}
unsigned int LMAILThreadProc(void *pThreadData)
{
LMAILConfig *pLMAILCfg = (LMAILConfig *) ShbLock(hShbLMAIL);
if (pLMAILCfg == NULL) {
ErrorPush();
SysLogMessage(LOG_LEV_ERROR, "%s\n", ErrGetErrorString());
return ErrorPop();
}
/* Get thread id and sleep timeout */
int iSleepTimeout = pLMAILCfg->iSleepTimeout;
long lThreadId = pLMAILCfg->lThreadCount;
/* Increase thread count */
LMAILThreadCountAdd(+1, hShbLMAIL, pLMAILCfg);
ShbUnlock(hShbLMAIL);
SysLogMessage(LOG_LEV_MESSAGE, "LMAIL thread [%02ld] started\n", lThreadId);
for (;;) {
/* Check shutdown condition */
pLMAILCfg = (LMAILConfig *) ShbLock(hShbLMAIL);
if (pLMAILCfg == NULL || pLMAILCfg->ulFlags & LMAILF_STOP_SERVER) {
SysLogMessage(LOG_LEV_MESSAGE, "LMAIL thread [%02ld] exiting\n",
lThreadId);
if (pLMAILCfg != NULL)
ShbUnlock(hShbLMAIL);
break;
}
ShbUnlock(hShbLMAIL);
/* Process local spool files */
int iProcessResult = LMAILProcessLocalSpool(hShbLMAIL, lThreadId);
if (iProcessResult == ERR_NO_LOCAL_SPOOL_FILES)
SysSleep(iSleepTimeout);
}
/* Decrease thread count */
LMAILThreadCountAdd(-1, hShbLMAIL);
SysLogMessage(LOG_LEV_MESSAGE, "LMAIL thread [%02ld] stopped\n", lThreadId);
return 0;
}