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
|
/*
kbs - Simple, easily fooled, POP3 spam filter
Copyright (C) 2003 Ian Cowburn (ianc@noddybox.demon.co.uk)
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
-------------------------------------------------------------------------
Rules database
*/
static const char id[]="$Id$";
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <ctype.h>
#include "global.h"
#include "dbase.h"
#include "dstring.h"
#include "config.h"
#include "debug.h"
#include "util.h"
static const char header_id[]=KBS_DBASE_H;
/* ---------------------------------------- TYPES
*/
struct Domain
{
RE_Expression name;
int def_block;
int no_user;
int no_block;
int no_allow;
int no_allow_to;
char **user;
RE_Expression *block;
RE_Expression *allow;
RE_Expression *allow_to;
Domain *next;
Domain *prev;
};
/* ---------------------------------------- GLOBALS
*/
static Domain *head;
static Domain *tail;
static int no_trusted_users=0;
static int no_trusted_domains=0;
static int no_blacklist=0;
static char **trusted_user=NULL;
static char **trusted_domain=NULL;
static RE_Expression *blacklist=NULL;
static DString reason=NULL;
/* ---------------------------------------- PRVIVATE FUNCTIONS
*/
static int IsTrustedUser(const char *username, const char *domain)
{
char *p;
int f;
int res=FALSE;
p=CopyStr(username);
p=CatStr(p,"@");
p=CatStr(p,domain);
for(f=0;f<no_trusted_users && !res;f++)
{
if (strcasecmp(p,trusted_user[f])==0)
{
res=TRUE;
}
}
free(p);
return res;
}
static int IsTrustedDomain(const char *domain)
{
int f;
for(f=0;f<no_trusted_domains;f++)
{
if (strcasecmp(domain,trusted_domain[f])==0)
return TRUE;
}
return FALSE;
}
static const Domain *GetDomain(const char *domain)
{
Domain *dom;
dom=head;
while(dom)
{
if (RESearch(dom->name,domain))
return dom;
dom=dom->next;
}
return NULL;
}
static void DeJunk(DString ds, const char *p)
{
int last_ws=FALSE;
while(*p)
{
if (isspace(*p))
{
if (!last_ws)
DSAddChar(ds,' ');
last_ws=TRUE;
}
else
last_ws=FALSE;
if (isalnum(*p))
{
DSAddChar(ds,*p);
}
p++;
}
}
/* ---------------------------------------- INTERFACES
*/
Domain *DBNewDomain(RE_Expression name)
{
Domain *dom;
dom=Malloc(sizeof *dom);
dom->name=name;
dom->def_block=FALSE;
dom->no_user=0;
dom->no_block=0;
dom->no_allow=0;
dom->no_allow_to=0;
dom->user=NULL;
dom->block=NULL;
dom->allow=NULL;
dom->allow_to=NULL;
if (tail)
tail->next=dom;
else
head=tail=dom;
dom->prev=tail;
dom->next=NULL;
return dom;
}
void DBDefault(Domain *domain, int block)
{
domain->def_block=block;
}
void DBBlockUser(Domain *domain, const char *username)
{
domain->no_user++;
domain->user=Realloc(domain->user,sizeof(char *)*domain->no_user);
domain->user[domain->no_user-1]=CopyStr(username);
}
void DBAllowSubject(Domain *domain, RE_Expression re)
{
domain->no_allow++;
domain->allow=Realloc(domain->allow,sizeof(RE_Expression)*domain->no_allow);
domain->allow[domain->no_allow-1]=re;
}
void DBBlockSubject(Domain *domain, RE_Expression re)
{
domain->no_block++;
domain->block=Realloc(domain->block,sizeof(RE_Expression)*domain->no_block);
domain->block[domain->no_block-1]=re;
}
void DBAllowTo(Domain *domain, RE_Expression re)
{
domain->no_allow_to++;
domain->allow_to=Realloc(domain->allow_to,
sizeof(RE_Expression)*domain->no_allow_to);
domain->allow_to[domain->no_allow_to-1]=re;
}
void DBTrustedUser(const char *username)
{
no_trusted_users++;
trusted_user=Realloc(trusted_user,sizeof(char *)*no_trusted_users);
trusted_user[no_trusted_users-1]=CopyStr(username);
}
void DBTrustedDomain(const char *domain)
{
no_trusted_domains++;
trusted_domain=Realloc(trusted_domain,sizeof(char *)*no_trusted_domains);
trusted_domain[no_trusted_domains-1]=CopyStr(domain);
}
void DBBlacklist(RE_Expression re)
{
no_blacklist++;
blacklist=Realloc(blacklist,sizeof(RE_Expression)*no_blacklist);
blacklist[no_blacklist-1]=re;
}
int DBBlockMessage(const POP3Message *msg)
{
static const char *html="text/html";
DString ds;
const Domain *dom;
int f;
int show;
KBSDEBUG(("msg.id=%d\n",msg->id));
KBSDEBUG(("msg.to='%s'\n",msg->to));
KBSDEBUG(("msg.from='%s'\n",msg->from));
KBSDEBUG(("msg.from_uname='%s'\n",msg->from_uname));
KBSDEBUG(("msg.from_domain='%s'\n",msg->from_domain));
KBSDEBUG(("msg.subject='%s'\n",msg->subject));
KBSDEBUG(("msg.content_type='%s'\n",msg->content_type));
show=ConfigInt(CONFIG_SHOWMATCH);
reason=DSReset(reason);
if (IsTrustedDomain(msg->from_domain))
{
KBSDEBUG(("**** ALLOWED - trusted domain '%s'\n",msg->from_domain));
return FALSE;
}
if (IsTrustedUser(msg->from_uname,msg->from_domain))
{
KBSDEBUG(("**** ALLOWED - trusted user '%s@%s'\n",
msg->from_uname,msg->from_domain));
return FALSE;
}
if (ConfigInt(CONFIG_BLOCKHTML) &&
(strncmp(msg->content_type,html,strlen(html))==0 ||
strcmp(msg->content_type,"UNKNOWN")==0))
{
KBSDEBUG(("**** BLOCKED - content_type '%s'\n",msg->content_type));
DSAddCP(reason,"HTML message or unknown content type");
return TRUE;
}
for(f=0;f<no_blacklist;f++)
{
if (RESearch(blacklist[f],msg->from_domain))
{
if (show)
{
DSAddCP(reason,"blacklisted - ");
DSAddCP(reason,REGetExpression(blacklist[f]));
}
else
DSAddCP(reason,"blacklisted");
KBSDEBUG(("**** BLOCKED - blacklisted '%s'\n",msg->from_domain));
return TRUE;
}
}
if (!(dom=GetDomain(msg->from_domain)))
{
KBSDEBUG(("**** ALLOWED - no domain for '%s'\n",msg->from_domain));
return FALSE;
}
ds=DSInit();
if (ConfigInt(CONFIG_DEJUNK))
DeJunk(ds,msg->subject);
else
DSAddCP(ds,msg->subject);
for(f=0;f<dom->no_allow;f++)
{
if (RESearch(dom->allow[f],ds->text))
{
KBSDEBUG(("**** ALLOWED - allowed subject '%s'\n",ds->text));
DSFree(ds);
return FALSE;
}
}
if (dom->no_allow_to)
{
int found=FALSE;
int f;
for(f=0;f<dom->no_allow_to && !found;f++)
if (RESearch(dom->allow_to[f],msg->to))
found=TRUE;
if (!found)
{
if (show)
{
DSAddCP(reason,"disallowed to address - ");
DSAddCP(reason,msg->to);
}
else
DSAddCP(reason,"disallowed to address");
KBSDEBUG(("**** BLOCKED - bad to address '%s'\n",msg->to));
DSFree(ds);
return TRUE;
}
}
for(f=0;f<dom->no_user;f++)
{
int res;
if (ConfigInt(CONFIG_CASESENSE))
res=strcmp(dom->user[f],msg->from_uname);
else
res=strcasecmp(dom->user[f],msg->from_uname);
if (res==0)
{
DSFree(ds);
if (show)
{
DSAddCP(reason,"disallowed name - ");
DSAddCP(reason,dom->user[f]);
}
else
DSAddCP(reason,"disallowed name");
KBSDEBUG(("**** BLOCKED - bad from username '%s'\n",
msg->from_uname));
return TRUE;
}
}
for(f=0;f<dom->no_block;f++)
{
if (RESearch(dom->block[f],ds->text))
{
if (show)
{
DSAddCP(reason,"disallowed subject - ");
DSAddCP(reason,REGetExpression(dom->block[f]));
}
else
DSAddCP(reason,"disallowed subject");
DSFree(ds);
KBSDEBUG(("**** BLOCKED - bad subject '%s'\n",ds->text));
return TRUE;
}
}
if (dom->def_block)
KBSDEBUG(("**** BLOCKED - default block\n"));
else
KBSDEBUG(("**** ALLOWED - default no block\n"));
DSAddCP(reason,"default block");
DSFree(ds);
return dom->def_block;
}
const char *DBBlockReason(void)
{
return reason->text;
}
void DBClose(void)
{
Domain *d;
int f;
DSFree(reason);
for(f=0;f<no_trusted_domains;f++)
free(trusted_domain[f]);
free(trusted_domain);
for(f=0;f<no_trusted_users;f++)
free(trusted_user[f]);
free(trusted_user);
for(f=0;f<no_blacklist;f++)
REFree(blacklist[f]);
free(blacklist);
d=head;
while(d)
{
Domain *t;
t=d;
d=d->next;
for(f=0;f<t->no_user;f++)
free(t->user[f]);
free(t->user);
for(f=0;f<t->no_block;f++)
REFree(t->block[f]);
free(t->block);
for(f=0;f<t->no_allow;f++)
REFree(t->allow[f]);
free(t->allow);
for(f=0;f<t->no_allow_to;f++)
REFree(t->allow_to[f]);
free(t->allow_to);
free(t);
}
no_trusted_users=0;
no_trusted_domains=0;
head=tail=NULL;
trusted_user=NULL;
trusted_domain=NULL;
}
void DBDump(void)
{
Domain *d;
int f;
for(f=0;f<no_trusted_domains;f++)
KBSDEBUG(("trusted_domain[%d]='%s'\n",f,trusted_domain[f]));
for(f=0;f<no_trusted_users;f++)
KBSDEBUG(("trusted_user[%d]='%s'\n",f,trusted_user[f]));
for(f=0;f<no_blacklist;f++)
KBSDEBUG(("blacklist[%d]='%s'\n",f,blacklist[f]));
d=head;
while(d)
{
KBSDEBUG(("DOMAIN '%s' default:%s\n",
REGetExpression(d->name),d->def_block ? "block":"allow"));
for(f=0;f<d->no_user;f++)
KBSDEBUG((" user[%d]='%s'\n",
f,d->user[f]));
for(f=0;f<d->no_block;f++)
KBSDEBUG((" block[%d]='%s'\n",
f,REGetExpression(d->block[f])));
for(f=0;f<d->no_allow;f++)
KBSDEBUG((" allow[%d]='%s'\n",
f,REGetExpression(d->allow[f])));
for(f=0;f<d->no_allow_to;f++)
KBSDEBUG((" allow_to[%d]='%s'\n",
f,REGetExpression(d->allow_to[f])));
d=d->next;
}
}
/* END OF FILE */
|