* More license declarations
[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 "room_ops.h"
55 #include "user_ops.h"
56 #include "policy.h"
57 #include "database.h"
58 #include "msgbase.h"
59 #include "serv_network.h"       /* Needed for defenition of FilterList */
60
61
62 #include "ctdl_module.h"
63
64
65 /*
66  * This handler detects whether an incoming network message is from some
67  * moron user who the site operator has elected to filter out.  If a match
68  * is found, the message is rejected.
69  */
70 int filter_the_idiots(struct CtdlMessage *msg, char *target_room) {
71         FilterList *fptr;
72         int zap_user = 0;
73         int zap_room = 0;
74         int zap_node = 0;
75
76         if ( (msg == NULL) || (filterlist == NULL) ) {
77                 return(0);
78         }
79
80         for (fptr = filterlist; fptr != NULL; fptr = fptr->next) {
81
82                 zap_user = 0;
83                 zap_room = 0;
84                 zap_node = 0;
85
86                 if (msg->cm_fields['A'] != NULL) {
87                         if ( (!strcasecmp(msg->cm_fields['A'], fptr->fl_user))
88                            || (fptr->fl_user[0] == 0) ) {
89                                 zap_user = 1;
90                         }
91                 }
92
93                 if (msg->cm_fields['C'] != NULL) {
94                         if ( (!strcasecmp(msg->cm_fields['C'], fptr->fl_room))
95                            || (fptr->fl_room[0] == 0) ) {
96                                 zap_room = 1;
97                         }
98                 }
99
100                 if (msg->cm_fields['O'] != NULL) {
101                         if ( (!strcasecmp(msg->cm_fields['O'], fptr->fl_room))
102                            || (fptr->fl_room[0] == 0) ) {
103                                 zap_room = 1;
104                         }
105                 }
106
107                 if (msg->cm_fields['N'] != NULL) {
108                         if ( (!strcasecmp(msg->cm_fields['N'], fptr->fl_node))
109                            || (fptr->fl_node[0] == 0) ) {
110                                 zap_node = 1;
111                         }
112                 }
113         
114                 if (zap_user + zap_room + zap_node == 3) return(1);
115
116         }
117
118         return(0);
119 }
120
121
122 CTDL_MODULE_INIT(netfilter)
123 {
124         if (!threading)
125         {
126                 CtdlRegisterNetprocHook(filter_the_idiots);
127         }
128         
129         /* return our Subversion id for the Log */
130         return "$Id$";
131 }