forked from GerHobbelt/xmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AliasDomain.cpp
535 lines (411 loc) · 15 KB
/
AliasDomain.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
/*
* 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 "ShBlocks.h"
#include "ResLocks.h"
#include "StrUtils.h"
#include "SList.h"
#include "BuffSock.h"
#include "MailConfig.h"
#include "MessQueue.h"
#include "MailSvr.h"
#include "MiscUtils.h"
#include "SvrUtils.h"
#include "POP3GwLink.h"
#include "ExtAliases.h"
#include "UsrUtils.h"
#include "UsrAuth.h"
#include "TabIndex.h"
#include "MailDomains.h"
#include "AliasDomain.h"
#define ADOMAIN_FILE "aliasdomain.tab"
#define WILD_ADOMAIN_HASH 0
#define ADOMAIN_LINE_MAX 512
struct ADomainScanData {
char szTmpDBFile[SYS_MAX_PATH];
FILE *pDBFile;
char **ppszStrings;
};
static int iIdxADomain_Alias[] = {
adomADomain,
INDEX_SEQUENCE_TERMINATOR
};
static bool ADomIsWildAlias(char const *pszAlias)
{
return (strchr(pszAlias, '*') != NULL) || (strchr(pszAlias, '?') != NULL);
}
static int ADomCalcAliasHash(char const *const *ppszTabTokens, int const *piFieldsIdx,
unsigned long *pulHashVal, bool bCaseSens)
{
/* This will group wild alias ( * ? ) */
int iFieldsCount = StrStringsCount(ppszTabTokens);
if (iFieldsCount > adomADomain && ADomIsWildAlias(ppszTabTokens[adomADomain])) {
*pulHashVal = WILD_ADOMAIN_HASH;
return 0;
}
return TbixCalculateHash(ppszTabTokens, piFieldsIdx, pulHashVal, bCaseSens);
}
static char *ADomGetADomainFilePath(char *pszADomainFilePath, int iMaxPath)
{
CfgGetRootPath(pszADomainFilePath, iMaxPath);
StrNCat(pszADomainFilePath, ADOMAIN_FILE, iMaxPath);
SysLogMessage(LOG_LEV_DEBUG, "Going to look at config file: '%s'\n", pszADomainFilePath);
return pszADomainFilePath;
}
int ADomCheckDomainsIndexes(void)
{
char szADomainFilePath[SYS_MAX_PATH] = "";
ADomGetADomainFilePath(szADomainFilePath, sizeof(szADomainFilePath));
/* Align RmtDomain-RmtName index */
if (TbixCheckIndex(szADomainFilePath, iIdxADomain_Alias, false, ADomCalcAliasHash) < 0)
return ErrGetErrorCode();
return 0;
}
static int ADomRebuildADomainIndexes(char const *pszADomainFilePath)
{
/* Rebuild RmtDomain-RmtName index */
if (TbixCreateIndex(pszADomainFilePath, iIdxADomain_Alias, false, ADomCalcAliasHash) < 0)
return ErrGetErrorCode();
return 0;
}
static int ADomLookupDomainLK(const char *pszADomainFilePath, const char *pszADomain,
char *pszDomain, bool bWildMatch)
{
/* Lookup record using the specified index ( lookup precise aliases ) */
char **ppszTabTokens = TbixLookup(pszADomainFilePath, iIdxADomain_Alias, false,
pszADomain,
NULL);
if (ppszTabTokens != NULL) {
if (pszDomain != NULL)
StrNCpy(pszDomain, ppszTabTokens[adomDomain], MAX_HOST_NAME);
StrFreeStrings(ppszTabTokens);
return 1;
}
/* We can stop here if wild alias matching is not required */
if (!bWildMatch)
return 0;
/* Lookup record using the specified index ( lookup wild aliases grouped */
/* under WILD_ADOMAIN_HASH hash key ) */
unsigned long ulLkHVal = WILD_ADOMAIN_HASH;
INDEX_HANDLE hIndexLookup = TbixOpenHandle(pszADomainFilePath, iIdxADomain_Alias,
&ulLkHVal, 1);
if (hIndexLookup != INVALID_INDEX_HANDLE) {
for (ppszTabTokens = TbixFirstRecord(hIndexLookup); ppszTabTokens != NULL;
ppszTabTokens = TbixNextRecord(hIndexLookup)) {
int iFieldsCount = StrStringsCount(ppszTabTokens);
if (iFieldsCount >= adomMax &&
StrIWildMatch(pszADomain, ppszTabTokens[adomADomain])) {
if (pszDomain != NULL)
StrNCpy(pszDomain, ppszTabTokens[adomDomain],
MAX_HOST_NAME);
StrFreeStrings(ppszTabTokens);
TbixCloseHandle(hIndexLookup);
return 1;
}
StrFreeStrings(ppszTabTokens);
}
TbixCloseHandle(hIndexLookup);
}
return 0;
}
int ADomLookupDomain(const char *pszADomain, char *pszDomain, bool bWildMatch)
{
char szADomainFilePath[SYS_MAX_PATH] = "";
ADomGetADomainFilePath(szADomainFilePath, sizeof(szADomainFilePath));
char szResLock[SYS_MAX_PATH] = "";
RLCK_HANDLE hResLock = RLckLockSH(CfgGetBasedPath(szADomainFilePath, szResLock,
sizeof(szResLock)));
if (hResLock == INVALID_RLCK_HANDLE)
return 0;
int iLookupResult = ADomLookupDomainLK(szADomainFilePath, pszADomain,
pszDomain, bWildMatch);
RLckUnlockSH(hResLock);
return iLookupResult;
}
int ADomAddADomain(char const *pszADomain, char const *pszDomain)
{
char szADomainFilePath[SYS_MAX_PATH] = "";
ADomGetADomainFilePath(szADomainFilePath, sizeof(szADomainFilePath));
char szResLock[SYS_MAX_PATH] = "";
RLCK_HANDLE hResLock = RLckLockEX(CfgGetBasedPath(szADomainFilePath, szResLock,
sizeof(szResLock)));
if (hResLock == INVALID_RLCK_HANDLE)
return ErrGetErrorCode();
FILE *pDomainsFile = fopen(szADomainFilePath, "r+t");
if (pDomainsFile == NULL) {
RLckUnlockEX(hResLock);
ErrSetErrorCode(ERR_ADOMAIN_FILE_NOT_FOUND, szADomainFilePath);
return ERR_ADOMAIN_FILE_NOT_FOUND;
}
char szADomainLine[ADOMAIN_LINE_MAX] = "";
while (MscGetConfigLine(szADomainLine, sizeof(szADomainLine) - 1, pDomainsFile) != NULL) { /* [i_a] */
char **ppszStrings = StrGetTabLineStrings(szADomainLine);
if (ppszStrings == NULL)
continue;
int iFieldsCount = StrStringsCount(ppszStrings);
if (iFieldsCount >= adomMax &&
stricmp(pszADomain, ppszStrings[adomADomain]) == 0) {
StrFreeStrings(ppszStrings);
fclose(pDomainsFile);
RLckUnlockEX(hResLock);
ErrSetErrorCode(ERR_ADOMAIN_EXIST);
return ERR_ADOMAIN_EXIST;
}
StrFreeStrings(ppszStrings);
}
fseek(pDomainsFile, 0, SEEK_END);
fprintf(pDomainsFile, "\"%s\"\t\"%s\"\n", pszADomain, pszDomain);
fclose(pDomainsFile);
/* Rebuild indexes */
if (ADomRebuildADomainIndexes(szADomainFilePath) < 0) {
ErrorPush();
RLckUnlockEX(hResLock);
return ErrorPop();
}
RLckUnlockEX(hResLock);
return 0;
}
int ADomRemoveADomain(char const *pszADomain)
{
char szADomainFilePath[SYS_MAX_PATH] = "";
char szTmpFile[SYS_MAX_PATH] = "";
ADomGetADomainFilePath(szADomainFilePath, sizeof(szADomainFilePath));
UsrGetTmpFile(NULL, szTmpFile, sizeof(szTmpFile));
char szResLock[SYS_MAX_PATH] = "";
RLCK_HANDLE hResLock = RLckLockEX(CfgGetBasedPath(szADomainFilePath, szResLock,
sizeof(szResLock)));
if (hResLock == INVALID_RLCK_HANDLE) {
ErrorPush();
SysRemove(szTmpFile);
return ErrorPop();
}
FILE *pDomainsFile = fopen(szADomainFilePath, "rt");
if (pDomainsFile == NULL) {
RLckUnlockEX(hResLock);
SysRemove(szTmpFile);
ErrSetErrorCode(ERR_ADOMAIN_FILE_NOT_FOUND, szADomainFilePath);
return ERR_ADOMAIN_FILE_NOT_FOUND;
}
FILE *pTmpFile = fopen(szTmpFile, "wt");
if (pTmpFile == NULL) {
fclose(pDomainsFile);
RLckUnlockEX(hResLock);
SysRemove(szTmpFile);
ErrSetErrorCode(ERR_FILE_CREATE, szTmpFile);
return ERR_FILE_CREATE;
}
int iADomainFound = 0;
char szADomainLine[ADOMAIN_LINE_MAX] = "";
/* [i_a] every config file supports comments */
while (MscFGets(szADomainLine, sizeof(szADomainLine) - 1, pDomainsFile) != NULL) {
if (szADomainLine[0] == TAB_COMMENT_CHAR) {
fprintf(pTmpFile, "%s\n", szADomainLine);
continue;
}
char **ppszStrings = StrGetTabLineStrings(szADomainLine);
if (ppszStrings == NULL) {
fprintf(pTmpFile, "%s\n", szADomainLine);
continue;
}
int iFieldsCount = StrStringsCount(ppszStrings);
if (iFieldsCount >= adomMax &&
stricmp(pszADomain, ppszStrings[adomADomain]) == 0) {
++iADomainFound;
} else
fprintf(pTmpFile, "%s\n", szADomainLine);
StrFreeStrings(ppszStrings);
}
fclose(pDomainsFile);
fclose(pTmpFile);
if (iADomainFound == 0) {
SysRemove(szTmpFile);
RLckUnlockEX(hResLock);
ErrSetErrorCode(ERR_ADOMAIN_NOT_FOUND);
return ERR_ADOMAIN_NOT_FOUND;
}
if (MscMoveFile(szTmpFile, szADomainFilePath) < 0) {
ErrorPush();
RLckUnlockEX(hResLock);
return ErrorPop();
}
/* Rebuild indexes */
if (ADomRebuildADomainIndexes(szADomainFilePath) < 0) {
ErrorPush();
RLckUnlockEX(hResLock);
return ErrorPop();
}
RLckUnlockEX(hResLock);
return 0;
}
int ADomRemoveLinkedDomains(char const *pszDomain)
{
char szADomainFilePath[SYS_MAX_PATH] = "";
char szTmpFile[SYS_MAX_PATH] = "";
ADomGetADomainFilePath(szADomainFilePath, sizeof(szADomainFilePath));
UsrGetTmpFile(NULL, szTmpFile, sizeof(szTmpFile));
char szResLock[SYS_MAX_PATH] = "";
RLCK_HANDLE hResLock = RLckLockEX(CfgGetBasedPath(szADomainFilePath, szResLock,
sizeof(szResLock)));
if (hResLock == INVALID_RLCK_HANDLE) {
ErrorPush();
SysRemove(szTmpFile);
return ErrorPop();
}
FILE *pDomainsFile = fopen(szADomainFilePath, "rt");
if (pDomainsFile == NULL) {
RLckUnlockEX(hResLock);
SysRemove(szTmpFile);
ErrSetErrorCode(ERR_ADOMAIN_FILE_NOT_FOUND, szADomainFilePath);
return ERR_ADOMAIN_FILE_NOT_FOUND;
}
FILE *pTmpFile = fopen(szTmpFile, "wt");
if (pTmpFile == NULL) {
fclose(pDomainsFile);
RLckUnlockEX(hResLock);
SysRemove(szTmpFile);
ErrSetErrorCode(ERR_FILE_CREATE, szTmpFile);
return ERR_FILE_CREATE;
}
int iDomainFound = 0;
char szADomainLine[ADOMAIN_LINE_MAX] = "";
/* [i_a] every config file supports comments */
while (MscFGets(szADomainLine, sizeof(szADomainLine) - 1, pDomainsFile) != NULL) {
if (szADomainLine[0] == TAB_COMMENT_CHAR) {
fprintf(pTmpFile, "%s\n", szADomainLine);
continue;
}
char **ppszStrings = StrGetTabLineStrings(szADomainLine);
if (ppszStrings == NULL) {
fprintf(pTmpFile, "%s\n", szADomainLine);
continue;
}
int iFieldsCount = StrStringsCount(ppszStrings);
if (iFieldsCount >= adomMax &&
stricmp(pszDomain, ppszStrings[adomDomain]) == 0) {
++iDomainFound;
} else
fprintf(pTmpFile, "%s\n", szADomainLine);
StrFreeStrings(ppszStrings);
}
fclose(pDomainsFile);
fclose(pTmpFile);
if (iDomainFound == 0) {
SysRemove(szTmpFile);
RLckUnlockEX(hResLock);
return 0;
}
if (MscMoveFile(szTmpFile, szADomainFilePath) < 0) {
ErrorPush();
RLckUnlockEX(hResLock);
return ErrorPop();
}
/* Rebuild indexes */
if (ADomRebuildADomainIndexes(szADomainFilePath) < 0) {
ErrorPush();
RLckUnlockEX(hResLock);
return ErrorPop();
}
RLckUnlockEX(hResLock);
return 0;
}
int ADomGetADomainFileSnapShot(const char *pszFileName)
{
char szADomainFilePath[SYS_MAX_PATH] = "";
ADomGetADomainFilePath(szADomainFilePath, sizeof(szADomainFilePath));
char szResLock[SYS_MAX_PATH] = "";
RLCK_HANDLE hResLock = RLckLockSH(CfgGetBasedPath(szADomainFilePath, szResLock,
sizeof(szResLock)));
if (hResLock == INVALID_RLCK_HANDLE)
return ErrGetErrorCode();
if (MscCopyFile(pszFileName, szADomainFilePath) < 0) {
ErrorPush();
RLckUnlockSH(hResLock);
return ErrorPop();
}
RLckUnlockSH(hResLock);
return 0;
}
ADOMAIN_HANDLE ADomOpenDB(void)
{
ADomainScanData *pDSD = (ADomainScanData *) SysAlloc(sizeof(ADomainScanData));
if (pDSD == NULL)
return INVALID_ADOMAIN_HANDLE;
UsrGetTmpFile(NULL, pDSD->szTmpDBFile, sizeof(pDSD->szTmpDBFile));
if (ADomGetADomainFileSnapShot(pDSD->szTmpDBFile) < 0) {
CheckRemoveFile(pDSD->szTmpDBFile);
SysFree(pDSD);
return INVALID_ADOMAIN_HANDLE;
}
if ((pDSD->pDBFile = fopen(pDSD->szTmpDBFile, "rt")) == NULL) {
SysRemove(pDSD->szTmpDBFile);
SysFree(pDSD);
return INVALID_ADOMAIN_HANDLE;
}
pDSD->ppszStrings = NULL;
return (ADOMAIN_HANDLE) pDSD;
}
void ADomCloseDB(ADOMAIN_HANDLE hDomainsDB)
{
ADomainScanData *pDSD = (ADomainScanData *) hDomainsDB;
fclose(pDSD->pDBFile);
SysRemove(pDSD->szTmpDBFile);
if (pDSD->ppszStrings != NULL)
StrFreeStrings(pDSD->ppszStrings);
SysFree(pDSD);
}
char const *const *ADomGetFirstDomain(ADOMAIN_HANDLE hDomainsDB)
{
ADomainScanData *pDSD = (ADomainScanData *) hDomainsDB;
rewind(pDSD->pDBFile);
if (pDSD->ppszStrings != NULL)
StrFreeStrings(pDSD->ppszStrings), pDSD->ppszStrings = NULL;
char szADomainLine[ADOMAIN_LINE_MAX] = "";
while (MscGetConfigLine(szADomainLine, sizeof(szADomainLine) - 1, pDSD->pDBFile) != NULL) { /* [i_a] */
char **ppszStrings = StrGetTabLineStrings(szADomainLine);
if (ppszStrings == NULL)
continue;
int iFieldsCount = StrStringsCount(ppszStrings);
if (iFieldsCount >= adomMax)
return pDSD->ppszStrings = ppszStrings;
StrFreeStrings(ppszStrings);
}
return NULL;
}
char const *const *ADomGetNextDomain(ADOMAIN_HANDLE hDomainsDB)
{
ADomainScanData *pDSD = (ADomainScanData *) hDomainsDB;
if (pDSD->ppszStrings != NULL)
StrFreeStrings(pDSD->ppszStrings), pDSD->ppszStrings = NULL;
char szADomainLine[ADOMAIN_LINE_MAX] = "";
while (MscGetConfigLine(szADomainLine, sizeof(szADomainLine) - 1, pDSD->pDBFile) != NULL) { /* [i_a] */
char **ppszStrings = StrGetTabLineStrings(szADomainLine);
if (ppszStrings == NULL)
continue;
int iFieldsCount = StrStringsCount(ppszStrings);
if (iFieldsCount >= adomMax)
return pDSD->ppszStrings = ppszStrings;
StrFreeStrings(ppszStrings);
}
return NULL;
}