a32a0e06c539ff8e5411404dc4119b9513448433
[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-2009 by the citadel.org team
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "sysdep.h"
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <pwd.h>
30 #include <errno.h>
31 #include <sys/types.h>
32
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
35 # include <time.h>
36 #else
37 # if HAVE_SYS_TIME_H
38 #  include <sys/time.h>
39 # else
40 #  include <time.h>
41 # endif
42 #endif
43
44 #include <sys/wait.h>
45 #include <string.h>
46 #include <limits.h>
47 #include <libcitadel.h>
48 #include "citadel.h"
49 #include "server.h"
50 #include "citserver.h"
51 #include "support.h"
52 #include "config.h"
53 #include "control.h"
54 #include "user_ops.h"
55 #include "database.h"
56 #include "msgbase.h"
57 #include "serv_network.h"       /* Needed for defenition of FilterList */
58
59
60 #include "ctdl_module.h"
61
62
63 /*
64  * This handler detects whether an incoming network message is from some
65  * moron user who the site operator has elected to filter out.  If a match
66  * is found, the message is rejected.
67  */
68 int filter_the_idiots(struct CtdlMessage *msg, char *target_room) {
69         FilterList *fptr;
70         int zap_user = 0;
71         int zap_room = 0;
72         int zap_node = 0;
73
74         if ( (msg == NULL) || (filterlist == NULL) ) {
75                 return(0);
76         }
77
78         for (fptr = filterlist; fptr != NULL; fptr = fptr->next) {
79
80                 zap_user = 0;
81                 zap_room = 0;
82                 zap_node = 0;
83
84                 if (msg->cm_fields['A'] != NULL) {
85                         if ( (!strcasecmp(msg->cm_fields['A'], fptr->fl_user))
86                            || (fptr->fl_user[0] == 0) ) {
87                                 zap_user = 1;
88                         }
89                 }
90
91                 if (msg->cm_fields['C'] != NULL) {
92                         if ( (!strcasecmp(msg->cm_fields['C'], fptr->fl_room))
93                            || (fptr->fl_room[0] == 0) ) {
94                                 zap_room = 1;
95                         }
96                 }
97
98                 if (msg->cm_fields['O'] != NULL) {
99                         if ( (!strcasecmp(msg->cm_fields['O'], fptr->fl_room))
100                            || (fptr->fl_room[0] == 0) ) {
101                                 zap_room = 1;
102                         }
103                 }
104
105                 if (msg->cm_fields['N'] != NULL) {
106                         if ( (!strcasecmp(msg->cm_fields['N'], fptr->fl_node))
107                            || (fptr->fl_node[0] == 0) ) {
108                                 zap_node = 1;
109                         }
110                 }
111         
112                 if (zap_user + zap_room + zap_node == 3) return(1);
113
114         }
115
116         return(0);
117 }
118
119
120 CTDL_MODULE_INIT(netfilter)
121 {
122         if (!threading)
123         {
124                 CtdlRegisterNetprocHook(filter_the_idiots);
125         }
126         
127         /* return our Subversion id for the Log */
128         return "$Id$";
129 }