-
Notifications
You must be signed in to change notification settings - Fork 6
/
FINGSvr.cpp
442 lines (346 loc) · 12.5 KB
/
FINGSvr.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
/*
* 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 <davidel@xmailserver.org>
*
*/
#include "SysInclude.h"
#include "SysDep.h"
#include "SvrDefines.h"
#include "ShBlocks.h"
#include "SList.h"
#include "BuffSock.h"
#include "SSLBind.h"
#include "SSLConfig.h"
#include "StrUtils.h"
#include "ResLocks.h"
#include "MiscUtils.h"
#include "SvrUtils.h"
#include "UsrUtils.h"
#include "ExtAliases.h"
#include "UsrMailList.h"
#include "MessQueue.h"
#include "SMTPUtils.h"
#include "MailConfig.h"
#include "AppDefines.h"
#include "MailSvr.h"
#include "FINGSvr.h"
#define FING_IPMAP_FILE "finger.ipmap.tab"
#define FING_LOG_FILE "finger"
static int FINGLogEnabled(SHB_HANDLE hShbFING, FINGConfig *pFINGCfg = NULL);
static int FINGDumpUser(char const *pszUser, char const *pszDomain,
BSOCK_HANDLE hBSock, FINGConfig *pFINGCfg);
static int FINGCheckPeerIP(SYS_SOCKET SockFD)
{
char szIPMapFile[SYS_MAX_PATH] = "";
CfgGetRootPath(szIPMapFile, sizeof(szIPMapFile));
StrNCat(szIPMapFile, FING_IPMAP_FILE, sizeof(szIPMapFile));
SysLogMessage(LOG_LEV_DEBUG, "Going to look at config file: '%s'\n", szIPMapFile);
if (SysExistFile(szIPMapFile)) {
SYS_INET_ADDR PeerInfo;
ZeroData(PeerInfo); /* [i_a] */
if (SysGetPeerInfo(SockFD, PeerInfo) < 0)
return ErrGetErrorCode();
if (MscCheckAllowedIP(szIPMapFile, PeerInfo, true) < 0)
return ErrGetErrorCode();
}
return 0;
}
static FINGConfig *FINGGetConfigCopy(SHB_HANDLE hShbFING)
{
FINGConfig *pFINGCfg = (FINGConfig *) ShbLock(hShbFING);
if (pFINGCfg == NULL)
return NULL;
FINGConfig *pFINGCfgCopy = (FINGConfig *) SysAlloc(sizeof(FINGConfig));
if (pFINGCfgCopy != NULL)
memcpy(pFINGCfgCopy, pFINGCfg, sizeof(FINGConfig));
ShbUnlock(hShbFING);
return pFINGCfgCopy;
}
static int FINGLogEnabled(SHB_HANDLE hShbFING, FINGConfig *pFINGCfg)
{
int iDoUnlock = 0;
if (pFINGCfg == NULL) {
if ((pFINGCfg = (FINGConfig *) ShbLock(hShbFING)) == NULL)
return ErrGetErrorCode();
++iDoUnlock;
}
unsigned long ulFlags = pFINGCfg->ulFlags;
if (iDoUnlock)
ShbUnlock(hShbFING);
return (ulFlags & FINGF_LOG_ENABLED) ? 1 : 0;
}
static int FINGLogSession(char const *pszSockHost, char const *pszSockDomain,
SYS_INET_ADDR & PeerInfo, char const *pszQuery)
{
char szTime[256] = "";
MscGetTimeNbrString(szTime, sizeof(szTime) - 1);
RLCK_HANDLE hResLock = RLckLockEX(SVR_LOGS_DIR SYS_SLASH_STR FING_LOG_FILE);
if (hResLock == INVALID_RLCK_HANDLE)
return ErrGetErrorCode();
char szIP[128] = "???.???.???.???";
MscFileLog(FING_LOG_FILE, "\"%s\""
"\t\"%s\""
"\t\"%s\""
"\t\"%s\""
"\t\"%s\""
"\n", pszSockHost, pszSockDomain, SysInetNToA(PeerInfo, szIP, sizeof(szIP)),
szTime, pszQuery);
RLckUnlockEX(hResLock);
return 0;
}
static int FINGDumpMailingList(UserInfo * pUI, BSOCK_HANDLE hBSock, FINGConfig * pFINGCfg)
{
USRML_HANDLE hUsersDB = UsrMLOpenDB(pUI);
if (hUsersDB == INVALID_USRML_HANDLE)
return ErrGetErrorCode();
/* Mailing list scan */
MLUserInfo *pMLUI = UsrMLGetFirstUser(hUsersDB);
for (; pMLUI != NULL; pMLUI = UsrMLGetNextUser(hUsersDB)) {
char szUser[MAX_ADDR_NAME] = "";
char szDomain[MAX_ADDR_NAME] = "";
if (USmtpSplitEmailAddr(pMLUI->pszAddress, szUser, szDomain) < 0) {
ErrorPush();
UsrMLFreeUser(pMLUI);
UsrMLCloseDB(hUsersDB);
return ErrorPop();
}
/* Dump user */
FINGDumpUser(szUser, szDomain, hBSock, pFINGCfg);
UsrMLFreeUser(pMLUI);
}
UsrMLCloseDB(hUsersDB);
return 0;
}
static int FINGDumpUser(char const *pszUser, char const *pszDomain,
BSOCK_HANDLE hBSock, FINGConfig *pFINGCfg)
{
/* Lookup user */
char szRealAddr[MAX_ADDR_NAME] = "";
UserInfo *pUI = UsrGetUserByNameOrAlias(pszDomain, pszUser, szRealAddr);
if (pUI != NULL) {
if (UsrGetUserType(pUI) == usrTypeUser) {
/* Local user case */
char *pszRealName = UsrGetUserInfoVar(pUI, "RealName");
char *pszHomePage = UsrGetUserInfoVar(pUI, "HomePage");
char szRespBuffer[2048] = "";
SysSNPrintf(szRespBuffer, sizeof(szRespBuffer),
"EMail : %s\r\n"
" Real Name : %s\r\n"
" Home Page : %s",
szRealAddr,
(pszRealName != NULL) ? pszRealName : "??",
(pszHomePage != NULL) ? pszHomePage : "??");
BSckSendString(hBSock, szRespBuffer, pFINGCfg->iTimeout);
SysFree(pszRealName);
SysFree(pszHomePage);
} else {
/* Local mailing list case */
FINGDumpMailingList(pUI, hBSock, pFINGCfg);
}
UsrFreeUserInfo(pUI);
} else {
ErrSetErrorCode(ERR_USER_NOT_FOUND);
return ERR_USER_NOT_FOUND;
}
return 0;
}
static int FINGProcessQuery(char const *pszQuery, BSOCK_HANDLE hBSock,
FINGConfig * pFINGCfg, char const *pszSockDomain,
SVRCFG_HANDLE hSvrConfig)
{
/* Check for verbose query */
int iVerbose = 0;
if (pszQuery[0] == '/') {
if (toupper(pszQuery[1]) != 'W') {
BSckSendString(hBSock, "Invalid query", pFINGCfg->iTimeout);
ErrSetErrorCode(ERR_FINGER_QUERY_FORMAT);
return ERR_FINGER_QUERY_FORMAT;
}
++iVerbose;
pszQuery += 2;
}
/* Discard spaces */
for (; *pszQuery == ' '; pszQuery++);
if (*pszQuery == '\0') {
BSckSendString(hBSock, "Empty query not allowed", pFINGCfg->iTimeout);
ErrSetErrorCode(ERR_FINGER_QUERY_FORMAT);
return ERR_FINGER_QUERY_FORMAT;
}
/* Split user-domain */
char szUser[MAX_ADDR_NAME] = "";
char szDomain[MAX_ADDR_NAME] = "";
if (strchr(pszQuery, '@') != NULL) {
if (USmtpSplitEmailAddr(pszQuery, szUser, szDomain) < 0) {
ErrorPush();
BSckSendString(hBSock, "Invalid query", pFINGCfg->iTimeout);
return ErrorPop();
}
} else
StrSNCpy(szUser, pszQuery);
/* Check if indirect query */
if (strchr(szDomain, '@') != NULL) {
BSckSendString(hBSock, "Indirect query not allowed", pFINGCfg->iTimeout);
ErrSetErrorCode(ERR_FINGER_QUERY_FORMAT);
return ERR_FINGER_QUERY_FORMAT;
}
/* Setup domain name in case of username only query */
if (IsEmptyString(szDomain)) {
if (SvrConfigVar("POP3Domain", szDomain, sizeof(szDomain), hSvrConfig) < 0) {
if (strlen(pszSockDomain) == 0) {
if (SvrConfigVar
("RootDomain", szDomain, sizeof(szDomain), hSvrConfig) < 0) {
ErrorPush();
BSckSendString(hBSock, "User not found",
pFINGCfg->iTimeout);
return ErrorPop();
}
} else
StrSNCpy(szDomain, pszSockDomain);
}
}
/* Dump user */
if (FINGDumpUser(szUser, szDomain, hBSock, pFINGCfg) < 0) {
ErrorPush();
BSckSendString(hBSock, "User not found", pFINGCfg->iTimeout);
return ErrorPop();
}
return 0;
}
static int FINGHandleSession(ThreadConfig const *pThCfg, BSOCK_HANDLE hBSock)
{
FINGConfig *pFINGCfg = FINGGetConfigCopy(pThCfg->hThShb);
/* Get client socket info */
SYS_INET_ADDR PeerInfo;
ZeroData(PeerInfo); /* [i_a] */
if (SysGetPeerInfo(BSckGetAttachedSocket(hBSock), PeerInfo) < 0) {
ErrorPush();
BSckSendString(hBSock, ErrGetErrorString(ErrorFetch()), pFINGCfg->iTimeout);
SysFree(pFINGCfg);
SysLogMessage(LOG_LEV_ERROR, "%s\n", ErrGetErrorString(ErrorFetch()));
return ErrorPop();
}
/* Get server socket FQDN */
char szSvrFQDN[MAX_HOST_NAME] = "";
if (MscGetSockHost(BSckGetAttachedSocket(hBSock), szSvrFQDN,
sizeof(szSvrFQDN)) < 0) {
ErrorPush();
BSckSendString(hBSock, ErrGetErrorString(ErrorFetch()), pFINGCfg->iTimeout);
SysFree(pFINGCfg);
SysLogMessage(LOG_LEV_ERROR, "%s\n", ErrGetErrorString(ErrorFetch()));
return ErrorPop();
}
char szSockHost[MAX_HOST_NAME] = "";
char szSockDomain[MAX_HOST_NAME] = "";
MscSplitFQDN(szSvrFQDN, szSockHost, sizeof(szSockHost),
szSockDomain, sizeof(szSockDomain));
char szIP[128] = "???.???.???.???";
SysLogMessage(LOG_LEV_MESSAGE, "FINGER client connection from [%s]\n",
SysInetNToA(PeerInfo, szIP, sizeof(szIP)));
char szQuery[1024] = "";
if (BSckGetString(hBSock, szQuery, sizeof(szQuery) - 1, pFINGCfg->iTimeout) != NULL &&
MscCmdStringCheck(szQuery) == 0) {
/* Log FINGER question */
if (FINGLogEnabled(hShbFING))
FINGLogSession(szSockHost, szSockDomain, PeerInfo, szQuery);
SysLogMessage(LOG_LEV_MESSAGE, "FINGER query [%s] : \"%s\"\n",
SysInetNToA(PeerInfo, szIP, sizeof(szIP)), szQuery);
SVRCFG_HANDLE hSvrConfig = SvrGetConfigHandle();
FINGProcessQuery(szQuery, hBSock, pFINGCfg, szSockDomain, hSvrConfig);
if (hSvrConfig != INVALID_SVRCFG_HANDLE)
SvrReleaseConfigHandle(hSvrConfig);
}
SysFree(pFINGCfg);
SysLogMessage(LOG_LEV_MESSAGE, "FINGER client exit [%s]\n",
SysInetNToA(PeerInfo, szIP, sizeof(szIP)));
return 0;
}
unsigned int FINGClientThread(void *pThreadData)
{
ThreadCreateCtx *pThCtx = (ThreadCreateCtx *) pThreadData;
/* Check IP permission */
if (FINGCheckPeerIP(pThCtx->SockFD) < 0) {
ErrorPush();
SysLogMessage(LOG_LEV_ERROR, "%s\n", ErrGetErrorString());
SysCloseSocket(pThCtx->SockFD);
SysFree(pThCtx);
return ErrorPop();
}
/* Link socket to the bufferer */
BSOCK_HANDLE hBSock = BSckAttach(pThCtx->SockFD);
if (hBSock == INVALID_BSOCK_HANDLE) {
ErrorPush();
SysCloseSocket(pThCtx->SockFD);
SysFree(pThCtx);
return ErrorPop();
}
/*
* Do we need to switch to TLS?
*/
if (pThCtx->pThCfg->ulFlags & THCF_USE_SSL) {
int iError;
SslServerBind SSLB;
SslBindEnv SslE;
if (CSslBindSetup(&SSLB) < 0) {
ErrorPush();
BSckDetach(hBSock, 1);
SysFree(pThCtx);
return ErrorPop();
}
ZeroData(SslE);
iError = BSslBindServer(hBSock, &SSLB, MscSslEnvCB, &SslE);
CSslBindCleanup(&SSLB);
if (iError < 0) {
ErrorPush();
BSckDetach(hBSock, 1);
SysFree(pThCtx);
return ErrorPop();
}
/*
* We may want to add verify code here ...
*/
SysFree(SslE.pszIssuer);
SysFree(SslE.pszSubject);
}
/* Increase threads count */
FINGConfig *pFINGCfg = (FINGConfig *) ShbLock(pThCtx->pThCfg->hThShb);
if (pFINGCfg == NULL) {
ErrorPush();
SysLogMessage(LOG_LEV_ERROR, "%s\n", ErrGetErrorString());
BSckDetach(hBSock, 1);
SysFree(pThCtx);
return ErrorPop();
}
++pFINGCfg->lThreadCount;
ShbUnlock(pThCtx->pThCfg->hThShb);
/* Handle client session */
FINGHandleSession(pThCtx->pThCfg, hBSock);
/* Decrease threads count */
pFINGCfg = (FINGConfig *) ShbLock(pThCtx->pThCfg->hThShb);
if (pFINGCfg == NULL) {
ErrorPush();
SysLogMessage(LOG_LEV_ERROR, "%s\n", ErrGetErrorString());
return ErrorPop();
}
--pFINGCfg->lThreadCount;
ShbUnlock(pThCtx->pThCfg->hThShb);
/* Unlink socket from the bufferer and close it */
BSckDetach(hBSock, 1);
SysFree(pThCtx);
return 0;
}