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