a60af6dde489448b2336a1d11ae51116185a66e5
[citadel.git] / citadel / modules / netfilter / serv_netfilter.c
1 /*
2  * $Id$
3  * 
4  * A server-side module for Citadel designed to filter idiots off the network.
5  * 
6  * Copyright (c) 2002 / released under the GNU General Public License
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <pwd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <sys/wait.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <libcitadel.h>
34 #include "citadel.h"
35 #include "server.h"
36 #include "citserver.h"
37 #include "support.h"
38 #include "config.h"
39 #include "control.h"
40 #include "room_ops.h"
41 #include "user_ops.h"
42 #include "policy.h"
43 #include "database.h"
44 #include "msgbase.h"
45 #include "serv_network.h"       /* Needed for defenition of FilterList */
46
47
48 #include "ctdl_module.h"
49
50
51 /*
52  * This handler detects whether an incoming network message is from some
53  * moron user who the site operator has elected to filter out.  If a match
54  * is found, the message is rejected.
55  */
56 int filter_the_idiots(struct CtdlMessage *msg, char *target_room) {
57         FilterList *fptr;
58         int zap_user = 0;
59         int zap_room = 0;
60         int zap_node = 0;
61
62         if ( (msg == NULL) || (filterlist == NULL) ) {
63                 return(0);
64         }
65
66         for (fptr = filterlist; fptr != NULL; fptr = fptr->next) {
67
68                 zap_user = 0;
69                 zap_room = 0;
70                 zap_node = 0;
71
72                 if (msg->cm_fields['A'] != NULL) {
73                         if ( (!strcasecmp(msg->cm_fields['A'], fptr->fl_user))
74                            || (fptr->fl_user[0] == 0) ) {
75                                 zap_user = 1;
76                         }
77                 }
78
79                 if (msg->cm_fields['C'] != NULL) {
80                         if ( (!strcasecmp(msg->cm_fields['C'], fptr->fl_room))
81                            || (fptr->fl_room[0] == 0) ) {
82                                 zap_room = 1;
83                         }
84                 }
85
86                 if (msg->cm_fields['O'] != NULL) {
87                         if ( (!strcasecmp(msg->cm_fields['O'], fptr->fl_room))
88                            || (fptr->fl_room[0] == 0) ) {
89                                 zap_room = 1;
90                         }
91                 }
92
93                 if (msg->cm_fields['N'] != NULL) {
94                         if ( (!strcasecmp(msg->cm_fields['N'], fptr->fl_node))
95                            || (fptr->fl_node[0] == 0) ) {
96                                 zap_node = 1;
97                         }
98                 }
99         
100                 if (zap_user + zap_room + zap_node == 3) return(1);
101
102         }
103
104         return(0);
105 }
106
107
108 CTDL_MODULE_INIT(netfilter)
109 {
110         if (!threading)
111         {
112                 CtdlRegisterNetprocHook(filter_the_idiots);
113         }
114         
115         /* return our Subversion id for the Log */
116         return "$Id$";
117 }